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