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