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