Change use to type-based pool allocator in cselib.c.
[gcc.git] / gcc / alias.c
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2015 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 "tm.h"
25 #include "rtl.h"
26 #include "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "varasm.h"
38 #include "hashtab.h"
39 #include "hard-reg-set.h"
40 #include "function.h"
41 #include "flags.h"
42 #include "statistics.h"
43 #include "real.h"
44 #include "fixed-value.h"
45 #include "insn-config.h"
46 #include "expmed.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "calls.h"
50 #include "emit-rtl.h"
51 #include "stmt.h"
52 #include "expr.h"
53 #include "tm_p.h"
54 #include "regs.h"
55 #include "diagnostic-core.h"
56 #include "alloc-pool.h"
57 #include "cselib.h"
58 #include "hash-map.h"
59 #include "langhooks.h"
60 #include "timevar.h"
61 #include "dumpfile.h"
62 #include "target.h"
63 #include "dominance.h"
64 #include "cfg.h"
65 #include "cfganal.h"
66 #include "predict.h"
67 #include "basic-block.h"
68 #include "df.h"
69 #include "tree-ssa-alias.h"
70 #include "internal-fn.h"
71 #include "gimple-expr.h"
72 #include "is-a.h"
73 #include "gimple.h"
74 #include "gimple-ssa.h"
75 #include "rtl-iter.h"
76
77 /* The aliasing API provided here solves related but different problems:
78
79 Say there exists (in c)
80
81 struct X {
82 struct Y y1;
83 struct Z z2;
84 } x1, *px1, *px2;
85
86 struct Y y2, *py;
87 struct Z z2, *pz;
88
89
90 py = &x1.y1;
91 px2 = &x1;
92
93 Consider the four questions:
94
95 Can a store to x1 interfere with px2->y1?
96 Can a store to x1 interfere with px2->z2?
97 Can a store to x1 change the value pointed to by with py?
98 Can a store to x1 change the value pointed to by with pz?
99
100 The answer to these questions can be yes, yes, yes, and maybe.
101
102 The first two questions can be answered with a simple examination
103 of the type system. If structure X contains a field of type Y then
104 a store through a pointer to an X can overwrite any field that is
105 contained (recursively) in an X (unless we know that px1 != px2).
106
107 The last two questions can be solved in the same way as the first
108 two questions but this is too conservative. The observation is
109 that in some cases we can know which (if any) fields are addressed
110 and if those addresses are used in bad ways. This analysis may be
111 language specific. In C, arbitrary operations may be applied to
112 pointers. However, there is some indication that this may be too
113 conservative for some C++ types.
114
115 The pass ipa-type-escape does this analysis for the types whose
116 instances do not escape across the compilation boundary.
117
118 Historically in GCC, these two problems were combined and a single
119 data structure that was used to represent the solution to these
120 problems. We now have two similar but different data structures,
121 The data structure to solve the last two questions is similar to
122 the first, but does not contain the fields whose address are never
123 taken. For types that do escape the compilation unit, the data
124 structures will have identical information.
125 */
126
127 /* The alias sets assigned to MEMs assist the back-end in determining
128 which MEMs can alias which other MEMs. In general, two MEMs in
129 different alias sets cannot alias each other, with one important
130 exception. Consider something like:
131
132 struct S { int i; double d; };
133
134 a store to an `S' can alias something of either type `int' or type
135 `double'. (However, a store to an `int' cannot alias a `double'
136 and vice versa.) We indicate this via a tree structure that looks
137 like:
138 struct S
139 / \
140 / \
141 |/_ _\|
142 int double
143
144 (The arrows are directed and point downwards.)
145 In this situation we say the alias set for `struct S' is the
146 `superset' and that those for `int' and `double' are `subsets'.
147
148 To see whether two alias sets can point to the same memory, we must
149 see if either alias set is a subset of the other. We need not trace
150 past immediate descendants, however, since we propagate all
151 grandchildren up one level.
152
153 Alias set zero is implicitly a superset of all other alias sets.
154 However, this is no actual entry for alias set zero. It is an
155 error to attempt to explicitly construct a subset of zero. */
156
157 struct alias_set_traits : default_hashmap_traits
158 {
159 template<typename T>
160 static bool
161 is_empty (T &e)
162 {
163 return e.m_key == INT_MIN;
164 }
165
166 template<typename T>
167 static bool
168 is_deleted (T &e)
169 {
170 return e.m_key == (INT_MIN + 1);
171 }
172
173 template<typename T> static void mark_empty (T &e) { e.m_key = INT_MIN; }
174
175 template<typename T>
176 static void
177 mark_deleted (T &e)
178 {
179 e.m_key = INT_MIN + 1;
180 }
181 };
182
183 struct GTY(()) alias_set_entry_d {
184 /* The alias set number, as stored in MEM_ALIAS_SET. */
185 alias_set_type alias_set;
186
187 /* The children of the alias set. These are not just the immediate
188 children, but, in fact, all descendants. So, if we have:
189
190 struct T { struct S s; float f; }
191
192 continuing our example above, the children here will be all of
193 `int', `double', `float', and `struct S'. */
194 hash_map<int, int, alias_set_traits> *children;
195
196 /* Nonzero if would have a child of zero: this effectively makes this
197 alias set the same as alias set zero. */
198 bool has_zero_child;
199 /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
200 aggregate contaiing pointer.
201 This is used for a special case where we need an universal pointer type
202 compatible with all other pointer types. */
203 bool is_pointer;
204 /* Nonzero if is_pointer or if one of childs have has_pointer set. */
205 bool has_pointer;
206 };
207 typedef struct alias_set_entry_d *alias_set_entry;
208
209 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
210 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
211 static void record_set (rtx, const_rtx, void *);
212 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
213 machine_mode);
214 static rtx find_base_value (rtx);
215 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
216 static alias_set_entry get_alias_set_entry (alias_set_type);
217 static tree decl_for_component_ref (tree);
218 static int write_dependence_p (const_rtx,
219 const_rtx, machine_mode, rtx,
220 bool, bool, bool);
221
222 static void memory_modified_1 (rtx, const_rtx, void *);
223
224 /* Query statistics for the different low-level disambiguators.
225 A high-level query may trigger multiple of them. */
226
227 static struct {
228 unsigned long long num_alias_zero;
229 unsigned long long num_same_alias_set;
230 unsigned long long num_same_objects;
231 unsigned long long num_volatile;
232 unsigned long long num_dag;
233 unsigned long long num_universal;
234 unsigned long long num_disambiguated;
235 } alias_stats;
236
237
238 /* Set up all info needed to perform alias analysis on memory references. */
239
240 /* Returns the size in bytes of the mode of X. */
241 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
242
243 /* Cap the number of passes we make over the insns propagating alias
244 information through set chains.
245 ??? 10 is a completely arbitrary choice. This should be based on the
246 maximum loop depth in the CFG, but we do not have this information
247 available (even if current_loops _is_ available). */
248 #define MAX_ALIAS_LOOP_PASSES 10
249
250 /* reg_base_value[N] gives an address to which register N is related.
251 If all sets after the first add or subtract to the current value
252 or otherwise modify it so it does not point to a different top level
253 object, reg_base_value[N] is equal to the address part of the source
254 of the first set.
255
256 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
257 expressions represent three types of base:
258
259 1. incoming arguments. There is just one ADDRESS to represent all
260 arguments, since we do not know at this level whether accesses
261 based on different arguments can alias. The ADDRESS has id 0.
262
263 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
264 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
265 Each of these rtxes has a separate ADDRESS associated with it,
266 each with a negative id.
267
268 GCC is (and is required to be) precise in which register it
269 chooses to access a particular region of stack. We can therefore
270 assume that accesses based on one of these rtxes do not alias
271 accesses based on another of these rtxes.
272
273 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
274 Each such piece of memory has a separate ADDRESS associated
275 with it, each with an id greater than 0.
276
277 Accesses based on one ADDRESS do not alias accesses based on other
278 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
279 alias globals either; the ADDRESSes have Pmode to indicate this.
280 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
281 indicate this. */
282
283 static GTY(()) vec<rtx, va_gc> *reg_base_value;
284 static rtx *new_reg_base_value;
285
286 /* The single VOIDmode ADDRESS that represents all argument bases.
287 It has id 0. */
288 static GTY(()) rtx arg_base_value;
289
290 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
291 static int unique_id;
292
293 /* We preserve the copy of old array around to avoid amount of garbage
294 produced. About 8% of garbage produced were attributed to this
295 array. */
296 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
297
298 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
299 registers. */
300 #define UNIQUE_BASE_VALUE_SP -1
301 #define UNIQUE_BASE_VALUE_ARGP -2
302 #define UNIQUE_BASE_VALUE_FP -3
303 #define UNIQUE_BASE_VALUE_HFP -4
304
305 #define static_reg_base_value \
306 (this_target_rtl->x_static_reg_base_value)
307
308 #define REG_BASE_VALUE(X) \
309 (REGNO (X) < vec_safe_length (reg_base_value) \
310 ? (*reg_base_value)[REGNO (X)] : 0)
311
312 /* Vector indexed by N giving the initial (unchanging) value known for
313 pseudo-register N. This vector is initialized in init_alias_analysis,
314 and does not change until end_alias_analysis is called. */
315 static GTY(()) vec<rtx, va_gc> *reg_known_value;
316
317 /* Vector recording for each reg_known_value whether it is due to a
318 REG_EQUIV note. Future passes (viz., reload) may replace the
319 pseudo with the equivalent expression and so we account for the
320 dependences that would be introduced if that happens.
321
322 The REG_EQUIV notes created in assign_parms may mention the arg
323 pointer, and there are explicit insns in the RTL that modify the
324 arg pointer. Thus we must ensure that such insns don't get
325 scheduled across each other because that would invalidate the
326 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
327 wrong, but solving the problem in the scheduler will likely give
328 better code, so we do it here. */
329 static sbitmap reg_known_equiv_p;
330
331 /* True when scanning insns from the start of the rtl to the
332 NOTE_INSN_FUNCTION_BEG note. */
333 static bool copying_arguments;
334
335
336 /* The splay-tree used to store the various alias set entries. */
337 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
338 \f
339 /* Build a decomposed reference object for querying the alias-oracle
340 from the MEM rtx and store it in *REF.
341 Returns false if MEM is not suitable for the alias-oracle. */
342
343 static bool
344 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
345 {
346 tree expr = MEM_EXPR (mem);
347 tree base;
348
349 if (!expr)
350 return false;
351
352 ao_ref_init (ref, expr);
353
354 /* Get the base of the reference and see if we have to reject or
355 adjust it. */
356 base = ao_ref_base (ref);
357 if (base == NULL_TREE)
358 return false;
359
360 /* The tree oracle doesn't like bases that are neither decls
361 nor indirect references of SSA names. */
362 if (!(DECL_P (base)
363 || (TREE_CODE (base) == MEM_REF
364 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
365 || (TREE_CODE (base) == TARGET_MEM_REF
366 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
367 return false;
368
369 /* If this is a reference based on a partitioned decl replace the
370 base with a MEM_REF of the pointer representative we
371 created during stack slot partitioning. */
372 if (TREE_CODE (base) == VAR_DECL
373 && ! is_global_var (base)
374 && cfun->gimple_df->decls_to_pointers != NULL)
375 {
376 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
377 if (namep)
378 ref->base = build_simple_mem_ref (*namep);
379 }
380
381 ref->ref_alias_set = MEM_ALIAS_SET (mem);
382
383 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
384 is conservative, so trust it. */
385 if (!MEM_OFFSET_KNOWN_P (mem)
386 || !MEM_SIZE_KNOWN_P (mem))
387 return true;
388
389 /* If the base decl is a parameter we can have negative MEM_OFFSET in
390 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
391 here. */
392 if (MEM_OFFSET (mem) < 0
393 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
394 return true;
395
396 /* Otherwise continue and refine size and offset we got from analyzing
397 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
398
399 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
400 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
401
402 /* The MEM may extend into adjacent fields, so adjust max_size if
403 necessary. */
404 if (ref->max_size != -1
405 && ref->size > ref->max_size)
406 ref->max_size = ref->size;
407
408 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
409 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
410 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
411 && (ref->offset < 0
412 || (DECL_P (ref->base)
413 && (DECL_SIZE (ref->base) == NULL_TREE
414 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
415 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
416 ref->offset + ref->size)))))
417 return false;
418
419 return true;
420 }
421
422 /* Query the alias-oracle on whether the two memory rtx X and MEM may
423 alias. If TBAA_P is set also apply TBAA. Returns true if the
424 two rtxen may alias, false otherwise. */
425
426 static bool
427 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
428 {
429 ao_ref ref1, ref2;
430
431 if (!ao_ref_from_mem (&ref1, x)
432 || !ao_ref_from_mem (&ref2, mem))
433 return true;
434
435 return refs_may_alias_p_1 (&ref1, &ref2,
436 tbaa_p
437 && MEM_ALIAS_SET (x) != 0
438 && MEM_ALIAS_SET (mem) != 0);
439 }
440
441 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
442 such an entry, or NULL otherwise. */
443
444 static inline alias_set_entry
445 get_alias_set_entry (alias_set_type alias_set)
446 {
447 return (*alias_sets)[alias_set];
448 }
449
450 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
451 the two MEMs cannot alias each other. */
452
453 static inline int
454 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
455 {
456 return (flag_strict_aliasing
457 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
458 MEM_ALIAS_SET (mem2)));
459 }
460
461 /* Return true if the first alias set is a subset of the second. */
462
463 bool
464 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
465 {
466 alias_set_entry ase2;
467
468 /* Everything is a subset of the "aliases everything" set. */
469 if (set2 == 0)
470 return true;
471
472 /* Check if set1 is a subset of set2. */
473 ase2 = get_alias_set_entry (set2);
474 if (ase2 != 0
475 && (ase2->has_zero_child
476 || (ase2->children && ase2->children->get (set1))))
477 return true;
478
479 /* As a special case we consider alias set of "void *" to be both subset
480 and superset of every alias set of a pointer. This extra symmetry does
481 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
482 to return true on the following testcase:
483
484 void *ptr;
485 char **ptr2=(char **)&ptr;
486 *ptr2 = ...
487
488 Additionally if a set contains universal pointer, we consider every pointer
489 to be a subset of it, but we do not represent this explicitely - doing so
490 would require us to update transitive closure each time we introduce new
491 pointer type. This makes aliasing_component_refs_p to return true
492 on the following testcase:
493
494 struct a {void *ptr;}
495 char **ptr = (char **)&a.ptr;
496 ptr = ...
497
498 This makes void * truly universal pointer type. See pointer handling in
499 get_alias_set for more details. */
500 if (ase2 && ase2->has_pointer)
501 {
502 alias_set_entry ase1 = get_alias_set_entry (set1);
503
504 if (ase1 && ase1->is_pointer)
505 {
506 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
507 /* If one is ptr_type_node and other is pointer, then we consider
508 them subset of each other. */
509 if (set1 == voidptr_set || set2 == voidptr_set)
510 return true;
511 /* If SET2 contains universal pointer's alias set, then we consdier
512 every (non-universal) pointer. */
513 if (ase2->children && set1 != voidptr_set
514 && ase2->children->get (voidptr_set))
515 return true;
516 }
517 }
518 return false;
519 }
520
521 /* Return 1 if the two specified alias sets may conflict. */
522
523 int
524 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
525 {
526 alias_set_entry ase1;
527 alias_set_entry ase2;
528
529 /* The easy case. */
530 if (alias_sets_must_conflict_p (set1, set2))
531 return 1;
532
533 /* See if the first alias set is a subset of the second. */
534 ase1 = get_alias_set_entry (set1);
535 if (ase1 != 0
536 && ase1->children && ase1->children->get (set2))
537 {
538 ++alias_stats.num_dag;
539 return 1;
540 }
541
542 /* Now do the same, but with the alias sets reversed. */
543 ase2 = get_alias_set_entry (set2);
544 if (ase2 != 0
545 && ase2->children && ase2->children->get (set1))
546 {
547 ++alias_stats.num_dag;
548 return 1;
549 }
550
551 /* We want void * to be compatible with any other pointer without
552 really dropping it to alias set 0. Doing so would make it
553 compatible with all non-pointer types too.
554
555 This is not strictly necessary by the C/C++ language
556 standards, but avoids common type punning mistakes. In
557 addition to that, we need the existence of such universal
558 pointer to implement Fortran's C_PTR type (which is defined as
559 type compatible with all C pointers). */
560 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
561 {
562 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
563
564 /* If one of the sets corresponds to universal pointer,
565 we consider it to conflict with anything that is
566 or contains pointer. */
567 if (set1 == voidptr_set || set2 == voidptr_set)
568 {
569 ++alias_stats.num_universal;
570 return true;
571 }
572 /* If one of sets is (non-universal) pointer and the other
573 contains universal pointer, we also get conflict. */
574 if (ase1->is_pointer && set2 != voidptr_set
575 && ase2->children && ase2->children->get (voidptr_set))
576 {
577 ++alias_stats.num_universal;
578 return true;
579 }
580 if (ase2->is_pointer && set1 != voidptr_set
581 && ase1->children && ase1->children->get (voidptr_set))
582 {
583 ++alias_stats.num_universal;
584 return true;
585 }
586 }
587
588 ++alias_stats.num_disambiguated;
589
590 /* The two alias sets are distinct and neither one is the
591 child of the other. Therefore, they cannot conflict. */
592 return 0;
593 }
594
595 /* Return 1 if the two specified alias sets will always conflict. */
596
597 int
598 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
599 {
600 if (set1 == 0 || set2 == 0)
601 {
602 ++alias_stats.num_alias_zero;
603 return 1;
604 }
605 if (set1 == set2)
606 {
607 ++alias_stats.num_same_alias_set;
608 return 1;
609 }
610
611 return 0;
612 }
613
614 /* Return 1 if any MEM object of type T1 will always conflict (using the
615 dependency routines in this file) with any MEM object of type T2.
616 This is used when allocating temporary storage. If T1 and/or T2 are
617 NULL_TREE, it means we know nothing about the storage. */
618
619 int
620 objects_must_conflict_p (tree t1, tree t2)
621 {
622 alias_set_type set1, set2;
623
624 /* If neither has a type specified, we don't know if they'll conflict
625 because we may be using them to store objects of various types, for
626 example the argument and local variables areas of inlined functions. */
627 if (t1 == 0 && t2 == 0)
628 return 0;
629
630 /* If they are the same type, they must conflict. */
631 if (t1 == t2)
632 {
633 ++alias_stats.num_same_objects;
634 return 1;
635 }
636 /* Likewise if both are volatile. */
637 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
638 {
639 ++alias_stats.num_volatile;
640 return 1;
641 }
642
643 set1 = t1 ? get_alias_set (t1) : 0;
644 set2 = t2 ? get_alias_set (t2) : 0;
645
646 /* We can't use alias_sets_conflict_p because we must make sure
647 that every subtype of t1 will conflict with every subtype of
648 t2 for which a pair of subobjects of these respective subtypes
649 overlaps on the stack. */
650 return alias_sets_must_conflict_p (set1, set2);
651 }
652 \f
653 /* Return the outermost parent of component present in the chain of
654 component references handled by get_inner_reference in T with the
655 following property:
656 - the component is non-addressable, or
657 - the parent has alias set zero,
658 or NULL_TREE if no such parent exists. In the former cases, the alias
659 set of this parent is the alias set that must be used for T itself. */
660
661 tree
662 component_uses_parent_alias_set_from (const_tree t)
663 {
664 const_tree found = NULL_TREE;
665
666 while (handled_component_p (t))
667 {
668 switch (TREE_CODE (t))
669 {
670 case COMPONENT_REF:
671 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
672 found = t;
673 break;
674
675 case ARRAY_REF:
676 case ARRAY_RANGE_REF:
677 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
678 found = t;
679 break;
680
681 case REALPART_EXPR:
682 case IMAGPART_EXPR:
683 break;
684
685 case BIT_FIELD_REF:
686 case VIEW_CONVERT_EXPR:
687 /* Bitfields and casts are never addressable. */
688 found = t;
689 break;
690
691 default:
692 gcc_unreachable ();
693 }
694
695 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
696 found = t;
697
698 t = TREE_OPERAND (t, 0);
699 }
700
701 if (found)
702 return TREE_OPERAND (found, 0);
703
704 return NULL_TREE;
705 }
706
707
708 /* Return whether the pointer-type T effective for aliasing may
709 access everything and thus the reference has to be assigned
710 alias-set zero. */
711
712 static bool
713 ref_all_alias_ptr_type_p (const_tree t)
714 {
715 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
716 || TYPE_REF_CAN_ALIAS_ALL (t));
717 }
718
719 /* Return the alias set for the memory pointed to by T, which may be
720 either a type or an expression. Return -1 if there is nothing
721 special about dereferencing T. */
722
723 static alias_set_type
724 get_deref_alias_set_1 (tree t)
725 {
726 /* All we care about is the type. */
727 if (! TYPE_P (t))
728 t = TREE_TYPE (t);
729
730 /* If we have an INDIRECT_REF via a void pointer, we don't
731 know anything about what that might alias. Likewise if the
732 pointer is marked that way. */
733 if (ref_all_alias_ptr_type_p (t))
734 return 0;
735
736 return -1;
737 }
738
739 /* Return the alias set for the memory pointed to by T, which may be
740 either a type or an expression. */
741
742 alias_set_type
743 get_deref_alias_set (tree t)
744 {
745 /* If we're not doing any alias analysis, just assume everything
746 aliases everything else. */
747 if (!flag_strict_aliasing)
748 return 0;
749
750 alias_set_type set = get_deref_alias_set_1 (t);
751
752 /* Fall back to the alias-set of the pointed-to type. */
753 if (set == -1)
754 {
755 if (! TYPE_P (t))
756 t = TREE_TYPE (t);
757 set = get_alias_set (TREE_TYPE (t));
758 }
759
760 return set;
761 }
762
763 /* Return the pointer-type relevant for TBAA purposes from the
764 memory reference tree *T or NULL_TREE in which case *T is
765 adjusted to point to the outermost component reference that
766 can be used for assigning an alias set. */
767
768 static tree
769 reference_alias_ptr_type_1 (tree *t)
770 {
771 tree inner;
772
773 /* Get the base object of the reference. */
774 inner = *t;
775 while (handled_component_p (inner))
776 {
777 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
778 the type of any component references that wrap it to
779 determine the alias-set. */
780 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
781 *t = TREE_OPERAND (inner, 0);
782 inner = TREE_OPERAND (inner, 0);
783 }
784
785 /* Handle pointer dereferences here, they can override the
786 alias-set. */
787 if (INDIRECT_REF_P (inner)
788 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
789 return TREE_TYPE (TREE_OPERAND (inner, 0));
790 else if (TREE_CODE (inner) == TARGET_MEM_REF)
791 return TREE_TYPE (TMR_OFFSET (inner));
792 else if (TREE_CODE (inner) == MEM_REF
793 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
794 return TREE_TYPE (TREE_OPERAND (inner, 1));
795
796 /* If the innermost reference is a MEM_REF that has a
797 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
798 using the memory access type for determining the alias-set. */
799 if (TREE_CODE (inner) == MEM_REF
800 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
801 != TYPE_MAIN_VARIANT
802 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
803 return TREE_TYPE (TREE_OPERAND (inner, 1));
804
805 /* Otherwise, pick up the outermost object that we could have
806 a pointer to. */
807 tree tem = component_uses_parent_alias_set_from (*t);
808 if (tem)
809 *t = tem;
810
811 return NULL_TREE;
812 }
813
814 /* Return the pointer-type relevant for TBAA purposes from the
815 gimple memory reference tree T. This is the type to be used for
816 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
817 and guarantees that get_alias_set will return the same alias
818 set for T and the replacement. */
819
820 tree
821 reference_alias_ptr_type (tree t)
822 {
823 tree ptype = reference_alias_ptr_type_1 (&t);
824 /* If there is a given pointer type for aliasing purposes, return it. */
825 if (ptype != NULL_TREE)
826 return ptype;
827
828 /* Otherwise build one from the outermost component reference we
829 may use. */
830 if (TREE_CODE (t) == MEM_REF
831 || TREE_CODE (t) == TARGET_MEM_REF)
832 return TREE_TYPE (TREE_OPERAND (t, 1));
833 else
834 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
835 }
836
837 /* Return whether the pointer-types T1 and T2 used to determine
838 two alias sets of two references will yield the same answer
839 from get_deref_alias_set. */
840
841 bool
842 alias_ptr_types_compatible_p (tree t1, tree t2)
843 {
844 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
845 return true;
846
847 if (ref_all_alias_ptr_type_p (t1)
848 || ref_all_alias_ptr_type_p (t2))
849 return false;
850
851 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
852 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
853 }
854
855 /* Create emptry alias set entry. */
856
857 alias_set_entry
858 init_alias_set_entry (alias_set_type set)
859 {
860 alias_set_entry ase = ggc_alloc<alias_set_entry_d> ();
861 ase->alias_set = set;
862 ase->children = NULL;
863 ase->has_zero_child = false;
864 ase->is_pointer = false;
865 ase->has_pointer = false;
866 gcc_checking_assert (!get_alias_set_entry (set));
867 (*alias_sets)[set] = ase;
868 return ase;
869 }
870
871 /* Return the alias set for T, which may be either a type or an
872 expression. Call language-specific routine for help, if needed. */
873
874 alias_set_type
875 get_alias_set (tree t)
876 {
877 alias_set_type set;
878
879 /* If we're not doing any alias analysis, just assume everything
880 aliases everything else. Also return 0 if this or its type is
881 an error. */
882 if (! flag_strict_aliasing || t == error_mark_node
883 || (! TYPE_P (t)
884 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
885 return 0;
886
887 /* We can be passed either an expression or a type. This and the
888 language-specific routine may make mutually-recursive calls to each other
889 to figure out what to do. At each juncture, we see if this is a tree
890 that the language may need to handle specially. First handle things that
891 aren't types. */
892 if (! TYPE_P (t))
893 {
894 /* Give the language a chance to do something with this tree
895 before we look at it. */
896 STRIP_NOPS (t);
897 set = lang_hooks.get_alias_set (t);
898 if (set != -1)
899 return set;
900
901 /* Get the alias pointer-type to use or the outermost object
902 that we could have a pointer to. */
903 tree ptype = reference_alias_ptr_type_1 (&t);
904 if (ptype != NULL)
905 return get_deref_alias_set (ptype);
906
907 /* If we've already determined the alias set for a decl, just return
908 it. This is necessary for C++ anonymous unions, whose component
909 variables don't look like union members (boo!). */
910 if (TREE_CODE (t) == VAR_DECL
911 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
912 return MEM_ALIAS_SET (DECL_RTL (t));
913
914 /* Now all we care about is the type. */
915 t = TREE_TYPE (t);
916 }
917
918 /* Variant qualifiers don't affect the alias set, so get the main
919 variant. */
920 t = TYPE_MAIN_VARIANT (t);
921
922 /* Always use the canonical type as well. If this is a type that
923 requires structural comparisons to identify compatible types
924 use alias set zero. */
925 if (TYPE_STRUCTURAL_EQUALITY_P (t))
926 {
927 /* Allow the language to specify another alias set for this
928 type. */
929 set = lang_hooks.get_alias_set (t);
930 if (set != -1)
931 return set;
932 return 0;
933 }
934
935 t = TYPE_CANONICAL (t);
936
937 /* The canonical type should not require structural equality checks. */
938 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
939
940 /* If this is a type with a known alias set, return it. */
941 if (TYPE_ALIAS_SET_KNOWN_P (t))
942 return TYPE_ALIAS_SET (t);
943
944 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
945 if (!COMPLETE_TYPE_P (t))
946 {
947 /* For arrays with unknown size the conservative answer is the
948 alias set of the element type. */
949 if (TREE_CODE (t) == ARRAY_TYPE)
950 return get_alias_set (TREE_TYPE (t));
951
952 /* But return zero as a conservative answer for incomplete types. */
953 return 0;
954 }
955
956 /* See if the language has special handling for this type. */
957 set = lang_hooks.get_alias_set (t);
958 if (set != -1)
959 return set;
960
961 /* There are no objects of FUNCTION_TYPE, so there's no point in
962 using up an alias set for them. (There are, of course, pointers
963 and references to functions, but that's different.) */
964 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
965 set = 0;
966
967 /* Unless the language specifies otherwise, let vector types alias
968 their components. This avoids some nasty type punning issues in
969 normal usage. And indeed lets vectors be treated more like an
970 array slice. */
971 else if (TREE_CODE (t) == VECTOR_TYPE)
972 set = get_alias_set (TREE_TYPE (t));
973
974 /* Unless the language specifies otherwise, treat array types the
975 same as their components. This avoids the asymmetry we get
976 through recording the components. Consider accessing a
977 character(kind=1) through a reference to a character(kind=1)[1:1].
978 Or consider if we want to assign integer(kind=4)[0:D.1387] and
979 integer(kind=4)[4] the same alias set or not.
980 Just be pragmatic here and make sure the array and its element
981 type get the same alias set assigned. */
982 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
983 set = get_alias_set (TREE_TYPE (t));
984
985 /* From the former common C and C++ langhook implementation:
986
987 Unfortunately, there is no canonical form of a pointer type.
988 In particular, if we have `typedef int I', then `int *', and
989 `I *' are different types. So, we have to pick a canonical
990 representative. We do this below.
991
992 Technically, this approach is actually more conservative that
993 it needs to be. In particular, `const int *' and `int *'
994 should be in different alias sets, according to the C and C++
995 standard, since their types are not the same, and so,
996 technically, an `int **' and `const int **' cannot point at
997 the same thing.
998
999 But, the standard is wrong. In particular, this code is
1000 legal C++:
1001
1002 int *ip;
1003 int **ipp = &ip;
1004 const int* const* cipp = ipp;
1005 And, it doesn't make sense for that to be legal unless you
1006 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
1007 the pointed-to types. This issue has been reported to the
1008 C++ committee.
1009
1010 For this reason go to canonical type of the unqalified pointer type.
1011 Until GCC 6 this code set all pointers sets to have alias set of
1012 ptr_type_node but that is a bad idea, because it prevents disabiguations
1013 in between pointers. For Firefox this accounts about 20% of all
1014 disambiguations in the program. */
1015 else if (POINTER_TYPE_P (t) && t != ptr_type_node && !in_lto_p)
1016 {
1017 tree p;
1018 auto_vec <bool, 8> reference;
1019
1020 /* Unnest all pointers and references.
1021 We also want to make pointer to array equivalent to pointer to its
1022 element. So skip all array types, too. */
1023 for (p = t; POINTER_TYPE_P (p)
1024 || (TREE_CODE (p) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (p));
1025 p = TREE_TYPE (p))
1026 {
1027 if (TREE_CODE (p) == REFERENCE_TYPE)
1028 reference.safe_push (true);
1029 if (TREE_CODE (p) == POINTER_TYPE)
1030 reference.safe_push (false);
1031 }
1032 p = TYPE_MAIN_VARIANT (p);
1033
1034 /* Make void * compatible with char * and also void **.
1035 Programs are commonly violating TBAA by this.
1036
1037 We also make void * to conflict with every pointer
1038 (see record_component_aliases) and thus it is safe it to use it for
1039 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
1040 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
1041 set = get_alias_set (ptr_type_node);
1042 else
1043 {
1044 /* Rebuild pointer type from starting from canonical types using
1045 unqualified pointers and references only. This way all such
1046 pointers will have the same alias set and will conflict with
1047 each other.
1048
1049 Most of time we already have pointers or references of a given type.
1050 If not we build new one just to be sure that if someone later
1051 (probably only middle-end can, as we should assign all alias
1052 classes only after finishing translation unit) builds the pointer
1053 type, the canonical type will match. */
1054 p = TYPE_CANONICAL (p);
1055 while (!reference.is_empty ())
1056 {
1057 if (reference.pop ())
1058 p = build_reference_type (p);
1059 else
1060 p = build_pointer_type (p);
1061 p = TYPE_CANONICAL (TYPE_MAIN_VARIANT (p));
1062 }
1063 gcc_checking_assert (TYPE_CANONICAL (p) == p);
1064
1065 /* Assign the alias set to both p and t.
1066 We can not call get_alias_set (p) here as that would trigger
1067 infinite recursion when p == t. In other cases it would just
1068 trigger unnecesary legwork of rebuilding the pointer again. */
1069 if (TYPE_ALIAS_SET_KNOWN_P (p))
1070 set = TYPE_ALIAS_SET (p);
1071 else
1072 {
1073 set = new_alias_set ();
1074 TYPE_ALIAS_SET (p) = set;
1075 }
1076 }
1077 }
1078 /* In LTO the rules above needs to be part of canonical type machinery.
1079 For now just punt. */
1080 else if (POINTER_TYPE_P (t) && t != ptr_type_node && in_lto_p)
1081 set = get_alias_set (ptr_type_node);
1082
1083 /* Otherwise make a new alias set for this type. */
1084 else
1085 {
1086 /* Each canonical type gets its own alias set, so canonical types
1087 shouldn't form a tree. It doesn't really matter for types
1088 we handle specially above, so only check it where it possibly
1089 would result in a bogus alias set. */
1090 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1091
1092 set = new_alias_set ();
1093 }
1094
1095 TYPE_ALIAS_SET (t) = set;
1096
1097 /* If this is an aggregate type or a complex type, we must record any
1098 component aliasing information. */
1099 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1100 record_component_aliases (t);
1101
1102 /* We treat pointer types specially in alias_set_subset_of. */
1103 if (POINTER_TYPE_P (t) && set)
1104 {
1105 alias_set_entry ase = get_alias_set_entry (set);
1106 if (!ase)
1107 ase = init_alias_set_entry (set);
1108 ase->is_pointer = true;
1109 ase->has_pointer = true;
1110 }
1111
1112 return set;
1113 }
1114
1115 /* Return a brand-new alias set. */
1116
1117 alias_set_type
1118 new_alias_set (void)
1119 {
1120 if (flag_strict_aliasing)
1121 {
1122 if (alias_sets == 0)
1123 vec_safe_push (alias_sets, (alias_set_entry) 0);
1124 vec_safe_push (alias_sets, (alias_set_entry) 0);
1125 return alias_sets->length () - 1;
1126 }
1127 else
1128 return 0;
1129 }
1130
1131 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1132 not everything that aliases SUPERSET also aliases SUBSET. For example,
1133 in C, a store to an `int' can alias a load of a structure containing an
1134 `int', and vice versa. But it can't alias a load of a 'double' member
1135 of the same structure. Here, the structure would be the SUPERSET and
1136 `int' the SUBSET. This relationship is also described in the comment at
1137 the beginning of this file.
1138
1139 This function should be called only once per SUPERSET/SUBSET pair.
1140
1141 It is illegal for SUPERSET to be zero; everything is implicitly a
1142 subset of alias set zero. */
1143
1144 void
1145 record_alias_subset (alias_set_type superset, alias_set_type subset)
1146 {
1147 alias_set_entry superset_entry;
1148 alias_set_entry subset_entry;
1149
1150 /* It is possible in complex type situations for both sets to be the same,
1151 in which case we can ignore this operation. */
1152 if (superset == subset)
1153 return;
1154
1155 gcc_assert (superset);
1156
1157 superset_entry = get_alias_set_entry (superset);
1158 if (superset_entry == 0)
1159 {
1160 /* Create an entry for the SUPERSET, so that we have a place to
1161 attach the SUBSET. */
1162 superset_entry = init_alias_set_entry (superset);
1163 }
1164
1165 if (subset == 0)
1166 superset_entry->has_zero_child = 1;
1167 else
1168 {
1169 subset_entry = get_alias_set_entry (subset);
1170 if (!superset_entry->children)
1171 superset_entry->children
1172 = hash_map<int, int, alias_set_traits>::create_ggc (64);
1173 /* If there is an entry for the subset, enter all of its children
1174 (if they are not already present) as children of the SUPERSET. */
1175 if (subset_entry)
1176 {
1177 if (subset_entry->has_zero_child)
1178 superset_entry->has_zero_child = true;
1179 if (subset_entry->has_pointer)
1180 superset_entry->has_pointer = true;
1181
1182 if (subset_entry->children)
1183 {
1184 hash_map<int, int, alias_set_traits>::iterator iter
1185 = subset_entry->children->begin ();
1186 for (; iter != subset_entry->children->end (); ++iter)
1187 superset_entry->children->put ((*iter).first, (*iter).second);
1188 }
1189 }
1190
1191 /* Enter the SUBSET itself as a child of the SUPERSET. */
1192 superset_entry->children->put (subset, 0);
1193 }
1194 }
1195
1196 /* Record that component types of TYPE, if any, are part of that type for
1197 aliasing purposes. For record types, we only record component types
1198 for fields that are not marked non-addressable. For array types, we
1199 only record the component type if it is not marked non-aliased. */
1200
1201 void
1202 record_component_aliases (tree type)
1203 {
1204 alias_set_type superset = get_alias_set (type);
1205 tree field;
1206
1207 if (superset == 0)
1208 return;
1209
1210 switch (TREE_CODE (type))
1211 {
1212 case RECORD_TYPE:
1213 case UNION_TYPE:
1214 case QUAL_UNION_TYPE:
1215 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1216 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1217 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1218 break;
1219
1220 case COMPLEX_TYPE:
1221 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1222 break;
1223
1224 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1225 element type. */
1226
1227 default:
1228 break;
1229 }
1230 }
1231
1232 /* Allocate an alias set for use in storing and reading from the varargs
1233 spill area. */
1234
1235 static GTY(()) alias_set_type varargs_set = -1;
1236
1237 alias_set_type
1238 get_varargs_alias_set (void)
1239 {
1240 #if 1
1241 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1242 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1243 consistently use the varargs alias set for loads from the varargs
1244 area. So don't use it anywhere. */
1245 return 0;
1246 #else
1247 if (varargs_set == -1)
1248 varargs_set = new_alias_set ();
1249
1250 return varargs_set;
1251 #endif
1252 }
1253
1254 /* Likewise, but used for the fixed portions of the frame, e.g., register
1255 save areas. */
1256
1257 static GTY(()) alias_set_type frame_set = -1;
1258
1259 alias_set_type
1260 get_frame_alias_set (void)
1261 {
1262 if (frame_set == -1)
1263 frame_set = new_alias_set ();
1264
1265 return frame_set;
1266 }
1267
1268 /* Create a new, unique base with id ID. */
1269
1270 static rtx
1271 unique_base_value (HOST_WIDE_INT id)
1272 {
1273 return gen_rtx_ADDRESS (Pmode, id);
1274 }
1275
1276 /* Return true if accesses based on any other base value cannot alias
1277 those based on X. */
1278
1279 static bool
1280 unique_base_value_p (rtx x)
1281 {
1282 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1283 }
1284
1285 /* Return true if X is known to be a base value. */
1286
1287 static bool
1288 known_base_value_p (rtx x)
1289 {
1290 switch (GET_CODE (x))
1291 {
1292 case LABEL_REF:
1293 case SYMBOL_REF:
1294 return true;
1295
1296 case ADDRESS:
1297 /* Arguments may or may not be bases; we don't know for sure. */
1298 return GET_MODE (x) != VOIDmode;
1299
1300 default:
1301 return false;
1302 }
1303 }
1304
1305 /* Inside SRC, the source of a SET, find a base address. */
1306
1307 static rtx
1308 find_base_value (rtx src)
1309 {
1310 unsigned int regno;
1311
1312 #if defined (FIND_BASE_TERM)
1313 /* Try machine-dependent ways to find the base term. */
1314 src = FIND_BASE_TERM (src);
1315 #endif
1316
1317 switch (GET_CODE (src))
1318 {
1319 case SYMBOL_REF:
1320 case LABEL_REF:
1321 return src;
1322
1323 case REG:
1324 regno = REGNO (src);
1325 /* At the start of a function, argument registers have known base
1326 values which may be lost later. Returning an ADDRESS
1327 expression here allows optimization based on argument values
1328 even when the argument registers are used for other purposes. */
1329 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1330 return new_reg_base_value[regno];
1331
1332 /* If a pseudo has a known base value, return it. Do not do this
1333 for non-fixed hard regs since it can result in a circular
1334 dependency chain for registers which have values at function entry.
1335
1336 The test above is not sufficient because the scheduler may move
1337 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1338 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1339 && regno < vec_safe_length (reg_base_value))
1340 {
1341 /* If we're inside init_alias_analysis, use new_reg_base_value
1342 to reduce the number of relaxation iterations. */
1343 if (new_reg_base_value && new_reg_base_value[regno]
1344 && DF_REG_DEF_COUNT (regno) == 1)
1345 return new_reg_base_value[regno];
1346
1347 if ((*reg_base_value)[regno])
1348 return (*reg_base_value)[regno];
1349 }
1350
1351 return 0;
1352
1353 case MEM:
1354 /* Check for an argument passed in memory. Only record in the
1355 copying-arguments block; it is too hard to track changes
1356 otherwise. */
1357 if (copying_arguments
1358 && (XEXP (src, 0) == arg_pointer_rtx
1359 || (GET_CODE (XEXP (src, 0)) == PLUS
1360 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1361 return arg_base_value;
1362 return 0;
1363
1364 case CONST:
1365 src = XEXP (src, 0);
1366 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1367 break;
1368
1369 /* ... fall through ... */
1370
1371 case PLUS:
1372 case MINUS:
1373 {
1374 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1375
1376 /* If either operand is a REG that is a known pointer, then it
1377 is the base. */
1378 if (REG_P (src_0) && REG_POINTER (src_0))
1379 return find_base_value (src_0);
1380 if (REG_P (src_1) && REG_POINTER (src_1))
1381 return find_base_value (src_1);
1382
1383 /* If either operand is a REG, then see if we already have
1384 a known value for it. */
1385 if (REG_P (src_0))
1386 {
1387 temp = find_base_value (src_0);
1388 if (temp != 0)
1389 src_0 = temp;
1390 }
1391
1392 if (REG_P (src_1))
1393 {
1394 temp = find_base_value (src_1);
1395 if (temp!= 0)
1396 src_1 = temp;
1397 }
1398
1399 /* If either base is named object or a special address
1400 (like an argument or stack reference), then use it for the
1401 base term. */
1402 if (src_0 != 0 && known_base_value_p (src_0))
1403 return src_0;
1404
1405 if (src_1 != 0 && known_base_value_p (src_1))
1406 return src_1;
1407
1408 /* Guess which operand is the base address:
1409 If either operand is a symbol, then it is the base. If
1410 either operand is a CONST_INT, then the other is the base. */
1411 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1412 return find_base_value (src_0);
1413 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1414 return find_base_value (src_1);
1415
1416 return 0;
1417 }
1418
1419 case LO_SUM:
1420 /* The standard form is (lo_sum reg sym) so look only at the
1421 second operand. */
1422 return find_base_value (XEXP (src, 1));
1423
1424 case AND:
1425 /* If the second operand is constant set the base
1426 address to the first operand. */
1427 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1428 return find_base_value (XEXP (src, 0));
1429 return 0;
1430
1431 case TRUNCATE:
1432 /* As we do not know which address space the pointer is referring to, we can
1433 handle this only if the target does not support different pointer or
1434 address modes depending on the address space. */
1435 if (!target_default_pointer_address_modes_p ())
1436 break;
1437 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1438 break;
1439 /* Fall through. */
1440 case HIGH:
1441 case PRE_INC:
1442 case PRE_DEC:
1443 case POST_INC:
1444 case POST_DEC:
1445 case PRE_MODIFY:
1446 case POST_MODIFY:
1447 return find_base_value (XEXP (src, 0));
1448
1449 case ZERO_EXTEND:
1450 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1451 /* As we do not know which address space the pointer is referring to, we can
1452 handle this only if the target does not support different pointer or
1453 address modes depending on the address space. */
1454 if (!target_default_pointer_address_modes_p ())
1455 break;
1456
1457 {
1458 rtx temp = find_base_value (XEXP (src, 0));
1459
1460 if (temp != 0 && CONSTANT_P (temp))
1461 temp = convert_memory_address (Pmode, temp);
1462
1463 return temp;
1464 }
1465
1466 default:
1467 break;
1468 }
1469
1470 return 0;
1471 }
1472
1473 /* Called from init_alias_analysis indirectly through note_stores,
1474 or directly if DEST is a register with a REG_NOALIAS note attached.
1475 SET is null in the latter case. */
1476
1477 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1478 register N has been set in this function. */
1479 static sbitmap reg_seen;
1480
1481 static void
1482 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1483 {
1484 unsigned regno;
1485 rtx src;
1486 int n;
1487
1488 if (!REG_P (dest))
1489 return;
1490
1491 regno = REGNO (dest);
1492
1493 gcc_checking_assert (regno < reg_base_value->length ());
1494
1495 n = REG_NREGS (dest);
1496 if (n != 1)
1497 {
1498 while (--n >= 0)
1499 {
1500 bitmap_set_bit (reg_seen, regno + n);
1501 new_reg_base_value[regno + n] = 0;
1502 }
1503 return;
1504 }
1505
1506 if (set)
1507 {
1508 /* A CLOBBER wipes out any old value but does not prevent a previously
1509 unset register from acquiring a base address (i.e. reg_seen is not
1510 set). */
1511 if (GET_CODE (set) == CLOBBER)
1512 {
1513 new_reg_base_value[regno] = 0;
1514 return;
1515 }
1516 src = SET_SRC (set);
1517 }
1518 else
1519 {
1520 /* There's a REG_NOALIAS note against DEST. */
1521 if (bitmap_bit_p (reg_seen, regno))
1522 {
1523 new_reg_base_value[regno] = 0;
1524 return;
1525 }
1526 bitmap_set_bit (reg_seen, regno);
1527 new_reg_base_value[regno] = unique_base_value (unique_id++);
1528 return;
1529 }
1530
1531 /* If this is not the first set of REGNO, see whether the new value
1532 is related to the old one. There are two cases of interest:
1533
1534 (1) The register might be assigned an entirely new value
1535 that has the same base term as the original set.
1536
1537 (2) The set might be a simple self-modification that
1538 cannot change REGNO's base value.
1539
1540 If neither case holds, reject the original base value as invalid.
1541 Note that the following situation is not detected:
1542
1543 extern int x, y; int *p = &x; p += (&y-&x);
1544
1545 ANSI C does not allow computing the difference of addresses
1546 of distinct top level objects. */
1547 if (new_reg_base_value[regno] != 0
1548 && find_base_value (src) != new_reg_base_value[regno])
1549 switch (GET_CODE (src))
1550 {
1551 case LO_SUM:
1552 case MINUS:
1553 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1554 new_reg_base_value[regno] = 0;
1555 break;
1556 case PLUS:
1557 /* If the value we add in the PLUS is also a valid base value,
1558 this might be the actual base value, and the original value
1559 an index. */
1560 {
1561 rtx other = NULL_RTX;
1562
1563 if (XEXP (src, 0) == dest)
1564 other = XEXP (src, 1);
1565 else if (XEXP (src, 1) == dest)
1566 other = XEXP (src, 0);
1567
1568 if (! other || find_base_value (other))
1569 new_reg_base_value[regno] = 0;
1570 break;
1571 }
1572 case AND:
1573 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1574 new_reg_base_value[regno] = 0;
1575 break;
1576 default:
1577 new_reg_base_value[regno] = 0;
1578 break;
1579 }
1580 /* If this is the first set of a register, record the value. */
1581 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1582 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1583 new_reg_base_value[regno] = find_base_value (src);
1584
1585 bitmap_set_bit (reg_seen, regno);
1586 }
1587
1588 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1589 using hard registers with non-null REG_BASE_VALUE for renaming. */
1590 rtx
1591 get_reg_base_value (unsigned int regno)
1592 {
1593 return (*reg_base_value)[regno];
1594 }
1595
1596 /* If a value is known for REGNO, return it. */
1597
1598 rtx
1599 get_reg_known_value (unsigned int regno)
1600 {
1601 if (regno >= FIRST_PSEUDO_REGISTER)
1602 {
1603 regno -= FIRST_PSEUDO_REGISTER;
1604 if (regno < vec_safe_length (reg_known_value))
1605 return (*reg_known_value)[regno];
1606 }
1607 return NULL;
1608 }
1609
1610 /* Set it. */
1611
1612 static void
1613 set_reg_known_value (unsigned int regno, rtx val)
1614 {
1615 if (regno >= FIRST_PSEUDO_REGISTER)
1616 {
1617 regno -= FIRST_PSEUDO_REGISTER;
1618 if (regno < vec_safe_length (reg_known_value))
1619 (*reg_known_value)[regno] = val;
1620 }
1621 }
1622
1623 /* Similarly for reg_known_equiv_p. */
1624
1625 bool
1626 get_reg_known_equiv_p (unsigned int regno)
1627 {
1628 if (regno >= FIRST_PSEUDO_REGISTER)
1629 {
1630 regno -= FIRST_PSEUDO_REGISTER;
1631 if (regno < vec_safe_length (reg_known_value))
1632 return bitmap_bit_p (reg_known_equiv_p, regno);
1633 }
1634 return false;
1635 }
1636
1637 static void
1638 set_reg_known_equiv_p (unsigned int regno, bool val)
1639 {
1640 if (regno >= FIRST_PSEUDO_REGISTER)
1641 {
1642 regno -= FIRST_PSEUDO_REGISTER;
1643 if (regno < vec_safe_length (reg_known_value))
1644 {
1645 if (val)
1646 bitmap_set_bit (reg_known_equiv_p, regno);
1647 else
1648 bitmap_clear_bit (reg_known_equiv_p, regno);
1649 }
1650 }
1651 }
1652
1653
1654 /* Returns a canonical version of X, from the point of view alias
1655 analysis. (For example, if X is a MEM whose address is a register,
1656 and the register has a known value (say a SYMBOL_REF), then a MEM
1657 whose address is the SYMBOL_REF is returned.) */
1658
1659 rtx
1660 canon_rtx (rtx x)
1661 {
1662 /* Recursively look for equivalences. */
1663 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1664 {
1665 rtx t = get_reg_known_value (REGNO (x));
1666 if (t == x)
1667 return x;
1668 if (t)
1669 return canon_rtx (t);
1670 }
1671
1672 if (GET_CODE (x) == PLUS)
1673 {
1674 rtx x0 = canon_rtx (XEXP (x, 0));
1675 rtx x1 = canon_rtx (XEXP (x, 1));
1676
1677 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1678 {
1679 if (CONST_INT_P (x0))
1680 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1681 else if (CONST_INT_P (x1))
1682 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1683 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1684 }
1685 }
1686
1687 /* This gives us much better alias analysis when called from
1688 the loop optimizer. Note we want to leave the original
1689 MEM alone, but need to return the canonicalized MEM with
1690 all the flags with their original values. */
1691 else if (MEM_P (x))
1692 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1693
1694 return x;
1695 }
1696
1697 /* Return 1 if X and Y are identical-looking rtx's.
1698 Expect that X and Y has been already canonicalized.
1699
1700 We use the data in reg_known_value above to see if two registers with
1701 different numbers are, in fact, equivalent. */
1702
1703 static int
1704 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1705 {
1706 int i;
1707 int j;
1708 enum rtx_code code;
1709 const char *fmt;
1710
1711 if (x == 0 && y == 0)
1712 return 1;
1713 if (x == 0 || y == 0)
1714 return 0;
1715
1716 if (x == y)
1717 return 1;
1718
1719 code = GET_CODE (x);
1720 /* Rtx's of different codes cannot be equal. */
1721 if (code != GET_CODE (y))
1722 return 0;
1723
1724 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1725 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1726
1727 if (GET_MODE (x) != GET_MODE (y))
1728 return 0;
1729
1730 /* Some RTL can be compared without a recursive examination. */
1731 switch (code)
1732 {
1733 case REG:
1734 return REGNO (x) == REGNO (y);
1735
1736 case LABEL_REF:
1737 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1738
1739 case SYMBOL_REF:
1740 return XSTR (x, 0) == XSTR (y, 0);
1741
1742 case ENTRY_VALUE:
1743 /* This is magic, don't go through canonicalization et al. */
1744 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1745
1746 case VALUE:
1747 CASE_CONST_UNIQUE:
1748 /* Pointer equality guarantees equality for these nodes. */
1749 return 0;
1750
1751 default:
1752 break;
1753 }
1754
1755 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1756 if (code == PLUS)
1757 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1758 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1759 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1760 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1761 /* For commutative operations, the RTX match if the operand match in any
1762 order. Also handle the simple binary and unary cases without a loop. */
1763 if (COMMUTATIVE_P (x))
1764 {
1765 rtx xop0 = canon_rtx (XEXP (x, 0));
1766 rtx yop0 = canon_rtx (XEXP (y, 0));
1767 rtx yop1 = canon_rtx (XEXP (y, 1));
1768
1769 return ((rtx_equal_for_memref_p (xop0, yop0)
1770 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1771 || (rtx_equal_for_memref_p (xop0, yop1)
1772 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1773 }
1774 else if (NON_COMMUTATIVE_P (x))
1775 {
1776 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1777 canon_rtx (XEXP (y, 0)))
1778 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1779 canon_rtx (XEXP (y, 1))));
1780 }
1781 else if (UNARY_P (x))
1782 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1783 canon_rtx (XEXP (y, 0)));
1784
1785 /* Compare the elements. If any pair of corresponding elements
1786 fail to match, return 0 for the whole things.
1787
1788 Limit cases to types which actually appear in addresses. */
1789
1790 fmt = GET_RTX_FORMAT (code);
1791 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1792 {
1793 switch (fmt[i])
1794 {
1795 case 'i':
1796 if (XINT (x, i) != XINT (y, i))
1797 return 0;
1798 break;
1799
1800 case 'E':
1801 /* Two vectors must have the same length. */
1802 if (XVECLEN (x, i) != XVECLEN (y, i))
1803 return 0;
1804
1805 /* And the corresponding elements must match. */
1806 for (j = 0; j < XVECLEN (x, i); j++)
1807 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1808 canon_rtx (XVECEXP (y, i, j))) == 0)
1809 return 0;
1810 break;
1811
1812 case 'e':
1813 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1814 canon_rtx (XEXP (y, i))) == 0)
1815 return 0;
1816 break;
1817
1818 /* This can happen for asm operands. */
1819 case 's':
1820 if (strcmp (XSTR (x, i), XSTR (y, i)))
1821 return 0;
1822 break;
1823
1824 /* This can happen for an asm which clobbers memory. */
1825 case '0':
1826 break;
1827
1828 /* It is believed that rtx's at this level will never
1829 contain anything but integers and other rtx's,
1830 except for within LABEL_REFs and SYMBOL_REFs. */
1831 default:
1832 gcc_unreachable ();
1833 }
1834 }
1835 return 1;
1836 }
1837
1838 static rtx
1839 find_base_term (rtx x)
1840 {
1841 cselib_val *val;
1842 struct elt_loc_list *l, *f;
1843 rtx ret;
1844
1845 #if defined (FIND_BASE_TERM)
1846 /* Try machine-dependent ways to find the base term. */
1847 x = FIND_BASE_TERM (x);
1848 #endif
1849
1850 switch (GET_CODE (x))
1851 {
1852 case REG:
1853 return REG_BASE_VALUE (x);
1854
1855 case TRUNCATE:
1856 /* As we do not know which address space the pointer is referring to, we can
1857 handle this only if the target does not support different pointer or
1858 address modes depending on the address space. */
1859 if (!target_default_pointer_address_modes_p ())
1860 return 0;
1861 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1862 return 0;
1863 /* Fall through. */
1864 case HIGH:
1865 case PRE_INC:
1866 case PRE_DEC:
1867 case POST_INC:
1868 case POST_DEC:
1869 case PRE_MODIFY:
1870 case POST_MODIFY:
1871 return find_base_term (XEXP (x, 0));
1872
1873 case ZERO_EXTEND:
1874 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1875 /* As we do not know which address space the pointer is referring to, we can
1876 handle this only if the target does not support different pointer or
1877 address modes depending on the address space. */
1878 if (!target_default_pointer_address_modes_p ())
1879 return 0;
1880
1881 {
1882 rtx temp = find_base_term (XEXP (x, 0));
1883
1884 if (temp != 0 && CONSTANT_P (temp))
1885 temp = convert_memory_address (Pmode, temp);
1886
1887 return temp;
1888 }
1889
1890 case VALUE:
1891 val = CSELIB_VAL_PTR (x);
1892 ret = NULL_RTX;
1893
1894 if (!val)
1895 return ret;
1896
1897 if (cselib_sp_based_value_p (val))
1898 return static_reg_base_value[STACK_POINTER_REGNUM];
1899
1900 f = val->locs;
1901 /* Temporarily reset val->locs to avoid infinite recursion. */
1902 val->locs = NULL;
1903
1904 for (l = f; l; l = l->next)
1905 if (GET_CODE (l->loc) == VALUE
1906 && CSELIB_VAL_PTR (l->loc)->locs
1907 && !CSELIB_VAL_PTR (l->loc)->locs->next
1908 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1909 continue;
1910 else if ((ret = find_base_term (l->loc)) != 0)
1911 break;
1912
1913 val->locs = f;
1914 return ret;
1915
1916 case LO_SUM:
1917 /* The standard form is (lo_sum reg sym) so look only at the
1918 second operand. */
1919 return find_base_term (XEXP (x, 1));
1920
1921 case CONST:
1922 x = XEXP (x, 0);
1923 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1924 return 0;
1925 /* Fall through. */
1926 case PLUS:
1927 case MINUS:
1928 {
1929 rtx tmp1 = XEXP (x, 0);
1930 rtx tmp2 = XEXP (x, 1);
1931
1932 /* This is a little bit tricky since we have to determine which of
1933 the two operands represents the real base address. Otherwise this
1934 routine may return the index register instead of the base register.
1935
1936 That may cause us to believe no aliasing was possible, when in
1937 fact aliasing is possible.
1938
1939 We use a few simple tests to guess the base register. Additional
1940 tests can certainly be added. For example, if one of the operands
1941 is a shift or multiply, then it must be the index register and the
1942 other operand is the base register. */
1943
1944 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1945 return find_base_term (tmp2);
1946
1947 /* If either operand is known to be a pointer, then prefer it
1948 to determine the base term. */
1949 if (REG_P (tmp1) && REG_POINTER (tmp1))
1950 ;
1951 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1952 std::swap (tmp1, tmp2);
1953 /* If second argument is constant which has base term, prefer it
1954 over variable tmp1. See PR64025. */
1955 else if (CONSTANT_P (tmp2) && !CONST_INT_P (tmp2))
1956 std::swap (tmp1, tmp2);
1957
1958 /* Go ahead and find the base term for both operands. If either base
1959 term is from a pointer or is a named object or a special address
1960 (like an argument or stack reference), then use it for the
1961 base term. */
1962 rtx base = find_base_term (tmp1);
1963 if (base != NULL_RTX
1964 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1965 || known_base_value_p (base)))
1966 return base;
1967 base = find_base_term (tmp2);
1968 if (base != NULL_RTX
1969 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1970 || known_base_value_p (base)))
1971 return base;
1972
1973 /* We could not determine which of the two operands was the
1974 base register and which was the index. So we can determine
1975 nothing from the base alias check. */
1976 return 0;
1977 }
1978
1979 case AND:
1980 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1981 return find_base_term (XEXP (x, 0));
1982 return 0;
1983
1984 case SYMBOL_REF:
1985 case LABEL_REF:
1986 return x;
1987
1988 default:
1989 return 0;
1990 }
1991 }
1992
1993 /* Return true if accesses to address X may alias accesses based
1994 on the stack pointer. */
1995
1996 bool
1997 may_be_sp_based_p (rtx x)
1998 {
1999 rtx base = find_base_term (x);
2000 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
2001 }
2002
2003 /* Return 0 if the addresses X and Y are known to point to different
2004 objects, 1 if they might be pointers to the same object. */
2005
2006 static int
2007 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
2008 machine_mode x_mode, machine_mode y_mode)
2009 {
2010 /* If the address itself has no known base see if a known equivalent
2011 value has one. If either address still has no known base, nothing
2012 is known about aliasing. */
2013 if (x_base == 0)
2014 {
2015 rtx x_c;
2016
2017 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
2018 return 1;
2019
2020 x_base = find_base_term (x_c);
2021 if (x_base == 0)
2022 return 1;
2023 }
2024
2025 if (y_base == 0)
2026 {
2027 rtx y_c;
2028 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
2029 return 1;
2030
2031 y_base = find_base_term (y_c);
2032 if (y_base == 0)
2033 return 1;
2034 }
2035
2036 /* If the base addresses are equal nothing is known about aliasing. */
2037 if (rtx_equal_p (x_base, y_base))
2038 return 1;
2039
2040 /* The base addresses are different expressions. If they are not accessed
2041 via AND, there is no conflict. We can bring knowledge of object
2042 alignment into play here. For example, on alpha, "char a, b;" can
2043 alias one another, though "char a; long b;" cannot. AND addesses may
2044 implicitly alias surrounding objects; i.e. unaligned access in DImode
2045 via AND address can alias all surrounding object types except those
2046 with aligment 8 or higher. */
2047 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2048 return 1;
2049 if (GET_CODE (x) == AND
2050 && (!CONST_INT_P (XEXP (x, 1))
2051 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2052 return 1;
2053 if (GET_CODE (y) == AND
2054 && (!CONST_INT_P (XEXP (y, 1))
2055 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2056 return 1;
2057
2058 /* Differing symbols not accessed via AND never alias. */
2059 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2060 return 0;
2061
2062 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2063 return 0;
2064
2065 return 1;
2066 }
2067
2068 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2069 that of V. */
2070
2071 static bool
2072 refs_newer_value_p (const_rtx expr, rtx v)
2073 {
2074 int minuid = CSELIB_VAL_PTR (v)->uid;
2075 subrtx_iterator::array_type array;
2076 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2077 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
2078 return true;
2079 return false;
2080 }
2081
2082 /* Convert the address X into something we can use. This is done by returning
2083 it unchanged unless it is a value; in the latter case we call cselib to get
2084 a more useful rtx. */
2085
2086 rtx
2087 get_addr (rtx x)
2088 {
2089 cselib_val *v;
2090 struct elt_loc_list *l;
2091
2092 if (GET_CODE (x) != VALUE)
2093 return x;
2094 v = CSELIB_VAL_PTR (x);
2095 if (v)
2096 {
2097 bool have_equivs = cselib_have_permanent_equivalences ();
2098 if (have_equivs)
2099 v = canonical_cselib_val (v);
2100 for (l = v->locs; l; l = l->next)
2101 if (CONSTANT_P (l->loc))
2102 return l->loc;
2103 for (l = v->locs; l; l = l->next)
2104 if (!REG_P (l->loc) && !MEM_P (l->loc)
2105 /* Avoid infinite recursion when potentially dealing with
2106 var-tracking artificial equivalences, by skipping the
2107 equivalences themselves, and not choosing expressions
2108 that refer to newer VALUEs. */
2109 && (!have_equivs
2110 || (GET_CODE (l->loc) != VALUE
2111 && !refs_newer_value_p (l->loc, x))))
2112 return l->loc;
2113 if (have_equivs)
2114 {
2115 for (l = v->locs; l; l = l->next)
2116 if (REG_P (l->loc)
2117 || (GET_CODE (l->loc) != VALUE
2118 && !refs_newer_value_p (l->loc, x)))
2119 return l->loc;
2120 /* Return the canonical value. */
2121 return v->val_rtx;
2122 }
2123 if (v->locs)
2124 return v->locs->loc;
2125 }
2126 return x;
2127 }
2128
2129 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2130 where SIZE is the size in bytes of the memory reference. If ADDR
2131 is not modified by the memory reference then ADDR is returned. */
2132
2133 static rtx
2134 addr_side_effect_eval (rtx addr, int size, int n_refs)
2135 {
2136 int offset = 0;
2137
2138 switch (GET_CODE (addr))
2139 {
2140 case PRE_INC:
2141 offset = (n_refs + 1) * size;
2142 break;
2143 case PRE_DEC:
2144 offset = -(n_refs + 1) * size;
2145 break;
2146 case POST_INC:
2147 offset = n_refs * size;
2148 break;
2149 case POST_DEC:
2150 offset = -n_refs * size;
2151 break;
2152
2153 default:
2154 return addr;
2155 }
2156
2157 if (offset)
2158 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
2159 gen_int_mode (offset, GET_MODE (addr)));
2160 else
2161 addr = XEXP (addr, 0);
2162 addr = canon_rtx (addr);
2163
2164 return addr;
2165 }
2166
2167 /* Return TRUE if an object X sized at XSIZE bytes and another object
2168 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2169 any of the sizes is zero, assume an overlap, otherwise use the
2170 absolute value of the sizes as the actual sizes. */
2171
2172 static inline bool
2173 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
2174 {
2175 return (xsize == 0 || ysize == 0
2176 || (c >= 0
2177 ? (abs (xsize) > c)
2178 : (abs (ysize) > -c)));
2179 }
2180
2181 /* Return one if X and Y (memory addresses) reference the
2182 same location in memory or if the references overlap.
2183 Return zero if they do not overlap, else return
2184 minus one in which case they still might reference the same location.
2185
2186 C is an offset accumulator. When
2187 C is nonzero, we are testing aliases between X and Y + C.
2188 XSIZE is the size in bytes of the X reference,
2189 similarly YSIZE is the size in bytes for Y.
2190 Expect that canon_rtx has been already called for X and Y.
2191
2192 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2193 referenced (the reference was BLKmode), so make the most pessimistic
2194 assumptions.
2195
2196 If XSIZE or YSIZE is negative, we may access memory outside the object
2197 being referenced as a side effect. This can happen when using AND to
2198 align memory references, as is done on the Alpha.
2199
2200 Nice to notice that varying addresses cannot conflict with fp if no
2201 local variables had their addresses taken, but that's too hard now.
2202
2203 ??? Contrary to the tree alias oracle this does not return
2204 one for X + non-constant and Y + non-constant when X and Y are equal.
2205 If that is fixed the TBAA hack for union type-punning can be removed. */
2206
2207 static int
2208 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2209 {
2210 if (GET_CODE (x) == VALUE)
2211 {
2212 if (REG_P (y))
2213 {
2214 struct elt_loc_list *l = NULL;
2215 if (CSELIB_VAL_PTR (x))
2216 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2217 l; l = l->next)
2218 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2219 break;
2220 if (l)
2221 x = y;
2222 else
2223 x = get_addr (x);
2224 }
2225 /* Don't call get_addr if y is the same VALUE. */
2226 else if (x != y)
2227 x = get_addr (x);
2228 }
2229 if (GET_CODE (y) == VALUE)
2230 {
2231 if (REG_P (x))
2232 {
2233 struct elt_loc_list *l = NULL;
2234 if (CSELIB_VAL_PTR (y))
2235 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2236 l; l = l->next)
2237 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2238 break;
2239 if (l)
2240 y = x;
2241 else
2242 y = get_addr (y);
2243 }
2244 /* Don't call get_addr if x is the same VALUE. */
2245 else if (y != x)
2246 y = get_addr (y);
2247 }
2248 if (GET_CODE (x) == HIGH)
2249 x = XEXP (x, 0);
2250 else if (GET_CODE (x) == LO_SUM)
2251 x = XEXP (x, 1);
2252 else
2253 x = addr_side_effect_eval (x, abs (xsize), 0);
2254 if (GET_CODE (y) == HIGH)
2255 y = XEXP (y, 0);
2256 else if (GET_CODE (y) == LO_SUM)
2257 y = XEXP (y, 1);
2258 else
2259 y = addr_side_effect_eval (y, abs (ysize), 0);
2260
2261 if (rtx_equal_for_memref_p (x, y))
2262 {
2263 return offset_overlap_p (c, xsize, ysize);
2264 }
2265
2266 /* This code used to check for conflicts involving stack references and
2267 globals but the base address alias code now handles these cases. */
2268
2269 if (GET_CODE (x) == PLUS)
2270 {
2271 /* The fact that X is canonicalized means that this
2272 PLUS rtx is canonicalized. */
2273 rtx x0 = XEXP (x, 0);
2274 rtx x1 = XEXP (x, 1);
2275
2276 if (GET_CODE (y) == PLUS)
2277 {
2278 /* The fact that Y is canonicalized means that this
2279 PLUS rtx is canonicalized. */
2280 rtx y0 = XEXP (y, 0);
2281 rtx y1 = XEXP (y, 1);
2282
2283 if (rtx_equal_for_memref_p (x1, y1))
2284 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2285 if (rtx_equal_for_memref_p (x0, y0))
2286 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2287 if (CONST_INT_P (x1))
2288 {
2289 if (CONST_INT_P (y1))
2290 return memrefs_conflict_p (xsize, x0, ysize, y0,
2291 c - INTVAL (x1) + INTVAL (y1));
2292 else
2293 return memrefs_conflict_p (xsize, x0, ysize, y,
2294 c - INTVAL (x1));
2295 }
2296 else if (CONST_INT_P (y1))
2297 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2298
2299 return -1;
2300 }
2301 else if (CONST_INT_P (x1))
2302 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2303 }
2304 else if (GET_CODE (y) == PLUS)
2305 {
2306 /* The fact that Y is canonicalized means that this
2307 PLUS rtx is canonicalized. */
2308 rtx y0 = XEXP (y, 0);
2309 rtx y1 = XEXP (y, 1);
2310
2311 if (CONST_INT_P (y1))
2312 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2313 else
2314 return -1;
2315 }
2316
2317 if (GET_CODE (x) == GET_CODE (y))
2318 switch (GET_CODE (x))
2319 {
2320 case MULT:
2321 {
2322 /* Handle cases where we expect the second operands to be the
2323 same, and check only whether the first operand would conflict
2324 or not. */
2325 rtx x0, y0;
2326 rtx x1 = canon_rtx (XEXP (x, 1));
2327 rtx y1 = canon_rtx (XEXP (y, 1));
2328 if (! rtx_equal_for_memref_p (x1, y1))
2329 return -1;
2330 x0 = canon_rtx (XEXP (x, 0));
2331 y0 = canon_rtx (XEXP (y, 0));
2332 if (rtx_equal_for_memref_p (x0, y0))
2333 return offset_overlap_p (c, xsize, ysize);
2334
2335 /* Can't properly adjust our sizes. */
2336 if (!CONST_INT_P (x1))
2337 return -1;
2338 xsize /= INTVAL (x1);
2339 ysize /= INTVAL (x1);
2340 c /= INTVAL (x1);
2341 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2342 }
2343
2344 default:
2345 break;
2346 }
2347
2348 /* Deal with alignment ANDs by adjusting offset and size so as to
2349 cover the maximum range, without taking any previously known
2350 alignment into account. Make a size negative after such an
2351 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2352 assume a potential overlap, because they may end up in contiguous
2353 memory locations and the stricter-alignment access may span over
2354 part of both. */
2355 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2356 {
2357 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2358 unsigned HOST_WIDE_INT uc = sc;
2359 if (sc < 0 && -uc == (uc & -uc))
2360 {
2361 if (xsize > 0)
2362 xsize = -xsize;
2363 if (xsize)
2364 xsize += sc + 1;
2365 c -= sc + 1;
2366 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2367 ysize, y, c);
2368 }
2369 }
2370 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2371 {
2372 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2373 unsigned HOST_WIDE_INT uc = sc;
2374 if (sc < 0 && -uc == (uc & -uc))
2375 {
2376 if (ysize > 0)
2377 ysize = -ysize;
2378 if (ysize)
2379 ysize += sc + 1;
2380 c += sc + 1;
2381 return memrefs_conflict_p (xsize, x,
2382 ysize, canon_rtx (XEXP (y, 0)), c);
2383 }
2384 }
2385
2386 if (CONSTANT_P (x))
2387 {
2388 if (CONST_INT_P (x) && CONST_INT_P (y))
2389 {
2390 c += (INTVAL (y) - INTVAL (x));
2391 return offset_overlap_p (c, xsize, ysize);
2392 }
2393
2394 if (GET_CODE (x) == CONST)
2395 {
2396 if (GET_CODE (y) == CONST)
2397 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2398 ysize, canon_rtx (XEXP (y, 0)), c);
2399 else
2400 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2401 ysize, y, c);
2402 }
2403 if (GET_CODE (y) == CONST)
2404 return memrefs_conflict_p (xsize, x, ysize,
2405 canon_rtx (XEXP (y, 0)), c);
2406
2407 /* Assume a potential overlap for symbolic addresses that went
2408 through alignment adjustments (i.e., that have negative
2409 sizes), because we can't know how far they are from each
2410 other. */
2411 if (CONSTANT_P (y))
2412 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2413
2414 return -1;
2415 }
2416
2417 return -1;
2418 }
2419
2420 /* Functions to compute memory dependencies.
2421
2422 Since we process the insns in execution order, we can build tables
2423 to keep track of what registers are fixed (and not aliased), what registers
2424 are varying in known ways, and what registers are varying in unknown
2425 ways.
2426
2427 If both memory references are volatile, then there must always be a
2428 dependence between the two references, since their order can not be
2429 changed. A volatile and non-volatile reference can be interchanged
2430 though.
2431
2432 We also must allow AND addresses, because they may generate accesses
2433 outside the object being referenced. This is used to generate aligned
2434 addresses from unaligned addresses, for instance, the alpha
2435 storeqi_unaligned pattern. */
2436
2437 /* Read dependence: X is read after read in MEM takes place. There can
2438 only be a dependence here if both reads are volatile, or if either is
2439 an explicit barrier. */
2440
2441 int
2442 read_dependence (const_rtx mem, const_rtx x)
2443 {
2444 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2445 return true;
2446 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2447 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2448 return true;
2449 return false;
2450 }
2451
2452 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2453
2454 static tree
2455 decl_for_component_ref (tree x)
2456 {
2457 do
2458 {
2459 x = TREE_OPERAND (x, 0);
2460 }
2461 while (x && TREE_CODE (x) == COMPONENT_REF);
2462
2463 return x && DECL_P (x) ? x : NULL_TREE;
2464 }
2465
2466 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2467 for the offset of the field reference. *KNOWN_P says whether the
2468 offset is known. */
2469
2470 static void
2471 adjust_offset_for_component_ref (tree x, bool *known_p,
2472 HOST_WIDE_INT *offset)
2473 {
2474 if (!*known_p)
2475 return;
2476 do
2477 {
2478 tree xoffset = component_ref_field_offset (x);
2479 tree field = TREE_OPERAND (x, 1);
2480 if (TREE_CODE (xoffset) != INTEGER_CST)
2481 {
2482 *known_p = false;
2483 return;
2484 }
2485
2486 offset_int woffset
2487 = (wi::to_offset (xoffset)
2488 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2489 LOG2_BITS_PER_UNIT));
2490 if (!wi::fits_uhwi_p (woffset))
2491 {
2492 *known_p = false;
2493 return;
2494 }
2495 *offset += woffset.to_uhwi ();
2496
2497 x = TREE_OPERAND (x, 0);
2498 }
2499 while (x && TREE_CODE (x) == COMPONENT_REF);
2500 }
2501
2502 /* Return nonzero if we can determine the exprs corresponding to memrefs
2503 X and Y and they do not overlap.
2504 If LOOP_VARIANT is set, skip offset-based disambiguation */
2505
2506 int
2507 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2508 {
2509 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2510 rtx rtlx, rtly;
2511 rtx basex, basey;
2512 bool moffsetx_known_p, moffsety_known_p;
2513 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2514 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2515
2516 /* Unless both have exprs, we can't tell anything. */
2517 if (exprx == 0 || expry == 0)
2518 return 0;
2519
2520 /* For spill-slot accesses make sure we have valid offsets. */
2521 if ((exprx == get_spill_slot_decl (false)
2522 && ! MEM_OFFSET_KNOWN_P (x))
2523 || (expry == get_spill_slot_decl (false)
2524 && ! MEM_OFFSET_KNOWN_P (y)))
2525 return 0;
2526
2527 /* If the field reference test failed, look at the DECLs involved. */
2528 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2529 if (moffsetx_known_p)
2530 moffsetx = MEM_OFFSET (x);
2531 if (TREE_CODE (exprx) == COMPONENT_REF)
2532 {
2533 tree t = decl_for_component_ref (exprx);
2534 if (! t)
2535 return 0;
2536 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2537 exprx = t;
2538 }
2539
2540 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2541 if (moffsety_known_p)
2542 moffsety = MEM_OFFSET (y);
2543 if (TREE_CODE (expry) == COMPONENT_REF)
2544 {
2545 tree t = decl_for_component_ref (expry);
2546 if (! t)
2547 return 0;
2548 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2549 expry = t;
2550 }
2551
2552 if (! DECL_P (exprx) || ! DECL_P (expry))
2553 return 0;
2554
2555 /* With invalid code we can end up storing into the constant pool.
2556 Bail out to avoid ICEing when creating RTL for this.
2557 See gfortran.dg/lto/20091028-2_0.f90. */
2558 if (TREE_CODE (exprx) == CONST_DECL
2559 || TREE_CODE (expry) == CONST_DECL)
2560 return 1;
2561
2562 rtlx = DECL_RTL (exprx);
2563 rtly = DECL_RTL (expry);
2564
2565 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2566 can't overlap unless they are the same because we never reuse that part
2567 of the stack frame used for locals for spilled pseudos. */
2568 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2569 && ! rtx_equal_p (rtlx, rtly))
2570 return 1;
2571
2572 /* If we have MEMs referring to different address spaces (which can
2573 potentially overlap), we cannot easily tell from the addresses
2574 whether the references overlap. */
2575 if (MEM_P (rtlx) && MEM_P (rtly)
2576 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2577 return 0;
2578
2579 /* Get the base and offsets of both decls. If either is a register, we
2580 know both are and are the same, so use that as the base. The only
2581 we can avoid overlap is if we can deduce that they are nonoverlapping
2582 pieces of that decl, which is very rare. */
2583 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2584 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2585 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2586
2587 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2588 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2589 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2590
2591 /* If the bases are different, we know they do not overlap if both
2592 are constants or if one is a constant and the other a pointer into the
2593 stack frame. Otherwise a different base means we can't tell if they
2594 overlap or not. */
2595 if (! rtx_equal_p (basex, basey))
2596 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2597 || (CONSTANT_P (basex) && REG_P (basey)
2598 && REGNO_PTR_FRAME_P (REGNO (basey)))
2599 || (CONSTANT_P (basey) && REG_P (basex)
2600 && REGNO_PTR_FRAME_P (REGNO (basex))));
2601
2602 /* Offset based disambiguation not appropriate for loop invariant */
2603 if (loop_invariant)
2604 return 0;
2605
2606 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2607 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2608 : -1);
2609 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2610 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2611 : -1);
2612
2613 /* If we have an offset for either memref, it can update the values computed
2614 above. */
2615 if (moffsetx_known_p)
2616 offsetx += moffsetx, sizex -= moffsetx;
2617 if (moffsety_known_p)
2618 offsety += moffsety, sizey -= moffsety;
2619
2620 /* If a memref has both a size and an offset, we can use the smaller size.
2621 We can't do this if the offset isn't known because we must view this
2622 memref as being anywhere inside the DECL's MEM. */
2623 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2624 sizex = MEM_SIZE (x);
2625 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2626 sizey = MEM_SIZE (y);
2627
2628 /* Put the values of the memref with the lower offset in X's values. */
2629 if (offsetx > offsety)
2630 {
2631 tem = offsetx, offsetx = offsety, offsety = tem;
2632 tem = sizex, sizex = sizey, sizey = tem;
2633 }
2634
2635 /* If we don't know the size of the lower-offset value, we can't tell
2636 if they conflict. Otherwise, we do the test. */
2637 return sizex >= 0 && offsety >= offsetx + sizex;
2638 }
2639
2640 /* Helper for true_dependence and canon_true_dependence.
2641 Checks for true dependence: X is read after store in MEM takes place.
2642
2643 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2644 NULL_RTX, and the canonical addresses of MEM and X are both computed
2645 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2646
2647 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2648
2649 Returns 1 if there is a true dependence, 0 otherwise. */
2650
2651 static int
2652 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2653 const_rtx x, rtx x_addr, bool mem_canonicalized)
2654 {
2655 rtx true_mem_addr;
2656 rtx base;
2657 int ret;
2658
2659 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2660 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2661
2662 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2663 return 1;
2664
2665 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2666 This is used in epilogue deallocation functions, and in cselib. */
2667 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2668 return 1;
2669 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2670 return 1;
2671 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2672 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2673 return 1;
2674
2675 if (! x_addr)
2676 x_addr = XEXP (x, 0);
2677 x_addr = get_addr (x_addr);
2678
2679 if (! mem_addr)
2680 {
2681 mem_addr = XEXP (mem, 0);
2682 if (mem_mode == VOIDmode)
2683 mem_mode = GET_MODE (mem);
2684 }
2685 true_mem_addr = get_addr (mem_addr);
2686
2687 /* Read-only memory is by definition never modified, and therefore can't
2688 conflict with anything. However, don't assume anything when AND
2689 addresses are involved and leave to the code below to determine
2690 dependence. We don't expect to find read-only set on MEM, but
2691 stupid user tricks can produce them, so don't die. */
2692 if (MEM_READONLY_P (x)
2693 && GET_CODE (x_addr) != AND
2694 && GET_CODE (true_mem_addr) != AND)
2695 return 0;
2696
2697 /* If we have MEMs referring to different address spaces (which can
2698 potentially overlap), we cannot easily tell from the addresses
2699 whether the references overlap. */
2700 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2701 return 1;
2702
2703 base = find_base_term (x_addr);
2704 if (base && (GET_CODE (base) == LABEL_REF
2705 || (GET_CODE (base) == SYMBOL_REF
2706 && CONSTANT_POOL_ADDRESS_P (base))))
2707 return 0;
2708
2709 rtx mem_base = find_base_term (true_mem_addr);
2710 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2711 GET_MODE (x), mem_mode))
2712 return 0;
2713
2714 x_addr = canon_rtx (x_addr);
2715 if (!mem_canonicalized)
2716 mem_addr = canon_rtx (true_mem_addr);
2717
2718 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2719 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2720 return ret;
2721
2722 if (mems_in_disjoint_alias_sets_p (x, mem))
2723 return 0;
2724
2725 if (nonoverlapping_memrefs_p (mem, x, false))
2726 return 0;
2727
2728 return rtx_refs_may_alias_p (x, mem, true);
2729 }
2730
2731 /* True dependence: X is read after store in MEM takes place. */
2732
2733 int
2734 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
2735 {
2736 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2737 x, NULL_RTX, /*mem_canonicalized=*/false);
2738 }
2739
2740 /* Canonical true dependence: X is read after store in MEM takes place.
2741 Variant of true_dependence which assumes MEM has already been
2742 canonicalized (hence we no longer do that here).
2743 The mem_addr argument has been added, since true_dependence_1 computed
2744 this value prior to canonicalizing. */
2745
2746 int
2747 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2748 const_rtx x, rtx x_addr)
2749 {
2750 return true_dependence_1 (mem, mem_mode, mem_addr,
2751 x, x_addr, /*mem_canonicalized=*/true);
2752 }
2753
2754 /* Returns nonzero if a write to X might alias a previous read from
2755 (or, if WRITEP is true, a write to) MEM.
2756 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2757 and X_MODE the mode for that access.
2758 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2759
2760 static int
2761 write_dependence_p (const_rtx mem,
2762 const_rtx x, machine_mode x_mode, rtx x_addr,
2763 bool mem_canonicalized, bool x_canonicalized, bool writep)
2764 {
2765 rtx mem_addr;
2766 rtx true_mem_addr, true_x_addr;
2767 rtx base;
2768 int ret;
2769
2770 gcc_checking_assert (x_canonicalized
2771 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2772 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2773
2774 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2775 return 1;
2776
2777 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2778 This is used in epilogue deallocation functions. */
2779 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2780 return 1;
2781 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2782 return 1;
2783 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2784 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2785 return 1;
2786
2787 if (!x_addr)
2788 x_addr = XEXP (x, 0);
2789 true_x_addr = get_addr (x_addr);
2790
2791 mem_addr = XEXP (mem, 0);
2792 true_mem_addr = get_addr (mem_addr);
2793
2794 /* A read from read-only memory can't conflict with read-write memory.
2795 Don't assume anything when AND addresses are involved and leave to
2796 the code below to determine dependence. */
2797 if (!writep
2798 && MEM_READONLY_P (mem)
2799 && GET_CODE (true_x_addr) != AND
2800 && GET_CODE (true_mem_addr) != AND)
2801 return 0;
2802
2803 /* If we have MEMs referring to different address spaces (which can
2804 potentially overlap), we cannot easily tell from the addresses
2805 whether the references overlap. */
2806 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2807 return 1;
2808
2809 base = find_base_term (true_mem_addr);
2810 if (! writep
2811 && base
2812 && (GET_CODE (base) == LABEL_REF
2813 || (GET_CODE (base) == SYMBOL_REF
2814 && CONSTANT_POOL_ADDRESS_P (base))))
2815 return 0;
2816
2817 rtx x_base = find_base_term (true_x_addr);
2818 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2819 GET_MODE (x), GET_MODE (mem)))
2820 return 0;
2821
2822 if (!x_canonicalized)
2823 {
2824 x_addr = canon_rtx (true_x_addr);
2825 x_mode = GET_MODE (x);
2826 }
2827 if (!mem_canonicalized)
2828 mem_addr = canon_rtx (true_mem_addr);
2829
2830 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2831 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2832 return ret;
2833
2834 if (nonoverlapping_memrefs_p (x, mem, false))
2835 return 0;
2836
2837 return rtx_refs_may_alias_p (x, mem, false);
2838 }
2839
2840 /* Anti dependence: X is written after read in MEM takes place. */
2841
2842 int
2843 anti_dependence (const_rtx mem, const_rtx x)
2844 {
2845 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2846 /*mem_canonicalized=*/false,
2847 /*x_canonicalized*/false, /*writep=*/false);
2848 }
2849
2850 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2851 Also, consider X in X_MODE (which might be from an enclosing
2852 STRICT_LOW_PART / ZERO_EXTRACT).
2853 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2854
2855 int
2856 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2857 const_rtx x, machine_mode x_mode, rtx x_addr)
2858 {
2859 return write_dependence_p (mem, x, x_mode, x_addr,
2860 mem_canonicalized, /*x_canonicalized=*/true,
2861 /*writep=*/false);
2862 }
2863
2864 /* Output dependence: X is written after store in MEM takes place. */
2865
2866 int
2867 output_dependence (const_rtx mem, const_rtx x)
2868 {
2869 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2870 /*mem_canonicalized=*/false,
2871 /*x_canonicalized*/false, /*writep=*/true);
2872 }
2873 \f
2874
2875
2876 /* Check whether X may be aliased with MEM. Don't do offset-based
2877 memory disambiguation & TBAA. */
2878 int
2879 may_alias_p (const_rtx mem, const_rtx x)
2880 {
2881 rtx x_addr, mem_addr;
2882
2883 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2884 return 1;
2885
2886 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2887 This is used in epilogue deallocation functions. */
2888 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2889 return 1;
2890 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2891 return 1;
2892 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2893 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2894 return 1;
2895
2896 x_addr = XEXP (x, 0);
2897 x_addr = get_addr (x_addr);
2898
2899 mem_addr = XEXP (mem, 0);
2900 mem_addr = get_addr (mem_addr);
2901
2902 /* Read-only memory is by definition never modified, and therefore can't
2903 conflict with anything. However, don't assume anything when AND
2904 addresses are involved and leave to the code below to determine
2905 dependence. We don't expect to find read-only set on MEM, but
2906 stupid user tricks can produce them, so don't die. */
2907 if (MEM_READONLY_P (x)
2908 && GET_CODE (x_addr) != AND
2909 && GET_CODE (mem_addr) != AND)
2910 return 0;
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 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2916 return 1;
2917
2918 rtx x_base = find_base_term (x_addr);
2919 rtx mem_base = find_base_term (mem_addr);
2920 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2921 GET_MODE (x), GET_MODE (mem_addr)))
2922 return 0;
2923
2924 if (nonoverlapping_memrefs_p (mem, x, true))
2925 return 0;
2926
2927 /* TBAA not valid for loop_invarint */
2928 return rtx_refs_may_alias_p (x, mem, false);
2929 }
2930
2931 void
2932 init_alias_target (void)
2933 {
2934 int i;
2935
2936 if (!arg_base_value)
2937 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2938
2939 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2940
2941 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2942 /* Check whether this register can hold an incoming pointer
2943 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2944 numbers, so translate if necessary due to register windows. */
2945 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2946 && HARD_REGNO_MODE_OK (i, Pmode))
2947 static_reg_base_value[i] = arg_base_value;
2948
2949 static_reg_base_value[STACK_POINTER_REGNUM]
2950 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2951 static_reg_base_value[ARG_POINTER_REGNUM]
2952 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2953 static_reg_base_value[FRAME_POINTER_REGNUM]
2954 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2955 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2956 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2957 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2958 }
2959
2960 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2961 to be memory reference. */
2962 static bool memory_modified;
2963 static void
2964 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2965 {
2966 if (MEM_P (x))
2967 {
2968 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2969 memory_modified = true;
2970 }
2971 }
2972
2973
2974 /* Return true when INSN possibly modify memory contents of MEM
2975 (i.e. address can be modified). */
2976 bool
2977 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2978 {
2979 if (!INSN_P (insn))
2980 return false;
2981 memory_modified = false;
2982 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2983 return memory_modified;
2984 }
2985
2986 /* Return TRUE if the destination of a set is rtx identical to
2987 ITEM. */
2988 static inline bool
2989 set_dest_equal_p (const_rtx set, const_rtx item)
2990 {
2991 rtx dest = SET_DEST (set);
2992 return rtx_equal_p (dest, item);
2993 }
2994
2995 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2996 *DEFINITELY* modify the memory contents of MEM. */
2997 bool
2998 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2999 {
3000 if (!INSN_P (insn))
3001 return false;
3002 insn = PATTERN (insn);
3003 if (GET_CODE (insn) == SET)
3004 return set_dest_equal_p (insn, mem);
3005 else if (GET_CODE (insn) == PARALLEL)
3006 {
3007 int i;
3008 for (i = 0; i < XVECLEN (insn, 0); i++)
3009 {
3010 rtx sub = XVECEXP (insn, 0, i);
3011 if (GET_CODE (sub) == SET
3012 && set_dest_equal_p (sub, mem))
3013 return true;
3014 }
3015 }
3016 return false;
3017 }
3018
3019 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3020 array. */
3021
3022 void
3023 init_alias_analysis (void)
3024 {
3025 unsigned int maxreg = max_reg_num ();
3026 int changed, pass;
3027 int i;
3028 unsigned int ui;
3029 rtx_insn *insn;
3030 rtx val;
3031 int rpo_cnt;
3032 int *rpo;
3033
3034 timevar_push (TV_ALIAS_ANALYSIS);
3035
3036 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
3037 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3038 bitmap_clear (reg_known_equiv_p);
3039
3040 /* If we have memory allocated from the previous run, use it. */
3041 if (old_reg_base_value)
3042 reg_base_value = old_reg_base_value;
3043
3044 if (reg_base_value)
3045 reg_base_value->truncate (0);
3046
3047 vec_safe_grow_cleared (reg_base_value, maxreg);
3048
3049 new_reg_base_value = XNEWVEC (rtx, maxreg);
3050 reg_seen = sbitmap_alloc (maxreg);
3051
3052 /* The basic idea is that each pass through this loop will use the
3053 "constant" information from the previous pass to propagate alias
3054 information through another level of assignments.
3055
3056 The propagation is done on the CFG in reverse post-order, to propagate
3057 things forward as far as possible in each iteration.
3058
3059 This could get expensive if the assignment chains are long. Maybe
3060 we should throttle the number of iterations, possibly based on
3061 the optimization level or flag_expensive_optimizations.
3062
3063 We could propagate more information in the first pass by making use
3064 of DF_REG_DEF_COUNT to determine immediately that the alias information
3065 for a pseudo is "constant".
3066
3067 A program with an uninitialized variable can cause an infinite loop
3068 here. Instead of doing a full dataflow analysis to detect such problems
3069 we just cap the number of iterations for the loop.
3070
3071 The state of the arrays for the set chain in question does not matter
3072 since the program has undefined behavior. */
3073
3074 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3075 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3076
3077 pass = 0;
3078 do
3079 {
3080 /* Assume nothing will change this iteration of the loop. */
3081 changed = 0;
3082
3083 /* We want to assign the same IDs each iteration of this loop, so
3084 start counting from one each iteration of the loop. */
3085 unique_id = 1;
3086
3087 /* We're at the start of the function each iteration through the
3088 loop, so we're copying arguments. */
3089 copying_arguments = true;
3090
3091 /* Wipe the potential alias information clean for this pass. */
3092 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3093
3094 /* Wipe the reg_seen array clean. */
3095 bitmap_clear (reg_seen);
3096
3097 /* Initialize the alias information for this pass. */
3098 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3099 if (static_reg_base_value[i])
3100 {
3101 new_reg_base_value[i] = static_reg_base_value[i];
3102 bitmap_set_bit (reg_seen, i);
3103 }
3104
3105 /* Walk the insns adding values to the new_reg_base_value array. */
3106 for (i = 0; i < rpo_cnt; i++)
3107 {
3108 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3109 FOR_BB_INSNS (bb, insn)
3110 {
3111 if (NONDEBUG_INSN_P (insn))
3112 {
3113 rtx note, set;
3114
3115 #if defined (HAVE_prologue)
3116 static const bool prologue = true;
3117 #else
3118 static const bool prologue = false;
3119 #endif
3120
3121 /* The prologue/epilogue insns are not threaded onto the
3122 insn chain until after reload has completed. Thus,
3123 there is no sense wasting time checking if INSN is in
3124 the prologue/epilogue until after reload has completed. */
3125 if ((prologue || HAVE_epilogue) && reload_completed
3126 && prologue_epilogue_contains (insn))
3127 continue;
3128
3129 /* If this insn has a noalias note, process it, Otherwise,
3130 scan for sets. A simple set will have no side effects
3131 which could change the base value of any other register. */
3132
3133 if (GET_CODE (PATTERN (insn)) == SET
3134 && REG_NOTES (insn) != 0
3135 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3136 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3137 else
3138 note_stores (PATTERN (insn), record_set, NULL);
3139
3140 set = single_set (insn);
3141
3142 if (set != 0
3143 && REG_P (SET_DEST (set))
3144 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3145 {
3146 unsigned int regno = REGNO (SET_DEST (set));
3147 rtx src = SET_SRC (set);
3148 rtx t;
3149
3150 note = find_reg_equal_equiv_note (insn);
3151 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3152 && DF_REG_DEF_COUNT (regno) != 1)
3153 note = NULL_RTX;
3154
3155 if (note != NULL_RTX
3156 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3157 && ! rtx_varies_p (XEXP (note, 0), 1)
3158 && ! reg_overlap_mentioned_p (SET_DEST (set),
3159 XEXP (note, 0)))
3160 {
3161 set_reg_known_value (regno, XEXP (note, 0));
3162 set_reg_known_equiv_p (regno,
3163 REG_NOTE_KIND (note) == REG_EQUIV);
3164 }
3165 else if (DF_REG_DEF_COUNT (regno) == 1
3166 && GET_CODE (src) == PLUS
3167 && REG_P (XEXP (src, 0))
3168 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3169 && CONST_INT_P (XEXP (src, 1)))
3170 {
3171 t = plus_constant (GET_MODE (src), t,
3172 INTVAL (XEXP (src, 1)));
3173 set_reg_known_value (regno, t);
3174 set_reg_known_equiv_p (regno, false);
3175 }
3176 else if (DF_REG_DEF_COUNT (regno) == 1
3177 && ! rtx_varies_p (src, 1))
3178 {
3179 set_reg_known_value (regno, src);
3180 set_reg_known_equiv_p (regno, false);
3181 }
3182 }
3183 }
3184 else if (NOTE_P (insn)
3185 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3186 copying_arguments = false;
3187 }
3188 }
3189
3190 /* Now propagate values from new_reg_base_value to reg_base_value. */
3191 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3192
3193 for (ui = 0; ui < maxreg; ui++)
3194 {
3195 if (new_reg_base_value[ui]
3196 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3197 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3198 {
3199 (*reg_base_value)[ui] = new_reg_base_value[ui];
3200 changed = 1;
3201 }
3202 }
3203 }
3204 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3205 XDELETEVEC (rpo);
3206
3207 /* Fill in the remaining entries. */
3208 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3209 {
3210 int regno = i + FIRST_PSEUDO_REGISTER;
3211 if (! val)
3212 set_reg_known_value (regno, regno_reg_rtx[regno]);
3213 }
3214
3215 /* Clean up. */
3216 free (new_reg_base_value);
3217 new_reg_base_value = 0;
3218 sbitmap_free (reg_seen);
3219 reg_seen = 0;
3220 timevar_pop (TV_ALIAS_ANALYSIS);
3221 }
3222
3223 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3224 Special API for var-tracking pass purposes. */
3225
3226 void
3227 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3228 {
3229 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3230 }
3231
3232 void
3233 end_alias_analysis (void)
3234 {
3235 old_reg_base_value = reg_base_value;
3236 vec_free (reg_known_value);
3237 sbitmap_free (reg_known_equiv_p);
3238 }
3239
3240 void
3241 dump_alias_stats_in_alias_c (FILE *s)
3242 {
3243 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3244 " %llu are in alias set 0\n"
3245 " %llu queries asked about the same object\n"
3246 " %llu queries asked about the same alias set\n"
3247 " %llu access volatile\n"
3248 " %llu are dependent in the DAG\n"
3249 " %llu are aritificially in conflict with void *\n",
3250 alias_stats.num_disambiguated,
3251 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3252 + alias_stats.num_same_objects + alias_stats.num_volatile
3253 + alias_stats.num_dag + alias_stats.num_disambiguated
3254 + alias_stats.num_universal,
3255 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3256 alias_stats.num_same_objects, alias_stats.num_volatile,
3257 alias_stats.num_dag, alias_stats.num_universal);
3258 }
3259 #include "gt-alias.h"