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