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