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