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