]> gcc.gnu.org Git - gcc.git/blame - gcc/reload1.c
ChangeLog.1: Fix typos.
[gcc.git] / gcc / reload1.c
CommitLineData
32131a9c 1/* Reload pseudo regs into hard regs for insns that require hard regs.
af841dbd 2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
f4f4d0f8 3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
32131a9c 4
1322177d 5This file is part of GCC.
32131a9c 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
32131a9c 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
32131a9c
RK
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
32131a9c 21
32131a9c 22#include "config.h"
670ee920 23#include "system.h"
cab634f2
KG
24
25#include "machmode.h"
26#include "hard-reg-set.h"
32131a9c 27#include "rtl.h"
6baf1cc8 28#include "tm_p.h"
32131a9c
RK
29#include "obstack.h"
30#include "insn-config.h"
32131a9c 31#include "flags.h"
49ad7cfa 32#include "function.h"
32131a9c 33#include "expr.h"
e78d8e51 34#include "optabs.h"
32131a9c 35#include "regs.h"
cad6f7d0 36#include "basic-block.h"
32131a9c
RK
37#include "reload.h"
38#include "recog.h"
32131a9c 39#include "output.h"
eab5c70a 40#include "cselib.h"
a9c366bf 41#include "real.h"
10f0ad3d 42#include "toplev.h"
39f95a2c 43#include "except.h"
a20fd5ac 44#include "tree.h"
32131a9c
RK
45
46/* This file contains the reload pass of the compiler, which is
47 run after register allocation has been done. It checks that
48 each insn is valid (operands required to be in registers really
49 are in registers of the proper class) and fixes up invalid ones
50 by copying values temporarily into registers for the insns
51 that need them.
52
53 The results of register allocation are described by the vector
54 reg_renumber; the insns still contain pseudo regs, but reg_renumber
55 can be used to find which hard reg, if any, a pseudo reg is in.
56
57 The technique we always use is to free up a few hard regs that are
58 called ``reload regs'', and for each place where a pseudo reg
59 must be in a hard reg, copy it temporarily into one of the reload regs.
60
03acd8f8
BS
61 Reload regs are allocated locally for every instruction that needs
62 reloads. When there are pseudos which are allocated to a register that
63 has been chosen as a reload reg, such pseudos must be ``spilled''.
64 This means that they go to other hard regs, or to stack slots if no other
32131a9c
RK
65 available hard regs can be found. Spilling can invalidate more
66 insns, requiring additional need for reloads, so we must keep checking
67 until the process stabilizes.
68
69 For machines with different classes of registers, we must keep track
70 of the register class needed for each reload, and make sure that
71 we allocate enough reload registers of each class.
72
73 The file reload.c contains the code that checks one insn for
74 validity and reports the reloads that it needs. This file
75 is in charge of scanning the entire rtl code, accumulating the
76 reload needs, spilling, assigning reload registers to use for
77 fixing up each insn, and generating the new insns to copy values
78 into the reload registers. */
546b63fb 79
546b63fb 80#ifndef REGISTER_MOVE_COST
e56b4594 81#define REGISTER_MOVE_COST(m, x, y) 2
546b63fb 82#endif
2a3e384f
RH
83
84#ifndef LOCAL_REGNO
85#define LOCAL_REGNO(REGNO) 0
86#endif
32131a9c
RK
87\f
88/* During reload_as_needed, element N contains a REG rtx for the hard reg
0f41302f 89 into which reg N has been reloaded (perhaps for a previous insn). */
32131a9c
RK
90static rtx *reg_last_reload_reg;
91
92/* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
93 for an output reload that stores into reg N. */
94static char *reg_has_output_reload;
95
96/* Indicates which hard regs are reload-registers for an output reload
97 in the current insn. */
98static HARD_REG_SET reg_is_output_reload;
99
100/* Element N is the constant value to which pseudo reg N is equivalent,
101 or zero if pseudo reg N is not equivalent to a constant.
102 find_reloads looks at this in order to replace pseudo reg N
103 with the constant it stands for. */
104rtx *reg_equiv_constant;
105
106/* Element N is a memory location to which pseudo reg N is equivalent,
107 prior to any register elimination (such as frame pointer to stack
108 pointer). Depending on whether or not it is a valid address, this value
109 is transferred to either reg_equiv_address or reg_equiv_mem. */
4803a34a 110rtx *reg_equiv_memory_loc;
32131a9c
RK
111
112/* Element N is the address of stack slot to which pseudo reg N is equivalent.
113 This is used when the address is not valid as a memory address
114 (because its displacement is too big for the machine.) */
115rtx *reg_equiv_address;
116
117/* Element N is the memory slot to which pseudo reg N is equivalent,
118 or zero if pseudo reg N is not equivalent to a memory slot. */
119rtx *reg_equiv_mem;
120
121/* Widest width in which each pseudo reg is referred to (via subreg). */
770ae6cc 122static unsigned int *reg_max_ref_width;
32131a9c 123
135eb61c 124/* Element N is the list of insns that initialized reg N from its equivalent
32131a9c
RK
125 constant or memory slot. */
126static rtx *reg_equiv_init;
127
03acd8f8
BS
128/* Vector to remember old contents of reg_renumber before spilling. */
129static short *reg_old_renumber;
130
e6e52be0 131/* During reload_as_needed, element N contains the last pseudo regno reloaded
03acd8f8 132 into hard register N. If that pseudo reg occupied more than one register,
32131a9c
RK
133 reg_reloaded_contents points to that pseudo for each spill register in
134 use; all of these must remain set for an inheritance to occur. */
135static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
136
137/* During reload_as_needed, element N contains the insn for which
e6e52be0
R
138 hard register N was last used. Its contents are significant only
139 when reg_reloaded_valid is set for this register. */
32131a9c
RK
140static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
141
3eae4643 142/* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
e6e52be0
R
143static HARD_REG_SET reg_reloaded_valid;
144/* Indicate if the register was dead at the end of the reload.
145 This is only valid if reg_reloaded_contents is set and valid. */
146static HARD_REG_SET reg_reloaded_dead;
147
32131a9c
RK
148/* Number of spill-regs so far; number of valid elements of spill_regs. */
149static int n_spills;
150
151/* In parallel with spill_regs, contains REG rtx's for those regs.
152 Holds the last rtx used for any given reg, or 0 if it has never
153 been used for spilling yet. This rtx is reused, provided it has
154 the proper mode. */
155static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
156
157/* In parallel with spill_regs, contains nonzero for a spill reg
158 that was stored after the last time it was used.
159 The precise value is the insn generated to do the store. */
160static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
161
cb2afeb3
R
162/* This is the register that was stored with spill_reg_store. This is a
163 copy of reload_out / reload_out_reg when the value was stored; if
164 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
165static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
166
32131a9c
RK
167/* This table is the inverse mapping of spill_regs:
168 indexed by hard reg number,
169 it contains the position of that reg in spill_regs,
05d10675 170 or -1 for something that is not in spill_regs.
13c8e8e3
JL
171
172 ?!? This is no longer accurate. */
32131a9c
RK
173static short spill_reg_order[FIRST_PSEUDO_REGISTER];
174
03acd8f8
BS
175/* This reg set indicates registers that can't be used as spill registers for
176 the currently processed insn. These are the hard registers which are live
177 during the insn, but not allocated to pseudos, as well as fixed
178 registers. */
32131a9c
RK
179static HARD_REG_SET bad_spill_regs;
180
03acd8f8
BS
181/* These are the hard registers that can't be used as spill register for any
182 insn. This includes registers used for user variables and registers that
183 we can't eliminate. A register that appears in this set also can't be used
184 to retry register allocation. */
185static HARD_REG_SET bad_spill_regs_global;
186
32131a9c 187/* Describes order of use of registers for reloading
03acd8f8
BS
188 of spilled pseudo-registers. `n_spills' is the number of
189 elements that are actually valid; new ones are added at the end.
190
191 Both spill_regs and spill_reg_order are used on two occasions:
192 once during find_reload_regs, where they keep track of the spill registers
193 for a single insn, but also during reload_as_needed where they show all
194 the registers ever used by reload. For the latter case, the information
195 is calculated during finish_spills. */
32131a9c
RK
196static short spill_regs[FIRST_PSEUDO_REGISTER];
197
03acd8f8
BS
198/* This vector of reg sets indicates, for each pseudo, which hard registers
199 may not be used for retrying global allocation because the register was
200 formerly spilled from one of them. If we allowed reallocating a pseudo to
201 a register that it was already allocated to, reload might not
202 terminate. */
203static HARD_REG_SET *pseudo_previous_regs;
204
205/* This vector of reg sets indicates, for each pseudo, which hard
206 registers may not be used for retrying global allocation because they
207 are used as spill registers during one of the insns in which the
208 pseudo is live. */
209static HARD_REG_SET *pseudo_forbidden_regs;
210
211/* All hard regs that have been used as spill registers for any insn are
212 marked in this set. */
213static HARD_REG_SET used_spill_regs;
8b4f9969 214
4079cd63
JW
215/* Index of last register assigned as a spill register. We allocate in
216 a round-robin fashion. */
4079cd63
JW
217static int last_spill_reg;
218
32131a9c
RK
219/* Nonzero if indirect addressing is supported on the machine; this means
220 that spilling (REG n) does not require reloading it into a register in
221 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
222 value indicates the level of indirect addressing supported, e.g., two
223 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
224 a hard register. */
32131a9c
RK
225static char spill_indirect_levels;
226
227/* Nonzero if indirect addressing is supported when the innermost MEM is
228 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
6d2f8887 229 which these are valid is the same as spill_indirect_levels, above. */
32131a9c
RK
230char indirect_symref_ok;
231
232/* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
32131a9c
RK
233char double_reg_address_ok;
234
235/* Record the stack slot for each spilled hard register. */
32131a9c
RK
236static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
237
238/* Width allocated so far for that stack slot. */
770ae6cc 239static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
32131a9c 240
7609e720 241/* Record which pseudos needed to be spilled. */
f5d8c9f4
BS
242static regset_head spilled_pseudos;
243
244/* Used for communication between order_regs_for_reload and count_pseudo.
245 Used to avoid counting one pseudo twice. */
246static regset_head pseudos_counted;
7609e720 247
32131a9c
RK
248/* First uid used by insns created by reload in this function.
249 Used in find_equiv_reg. */
250int reload_first_uid;
251
252/* Flag set by local-alloc or global-alloc if anything is live in
253 a call-clobbered reg across calls. */
32131a9c
RK
254int caller_save_needed;
255
256/* Set to 1 while reload_as_needed is operating.
257 Required by some machines to handle any generated moves differently. */
32131a9c
RK
258int reload_in_progress = 0;
259
260/* These arrays record the insn_code of insns that may be needed to
261 perform input and output reloads of special objects. They provide a
262 place to pass a scratch register. */
32131a9c
RK
263enum insn_code reload_in_optab[NUM_MACHINE_MODES];
264enum insn_code reload_out_optab[NUM_MACHINE_MODES];
265
d45cf215 266/* This obstack is used for allocation of rtl during register elimination.
32131a9c
RK
267 The allocated storage can be freed once find_reloads has processed the
268 insn. */
32131a9c 269struct obstack reload_obstack;
cad6f7d0
BS
270
271/* Points to the beginning of the reload_obstack. All insn_chain structures
272 are allocated first. */
273char *reload_startobj;
274
275/* The point after all insn_chain structures. Used to quickly deallocate
f5d8c9f4 276 memory allocated in copy_reloads during calculate_needs_all_insns. */
32131a9c
RK
277char *reload_firstobj;
278
f5d8c9f4
BS
279/* This points before all local rtl generated by register elimination.
280 Used to quickly free all memory after processing one insn. */
281static char *reload_insn_firstobj;
282
cad6f7d0
BS
283/* List of insn_chain instructions, one for every insn that reload needs to
284 examine. */
285struct insn_chain *reload_insn_chain;
7609e720 286
dfb7c80f
JL
287#ifdef TREE_CODE
288extern tree current_function_decl;
289#else
122a860e 290extern union tree_node *current_function_decl;
dfb7c80f
JL
291#endif
292
03acd8f8 293/* List of all insns needing reloads. */
7609e720 294static struct insn_chain *insns_need_reload;
32131a9c
RK
295\f
296/* This structure is used to record information about register eliminations.
297 Each array entry describes one possible way of eliminating a register
298 in favor of another. If there is more than one way of eliminating a
299 particular register, the most preferred should be specified first. */
300
590cf94d 301struct elim_table
32131a9c 302{
0f41302f
MS
303 int from; /* Register number to be eliminated. */
304 int to; /* Register number used as replacement. */
305 int initial_offset; /* Initial difference between values. */
306 int can_eliminate; /* Non-zero if this elimination can be done. */
32131a9c 307 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
0f41302f
MS
308 insns made by reload. */
309 int offset; /* Current offset between the two regs. */
0f41302f
MS
310 int previous_offset; /* Offset at end of previous insn. */
311 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
32131a9c
RK
312 rtx from_rtx; /* REG rtx for the register to be eliminated.
313 We cannot simply compare the number since
314 we might then spuriously replace a hard
315 register corresponding to a pseudo
0f41302f
MS
316 assigned to the reg to be eliminated. */
317 rtx to_rtx; /* REG rtx for the replacement. */
590cf94d
KG
318};
319
1d7254c5 320static struct elim_table *reg_eliminate = 0;
590cf94d
KG
321
322/* This is an intermediate structure to initialize the table. It has
1d7254c5 323 exactly the members provided by ELIMINABLE_REGS. */
0b5826ac 324static const struct elim_table_1
590cf94d 325{
0b5826ac
KG
326 const int from;
327 const int to;
590cf94d 328} reg_eliminate_1[] =
32131a9c
RK
329
330/* If a set of eliminable registers was specified, define the table from it.
331 Otherwise, default to the normal case of the frame pointer being
332 replaced by the stack pointer. */
333
334#ifdef ELIMINABLE_REGS
335 ELIMINABLE_REGS;
336#else
337 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
338#endif
339
b6a1cbae 340#define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
32131a9c
RK
341
342/* Record the number of pending eliminations that have an offset not equal
40f03658 343 to their initial offset. If nonzero, we use a new copy of each
32131a9c 344 replacement result in any insns encountered. */
cb2afeb3 345int num_not_at_initial_offset;
32131a9c
RK
346
347/* Count the number of registers that we may be able to eliminate. */
348static int num_eliminable;
2b49ee39
R
349/* And the number of registers that are equivalent to a constant that
350 can be eliminated to frame_pointer / arg_pointer + constant. */
351static int num_eliminable_invariants;
32131a9c
RK
352
353/* For each label, we record the offset of each elimination. If we reach
354 a label by more than one path and an offset differs, we cannot do the
355 elimination. This information is indexed by the number of the label.
356 The first table is an array of flags that records whether we have yet
357 encountered a label and the second table is an array of arrays, one
358 entry in the latter array for each elimination. */
359
360static char *offsets_known_at;
361static int (*offsets_at)[NUM_ELIMINABLE_REGS];
362
363/* Number of labels in the current function. */
364
365static int num_labels;
366\f
8e2e89f7
KH
367static void replace_pseudos_in_call_usage PARAMS ((rtx *,
368 enum machine_mode,
369 rtx));
cdadb1dd
KG
370static void maybe_fix_stack_asms PARAMS ((void));
371static void copy_reloads PARAMS ((struct insn_chain *));
372static void calculate_needs_all_insns PARAMS ((int));
e04ca094
JL
373static int find_reg PARAMS ((struct insn_chain *, int));
374static void find_reload_regs PARAMS ((struct insn_chain *));
375static void select_reload_regs PARAMS ((void));
cdadb1dd
KG
376static void delete_caller_save_insns PARAMS ((void));
377
378static void spill_failure PARAMS ((rtx, enum reg_class));
379static void count_spilled_pseudo PARAMS ((int, int, int));
380static void delete_dead_insn PARAMS ((rtx));
174fa2c4 381static void alter_reg PARAMS ((int, int));
cdadb1dd
KG
382static void set_label_offsets PARAMS ((rtx, rtx, int));
383static void check_eliminable_occurrences PARAMS ((rtx));
384static void elimination_effects PARAMS ((rtx, enum machine_mode));
385static int eliminate_regs_in_insn PARAMS ((rtx, int));
386static void update_eliminable_offsets PARAMS ((void));
387static void mark_not_eliminable PARAMS ((rtx, rtx, void *));
388static void set_initial_elim_offsets PARAMS ((void));
389static void verify_initial_elim_offsets PARAMS ((void));
390static void set_initial_label_offsets PARAMS ((void));
391static void set_offsets_for_label PARAMS ((rtx));
392static void init_elim_table PARAMS ((void));
393static void update_eliminables PARAMS ((HARD_REG_SET *));
e04ca094
JL
394static void spill_hard_reg PARAMS ((unsigned int, int));
395static int finish_spills PARAMS ((int));
cdadb1dd
KG
396static void ior_hard_reg_set PARAMS ((HARD_REG_SET *, HARD_REG_SET *));
397static void scan_paradoxical_subregs PARAMS ((rtx));
398static void count_pseudo PARAMS ((int));
399static void order_regs_for_reload PARAMS ((struct insn_chain *));
e04ca094 400static void reload_as_needed PARAMS ((int));
cdadb1dd
KG
401static void forget_old_reloads_1 PARAMS ((rtx, rtx, void *));
402static int reload_reg_class_lower PARAMS ((const PTR, const PTR));
770ae6cc
RK
403static void mark_reload_reg_in_use PARAMS ((unsigned int, int,
404 enum reload_type,
405 enum machine_mode));
406static void clear_reload_reg_in_use PARAMS ((unsigned int, int,
407 enum reload_type,
408 enum machine_mode));
409static int reload_reg_free_p PARAMS ((unsigned int, int,
410 enum reload_type));
304a22dd
R
411static int reload_reg_free_for_value_p PARAMS ((int, int, int,
412 enum reload_type,
770ae6cc 413 rtx, rtx, int, int));
c02cad8f
BS
414static int free_for_value_p PARAMS ((int, enum machine_mode, int,
415 enum reload_type, rtx, rtx,
416 int, int));
770ae6cc
RK
417static int reload_reg_reaches_end_p PARAMS ((unsigned int, int,
418 enum reload_type));
419static int allocate_reload_reg PARAMS ((struct insn_chain *, int,
420 int));
ff6534ad 421static int conflicts_with_override PARAMS ((rtx));
cdadb1dd
KG
422static void failed_reload PARAMS ((rtx, int));
423static int set_reload_reg PARAMS ((int, int));
424static void choose_reload_regs_init PARAMS ((struct insn_chain *, rtx *));
425static void choose_reload_regs PARAMS ((struct insn_chain *));
426static void merge_assigned_reloads PARAMS ((rtx));
427static void emit_input_reload_insns PARAMS ((struct insn_chain *,
770ae6cc 428 struct reload *, rtx, int));
cdadb1dd 429static void emit_output_reload_insns PARAMS ((struct insn_chain *,
770ae6cc 430 struct reload *, int));
cdadb1dd 431static void do_input_reload PARAMS ((struct insn_chain *,
770ae6cc 432 struct reload *, int));
cdadb1dd 433static void do_output_reload PARAMS ((struct insn_chain *,
770ae6cc 434 struct reload *, int));
e04ca094 435static void emit_reload_insns PARAMS ((struct insn_chain *));
cdadb1dd
KG
436static void delete_output_reload PARAMS ((rtx, int, int));
437static void delete_address_reloads PARAMS ((rtx, rtx));
438static void delete_address_reloads_1 PARAMS ((rtx, rtx, rtx));
439static rtx inc_for_reload PARAMS ((rtx, rtx, rtx, int));
cdadb1dd 440static void reload_cse_regs_1 PARAMS ((rtx));
eab5c70a 441static int reload_cse_noop_set_p PARAMS ((rtx));
cdadb1dd 442static int reload_cse_simplify_set PARAMS ((rtx, rtx));
bf1660a6 443static int reload_cse_simplify_operands PARAMS ((rtx, rtx));
770ae6cc
RK
444static void reload_combine PARAMS ((void));
445static void reload_combine_note_use PARAMS ((rtx *, rtx));
446static void reload_combine_note_store PARAMS ((rtx, rtx, void *));
447static void reload_cse_move2add PARAMS ((rtx));
448static void move2add_note_store PARAMS ((rtx, rtx, void *));
2dfa9a87 449#ifdef AUTO_INC_DEC
770ae6cc 450static void add_auto_inc_notes PARAMS ((rtx, rtx));
2dfa9a87 451#endif
94bd63e5 452static void copy_eh_notes PARAMS ((rtx, rtx));
61f5625b 453static HOST_WIDE_INT sext_for_mode PARAMS ((enum machine_mode,
770ae6cc 454 HOST_WIDE_INT));
cdadb1dd
KG
455static void failed_reload PARAMS ((rtx, int));
456static int set_reload_reg PARAMS ((int, int));
bf1660a6 457static void reload_cse_simplify PARAMS ((rtx, rtx));
068473ec 458void fixup_abnormal_edges PARAMS ((void));
e04ca094 459extern void dump_needs PARAMS ((struct insn_chain *));
32131a9c 460\f
546b63fb
RK
461/* Initialize the reload pass once per compilation. */
462
32131a9c
RK
463void
464init_reload ()
465{
b3694847 466 int i;
32131a9c
RK
467
468 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
469 Set spill_indirect_levels to the number of levels such addressing is
470 permitted, zero if it is not permitted at all. */
471
b3694847 472 rtx tem
38a448ca
RH
473 = gen_rtx_MEM (Pmode,
474 gen_rtx_PLUS (Pmode,
c5c76735
JL
475 gen_rtx_REG (Pmode,
476 LAST_VIRTUAL_REGISTER + 1),
38a448ca 477 GEN_INT (4)));
32131a9c
RK
478 spill_indirect_levels = 0;
479
480 while (memory_address_p (QImode, tem))
481 {
482 spill_indirect_levels++;
38a448ca 483 tem = gen_rtx_MEM (Pmode, tem);
32131a9c
RK
484 }
485
486 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
487
38a448ca 488 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
32131a9c
RK
489 indirect_symref_ok = memory_address_p (QImode, tem);
490
491 /* See if reg+reg is a valid (and offsettable) address. */
492
65701fd2 493 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
57caa638 494 {
38a448ca
RH
495 tem = gen_rtx_PLUS (Pmode,
496 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
497 gen_rtx_REG (Pmode, i));
c5c76735 498
57caa638
RS
499 /* This way, we make sure that reg+reg is an offsettable address. */
500 tem = plus_constant (tem, 4);
501
502 if (memory_address_p (QImode, tem))
503 {
504 double_reg_address_ok = 1;
505 break;
506 }
507 }
32131a9c 508
0f41302f 509 /* Initialize obstack for our rtl allocation. */
32131a9c 510 gcc_obstack_init (&reload_obstack);
cad6f7d0 511 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
f5d8c9f4
BS
512
513 INIT_REG_SET (&spilled_pseudos);
514 INIT_REG_SET (&pseudos_counted);
32131a9c
RK
515}
516
cad6f7d0
BS
517/* List of insn chains that are currently unused. */
518static struct insn_chain *unused_insn_chains = 0;
519
520/* Allocate an empty insn_chain structure. */
521struct insn_chain *
522new_insn_chain ()
523{
524 struct insn_chain *c;
525
526 if (unused_insn_chains == 0)
527 {
8db99db2
KG
528 c = (struct insn_chain *)
529 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
239a0f5b
BS
530 INIT_REG_SET (&c->live_throughout);
531 INIT_REG_SET (&c->dead_or_set);
cad6f7d0
BS
532 }
533 else
534 {
535 c = unused_insn_chains;
536 unused_insn_chains = c->next;
537 }
538 c->is_caller_save_insn = 0;
03acd8f8 539 c->need_operand_change = 0;
cad6f7d0
BS
540 c->need_reload = 0;
541 c->need_elim = 0;
542 return c;
543}
544
7609e720
BS
545/* Small utility function to set all regs in hard reg set TO which are
546 allocated to pseudos in regset FROM. */
770ae6cc 547
7609e720
BS
548void
549compute_use_by_pseudos (to, from)
550 HARD_REG_SET *to;
551 regset from;
552{
770ae6cc
RK
553 unsigned int regno;
554
7609e720
BS
555 EXECUTE_IF_SET_IN_REG_SET
556 (from, FIRST_PSEUDO_REGISTER, regno,
557 {
558 int r = reg_renumber[regno];
559 int nregs;
770ae6cc 560
7609e720 561 if (r < 0)
404d95c4
R
562 {
563 /* reload_combine uses the information from
e881bb1b
RH
564 BASIC_BLOCK->global_live_at_start, which might still
565 contain registers that have not actually been allocated
566 since they have an equivalence. */
404d95c4
R
567 if (! reload_completed)
568 abort ();
569 }
570 else
571 {
572 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
573 while (nregs-- > 0)
574 SET_HARD_REG_BIT (*to, r + nregs);
575 }
7609e720
BS
576 });
577}
f474c6f8
AO
578
579/* Replace all pseudos found in LOC with their corresponding
580 equivalences. */
581
582static void
583replace_pseudos_in_call_usage (loc, mem_mode, usage)
584 rtx *loc;
585 enum machine_mode mem_mode;
586 rtx usage;
587{
588 rtx x = *loc;
589 enum rtx_code code;
590 const char *fmt;
591 int i, j;
592
593 if (! x)
594 return;
174fa2c4 595
f474c6f8
AO
596 code = GET_CODE (x);
597 if (code == REG)
598 {
ae0ed63a 599 unsigned int regno = REGNO (x);
086fef9e
AO
600
601 if (regno < FIRST_PSEUDO_REGISTER)
f474c6f8
AO
602 return;
603
604 x = eliminate_regs (x, mem_mode, usage);
605 if (x != *loc)
606 {
607 *loc = x;
608 replace_pseudos_in_call_usage (loc, mem_mode, usage);
609 return;
610 }
611
086fef9e
AO
612 if (reg_equiv_constant[regno])
613 *loc = reg_equiv_constant[regno];
614 else if (reg_equiv_mem[regno])
615 *loc = reg_equiv_mem[regno];
616 else if (reg_equiv_address[regno])
617 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
618 else if (GET_CODE (regno_reg_rtx[regno]) != REG
619 || REGNO (regno_reg_rtx[regno]) != regno)
620 *loc = regno_reg_rtx[regno];
f474c6f8
AO
621 else
622 abort ();
623
624 return;
625 }
626 else if (code == MEM)
627 {
628 replace_pseudos_in_call_usage (& XEXP (x, 0), GET_MODE (x), usage);
629 return;
630 }
174fa2c4 631
f474c6f8
AO
632 /* Process each of our operands recursively. */
633 fmt = GET_RTX_FORMAT (code);
634 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
635 if (*fmt == 'e')
636 replace_pseudos_in_call_usage (&XEXP (x, i), mem_mode, usage);
637 else if (*fmt == 'E')
638 for (j = 0; j < XVECLEN (x, i); j++)
639 replace_pseudos_in_call_usage (& XVECEXP (x, i, j), mem_mode, usage);
640}
641
03acd8f8 642\f
1e5bd841
BS
643/* Global variables used by reload and its subroutines. */
644
1e5bd841
BS
645/* Set during calculate_needs if an insn needs register elimination. */
646static int something_needs_elimination;
cb2afeb3
R
647/* Set during calculate_needs if an insn needs an operand changed. */
648int something_needs_operands_changed;
1e5bd841 649
1e5bd841
BS
650/* Nonzero means we couldn't get enough spill regs. */
651static int failure;
652
546b63fb 653/* Main entry point for the reload pass.
32131a9c
RK
654
655 FIRST is the first insn of the function being compiled.
656
657 GLOBAL nonzero means we were called from global_alloc
658 and should attempt to reallocate any pseudoregs that we
659 displace from hard regs we will use for reloads.
660 If GLOBAL is zero, we do not have enough information to do that,
661 so any pseudo reg that is spilled must go to the stack.
662
5352b11a
RS
663 Return value is nonzero if reload failed
664 and we must not do any more for this function. */
665
666int
e04ca094 667reload (first, global)
32131a9c
RK
668 rtx first;
669 int global;
32131a9c 670{
b3694847
SS
671 int i;
672 rtx insn;
673 struct elim_table *ep;
e0082a72 674 basic_block bb;
32131a9c 675
a68d4b75
BK
676 /* The two pointers used to track the true location of the memory used
677 for label offsets. */
9714cf43 678 char *real_known_ptr = NULL;
a68d4b75
BK
679 int (*real_at_ptr)[NUM_ELIMINABLE_REGS];
680
32131a9c
RK
681 /* Make sure even insns with volatile mem refs are recognizable. */
682 init_recog ();
683
1e5bd841
BS
684 failure = 0;
685
cad6f7d0
BS
686 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
687
437a710d
BS
688 /* Make sure that the last insn in the chain
689 is not something that needs reloading. */
6496a589 690 emit_note (NULL, NOTE_INSN_DELETED);
437a710d 691
32131a9c
RK
692 /* Enable find_equiv_reg to distinguish insns made by reload. */
693 reload_first_uid = get_max_uid ();
694
0dadecf6
RK
695#ifdef SECONDARY_MEMORY_NEEDED
696 /* Initialize the secondary memory table. */
697 clear_secondary_mem ();
698#endif
699
32131a9c 700 /* We don't have a stack slot for any spill reg yet. */
961192e1
JM
701 memset ((char *) spill_stack_slot, 0, sizeof spill_stack_slot);
702 memset ((char *) spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
32131a9c 703
a8efe40d
RK
704 /* Initialize the save area information for caller-save, in case some
705 are needed. */
706 init_save_areas ();
a8fdc208 707
32131a9c
RK
708 /* Compute which hard registers are now in use
709 as homes for pseudo registers.
710 This is done here rather than (eg) in global_alloc
711 because this point is reached even if not optimizing. */
32131a9c
RK
712 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
713 mark_home_live (i);
714
8dddd002
RK
715 /* A function that receives a nonlocal goto must save all call-saved
716 registers. */
717 if (current_function_has_nonlocal_label)
718 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2a3e384f
RH
719 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
720 regs_ever_live[i] = 1;
8dddd002 721
32131a9c
RK
722 /* Find all the pseudo registers that didn't get hard regs
723 but do have known equivalent constants or memory slots.
724 These include parameters (known equivalent to parameter slots)
725 and cse'd or loop-moved constant memory addresses.
726
727 Record constant equivalents in reg_equiv_constant
728 so they will be substituted by find_reloads.
729 Record memory equivalents in reg_mem_equiv so they can
730 be substituted eventually by altering the REG-rtx's. */
731
ad85216e 732 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
ad85216e
KG
733 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
734 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
735 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
f9e158c3 736 reg_max_ref_width = (unsigned int *) xcalloc (max_regno, sizeof (int));
ad85216e 737 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
4e135bdd 738 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
03acd8f8
BS
739 pseudo_forbidden_regs
740 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
741 pseudo_previous_regs
ad85216e 742 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
32131a9c 743
03acd8f8 744 CLEAR_HARD_REG_SET (bad_spill_regs_global);
56f58d3a 745
32131a9c 746 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
56f58d3a
RK
747 Also find all paradoxical subregs and find largest such for each pseudo.
748 On machines with small register classes, record hard registers that
05d10675 749 are used for user variables. These can never be used for spills.
570a98eb 750 Also look for a "constant" REG_SETJMP. This means that all
b453cb0b 751 caller-saved registers must be marked live. */
32131a9c 752
2b49ee39 753 num_eliminable_invariants = 0;
32131a9c
RK
754 for (insn = first; insn; insn = NEXT_INSN (insn))
755 {
756 rtx set = single_set (insn);
757
3d17d93d
AO
758 /* We may introduce USEs that we want to remove at the end, so
759 we'll mark them with QImode. Make sure there are no
760 previously-marked insns left by say regmove. */
761 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
762 && GET_MODE (insn) != VOIDmode)
763 PUT_MODE (insn, VOIDmode);
764
19652adf
ZW
765 if (GET_CODE (insn) == CALL_INSN
766 && find_reg_note (insn, REG_SETJMP, NULL))
b453cb0b
RK
767 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
768 if (! call_used_regs[i])
769 regs_ever_live[i] = 1;
770
32131a9c
RK
771 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
772 {
fb3821f7 773 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
a8efe40d
RK
774 if (note
775#ifdef LEGITIMATE_PIC_OPERAND_P
2b49ee39
R
776 && (! function_invariant_p (XEXP (note, 0))
777 || ! flag_pic
129c0899
HPN
778 /* A function invariant is often CONSTANT_P but may
779 include a register. We promise to only pass
780 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
781 || (CONSTANT_P (XEXP (note, 0))
782 && LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0))))
a8efe40d
RK
783#endif
784 )
32131a9c
RK
785 {
786 rtx x = XEXP (note, 0);
787 i = REGNO (SET_DEST (set));
788 if (i > LAST_VIRTUAL_REGISTER)
789 {
6a45951f
UW
790 /* It can happen that a REG_EQUIV note contains a MEM
791 that is not a legitimate memory operand. As later
792 stages of reload assume that all addresses found
793 in the reg_equiv_* arrays were originally legitimate,
794 we ignore such REG_EQUIV notes. */
795 if (memory_operand (x, VOIDmode))
956d6950 796 {
cf728d61
HPN
797 /* Always unshare the equivalence, so we can
798 substitute into this insn without touching the
2ba84f36 799 equivalence. */
cf728d61 800 reg_equiv_memory_loc[i] = copy_rtx (x);
956d6950 801 }
2b49ee39 802 else if (function_invariant_p (x))
32131a9c 803 {
2b49ee39
R
804 if (GET_CODE (x) == PLUS)
805 {
806 /* This is PLUS of frame pointer and a constant,
807 and might be shared. Unshare it. */
808 reg_equiv_constant[i] = copy_rtx (x);
809 num_eliminable_invariants++;
810 }
811 else if (x == frame_pointer_rtx
812 || x == arg_pointer_rtx)
813 {
814 reg_equiv_constant[i] = x;
815 num_eliminable_invariants++;
816 }
817 else if (LEGITIMATE_CONSTANT_P (x))
32131a9c
RK
818 reg_equiv_constant[i] = x;
819 else
820 reg_equiv_memory_loc[i]
d445b551 821 = force_const_mem (GET_MODE (SET_DEST (set)), x);
32131a9c
RK
822 }
823 else
824 continue;
825
826 /* If this register is being made equivalent to a MEM
827 and the MEM is not SET_SRC, the equivalencing insn
828 is one with the MEM as a SET_DEST and it occurs later.
829 So don't mark this insn now. */
830 if (GET_CODE (x) != MEM
831 || rtx_equal_p (SET_SRC (set), x))
135eb61c
R
832 reg_equiv_init[i]
833 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
32131a9c
RK
834 }
835 }
836 }
837
838 /* If this insn is setting a MEM from a register equivalent to it,
839 this is the equivalencing insn. */
840 else if (set && GET_CODE (SET_DEST (set)) == MEM
841 && GET_CODE (SET_SRC (set)) == REG
842 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
843 && rtx_equal_p (SET_DEST (set),
844 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
135eb61c
R
845 reg_equiv_init[REGNO (SET_SRC (set))]
846 = gen_rtx_INSN_LIST (VOIDmode, insn,
847 reg_equiv_init[REGNO (SET_SRC (set))]);
32131a9c 848
2c3c49de 849 if (INSN_P (insn))
32131a9c
RK
850 scan_paradoxical_subregs (PATTERN (insn));
851 }
852
09dd1133 853 init_elim_table ();
32131a9c
RK
854
855 num_labels = max_label_num () - get_first_label_num ();
856
857 /* Allocate the tables used to store offset information at labels. */
a68d4b75
BK
858 /* We used to use alloca here, but the size of what it would try to
859 allocate would occasionally cause it to exceed the stack limit and
860 cause a core dump. */
861 real_known_ptr = xmalloc (num_labels);
862 real_at_ptr
32131a9c 863 = (int (*)[NUM_ELIMINABLE_REGS])
a68d4b75 864 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
32131a9c 865
a68d4b75
BK
866 offsets_known_at = real_known_ptr - get_first_label_num ();
867 offsets_at
868 = (int (*)[NUM_ELIMINABLE_REGS]) (real_at_ptr - get_first_label_num ());
32131a9c
RK
869
870 /* Alter each pseudo-reg rtx to contain its hard reg number.
871 Assign stack slots to the pseudos that lack hard regs or equivalents.
872 Do not touch virtual registers. */
873
874 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
875 alter_reg (i, -1);
876
32131a9c
RK
877 /* If we have some registers we think can be eliminated, scan all insns to
878 see if there is an insn that sets one of these registers to something
879 other than itself plus a constant. If so, the register cannot be
880 eliminated. Doing this scan here eliminates an extra pass through the
881 main reload loop in the most common case where register elimination
882 cannot be done. */
883 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
884 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
885 || GET_CODE (insn) == CALL_INSN)
84832317 886 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
32131a9c 887
18a90182
BS
888 maybe_fix_stack_asms ();
889
03acd8f8
BS
890 insns_need_reload = 0;
891 something_needs_elimination = 0;
05d10675 892
4079cd63
JW
893 /* Initialize to -1, which means take the first spill register. */
894 last_spill_reg = -1;
895
32131a9c 896 /* Spill any hard regs that we know we can't eliminate. */
03acd8f8 897 CLEAR_HARD_REG_SET (used_spill_regs);
32131a9c
RK
898 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
899 if (! ep->can_eliminate)
e04ca094 900 spill_hard_reg (ep->from, 1);
9ff3516a
RK
901
902#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
903 if (frame_pointer_needed)
e04ca094 904 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
9ff3516a 905#endif
e04ca094 906 finish_spills (global);
7609e720 907
f1db3576
JL
908 /* From now on, we may need to generate moves differently. We may also
909 allow modifications of insns which cause them to not be recognized.
910 Any such modifications will be cleaned up during reload itself. */
b2f15f94
RK
911 reload_in_progress = 1;
912
32131a9c
RK
913 /* This loop scans the entire function each go-round
914 and repeats until one repetition spills no additional hard regs. */
03acd8f8 915 for (;;)
32131a9c 916 {
03acd8f8
BS
917 int something_changed;
918 int did_spill;
32131a9c 919
03acd8f8 920 HOST_WIDE_INT starting_frame_size;
32131a9c 921
665792eb 922 /* Round size of stack frame to stack_alignment_needed. This must be done
7657bf2f
JW
923 here because the stack size may be a part of the offset computation
924 for register elimination, and there might have been new stack slots
6d2f8887 925 created in the last iteration of this loop. */
665792eb
JH
926 if (cfun->stack_alignment_needed)
927 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
7657bf2f
JW
928
929 starting_frame_size = get_frame_size ();
930
09dd1133 931 set_initial_elim_offsets ();
1f3b1e1a 932 set_initial_label_offsets ();
03acd8f8 933
32131a9c
RK
934 /* For each pseudo register that has an equivalent location defined,
935 try to eliminate any eliminable registers (such as the frame pointer)
936 assuming initial offsets for the replacement register, which
937 is the normal case.
938
939 If the resulting location is directly addressable, substitute
940 the MEM we just got directly for the old REG.
941
942 If it is not addressable but is a constant or the sum of a hard reg
943 and constant, it is probably not addressable because the constant is
944 out of range, in that case record the address; we will generate
945 hairy code to compute the address in a register each time it is
6491dbbb
RK
946 needed. Similarly if it is a hard register, but one that is not
947 valid as an address register.
32131a9c
RK
948
949 If the location is not addressable, but does not have one of the
950 above forms, assign a stack slot. We have to do this to avoid the
951 potential of producing lots of reloads if, e.g., a location involves
952 a pseudo that didn't get a hard register and has an equivalent memory
953 location that also involves a pseudo that didn't get a hard register.
954
955 Perhaps at some point we will improve reload_when_needed handling
956 so this problem goes away. But that's very hairy. */
957
958 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
959 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
960 {
1914f5da 961 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
32131a9c
RK
962
963 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
964 XEXP (x, 0)))
965 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
966 else if (CONSTANT_P (XEXP (x, 0))
6491dbbb
RK
967 || (GET_CODE (XEXP (x, 0)) == REG
968 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
32131a9c
RK
969 || (GET_CODE (XEXP (x, 0)) == PLUS
970 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
971 && (REGNO (XEXP (XEXP (x, 0), 0))
972 < FIRST_PSEUDO_REGISTER)
973 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
974 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
975 else
976 {
977 /* Make a new stack slot. Then indicate that something
a8fdc208 978 changed so we go back and recompute offsets for
32131a9c
RK
979 eliminable registers because the allocation of memory
980 below might change some offset. reg_equiv_{mem,address}
981 will be set up for this pseudo on the next pass around
982 the loop. */
983 reg_equiv_memory_loc[i] = 0;
984 reg_equiv_init[i] = 0;
985 alter_reg (i, -1);
32131a9c
RK
986 }
987 }
a8fdc208 988
437a710d
BS
989 if (caller_save_needed)
990 setup_save_areas ();
991
03acd8f8 992 /* If we allocated another stack slot, redo elimination bookkeeping. */
437a710d 993 if (starting_frame_size != get_frame_size ())
32131a9c
RK
994 continue;
995
437a710d 996 if (caller_save_needed)
a8efe40d 997 {
437a710d
BS
998 save_call_clobbered_regs ();
999 /* That might have allocated new insn_chain structures. */
1000 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
a8efe40d
RK
1001 }
1002
03acd8f8
BS
1003 calculate_needs_all_insns (global);
1004
f5d8c9f4 1005 CLEAR_REG_SET (&spilled_pseudos);
03acd8f8
BS
1006 did_spill = 0;
1007
1008 something_changed = 0;
32131a9c 1009
0dadecf6
RK
1010 /* If we allocated any new memory locations, make another pass
1011 since it might have changed elimination offsets. */
1012 if (starting_frame_size != get_frame_size ())
1013 something_changed = 1;
1014
09dd1133
BS
1015 {
1016 HARD_REG_SET to_spill;
1017 CLEAR_HARD_REG_SET (to_spill);
1018 update_eliminables (&to_spill);
1019 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1020 if (TEST_HARD_REG_BIT (to_spill, i))
32131a9c 1021 {
e04ca094 1022 spill_hard_reg (i, 1);
03acd8f8 1023 did_spill = 1;
8f5db3c1
JL
1024
1025 /* Regardless of the state of spills, if we previously had
e591c83d 1026 a register that we thought we could eliminate, but now can
8f5db3c1
JL
1027 not eliminate, we must run another pass.
1028
1029 Consider pseudos which have an entry in reg_equiv_* which
1030 reference an eliminable register. We must make another pass
1031 to update reg_equiv_* so that we do not substitute in the
1032 old value from when we thought the elimination could be
1033 performed. */
1034 something_changed = 1;
32131a9c 1035 }
09dd1133 1036 }
9ff3516a 1037
e04ca094 1038 select_reload_regs ();
e483bf9c
BS
1039 if (failure)
1040 goto failed;
437a710d 1041
e483bf9c 1042 if (insns_need_reload != 0 || did_spill)
e04ca094 1043 something_changed |= finish_spills (global);
7609e720 1044
03acd8f8
BS
1045 if (! something_changed)
1046 break;
1047
1048 if (caller_save_needed)
7609e720 1049 delete_caller_save_insns ();
f5d8c9f4
BS
1050
1051 obstack_free (&reload_obstack, reload_firstobj);
32131a9c
RK
1052 }
1053
1054 /* If global-alloc was run, notify it of any register eliminations we have
1055 done. */
1056 if (global)
1057 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1058 if (ep->can_eliminate)
1059 mark_elimination (ep->from, ep->to);
1060
32131a9c
RK
1061 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1062 If that insn didn't set the register (i.e., it copied the register to
1063 memory), just delete that insn instead of the equivalencing insn plus
1064 anything now dead. If we call delete_dead_insn on that insn, we may
135eb61c 1065 delete the insn that actually sets the register if the register dies
32131a9c
RK
1066 there and that is incorrect. */
1067
1068 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
135eb61c
R
1069 {
1070 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1071 {
1072 rtx list;
1073 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1074 {
1075 rtx equiv_insn = XEXP (list, 0);
78571511
RK
1076
1077 /* If we already deleted the insn or if it may trap, we can't
1078 delete it. The latter case shouldn't happen, but can
1079 if an insn has a variable address, gets a REG_EH_REGION
1080 note added to it, and then gets converted into an load
1081 from a constant address. */
1082 if (GET_CODE (equiv_insn) == NOTE
1083 || can_throw_internal (equiv_insn))
1084 ;
1085 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
135eb61c
R
1086 delete_dead_insn (equiv_insn);
1087 else
1088 {
1089 PUT_CODE (equiv_insn, NOTE);
1090 NOTE_SOURCE_FILE (equiv_insn) = 0;
1091 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1092 }
1093 }
1094 }
1095 }
32131a9c
RK
1096
1097 /* Use the reload registers where necessary
1098 by generating move instructions to move the must-be-register
1099 values into or out of the reload registers. */
1100
03acd8f8
BS
1101 if (insns_need_reload != 0 || something_needs_elimination
1102 || something_needs_operands_changed)
c47f5ea5 1103 {
102870fb 1104 HOST_WIDE_INT old_frame_size = get_frame_size ();
c47f5ea5 1105
e04ca094 1106 reload_as_needed (global);
c47f5ea5
BS
1107
1108 if (old_frame_size != get_frame_size ())
1109 abort ();
1110
1111 if (num_eliminable)
1112 verify_initial_elim_offsets ();
1113 }
32131a9c 1114
2a1f8b6b 1115 /* If we were able to eliminate the frame pointer, show that it is no
546b63fb 1116 longer live at the start of any basic block. If it ls live by
2a1f8b6b
RK
1117 virtue of being in a pseudo, that pseudo will be marked live
1118 and hence the frame pointer will be known to be live via that
1119 pseudo. */
1120
1121 if (! frame_pointer_needed)
e0082a72
ZD
1122 FOR_EACH_BB (bb)
1123 CLEAR_REGNO_REG_SET (bb->global_live_at_start,
8e08106d 1124 HARD_FRAME_POINTER_REGNUM);
2a1f8b6b 1125
5352b11a
RS
1126 /* Come here (with failure set nonzero) if we can't get enough spill regs
1127 and we decide not to abort about it. */
1128 failed:
1129
f5d8c9f4 1130 CLEAR_REG_SET (&spilled_pseudos);
a3ec87a8
RS
1131 reload_in_progress = 0;
1132
32131a9c
RK
1133 /* Now eliminate all pseudo regs by modifying them into
1134 their equivalent memory references.
1135 The REG-rtx's for the pseudos are modified in place,
1136 so all insns that used to refer to them now refer to memory.
1137
1138 For a reg that has a reg_equiv_address, all those insns
1139 were changed by reloading so that no insns refer to it any longer;
1140 but the DECL_RTL of a variable decl may refer to it,
1141 and if so this causes the debugging info to mention the variable. */
1142
1143 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1144 {
1145 rtx addr = 0;
9ec36da5
JL
1146
1147 if (reg_equiv_mem[i])
1148 addr = XEXP (reg_equiv_mem[i], 0);
1149
32131a9c
RK
1150 if (reg_equiv_address[i])
1151 addr = reg_equiv_address[i];
9ec36da5 1152
32131a9c
RK
1153 if (addr)
1154 {
1155 if (reg_renumber[i] < 0)
1156 {
1157 rtx reg = regno_reg_rtx[i];
173b24b9 1158
5a63e069 1159 REG_USERVAR_P (reg) = 0;
ef178af3 1160 PUT_CODE (reg, MEM);
32131a9c 1161 XEXP (reg, 0) = addr;
173b24b9
RK
1162 if (reg_equiv_memory_loc[i])
1163 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1164 else
1165 {
1166 RTX_UNCHANGING_P (reg) = MEM_IN_STRUCT_P (reg)
1167 = MEM_SCALAR_P (reg) = 0;
1168 MEM_ATTRS (reg) = 0;
1169 }
32131a9c
RK
1170 }
1171 else if (reg_equiv_mem[i])
1172 XEXP (reg_equiv_mem[i], 0) = addr;
1173 }
1174 }
1175
2ae74651
JL
1176 /* We must set reload_completed now since the cleanup_subreg_operands call
1177 below will re-recognize each insn and reload may have generated insns
1178 which are only valid during and after reload. */
1179 reload_completed = 1;
1180
bd695e1e
RH
1181 /* Make a pass over all the insns and delete all USEs which we inserted
1182 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
41e34bab
DJ
1183 notes. Delete all CLOBBER insns, except those that refer to the return
1184 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1185 from misarranging variable-array code, and simplify (subreg (reg))
260f91c2
DJ
1186 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1187 are no longer useful or accurate. Strip and regenerate REG_INC notes
1188 that may have been moved around. */
32131a9c
RK
1189
1190 for (insn = first; insn; insn = NEXT_INSN (insn))
2c3c49de 1191 if (INSN_P (insn))
32131a9c 1192 {
6764d250 1193 rtx *pnote;
32131a9c 1194
f474c6f8
AO
1195 if (GET_CODE (insn) == CALL_INSN)
1196 replace_pseudos_in_call_usage (& CALL_INSN_FUNCTION_USAGE (insn),
1197 VOIDmode,
1198 CALL_INSN_FUNCTION_USAGE (insn));
1199
0304f787 1200 if ((GET_CODE (PATTERN (insn)) == USE
3d17d93d
AO
1201 /* We mark with QImode USEs introduced by reload itself. */
1202 && (GET_MODE (insn) == QImode
1203 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
bd695e1e 1204 || (GET_CODE (PATTERN (insn)) == CLOBBER
260f91c2 1205 && (GET_CODE (XEXP (PATTERN (insn), 0)) != MEM
41e34bab
DJ
1206 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1207 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1208 && XEXP (XEXP (PATTERN (insn), 0), 0)
1209 != stack_pointer_rtx))
bd695e1e
RH
1210 && (GET_CODE (XEXP (PATTERN (insn), 0)) != REG
1211 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
b60a8416 1212 {
e5eac8ef 1213 delete_insn (insn);
b60a8416
R
1214 continue;
1215 }
6764d250
BS
1216
1217 pnote = &REG_NOTES (insn);
1218 while (*pnote != 0)
32131a9c 1219 {
6764d250 1220 if (REG_NOTE_KIND (*pnote) == REG_DEAD
80599fd9 1221 || REG_NOTE_KIND (*pnote) == REG_UNUSED
2dfa9a87 1222 || REG_NOTE_KIND (*pnote) == REG_INC
80599fd9
NC
1223 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1224 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
6764d250
BS
1225 *pnote = XEXP (*pnote, 1);
1226 else
1227 pnote = &XEXP (*pnote, 1);
32131a9c 1228 }
0304f787 1229
2dfa9a87
MH
1230#ifdef AUTO_INC_DEC
1231 add_auto_inc_notes (insn, PATTERN (insn));
1232#endif
1233
0304f787
JL
1234 /* And simplify (subreg (reg)) if it appears as an operand. */
1235 cleanup_subreg_operands (insn);
b60a8416 1236 }
32131a9c 1237
ab87f8c8
JL
1238 /* If we are doing stack checking, give a warning if this function's
1239 frame size is larger than we expect. */
1240 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1241 {
1242 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
05d10675
BS
1243 static int verbose_warned = 0;
1244
ab87f8c8
JL
1245 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1246 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1247 size += UNITS_PER_WORD;
1248
1249 if (size > STACK_CHECK_MAX_FRAME_SIZE)
05d10675 1250 {
ab87f8c8
JL
1251 warning ("frame size too large for reliable stack checking");
1252 if (! verbose_warned)
1253 {
1254 warning ("try reducing the number of local variables");
1255 verbose_warned = 1;
1256 }
1257 }
1258 }
1259
32131a9c 1260 /* Indicate that we no longer have known memory locations or constants. */
58d9f9d9
JL
1261 if (reg_equiv_constant)
1262 free (reg_equiv_constant);
32131a9c 1263 reg_equiv_constant = 0;
58d9f9d9
JL
1264 if (reg_equiv_memory_loc)
1265 free (reg_equiv_memory_loc);
32131a9c 1266 reg_equiv_memory_loc = 0;
5352b11a 1267
a68d4b75
BK
1268 if (real_known_ptr)
1269 free (real_known_ptr);
1270 if (real_at_ptr)
1271 free (real_at_ptr);
1272
56a65848
DB
1273 free (reg_equiv_mem);
1274 free (reg_equiv_init);
1275 free (reg_equiv_address);
1276 free (reg_max_ref_width);
03acd8f8
BS
1277 free (reg_old_renumber);
1278 free (pseudo_previous_regs);
1279 free (pseudo_forbidden_regs);
56a65848 1280
8b4f9969
JW
1281 CLEAR_HARD_REG_SET (used_spill_regs);
1282 for (i = 0; i < n_spills; i++)
1283 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1284
7609e720
BS
1285 /* Free all the insn_chain structures at once. */
1286 obstack_free (&reload_obstack, reload_startobj);
1287 unused_insn_chains = 0;
f1330226 1288 fixup_abnormal_edges ();
7609e720 1289
e16e3291
UW
1290 /* Replacing pseudos with their memory equivalents might have
1291 created shared rtx. Subsequent passes would get confused
1292 by this, so unshare everything here. */
1293 unshare_all_rtl_again (first);
1294
5352b11a 1295 return failure;
32131a9c 1296}
1e5bd841 1297
18a90182
BS
1298/* Yet another special case. Unfortunately, reg-stack forces people to
1299 write incorrect clobbers in asm statements. These clobbers must not
1300 cause the register to appear in bad_spill_regs, otherwise we'll call
1301 fatal_insn later. We clear the corresponding regnos in the live
1302 register sets to avoid this.
1303 The whole thing is rather sick, I'm afraid. */
efc9bd41 1304
18a90182
BS
1305static void
1306maybe_fix_stack_asms ()
1307{
1308#ifdef STACK_REGS
392dccb7 1309 const char *constraints[MAX_RECOG_OPERANDS];
18a90182
BS
1310 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1311 struct insn_chain *chain;
1312
1313 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1314 {
1315 int i, noperands;
1316 HARD_REG_SET clobbered, allowed;
1317 rtx pat;
1318
2c3c49de 1319 if (! INSN_P (chain->insn)
18a90182
BS
1320 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1321 continue;
1322 pat = PATTERN (chain->insn);
1323 if (GET_CODE (pat) != PARALLEL)
1324 continue;
1325
1326 CLEAR_HARD_REG_SET (clobbered);
1327 CLEAR_HARD_REG_SET (allowed);
1328
1329 /* First, make a mask of all stack regs that are clobbered. */
1330 for (i = 0; i < XVECLEN (pat, 0); i++)
1331 {
1332 rtx t = XVECEXP (pat, 0, i);
1333 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1334 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1335 }
1336
1337 /* Get the operand values and constraints out of the insn. */
1ccbefce 1338 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
18a90182
BS
1339 constraints, operand_mode);
1340
1341 /* For every operand, see what registers are allowed. */
1342 for (i = 0; i < noperands; i++)
1343 {
6b9c6f4f 1344 const char *p = constraints[i];
18a90182
BS
1345 /* For every alternative, we compute the class of registers allowed
1346 for reloading in CLS, and merge its contents into the reg set
1347 ALLOWED. */
1348 int cls = (int) NO_REGS;
1349
1350 for (;;)
1351 {
1352 char c = *p++;
1353
1354 if (c == '\0' || c == ',' || c == '#')
1355 {
1356 /* End of one alternative - mark the regs in the current
1357 class, and reset the class. */
1358 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1359 cls = NO_REGS;
1360 if (c == '#')
1361 do {
1362 c = *p++;
1363 } while (c != '\0' && c != ',');
1364 if (c == '\0')
1365 break;
1366 continue;
1367 }
1368
1369 switch (c)
1370 {
1371 case '=': case '+': case '*': case '%': case '?': case '!':
1372 case '0': case '1': case '2': case '3': case '4': case 'm':
1373 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1374 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1375 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1376 case 'P':
18a90182
BS
1377 break;
1378
1379 case 'p':
3dcc68a4
NC
1380 cls = (int) reg_class_subunion[cls]
1381 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
18a90182
BS
1382 break;
1383
1384 case 'g':
1385 case 'r':
1386 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1387 break;
1388
1389 default:
ccfc6cc8
UW
1390 if (EXTRA_ADDRESS_CONSTRAINT (c))
1391 cls = (int) reg_class_subunion[cls]
1392 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1393 else
1394 cls = (int) reg_class_subunion[cls]
1395 [(int) REG_CLASS_FROM_LETTER (c)];
18a90182
BS
1396 }
1397 }
1398 }
1399 /* Those of the registers which are clobbered, but allowed by the
1400 constraints, must be usable as reload registers. So clear them
1401 out of the life information. */
1402 AND_HARD_REG_SET (allowed, clobbered);
1403 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1404 if (TEST_HARD_REG_BIT (allowed, i))
1405 {
239a0f5b
BS
1406 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1407 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
18a90182
BS
1408 }
1409 }
1410
1411#endif
1412}
03acd8f8 1413\f
f5d8c9f4
BS
1414/* Copy the global variables n_reloads and rld into the corresponding elts
1415 of CHAIN. */
1416static void
1417copy_reloads (chain)
1418 struct insn_chain *chain;
1419{
1420 chain->n_reloads = n_reloads;
1421 chain->rld
1422 = (struct reload *) obstack_alloc (&reload_obstack,
1423 n_reloads * sizeof (struct reload));
1424 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1425 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1426}
1427
03acd8f8
BS
1428/* Walk the chain of insns, and determine for each whether it needs reloads
1429 and/or eliminations. Build the corresponding insns_need_reload list, and
1430 set something_needs_elimination as appropriate. */
1431static void
7609e720 1432calculate_needs_all_insns (global)
1e5bd841
BS
1433 int global;
1434{
7609e720 1435 struct insn_chain **pprev_reload = &insns_need_reload;
462561b7 1436 struct insn_chain *chain, *next = 0;
1e5bd841 1437
03acd8f8
BS
1438 something_needs_elimination = 0;
1439
f5d8c9f4 1440 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
462561b7 1441 for (chain = reload_insn_chain; chain != 0; chain = next)
1e5bd841 1442 {
67e61fe7 1443 rtx insn = chain->insn;
03acd8f8 1444
462561b7
JJ
1445 next = chain->next;
1446
f5d8c9f4
BS
1447 /* Clear out the shortcuts. */
1448 chain->n_reloads = 0;
67e61fe7
BS
1449 chain->need_elim = 0;
1450 chain->need_reload = 0;
1451 chain->need_operand_change = 0;
1e5bd841 1452
03acd8f8
BS
1453 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1454 include REG_LABEL), we need to see what effects this has on the
1455 known offsets at labels. */
1e5bd841
BS
1456
1457 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
2c3c49de 1458 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1e5bd841
BS
1459 set_label_offsets (insn, insn, 0);
1460
2c3c49de 1461 if (INSN_P (insn))
1e5bd841
BS
1462 {
1463 rtx old_body = PATTERN (insn);
1464 int old_code = INSN_CODE (insn);
1465 rtx old_notes = REG_NOTES (insn);
1466 int did_elimination = 0;
cb2afeb3 1467 int operands_changed = 0;
2b49ee39
R
1468 rtx set = single_set (insn);
1469
1470 /* Skip insns that only set an equivalence. */
1471 if (set && GET_CODE (SET_DEST (set)) == REG
1472 && reg_renumber[REGNO (SET_DEST (set))] < 0
1473 && reg_equiv_constant[REGNO (SET_DEST (set))])
67e61fe7 1474 continue;
1e5bd841 1475
1e5bd841 1476 /* If needed, eliminate any eliminable registers. */
2b49ee39 1477 if (num_eliminable || num_eliminable_invariants)
1e5bd841
BS
1478 did_elimination = eliminate_regs_in_insn (insn, 0);
1479
1480 /* Analyze the instruction. */
cb2afeb3
R
1481 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1482 global, spill_reg_order);
1483
1484 /* If a no-op set needs more than one reload, this is likely
1485 to be something that needs input address reloads. We
1486 can't get rid of this cleanly later, and it is of no use
1487 anyway, so discard it now.
1488 We only do this when expensive_optimizations is enabled,
1489 since this complements reload inheritance / output
1490 reload deletion, and it can make debugging harder. */
1491 if (flag_expensive_optimizations && n_reloads > 1)
1492 {
1493 rtx set = single_set (insn);
1494 if (set
1495 && SET_SRC (set) == SET_DEST (set)
1496 && GET_CODE (SET_SRC (set)) == REG
1497 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1498 {
ca6c03ca 1499 delete_insn (insn);
3eae4643 1500 /* Delete it from the reload chain. */
462561b7
JJ
1501 if (chain->prev)
1502 chain->prev->next = next;
1503 else
1504 reload_insn_chain = next;
1505 if (next)
1506 next->prev = chain->prev;
1507 chain->next = unused_insn_chains;
1508 unused_insn_chains = chain;
cb2afeb3
R
1509 continue;
1510 }
1511 }
1512 if (num_eliminable)
1513 update_eliminable_offsets ();
1e5bd841
BS
1514
1515 /* Remember for later shortcuts which insns had any reloads or
7609e720
BS
1516 register eliminations. */
1517 chain->need_elim = did_elimination;
03acd8f8
BS
1518 chain->need_reload = n_reloads > 0;
1519 chain->need_operand_change = operands_changed;
1e5bd841
BS
1520
1521 /* Discard any register replacements done. */
1522 if (did_elimination)
1523 {
f5d8c9f4 1524 obstack_free (&reload_obstack, reload_insn_firstobj);
1e5bd841
BS
1525 PATTERN (insn) = old_body;
1526 INSN_CODE (insn) = old_code;
1527 REG_NOTES (insn) = old_notes;
1528 something_needs_elimination = 1;
1529 }
1530
cb2afeb3
R
1531 something_needs_operands_changed |= operands_changed;
1532
437a710d 1533 if (n_reloads != 0)
7609e720 1534 {
f5d8c9f4 1535 copy_reloads (chain);
7609e720
BS
1536 *pprev_reload = chain;
1537 pprev_reload = &chain->next_need_reload;
7609e720 1538 }
1e5bd841 1539 }
1e5bd841 1540 }
7609e720 1541 *pprev_reload = 0;
1e5bd841 1542}
f5d8c9f4
BS
1543\f
1544/* Comparison function for qsort to decide which of two reloads
1545 should be handled first. *P1 and *P2 are the reload numbers. */
1e5bd841 1546
f5d8c9f4
BS
1547static int
1548reload_reg_class_lower (r1p, r2p)
1549 const PTR r1p;
1550 const PTR r2p;
1e5bd841 1551{
b3694847
SS
1552 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1553 int t;
1e5bd841 1554
f5d8c9f4
BS
1555 /* Consider required reloads before optional ones. */
1556 t = rld[r1].optional - rld[r2].optional;
1557 if (t != 0)
1558 return t;
1e5bd841 1559
f5d8c9f4
BS
1560 /* Count all solitary classes before non-solitary ones. */
1561 t = ((reg_class_size[(int) rld[r2].class] == 1)
1562 - (reg_class_size[(int) rld[r1].class] == 1));
1563 if (t != 0)
1564 return t;
1e5bd841 1565
f5d8c9f4
BS
1566 /* Aside from solitaires, consider all multi-reg groups first. */
1567 t = rld[r2].nregs - rld[r1].nregs;
1568 if (t != 0)
1569 return t;
1e5bd841 1570
f5d8c9f4
BS
1571 /* Consider reloads in order of increasing reg-class number. */
1572 t = (int) rld[r1].class - (int) rld[r2].class;
1573 if (t != 0)
1574 return t;
1e5bd841 1575
f5d8c9f4
BS
1576 /* If reloads are equally urgent, sort by reload number,
1577 so that the results of qsort leave nothing to chance. */
1578 return r1 - r2;
1579}
1580\f
1581/* The cost of spilling each hard reg. */
1582static int spill_cost[FIRST_PSEUDO_REGISTER];
1e5bd841 1583
f5d8c9f4
BS
1584/* When spilling multiple hard registers, we use SPILL_COST for the first
1585 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1586 only the first hard reg for a multi-reg pseudo. */
1587static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1e5bd841 1588
f5d8c9f4 1589/* Update the spill cost arrays, considering that pseudo REG is live. */
770ae6cc 1590
f5d8c9f4
BS
1591static void
1592count_pseudo (reg)
1593 int reg;
1594{
b2aec5c0 1595 int freq = REG_FREQ (reg);
f5d8c9f4
BS
1596 int r = reg_renumber[reg];
1597 int nregs;
1e5bd841 1598
f5d8c9f4
BS
1599 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1600 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1601 return;
1e5bd841 1602
f5d8c9f4 1603 SET_REGNO_REG_SET (&pseudos_counted, reg);
1e5bd841 1604
f5d8c9f4
BS
1605 if (r < 0)
1606 abort ();
1d7254c5 1607
b2aec5c0 1608 spill_add_cost[r] += freq;
1e5bd841 1609
f5d8c9f4
BS
1610 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1611 while (nregs-- > 0)
b2aec5c0 1612 spill_cost[r + nregs] += freq;
f5d8c9f4 1613}
1e5bd841 1614
f5d8c9f4
BS
1615/* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1616 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
efc9bd41 1617
f5d8c9f4
BS
1618static void
1619order_regs_for_reload (chain)
1620 struct insn_chain *chain;
1621{
fbd40359 1622 int i;
efc9bd41
RK
1623 HARD_REG_SET used_by_pseudos;
1624 HARD_REG_SET used_by_pseudos2;
1e5bd841 1625
efc9bd41 1626 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1e5bd841 1627
f5d8c9f4
BS
1628 memset (spill_cost, 0, sizeof spill_cost);
1629 memset (spill_add_cost, 0, sizeof spill_add_cost);
1e5bd841 1630
f5d8c9f4 1631 /* Count number of uses of each hard reg by pseudo regs allocated to it
efc9bd41
RK
1632 and then order them by decreasing use. First exclude hard registers
1633 that are live in or across this insn. */
1634
1635 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1636 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1637 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1638 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1e5bd841 1639
f5d8c9f4
BS
1640 /* Now find out which pseudos are allocated to it, and update
1641 hard_reg_n_uses. */
1642 CLEAR_REG_SET (&pseudos_counted);
1e5bd841 1643
f5d8c9f4 1644 EXECUTE_IF_SET_IN_REG_SET
fbd40359 1645 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
f5d8c9f4 1646 {
fbd40359 1647 count_pseudo (i);
f5d8c9f4
BS
1648 });
1649 EXECUTE_IF_SET_IN_REG_SET
fbd40359 1650 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
f5d8c9f4 1651 {
fbd40359 1652 count_pseudo (i);
f5d8c9f4
BS
1653 });
1654 CLEAR_REG_SET (&pseudos_counted);
1e5bd841 1655}
03acd8f8 1656\f
f5d8c9f4
BS
1657/* Vector of reload-numbers showing the order in which the reloads should
1658 be processed. */
1659static short reload_order[MAX_RELOADS];
1e5bd841 1660
f5d8c9f4
BS
1661/* This is used to keep track of the spill regs used in one insn. */
1662static HARD_REG_SET used_spill_regs_local;
03acd8f8 1663
f5d8c9f4
BS
1664/* We decided to spill hard register SPILLED, which has a size of
1665 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1666 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1667 update SPILL_COST/SPILL_ADD_COST. */
770ae6cc 1668
03acd8f8 1669static void
f5d8c9f4
BS
1670count_spilled_pseudo (spilled, spilled_nregs, reg)
1671 int spilled, spilled_nregs, reg;
1e5bd841 1672{
f5d8c9f4
BS
1673 int r = reg_renumber[reg];
1674 int nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1e5bd841 1675
f5d8c9f4
BS
1676 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1677 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1678 return;
1e5bd841 1679
f5d8c9f4 1680 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1e5bd841 1681
b2aec5c0 1682 spill_add_cost[r] -= REG_FREQ (reg);
f5d8c9f4 1683 while (nregs-- > 0)
b2aec5c0 1684 spill_cost[r + nregs] -= REG_FREQ (reg);
1e5bd841
BS
1685}
1686
f5d8c9f4 1687/* Find reload register to use for reload number ORDER. */
03acd8f8 1688
f5d8c9f4 1689static int
e04ca094 1690find_reg (chain, order)
03acd8f8 1691 struct insn_chain *chain;
f5d8c9f4 1692 int order;
1e5bd841 1693{
f5d8c9f4
BS
1694 int rnum = reload_order[order];
1695 struct reload *rl = rld + rnum;
1696 int best_cost = INT_MAX;
1697 int best_reg = -1;
770ae6cc
RK
1698 unsigned int i, j;
1699 int k;
f5d8c9f4
BS
1700 HARD_REG_SET not_usable;
1701 HARD_REG_SET used_by_other_reload;
1e5bd841 1702
f5d8c9f4
BS
1703 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1704 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1705 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1706
1707 CLEAR_HARD_REG_SET (used_by_other_reload);
770ae6cc 1708 for (k = 0; k < order; k++)
1e5bd841 1709 {
770ae6cc
RK
1710 int other = reload_order[k];
1711
f5d8c9f4
BS
1712 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1713 for (j = 0; j < rld[other].nregs; j++)
1714 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1715 }
1e5bd841 1716
f5d8c9f4
BS
1717 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1718 {
770ae6cc
RK
1719 unsigned int regno = i;
1720
f5d8c9f4
BS
1721 if (! TEST_HARD_REG_BIT (not_usable, regno)
1722 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1723 && HARD_REGNO_MODE_OK (regno, rl->mode))
1e5bd841 1724 {
f5d8c9f4
BS
1725 int this_cost = spill_cost[regno];
1726 int ok = 1;
770ae6cc 1727 unsigned int this_nregs = HARD_REGNO_NREGS (regno, rl->mode);
1e5bd841 1728
f5d8c9f4
BS
1729 for (j = 1; j < this_nregs; j++)
1730 {
1731 this_cost += spill_add_cost[regno + j];
1732 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1733 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1734 ok = 0;
1735 }
1736 if (! ok)
1737 continue;
1738 if (rl->in && GET_CODE (rl->in) == REG && REGNO (rl->in) == regno)
1739 this_cost--;
1740 if (rl->out && GET_CODE (rl->out) == REG && REGNO (rl->out) == regno)
1741 this_cost--;
1742 if (this_cost < best_cost
1743 /* Among registers with equal cost, prefer caller-saved ones, or
1744 use REG_ALLOC_ORDER if it is defined. */
1745 || (this_cost == best_cost
1746#ifdef REG_ALLOC_ORDER
1747 && (inv_reg_alloc_order[regno]
1748 < inv_reg_alloc_order[best_reg])
1749#else
1750 && call_used_regs[regno]
1751 && ! call_used_regs[best_reg]
1752#endif
1753 ))
1754 {
1755 best_reg = regno;
1756 best_cost = this_cost;
1e5bd841
BS
1757 }
1758 }
1759 }
f5d8c9f4
BS
1760 if (best_reg == -1)
1761 return 0;
770ae6cc 1762
e04ca094
JL
1763 if (rtl_dump_file)
1764 fprintf (rtl_dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
770ae6cc 1765
f5d8c9f4
BS
1766 rl->nregs = HARD_REGNO_NREGS (best_reg, rl->mode);
1767 rl->regno = best_reg;
1e5bd841 1768
f5d8c9f4 1769 EXECUTE_IF_SET_IN_REG_SET
239a0f5b 1770 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j,
f5d8c9f4
BS
1771 {
1772 count_spilled_pseudo (best_reg, rl->nregs, j);
1773 });
770ae6cc 1774
f5d8c9f4 1775 EXECUTE_IF_SET_IN_REG_SET
239a0f5b 1776 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j,
f5d8c9f4
BS
1777 {
1778 count_spilled_pseudo (best_reg, rl->nregs, j);
1779 });
03acd8f8 1780
f5d8c9f4
BS
1781 for (i = 0; i < rl->nregs; i++)
1782 {
1783 if (spill_cost[best_reg + i] != 0
1784 || spill_add_cost[best_reg + i] != 0)
1785 abort ();
1786 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1787 }
1788 return 1;
03acd8f8
BS
1789}
1790
1791/* Find more reload regs to satisfy the remaining need of an insn, which
1792 is given by CHAIN.
1e5bd841
BS
1793 Do it by ascending class number, since otherwise a reg
1794 might be spilled for a big class and might fail to count
f5d8c9f4 1795 for a smaller class even though it belongs to that class. */
1e5bd841 1796
03acd8f8 1797static void
e04ca094 1798find_reload_regs (chain)
03acd8f8 1799 struct insn_chain *chain;
1e5bd841 1800{
f5d8c9f4 1801 int i;
1e5bd841 1802
f5d8c9f4
BS
1803 /* In order to be certain of getting the registers we need,
1804 we must sort the reloads into order of increasing register class.
1805 Then our grabbing of reload registers will parallel the process
1806 that provided the reload registers. */
1807 for (i = 0; i < chain->n_reloads; i++)
1e5bd841 1808 {
f5d8c9f4
BS
1809 /* Show whether this reload already has a hard reg. */
1810 if (chain->rld[i].reg_rtx)
1e5bd841 1811 {
f5d8c9f4
BS
1812 int regno = REGNO (chain->rld[i].reg_rtx);
1813 chain->rld[i].regno = regno;
770ae6cc
RK
1814 chain->rld[i].nregs
1815 = HARD_REGNO_NREGS (regno, GET_MODE (chain->rld[i].reg_rtx));
1e5bd841 1816 }
f5d8c9f4
BS
1817 else
1818 chain->rld[i].regno = -1;
1819 reload_order[i] = i;
1820 }
1e5bd841 1821
f5d8c9f4
BS
1822 n_reloads = chain->n_reloads;
1823 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1e5bd841 1824
f5d8c9f4 1825 CLEAR_HARD_REG_SET (used_spill_regs_local);
03acd8f8 1826
e04ca094
JL
1827 if (rtl_dump_file)
1828 fprintf (rtl_dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1e5bd841 1829
f5d8c9f4 1830 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1e5bd841 1831
f5d8c9f4 1832 /* Compute the order of preference for hard registers to spill. */
1e5bd841 1833
f5d8c9f4 1834 order_regs_for_reload (chain);
1e5bd841 1835
f5d8c9f4
BS
1836 for (i = 0; i < n_reloads; i++)
1837 {
1838 int r = reload_order[i];
1e5bd841 1839
f5d8c9f4
BS
1840 /* Ignore reloads that got marked inoperative. */
1841 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1842 && ! rld[r].optional
1843 && rld[r].regno == -1)
e04ca094 1844 if (! find_reg (chain, i))
f5d8c9f4 1845 {
ecf3151a 1846 spill_failure (chain->insn, rld[r].class);
f5d8c9f4 1847 failure = 1;
03acd8f8 1848 return;
f5d8c9f4 1849 }
1e5bd841 1850 }
05d10675 1851
f5d8c9f4
BS
1852 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1853 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
03acd8f8 1854
f5d8c9f4 1855 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1e5bd841
BS
1856}
1857
f5d8c9f4 1858static void
e04ca094 1859select_reload_regs ()
09dd1133 1860{
f5d8c9f4 1861 struct insn_chain *chain;
09dd1133 1862
f5d8c9f4
BS
1863 /* Try to satisfy the needs for each insn. */
1864 for (chain = insns_need_reload; chain != 0;
1865 chain = chain->next_need_reload)
e04ca094 1866 find_reload_regs (chain);
09dd1133 1867}
32131a9c 1868\f
437a710d
BS
1869/* Delete all insns that were inserted by emit_caller_save_insns during
1870 this iteration. */
1871static void
7609e720 1872delete_caller_save_insns ()
437a710d 1873{
7609e720 1874 struct insn_chain *c = reload_insn_chain;
437a710d 1875
7609e720 1876 while (c != 0)
437a710d 1877 {
7609e720 1878 while (c != 0 && c->is_caller_save_insn)
437a710d 1879 {
7609e720
BS
1880 struct insn_chain *next = c->next;
1881 rtx insn = c->insn;
1882
7609e720
BS
1883 if (c == reload_insn_chain)
1884 reload_insn_chain = next;
ca6c03ca 1885 delete_insn (insn);
7609e720
BS
1886
1887 if (next)
1888 next->prev = c->prev;
1889 if (c->prev)
1890 c->prev->next = next;
1891 c->next = unused_insn_chains;
1892 unused_insn_chains = c;
1893 c = next;
437a710d 1894 }
7609e720
BS
1895 if (c != 0)
1896 c = c->next;
437a710d
BS
1897 }
1898}
1899\f
5352b11a
RS
1900/* Handle the failure to find a register to spill.
1901 INSN should be one of the insns which needed this particular spill reg. */
1902
1903static void
ecf3151a 1904spill_failure (insn, class)
5352b11a 1905 rtx insn;
ecf3151a 1906 enum reg_class class;
5352b11a 1907{
ecf3151a 1908 static const char *const reg_class_names[] = REG_CLASS_NAMES;
5352b11a 1909 if (asm_noperands (PATTERN (insn)) >= 0)
1f978f5f 1910 error_for_asm (insn, "can't find a register in class `%s' while reloading `asm'",
ecf3151a 1911 reg_class_names[class]);
5352b11a 1912 else
ecf3151a 1913 {
1f978f5f 1914 error ("unable to find a register to spill in class `%s'",
ecf3151a 1915 reg_class_names[class]);
1f978f5f 1916 fatal_insn ("this is the insn:", insn);
ecf3151a 1917 }
5352b11a 1918}
32131a9c
RK
1919\f
1920/* Delete an unneeded INSN and any previous insns who sole purpose is loading
1921 data that is dead in INSN. */
1922
1923static void
1924delete_dead_insn (insn)
1925 rtx insn;
1926{
1927 rtx prev = prev_real_insn (insn);
1928 rtx prev_dest;
1929
1930 /* If the previous insn sets a register that dies in our insn, delete it
1931 too. */
1932 if (prev && GET_CODE (PATTERN (prev)) == SET
1933 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
1934 && reg_mentioned_p (prev_dest, PATTERN (insn))
b294ca38
R
1935 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1936 && ! side_effects_p (SET_SRC (PATTERN (prev))))
32131a9c
RK
1937 delete_dead_insn (prev);
1938
1939 PUT_CODE (insn, NOTE);
1940 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1941 NOTE_SOURCE_FILE (insn) = 0;
1942}
1943
1944/* Modify the home of pseudo-reg I.
1945 The new home is present in reg_renumber[I].
1946
1947 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1948 or it may be -1, meaning there is none or it is not relevant.
1949 This is used so that all pseudos spilled from a given hard reg
1950 can share one stack slot. */
1951
1952static void
1953alter_reg (i, from_reg)
b3694847 1954 int i;
32131a9c
RK
1955 int from_reg;
1956{
1957 /* When outputting an inline function, this can happen
1958 for a reg that isn't actually used. */
1959 if (regno_reg_rtx[i] == 0)
1960 return;
1961
1962 /* If the reg got changed to a MEM at rtl-generation time,
1963 ignore it. */
1964 if (GET_CODE (regno_reg_rtx[i]) != REG)
1965 return;
1966
1967 /* Modify the reg-rtx to contain the new hard reg
1968 number or else to contain its pseudo reg number. */
1969 REGNO (regno_reg_rtx[i])
1970 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1971
1972 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1973 allocate a stack slot for it. */
1974
1975 if (reg_renumber[i] < 0
b1f21e0a 1976 && REG_N_REFS (i) > 0
32131a9c
RK
1977 && reg_equiv_constant[i] == 0
1978 && reg_equiv_memory_loc[i] == 0)
1979 {
b3694847 1980 rtx x;
770ae6cc
RK
1981 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1982 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
32131a9c
RK
1983 int adjust = 0;
1984
1985 /* Each pseudo reg has an inherent size which comes from its own mode,
1986 and a total size which provides room for paradoxical subregs
1987 which refer to the pseudo reg in wider modes.
1988
1989 We can use a slot already allocated if it provides both
1990 enough inherent space and enough total space.
1991 Otherwise, we allocate a new slot, making sure that it has no less
1992 inherent space, and no less total space, then the previous slot. */
1993 if (from_reg == -1)
1994 {
1995 /* No known place to spill from => no slot to reuse. */
cabcf079
ILT
1996 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1997 inherent_size == total_size ? 0 : -1);
f76b9db2 1998 if (BYTES_BIG_ENDIAN)
02db8dd0
RK
1999 /* Cancel the big-endian correction done in assign_stack_local.
2000 Get the address of the beginning of the slot.
2001 This is so we can do a big-endian correction unconditionally
2002 below. */
2003 adjust = inherent_size - total_size;
2004
2005 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
3bdf5ad1
RK
2006
2007 /* Nothing can alias this slot except this pseudo. */
ba4828e0 2008 set_mem_alias_set (x, new_alias_set ());
32131a9c 2009 }
3bdf5ad1 2010
32131a9c
RK
2011 /* Reuse a stack slot if possible. */
2012 else if (spill_stack_slot[from_reg] != 0
2013 && spill_stack_slot_width[from_reg] >= total_size
2014 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2015 >= inherent_size))
2016 x = spill_stack_slot[from_reg];
3bdf5ad1 2017
32131a9c
RK
2018 /* Allocate a bigger slot. */
2019 else
2020 {
2021 /* Compute maximum size needed, both for inherent size
2022 and for total size. */
2023 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
4f2d3674 2024 rtx stack_slot;
3bdf5ad1 2025
32131a9c
RK
2026 if (spill_stack_slot[from_reg])
2027 {
2028 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2029 > inherent_size)
2030 mode = GET_MODE (spill_stack_slot[from_reg]);
2031 if (spill_stack_slot_width[from_reg] > total_size)
2032 total_size = spill_stack_slot_width[from_reg];
2033 }
3bdf5ad1 2034
32131a9c 2035 /* Make a slot with that size. */
cabcf079
ILT
2036 x = assign_stack_local (mode, total_size,
2037 inherent_size == total_size ? 0 : -1);
4f2d3674 2038 stack_slot = x;
3bdf5ad1
RK
2039
2040 /* All pseudos mapped to this slot can alias each other. */
2041 if (spill_stack_slot[from_reg])
ba4828e0 2042 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
3bdf5ad1 2043 else
ba4828e0 2044 set_mem_alias_set (x, new_alias_set ());
3bdf5ad1 2045
f76b9db2
ILT
2046 if (BYTES_BIG_ENDIAN)
2047 {
2048 /* Cancel the big-endian correction done in assign_stack_local.
2049 Get the address of the beginning of the slot.
2050 This is so we can do a big-endian correction unconditionally
2051 below. */
2052 adjust = GET_MODE_SIZE (mode) - total_size;
4f2d3674 2053 if (adjust)
8ac61af7
RK
2054 stack_slot
2055 = adjust_address_nv (x, mode_for_size (total_size
38a448ca
RH
2056 * BITS_PER_UNIT,
2057 MODE_INT, 1),
8ac61af7 2058 adjust);
f76b9db2 2059 }
3bdf5ad1 2060
4f2d3674 2061 spill_stack_slot[from_reg] = stack_slot;
32131a9c
RK
2062 spill_stack_slot_width[from_reg] = total_size;
2063 }
2064
32131a9c
RK
2065 /* On a big endian machine, the "address" of the slot
2066 is the address of the low part that fits its inherent mode. */
f76b9db2 2067 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
32131a9c 2068 adjust += (total_size - inherent_size);
32131a9c
RK
2069
2070 /* If we have any adjustment to make, or if the stack slot is the
2071 wrong mode, make a new stack slot. */
1285011e
RK
2072 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2073
2074 /* If we have a decl for the original register, set it for the
2075 memory. If this is a shared MEM, make a copy. */
2076 if (REGNO_DECL (i))
2077 {
a20fd5ac 2078 rtx decl = DECL_RTL_IF_SET (REGNO_DECL (i));
1285011e 2079
a20fd5ac
JJ
2080 /* We can do this only for the DECLs home pseudo, not for
2081 any copies of it, since otherwise when the stack slot
2082 is reused, nonoverlapping_memrefs_p might think they
2083 cannot overlap. */
2084 if (decl && GET_CODE (decl) == REG && REGNO (decl) == (unsigned) i)
2085 {
2086 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2087 x = copy_rtx (x);
2088
2089 set_mem_expr (x, REGNO_DECL (i));
2090 }
1285011e 2091 }
32131a9c 2092
6d2f8887 2093 /* Save the stack slot for later. */
32131a9c
RK
2094 reg_equiv_memory_loc[i] = x;
2095 }
2096}
2097
2098/* Mark the slots in regs_ever_live for the hard regs
2099 used by pseudo-reg number REGNO. */
2100
2101void
2102mark_home_live (regno)
2103 int regno;
2104{
b3694847 2105 int i, lim;
770ae6cc 2106
32131a9c
RK
2107 i = reg_renumber[regno];
2108 if (i < 0)
2109 return;
2110 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2111 while (i < lim)
2112 regs_ever_live[i++] = 1;
2113}
2114\f
2115/* This function handles the tracking of elimination offsets around branches.
2116
2117 X is a piece of RTL being scanned.
2118
2119 INSN is the insn that it came from, if any.
2120
40f03658 2121 INITIAL_P is nonzero if we are to set the offset to be the initial
32131a9c
RK
2122 offset and zero if we are setting the offset of the label to be the
2123 current offset. */
2124
2125static void
2126set_label_offsets (x, insn, initial_p)
2127 rtx x;
2128 rtx insn;
2129 int initial_p;
2130{
2131 enum rtx_code code = GET_CODE (x);
2132 rtx tem;
e51712db 2133 unsigned int i;
32131a9c
RK
2134 struct elim_table *p;
2135
2136 switch (code)
2137 {
2138 case LABEL_REF:
8be386d9
RS
2139 if (LABEL_REF_NONLOCAL_P (x))
2140 return;
2141
32131a9c
RK
2142 x = XEXP (x, 0);
2143
0f41302f 2144 /* ... fall through ... */
32131a9c
RK
2145
2146 case CODE_LABEL:
2147 /* If we know nothing about this label, set the desired offsets. Note
2148 that this sets the offset at a label to be the offset before a label
2149 if we don't know anything about the label. This is not correct for
2150 the label after a BARRIER, but is the best guess we can make. If
2151 we guessed wrong, we will suppress an elimination that might have
2152 been possible had we been able to guess correctly. */
2153
2154 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2155 {
2156 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2157 offsets_at[CODE_LABEL_NUMBER (x)][i]
2158 = (initial_p ? reg_eliminate[i].initial_offset
2159 : reg_eliminate[i].offset);
2160 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2161 }
2162
2163 /* Otherwise, if this is the definition of a label and it is
d45cf215 2164 preceded by a BARRIER, set our offsets to the known offset of
32131a9c
RK
2165 that label. */
2166
2167 else if (x == insn
2168 && (tem = prev_nonnote_insn (insn)) != 0
2169 && GET_CODE (tem) == BARRIER)
1f3b1e1a 2170 set_offsets_for_label (insn);
32131a9c
RK
2171 else
2172 /* If neither of the above cases is true, compare each offset
2173 with those previously recorded and suppress any eliminations
2174 where the offsets disagree. */
a8fdc208 2175
32131a9c
RK
2176 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2177 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2178 != (initial_p ? reg_eliminate[i].initial_offset
2179 : reg_eliminate[i].offset))
2180 reg_eliminate[i].can_eliminate = 0;
2181
2182 return;
2183
2184 case JUMP_INSN:
2185 set_label_offsets (PATTERN (insn), insn, initial_p);
2186
0f41302f 2187 /* ... fall through ... */
32131a9c
RK
2188
2189 case INSN:
2190 case CALL_INSN:
2191 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2192 and hence must have all eliminations at their initial offsets. */
2193 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2194 if (REG_NOTE_KIND (tem) == REG_LABEL)
2195 set_label_offsets (XEXP (tem, 0), insn, 1);
2196 return;
2197
0c0ba09c 2198 case PARALLEL:
32131a9c
RK
2199 case ADDR_VEC:
2200 case ADDR_DIFF_VEC:
0c0ba09c
JJ
2201 /* Each of the labels in the parallel or address vector must be
2202 at their initial offsets. We want the first field for PARALLEL
2203 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
32131a9c 2204
e51712db 2205 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
32131a9c
RK
2206 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2207 insn, initial_p);
2208 return;
2209
2210 case SET:
2211 /* We only care about setting PC. If the source is not RETURN,
2212 IF_THEN_ELSE, or a label, disable any eliminations not at
2213 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2214 isn't one of those possibilities. For branches to a label,
2215 call ourselves recursively.
2216
2217 Note that this can disable elimination unnecessarily when we have
2218 a non-local goto since it will look like a non-constant jump to
2219 someplace in the current function. This isn't a significant
2220 problem since such jumps will normally be when all elimination
2221 pairs are back to their initial offsets. */
2222
2223 if (SET_DEST (x) != pc_rtx)
2224 return;
2225
2226 switch (GET_CODE (SET_SRC (x)))
2227 {
2228 case PC:
2229 case RETURN:
2230 return;
2231
2232 case LABEL_REF:
2233 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2234 return;
2235
2236 case IF_THEN_ELSE:
2237 tem = XEXP (SET_SRC (x), 1);
2238 if (GET_CODE (tem) == LABEL_REF)
2239 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2240 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2241 break;
2242
2243 tem = XEXP (SET_SRC (x), 2);
2244 if (GET_CODE (tem) == LABEL_REF)
2245 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2246 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2247 break;
2248 return;
e9a25f70
JL
2249
2250 default:
2251 break;
32131a9c
RK
2252 }
2253
2254 /* If we reach here, all eliminations must be at their initial
2255 offset because we are doing a jump to a variable address. */
2256 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2257 if (p->offset != p->initial_offset)
2258 p->can_eliminate = 0;
e9a25f70 2259 break;
05d10675 2260
e9a25f70
JL
2261 default:
2262 break;
32131a9c
RK
2263 }
2264}
2265\f
a8fdc208 2266/* Scan X and replace any eliminable registers (such as fp) with a
32131a9c
RK
2267 replacement (such as sp), plus an offset.
2268
2269 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2270 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2271 MEM, we are allowed to replace a sum of a register and the constant zero
2272 with the register, which we cannot do outside a MEM. In addition, we need
2273 to record the fact that a register is referenced outside a MEM.
2274
ff32812a 2275 If INSN is an insn, it is the insn containing X. If we replace a REG
40f03658 2276 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
32131a9c 2277 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
38e01259 2278 the REG is being modified.
32131a9c 2279
ff32812a
RS
2280 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2281 That's used when we eliminate in expressions stored in notes.
2282 This means, do not set ref_outside_mem even if the reference
2283 is outside of MEMs.
2284
32131a9c
RK
2285 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2286 replacements done assuming all offsets are at their initial values. If
2287 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2288 encounter, return the actual location so that find_reloads will do
2289 the proper thing. */
2290
2291rtx
1914f5da 2292eliminate_regs (x, mem_mode, insn)
32131a9c
RK
2293 rtx x;
2294 enum machine_mode mem_mode;
2295 rtx insn;
2296{
2297 enum rtx_code code = GET_CODE (x);
2298 struct elim_table *ep;
2299 int regno;
2300 rtx new;
2301 int i, j;
6f7d635c 2302 const char *fmt;
32131a9c
RK
2303 int copied = 0;
2304
d6633f01
NS
2305 if (! current_function_decl)
2306 return x;
9969bb2c 2307
32131a9c
RK
2308 switch (code)
2309 {
2310 case CONST_INT:
2311 case CONST_DOUBLE:
69ef87e2 2312 case CONST_VECTOR:
32131a9c
RK
2313 case CONST:
2314 case SYMBOL_REF:
2315 case CODE_LABEL:
2316 case PC:
2317 case CC0:
2318 case ASM_INPUT:
2319 case ADDR_VEC:
2320 case ADDR_DIFF_VEC:
2321 case RETURN:
2322 return x;
2323
e9a25f70
JL
2324 case ADDRESSOF:
2325 /* This is only for the benefit of the debugging backends, which call
2326 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2327 removed after CSE. */
1914f5da 2328 new = eliminate_regs (XEXP (x, 0), 0, insn);
e9a25f70
JL
2329 if (GET_CODE (new) == MEM)
2330 return XEXP (new, 0);
2331 return x;
2332
32131a9c
RK
2333 case REG:
2334 regno = REGNO (x);
2335
2336 /* First handle the case where we encounter a bare register that
2337 is eliminable. Replace it with a PLUS. */
2338 if (regno < FIRST_PSEUDO_REGISTER)
2339 {
2340 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2341 ep++)
2342 if (ep->from_rtx == x && ep->can_eliminate)
dfac187e 2343 return plus_constant (ep->to_rtx, ep->previous_offset);
32131a9c
RK
2344
2345 }
cd7c9015
RK
2346 else if (reg_renumber && reg_renumber[regno] < 0
2347 && reg_equiv_constant && reg_equiv_constant[regno]
2b49ee39
R
2348 && ! CONSTANT_P (reg_equiv_constant[regno]))
2349 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2350 mem_mode, insn);
32131a9c
RK
2351 return x;
2352
c5c76735
JL
2353 /* You might think handling MINUS in a manner similar to PLUS is a
2354 good idea. It is not. It has been tried multiple times and every
2355 time the change has had to have been reverted.
2356
2357 Other parts of reload know a PLUS is special (gen_reload for example)
2358 and require special code to handle code a reloaded PLUS operand.
2359
2360 Also consider backends where the flags register is clobbered by a
2361 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2362 lea instruction comes to mind). If we try to reload a MINUS, we
2363 may kill the flags register that was holding a useful value.
2364
2365 So, please before trying to handle MINUS, consider reload as a
2366 whole instead of this little section as well as the backend issues. */
32131a9c
RK
2367 case PLUS:
2368 /* If this is the sum of an eliminable register and a constant, rework
6d2f8887 2369 the sum. */
32131a9c
RK
2370 if (GET_CODE (XEXP (x, 0)) == REG
2371 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2372 && CONSTANT_P (XEXP (x, 1)))
2373 {
2374 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2375 ep++)
2376 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2377 {
32131a9c
RK
2378 /* The only time we want to replace a PLUS with a REG (this
2379 occurs when the constant operand of the PLUS is the negative
2380 of the offset) is when we are inside a MEM. We won't want
2381 to do so at other times because that would change the
2382 structure of the insn in a way that reload can't handle.
2383 We special-case the commonest situation in
2384 eliminate_regs_in_insn, so just replace a PLUS with a
2385 PLUS here, unless inside a MEM. */
a23b64d5 2386 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
32131a9c
RK
2387 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2388 return ep->to_rtx;
2389 else
38a448ca
RH
2390 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2391 plus_constant (XEXP (x, 1),
2392 ep->previous_offset));
32131a9c
RK
2393 }
2394
2395 /* If the register is not eliminable, we are done since the other
2396 operand is a constant. */
2397 return x;
2398 }
2399
2400 /* If this is part of an address, we want to bring any constant to the
2401 outermost PLUS. We will do this by doing register replacement in
2402 our operands and seeing if a constant shows up in one of them.
2403
dfac187e
BS
2404 Note that there is no risk of modifying the structure of the insn,
2405 since we only get called for its operands, thus we are either
2406 modifying the address inside a MEM, or something like an address
2407 operand of a load-address insn. */
32131a9c
RK
2408
2409 {
1914f5da
RH
2410 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2411 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
32131a9c 2412
cd7c9015 2413 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
32131a9c
RK
2414 {
2415 /* If one side is a PLUS and the other side is a pseudo that
a8fdc208 2416 didn't get a hard register but has a reg_equiv_constant,
32131a9c
RK
2417 we must replace the constant here since it may no longer
2418 be in the position of any operand. */
2419 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2420 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2421 && reg_renumber[REGNO (new1)] < 0
2422 && reg_equiv_constant != 0
2423 && reg_equiv_constant[REGNO (new1)] != 0)
2424 new1 = reg_equiv_constant[REGNO (new1)];
2425 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2426 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2427 && reg_renumber[REGNO (new0)] < 0
2428 && reg_equiv_constant[REGNO (new0)] != 0)
2429 new0 = reg_equiv_constant[REGNO (new0)];
2430
2431 new = form_sum (new0, new1);
2432
2433 /* As above, if we are not inside a MEM we do not want to
2434 turn a PLUS into something else. We might try to do so here
2435 for an addition of 0 if we aren't optimizing. */
2436 if (! mem_mode && GET_CODE (new) != PLUS)
38a448ca 2437 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
32131a9c
RK
2438 else
2439 return new;
2440 }
2441 }
2442 return x;
2443
981c7390 2444 case MULT:
05d10675 2445 /* If this is the product of an eliminable register and a
981c7390
RK
2446 constant, apply the distribute law and move the constant out
2447 so that we have (plus (mult ..) ..). This is needed in order
9faa82d8 2448 to keep load-address insns valid. This case is pathological.
981c7390
RK
2449 We ignore the possibility of overflow here. */
2450 if (GET_CODE (XEXP (x, 0)) == REG
2451 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2452 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2453 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2454 ep++)
2455 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2456 {
2457 if (! mem_mode
2458 /* Refs inside notes don't count for this purpose. */
2459 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2460 || GET_CODE (insn) == INSN_LIST)))
2461 ep->ref_outside_mem = 1;
2462
2463 return
38a448ca 2464 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
981c7390
RK
2465 ep->previous_offset * INTVAL (XEXP (x, 1)));
2466 }
32131a9c 2467
0f41302f 2468 /* ... fall through ... */
32131a9c 2469
32131a9c
RK
2470 case CALL:
2471 case COMPARE:
c5c76735 2472 /* See comments before PLUS about handling MINUS. */
930aeef3 2473 case MINUS:
32131a9c
RK
2474 case DIV: case UDIV:
2475 case MOD: case UMOD:
2476 case AND: case IOR: case XOR:
45620ed4
RK
2477 case ROTATERT: case ROTATE:
2478 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
32131a9c
RK
2479 case NE: case EQ:
2480 case GE: case GT: case GEU: case GTU:
2481 case LE: case LT: case LEU: case LTU:
2482 {
1914f5da 2483 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
fb3821f7 2484 rtx new1
1914f5da 2485 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
32131a9c
RK
2486
2487 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
38a448ca 2488 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
32131a9c
RK
2489 }
2490 return x;
2491
981c7390
RK
2492 case EXPR_LIST:
2493 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2494 if (XEXP (x, 0))
2495 {
1914f5da 2496 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
981c7390 2497 if (new != XEXP (x, 0))
13bb79d4
R
2498 {
2499 /* If this is a REG_DEAD note, it is not valid anymore.
2500 Using the eliminated version could result in creating a
2501 REG_DEAD note for the stack or frame pointer. */
2502 if (GET_MODE (x) == REG_DEAD)
2503 return (XEXP (x, 1)
2504 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2505 : NULL_RTX);
2506
2507 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2508 }
981c7390
RK
2509 }
2510
0f41302f 2511 /* ... fall through ... */
981c7390
RK
2512
2513 case INSN_LIST:
2514 /* Now do eliminations in the rest of the chain. If this was
2515 an EXPR_LIST, this might result in allocating more memory than is
2516 strictly needed, but it simplifies the code. */
2517 if (XEXP (x, 1))
2518 {
1914f5da 2519 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
981c7390 2520 if (new != XEXP (x, 1))
cd7c9015
RK
2521 return
2522 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
981c7390
RK
2523 }
2524 return x;
2525
32131a9c
RK
2526 case PRE_INC:
2527 case POST_INC:
2528 case PRE_DEC:
2529 case POST_DEC:
32131a9c
RK
2530 case STRICT_LOW_PART:
2531 case NEG: case NOT:
2532 case SIGN_EXTEND: case ZERO_EXTEND:
2533 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2534 case FLOAT: case FIX:
2535 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2536 case ABS:
2537 case SQRT:
2538 case FFS:
1914f5da 2539 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
32131a9c 2540 if (new != XEXP (x, 0))
38a448ca 2541 return gen_rtx_fmt_e (code, GET_MODE (x), new);
32131a9c
RK
2542 return x;
2543
2544 case SUBREG:
ddef6bc7 2545 /* Similar to above processing, but preserve SUBREG_BYTE.
32131a9c
RK
2546 Convert (subreg (mem)) to (mem) if not paradoxical.
2547 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2548 pseudo didn't get a hard reg, we must replace this with the
2549 eliminated version of the memory location because push_reloads
2550 may do the replacement in certain circumstances. */
2551 if (GET_CODE (SUBREG_REG (x)) == REG
2552 && (GET_MODE_SIZE (GET_MODE (x))
2553 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2554 && reg_equiv_memory_loc != 0
2555 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2556 {
cb2afeb3 2557 new = SUBREG_REG (x);
32131a9c
RK
2558 }
2559 else
1914f5da 2560 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
32131a9c 2561
ddef6bc7 2562 if (new != SUBREG_REG (x))
32131a9c 2563 {
29ae5012
RK
2564 int x_size = GET_MODE_SIZE (GET_MODE (x));
2565 int new_size = GET_MODE_SIZE (GET_MODE (new));
2566
1914f5da 2567 if (GET_CODE (new) == MEM
6d49a073 2568 && ((x_size < new_size
1914f5da 2569#ifdef WORD_REGISTER_OPERATIONS
6d49a073
JW
2570 /* On these machines, combine can create rtl of the form
2571 (set (subreg:m1 (reg:m2 R) 0) ...)
05d10675 2572 where m1 < m2, and expects something interesting to
6d49a073
JW
2573 happen to the entire word. Moreover, it will use the
2574 (reg:m2 R) later, expecting all bits to be preserved.
05d10675 2575 So if the number of words is the same, preserve the
6d49a073 2576 subreg so that push_reloads can see it. */
5d9669fd
RK
2577 && ! ((x_size - 1) / UNITS_PER_WORD
2578 == (new_size -1 ) / UNITS_PER_WORD)
1914f5da 2579#endif
6d49a073 2580 )
5d9669fd 2581 || x_size == new_size)
1914f5da 2582 )
a2ff290c 2583 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
32131a9c 2584 else
ddef6bc7 2585 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
32131a9c
RK
2586 }
2587
2588 return x;
2589
32131a9c 2590 case MEM:
e9a25f70
JL
2591 /* This is only for the benefit of the debugging backends, which call
2592 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2593 removed after CSE. */
2594 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
1914f5da 2595 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
e9a25f70 2596
32131a9c
RK
2597 /* Our only special processing is to pass the mode of the MEM to our
2598 recursive call and copy the flags. While we are here, handle this
2599 case more efficiently. */
f1ec5147
RK
2600 return
2601 replace_equiv_address_nv (x,
2602 eliminate_regs (XEXP (x, 0),
2603 GET_MODE (x), insn));
05d10675 2604
dfac187e 2605 case USE:
055c7759
JDA
2606 /* Handle insn_list USE that a call to a pure function may generate. */
2607 new = eliminate_regs (XEXP (x, 0), 0, insn);
2608 if (new != XEXP (x, 0))
2609 return gen_rtx_USE (GET_MODE (x), new);
2610 return x;
2611
dfac187e
BS
2612 case CLOBBER:
2613 case ASM_OPERANDS:
2614 case SET:
2615 abort ();
2616
e9a25f70
JL
2617 default:
2618 break;
32131a9c
RK
2619 }
2620
2621 /* Process each of our operands recursively. If any have changed, make a
2622 copy of the rtx. */
2623 fmt = GET_RTX_FORMAT (code);
2624 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2625 {
2626 if (*fmt == 'e')
2627 {
1914f5da 2628 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
32131a9c
RK
2629 if (new != XEXP (x, i) && ! copied)
2630 {
2631 rtx new_x = rtx_alloc (code);
4e135bdd
KG
2632 memcpy (new_x, x,
2633 (sizeof (*new_x) - sizeof (new_x->fld)
2634 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
32131a9c
RK
2635 x = new_x;
2636 copied = 1;
2637 }
2638 XEXP (x, i) = new;
2639 }
2640 else if (*fmt == 'E')
2641 {
2642 int copied_vec = 0;
2643 for (j = 0; j < XVECLEN (x, i); j++)
2644 {
1914f5da 2645 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
32131a9c
RK
2646 if (new != XVECEXP (x, i, j) && ! copied_vec)
2647 {
8f985ec4
ZW
2648 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2649 XVEC (x, i)->elem);
32131a9c
RK
2650 if (! copied)
2651 {
2652 rtx new_x = rtx_alloc (code);
4e135bdd
KG
2653 memcpy (new_x, x,
2654 (sizeof (*new_x) - sizeof (new_x->fld)
2655 + (sizeof (new_x->fld[0])
2656 * GET_RTX_LENGTH (code))));
32131a9c
RK
2657 x = new_x;
2658 copied = 1;
2659 }
2660 XVEC (x, i) = new_v;
2661 copied_vec = 1;
2662 }
2663 XVECEXP (x, i, j) = new;
2664 }
2665 }
2666 }
2667
2668 return x;
2669}
dfac187e
BS
2670
2671/* Scan rtx X for modifications of elimination target registers. Update
2672 the table of eliminables to reflect the changed state. MEM_MODE is
2673 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2674
2675static void
2676elimination_effects (x, mem_mode)
2677 rtx x;
2678 enum machine_mode mem_mode;
2679
2680{
2681 enum rtx_code code = GET_CODE (x);
2682 struct elim_table *ep;
2683 int regno;
2684 int i, j;
2685 const char *fmt;
2686
2687 switch (code)
2688 {
2689 case CONST_INT:
2690 case CONST_DOUBLE:
69ef87e2 2691 case CONST_VECTOR:
dfac187e
BS
2692 case CONST:
2693 case SYMBOL_REF:
2694 case CODE_LABEL:
2695 case PC:
2696 case CC0:
2697 case ASM_INPUT:
2698 case ADDR_VEC:
2699 case ADDR_DIFF_VEC:
2700 case RETURN:
2701 return;
2702
2703 case ADDRESSOF:
2704 abort ();
2705
2706 case REG:
2707 regno = REGNO (x);
2708
2709 /* First handle the case where we encounter a bare register that
2710 is eliminable. Replace it with a PLUS. */
2711 if (regno < FIRST_PSEUDO_REGISTER)
2712 {
2713 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2714 ep++)
2715 if (ep->from_rtx == x && ep->can_eliminate)
2716 {
2717 if (! mem_mode)
2718 ep->ref_outside_mem = 1;
2719 return;
2720 }
2721
2722 }
2723 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2724 && reg_equiv_constant[regno]
92a21141 2725 && ! function_invariant_p (reg_equiv_constant[regno]))
dfac187e
BS
2726 elimination_effects (reg_equiv_constant[regno], mem_mode);
2727 return;
2728
2729 case PRE_INC:
2730 case POST_INC:
2731 case PRE_DEC:
2732 case POST_DEC:
4b983fdc
RH
2733 case POST_MODIFY:
2734 case PRE_MODIFY:
dfac187e
BS
2735 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2736 if (ep->to_rtx == XEXP (x, 0))
2737 {
2738 int size = GET_MODE_SIZE (mem_mode);
2739
2740 /* If more bytes than MEM_MODE are pushed, account for them. */
2741#ifdef PUSH_ROUNDING
2742 if (ep->to_rtx == stack_pointer_rtx)
2743 size = PUSH_ROUNDING (size);
2744#endif
2745 if (code == PRE_DEC || code == POST_DEC)
2746 ep->offset += size;
4b983fdc 2747 else if (code == PRE_INC || code == POST_INC)
dfac187e 2748 ep->offset -= size;
4b983fdc
RH
2749 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2750 && GET_CODE (XEXP (x, 1)) == PLUS
2751 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2752 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2753 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
dfac187e
BS
2754 }
2755
4b983fdc
RH
2756 /* These two aren't unary operators. */
2757 if (code == POST_MODIFY || code == PRE_MODIFY)
2758 break;
2759
dfac187e
BS
2760 /* Fall through to generic unary operation case. */
2761 case STRICT_LOW_PART:
2762 case NEG: case NOT:
2763 case SIGN_EXTEND: case ZERO_EXTEND:
2764 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2765 case FLOAT: case FIX:
2766 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2767 case ABS:
2768 case SQRT:
2769 case FFS:
2770 elimination_effects (XEXP (x, 0), mem_mode);
2771 return;
2772
2773 case SUBREG:
2774 if (GET_CODE (SUBREG_REG (x)) == REG
2775 && (GET_MODE_SIZE (GET_MODE (x))
2776 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2777 && reg_equiv_memory_loc != 0
2778 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2779 return;
2780
2781 elimination_effects (SUBREG_REG (x), mem_mode);
2782 return;
2783
2784 case USE:
2785 /* If using a register that is the source of an eliminate we still
2786 think can be performed, note it cannot be performed since we don't
2787 know how this register is used. */
2788 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2789 if (ep->from_rtx == XEXP (x, 0))
2790 ep->can_eliminate = 0;
2791
2792 elimination_effects (XEXP (x, 0), mem_mode);
2793 return;
2794
2795 case CLOBBER:
2796 /* If clobbering a register that is the replacement register for an
2797 elimination we still think can be performed, note that it cannot
2798 be performed. Otherwise, we need not be concerned about it. */
2799 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2800 if (ep->to_rtx == XEXP (x, 0))
2801 ep->can_eliminate = 0;
2802
2803 elimination_effects (XEXP (x, 0), mem_mode);
2804 return;
2805
2806 case SET:
2807 /* Check for setting a register that we know about. */
2808 if (GET_CODE (SET_DEST (x)) == REG)
2809 {
2810 /* See if this is setting the replacement register for an
2811 elimination.
2812
2813 If DEST is the hard frame pointer, we do nothing because we
2814 assume that all assignments to the frame pointer are for
2815 non-local gotos and are being done at a time when they are valid
2816 and do not disturb anything else. Some machines want to
2817 eliminate a fake argument pointer (or even a fake frame pointer)
2818 with either the real frame or the stack pointer. Assignments to
2819 the hard frame pointer must not prevent this elimination. */
2820
2821 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2822 ep++)
2823 if (ep->to_rtx == SET_DEST (x)
2824 && SET_DEST (x) != hard_frame_pointer_rtx)
2825 {
2826 /* If it is being incremented, adjust the offset. Otherwise,
2827 this elimination can't be done. */
2828 rtx src = SET_SRC (x);
2829
2830 if (GET_CODE (src) == PLUS
2831 && XEXP (src, 0) == SET_DEST (x)
2832 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2833 ep->offset -= INTVAL (XEXP (src, 1));
2834 else
2835 ep->can_eliminate = 0;
2836 }
2837 }
2838
2839 elimination_effects (SET_DEST (x), 0);
2840 elimination_effects (SET_SRC (x), 0);
2841 return;
2842
2843 case MEM:
2844 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2845 abort ();
2846
2847 /* Our only special processing is to pass the mode of the MEM to our
2848 recursive call. */
2849 elimination_effects (XEXP (x, 0), GET_MODE (x));
2850 return;
2851
2852 default:
2853 break;
2854 }
2855
2856 fmt = GET_RTX_FORMAT (code);
2857 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2858 {
2859 if (*fmt == 'e')
2860 elimination_effects (XEXP (x, i), mem_mode);
2861 else if (*fmt == 'E')
2862 for (j = 0; j < XVECLEN (x, i); j++)
2863 elimination_effects (XVECEXP (x, i, j), mem_mode);
2864 }
2865}
2866
2867/* Descend through rtx X and verify that no references to eliminable registers
2868 remain. If any do remain, mark the involved register as not
2869 eliminable. */
1d813780 2870
dfac187e
BS
2871static void
2872check_eliminable_occurrences (x)
2873 rtx x;
2874{
2875 const char *fmt;
2876 int i;
2877 enum rtx_code code;
2878
2879 if (x == 0)
2880 return;
1d7254c5 2881
dfac187e
BS
2882 code = GET_CODE (x);
2883
2884 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2885 {
2886 struct elim_table *ep;
2887
2888 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2889 if (ep->from_rtx == x && ep->can_eliminate)
2890 ep->can_eliminate = 0;
2891 return;
2892 }
1d7254c5 2893
dfac187e
BS
2894 fmt = GET_RTX_FORMAT (code);
2895 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2896 {
2897 if (*fmt == 'e')
2898 check_eliminable_occurrences (XEXP (x, i));
2899 else if (*fmt == 'E')
2900 {
2901 int j;
2902 for (j = 0; j < XVECLEN (x, i); j++)
2903 check_eliminable_occurrences (XVECEXP (x, i, j));
2904 }
2905 }
2906}
32131a9c
RK
2907\f
2908/* Scan INSN and eliminate all eliminable registers in it.
2909
2910 If REPLACE is nonzero, do the replacement destructively. Also
2911 delete the insn as dead it if it is setting an eliminable register.
2912
2913 If REPLACE is zero, do all our allocations in reload_obstack.
2914
2915 If no eliminations were done and this insn doesn't require any elimination
2916 processing (these are not identical conditions: it might be updating sp,
2917 but not referencing fp; this needs to be seen during reload_as_needed so
2918 that the offset between fp and sp can be taken into consideration), zero
2919 is returned. Otherwise, 1 is returned. */
2920
2921static int
2922eliminate_regs_in_insn (insn, replace)
2923 rtx insn;
2924 int replace;
2925{
dfac187e 2926 int icode = recog_memoized (insn);
32131a9c 2927 rtx old_body = PATTERN (insn);
dfac187e 2928 int insn_is_asm = asm_noperands (old_body) >= 0;
774672d2 2929 rtx old_set = single_set (insn);
32131a9c
RK
2930 rtx new_body;
2931 int val = 0;
dfac187e
BS
2932 int i, any_changes;
2933 rtx substed_operand[MAX_RECOG_OPERANDS];
2934 rtx orig_operand[MAX_RECOG_OPERANDS];
32131a9c
RK
2935 struct elim_table *ep;
2936
dfac187e
BS
2937 if (! insn_is_asm && icode < 0)
2938 {
2939 if (GET_CODE (PATTERN (insn)) == USE
2940 || GET_CODE (PATTERN (insn)) == CLOBBER
2941 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2942 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2943 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
2944 return 0;
2945 abort ();
2946 }
2947
774672d2
RK
2948 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
2949 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
32131a9c
RK
2950 {
2951 /* Check for setting an eliminable register. */
2952 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
774672d2 2953 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
32131a9c 2954 {
dd1eab0a
RK
2955#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2956 /* If this is setting the frame pointer register to the
2957 hardware frame pointer register and this is an elimination
2958 that will be done (tested above), this insn is really
2959 adjusting the frame pointer downward to compensate for
2960 the adjustment done before a nonlocal goto. */
2961 if (ep->from == FRAME_POINTER_REGNUM
2962 && ep->to == HARD_FRAME_POINTER_REGNUM)
2963 {
75eefe3f
UW
2964 rtx base = SET_SRC (old_set);
2965 rtx base_insn = insn;
2966 int offset = 0;
2967
2968 while (base != ep->to_rtx)
8026ebba 2969 {
75eefe3f
UW
2970 rtx prev_insn, prev_set;
2971
2972 if (GET_CODE (base) == PLUS
2973 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2974 {
2975 offset += INTVAL (XEXP (base, 1));
2976 base = XEXP (base, 0);
2977 }
2978 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2979 && (prev_set = single_set (prev_insn)) != 0
2980 && rtx_equal_p (SET_DEST (prev_set), base))
2981 {
2982 base = SET_SRC (prev_set);
2983 base_insn = prev_insn;
2984 }
2985 else
2986 break;
8026ebba 2987 }
dd1eab0a 2988
75eefe3f 2989 if (base == ep->to_rtx)
dd1eab0a 2990 {
c77fbfbe
GK
2991 rtx src
2992 = plus_constant (ep->to_rtx, offset - ep->offset);
2993
2994 new_body = old_body;
2995 if (! replace)
2996 {
2997 new_body = copy_insn (old_body);
2998 if (REG_NOTES (insn))
2999 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3000 }
3001 PATTERN (insn) = new_body;
3002 old_set = single_set (insn);
3003
3004 /* First see if this insn remains valid when we
3005 make the change. If not, keep the INSN_CODE
3006 the same and let reload fit it up. */
3007 validate_change (insn, &SET_SRC (old_set), src, 1);
3008 validate_change (insn, &SET_DEST (old_set),
3009 ep->to_rtx, 1);
3010 if (! apply_change_group ())
dd1eab0a 3011 {
c77fbfbe
GK
3012 SET_SRC (old_set) = src;
3013 SET_DEST (old_set) = ep->to_rtx;
dd1eab0a
RK
3014 }
3015
3016 val = 1;
3017 goto done;
3018 }
3019 }
3020#endif
3021
32131a9c
RK
3022 /* In this case this insn isn't serving a useful purpose. We
3023 will delete it in reload_as_needed once we know that this
3024 elimination is, in fact, being done.
3025
abc95ed3 3026 If REPLACE isn't set, we can't delete this insn, but needn't
32131a9c
RK
3027 process it since it won't be used unless something changes. */
3028 if (replace)
8a34409d 3029 {
1d7254c5 3030 delete_dead_insn (insn);
8a34409d
RH
3031 return 1;
3032 }
32131a9c
RK
3033 val = 1;
3034 goto done;
3035 }
aa5524a9 3036 }
32131a9c 3037
aa5524a9
BS
3038 /* We allow one special case which happens to work on all machines we
3039 currently support: a single set with the source being a PLUS of an
3040 eliminable register and a constant. */
3041 if (old_set
1abdf5e7 3042 && GET_CODE (SET_DEST (old_set)) == REG
aa5524a9
BS
3043 && GET_CODE (SET_SRC (old_set)) == PLUS
3044 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3045 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT
3046 && REGNO (XEXP (SET_SRC (old_set), 0)) < FIRST_PSEUDO_REGISTER)
3047 {
3048 rtx reg = XEXP (SET_SRC (old_set), 0);
3049 int offset = INTVAL (XEXP (SET_SRC (old_set), 1));
32131a9c 3050
aa5524a9
BS
3051 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3052 if (ep->from_rtx == reg && ep->can_eliminate)
3053 {
3054 offset += ep->offset;
32131a9c 3055
aa5524a9
BS
3056 if (offset == 0)
3057 {
f34c06e5
R
3058 int num_clobbers;
3059 /* We assume here that if we need a PARALLEL with
3060 CLOBBERs for this assignment, we can do with the
3061 MATCH_SCRATCHes that add_clobbers allocates.
3062 There's not much we can do if that doesn't work. */
aa5524a9
BS
3063 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3064 SET_DEST (old_set),
3065 ep->to_rtx);
f34c06e5
R
3066 num_clobbers = 0;
3067 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3068 if (num_clobbers)
3069 {
3070 rtvec vec = rtvec_alloc (num_clobbers + 1);
3071
3072 vec->elem[0] = PATTERN (insn);
3073 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3074 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3075 }
aa5524a9
BS
3076 if (INSN_CODE (insn) < 0)
3077 abort ();
3078 }
3079 else
3080 {
3081 new_body = old_body;
3082 if (! replace)
3083 {
3084 new_body = copy_insn (old_body);
3085 if (REG_NOTES (insn))
3086 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3087 }
3088 PATTERN (insn) = new_body;
3089 old_set = single_set (insn);
922d9d40 3090
aa5524a9
BS
3091 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3092 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3093 }
3094 val = 1;
3095 /* This can't have an effect on elimination offsets, so skip right
3096 to the end. */
3097 goto done;
3098 }
32131a9c
RK
3099 }
3100
dfac187e
BS
3101 /* Determine the effects of this insn on elimination offsets. */
3102 elimination_effects (old_body, 0);
3103
3104 /* Eliminate all eliminable registers occurring in operands that
3105 can be handled by reload. */
3106 extract_insn (insn);
3107 any_changes = 0;
3108 for (i = 0; i < recog_data.n_operands; i++)
3109 {
3110 orig_operand[i] = recog_data.operand[i];
3111 substed_operand[i] = recog_data.operand[i];
3112
3113 /* For an asm statement, every operand is eliminable. */
3114 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3115 {
3116 /* Check for setting a register that we know about. */
3117 if (recog_data.operand_type[i] != OP_IN
3118 && GET_CODE (orig_operand[i]) == REG)
3119 {
3120 /* If we are assigning to a register that can be eliminated, it
3121 must be as part of a PARALLEL, since the code above handles
3122 single SETs. We must indicate that we can no longer
3123 eliminate this reg. */
3124 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3125 ep++)
3126 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3127 ep->can_eliminate = 0;
3128 }
3129
3130 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3131 replace ? insn : NULL_RTX);
3132 if (substed_operand[i] != orig_operand[i])
3133 val = any_changes = 1;
3134 /* Terminate the search in check_eliminable_occurrences at
3135 this point. */
3136 *recog_data.operand_loc[i] = 0;
3137
3138 /* If an output operand changed from a REG to a MEM and INSN is an
3139 insn, write a CLOBBER insn. */
3140 if (recog_data.operand_type[i] != OP_IN
3141 && GET_CODE (orig_operand[i]) == REG
3142 && GET_CODE (substed_operand[i]) == MEM
3143 && replace)
3144 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3145 insn);
3146 }
3147 }
3148
3149 for (i = 0; i < recog_data.n_dups; i++)
3150 *recog_data.dup_loc[i]
1d7254c5 3151 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
dfac187e
BS
3152
3153 /* If any eliminable remain, they aren't eliminable anymore. */
3154 check_eliminable_occurrences (old_body);
32131a9c 3155
dfac187e
BS
3156 /* Substitute the operands; the new values are in the substed_operand
3157 array. */
3158 for (i = 0; i < recog_data.n_operands; i++)
3159 *recog_data.operand_loc[i] = substed_operand[i];
3160 for (i = 0; i < recog_data.n_dups; i++)
1d7254c5 3161 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
32131a9c 3162
dfac187e 3163 /* If we are replacing a body that was a (set X (plus Y Z)), try to
32131a9c
RK
3164 re-recognize the insn. We do this in case we had a simple addition
3165 but now can do this as a load-address. This saves an insn in this
dfac187e
BS
3166 common case.
3167 If re-recognition fails, the old insn code number will still be used,
3168 and some register operands may have changed into PLUS expressions.
3169 These will be handled by find_reloads by loading them into a register
1d7254c5 3170 again. */
32131a9c 3171
dfac187e 3172 if (val)
32131a9c 3173 {
7c791b13
RK
3174 /* If we aren't replacing things permanently and we changed something,
3175 make another copy to ensure that all the RTL is new. Otherwise
3176 things can go wrong if find_reload swaps commutative operands
0f41302f 3177 and one is inside RTL that has been copied while the other is not. */
dfac187e
BS
3178 new_body = old_body;
3179 if (! replace)
1b3b5765
BS
3180 {
3181 new_body = copy_insn (old_body);
3182 if (REG_NOTES (insn))
3183 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3184 }
dfac187e 3185 PATTERN (insn) = new_body;
7c791b13 3186
774672d2
RK
3187 /* If we had a move insn but now we don't, rerecognize it. This will
3188 cause spurious re-recognition if the old move had a PARALLEL since
3189 the new one still will, but we can't call single_set without
3190 having put NEW_BODY into the insn and the re-recognition won't
3191 hurt in this rare case. */
dfac187e
BS
3192 /* ??? Why this huge if statement - why don't we just rerecognize the
3193 thing always? */
3194 if (! insn_is_asm
3195 && old_set != 0
774672d2
RK
3196 && ((GET_CODE (SET_SRC (old_set)) == REG
3197 && (GET_CODE (new_body) != SET
3198 || GET_CODE (SET_SRC (new_body)) != REG))
3199 /* If this was a load from or store to memory, compare
1ccbefce
RH
3200 the MEM in recog_data.operand to the one in the insn.
3201 If they are not equal, then rerecognize the insn. */
774672d2
RK
3202 || (old_set != 0
3203 && ((GET_CODE (SET_SRC (old_set)) == MEM
1ccbefce 3204 && SET_SRC (old_set) != recog_data.operand[1])
774672d2 3205 || (GET_CODE (SET_DEST (old_set)) == MEM
1ccbefce 3206 && SET_DEST (old_set) != recog_data.operand[0])))
774672d2
RK
3207 /* If this was an add insn before, rerecognize. */
3208 || GET_CODE (SET_SRC (old_set)) == PLUS))
4a5d0fb5 3209 {
dfac187e
BS
3210 int new_icode = recog (PATTERN (insn), insn, 0);
3211 if (new_icode < 0)
3212 INSN_CODE (insn) = icode;
4a5d0fb5 3213 }
dfac187e 3214 }
32131a9c 3215
dfac187e
BS
3216 /* Restore the old body. If there were any changes to it, we made a copy
3217 of it while the changes were still in place, so we'll correctly return
3218 a modified insn below. */
3219 if (! replace)
3220 {
3221 /* Restore the old body. */
3222 for (i = 0; i < recog_data.n_operands; i++)
3223 *recog_data.operand_loc[i] = orig_operand[i];
3224 for (i = 0; i < recog_data.n_dups; i++)
1d7254c5 3225 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
32131a9c 3226 }
a8fdc208 3227
dfac187e
BS
3228 /* Update all elimination pairs to reflect the status after the current
3229 insn. The changes we make were determined by the earlier call to
3230 elimination_effects.
a8efe40d 3231
423adbb9 3232 We also detect cases where register elimination cannot be done,
32131a9c
RK
3233 namely, if a register would be both changed and referenced outside a MEM
3234 in the resulting insn since such an insn is often undefined and, even if
3235 not, we cannot know what meaning will be given to it. Note that it is
3236 valid to have a register used in an address in an insn that changes it
3237 (presumably with a pre- or post-increment or decrement).
3238
3239 If anything changes, return nonzero. */
3240
32131a9c
RK
3241 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3242 {
3243 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3244 ep->can_eliminate = 0;
3245
3246 ep->ref_outside_mem = 0;
3247
3248 if (ep->previous_offset != ep->offset)
3249 val = 1;
32131a9c
RK
3250 }
3251
3252 done:
9faa82d8 3253 /* If we changed something, perform elimination in REG_NOTES. This is
05b4c365
RK
3254 needed even when REPLACE is zero because a REG_DEAD note might refer
3255 to a register that we eliminate and could cause a different number
3256 of spill registers to be needed in the final reload pass than in
3257 the pre-passes. */
20748cab 3258 if (val && REG_NOTES (insn) != 0)
1914f5da 3259 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
05b4c365 3260
32131a9c
RK
3261 return val;
3262}
3263
cb2afeb3
R
3264/* Loop through all elimination pairs.
3265 Recalculate the number not at initial offset.
3266
3267 Compute the maximum offset (minimum offset if the stack does not
3268 grow downward) for each elimination pair. */
3269
3270static void
3271update_eliminable_offsets ()
3272{
3273 struct elim_table *ep;
3274
3275 num_not_at_initial_offset = 0;
3276 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3277 {
3278 ep->previous_offset = ep->offset;
3279 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3280 num_not_at_initial_offset++;
cb2afeb3
R
3281 }
3282}
3283
32131a9c
RK
3284/* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3285 replacement we currently believe is valid, mark it as not eliminable if X
3286 modifies DEST in any way other than by adding a constant integer to it.
3287
3288 If DEST is the frame pointer, we do nothing because we assume that
3ec2ea3e
DE
3289 all assignments to the hard frame pointer are nonlocal gotos and are being
3290 done at a time when they are valid and do not disturb anything else.
32131a9c 3291 Some machines want to eliminate a fake argument pointer with either the
3ec2ea3e
DE
3292 frame or stack pointer. Assignments to the hard frame pointer must not
3293 prevent this elimination.
32131a9c
RK
3294
3295 Called via note_stores from reload before starting its passes to scan
3296 the insns of the function. */
3297
3298static void
84832317 3299mark_not_eliminable (dest, x, data)
32131a9c
RK
3300 rtx dest;
3301 rtx x;
84832317 3302 void *data ATTRIBUTE_UNUSED;
32131a9c 3303{
b3694847 3304 unsigned int i;
32131a9c
RK
3305
3306 /* A SUBREG of a hard register here is just changing its mode. We should
3307 not see a SUBREG of an eliminable hard register, but check just in
3308 case. */
3309 if (GET_CODE (dest) == SUBREG)
3310 dest = SUBREG_REG (dest);
3311
3ec2ea3e 3312 if (dest == hard_frame_pointer_rtx)
32131a9c
RK
3313 return;
3314
3315 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3316 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3317 && (GET_CODE (x) != SET
3318 || GET_CODE (SET_SRC (x)) != PLUS
3319 || XEXP (SET_SRC (x), 0) != dest
3320 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3321 {
3322 reg_eliminate[i].can_eliminate_previous
3323 = reg_eliminate[i].can_eliminate = 0;
3324 num_eliminable--;
3325 }
3326}
09dd1133 3327
c47f5ea5
BS
3328/* Verify that the initial elimination offsets did not change since the
3329 last call to set_initial_elim_offsets. This is used to catch cases
3330 where something illegal happened during reload_as_needed that could
3331 cause incorrect code to be generated if we did not check for it. */
c8d8ed65 3332
c47f5ea5
BS
3333static void
3334verify_initial_elim_offsets ()
3335{
3336 int t;
3337
3338#ifdef ELIMINABLE_REGS
3339 struct elim_table *ep;
3340
3341 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3342 {
3343 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3344 if (t != ep->initial_offset)
3345 abort ();
3346 }
3347#else
3348 INITIAL_FRAME_POINTER_OFFSET (t);
3349 if (t != reg_eliminate[0].initial_offset)
3350 abort ();
05d10675 3351#endif
c47f5ea5
BS
3352}
3353
09dd1133 3354/* Reset all offsets on eliminable registers to their initial values. */
1d813780 3355
09dd1133
BS
3356static void
3357set_initial_elim_offsets ()
3358{
1f3b1e1a 3359 struct elim_table *ep = reg_eliminate;
09dd1133
BS
3360
3361#ifdef ELIMINABLE_REGS
1f3b1e1a 3362 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
09dd1133
BS
3363 {
3364 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
1f3b1e1a 3365 ep->previous_offset = ep->offset = ep->initial_offset;
09dd1133
BS
3366 }
3367#else
1f3b1e1a
JL
3368 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3369 ep->previous_offset = ep->offset = ep->initial_offset;
09dd1133
BS
3370#endif
3371
3372 num_not_at_initial_offset = 0;
1f3b1e1a 3373}
09dd1133 3374
1f3b1e1a
JL
3375/* Initialize the known label offsets.
3376 Set a known offset for each forced label to be at the initial offset
3377 of each elimination. We do this because we assume that all
3378 computed jumps occur from a location where each elimination is
3379 at its initial offset.
3380 For all other labels, show that we don't know the offsets. */
09dd1133 3381
1f3b1e1a
JL
3382static void
3383set_initial_label_offsets ()
3384{
3385 rtx x;
961192e1 3386 memset ((char *) &offsets_known_at[get_first_label_num ()], 0, num_labels);
09dd1133
BS
3387
3388 for (x = forced_labels; x; x = XEXP (x, 1))
3389 if (XEXP (x, 0))
3390 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3391}
3392
1f3b1e1a
JL
3393/* Set all elimination offsets to the known values for the code label given
3394 by INSN. */
1d813780 3395
1f3b1e1a
JL
3396static void
3397set_offsets_for_label (insn)
3398 rtx insn;
3399{
973838fd 3400 unsigned int i;
1f3b1e1a
JL
3401 int label_nr = CODE_LABEL_NUMBER (insn);
3402 struct elim_table *ep;
3403
3404 num_not_at_initial_offset = 0;
3405 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3406 {
3407 ep->offset = ep->previous_offset = offsets_at[label_nr][i];
3408 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3409 num_not_at_initial_offset++;
3410 }
3411}
3412
09dd1133 3413/* See if anything that happened changes which eliminations are valid.
981f6289 3414 For example, on the SPARC, whether or not the frame pointer can
09dd1133
BS
3415 be eliminated can depend on what registers have been used. We need
3416 not check some conditions again (such as flag_omit_frame_pointer)
3417 since they can't have changed. */
3418
3419static void
3420update_eliminables (pset)
3421 HARD_REG_SET *pset;
3422{
3423#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3424 int previous_frame_pointer_needed = frame_pointer_needed;
3425#endif
3426 struct elim_table *ep;
3427
3428 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3429 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3430#ifdef ELIMINABLE_REGS
3431 || ! CAN_ELIMINATE (ep->from, ep->to)
3432#endif
3433 )
3434 ep->can_eliminate = 0;
3435
3436 /* Look for the case where we have discovered that we can't replace
3437 register A with register B and that means that we will now be
3438 trying to replace register A with register C. This means we can
3439 no longer replace register C with register B and we need to disable
3440 such an elimination, if it exists. This occurs often with A == ap,
3441 B == sp, and C == fp. */
3442
3443 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3444 {
3445 struct elim_table *op;
b3694847 3446 int new_to = -1;
09dd1133
BS
3447
3448 if (! ep->can_eliminate && ep->can_eliminate_previous)
3449 {
3450 /* Find the current elimination for ep->from, if there is a
3451 new one. */
3452 for (op = reg_eliminate;
3453 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3454 if (op->from == ep->from && op->can_eliminate)
3455 {
3456 new_to = op->to;
3457 break;
3458 }
3459
3460 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3461 disable it. */
3462 for (op = reg_eliminate;
3463 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3464 if (op->from == new_to && op->to == ep->to)
3465 op->can_eliminate = 0;
3466 }
3467 }
3468
3469 /* See if any registers that we thought we could eliminate the previous
3470 time are no longer eliminable. If so, something has changed and we
3471 must spill the register. Also, recompute the number of eliminable
3472 registers and see if the frame pointer is needed; it is if there is
3473 no elimination of the frame pointer that we can perform. */
3474
3475 frame_pointer_needed = 1;
3476 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3477 {
3478 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3479 && ep->to != HARD_FRAME_POINTER_REGNUM)
3480 frame_pointer_needed = 0;
3481
3482 if (! ep->can_eliminate && ep->can_eliminate_previous)
3483 {
3484 ep->can_eliminate_previous = 0;
3485 SET_HARD_REG_BIT (*pset, ep->from);
3486 num_eliminable--;
3487 }
3488 }
3489
3490#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3491 /* If we didn't need a frame pointer last time, but we do now, spill
3492 the hard frame pointer. */
3493 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3494 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3495#endif
3496}
3497
3498/* Initialize the table of registers to eliminate. */
1d813780 3499
09dd1133
BS
3500static void
3501init_elim_table ()
3502{
3503 struct elim_table *ep;
590cf94d 3504#ifdef ELIMINABLE_REGS
0b5826ac 3505 const struct elim_table_1 *ep1;
590cf94d 3506#endif
09dd1133 3507
590cf94d 3508 if (!reg_eliminate)
ad85216e 3509 reg_eliminate = (struct elim_table *)
1d7254c5 3510 xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
05d10675 3511
09dd1133
BS
3512 /* Does this function require a frame pointer? */
3513
3514 frame_pointer_needed = (! flag_omit_frame_pointer
3515#ifdef EXIT_IGNORE_STACK
3516 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3517 and restore sp for alloca. So we can't eliminate
3518 the frame pointer in that case. At some point,
3519 we should improve this by emitting the
3520 sp-adjusting insns for this case. */
3521 || (current_function_calls_alloca
3522 && EXIT_IGNORE_STACK)
3523#endif
3524 || FRAME_POINTER_REQUIRED);
3525
3526 num_eliminable = 0;
3527
3528#ifdef ELIMINABLE_REGS
590cf94d
KG
3529 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3530 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
09dd1133 3531 {
590cf94d
KG
3532 ep->from = ep1->from;
3533 ep->to = ep1->to;
09dd1133
BS
3534 ep->can_eliminate = ep->can_eliminate_previous
3535 = (CAN_ELIMINATE (ep->from, ep->to)
3536 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3537 }
3538#else
590cf94d
KG
3539 reg_eliminate[0].from = reg_eliminate_1[0].from;
3540 reg_eliminate[0].to = reg_eliminate_1[0].to;
09dd1133
BS
3541 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3542 = ! frame_pointer_needed;
3543#endif
3544
3545 /* Count the number of eliminable registers and build the FROM and TO
3546 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3547 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3548 We depend on this. */
3549 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3550 {
3551 num_eliminable += ep->can_eliminate;
3552 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3553 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3554 }
3555}
32131a9c
RK
3556\f
3557/* Kick all pseudos out of hard register REGNO.
32131a9c
RK
3558
3559 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3560 because we found we can't eliminate some register. In the case, no pseudos
3561 are allowed to be in the register, even if they are only in a block that
3562 doesn't require spill registers, unlike the case when we are spilling this
3563 hard reg to produce another spill register.
3564
3565 Return nonzero if any pseudos needed to be kicked out. */
3566
03acd8f8 3567static void
e04ca094 3568spill_hard_reg (regno, cant_eliminate)
770ae6cc 3569 unsigned int regno;
32131a9c
RK
3570 int cant_eliminate;
3571{
b3694847 3572 int i;
32131a9c 3573
9ff3516a 3574 if (cant_eliminate)
03acd8f8
BS
3575 {
3576 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3577 regs_ever_live[regno] = 1;
3578 }
9ff3516a 3579
32131a9c
RK
3580 /* Spill every pseudo reg that was allocated to this reg
3581 or to something that overlaps this reg. */
3582
3583 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3584 if (reg_renumber[i] >= 0
770ae6cc
RK
3585 && (unsigned int) reg_renumber[i] <= regno
3586 && ((unsigned int) reg_renumber[i]
3587 + HARD_REGNO_NREGS ((unsigned int) reg_renumber[i],
32131a9c
RK
3588 PSEUDO_REGNO_MODE (i))
3589 > regno))
f5d8c9f4 3590 SET_REGNO_REG_SET (&spilled_pseudos, i);
03acd8f8 3591}
32131a9c 3592
03acd8f8
BS
3593/* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3594 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
770ae6cc 3595
03acd8f8
BS
3596static void
3597ior_hard_reg_set (set1, set2)
3598 HARD_REG_SET *set1, *set2;
3599{
3600 IOR_HARD_REG_SET (*set1, *set2);
3601}
05d10675 3602
03acd8f8
BS
3603/* After find_reload_regs has been run for all insn that need reloads,
3604 and/or spill_hard_regs was called, this function is used to actually
3605 spill pseudo registers and try to reallocate them. It also sets up the
3606 spill_regs array for use by choose_reload_regs. */
a8fdc208 3607
03acd8f8 3608static int
e04ca094 3609finish_spills (global)
03acd8f8 3610 int global;
03acd8f8
BS
3611{
3612 struct insn_chain *chain;
3613 int something_changed = 0;
3614 int i;
3615
3616 /* Build the spill_regs array for the function. */
3617 /* If there are some registers still to eliminate and one of the spill regs
3618 wasn't ever used before, additional stack space may have to be
3619 allocated to store this register. Thus, we may have changed the offset
3620 between the stack and frame pointers, so mark that something has changed.
32131a9c 3621
03acd8f8
BS
3622 One might think that we need only set VAL to 1 if this is a call-used
3623 register. However, the set of registers that must be saved by the
3624 prologue is not identical to the call-used set. For example, the
3625 register used by the call insn for the return PC is a call-used register,
3626 but must be saved by the prologue. */
3627
3628 n_spills = 0;
3629 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3630 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3631 {
3632 spill_reg_order[i] = n_spills;
3633 spill_regs[n_spills++] = i;
3634 if (num_eliminable && ! regs_ever_live[i])
3635 something_changed = 1;
3636 regs_ever_live[i] = 1;
3637 }
3638 else
3639 spill_reg_order[i] = -1;
3640
efc9bd41
RK
3641 EXECUTE_IF_SET_IN_REG_SET
3642 (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i,
3643 {
3644 /* Record the current hard register the pseudo is allocated to in
3645 pseudo_previous_regs so we avoid reallocating it to the same
3646 hard reg in a later pass. */
3647 if (reg_renumber[i] < 0)
3648 abort ();
3649
3650 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3651 /* Mark it as no longer having a hard register home. */
3652 reg_renumber[i] = -1;
3653 /* We will need to scan everything again. */
3654 something_changed = 1;
3655 });
7609e720 3656
03acd8f8
BS
3657 /* Retry global register allocation if possible. */
3658 if (global)
3659 {
961192e1 3660 memset ((char *) pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
03acd8f8
BS
3661 /* For every insn that needs reloads, set the registers used as spill
3662 regs in pseudo_forbidden_regs for every pseudo live across the
3663 insn. */
3664 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3665 {
3666 EXECUTE_IF_SET_IN_REG_SET
239a0f5b 3667 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
03acd8f8
BS
3668 {
3669 ior_hard_reg_set (pseudo_forbidden_regs + i,
3670 &chain->used_spill_regs);
3671 });
3672 EXECUTE_IF_SET_IN_REG_SET
239a0f5b 3673 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
03acd8f8
BS
3674 {
3675 ior_hard_reg_set (pseudo_forbidden_regs + i,
3676 &chain->used_spill_regs);
3677 });
3678 }
7609e720 3679
03acd8f8
BS
3680 /* Retry allocating the spilled pseudos. For each reg, merge the
3681 various reg sets that indicate which hard regs can't be used,
3682 and call retry_global_alloc.
05d10675 3683 We change spill_pseudos here to only contain pseudos that did not
03acd8f8
BS
3684 get a new hard register. */
3685 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3686 if (reg_old_renumber[i] != reg_renumber[i])
32131a9c 3687 {
03acd8f8
BS
3688 HARD_REG_SET forbidden;
3689 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3690 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3691 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3692 retry_global_alloc (i, forbidden);
3693 if (reg_renumber[i] >= 0)
f5d8c9f4 3694 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
32131a9c 3695 }
03acd8f8 3696 }
7609e720 3697
03acd8f8
BS
3698 /* Fix up the register information in the insn chain.
3699 This involves deleting those of the spilled pseudos which did not get
3700 a new hard register home from the live_{before,after} sets. */
7609e720
BS
3701 for (chain = reload_insn_chain; chain; chain = chain->next)
3702 {
03acd8f8
BS
3703 HARD_REG_SET used_by_pseudos;
3704 HARD_REG_SET used_by_pseudos2;
3705
239a0f5b
BS
3706 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3707 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
03acd8f8
BS
3708
3709 /* Mark any unallocated hard regs as available for spills. That
3710 makes inheritance work somewhat better. */
3711 if (chain->need_reload)
3712 {
239a0f5b
BS
3713 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3714 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
03acd8f8
BS
3715 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3716
3717 /* Save the old value for the sanity test below. */
3718 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3719
239a0f5b
BS
3720 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3721 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
03acd8f8
BS
3722 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3723 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3724
3725 /* Make sure we only enlarge the set. */
3726 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3727 abort ();
3728 ok:;
3729 }
7609e720 3730 }
03acd8f8
BS
3731
3732 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3733 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3734 {
3735 int regno = reg_renumber[i];
3736 if (reg_old_renumber[i] == regno)
3737 continue;
05d10675 3738
03acd8f8
BS
3739 alter_reg (i, reg_old_renumber[i]);
3740 reg_old_renumber[i] = regno;
e04ca094 3741 if (rtl_dump_file)
03acd8f8
BS
3742 {
3743 if (regno == -1)
e04ca094 3744 fprintf (rtl_dump_file, " Register %d now on stack.\n\n", i);
03acd8f8 3745 else
e04ca094 3746 fprintf (rtl_dump_file, " Register %d now in %d.\n\n",
03acd8f8
BS
3747 i, reg_renumber[i]);
3748 }
3749 }
3750
3751 return something_changed;
7609e720 3752}
32131a9c 3753\f
05d10675 3754/* Find all paradoxical subregs within X and update reg_max_ref_width.
56f58d3a
RK
3755 Also mark any hard registers used to store user variables as
3756 forbidden from being used for spill registers. */
32131a9c
RK
3757
3758static void
3759scan_paradoxical_subregs (x)
b3694847 3760 rtx x;
32131a9c 3761{
b3694847
SS
3762 int i;
3763 const char *fmt;
3764 enum rtx_code code = GET_CODE (x);
32131a9c
RK
3765
3766 switch (code)
3767 {
56f58d3a 3768 case REG:
03acd8f8 3769#if 0
e9a25f70 3770 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
f95182a4 3771 && REG_USERVAR_P (x))
03acd8f8
BS
3772 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
3773#endif
56f58d3a
RK
3774 return;
3775
32131a9c
RK
3776 case CONST_INT:
3777 case CONST:
3778 case SYMBOL_REF:
3779 case LABEL_REF:
3780 case CONST_DOUBLE:
69ef87e2 3781 case CONST_VECTOR: /* shouldn't happen, but just in case. */
32131a9c
RK
3782 case CC0:
3783 case PC:
32131a9c
RK
3784 case USE:
3785 case CLOBBER:
3786 return;
3787
3788 case SUBREG:
3789 if (GET_CODE (SUBREG_REG (x)) == REG
3790 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3791 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3792 = GET_MODE_SIZE (GET_MODE (x));
3793 return;
05d10675 3794
e9a25f70
JL
3795 default:
3796 break;
32131a9c
RK
3797 }
3798
3799 fmt = GET_RTX_FORMAT (code);
3800 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3801 {
3802 if (fmt[i] == 'e')
3803 scan_paradoxical_subregs (XEXP (x, i));
3804 else if (fmt[i] == 'E')
3805 {
b3694847 3806 int j;
1d7254c5 3807 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
32131a9c
RK
3808 scan_paradoxical_subregs (XVECEXP (x, i, j));
3809 }
3810 }
3811}
3812\f
32131a9c
RK
3813/* Reload pseudo-registers into hard regs around each insn as needed.
3814 Additional register load insns are output before the insn that needs it
3815 and perhaps store insns after insns that modify the reloaded pseudo reg.
3816
3817 reg_last_reload_reg and reg_reloaded_contents keep track of
d08ea79f 3818 which registers are already available in reload registers.
32131a9c
RK
3819 We update these for the reloads that we perform,
3820 as the insns are scanned. */
3821
3822static void
e04ca094 3823reload_as_needed (live_known)
32131a9c
RK
3824 int live_known;
3825{
7609e720 3826 struct insn_chain *chain;
553687c9 3827#if defined (AUTO_INC_DEC)
b3694847 3828 int i;
973838fd 3829#endif
32131a9c 3830 rtx x;
32131a9c 3831
961192e1
JM
3832 memset ((char *) spill_reg_rtx, 0, sizeof spill_reg_rtx);
3833 memset ((char *) spill_reg_store, 0, sizeof spill_reg_store);
ff154f78
MM
3834 reg_last_reload_reg = (rtx *) xcalloc (max_regno, sizeof (rtx));
3835 reg_has_output_reload = (char *) xmalloc (max_regno);
e6e52be0 3836 CLEAR_HARD_REG_SET (reg_reloaded_valid);
32131a9c 3837
1f3b1e1a 3838 set_initial_elim_offsets ();
32131a9c 3839
7609e720 3840 for (chain = reload_insn_chain; chain; chain = chain->next)
32131a9c 3841 {
03acd8f8 3842 rtx prev;
7609e720
BS
3843 rtx insn = chain->insn;
3844 rtx old_next = NEXT_INSN (insn);
32131a9c
RK
3845
3846 /* If we pass a label, copy the offsets from the label information
3847 into the current offsets of each elimination. */
3848 if (GET_CODE (insn) == CODE_LABEL)
1f3b1e1a 3849 set_offsets_for_label (insn);
32131a9c 3850
2c3c49de 3851 else if (INSN_P (insn))
32131a9c 3852 {
449655a6 3853 rtx oldpat = copy_rtx (PATTERN (insn));
32131a9c 3854
2758481d
RS
3855 /* If this is a USE and CLOBBER of a MEM, ensure that any
3856 references to eliminable registers have been removed. */
3857
3858 if ((GET_CODE (PATTERN (insn)) == USE
3859 || GET_CODE (PATTERN (insn)) == CLOBBER)
3860 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3861 XEXP (XEXP (PATTERN (insn), 0), 0)
3862 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
29ae5012 3863 GET_MODE (XEXP (PATTERN (insn), 0)),
1914f5da 3864 NULL_RTX);
2758481d 3865
32131a9c
RK
3866 /* If we need to do register elimination processing, do so.
3867 This might delete the insn, in which case we are done. */
2b49ee39 3868 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
32131a9c
RK
3869 {
3870 eliminate_regs_in_insn (insn, 1);
3871 if (GET_CODE (insn) == NOTE)
cb2afeb3
R
3872 {
3873 update_eliminable_offsets ();
3874 continue;
3875 }
32131a9c
RK
3876 }
3877
7609e720
BS
3878 /* If need_elim is nonzero but need_reload is zero, one might think
3879 that we could simply set n_reloads to 0. However, find_reloads
3880 could have done some manipulation of the insn (such as swapping
3881 commutative operands), and these manipulations are lost during
3882 the first pass for every insn that needs register elimination.
3883 So the actions of find_reloads must be redone here. */
3884
03acd8f8
BS
3885 if (! chain->need_elim && ! chain->need_reload
3886 && ! chain->need_operand_change)
32131a9c
RK
3887 n_reloads = 0;
3888 /* First find the pseudo regs that must be reloaded for this insn.
3889 This info is returned in the tables reload_... (see reload.h).
3890 Also modify the body of INSN by substituting RELOAD
3891 rtx's for those pseudo regs. */
3892 else
3893 {
961192e1 3894 memset (reg_has_output_reload, 0, max_regno);
32131a9c
RK
3895 CLEAR_HARD_REG_SET (reg_is_output_reload);
3896
3897 find_reloads (insn, 1, spill_indirect_levels, live_known,
3898 spill_reg_order);
3899 }
3900
3901 if (n_reloads > 0)
3902 {
cb2afeb3 3903 rtx next = NEXT_INSN (insn);
3c3eeea6 3904 rtx p;
32131a9c 3905
cb2afeb3
R
3906 prev = PREV_INSN (insn);
3907
32131a9c
RK
3908 /* Now compute which reload regs to reload them into. Perhaps
3909 reusing reload regs from previous insns, or else output
3910 load insns to reload them. Maybe output store insns too.
3911 Record the choices of reload reg in reload_reg_rtx. */
03acd8f8 3912 choose_reload_regs (chain);
32131a9c 3913
05d10675 3914 /* Merge any reloads that we didn't combine for fear of
546b63fb
RK
3915 increasing the number of spill registers needed but now
3916 discover can be safely merged. */
f95182a4
ILT
3917 if (SMALL_REGISTER_CLASSES)
3918 merge_assigned_reloads (insn);
546b63fb 3919
32131a9c
RK
3920 /* Generate the insns to reload operands into or out of
3921 their reload regs. */
e04ca094 3922 emit_reload_insns (chain);
32131a9c
RK
3923
3924 /* Substitute the chosen reload regs from reload_reg_rtx
3925 into the insn's body (or perhaps into the bodies of other
3926 load and store insn that we just made for reloading
3927 and that we moved the structure into). */
f759eb8b 3928 subst_reloads (insn);
3c3eeea6
RK
3929
3930 /* If this was an ASM, make sure that all the reload insns
3931 we have generated are valid. If not, give an error
3932 and delete them. */
3933
3934 if (asm_noperands (PATTERN (insn)) >= 0)
3935 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
2c3c49de 3936 if (p != insn && INSN_P (p)
3c3eeea6 3937 && (recog_memoized (p) < 0
0eadeb15 3938 || (extract_insn (p), ! constrain_operands (1))))
3c3eeea6
RK
3939 {
3940 error_for_asm (insn,
3941 "`asm' operand requires impossible reload");
ca6c03ca 3942 delete_insn (p);
3c3eeea6 3943 }
32131a9c 3944 }
5d7ef82a
BS
3945
3946 if (num_eliminable && chain->need_elim)
3947 update_eliminable_offsets ();
3948
32131a9c
RK
3949 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3950 is no longer validly lying around to save a future reload.
3951 Note that this does not detect pseudos that were reloaded
3952 for this insn in order to be stored in
3953 (obeying register constraints). That is correct; such reload
3954 registers ARE still valid. */
84832317 3955 note_stores (oldpat, forget_old_reloads_1, NULL);
32131a9c
RK
3956
3957 /* There may have been CLOBBER insns placed after INSN. So scan
3958 between INSN and NEXT and use them to forget old reloads. */
7609e720 3959 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
32131a9c 3960 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
84832317 3961 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
32131a9c
RK
3962
3963#ifdef AUTO_INC_DEC
cb2afeb3
R
3964 /* Likewise for regs altered by auto-increment in this insn.
3965 REG_INC notes have been changed by reloading:
3966 find_reloads_address_1 records substitutions for them,
3967 which have been performed by subst_reloads above. */
3968 for (i = n_reloads - 1; i >= 0; i--)
3969 {
eceef4c9 3970 rtx in_reg = rld[i].in_reg;
cb2afeb3
R
3971 if (in_reg)
3972 {
3973 enum rtx_code code = GET_CODE (in_reg);
3974 /* PRE_INC / PRE_DEC will have the reload register ending up
3975 with the same value as the stack slot, but that doesn't
3976 hold true for POST_INC / POST_DEC. Either we have to
3977 convert the memory access to a true POST_INC / POST_DEC,
3978 or we can't use the reload register for inheritance. */
3979 if ((code == POST_INC || code == POST_DEC)
3980 && TEST_HARD_REG_BIT (reg_reloaded_valid,
eceef4c9 3981 REGNO (rld[i].reg_rtx))
04bbb0c5
JW
3982 /* Make sure it is the inc/dec pseudo, and not
3983 some other (e.g. output operand) pseudo. */
eceef4c9 3984 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
04bbb0c5 3985 == REGNO (XEXP (in_reg, 0))))
05d10675 3986
cb2afeb3 3987 {
eceef4c9 3988 rtx reload_reg = rld[i].reg_rtx;
cb2afeb3
R
3989 enum machine_mode mode = GET_MODE (reload_reg);
3990 int n = 0;
3991 rtx p;
3992
3993 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
3994 {
3995 /* We really want to ignore REG_INC notes here, so
3996 use PATTERN (p) as argument to reg_set_p . */
3997 if (reg_set_p (reload_reg, PATTERN (p)))
3998 break;
4b983fdc 3999 n = count_occurrences (PATTERN (p), reload_reg, 0);
cb2afeb3
R
4000 if (! n)
4001 continue;
4002 if (n == 1)
f67c2384
JL
4003 {
4004 n = validate_replace_rtx (reload_reg,
4005 gen_rtx (code, mode,
4006 reload_reg),
4007 p);
4008
4009 /* We must also verify that the constraints
4010 are met after the replacement. */
4011 extract_insn (p);
4012 if (n)
4013 n = constrain_operands (1);
4014 else
4015 break;
4016
4017 /* If the constraints were not met, then
4018 undo the replacement. */
4019 if (!n)
4020 {
4021 validate_replace_rtx (gen_rtx (code, mode,
4022 reload_reg),
4023 reload_reg, p);
4024 break;
4025 }
05d10675 4026
f67c2384 4027 }
cb2afeb3
R
4028 break;
4029 }
4030 if (n == 1)
02eb1393
R
4031 {
4032 REG_NOTES (p)
4033 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4034 REG_NOTES (p));
4035 /* Mark this as having an output reload so that the
4036 REG_INC processing code below won't invalidate
4037 the reload for inheritance. */
4038 SET_HARD_REG_BIT (reg_is_output_reload,
4039 REGNO (reload_reg));
4040 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4041 }
cb2afeb3 4042 else
1d7254c5 4043 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
84832317 4044 NULL);
cb2afeb3 4045 }
02eb1393
R
4046 else if ((code == PRE_INC || code == PRE_DEC)
4047 && TEST_HARD_REG_BIT (reg_reloaded_valid,
eceef4c9 4048 REGNO (rld[i].reg_rtx))
02eb1393
R
4049 /* Make sure it is the inc/dec pseudo, and not
4050 some other (e.g. output operand) pseudo. */
eceef4c9 4051 && (reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
02eb1393
R
4052 == REGNO (XEXP (in_reg, 0))))
4053 {
4054 SET_HARD_REG_BIT (reg_is_output_reload,
eceef4c9 4055 REGNO (rld[i].reg_rtx));
02eb1393
R
4056 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4057 }
cb2afeb3
R
4058 }
4059 }
02eb1393
R
4060 /* If a pseudo that got a hard register is auto-incremented,
4061 we must purge records of copying it into pseudos without
4062 hard registers. */
32131a9c
RK
4063 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4064 if (REG_NOTE_KIND (x) == REG_INC)
4065 {
4066 /* See if this pseudo reg was reloaded in this insn.
4067 If so, its last-reload info is still valid
4068 because it is based on this insn's reload. */
4069 for (i = 0; i < n_reloads; i++)
eceef4c9 4070 if (rld[i].out == XEXP (x, 0))
32131a9c
RK
4071 break;
4072
08fb99fa 4073 if (i == n_reloads)
84832317 4074 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
32131a9c
RK
4075 }
4076#endif
4077 }
4078 /* A reload reg's contents are unknown after a label. */
4079 if (GET_CODE (insn) == CODE_LABEL)
e6e52be0 4080 CLEAR_HARD_REG_SET (reg_reloaded_valid);
32131a9c
RK
4081
4082 /* Don't assume a reload reg is still good after a call insn
4083 if it is a call-used reg. */
546b63fb 4084 else if (GET_CODE (insn) == CALL_INSN)
8e2e89f7 4085 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
32131a9c 4086 }
ff154f78
MM
4087
4088 /* Clean up. */
4089 free (reg_last_reload_reg);
4090 free (reg_has_output_reload);
32131a9c
RK
4091}
4092
4093/* Discard all record of any value reloaded from X,
4094 or reloaded in X from someplace else;
4095 unless X is an output reload reg of the current insn.
4096
4097 X may be a hard reg (the reload reg)
4098 or it may be a pseudo reg that was reloaded from. */
4099
4100static void
84832317 4101forget_old_reloads_1 (x, ignored, data)
32131a9c 4102 rtx x;
487a6e06 4103 rtx ignored ATTRIBUTE_UNUSED;
84832317 4104 void *data ATTRIBUTE_UNUSED;
32131a9c 4105{
770ae6cc
RK
4106 unsigned int regno;
4107 unsigned int nr;
0a2e51a9 4108
ddef6bc7
JJ
4109 /* note_stores does give us subregs of hard regs,
4110 subreg_regno_offset will abort if it is not a hard reg. */
0a2e51a9
RS
4111 while (GET_CODE (x) == SUBREG)
4112 {
fefac463
AH
4113 /* We ignore the subreg offset when calculating the regno,
4114 because we are using the entire underlying hard register
4115 below. */
0a2e51a9
RS
4116 x = SUBREG_REG (x);
4117 }
32131a9c
RK
4118
4119 if (GET_CODE (x) != REG)
4120 return;
4121
fefac463 4122 regno = REGNO (x);
32131a9c
RK
4123
4124 if (regno >= FIRST_PSEUDO_REGISTER)
4125 nr = 1;
4126 else
4127 {
770ae6cc
RK
4128 unsigned int i;
4129
32131a9c
RK
4130 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4131 /* Storing into a spilled-reg invalidates its contents.
4132 This can happen if a block-local pseudo is allocated to that reg
4133 and it wasn't spilled because this block's total need is 0.
4134 Then some insn might have an optional reload and use this reg. */
4135 for (i = 0; i < nr; i++)
e6e52be0
R
4136 /* But don't do this if the reg actually serves as an output
4137 reload reg in the current instruction. */
4138 if (n_reloads == 0
4139 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
5d77a50c
BS
4140 {
4141 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4142 spill_reg_store[regno + i] = 0;
4143 }
32131a9c
RK
4144 }
4145
4146 /* Since value of X has changed,
4147 forget any value previously copied from it. */
4148
4149 while (nr-- > 0)
4150 /* But don't forget a copy if this is the output reload
4151 that establishes the copy's validity. */
4152 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4153 reg_last_reload_reg[regno + nr] = 0;
4154}
4155\f
32131a9c
RK
4156/* The following HARD_REG_SETs indicate when each hard register is
4157 used for a reload of various parts of the current insn. */
4158
9e3a9cf2
BS
4159/* If reg is unavailable for all reloads. */
4160static HARD_REG_SET reload_reg_unavailable;
32131a9c
RK
4161/* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4162static HARD_REG_SET reload_reg_used;
546b63fb
RK
4163/* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4164static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
47c8cf91
ILT
4165/* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4166static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
546b63fb
RK
4167/* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4168static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
47c8cf91
ILT
4169/* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4170static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
546b63fb
RK
4171/* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4172static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4173/* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4174static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
32131a9c
RK
4175/* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4176static HARD_REG_SET reload_reg_used_in_op_addr;
893bc853
RK
4177/* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4178static HARD_REG_SET reload_reg_used_in_op_addr_reload;
546b63fb
RK
4179/* If reg is in use for a RELOAD_FOR_INSN reload. */
4180static HARD_REG_SET reload_reg_used_in_insn;
4181/* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4182static HARD_REG_SET reload_reg_used_in_other_addr;
32131a9c
RK
4183
4184/* If reg is in use as a reload reg for any sort of reload. */
4185static HARD_REG_SET reload_reg_used_at_all;
4186
be7ae2a4
RK
4187/* If reg is use as an inherited reload. We just mark the first register
4188 in the group. */
4189static HARD_REG_SET reload_reg_used_for_inherit;
4190
f1db3576
JL
4191/* Records which hard regs are used in any way, either as explicit use or
4192 by being allocated to a pseudo during any point of the current insn. */
4193static HARD_REG_SET reg_used_in_insn;
297927a8 4194
546b63fb
RK
4195/* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4196 TYPE. MODE is used to indicate how many consecutive regs are
4197 actually used. */
32131a9c
RK
4198
4199static void
546b63fb 4200mark_reload_reg_in_use (regno, opnum, type, mode)
770ae6cc 4201 unsigned int regno;
546b63fb
RK
4202 int opnum;
4203 enum reload_type type;
32131a9c
RK
4204 enum machine_mode mode;
4205{
770ae6cc
RK
4206 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4207 unsigned int i;
32131a9c
RK
4208
4209 for (i = regno; i < nregs + regno; i++)
4210 {
546b63fb 4211 switch (type)
32131a9c
RK
4212 {
4213 case RELOAD_OTHER:
4214 SET_HARD_REG_BIT (reload_reg_used, i);
4215 break;
4216
546b63fb
RK
4217 case RELOAD_FOR_INPUT_ADDRESS:
4218 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
32131a9c
RK
4219 break;
4220
47c8cf91
ILT
4221 case RELOAD_FOR_INPADDR_ADDRESS:
4222 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4223 break;
4224
546b63fb
RK
4225 case RELOAD_FOR_OUTPUT_ADDRESS:
4226 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
32131a9c
RK
4227 break;
4228
47c8cf91
ILT
4229 case RELOAD_FOR_OUTADDR_ADDRESS:
4230 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4231 break;
4232
32131a9c
RK
4233 case RELOAD_FOR_OPERAND_ADDRESS:
4234 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4235 break;
4236
893bc853
RK
4237 case RELOAD_FOR_OPADDR_ADDR:
4238 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4239 break;
4240
546b63fb
RK
4241 case RELOAD_FOR_OTHER_ADDRESS:
4242 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4243 break;
4244
32131a9c 4245 case RELOAD_FOR_INPUT:
546b63fb 4246 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
32131a9c
RK
4247 break;
4248
4249 case RELOAD_FOR_OUTPUT:
546b63fb
RK
4250 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4251 break;
4252
4253 case RELOAD_FOR_INSN:
4254 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
32131a9c
RK
4255 break;
4256 }
4257
4258 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4259 }
4260}
4261
be7ae2a4
RK
4262/* Similarly, but show REGNO is no longer in use for a reload. */
4263
4264static void
4265clear_reload_reg_in_use (regno, opnum, type, mode)
770ae6cc 4266 unsigned int regno;
be7ae2a4
RK
4267 int opnum;
4268 enum reload_type type;
4269 enum machine_mode mode;
4270{
770ae6cc
RK
4271 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4272 unsigned int start_regno, end_regno, r;
be7ae2a4 4273 int i;
cb2afeb3
R
4274 /* A complication is that for some reload types, inheritance might
4275 allow multiple reloads of the same types to share a reload register.
4276 We set check_opnum if we have to check only reloads with the same
4277 operand number, and check_any if we have to check all reloads. */
4278 int check_opnum = 0;
4279 int check_any = 0;
4280 HARD_REG_SET *used_in_set;
be7ae2a4 4281
cb2afeb3 4282 switch (type)
be7ae2a4 4283 {
cb2afeb3
R
4284 case RELOAD_OTHER:
4285 used_in_set = &reload_reg_used;
4286 break;
be7ae2a4 4287
cb2afeb3
R
4288 case RELOAD_FOR_INPUT_ADDRESS:
4289 used_in_set = &reload_reg_used_in_input_addr[opnum];
4290 break;
be7ae2a4 4291
cb2afeb3
R
4292 case RELOAD_FOR_INPADDR_ADDRESS:
4293 check_opnum = 1;
4294 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4295 break;
47c8cf91 4296
cb2afeb3
R
4297 case RELOAD_FOR_OUTPUT_ADDRESS:
4298 used_in_set = &reload_reg_used_in_output_addr[opnum];
4299 break;
be7ae2a4 4300
cb2afeb3
R
4301 case RELOAD_FOR_OUTADDR_ADDRESS:
4302 check_opnum = 1;
4303 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4304 break;
47c8cf91 4305
cb2afeb3
R
4306 case RELOAD_FOR_OPERAND_ADDRESS:
4307 used_in_set = &reload_reg_used_in_op_addr;
4308 break;
be7ae2a4 4309
cb2afeb3
R
4310 case RELOAD_FOR_OPADDR_ADDR:
4311 check_any = 1;
4312 used_in_set = &reload_reg_used_in_op_addr_reload;
4313 break;
893bc853 4314
cb2afeb3
R
4315 case RELOAD_FOR_OTHER_ADDRESS:
4316 used_in_set = &reload_reg_used_in_other_addr;
4317 check_any = 1;
4318 break;
be7ae2a4 4319
cb2afeb3
R
4320 case RELOAD_FOR_INPUT:
4321 used_in_set = &reload_reg_used_in_input[opnum];
4322 break;
be7ae2a4 4323
cb2afeb3
R
4324 case RELOAD_FOR_OUTPUT:
4325 used_in_set = &reload_reg_used_in_output[opnum];
4326 break;
be7ae2a4 4327
cb2afeb3
R
4328 case RELOAD_FOR_INSN:
4329 used_in_set = &reload_reg_used_in_insn;
4330 break;
4331 default:
4332 abort ();
4333 }
4334 /* We resolve conflicts with remaining reloads of the same type by
68e82b83 4335 excluding the intervals of reload registers by them from the
cb2afeb3
R
4336 interval of freed reload registers. Since we only keep track of
4337 one set of interval bounds, we might have to exclude somewhat
3e92902c 4338 more than what would be necessary if we used a HARD_REG_SET here.
cb2afeb3
R
4339 But this should only happen very infrequently, so there should
4340 be no reason to worry about it. */
05d10675 4341
cb2afeb3
R
4342 start_regno = regno;
4343 end_regno = regno + nregs;
4344 if (check_opnum || check_any)
4345 {
4346 for (i = n_reloads - 1; i >= 0; i--)
4347 {
eceef4c9
BS
4348 if (rld[i].when_needed == type
4349 && (check_any || rld[i].opnum == opnum)
4350 && rld[i].reg_rtx)
cb2afeb3 4351 {
770ae6cc
RK
4352 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4353 unsigned int conflict_end
cb2afeb3 4354 = (conflict_start
8ec450a4 4355 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
cb2afeb3
R
4356
4357 /* If there is an overlap with the first to-be-freed register,
4358 adjust the interval start. */
4359 if (conflict_start <= start_regno && conflict_end > start_regno)
4360 start_regno = conflict_end;
4361 /* Otherwise, if there is a conflict with one of the other
4362 to-be-freed registers, adjust the interval end. */
4363 if (conflict_start > start_regno && conflict_start < end_regno)
4364 end_regno = conflict_start;
4365 }
be7ae2a4
RK
4366 }
4367 }
770ae6cc
RK
4368
4369 for (r = start_regno; r < end_regno; r++)
4370 CLEAR_HARD_REG_BIT (*used_in_set, r);
be7ae2a4
RK
4371}
4372
32131a9c 4373/* 1 if reg REGNO is free as a reload reg for a reload of the sort
546b63fb 4374 specified by OPNUM and TYPE. */
32131a9c
RK
4375
4376static int
546b63fb 4377reload_reg_free_p (regno, opnum, type)
770ae6cc 4378 unsigned int regno;
546b63fb
RK
4379 int opnum;
4380 enum reload_type type;
32131a9c 4381{
546b63fb
RK
4382 int i;
4383
2edc8d65 4384 /* In use for a RELOAD_OTHER means it's not available for anything. */
9e3a9cf2
BS
4385 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4386 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
32131a9c 4387 return 0;
546b63fb
RK
4388
4389 switch (type)
32131a9c
RK
4390 {
4391 case RELOAD_OTHER:
2edc8d65
RK
4392 /* In use for anything means we can't use it for RELOAD_OTHER. */
4393 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
224f1d71
RK
4394 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4395 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4396 return 0;
4397
4398 for (i = 0; i < reload_n_operands; i++)
4399 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4400 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
224f1d71 4401 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4402 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
224f1d71
RK
4403 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4404 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4405 return 0;
4406
4407 return 1;
32131a9c 4408
32131a9c 4409 case RELOAD_FOR_INPUT:
546b63fb
RK
4410 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4411 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4412 return 0;
4413
893bc853
RK
4414 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4415 return 0;
4416
546b63fb
RK
4417 /* If it is used for some other input, can't use it. */
4418 for (i = 0; i < reload_n_operands; i++)
4419 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4420 return 0;
4421
4422 /* If it is used in a later operand's address, can't use it. */
4423 for (i = opnum + 1; i < reload_n_operands; i++)
47c8cf91
ILT
4424 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4425 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
546b63fb
RK
4426 return 0;
4427
4428 return 1;
4429
4430 case RELOAD_FOR_INPUT_ADDRESS:
4431 /* Can't use a register if it is used for an input address for this
4432 operand or used as an input in an earlier one. */
47c8cf91
ILT
4433 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4434 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4435 return 0;
4436
4437 for (i = 0; i < opnum; i++)
4438 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4439 return 0;
4440
4441 return 1;
4442
4443 case RELOAD_FOR_INPADDR_ADDRESS:
4444 /* Can't use a register if it is used for an input address
05d10675
BS
4445 for this operand or used as an input in an earlier
4446 one. */
47c8cf91 4447 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
546b63fb
RK
4448 return 0;
4449
4450 for (i = 0; i < opnum; i++)
4451 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4452 return 0;
4453
4454 return 1;
4455
4456 case RELOAD_FOR_OUTPUT_ADDRESS:
4457 /* Can't use a register if it is used for an output address for this
d1d18b46
DJ
4458 operand or used as an output in this or a later operand. Note
4459 that multiple output operands are emitted in reverse order, so
4460 the conflicting ones are those with lower indices. */
546b63fb
RK
4461 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4462 return 0;
4463
d1d18b46 4464 for (i = 0; i <= opnum; i++)
546b63fb
RK
4465 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4466 return 0;
4467
4468 return 1;
4469
47c8cf91
ILT
4470 case RELOAD_FOR_OUTADDR_ADDRESS:
4471 /* Can't use a register if it is used for an output address
05d10675 4472 for this operand or used as an output in this or a
d1d18b46
DJ
4473 later operand. Note that multiple output operands are
4474 emitted in reverse order, so the conflicting ones are
4475 those with lower indices. */
47c8cf91
ILT
4476 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4477 return 0;
4478
d1d18b46 4479 for (i = 0; i <= opnum; i++)
47c8cf91
ILT
4480 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4481 return 0;
4482
4483 return 1;
4484
32131a9c 4485 case RELOAD_FOR_OPERAND_ADDRESS:
546b63fb
RK
4486 for (i = 0; i < reload_n_operands; i++)
4487 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4488 return 0;
4489
4490 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4491 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4492
893bc853
RK
4493 case RELOAD_FOR_OPADDR_ADDR:
4494 for (i = 0; i < reload_n_operands; i++)
05d10675
BS
4495 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4496 return 0;
893bc853 4497
a94ce333 4498 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
893bc853 4499
32131a9c 4500 case RELOAD_FOR_OUTPUT:
546b63fb 4501 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
d1d18b46
DJ
4502 outputs, or an operand address for this or an earlier output.
4503 Note that multiple output operands are emitted in reverse order,
4504 so the conflicting ones are those with higher indices. */
546b63fb
RK
4505 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4506 return 0;
4507
4508 for (i = 0; i < reload_n_operands; i++)
4509 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4510 return 0;
4511
d1d18b46 4512 for (i = opnum; i < reload_n_operands; i++)
47c8cf91
ILT
4513 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4514 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
546b63fb
RK
4515 return 0;
4516
4517 return 1;
4518
4519 case RELOAD_FOR_INSN:
4520 for (i = 0; i < reload_n_operands; i++)
4521 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4522 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4523 return 0;
4524
4525 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4526 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4527
4528 case RELOAD_FOR_OTHER_ADDRESS:
4529 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
32131a9c
RK
4530 }
4531 abort ();
4532}
4533
32131a9c 4534/* Return 1 if the value in reload reg REGNO, as used by a reload
546b63fb 4535 needed for the part of the insn specified by OPNUM and TYPE,
32131a9c
RK
4536 is still available in REGNO at the end of the insn.
4537
4538 We can assume that the reload reg was already tested for availability
4539 at the time it is needed, and we should not check this again,
4540 in case the reg has already been marked in use. */
4541
4542static int
546b63fb 4543reload_reg_reaches_end_p (regno, opnum, type)
770ae6cc 4544 unsigned int regno;
546b63fb
RK
4545 int opnum;
4546 enum reload_type type;
32131a9c 4547{
546b63fb
RK
4548 int i;
4549
4550 switch (type)
32131a9c
RK
4551 {
4552 case RELOAD_OTHER:
4553 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4554 its value must reach the end. */
4555 return 1;
4556
4557 /* If this use is for part of the insn,
05d10675 4558 its value reaches if no subsequent part uses the same register.
546b63fb
RK
4559 Just like the above function, don't try to do this with lots
4560 of fallthroughs. */
4561
4562 case RELOAD_FOR_OTHER_ADDRESS:
4563 /* Here we check for everything else, since these don't conflict
4564 with anything else and everything comes later. */
4565
4566 for (i = 0; i < reload_n_operands; i++)
4567 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4568 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4569 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4570 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4571 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
546b63fb
RK
4572 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4573 return 0;
4574
4575 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4576 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4577 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4578
4579 case RELOAD_FOR_INPUT_ADDRESS:
47c8cf91 4580 case RELOAD_FOR_INPADDR_ADDRESS:
546b63fb
RK
4581 /* Similar, except that we check only for this and subsequent inputs
4582 and the address of only subsequent inputs and we do not need
4583 to check for RELOAD_OTHER objects since they are known not to
4584 conflict. */
4585
4586 for (i = opnum; i < reload_n_operands; i++)
4587 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4588 return 0;
4589
4590 for (i = opnum + 1; i < reload_n_operands; i++)
47c8cf91
ILT
4591 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4592 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
546b63fb
RK
4593 return 0;
4594
4595 for (i = 0; i < reload_n_operands; i++)
4596 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4597 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4598 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4599 return 0;
4600
893bc853
RK
4601 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4602 return 0;
4603
2af88768
GK
4604 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4605 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4606 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
546b63fb 4607
32131a9c 4608 case RELOAD_FOR_INPUT:
546b63fb 4609 /* Similar to input address, except we start at the next operand for
05d10675 4610 both input and input address and we do not check for
546b63fb
RK
4611 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4612 would conflict. */
4613
4614 for (i = opnum + 1; i < reload_n_operands; i++)
4615 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
47c8cf91 4616 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
546b63fb
RK
4617 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4618 return 0;
4619
0f41302f 4620 /* ... fall through ... */
546b63fb 4621
32131a9c 4622 case RELOAD_FOR_OPERAND_ADDRESS:
546b63fb
RK
4623 /* Check outputs and their addresses. */
4624
4625 for (i = 0; i < reload_n_operands; i++)
4626 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4627 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
546b63fb
RK
4628 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4629 return 0;
4630
2af88768 4631 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
546b63fb 4632
893bc853
RK
4633 case RELOAD_FOR_OPADDR_ADDR:
4634 for (i = 0; i < reload_n_operands; i++)
4635 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
47c8cf91 4636 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
893bc853
RK
4637 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4638 return 0;
4639
2af88768
GK
4640 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4641 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4642 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
893bc853 4643
546b63fb 4644 case RELOAD_FOR_INSN:
893bc853 4645 /* These conflict with other outputs with RELOAD_OTHER. So
546b63fb
RK
4646 we need only check for output addresses. */
4647
d1d18b46 4648 opnum = reload_n_operands;
546b63fb 4649
0f41302f 4650 /* ... fall through ... */
546b63fb 4651
32131a9c 4652 case RELOAD_FOR_OUTPUT:
546b63fb 4653 case RELOAD_FOR_OUTPUT_ADDRESS:
47c8cf91 4654 case RELOAD_FOR_OUTADDR_ADDRESS:
546b63fb 4655 /* We already know these can't conflict with a later output. So the
d1d18b46
DJ
4656 only thing to check are later output addresses.
4657 Note that multiple output operands are emitted in reverse order,
4658 so the conflicting ones are those with lower indices. */
4659 for (i = 0; i < opnum; i++)
47c8cf91
ILT
4660 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4661 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
546b63fb
RK
4662 return 0;
4663
32131a9c
RK
4664 return 1;
4665 }
546b63fb 4666
32131a9c
RK
4667 abort ();
4668}
4669\f
351aa1c1
RK
4670/* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4671 Return 0 otherwise.
4672
4673 This function uses the same algorithm as reload_reg_free_p above. */
4674
f5963e61 4675int
351aa1c1
RK
4676reloads_conflict (r1, r2)
4677 int r1, r2;
4678{
eceef4c9
BS
4679 enum reload_type r1_type = rld[r1].when_needed;
4680 enum reload_type r2_type = rld[r2].when_needed;
4681 int r1_opnum = rld[r1].opnum;
4682 int r2_opnum = rld[r2].opnum;
351aa1c1 4683
2edc8d65
RK
4684 /* RELOAD_OTHER conflicts with everything. */
4685 if (r2_type == RELOAD_OTHER)
351aa1c1
RK
4686 return 1;
4687
4688 /* Otherwise, check conflicts differently for each type. */
4689
4690 switch (r1_type)
4691 {
4692 case RELOAD_FOR_INPUT:
05d10675 4693 return (r2_type == RELOAD_FOR_INSN
351aa1c1 4694 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
893bc853 4695 || r2_type == RELOAD_FOR_OPADDR_ADDR
351aa1c1 4696 || r2_type == RELOAD_FOR_INPUT
47c8cf91
ILT
4697 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4698 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4699 && r2_opnum > r1_opnum));
351aa1c1
RK
4700
4701 case RELOAD_FOR_INPUT_ADDRESS:
4702 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4703 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4704
47c8cf91
ILT
4705 case RELOAD_FOR_INPADDR_ADDRESS:
4706 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4707 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4708
351aa1c1
RK
4709 case RELOAD_FOR_OUTPUT_ADDRESS:
4710 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
d1d18b46 4711 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
351aa1c1 4712
47c8cf91
ILT
4713 case RELOAD_FOR_OUTADDR_ADDRESS:
4714 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
d1d18b46 4715 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
47c8cf91 4716
351aa1c1
RK
4717 case RELOAD_FOR_OPERAND_ADDRESS:
4718 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
a94ce333 4719 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
351aa1c1 4720
893bc853 4721 case RELOAD_FOR_OPADDR_ADDR:
05d10675 4722 return (r2_type == RELOAD_FOR_INPUT
a94ce333 4723 || r2_type == RELOAD_FOR_OPADDR_ADDR);
893bc853 4724
351aa1c1
RK
4725 case RELOAD_FOR_OUTPUT:
4726 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
47c8cf91
ILT
4727 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4728 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
d1d18b46 4729 && r2_opnum >= r1_opnum));
351aa1c1
RK
4730
4731 case RELOAD_FOR_INSN:
4732 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4733 || r2_type == RELOAD_FOR_INSN
4734 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4735
4736 case RELOAD_FOR_OTHER_ADDRESS:
4737 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4738
adab4fc5 4739 case RELOAD_OTHER:
2edc8d65 4740 return 1;
adab4fc5 4741
351aa1c1
RK
4742 default:
4743 abort ();
4744 }
4745}
4746\f
32131a9c
RK
4747/* Indexed by reload number, 1 if incoming value
4748 inherited from previous insns. */
4749char reload_inherited[MAX_RELOADS];
4750
4751/* For an inherited reload, this is the insn the reload was inherited from,
4752 if we know it. Otherwise, this is 0. */
4753rtx reload_inheritance_insn[MAX_RELOADS];
4754
40f03658 4755/* If nonzero, this is a place to get the value of the reload,
32131a9c
RK
4756 rather than using reload_in. */
4757rtx reload_override_in[MAX_RELOADS];
4758
e6e52be0
R
4759/* For each reload, the hard register number of the register used,
4760 or -1 if we did not need a register for this reload. */
32131a9c
RK
4761int reload_spill_index[MAX_RELOADS];
4762
304a22dd
R
4763/* Subroutine of free_for_value_p, used to check a single register.
4764 START_REGNO is the starting regno of the full reload register
4765 (possibly comprising multiple hard registers) that we are considering. */
f5470689 4766
6e684430 4767static int
304a22dd
R
4768reload_reg_free_for_value_p (start_regno, regno, opnum, type, value, out,
4769 reloadnum, ignore_address_reloads)
4770 int start_regno, regno;
6e684430
R
4771 int opnum;
4772 enum reload_type type;
f5470689
R
4773 rtx value, out;
4774 int reloadnum;
5828374f 4775 int ignore_address_reloads;
6e684430
R
4776{
4777 int time1;
09a308fe
R
4778 /* Set if we see an input reload that must not share its reload register
4779 with any new earlyclobber, but might otherwise share the reload
4780 register with an output or input-output reload. */
4781 int check_earlyclobber = 0;
6e684430 4782 int i;
dfe96118
R
4783 int copy = 0;
4784
9e3a9cf2 4785 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
dc8842bf
AH
4786 return 0;
4787
dfe96118
R
4788 if (out == const0_rtx)
4789 {
4790 copy = 1;
4791 out = NULL_RTX;
4792 }
6e684430
R
4793
4794 /* We use some pseudo 'time' value to check if the lifetimes of the
4795 new register use would overlap with the one of a previous reload
4796 that is not read-only or uses a different value.
4797 The 'time' used doesn't have to be linear in any shape or form, just
4798 monotonic.
4799 Some reload types use different 'buckets' for each operand.
4800 So there are MAX_RECOG_OPERANDS different time values for each
cecbf6e2
R
4801 such reload type.
4802 We compute TIME1 as the time when the register for the prospective
4803 new reload ceases to be live, and TIME2 for each existing
4804 reload as the time when that the reload register of that reload
4805 becomes live.
4806 Where there is little to be gained by exact lifetime calculations,
4807 we just make conservative assumptions, i.e. a longer lifetime;
4808 this is done in the 'default:' cases. */
6e684430
R
4809 switch (type)
4810 {
4811 case RELOAD_FOR_OTHER_ADDRESS:
203588e7 4812 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
c2b4b171 4813 time1 = copy ? 0 : 1;
6e684430 4814 break;
dfe96118
R
4815 case RELOAD_OTHER:
4816 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4817 break;
05d10675
BS
4818 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4819 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4820 respectively, to the time values for these, we get distinct time
4821 values. To get distinct time values for each operand, we have to
4822 multiply opnum by at least three. We round that up to four because
4823 multiply by four is often cheaper. */
6e684430 4824 case RELOAD_FOR_INPADDR_ADDRESS:
dfe96118 4825 time1 = opnum * 4 + 2;
6e684430
R
4826 break;
4827 case RELOAD_FOR_INPUT_ADDRESS:
dfe96118
R
4828 time1 = opnum * 4 + 3;
4829 break;
4830 case RELOAD_FOR_INPUT:
4831 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4832 executes (inclusive). */
4833 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
6e684430 4834 break;
cb2afeb3 4835 case RELOAD_FOR_OPADDR_ADDR:
05d10675
BS
4836 /* opnum * 4 + 4
4837 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
cb2afeb3
R
4838 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4839 break;
4840 case RELOAD_FOR_OPERAND_ADDRESS:
4841 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4842 is executed. */
dfe96118
R
4843 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4844 break;
4845 case RELOAD_FOR_OUTADDR_ADDRESS:
4846 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
6e684430 4847 break;
6e684430 4848 case RELOAD_FOR_OUTPUT_ADDRESS:
dfe96118 4849 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
6e684430
R
4850 break;
4851 default:
dfe96118 4852 time1 = MAX_RECOG_OPERANDS * 5 + 5;
6e684430
R
4853 }
4854
4855 for (i = 0; i < n_reloads; i++)
4856 {
eceef4c9 4857 rtx reg = rld[i].reg_rtx;
6e684430
R
4858 if (reg && GET_CODE (reg) == REG
4859 && ((unsigned) regno - true_regnum (reg)
f4f4d0f8 4860 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned) 1)
f5470689 4861 && i != reloadnum)
6e684430 4862 {
304a22dd
R
4863 rtx other_input = rld[i].in;
4864
4865 /* If the other reload loads the same input value, that
4866 will not cause a conflict only if it's loading it into
4867 the same register. */
4868 if (true_regnum (reg) != start_regno)
4869 other_input = NULL_RTX;
4870 if (! other_input || ! rtx_equal_p (other_input, value)
eceef4c9 4871 || rld[i].out || out)
6e684430 4872 {
09a308fe 4873 int time2;
eceef4c9 4874 switch (rld[i].when_needed)
f5470689
R
4875 {
4876 case RELOAD_FOR_OTHER_ADDRESS:
4877 time2 = 0;
4878 break;
4879 case RELOAD_FOR_INPADDR_ADDRESS:
cb2afeb3
R
4880 /* find_reloads makes sure that a
4881 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4882 by at most one - the first -
4883 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4884 address reload is inherited, the address address reload
4885 goes away, so we can ignore this conflict. */
dfe96118
R
4886 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4887 && ignore_address_reloads
4888 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4889 Then the address address is still needed to store
4890 back the new address. */
eceef4c9 4891 && ! rld[reloadnum].out)
cb2afeb3 4892 continue;
dfe96118
R
4893 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4894 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4895 reloads go away. */
eceef4c9 4896 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
dfe96118
R
4897 && ignore_address_reloads
4898 /* Unless we are reloading an auto_inc expression. */
eceef4c9 4899 && ! rld[reloadnum].out)
dfe96118 4900 continue;
eceef4c9 4901 time2 = rld[i].opnum * 4 + 2;
f5470689
R
4902 break;
4903 case RELOAD_FOR_INPUT_ADDRESS:
eceef4c9 4904 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
dfe96118 4905 && ignore_address_reloads
eceef4c9 4906 && ! rld[reloadnum].out)
dfe96118 4907 continue;
eceef4c9 4908 time2 = rld[i].opnum * 4 + 3;
f5470689
R
4909 break;
4910 case RELOAD_FOR_INPUT:
eceef4c9 4911 time2 = rld[i].opnum * 4 + 4;
09a308fe 4912 check_earlyclobber = 1;
f5470689 4913 break;
eceef4c9 4914 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
05d10675 4915 == MAX_RECOG_OPERAND * 4 */
cb2afeb3 4916 case RELOAD_FOR_OPADDR_ADDR:
dfe96118
R
4917 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4918 && ignore_address_reloads
eceef4c9 4919 && ! rld[reloadnum].out)
cb2afeb3 4920 continue;
dfe96118 4921 time2 = MAX_RECOG_OPERANDS * 4 + 1;
cb2afeb3
R
4922 break;
4923 case RELOAD_FOR_OPERAND_ADDRESS:
dfe96118 4924 time2 = MAX_RECOG_OPERANDS * 4 + 2;
09a308fe 4925 check_earlyclobber = 1;
dfe96118
R
4926 break;
4927 case RELOAD_FOR_INSN:
4928 time2 = MAX_RECOG_OPERANDS * 4 + 3;
cb2afeb3 4929 break;
f5470689 4930 case RELOAD_FOR_OUTPUT:
05d10675
BS
4931 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4932 instruction is executed. */
dfe96118 4933 time2 = MAX_RECOG_OPERANDS * 4 + 4;
f5470689 4934 break;
05d10675
BS
4935 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4936 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4937 value. */
cb2afeb3 4938 case RELOAD_FOR_OUTADDR_ADDRESS:
dfe96118
R
4939 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4940 && ignore_address_reloads
eceef4c9 4941 && ! rld[reloadnum].out)
cb2afeb3 4942 continue;
eceef4c9 4943 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
dfe96118 4944 break;
f5470689 4945 case RELOAD_FOR_OUTPUT_ADDRESS:
eceef4c9 4946 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
f5470689
R
4947 break;
4948 case RELOAD_OTHER:
dfe96118
R
4949 /* If there is no conflict in the input part, handle this
4950 like an output reload. */
304a22dd 4951 if (! rld[i].in || rtx_equal_p (other_input, value))
f5470689 4952 {
dfe96118 4953 time2 = MAX_RECOG_OPERANDS * 4 + 4;
57850c85 4954 /* Earlyclobbered outputs must conflict with inputs. */
09a308fe
R
4955 if (earlyclobber_operand_p (rld[i].out))
4956 time2 = MAX_RECOG_OPERANDS * 4 + 3;
1d7254c5 4957
f5470689
R
4958 break;
4959 }
dfe96118
R
4960 time2 = 1;
4961 /* RELOAD_OTHER might be live beyond instruction execution,
4962 but this is not obvious when we set time2 = 1. So check
4963 here if there might be a problem with the new reload
4964 clobbering the register used by the RELOAD_OTHER. */
4965 if (out)
4966 return 0;
4967 break;
f5470689 4968 default:
dfe96118 4969 return 0;
f5470689 4970 }
25963977 4971 if ((time1 >= time2
eceef4c9 4972 && (! rld[i].in || rld[i].out
304a22dd 4973 || ! rtx_equal_p (other_input, value)))
eceef4c9 4974 || (out && rld[reloadnum].out_reg
701d55e8 4975 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
f5470689 4976 return 0;
6e684430 4977 }
6e684430
R
4978 }
4979 }
09a308fe
R
4980
4981 /* Earlyclobbered outputs must conflict with inputs. */
4982 if (check_earlyclobber && out && earlyclobber_operand_p (out))
4983 return 0;
4984
6e684430
R
4985 return 1;
4986}
4987
c02cad8f
BS
4988/* Return 1 if the value in reload reg REGNO, as used by a reload
4989 needed for the part of the insn specified by OPNUM and TYPE,
4990 may be used to load VALUE into it.
4991
4992 MODE is the mode in which the register is used, this is needed to
4993 determine how many hard regs to test.
4994
4995 Other read-only reloads with the same value do not conflict
40f03658 4996 unless OUT is nonzero and these other reloads have to live while
c02cad8f
BS
4997 output reloads live.
4998 If OUT is CONST0_RTX, this is a special case: it means that the
4999 test should not be for using register REGNO as reload register, but
5000 for copying from register REGNO into the reload register.
5001
5002 RELOADNUM is the number of the reload we want to load this value for;
5003 a reload does not conflict with itself.
5004
5005 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5006 reloads that load an address for the very reload we are considering.
5007
5008 The caller has to make sure that there is no conflict with the return
5009 register. */
5010
5011static int
5012free_for_value_p (regno, mode, opnum, type, value, out, reloadnum,
5013 ignore_address_reloads)
5014 int regno;
5015 enum machine_mode mode;
5016 int opnum;
5017 enum reload_type type;
5018 rtx value, out;
5019 int reloadnum;
5020 int ignore_address_reloads;
5021{
5022 int nregs = HARD_REGNO_NREGS (regno, mode);
5023 while (nregs-- > 0)
304a22dd
R
5024 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5025 value, out, reloadnum,
5026 ignore_address_reloads))
c02cad8f
BS
5027 return 0;
5028 return 1;
5029}
5030
ff6534ad
BS
5031/* Determine whether the reload reg X overlaps any rtx'es used for
5032 overriding inheritance. Return nonzero if so. */
5033
5034static int
5035conflicts_with_override (x)
5036 rtx x;
5037{
5038 int i;
5039 for (i = 0; i < n_reloads; i++)
5040 if (reload_override_in[i]
5041 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5042 return 1;
5043 return 0;
5044}
5045\f
67e61fe7
BS
5046/* Give an error message saying we failed to find a reload for INSN,
5047 and clear out reload R. */
5048static void
5049failed_reload (insn, r)
5050 rtx insn;
5051 int r;
5052{
5053 if (asm_noperands (PATTERN (insn)) < 0)
5054 /* It's the compiler's fault. */
1f978f5f 5055 fatal_insn ("could not find a spill register", insn);
67e61fe7
BS
5056
5057 /* It's the user's fault; the operand's mode and constraint
5058 don't match. Disable this reload so we don't crash in final. */
5059 error_for_asm (insn,
5060 "`asm' operand constraint incompatible with operand size");
5061 rld[r].in = 0;
5062 rld[r].out = 0;
5063 rld[r].reg_rtx = 0;
5064 rld[r].optional = 1;
5065 rld[r].secondary_p = 1;
5066}
5067
5068/* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5069 for reload R. If it's valid, get an rtx for it. Return nonzero if
5070 successful. */
5071static int
5072set_reload_reg (i, r)
5073 int i, r;
5074{
5075 int regno;
5076 rtx reg = spill_reg_rtx[i];
5077
5078 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5079 spill_reg_rtx[i] = reg
5080 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5081
5082 regno = true_regnum (reg);
5083
5084 /* Detect when the reload reg can't hold the reload mode.
5085 This used to be one `if', but Sequent compiler can't handle that. */
5086 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5087 {
5088 enum machine_mode test_mode = VOIDmode;
5089 if (rld[r].in)
5090 test_mode = GET_MODE (rld[r].in);
5091 /* If rld[r].in has VOIDmode, it means we will load it
5092 in whatever mode the reload reg has: to wit, rld[r].mode.
5093 We have already tested that for validity. */
5094 /* Aside from that, we need to test that the expressions
5095 to reload from or into have modes which are valid for this
5096 reload register. Otherwise the reload insns would be invalid. */
5097 if (! (rld[r].in != 0 && test_mode != VOIDmode
5098 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5099 if (! (rld[r].out != 0
5100 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5101 {
5102 /* The reg is OK. */
5103 last_spill_reg = i;
5104
5105 /* Mark as in use for this insn the reload regs we use
5106 for this. */
5107 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5108 rld[r].when_needed, rld[r].mode);
5109
5110 rld[r].reg_rtx = reg;
5111 reload_spill_index[r] = spill_regs[i];
5112 return 1;
5113 }
5114 }
5115 return 0;
5116}
5117
32131a9c 5118/* Find a spill register to use as a reload register for reload R.
40f03658 5119 LAST_RELOAD is nonzero if this is the last reload for the insn being
32131a9c
RK
5120 processed.
5121
eceef4c9 5122 Set rld[R].reg_rtx to the register allocated.
32131a9c 5123
f5d8c9f4
BS
5124 We return 1 if successful, or 0 if we couldn't find a spill reg and
5125 we didn't change anything. */
32131a9c
RK
5126
5127static int
f5d8c9f4 5128allocate_reload_reg (chain, r, last_reload)
272df862 5129 struct insn_chain *chain ATTRIBUTE_UNUSED;
32131a9c 5130 int r;
32131a9c 5131 int last_reload;
32131a9c 5132{
67e61fe7 5133 int i, pass, count;
32131a9c
RK
5134
5135 /* If we put this reload ahead, thinking it is a group,
5136 then insist on finding a group. Otherwise we can grab a
a8fdc208 5137 reg that some other reload needs.
32131a9c
RK
5138 (That can happen when we have a 68000 DATA_OR_FP_REG
5139 which is a group of data regs or one fp reg.)
5140 We need not be so restrictive if there are no more reloads
5141 for this insn.
5142
5143 ??? Really it would be nicer to have smarter handling
5144 for that kind of reg class, where a problem like this is normal.
5145 Perhaps those classes should be avoided for reloading
5146 by use of more alternatives. */
5147
8ec450a4 5148 int force_group = rld[r].nregs > 1 && ! last_reload;
32131a9c
RK
5149
5150 /* If we want a single register and haven't yet found one,
5151 take any reg in the right class and not in use.
5152 If we want a consecutive group, here is where we look for it.
5153
5154 We use two passes so we can first look for reload regs to
5155 reuse, which are already in use for other reloads in this insn,
5156 and only then use additional registers.
5157 I think that maximizing reuse is needed to make sure we don't
5158 run out of reload regs. Suppose we have three reloads, and
5159 reloads A and B can share regs. These need two regs.
5160 Suppose A and B are given different regs.
5161 That leaves none for C. */
5162 for (pass = 0; pass < 2; pass++)
5163 {
5164 /* I is the index in spill_regs.
5165 We advance it round-robin between insns to use all spill regs
5166 equally, so that inherited reloads have a chance
f5d8c9f4
BS
5167 of leapfrogging each other. */
5168
5169 i = last_spill_reg;
05d10675 5170
a5339699 5171 for (count = 0; count < n_spills; count++)
32131a9c 5172 {
eceef4c9 5173 int class = (int) rld[r].class;
03acd8f8 5174 int regnum;
32131a9c 5175
03acd8f8
BS
5176 i++;
5177 if (i >= n_spills)
5178 i -= n_spills;
5179 regnum = spill_regs[i];
32131a9c 5180
eceef4c9
BS
5181 if ((reload_reg_free_p (regnum, rld[r].opnum,
5182 rld[r].when_needed)
5183 || (rld[r].in
05d10675
BS
5184 /* We check reload_reg_used to make sure we
5185 don't clobber the return register. */
03acd8f8 5186 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
c02cad8f
BS
5187 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5188 rld[r].when_needed, rld[r].in,
5189 rld[r].out, r, 1)))
03acd8f8 5190 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
8ec450a4 5191 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
be7ae2a4
RK
5192 /* Look first for regs to share, then for unshared. But
5193 don't share regs used for inherited reloads; they are
5194 the ones we want to preserve. */
5195 && (pass
5196 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
03acd8f8 5197 regnum)
be7ae2a4 5198 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
03acd8f8 5199 regnum))))
32131a9c 5200 {
8ec450a4 5201 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
32131a9c
RK
5202 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5203 (on 68000) got us two FP regs. If NR is 1,
5204 we would reject both of them. */
5205 if (force_group)
67e61fe7 5206 nr = rld[r].nregs;
32131a9c
RK
5207 /* If we need only one reg, we have already won. */
5208 if (nr == 1)
5209 {
5210 /* But reject a single reg if we demand a group. */
5211 if (force_group)
5212 continue;
5213 break;
5214 }
5215 /* Otherwise check that as many consecutive regs as we need
f5d8c9f4
BS
5216 are available here. */
5217 while (nr > 1)
5218 {
5219 int regno = regnum + nr - 1;
5220 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5221 && spill_reg_order[regno] >= 0
5222 && reload_reg_free_p (regno, rld[r].opnum,
5223 rld[r].when_needed)))
5224 break;
5225 nr--;
5226 }
32131a9c
RK
5227 if (nr == 1)
5228 break;
5229 }
5230 }
5231
5232 /* If we found something on pass 1, omit pass 2. */
5233 if (count < n_spills)
5234 break;
5235 }
1d7254c5 5236
32131a9c 5237 /* We should have found a spill register by now. */
f5d8c9f4 5238 if (count >= n_spills)
32131a9c
RK
5239 return 0;
5240
f5d8c9f4
BS
5241 /* I is the index in SPILL_REG_RTX of the reload register we are to
5242 allocate. Get an rtx for it and find its register number. */
32131a9c 5243
f5d8c9f4 5244 return set_reload_reg (i, r);
32131a9c
RK
5245}
5246\f
67e61fe7
BS
5247/* Initialize all the tables needed to allocate reload registers.
5248 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5249 is the array we use to restore the reg_rtx field for every reload. */
efc9bd41 5250
32131a9c 5251static void
67e61fe7 5252choose_reload_regs_init (chain, save_reload_reg_rtx)
7609e720 5253 struct insn_chain *chain;
67e61fe7 5254 rtx *save_reload_reg_rtx;
32131a9c 5255{
67e61fe7 5256 int i;
32131a9c 5257
67e61fe7
BS
5258 for (i = 0; i < n_reloads; i++)
5259 rld[i].reg_rtx = save_reload_reg_rtx[i];
32131a9c 5260
961192e1
JM
5261 memset (reload_inherited, 0, MAX_RELOADS);
5262 memset ((char *) reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5263 memset ((char *) reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
32131a9c
RK
5264
5265 CLEAR_HARD_REG_SET (reload_reg_used);
5266 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
32131a9c 5267 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
893bc853 5268 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
546b63fb
RK
5269 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5270 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
32131a9c 5271
f1db3576
JL
5272 CLEAR_HARD_REG_SET (reg_used_in_insn);
5273 {
5274 HARD_REG_SET tmp;
239a0f5b 5275 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
f1db3576 5276 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
239a0f5b 5277 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
f1db3576 5278 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
239a0f5b
BS
5279 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5280 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
f1db3576 5281 }
efc9bd41 5282
546b63fb
RK
5283 for (i = 0; i < reload_n_operands; i++)
5284 {
5285 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5286 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5287 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
47c8cf91 5288 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
546b63fb 5289 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
47c8cf91 5290 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
546b63fb 5291 }
32131a9c 5292
9e3a9cf2 5293 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
05d10675 5294
67e61fe7 5295 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
32131a9c 5296
67e61fe7
BS
5297 for (i = 0; i < n_reloads; i++)
5298 /* If we have already decided to use a certain register,
5299 don't use it in another way. */
5300 if (rld[i].reg_rtx)
5301 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5302 rld[i].when_needed, rld[i].mode);
5303}
32131a9c 5304
67e61fe7
BS
5305/* Assign hard reg targets for the pseudo-registers we must reload
5306 into hard regs for this insn.
5307 Also output the instructions to copy them in and out of the hard regs.
5308
5309 For machines with register classes, we are responsible for
5310 finding a reload reg in the proper class. */
5311
5312static void
5313choose_reload_regs (chain)
5314 struct insn_chain *chain;
5315{
5316 rtx insn = chain->insn;
b3694847 5317 int i, j;
770ae6cc 5318 unsigned int max_group_size = 1;
67e61fe7 5319 enum reg_class group_class = NO_REGS;
f5d8c9f4 5320 int pass, win, inheritance;
67e61fe7
BS
5321
5322 rtx save_reload_reg_rtx[MAX_RELOADS];
32131a9c 5323
32131a9c
RK
5324 /* In order to be certain of getting the registers we need,
5325 we must sort the reloads into order of increasing register class.
5326 Then our grabbing of reload registers will parallel the process
a8fdc208 5327 that provided the reload registers.
32131a9c
RK
5328
5329 Also note whether any of the reloads wants a consecutive group of regs.
5330 If so, record the maximum size of the group desired and what
5331 register class contains all the groups needed by this insn. */
5332
5333 for (j = 0; j < n_reloads; j++)
5334 {
5335 reload_order[j] = j;
5336 reload_spill_index[j] = -1;
5337
8ec450a4 5338 if (rld[j].nregs > 1)
32131a9c 5339 {
8ec450a4 5340 max_group_size = MAX (rld[j].nregs, max_group_size);
770ae6cc 5341 group_class
8e2e89f7 5342 = reg_class_superunion[(int) rld[j].class][(int) group_class];
32131a9c
RK
5343 }
5344
eceef4c9 5345 save_reload_reg_rtx[j] = rld[j].reg_rtx;
32131a9c
RK
5346 }
5347
5348 if (n_reloads > 1)
5349 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5350
58b1581b
RS
5351 /* If -O, try first with inheritance, then turning it off.
5352 If not -O, don't do inheritance.
5353 Using inheritance when not optimizing leads to paradoxes
5354 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5355 because one side of the comparison might be inherited. */
f5d8c9f4 5356 win = 0;
58b1581b 5357 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
32131a9c 5358 {
67e61fe7
BS
5359 choose_reload_regs_init (chain, save_reload_reg_rtx);
5360
32131a9c
RK
5361 /* Process the reloads in order of preference just found.
5362 Beyond this point, subregs can be found in reload_reg_rtx.
5363
770ae6cc
RK
5364 This used to look for an existing reloaded home for all of the
5365 reloads, and only then perform any new reloads. But that could lose
5366 if the reloads were done out of reg-class order because a later
5367 reload with a looser constraint might have an old home in a register
5368 needed by an earlier reload with a tighter constraint.
32131a9c
RK
5369
5370 To solve this, we make two passes over the reloads, in the order
5371 described above. In the first pass we try to inherit a reload
5372 from a previous insn. If there is a later reload that needs a
5373 class that is a proper subset of the class being processed, we must
5374 also allocate a spill register during the first pass.
5375
5376 Then make a second pass over the reloads to allocate any reloads
5377 that haven't been given registers yet. */
5378
5379 for (j = 0; j < n_reloads; j++)
5380 {
b3694847 5381 int r = reload_order[j];
8593b745 5382 rtx search_equiv = NULL_RTX;
32131a9c
RK
5383
5384 /* Ignore reloads that got marked inoperative. */
eceef4c9
BS
5385 if (rld[r].out == 0 && rld[r].in == 0
5386 && ! rld[r].secondary_p)
32131a9c
RK
5387 continue;
5388
b29514ee 5389 /* If find_reloads chose to use reload_in or reload_out as a reload
b080c137
RK
5390 register, we don't need to chose one. Otherwise, try even if it
5391 found one since we might save an insn if we find the value lying
b29514ee
R
5392 around.
5393 Try also when reload_in is a pseudo without a hard reg. */
eceef4c9
BS
5394 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5395 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5396 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5397 && GET_CODE (rld[r].in) != MEM
5398 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
32131a9c
RK
5399 continue;
5400
5401#if 0 /* No longer needed for correct operation.
5402 It might give better code, or might not; worth an experiment? */
5403 /* If this is an optional reload, we can't inherit from earlier insns
5404 until we are sure that any non-optional reloads have been allocated.
5405 The following code takes advantage of the fact that optional reloads
5406 are at the end of reload_order. */
eceef4c9 5407 if (rld[r].optional != 0)
32131a9c 5408 for (i = 0; i < j; i++)
eceef4c9
BS
5409 if ((rld[reload_order[i]].out != 0
5410 || rld[reload_order[i]].in != 0
5411 || rld[reload_order[i]].secondary_p)
5412 && ! rld[reload_order[i]].optional
5413 && rld[reload_order[i]].reg_rtx == 0)
f5d8c9f4 5414 allocate_reload_reg (chain, reload_order[i], 0);
32131a9c
RK
5415#endif
5416
5417 /* First see if this pseudo is already available as reloaded
5418 for a previous insn. We cannot try to inherit for reloads
5419 that are smaller than the maximum number of registers needed
5420 for groups unless the register we would allocate cannot be used
5421 for the groups.
5422
5423 We could check here to see if this is a secondary reload for
5424 an object that is already in a register of the desired class.
5425 This would avoid the need for the secondary reload register.
5426 But this is complex because we can't easily determine what
b080c137
RK
5427 objects might want to be loaded via this reload. So let a
5428 register be allocated here. In `emit_reload_insns' we suppress
5429 one of the loads in the case described above. */
32131a9c
RK
5430
5431 if (inheritance)
5432 {
ddef6bc7 5433 int byte = 0;
b3694847 5434 int regno = -1;
6a651371 5435 enum machine_mode mode = VOIDmode;
32131a9c 5436
eceef4c9 5437 if (rld[r].in == 0)
32131a9c 5438 ;
eceef4c9 5439 else if (GET_CODE (rld[r].in) == REG)
db660765 5440 {
eceef4c9
BS
5441 regno = REGNO (rld[r].in);
5442 mode = GET_MODE (rld[r].in);
db660765 5443 }
eceef4c9 5444 else if (GET_CODE (rld[r].in_reg) == REG)
db660765 5445 {
eceef4c9
BS
5446 regno = REGNO (rld[r].in_reg);
5447 mode = GET_MODE (rld[r].in_reg);
db660765 5448 }
eceef4c9
BS
5449 else if (GET_CODE (rld[r].in_reg) == SUBREG
5450 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
b60a8416 5451 {
ddef6bc7 5452 byte = SUBREG_BYTE (rld[r].in_reg);
eceef4c9 5453 regno = REGNO (SUBREG_REG (rld[r].in_reg));
cb2afeb3 5454 if (regno < FIRST_PSEUDO_REGISTER)
ddef6bc7 5455 regno = subreg_regno (rld[r].in_reg);
eceef4c9 5456 mode = GET_MODE (rld[r].in_reg);
cb2afeb3
R
5457 }
5458#ifdef AUTO_INC_DEC
eceef4c9
BS
5459 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5460 || GET_CODE (rld[r].in_reg) == PRE_DEC
5461 || GET_CODE (rld[r].in_reg) == POST_INC
5462 || GET_CODE (rld[r].in_reg) == POST_DEC)
5463 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
cb2afeb3 5464 {
eceef4c9
BS
5465 regno = REGNO (XEXP (rld[r].in_reg, 0));
5466 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5467 rld[r].out = rld[r].in;
b60a8416 5468 }
cb2afeb3 5469#endif
32131a9c
RK
5470#if 0
5471 /* This won't work, since REGNO can be a pseudo reg number.
5472 Also, it takes much more hair to keep track of all the things
5473 that can invalidate an inherited reload of part of a pseudoreg. */
eceef4c9
BS
5474 else if (GET_CODE (rld[r].in) == SUBREG
5475 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
ddef6bc7 5476 regno = subreg_regno (rld[r].in);
32131a9c
RK
5477#endif
5478
5479 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5480 {
eceef4c9 5481 enum reg_class class = rld[r].class, last_class;
cb2afeb3 5482 rtx last_reg = reg_last_reload_reg[regno];
02188693 5483 enum machine_mode need_mode;
05d10675 5484
ddef6bc7
JJ
5485 i = REGNO (last_reg);
5486 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
cb2afeb3 5487 last_class = REGNO_REG_CLASS (i);
02188693 5488
ddef6bc7 5489 if (byte == 0)
ce701d1b
BS
5490 need_mode = mode;
5491 else
5492 need_mode
ddef6bc7 5493 = smallest_mode_for_size (GET_MODE_SIZE (mode) + byte,
ce701d1b 5494 GET_MODE_CLASS (mode));
02188693 5495
c9d8a813 5496 if (
02188693 5497#ifdef CLASS_CANNOT_CHANGE_MODE
c9d8a813 5498 (TEST_HARD_REG_BIT
02188693 5499 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE], i)
1d7254c5 5500 ? ! CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (last_reg),
02188693 5501 need_mode)
c9d8a813 5502 : (GET_MODE_SIZE (GET_MODE (last_reg))
02188693 5503 >= GET_MODE_SIZE (need_mode)))
c9d8a813
RH
5504#else
5505 (GET_MODE_SIZE (GET_MODE (last_reg))
02188693 5506 >= GET_MODE_SIZE (need_mode))
c9d8a813 5507#endif
cb2afeb3 5508 && reg_reloaded_contents[i] == regno
e6e52be0 5509 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
8ec450a4 5510 && HARD_REGNO_MODE_OK (i, rld[r].mode)
cb2afeb3
R
5511 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5512 /* Even if we can't use this register as a reload
5513 register, we might use it for reload_override_in,
5514 if copying it to the desired class is cheap
5515 enough. */
e56b4594 5516 || ((REGISTER_MOVE_COST (mode, last_class, class)
cb2afeb3
R
5517 < MEMORY_MOVE_COST (mode, class, 1))
5518#ifdef SECONDARY_INPUT_RELOAD_CLASS
5519 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5520 last_reg)
5521 == NO_REGS)
5522#endif
5523#ifdef SECONDARY_MEMORY_NEEDED
5524 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5525 mode)
5526#endif
5527 ))
5528
8ec450a4 5529 && (rld[r].nregs == max_group_size
32131a9c 5530 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
e6e52be0 5531 i))
c02cad8f
BS
5532 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5533 rld[r].when_needed, rld[r].in,
5534 const0_rtx, r, 1))
32131a9c
RK
5535 {
5536 /* If a group is needed, verify that all the subsequent
0f41302f 5537 registers still have their values intact. */
1d7254c5 5538 int nr = HARD_REGNO_NREGS (i, rld[r].mode);
32131a9c
RK
5539 int k;
5540
5541 for (k = 1; k < nr; k++)
e6e52be0
R
5542 if (reg_reloaded_contents[i + k] != regno
5543 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
32131a9c
RK
5544 break;
5545
5546 if (k == nr)
5547 {
c74fa651 5548 int i1;
eb4d554e 5549 int bad_for_class;
c74fa651 5550
cb2afeb3
R
5551 last_reg = (GET_MODE (last_reg) == mode
5552 ? last_reg : gen_rtx_REG (mode, i));
5553
eb4d554e
GK
5554 bad_for_class = 0;
5555 for (k = 0; k < nr; k++)
5556 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5557 i+k);
5558
c74fa651
RS
5559 /* We found a register that contains the
5560 value we need. If this register is the
5561 same as an `earlyclobber' operand of the
5562 current insn, just mark it as a place to
5563 reload from since we can't use it as the
5564 reload register itself. */
5565
5566 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5567 if (reg_overlap_mentioned_for_reload_p
5568 (reg_last_reload_reg[regno],
5569 reload_earlyclobbers[i1]))
5570 break;
5571
8908158d 5572 if (i1 != n_earlyclobbers
c02cad8f
BS
5573 || ! (free_for_value_p (i, rld[r].mode,
5574 rld[r].opnum,
5575 rld[r].when_needed, rld[r].in,
5576 rld[r].out, r, 1))
e6e52be0 5577 /* Don't use it if we'd clobber a pseudo reg. */
f1db3576 5578 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
eceef4c9 5579 && rld[r].out
e6e52be0 5580 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
0c7f2259 5581 /* Don't clobber the frame pointer. */
1d7254c5 5582 || (i == HARD_FRAME_POINTER_REGNUM
2f460a0a 5583 && frame_pointer_needed
1d7254c5 5584 && rld[r].out)
8908158d
RS
5585 /* Don't really use the inherited spill reg
5586 if we need it wider than we've got it. */
8ec450a4 5587 || (GET_MODE_SIZE (rld[r].mode)
b29514ee 5588 > GET_MODE_SIZE (mode))
eb4d554e 5589 || bad_for_class
cb2afeb3 5590
b29514ee
R
5591 /* If find_reloads chose reload_out as reload
5592 register, stay with it - that leaves the
5593 inherited register for subsequent reloads. */
eceef4c9 5594 || (rld[r].out && rld[r].reg_rtx
67e61fe7 5595 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
cb2afeb3 5596 {
4c3a2649
BS
5597 if (! rld[r].optional)
5598 {
5599 reload_override_in[r] = last_reg;
5600 reload_inheritance_insn[r]
5601 = reg_reloaded_insn[i];
5602 }
cb2afeb3 5603 }
c74fa651
RS
5604 else
5605 {
54c40e68 5606 int k;
c74fa651
RS
5607 /* We can use this as a reload reg. */
5608 /* Mark the register as in use for this part of
5609 the insn. */
e6e52be0 5610 mark_reload_reg_in_use (i,
eceef4c9
BS
5611 rld[r].opnum,
5612 rld[r].when_needed,
8ec450a4 5613 rld[r].mode);
eceef4c9 5614 rld[r].reg_rtx = last_reg;
c74fa651
RS
5615 reload_inherited[r] = 1;
5616 reload_inheritance_insn[r]
5617 = reg_reloaded_insn[i];
5618 reload_spill_index[r] = i;
54c40e68
RS
5619 for (k = 0; k < nr; k++)
5620 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
e6e52be0 5621 i + k);
c74fa651 5622 }
32131a9c
RK
5623 }
5624 }
5625 }
5626 }
5627
5628 /* Here's another way to see if the value is already lying around. */
5629 if (inheritance
eceef4c9 5630 && rld[r].in != 0
32131a9c 5631 && ! reload_inherited[r]
eceef4c9
BS
5632 && rld[r].out == 0
5633 && (CONSTANT_P (rld[r].in)
5634 || GET_CODE (rld[r].in) == PLUS
5635 || GET_CODE (rld[r].in) == REG
5636 || GET_CODE (rld[r].in) == MEM)
8ec450a4 5637 && (rld[r].nregs == max_group_size
eceef4c9
BS
5638 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5639 search_equiv = rld[r].in;
8593b745
R
5640 /* If this is an output reload from a simple move insn, look
5641 if an equivalence for the input is available. */
eceef4c9 5642 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
8593b745
R
5643 {
5644 rtx set = single_set (insn);
5645
5646 if (set
eceef4c9 5647 && rtx_equal_p (rld[r].out, SET_DEST (set))
8593b745
R
5648 && CONSTANT_P (SET_SRC (set)))
5649 search_equiv = SET_SRC (set);
5650 }
5651
5652 if (search_equiv)
32131a9c 5653 {
b3694847 5654 rtx equiv
eceef4c9 5655 = find_equiv_reg (search_equiv, insn, rld[r].class,
9714cf43 5656 -1, NULL, 0, rld[r].mode);
f428f252 5657 int regno = 0;
32131a9c
RK
5658
5659 if (equiv != 0)
5660 {
5661 if (GET_CODE (equiv) == REG)
5662 regno = REGNO (equiv);
5663 else if (GET_CODE (equiv) == SUBREG)
5664 {
f8a9e02b
RK
5665 /* This must be a SUBREG of a hard register.
5666 Make a new REG since this might be used in an
5667 address and not all machines support SUBREGs
5668 there. */
ddef6bc7 5669 regno = subreg_regno (equiv);
8ec450a4 5670 equiv = gen_rtx_REG (rld[r].mode, regno);
32131a9c
RK
5671 }
5672 else
5673 abort ();
5674 }
5675
5676 /* If we found a spill reg, reject it unless it is free
5677 and of the desired class. */
5678 if (equiv != 0
cb2afeb3 5679 && ((TEST_HARD_REG_BIT (reload_reg_used_at_all, regno)
c02cad8f
BS
5680 && ! free_for_value_p (regno, rld[r].mode,
5681 rld[r].opnum, rld[r].when_needed,
5682 rld[r].in, rld[r].out, r, 1))
eceef4c9 5683 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
32131a9c
RK
5684 regno)))
5685 equiv = 0;
5686
8ec450a4 5687 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
32131a9c
RK
5688 equiv = 0;
5689
5690 /* We found a register that contains the value we need.
5691 If this register is the same as an `earlyclobber' operand
5692 of the current insn, just mark it as a place to reload from
5693 since we can't use it as the reload register itself. */
5694
5695 if (equiv != 0)
5696 for (i = 0; i < n_earlyclobbers; i++)
bfa30b22
RK
5697 if (reg_overlap_mentioned_for_reload_p (equiv,
5698 reload_earlyclobbers[i]))
32131a9c 5699 {
4c3a2649
BS
5700 if (! rld[r].optional)
5701 reload_override_in[r] = equiv;
32131a9c
RK
5702 equiv = 0;
5703 break;
5704 }
5705
3c785e47
R
5706 /* If the equiv register we have found is explicitly clobbered
5707 in the current insn, it depends on the reload type if we
5708 can use it, use it for reload_override_in, or not at all.
5709 In particular, we then can't use EQUIV for a
5710 RELOAD_FOR_OUTPUT_ADDRESS reload. */
32131a9c 5711
9532e31f 5712 if (equiv != 0)
174fa2c4 5713 {
9532e31f
BS
5714 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5715 switch (rld[r].when_needed)
5716 {
5717 case RELOAD_FOR_OTHER_ADDRESS:
5718 case RELOAD_FOR_INPADDR_ADDRESS:
5719 case RELOAD_FOR_INPUT_ADDRESS:
5720 case RELOAD_FOR_OPADDR_ADDR:
5721 break;
5722 case RELOAD_OTHER:
5723 case RELOAD_FOR_INPUT:
5724 case RELOAD_FOR_OPERAND_ADDRESS:
5725 if (! rld[r].optional)
5726 reload_override_in[r] = equiv;
5727 /* Fall through. */
5728 default:
5729 equiv = 0;
5730 break;
5731 }
5732 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5733 switch (rld[r].when_needed)
5734 {
5735 case RELOAD_FOR_OTHER_ADDRESS:
5736 case RELOAD_FOR_INPADDR_ADDRESS:
5737 case RELOAD_FOR_INPUT_ADDRESS:
5738 case RELOAD_FOR_OPADDR_ADDR:
5739 case RELOAD_FOR_OPERAND_ADDRESS:
5740 case RELOAD_FOR_INPUT:
5741 break;
5742 case RELOAD_OTHER:
5743 if (! rld[r].optional)
5744 reload_override_in[r] = equiv;
5745 /* Fall through. */
5746 default:
5747 equiv = 0;
5748 break;
5749 }
32131a9c
RK
5750 }
5751
5752 /* If we found an equivalent reg, say no code need be generated
5753 to load it, and use it as our reload reg. */
a6a2274a
KH
5754 if (equiv != 0
5755 && (regno != HARD_FRAME_POINTER_REGNUM
2f460a0a 5756 || !frame_pointer_needed))
32131a9c 5757 {
8ec450a4 5758 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
100338df 5759 int k;
eceef4c9 5760 rld[r].reg_rtx = equiv;
32131a9c 5761 reload_inherited[r] = 1;
100338df 5762
91d7e7ac
R
5763 /* If reg_reloaded_valid is not set for this register,
5764 there might be a stale spill_reg_store lying around.
5765 We must clear it, since otherwise emit_reload_insns
5766 might delete the store. */
5767 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5768 spill_reg_store[regno] = NULL_RTX;
100338df
JL
5769 /* If any of the hard registers in EQUIV are spill
5770 registers, mark them as in use for this insn. */
5771 for (k = 0; k < nr; k++)
be7ae2a4 5772 {
100338df
JL
5773 i = spill_reg_order[regno + k];
5774 if (i >= 0)
5775 {
eceef4c9
BS
5776 mark_reload_reg_in_use (regno, rld[r].opnum,
5777 rld[r].when_needed,
8ec450a4 5778 rld[r].mode);
100338df
JL
5779 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5780 regno + k);
5781 }
be7ae2a4 5782 }
32131a9c
RK
5783 }
5784 }
5785
5786 /* If we found a register to use already, or if this is an optional
5787 reload, we are done. */
eceef4c9 5788 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
32131a9c
RK
5789 continue;
5790
1d7254c5
KH
5791#if 0
5792 /* No longer needed for correct operation. Might or might
5793 not give better code on the average. Want to experiment? */
32131a9c
RK
5794
5795 /* See if there is a later reload that has a class different from our
5796 class that intersects our class or that requires less register
5797 than our reload. If so, we must allocate a register to this
5798 reload now, since that reload might inherit a previous reload
5799 and take the only available register in our class. Don't do this
5800 for optional reloads since they will force all previous reloads
5801 to be allocated. Also don't do this for reloads that have been
5802 turned off. */
5803
5804 for (i = j + 1; i < n_reloads; i++)
5805 {
5806 int s = reload_order[i];
5807
eceef4c9
BS
5808 if ((rld[s].in == 0 && rld[s].out == 0
5809 && ! rld[s].secondary_p)
5810 || rld[s].optional)
32131a9c
RK
5811 continue;
5812
eceef4c9
BS
5813 if ((rld[s].class != rld[r].class
5814 && reg_classes_intersect_p (rld[r].class,
5815 rld[s].class))
8ec450a4 5816 || rld[s].nregs < rld[r].nregs)
05d10675 5817 break;
32131a9c
RK
5818 }
5819
5820 if (i == n_reloads)
5821 continue;
5822
f5d8c9f4 5823 allocate_reload_reg (chain, r, j == n_reloads - 1);
32131a9c
RK
5824#endif
5825 }
5826
5827 /* Now allocate reload registers for anything non-optional that
5828 didn't get one yet. */
5829 for (j = 0; j < n_reloads; j++)
5830 {
b3694847 5831 int r = reload_order[j];
32131a9c
RK
5832
5833 /* Ignore reloads that got marked inoperative. */
eceef4c9 5834 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
32131a9c
RK
5835 continue;
5836
5837 /* Skip reloads that already have a register allocated or are
0f41302f 5838 optional. */
eceef4c9 5839 if (rld[r].reg_rtx != 0 || rld[r].optional)
32131a9c
RK
5840 continue;
5841
f5d8c9f4 5842 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
32131a9c
RK
5843 break;
5844 }
5845
5846 /* If that loop got all the way, we have won. */
5847 if (j == n_reloads)
f5d8c9f4
BS
5848 {
5849 win = 1;
5850 break;
5851 }
32131a9c 5852
32131a9c 5853 /* Loop around and try without any inheritance. */
32131a9c
RK
5854 }
5855
f5d8c9f4
BS
5856 if (! win)
5857 {
5858 /* First undo everything done by the failed attempt
5859 to allocate with inheritance. */
5860 choose_reload_regs_init (chain, save_reload_reg_rtx);
5861
5862 /* Some sanity tests to verify that the reloads found in the first
5863 pass are identical to the ones we have now. */
5864 if (chain->n_reloads != n_reloads)
5865 abort ();
5866
5867 for (i = 0; i < n_reloads; i++)
5868 {
5869 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5870 continue;
5871 if (chain->rld[i].when_needed != rld[i].when_needed)
5872 abort ();
5873 for (j = 0; j < n_spills; j++)
5874 if (spill_regs[j] == chain->rld[i].regno)
5875 if (! set_reload_reg (j, i))
5876 failed_reload (chain->insn, i);
5877 }
5878 }
5879
32131a9c
RK
5880 /* If we thought we could inherit a reload, because it seemed that
5881 nothing else wanted the same reload register earlier in the insn,
cb2afeb3
R
5882 verify that assumption, now that all reloads have been assigned.
5883 Likewise for reloads where reload_override_in has been set. */
32131a9c 5884
cb2afeb3
R
5885 /* If doing expensive optimizations, do one preliminary pass that doesn't
5886 cancel any inheritance, but removes reloads that have been needed only
5887 for reloads that we know can be inherited. */
5888 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
32131a9c 5889 {
cb2afeb3 5890 for (j = 0; j < n_reloads; j++)
029b38ff 5891 {
b3694847 5892 int r = reload_order[j];
cb2afeb3 5893 rtx check_reg;
eceef4c9
BS
5894 if (reload_inherited[r] && rld[r].reg_rtx)
5895 check_reg = rld[r].reg_rtx;
cb2afeb3
R
5896 else if (reload_override_in[r]
5897 && (GET_CODE (reload_override_in[r]) == REG
05d10675 5898 || GET_CODE (reload_override_in[r]) == SUBREG))
cb2afeb3
R
5899 check_reg = reload_override_in[r];
5900 else
5901 continue;
c02cad8f
BS
5902 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5903 rld[r].opnum, rld[r].when_needed, rld[r].in,
5904 (reload_inherited[r]
5905 ? rld[r].out : const0_rtx),
5906 r, 1))
029b38ff 5907 {
cb2afeb3
R
5908 if (pass)
5909 continue;
5910 reload_inherited[r] = 0;
5911 reload_override_in[r] = 0;
029b38ff 5912 }
cb2afeb3
R
5913 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5914 reload_override_in, then we do not need its related
5915 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5916 likewise for other reload types.
5917 We handle this by removing a reload when its only replacement
5918 is mentioned in reload_in of the reload we are going to inherit.
5919 A special case are auto_inc expressions; even if the input is
5920 inherited, we still need the address for the output. We can
fe92fe26 5921 recognize them because they have RELOAD_OUT set to RELOAD_IN.
eaec9b3d 5922 If we succeeded removing some reload and we are doing a preliminary
cb2afeb3
R
5923 pass just to remove such reloads, make another pass, since the
5924 removal of one reload might allow us to inherit another one. */
eceef4c9
BS
5925 else if (rld[r].in
5926 && rld[r].out != rld[r].in
5927 && remove_address_replacements (rld[r].in) && pass)
cb2afeb3 5928 pass = 2;
32131a9c
RK
5929 }
5930 }
5931
5932 /* Now that reload_override_in is known valid,
5933 actually override reload_in. */
5934 for (j = 0; j < n_reloads; j++)
5935 if (reload_override_in[j])
eceef4c9 5936 rld[j].in = reload_override_in[j];
32131a9c
RK
5937
5938 /* If this reload won't be done because it has been cancelled or is
5939 optional and not inherited, clear reload_reg_rtx so other
5940 routines (such as subst_reloads) don't get confused. */
5941 for (j = 0; j < n_reloads; j++)
eceef4c9
BS
5942 if (rld[j].reg_rtx != 0
5943 && ((rld[j].optional && ! reload_inherited[j])
5944 || (rld[j].in == 0 && rld[j].out == 0
5945 && ! rld[j].secondary_p)))
be7ae2a4 5946 {
eceef4c9 5947 int regno = true_regnum (rld[j].reg_rtx);
be7ae2a4
RK
5948
5949 if (spill_reg_order[regno] >= 0)
eceef4c9 5950 clear_reload_reg_in_use (regno, rld[j].opnum,
8ec450a4 5951 rld[j].when_needed, rld[j].mode);
eceef4c9 5952 rld[j].reg_rtx = 0;
c0029be5 5953 reload_spill_index[j] = -1;
be7ae2a4 5954 }
32131a9c
RK
5955
5956 /* Record which pseudos and which spill regs have output reloads. */
5957 for (j = 0; j < n_reloads; j++)
5958 {
b3694847 5959 int r = reload_order[j];
32131a9c
RK
5960
5961 i = reload_spill_index[r];
5962
e6e52be0 5963 /* I is nonneg if this reload uses a register.
eceef4c9 5964 If rld[r].reg_rtx is 0, this is an optional reload
32131a9c 5965 that we opted to ignore. */
eceef4c9
BS
5966 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
5967 && rld[r].reg_rtx != 0)
32131a9c 5968 {
b3694847 5969 int nregno = REGNO (rld[r].out_reg);
372e033b
RS
5970 int nr = 1;
5971
5972 if (nregno < FIRST_PSEUDO_REGISTER)
8ec450a4 5973 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
32131a9c
RK
5974
5975 while (--nr >= 0)
372e033b
RS
5976 reg_has_output_reload[nregno + nr] = 1;
5977
5978 if (i >= 0)
32131a9c 5979 {
8ec450a4 5980 nr = HARD_REGNO_NREGS (i, rld[r].mode);
372e033b 5981 while (--nr >= 0)
e6e52be0 5982 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
32131a9c
RK
5983 }
5984
eceef4c9
BS
5985 if (rld[r].when_needed != RELOAD_OTHER
5986 && rld[r].when_needed != RELOAD_FOR_OUTPUT
5987 && rld[r].when_needed != RELOAD_FOR_INSN)
32131a9c
RK
5988 abort ();
5989 }
5990 }
5991}
cb2afeb3
R
5992
5993/* Deallocate the reload register for reload R. This is called from
5994 remove_address_replacements. */
1d813780 5995
cb2afeb3
R
5996void
5997deallocate_reload_reg (r)
5998 int r;
5999{
6000 int regno;
6001
eceef4c9 6002 if (! rld[r].reg_rtx)
cb2afeb3 6003 return;
eceef4c9
BS
6004 regno = true_regnum (rld[r].reg_rtx);
6005 rld[r].reg_rtx = 0;
cb2afeb3 6006 if (spill_reg_order[regno] >= 0)
eceef4c9 6007 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
8ec450a4 6008 rld[r].mode);
cb2afeb3
R
6009 reload_spill_index[r] = -1;
6010}
32131a9c 6011\f
40f03658 6012/* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
546b63fb
RK
6013 reloads of the same item for fear that we might not have enough reload
6014 registers. However, normally they will get the same reload register
05d10675 6015 and hence actually need not be loaded twice.
546b63fb
RK
6016
6017 Here we check for the most common case of this phenomenon: when we have
6018 a number of reloads for the same object, each of which were allocated
6019 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6020 reload, and is not modified in the insn itself. If we find such,
6021 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6022 This will not increase the number of spill registers needed and will
6023 prevent redundant code. */
6024
546b63fb
RK
6025static void
6026merge_assigned_reloads (insn)
6027 rtx insn;
6028{
6029 int i, j;
6030
6031 /* Scan all the reloads looking for ones that only load values and
6032 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6033 assigned and not modified by INSN. */
6034
6035 for (i = 0; i < n_reloads; i++)
6036 {
d668e863
R
6037 int conflicting_input = 0;
6038 int max_input_address_opnum = -1;
6039 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6040
eceef4c9
BS
6041 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6042 || rld[i].out != 0 || rld[i].reg_rtx == 0
6043 || reg_set_p (rld[i].reg_rtx, insn))
546b63fb
RK
6044 continue;
6045
6046 /* Look at all other reloads. Ensure that the only use of this
6047 reload_reg_rtx is in a reload that just loads the same value
6048 as we do. Note that any secondary reloads must be of the identical
6049 class since the values, modes, and result registers are the
6050 same, so we need not do anything with any secondary reloads. */
6051
6052 for (j = 0; j < n_reloads; j++)
6053 {
eceef4c9
BS
6054 if (i == j || rld[j].reg_rtx == 0
6055 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6056 rld[i].reg_rtx))
546b63fb
RK
6057 continue;
6058
eceef4c9
BS
6059 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6060 && rld[j].opnum > max_input_address_opnum)
6061 max_input_address_opnum = rld[j].opnum;
d668e863 6062
546b63fb 6063 /* If the reload regs aren't exactly the same (e.g, different modes)
d668e863
R
6064 or if the values are different, we can't merge this reload.
6065 But if it is an input reload, we might still merge
6066 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
546b63fb 6067
eceef4c9
BS
6068 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6069 || rld[j].out != 0 || rld[j].in == 0
6070 || ! rtx_equal_p (rld[i].in, rld[j].in))
d668e863 6071 {
eceef4c9
BS
6072 if (rld[j].when_needed != RELOAD_FOR_INPUT
6073 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6074 || rld[i].opnum > rld[j].opnum)
6075 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
d668e863
R
6076 break;
6077 conflicting_input = 1;
eceef4c9
BS
6078 if (min_conflicting_input_opnum > rld[j].opnum)
6079 min_conflicting_input_opnum = rld[j].opnum;
d668e863 6080 }
546b63fb
RK
6081 }
6082
6083 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6084 we, in fact, found any matching reloads. */
6085
d668e863
R
6086 if (j == n_reloads
6087 && max_input_address_opnum <= min_conflicting_input_opnum)
546b63fb
RK
6088 {
6089 for (j = 0; j < n_reloads; j++)
eceef4c9
BS
6090 if (i != j && rld[j].reg_rtx != 0
6091 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
d668e863 6092 && (! conflicting_input
eceef4c9
BS
6093 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6094 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
546b63fb 6095 {
eceef4c9
BS
6096 rld[i].when_needed = RELOAD_OTHER;
6097 rld[j].in = 0;
efdb3590 6098 reload_spill_index[j] = -1;
546b63fb
RK
6099 transfer_replacements (i, j);
6100 }
6101
6102 /* If this is now RELOAD_OTHER, look for any reloads that load
6103 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6104 if they were for inputs, RELOAD_OTHER for outputs. Note that
6105 this test is equivalent to looking for reloads for this operand
6106 number. */
dec0798e
R
6107 /* We must take special care when there are two or more reloads to
6108 be merged and a RELOAD_FOR_OUTPUT_ADDRESS reload that loads the
6109 same value or a part of it; we must not change its type if there
6110 is a conflicting input. */
546b63fb 6111
eceef4c9 6112 if (rld[i].when_needed == RELOAD_OTHER)
546b63fb 6113 for (j = 0; j < n_reloads; j++)
eceef4c9 6114 if (rld[j].in != 0
91667711 6115 && rld[j].when_needed != RELOAD_OTHER
dec0798e
R
6116 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6117 && (! conflicting_input
6118 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6119 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
eceef4c9
BS
6120 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6121 rld[i].in))
c15c18c5
JW
6122 {
6123 int k;
6124
6125 rld[j].when_needed
6126 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6127 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6128 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6129
6130 /* Check to see if we accidentally converted two reloads
6131 that use the same reload register to the same type.
6132 If so, the resulting code won't work, so abort. */
6133 if (rld[j].reg_rtx)
6134 for (k = 0; k < j; k++)
6135 if (rld[k].in != 0 && rld[k].reg_rtx != 0
6136 && rld[k].when_needed == rld[j].when_needed
6137 && rtx_equal_p (rld[k].reg_rtx, rld[j].reg_rtx))
6138 abort ();
6139 }
546b63fb
RK
6140 }
6141 }
05d10675 6142}
546b63fb 6143\f
367b1cf5
BS
6144/* These arrays are filled by emit_reload_insns and its subroutines. */
6145static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6146static rtx other_input_address_reload_insns = 0;
6147static rtx other_input_reload_insns = 0;
6148static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6149static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6150static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6151static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6152static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6153static rtx operand_reload_insns = 0;
6154static rtx other_operand_reload_insns = 0;
6155static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6156
6157/* Values to be put in spill_reg_store are put here first. */
6158static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6159static HARD_REG_SET reg_reloaded_died;
6160
6161/* Generate insns to perform reload RL, which is for the insn in CHAIN and
6162 has the number J. OLD contains the value to be used as input. */
770ae6cc 6163
32131a9c 6164static void
367b1cf5 6165emit_input_reload_insns (chain, rl, old, j)
7609e720 6166 struct insn_chain *chain;
367b1cf5
BS
6167 struct reload *rl;
6168 rtx old;
6169 int j;
32131a9c 6170{
7609e720 6171 rtx insn = chain->insn;
b3694847 6172 rtx reloadreg = rl->reg_rtx;
367b1cf5
BS
6173 rtx oldequiv_reg = 0;
6174 rtx oldequiv = 0;
6175 int special = 0;
6176 enum machine_mode mode;
6177 rtx *where;
6178
6179 /* Determine the mode to reload in.
6180 This is very tricky because we have three to choose from.
6181 There is the mode the insn operand wants (rl->inmode).
6182 There is the mode of the reload register RELOADREG.
6183 There is the intrinsic mode of the operand, which we could find
6184 by stripping some SUBREGs.
6185 It turns out that RELOADREG's mode is irrelevant:
6186 we can change that arbitrarily.
6187
6188 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6189 then the reload reg may not support QImode moves, so use SImode.
6190 If foo is in memory due to spilling a pseudo reg, this is safe,
6191 because the QImode value is in the least significant part of a
6192 slot big enough for a SImode. If foo is some other sort of
6193 memory reference, then it is impossible to reload this case,
6194 so previous passes had better make sure this never happens.
6195
6196 Then consider a one-word union which has SImode and one of its
6197 members is a float, being fetched as (SUBREG:SF union:SI).
6198 We must fetch that as SFmode because we could be loading into
6199 a float-only register. In this case OLD's mode is correct.
6200
6201 Consider an immediate integer: it has VOIDmode. Here we need
6202 to get a mode from something else.
6203
6204 In some cases, there is a fourth mode, the operand's
6205 containing mode. If the insn specifies a containing mode for
6206 this operand, it overrides all others.
6207
6208 I am not sure whether the algorithm here is always right,
6209 but it does the right things in those cases. */
6210
6211 mode = GET_MODE (old);
6212 if (mode == VOIDmode)
6213 mode = rl->inmode;
7609e720 6214
367b1cf5
BS
6215#ifdef SECONDARY_INPUT_RELOAD_CLASS
6216 /* If we need a secondary register for this operation, see if
6217 the value is already in a register in that class. Don't
6218 do this if the secondary register will be used as a scratch
6219 register. */
6220
6221 if (rl->secondary_in_reload >= 0
6222 && rl->secondary_in_icode == CODE_FOR_nothing
6223 && optimize)
6224 oldequiv
6225 = find_equiv_reg (old, insn,
6226 rld[rl->secondary_in_reload].class,
9714cf43 6227 -1, NULL, 0, mode);
367b1cf5 6228#endif
e6e52be0 6229
367b1cf5
BS
6230 /* If reloading from memory, see if there is a register
6231 that already holds the same value. If so, reload from there.
6232 We can pass 0 as the reload_reg_p argument because
6233 any other reload has either already been emitted,
6234 in which case find_equiv_reg will see the reload-insn,
6235 or has yet to be emitted, in which case it doesn't matter
6236 because we will use this equiv reg right away. */
6237
6238 if (oldequiv == 0 && optimize
6239 && (GET_CODE (old) == MEM
6240 || (GET_CODE (old) == REG
6241 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6242 && reg_renumber[REGNO (old)] < 0)))
9714cf43 6243 oldequiv = find_equiv_reg (old, insn, ALL_REGS, -1, NULL, 0, mode);
367b1cf5
BS
6244
6245 if (oldequiv)
6246 {
770ae6cc 6247 unsigned int regno = true_regnum (oldequiv);
367b1cf5
BS
6248
6249 /* Don't use OLDEQUIV if any other reload changes it at an
6250 earlier stage of this insn or at this stage. */
c02cad8f
BS
6251 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6252 rl->in, const0_rtx, j, 0))
367b1cf5
BS
6253 oldequiv = 0;
6254
6255 /* If it is no cheaper to copy from OLDEQUIV into the
6256 reload register than it would be to move from memory,
6257 don't use it. Likewise, if we need a secondary register
6d2f8887 6258 or memory. */
367b1cf5
BS
6259
6260 if (oldequiv != 0
6261 && ((REGNO_REG_CLASS (regno) != rl->class
e56b4594 6262 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
367b1cf5
BS
6263 rl->class)
6264 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6265#ifdef SECONDARY_INPUT_RELOAD_CLASS
6266 || (SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6267 mode, oldequiv)
6268 != NO_REGS)
6269#endif
6270#ifdef SECONDARY_MEMORY_NEEDED
6271 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6272 rl->class,
6273 mode)
6274#endif
6275 ))
6276 oldequiv = 0;
6277 }
32131a9c 6278
367b1cf5
BS
6279 /* delete_output_reload is only invoked properly if old contains
6280 the original pseudo register. Since this is replaced with a
6281 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6282 find the pseudo in RELOAD_IN_REG. */
6283 if (oldequiv == 0
6284 && reload_override_in[j]
6285 && GET_CODE (rl->in_reg) == REG)
6286 {
6287 oldequiv = old;
6288 old = rl->in_reg;
6289 }
6290 if (oldequiv == 0)
6291 oldequiv = old;
6292 else if (GET_CODE (oldequiv) == REG)
6293 oldequiv_reg = oldequiv;
6294 else if (GET_CODE (oldequiv) == SUBREG)
6295 oldequiv_reg = SUBREG_REG (oldequiv);
6296
6297 /* If we are reloading from a register that was recently stored in
6298 with an output-reload, see if we can prove there was
6299 actually no need to store the old value in it. */
6300
6301 if (optimize && GET_CODE (oldequiv) == REG
6302 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6303 && spill_reg_store[REGNO (oldequiv)]
6304 && GET_CODE (old) == REG
6305 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6306 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6307 rl->out_reg)))
6308 delete_output_reload (insn, j, REGNO (oldequiv));
6309
6310 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6311 then load RELOADREG from OLDEQUIV. Note that we cannot use
6312 gen_lowpart_common since it can do the wrong thing when
6313 RELOADREG has a multi-word mode. Note that RELOADREG
6314 must always be a REG here. */
6315
6316 if (GET_MODE (reloadreg) != mode)
6317 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
6318 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6319 oldequiv = SUBREG_REG (oldequiv);
6320 if (GET_MODE (oldequiv) != VOIDmode
6321 && mode != GET_MODE (oldequiv))
ddef6bc7 6322 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
367b1cf5
BS
6323
6324 /* Switch to the right place to emit the reload insns. */
6325 switch (rl->when_needed)
6326 {
6327 case RELOAD_OTHER:
6328 where = &other_input_reload_insns;
6329 break;
6330 case RELOAD_FOR_INPUT:
6331 where = &input_reload_insns[rl->opnum];
6332 break;
6333 case RELOAD_FOR_INPUT_ADDRESS:
6334 where = &input_address_reload_insns[rl->opnum];
6335 break;
6336 case RELOAD_FOR_INPADDR_ADDRESS:
6337 where = &inpaddr_address_reload_insns[rl->opnum];
6338 break;
6339 case RELOAD_FOR_OUTPUT_ADDRESS:
6340 where = &output_address_reload_insns[rl->opnum];
6341 break;
6342 case RELOAD_FOR_OUTADDR_ADDRESS:
6343 where = &outaddr_address_reload_insns[rl->opnum];
6344 break;
6345 case RELOAD_FOR_OPERAND_ADDRESS:
6346 where = &operand_reload_insns;
6347 break;
6348 case RELOAD_FOR_OPADDR_ADDR:
6349 where = &other_operand_reload_insns;
6350 break;
6351 case RELOAD_FOR_OTHER_ADDRESS:
6352 where = &other_input_address_reload_insns;
6353 break;
6354 default:
6355 abort ();
6356 }
546b63fb 6357
367b1cf5 6358 push_to_sequence (*where);
32131a9c 6359
367b1cf5
BS
6360 /* Auto-increment addresses must be reloaded in a special way. */
6361 if (rl->out && ! rl->out_reg)
32131a9c 6362 {
367b1cf5
BS
6363 /* We are not going to bother supporting the case where a
6364 incremented register can't be copied directly from
6365 OLDEQUIV since this seems highly unlikely. */
6366 if (rl->secondary_in_reload >= 0)
6367 abort ();
32131a9c 6368
367b1cf5
BS
6369 if (reload_inherited[j])
6370 oldequiv = reloadreg;
cb2afeb3 6371
367b1cf5 6372 old = XEXP (rl->in_reg, 0);
32131a9c 6373
367b1cf5
BS
6374 if (optimize && GET_CODE (oldequiv) == REG
6375 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6376 && spill_reg_store[REGNO (oldequiv)]
6377 && GET_CODE (old) == REG
6378 && (dead_or_set_p (insn,
6379 spill_reg_stored_to[REGNO (oldequiv)])
6380 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6381 old)))
6382 delete_output_reload (insn, j, REGNO (oldequiv));
6383
6384 /* Prevent normal processing of this reload. */
6385 special = 1;
6386 /* Output a special code sequence for this case. */
6387 new_spill_reg_store[REGNO (reloadreg)]
6388 = inc_for_reload (reloadreg, oldequiv, rl->out,
6389 rl->inc);
6390 }
32131a9c 6391
367b1cf5
BS
6392 /* If we are reloading a pseudo-register that was set by the previous
6393 insn, see if we can get rid of that pseudo-register entirely
6394 by redirecting the previous insn into our reload register. */
6395
6396 else if (optimize && GET_CODE (old) == REG
6397 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6398 && dead_or_set_p (insn, old)
6399 /* This is unsafe if some other reload
6400 uses the same reg first. */
ff6534ad 6401 && ! conflicts_with_override (reloadreg)
c02cad8f
BS
6402 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6403 rl->when_needed, old, rl->out, j, 0))
367b1cf5
BS
6404 {
6405 rtx temp = PREV_INSN (insn);
6406 while (temp && GET_CODE (temp) == NOTE)
6407 temp = PREV_INSN (temp);
6408 if (temp
6409 && GET_CODE (temp) == INSN
6410 && GET_CODE (PATTERN (temp)) == SET
6411 && SET_DEST (PATTERN (temp)) == old
6412 /* Make sure we can access insn_operand_constraint. */
6413 && asm_noperands (PATTERN (temp)) < 0
367b1cf5
BS
6414 /* This is unsafe if operand occurs more than once in current
6415 insn. Perhaps some occurrences aren't reloaded. */
10d1bb36 6416 && count_occurrences (PATTERN (insn), old, 0) == 1)
367b1cf5 6417 {
10d1bb36 6418 rtx old = SET_DEST (PATTERN (temp));
367b1cf5
BS
6419 /* Store into the reload register instead of the pseudo. */
6420 SET_DEST (PATTERN (temp)) = reloadreg;
6421
10d1bb36
JH
6422 /* Verify that resulting insn is valid. */
6423 extract_insn (temp);
6424 if (constrain_operands (1))
32131a9c 6425 {
10d1bb36
JH
6426 /* If the previous insn is an output reload, the source is
6427 a reload register, and its spill_reg_store entry will
6428 contain the previous destination. This is now
6429 invalid. */
6430 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6431 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6432 {
6433 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6434 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6435 }
32131a9c 6436
10d1bb36
JH
6437 /* If these are the only uses of the pseudo reg,
6438 pretend for GDB it lives in the reload reg we used. */
6439 if (REG_N_DEATHS (REGNO (old)) == 1
6440 && REG_N_SETS (REGNO (old)) == 1)
6441 {
6442 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6443 alter_reg (REGNO (old), -1);
6444 }
6445 special = 1;
6446 }
6447 else
cb2afeb3 6448 {
10d1bb36 6449 SET_DEST (PATTERN (temp)) = old;
32131a9c 6450 }
367b1cf5
BS
6451 }
6452 }
32131a9c 6453
367b1cf5 6454 /* We can't do that, so output an insn to load RELOADREG. */
32131a9c 6455
367b1cf5
BS
6456#ifdef SECONDARY_INPUT_RELOAD_CLASS
6457 /* If we have a secondary reload, pick up the secondary register
6458 and icode, if any. If OLDEQUIV and OLD are different or
6459 if this is an in-out reload, recompute whether or not we
6460 still need a secondary register and what the icode should
6461 be. If we still need a secondary register and the class or
6462 icode is different, go back to reloading from OLD if using
6463 OLDEQUIV means that we got the wrong type of register. We
6464 cannot have different class or icode due to an in-out reload
6465 because we don't make such reloads when both the input and
6466 output need secondary reload registers. */
6467
07875628 6468 if (! special && rl->secondary_in_reload >= 0)
367b1cf5
BS
6469 {
6470 rtx second_reload_reg = 0;
6471 int secondary_reload = rl->secondary_in_reload;
6472 rtx real_oldequiv = oldequiv;
6473 rtx real_old = old;
6474 rtx tmp;
6475 enum insn_code icode;
6476
6477 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6478 and similarly for OLD.
6479 See comments in get_secondary_reload in reload.c. */
6480 /* If it is a pseudo that cannot be replaced with its
6481 equivalent MEM, we must fall back to reload_in, which
6482 will have all the necessary substitutions registered.
6483 Likewise for a pseudo that can't be replaced with its
6484 equivalent constant.
6485
6486 Take extra care for subregs of such pseudos. Note that
6487 we cannot use reg_equiv_mem in this case because it is
6488 not in the right mode. */
6489
6490 tmp = oldequiv;
6491 if (GET_CODE (tmp) == SUBREG)
6492 tmp = SUBREG_REG (tmp);
6493 if (GET_CODE (tmp) == REG
6494 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6495 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6496 || reg_equiv_constant[REGNO (tmp)] != 0))
6497 {
6498 if (! reg_equiv_mem[REGNO (tmp)]
6499 || num_not_at_initial_offset
6500 || GET_CODE (oldequiv) == SUBREG)
6501 real_oldequiv = rl->in;
6502 else
6503 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6504 }
32131a9c 6505
367b1cf5
BS
6506 tmp = old;
6507 if (GET_CODE (tmp) == SUBREG)
6508 tmp = SUBREG_REG (tmp);
6509 if (GET_CODE (tmp) == REG
6510 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6511 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6512 || reg_equiv_constant[REGNO (tmp)] != 0))
6513 {
6514 if (! reg_equiv_mem[REGNO (tmp)]
6515 || num_not_at_initial_offset
6516 || GET_CODE (old) == SUBREG)
6517 real_old = rl->in;
6518 else
6519 real_old = reg_equiv_mem[REGNO (tmp)];
6520 }
6521
6522 second_reload_reg = rld[secondary_reload].reg_rtx;
6523 icode = rl->secondary_in_icode;
6524
6525 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6526 || (rl->in != 0 && rl->out != 0))
6527 {
6528 enum reg_class new_class
6529 = SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6530 mode, real_oldequiv);
6531
6532 if (new_class == NO_REGS)
6533 second_reload_reg = 0;
6534 else
32131a9c 6535 {
367b1cf5
BS
6536 enum insn_code new_icode;
6537 enum machine_mode new_mode;
6538
6539 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6540 REGNO (second_reload_reg)))
6541 oldequiv = old, real_oldequiv = real_old;
6542 else
32131a9c 6543 {
367b1cf5
BS
6544 new_icode = reload_in_optab[(int) mode];
6545 if (new_icode != CODE_FOR_nothing
6546 && ((insn_data[(int) new_icode].operand[0].predicate
6547 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6548 (reloadreg, mode)))
6549 || (insn_data[(int) new_icode].operand[1].predicate
6550 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6551 (real_oldequiv, mode)))))
6552 new_icode = CODE_FOR_nothing;
6553
6554 if (new_icode == CODE_FOR_nothing)
6555 new_mode = mode;
6556 else
6557 new_mode = insn_data[(int) new_icode].operand[2].mode;
d30e8ef0 6558
367b1cf5 6559 if (GET_MODE (second_reload_reg) != new_mode)
32131a9c 6560 {
367b1cf5
BS
6561 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6562 new_mode))
6563 oldequiv = old, real_oldequiv = real_old;
6564 else
6565 second_reload_reg
6566 = gen_rtx_REG (new_mode,
6567 REGNO (second_reload_reg));
32131a9c 6568 }
32131a9c
RK
6569 }
6570 }
367b1cf5 6571 }
32131a9c 6572
367b1cf5
BS
6573 /* If we still need a secondary reload register, check
6574 to see if it is being used as a scratch or intermediate
6575 register and generate code appropriately. If we need
6576 a scratch register, use REAL_OLDEQUIV since the form of
6577 the insn may depend on the actual address if it is
6578 a MEM. */
546b63fb 6579
367b1cf5
BS
6580 if (second_reload_reg)
6581 {
6582 if (icode != CODE_FOR_nothing)
32131a9c 6583 {
367b1cf5
BS
6584 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6585 second_reload_reg));
07875628 6586 special = 1;
367b1cf5
BS
6587 }
6588 else
6589 {
6590 /* See if we need a scratch register to load the
6591 intermediate register (a tertiary reload). */
6592 enum insn_code tertiary_icode
6593 = rld[secondary_reload].secondary_in_icode;
1554c2c6 6594
367b1cf5
BS
6595 if (tertiary_icode != CODE_FOR_nothing)
6596 {
6597 rtx third_reload_reg
6598 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
1554c2c6 6599
367b1cf5
BS
6600 emit_insn ((GEN_FCN (tertiary_icode)
6601 (second_reload_reg, real_oldequiv,
6602 third_reload_reg)));
6603 }
6604 else
6605 gen_reload (second_reload_reg, real_oldequiv,
6606 rl->opnum,
6607 rl->when_needed);
32131a9c 6608
367b1cf5
BS
6609 oldequiv = second_reload_reg;
6610 }
6611 }
6612 }
6613#endif
32131a9c 6614
07875628 6615 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
367b1cf5
BS
6616 {
6617 rtx real_oldequiv = oldequiv;
6618
6619 if ((GET_CODE (oldequiv) == REG
6620 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6621 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6622 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6623 || (GET_CODE (oldequiv) == SUBREG
6624 && GET_CODE (SUBREG_REG (oldequiv)) == REG
6625 && (REGNO (SUBREG_REG (oldequiv))
6626 >= FIRST_PSEUDO_REGISTER)
6627 && ((reg_equiv_memory_loc
6628 [REGNO (SUBREG_REG (oldequiv))] != 0)
6629 || (reg_equiv_constant
716120a7
JJ
6630 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6631 || (CONSTANT_P (oldequiv)
019d2e99
AS
6632 && (PREFERRED_RELOAD_CLASS (oldequiv,
6633 REGNO_REG_CLASS (REGNO (reloadreg)))
6634 == NO_REGS)))
367b1cf5
BS
6635 real_oldequiv = rl->in;
6636 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6637 rl->when_needed);
6638 }
32131a9c 6639
94bd63e5
AH
6640 if (flag_non_call_exceptions)
6641 copy_eh_notes (insn, get_insns ());
6642
367b1cf5
BS
6643 /* End this sequence. */
6644 *where = get_insns ();
6645 end_sequence ();
a6a2274a 6646
367b1cf5
BS
6647 /* Update reload_override_in so that delete_address_reloads_1
6648 can see the actual register usage. */
6649 if (oldequiv_reg)
6650 reload_override_in[j] = oldequiv;
6651}
32131a9c 6652
367b1cf5
BS
6653/* Generate insns to for the output reload RL, which is for the insn described
6654 by CHAIN and has the number J. */
6655static void
6656emit_output_reload_insns (chain, rl, j)
6657 struct insn_chain *chain;
6658 struct reload *rl;
6659 int j;
6660{
6661 rtx reloadreg = rl->reg_rtx;
6662 rtx insn = chain->insn;
6663 int special = 0;
6664 rtx old = rl->out;
6665 enum machine_mode mode = GET_MODE (old);
6666 rtx p;
32131a9c 6667
367b1cf5
BS
6668 if (rl->when_needed == RELOAD_OTHER)
6669 start_sequence ();
6670 else
6671 push_to_sequence (output_reload_insns[rl->opnum]);
32131a9c 6672
367b1cf5
BS
6673 /* Determine the mode to reload in.
6674 See comments above (for input reloading). */
32131a9c 6675
367b1cf5
BS
6676 if (mode == VOIDmode)
6677 {
6678 /* VOIDmode should never happen for an output. */
6679 if (asm_noperands (PATTERN (insn)) < 0)
6680 /* It's the compiler's fault. */
6681 fatal_insn ("VOIDmode on an output", insn);
6682 error_for_asm (insn, "output operand is constant in `asm'");
6683 /* Prevent crash--use something we know is valid. */
6684 mode = word_mode;
6685 old = gen_rtx_REG (mode, REGNO (reloadreg));
6686 }
546b63fb 6687
367b1cf5
BS
6688 if (GET_MODE (reloadreg) != mode)
6689 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
32131a9c 6690
367b1cf5 6691#ifdef SECONDARY_OUTPUT_RELOAD_CLASS
32131a9c 6692
367b1cf5
BS
6693 /* If we need two reload regs, set RELOADREG to the intermediate
6694 one, since it will be stored into OLD. We might need a secondary
6695 register only for an input reload, so check again here. */
32131a9c 6696
367b1cf5
BS
6697 if (rl->secondary_out_reload >= 0)
6698 {
6699 rtx real_old = old;
cb2afeb3 6700
367b1cf5
BS
6701 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6702 && reg_equiv_mem[REGNO (old)] != 0)
6703 real_old = reg_equiv_mem[REGNO (old)];
32131a9c 6704
367b1cf5
BS
6705 if ((SECONDARY_OUTPUT_RELOAD_CLASS (rl->class,
6706 mode, real_old)
6707 != NO_REGS))
b60a8416 6708 {
367b1cf5
BS
6709 rtx second_reloadreg = reloadreg;
6710 reloadreg = rld[rl->secondary_out_reload].reg_rtx;
32131a9c 6711
367b1cf5
BS
6712 /* See if RELOADREG is to be used as a scratch register
6713 or as an intermediate register. */
6714 if (rl->secondary_out_icode != CODE_FOR_nothing)
6715 {
6716 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6717 (real_old, second_reloadreg, reloadreg)));
6718 special = 1;
6719 }
6720 else
6721 {
6722 /* See if we need both a scratch and intermediate reload
6723 register. */
32131a9c 6724
367b1cf5
BS
6725 int secondary_reload = rl->secondary_out_reload;
6726 enum insn_code tertiary_icode
6727 = rld[secondary_reload].secondary_out_icode;
32131a9c 6728
367b1cf5
BS
6729 if (GET_MODE (reloadreg) != mode)
6730 reloadreg = gen_rtx_REG (mode, REGNO (reloadreg));
cb2afeb3 6731
367b1cf5
BS
6732 if (tertiary_icode != CODE_FOR_nothing)
6733 {
6734 rtx third_reloadreg
6735 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
6736 rtx tem;
6737
6738 /* Copy primary reload reg to secondary reload reg.
6739 (Note that these have been swapped above, then
78adc5a0 6740 secondary reload reg to OLD using our insn.) */
367b1cf5
BS
6741
6742 /* If REAL_OLD is a paradoxical SUBREG, remove it
6743 and try to put the opposite SUBREG on
6744 RELOADREG. */
6745 if (GET_CODE (real_old) == SUBREG
6746 && (GET_MODE_SIZE (GET_MODE (real_old))
6747 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6748 && 0 != (tem = gen_lowpart_common
6749 (GET_MODE (SUBREG_REG (real_old)),
6750 reloadreg)))
6751 real_old = SUBREG_REG (real_old), reloadreg = tem;
6752
6753 gen_reload (reloadreg, second_reloadreg,
6754 rl->opnum, rl->when_needed);
6755 emit_insn ((GEN_FCN (tertiary_icode)
6756 (real_old, reloadreg, third_reloadreg)));
6757 special = 1;
6758 }
05d10675 6759
367b1cf5
BS
6760 else
6761 /* Copy between the reload regs here and then to
6762 OUT later. */
cb2afeb3 6763
367b1cf5
BS
6764 gen_reload (reloadreg, second_reloadreg,
6765 rl->opnum, rl->when_needed);
a7911cd2 6766 }
367b1cf5
BS
6767 }
6768 }
32131a9c
RK
6769#endif
6770
367b1cf5
BS
6771 /* Output the last reload insn. */
6772 if (! special)
6773 {
6774 rtx set;
6775
6776 /* Don't output the last reload if OLD is not the dest of
1d7254c5 6777 INSN and is in the src and is clobbered by INSN. */
367b1cf5
BS
6778 if (! flag_expensive_optimizations
6779 || GET_CODE (old) != REG
6780 || !(set = single_set (insn))
6781 || rtx_equal_p (old, SET_DEST (set))
6782 || !reg_mentioned_p (old, SET_SRC (set))
9532e31f 6783 || !regno_clobbered_p (REGNO (old), insn, rl->mode, 0))
367b1cf5
BS
6784 gen_reload (old, reloadreg, rl->opnum,
6785 rl->when_needed);
6786 }
32131a9c 6787
367b1cf5
BS
6788 /* Look at all insns we emitted, just to be safe. */
6789 for (p = get_insns (); p; p = NEXT_INSN (p))
2c3c49de 6790 if (INSN_P (p))
367b1cf5
BS
6791 {
6792 rtx pat = PATTERN (p);
546b63fb 6793
367b1cf5
BS
6794 /* If this output reload doesn't come from a spill reg,
6795 clear any memory of reloaded copies of the pseudo reg.
6796 If this output reload comes from a spill reg,
6797 reg_has_output_reload will make this do nothing. */
6798 note_stores (pat, forget_old_reloads_1, NULL);
cb2afeb3 6799
367b1cf5
BS
6800 if (reg_mentioned_p (rl->reg_rtx, pat))
6801 {
6802 rtx set = single_set (insn);
6803 if (reload_spill_index[j] < 0
6804 && set
6805 && SET_SRC (set) == rl->reg_rtx)
6806 {
6807 int src = REGNO (SET_SRC (set));
32131a9c 6808
367b1cf5
BS
6809 reload_spill_index[j] = src;
6810 SET_HARD_REG_BIT (reg_is_output_reload, src);
6811 if (find_regno_note (insn, REG_DEAD, src))
6812 SET_HARD_REG_BIT (reg_reloaded_died, src);
6813 }
6814 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6815 {
6816 int s = rl->secondary_out_reload;
6817 set = single_set (p);
6818 /* If this reload copies only to the secondary reload
6819 register, the secondary reload does the actual
6820 store. */
6821 if (s >= 0 && set == NULL_RTX)
1d7254c5
KH
6822 /* We can't tell what function the secondary reload
6823 has and where the actual store to the pseudo is
6824 made; leave new_spill_reg_store alone. */
6825 ;
367b1cf5
BS
6826 else if (s >= 0
6827 && SET_SRC (set) == rl->reg_rtx
6828 && SET_DEST (set) == rld[s].reg_rtx)
6829 {
6830 /* Usually the next instruction will be the
6831 secondary reload insn; if we can confirm
6832 that it is, setting new_spill_reg_store to
6833 that insn will allow an extra optimization. */
6834 rtx s_reg = rld[s].reg_rtx;
6835 rtx next = NEXT_INSN (p);
6836 rld[s].out = rl->out;
6837 rld[s].out_reg = rl->out_reg;
6838 set = single_set (next);
6839 if (set && SET_SRC (set) == s_reg
6840 && ! new_spill_reg_store[REGNO (s_reg)])
6841 {
6842 SET_HARD_REG_BIT (reg_is_output_reload,
6843 REGNO (s_reg));
6844 new_spill_reg_store[REGNO (s_reg)] = next;
6845 }
6846 }
6847 else
6848 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6849 }
6850 }
6851 }
32131a9c 6852
367b1cf5
BS
6853 if (rl->when_needed == RELOAD_OTHER)
6854 {
2f937369 6855 emit_insn (other_output_reload_insns[rl->opnum]);
367b1cf5
BS
6856 other_output_reload_insns[rl->opnum] = get_insns ();
6857 }
6858 else
6859 output_reload_insns[rl->opnum] = get_insns ();
32131a9c 6860
94bd63e5
AH
6861 if (flag_non_call_exceptions)
6862 copy_eh_notes (insn, get_insns ());
6863
1d7254c5 6864 end_sequence ();
367b1cf5 6865}
32131a9c 6866
367b1cf5
BS
6867/* Do input reloading for reload RL, which is for the insn described by CHAIN
6868 and has the number J. */
6869static void
6870do_input_reload (chain, rl, j)
6871 struct insn_chain *chain;
6872 struct reload *rl;
6873 int j;
6874{
6875 int expect_occurrences = 1;
6876 rtx insn = chain->insn;
6877 rtx old = (rl->in && GET_CODE (rl->in) == MEM
6878 ? rl->in_reg : rl->in);
6879
6880 if (old != 0
6881 /* AUTO_INC reloads need to be handled even if inherited. We got an
6882 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6883 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
6884 && ! rtx_equal_p (rl->reg_rtx, old)
6885 && rl->reg_rtx != 0)
1d813780 6886 emit_input_reload_insns (chain, rld + j, old, j);
32131a9c 6887
367b1cf5
BS
6888 /* When inheriting a wider reload, we have a MEM in rl->in,
6889 e.g. inheriting a SImode output reload for
6890 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6891 if (optimize && reload_inherited[j] && rl->in
6892 && GET_CODE (rl->in) == MEM
6893 && GET_CODE (rl->in_reg) == MEM
6894 && reload_spill_index[j] >= 0
6895 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6896 {
6897 expect_occurrences
4b983fdc 6898 = count_occurrences (PATTERN (insn), rl->in, 0) == 1 ? 0 : -1;
1d7254c5 6899 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
367b1cf5 6900 }
32131a9c 6901
367b1cf5
BS
6902 /* If we are reloading a register that was recently stored in with an
6903 output-reload, see if we can prove there was
6904 actually no need to store the old value in it. */
32131a9c 6905
367b1cf5
BS
6906 if (optimize
6907 && (reload_inherited[j] || reload_override_in[j])
6908 && rl->reg_rtx
6909 && GET_CODE (rl->reg_rtx) == REG
6910 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
6911#if 0
6912 /* There doesn't seem to be any reason to restrict this to pseudos
6913 and doing so loses in the case where we are copying from a
6914 register of the wrong class. */
6915 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
6916 >= FIRST_PSEUDO_REGISTER)
6917#endif
6918 /* The insn might have already some references to stackslots
6919 replaced by MEMs, while reload_out_reg still names the
6920 original pseudo. */
6921 && (dead_or_set_p (insn,
6922 spill_reg_stored_to[REGNO (rl->reg_rtx)])
6923 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
6924 rl->out_reg)))
6925 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
6926}
32131a9c 6927
367b1cf5
BS
6928/* Do output reloading for reload RL, which is for the insn described by
6929 CHAIN and has the number J.
6930 ??? At some point we need to support handling output reloads of
6931 JUMP_INSNs or insns that set cc0. */
6932static void
6933do_output_reload (chain, rl, j)
6934 struct insn_chain *chain;
6935 struct reload *rl;
6936 int j;
6937{
6938 rtx note, old;
6939 rtx insn = chain->insn;
6940 /* If this is an output reload that stores something that is
6941 not loaded in this same reload, see if we can eliminate a previous
6942 store. */
6943 rtx pseudo = rl->out_reg;
6944
6945 if (pseudo
159d5964 6946 && optimize
367b1cf5
BS
6947 && GET_CODE (pseudo) == REG
6948 && ! rtx_equal_p (rl->in_reg, pseudo)
6949 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
6950 && reg_last_reload_reg[REGNO (pseudo)])
6951 {
6952 int pseudo_no = REGNO (pseudo);
6953 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
6954
6955 /* We don't need to test full validity of last_regno for
6956 inherit here; we only want to know if the store actually
6957 matches the pseudo. */
60ef417d
GK
6958 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
6959 && reg_reloaded_contents[last_regno] == pseudo_no
367b1cf5
BS
6960 && spill_reg_store[last_regno]
6961 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
6962 delete_output_reload (insn, j, last_regno);
6963 }
5e03c156 6964
367b1cf5
BS
6965 old = rl->out_reg;
6966 if (old == 0
6967 || rl->reg_rtx == old
6968 || rl->reg_rtx == 0)
6969 return;
32131a9c 6970
367b1cf5
BS
6971 /* An output operand that dies right away does need a reload,
6972 but need not be copied from it. Show the new location in the
6973 REG_UNUSED note. */
6974 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6975 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6976 {
6977 XEXP (note, 0) = rl->reg_rtx;
6978 return;
6979 }
6980 /* Likewise for a SUBREG of an operand that dies. */
6981 else if (GET_CODE (old) == SUBREG
6982 && GET_CODE (SUBREG_REG (old)) == REG
6983 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6984 SUBREG_REG (old))))
6985 {
6986 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6987 rl->reg_rtx);
6988 return;
6989 }
6990 else if (GET_CODE (old) == SCRATCH)
6991 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6992 but we don't want to make an output reload. */
6993 return;
1554c2c6 6994
367b1cf5
BS
6995 /* If is a JUMP_INSN, we can't support output reloads yet. */
6996 if (GET_CODE (insn) == JUMP_INSN)
6997 abort ();
5e03c156 6998
367b1cf5
BS
6999 emit_output_reload_insns (chain, rld + j, j);
7000}
1554c2c6 7001
367b1cf5 7002/* Output insns to reload values in and out of the chosen reload regs. */
32131a9c 7003
367b1cf5 7004static void
e04ca094 7005emit_reload_insns (chain)
367b1cf5
BS
7006 struct insn_chain *chain;
7007{
7008 rtx insn = chain->insn;
32131a9c 7009
b3694847 7010 int j;
e6e52be0 7011
367b1cf5 7012 CLEAR_HARD_REG_SET (reg_reloaded_died);
e6e52be0 7013
367b1cf5
BS
7014 for (j = 0; j < reload_n_operands; j++)
7015 input_reload_insns[j] = input_address_reload_insns[j]
7016 = inpaddr_address_reload_insns[j]
7017 = output_reload_insns[j] = output_address_reload_insns[j]
7018 = outaddr_address_reload_insns[j]
7019 = other_output_reload_insns[j] = 0;
7020 other_input_address_reload_insns = 0;
7021 other_input_reload_insns = 0;
7022 operand_reload_insns = 0;
7023 other_operand_reload_insns = 0;
32131a9c 7024
850aac53 7025 /* Dump reloads into the dump file. */
e04ca094 7026 if (rtl_dump_file)
850aac53 7027 {
e04ca094
JL
7028 fprintf (rtl_dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7029 debug_reload_to_stream (rtl_dump_file);
850aac53
JL
7030 }
7031
367b1cf5
BS
7032 /* Now output the instructions to copy the data into and out of the
7033 reload registers. Do these in the order that the reloads were reported,
7034 since reloads of base and index registers precede reloads of operands
7035 and the operands may need the base and index registers reloaded. */
32131a9c 7036
367b1cf5
BS
7037 for (j = 0; j < n_reloads; j++)
7038 {
7039 if (rld[j].reg_rtx
7040 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7041 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
d7e0324f 7042
367b1cf5
BS
7043 do_input_reload (chain, rld + j, j);
7044 do_output_reload (chain, rld + j, j);
32131a9c
RK
7045 }
7046
546b63fb
RK
7047 /* Now write all the insns we made for reloads in the order expected by
7048 the allocation functions. Prior to the insn being reloaded, we write
7049 the following reloads:
7050
7051 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7052
2edc8d65 7053 RELOAD_OTHER reloads.
546b63fb 7054
47c8cf91
ILT
7055 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7056 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7057 RELOAD_FOR_INPUT reload for the operand.
546b63fb 7058
893bc853
RK
7059 RELOAD_FOR_OPADDR_ADDRS reloads.
7060
546b63fb
RK
7061 RELOAD_FOR_OPERAND_ADDRESS reloads.
7062
7063 After the insn being reloaded, we write the following:
7064
47c8cf91
ILT
7065 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7066 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7067 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7068 reloads for the operand. The RELOAD_OTHER output reloads are
7069 output in descending order by reload number. */
546b63fb 7070
2f937369
DM
7071 emit_insn_before (other_input_address_reload_insns, insn);
7072 emit_insn_before (other_input_reload_insns, insn);
546b63fb
RK
7073
7074 for (j = 0; j < reload_n_operands; j++)
7075 {
2f937369
DM
7076 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7077 emit_insn_before (input_address_reload_insns[j], insn);
7078 emit_insn_before (input_reload_insns[j], insn);
546b63fb
RK
7079 }
7080
2f937369
DM
7081 emit_insn_before (other_operand_reload_insns, insn);
7082 emit_insn_before (operand_reload_insns, insn);
546b63fb
RK
7083
7084 for (j = 0; j < reload_n_operands; j++)
7085 {
2f937369
DM
7086 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7087 x = emit_insn_after (output_address_reload_insns[j], x);
7088 x = emit_insn_after (output_reload_insns[j], x);
7089 emit_insn_after (other_output_reload_insns[j], x);
546b63fb
RK
7090 }
7091
32131a9c
RK
7092 /* For all the spill regs newly reloaded in this instruction,
7093 record what they were reloaded from, so subsequent instructions
d445b551
RK
7094 can inherit the reloads.
7095
7096 Update spill_reg_store for the reloads of this insn.
e9e79d69 7097 Copy the elements that were updated in the loop above. */
32131a9c
RK
7098
7099 for (j = 0; j < n_reloads; j++)
7100 {
b3694847
SS
7101 int r = reload_order[j];
7102 int i = reload_spill_index[r];
32131a9c 7103
78a2bc08 7104 /* If this is a non-inherited input reload from a pseudo, we must
05d10675
BS
7105 clear any memory of a previous store to the same pseudo. Only do
7106 something if there will not be an output reload for the pseudo
7107 being reloaded. */
eceef4c9 7108 if (rld[r].in_reg != 0
05d10675
BS
7109 && ! (reload_inherited[r] || reload_override_in[r]))
7110 {
eceef4c9 7111 rtx reg = rld[r].in_reg;
78a2bc08 7112
05d10675 7113 if (GET_CODE (reg) == SUBREG)
78a2bc08 7114 reg = SUBREG_REG (reg);
05d10675
BS
7115
7116 if (GET_CODE (reg) == REG
78a2bc08
R
7117 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7118 && ! reg_has_output_reload[REGNO (reg)])
7119 {
7120 int nregno = REGNO (reg);
7121
7122 if (reg_last_reload_reg[nregno])
05d10675
BS
7123 {
7124 int last_regno = REGNO (reg_last_reload_reg[nregno]);
78a2bc08 7125
05d10675 7126 if (reg_reloaded_contents[last_regno] == nregno)
78a2bc08 7127 spill_reg_store[last_regno] = 0;
05d10675 7128 }
78a2bc08
R
7129 }
7130 }
05d10675 7131
e6e52be0 7132 /* I is nonneg if this reload used a register.
eceef4c9 7133 If rld[r].reg_rtx is 0, this is an optional reload
51f0c3b7 7134 that we opted to ignore. */
d445b551 7135
eceef4c9 7136 if (i >= 0 && rld[r].reg_rtx != 0)
32131a9c 7137 {
1d7254c5 7138 int nr = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
32131a9c 7139 int k;
51f0c3b7
JW
7140 int part_reaches_end = 0;
7141 int all_reaches_end = 1;
32131a9c 7142
51f0c3b7
JW
7143 /* For a multi register reload, we need to check if all or part
7144 of the value lives to the end. */
32131a9c
RK
7145 for (k = 0; k < nr; k++)
7146 {
eceef4c9
BS
7147 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7148 rld[r].when_needed))
51f0c3b7
JW
7149 part_reaches_end = 1;
7150 else
7151 all_reaches_end = 0;
32131a9c
RK
7152 }
7153
51f0c3b7
JW
7154 /* Ignore reloads that don't reach the end of the insn in
7155 entirety. */
7156 if (all_reaches_end)
32131a9c 7157 {
51f0c3b7
JW
7158 /* First, clear out memory of what used to be in this spill reg.
7159 If consecutive registers are used, clear them all. */
d08ea79f 7160
32131a9c 7161 for (k = 0; k < nr; k++)
e6e52be0 7162 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
d08ea79f 7163
51f0c3b7 7164 /* Maybe the spill reg contains a copy of reload_out. */
eceef4c9
BS
7165 if (rld[r].out != 0
7166 && (GET_CODE (rld[r].out) == REG
cb2afeb3 7167#ifdef AUTO_INC_DEC
eceef4c9 7168 || ! rld[r].out_reg
cb2afeb3 7169#endif
eceef4c9 7170 || GET_CODE (rld[r].out_reg) == REG))
51f0c3b7 7171 {
eceef4c9
BS
7172 rtx out = (GET_CODE (rld[r].out) == REG
7173 ? rld[r].out
7174 : rld[r].out_reg
7175 ? rld[r].out_reg
7176/* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
b3694847 7177 int nregno = REGNO (out);
51f0c3b7
JW
7178 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7179 : HARD_REGNO_NREGS (nregno,
eceef4c9 7180 GET_MODE (rld[r].reg_rtx)));
51f0c3b7
JW
7181
7182 spill_reg_store[i] = new_spill_reg_store[i];
cb2afeb3 7183 spill_reg_stored_to[i] = out;
eceef4c9 7184 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
51f0c3b7
JW
7185
7186 /* If NREGNO is a hard register, it may occupy more than
05d10675 7187 one register. If it does, say what is in the
51f0c3b7
JW
7188 rest of the registers assuming that both registers
7189 agree on how many words the object takes. If not,
7190 invalidate the subsequent registers. */
7191
7192 if (nregno < FIRST_PSEUDO_REGISTER)
7193 for (k = 1; k < nnr; k++)
7194 reg_last_reload_reg[nregno + k]
7195 = (nr == nnr
39d31de8 7196 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
51f0c3b7
JW
7197 : 0);
7198
7199 /* Now do the inverse operation. */
7200 for (k = 0; k < nr; k++)
7201 {
e6e52be0
R
7202 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7203 reg_reloaded_contents[i + k]
51f0c3b7
JW
7204 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7205 ? nregno
7206 : nregno + k);
e6e52be0
R
7207 reg_reloaded_insn[i + k] = insn;
7208 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
51f0c3b7
JW
7209 }
7210 }
d08ea79f 7211
51f0c3b7
JW
7212 /* Maybe the spill reg contains a copy of reload_in. Only do
7213 something if there will not be an output reload for
7214 the register being reloaded. */
eceef4c9
BS
7215 else if (rld[r].out_reg == 0
7216 && rld[r].in != 0
7217 && ((GET_CODE (rld[r].in) == REG
7218 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7219 && ! reg_has_output_reload[REGNO (rld[r].in)])
7220 || (GET_CODE (rld[r].in_reg) == REG
7221 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7222 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
51f0c3b7 7223 {
b3694847 7224 int nregno;
51f0c3b7 7225 int nnr;
d445b551 7226
eceef4c9
BS
7227 if (GET_CODE (rld[r].in) == REG
7228 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7229 nregno = REGNO (rld[r].in);
7230 else if (GET_CODE (rld[r].in_reg) == REG)
7231 nregno = REGNO (rld[r].in_reg);
cb2afeb3 7232 else
eceef4c9 7233 nregno = REGNO (XEXP (rld[r].in_reg, 0));
d08ea79f 7234
51f0c3b7
JW
7235 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7236 : HARD_REGNO_NREGS (nregno,
eceef4c9 7237 GET_MODE (rld[r].reg_rtx)));
05d10675 7238
eceef4c9 7239 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
51f0c3b7
JW
7240
7241 if (nregno < FIRST_PSEUDO_REGISTER)
7242 for (k = 1; k < nnr; k++)
7243 reg_last_reload_reg[nregno + k]
7244 = (nr == nnr
39d31de8 7245 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
51f0c3b7
JW
7246 : 0);
7247
7248 /* Unless we inherited this reload, show we haven't
cb2afeb3
R
7249 recently done a store.
7250 Previous stores of inherited auto_inc expressions
7251 also have to be discarded. */
7252 if (! reload_inherited[r]
eceef4c9 7253 || (rld[r].out && ! rld[r].out_reg))
51f0c3b7
JW
7254 spill_reg_store[i] = 0;
7255
7256 for (k = 0; k < nr; k++)
7257 {
e6e52be0
R
7258 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7259 reg_reloaded_contents[i + k]
51f0c3b7
JW
7260 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7261 ? nregno
7262 : nregno + k);
e6e52be0
R
7263 reg_reloaded_insn[i + k] = insn;
7264 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
51f0c3b7
JW
7265 }
7266 }
7267 }
d445b551 7268
51f0c3b7
JW
7269 /* However, if part of the reload reaches the end, then we must
7270 invalidate the old info for the part that survives to the end. */
7271 else if (part_reaches_end)
7272 {
546b63fb 7273 for (k = 0; k < nr; k++)
e6e52be0 7274 if (reload_reg_reaches_end_p (i + k,
eceef4c9
BS
7275 rld[r].opnum,
7276 rld[r].when_needed))
e6e52be0 7277 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
32131a9c
RK
7278 }
7279 }
7280
7281 /* The following if-statement was #if 0'd in 1.34 (or before...).
7282 It's reenabled in 1.35 because supposedly nothing else
7283 deals with this problem. */
7284
7285 /* If a register gets output-reloaded from a non-spill register,
7286 that invalidates any previous reloaded copy of it.
7287 But forget_old_reloads_1 won't get to see it, because
7288 it thinks only about the original insn. So invalidate it here. */
eceef4c9
BS
7289 if (i < 0 && rld[r].out != 0
7290 && (GET_CODE (rld[r].out) == REG
7291 || (GET_CODE (rld[r].out) == MEM
7292 && GET_CODE (rld[r].out_reg) == REG)))
32131a9c 7293 {
eceef4c9
BS
7294 rtx out = (GET_CODE (rld[r].out) == REG
7295 ? rld[r].out : rld[r].out_reg);
b3694847 7296 int nregno = REGNO (out);
c7093272 7297 if (nregno >= FIRST_PSEUDO_REGISTER)
cb2afeb3 7298 {
6a651371 7299 rtx src_reg, store_insn = NULL_RTX;
cb2afeb3
R
7300
7301 reg_last_reload_reg[nregno] = 0;
7302
7303 /* If we can find a hard register that is stored, record
7304 the storing insn so that we may delete this insn with
7305 delete_output_reload. */
eceef4c9 7306 src_reg = rld[r].reg_rtx;
cb2afeb3
R
7307
7308 /* If this is an optional reload, try to find the source reg
7309 from an input reload. */
7310 if (! src_reg)
7311 {
7312 rtx set = single_set (insn);
eceef4c9 7313 if (set && SET_DEST (set) == rld[r].out)
cb2afeb3
R
7314 {
7315 int k;
7316
7317 src_reg = SET_SRC (set);
7318 store_insn = insn;
7319 for (k = 0; k < n_reloads; k++)
7320 {
eceef4c9 7321 if (rld[k].in == src_reg)
cb2afeb3 7322 {
eceef4c9 7323 src_reg = rld[k].reg_rtx;
cb2afeb3
R
7324 break;
7325 }
7326 }
7327 }
7328 }
7329 else
7330 store_insn = new_spill_reg_store[REGNO (src_reg)];
7331 if (src_reg && GET_CODE (src_reg) == REG
7332 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7333 {
7334 int src_regno = REGNO (src_reg);
8ec450a4 7335 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
cb2afeb3
R
7336 /* The place where to find a death note varies with
7337 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7338 necessarily checked exactly in the code that moves
7339 notes, so just check both locations. */
7340 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
1558b970 7341 if (! note && store_insn)
cb2afeb3
R
7342 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7343 while (nr-- > 0)
7344 {
7345 spill_reg_store[src_regno + nr] = store_insn;
7346 spill_reg_stored_to[src_regno + nr] = out;
7347 reg_reloaded_contents[src_regno + nr] = nregno;
7348 reg_reloaded_insn[src_regno + nr] = store_insn;
00f9f1bc 7349 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
cb2afeb3
R
7350 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7351 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7352 if (note)
7353 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7354 else
7355 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7356 }
7357 reg_last_reload_reg[nregno] = src_reg;
7358 }
7359 }
c7093272
RK
7360 else
7361 {
1d7254c5 7362 int num_regs = HARD_REGNO_NREGS (nregno, GET_MODE (rld[r].out));
36281332 7363
c7093272
RK
7364 while (num_regs-- > 0)
7365 reg_last_reload_reg[nregno + num_regs] = 0;
7366 }
32131a9c
RK
7367 }
7368 }
e6e52be0 7369 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
32131a9c
RK
7370}
7371\f
5e03c156
RK
7372/* Emit code to perform a reload from IN (which may be a reload register) to
7373 OUT (which may also be a reload register). IN or OUT is from operand
05d10675 7374 OPNUM with reload type TYPE.
546b63fb 7375
3c3eeea6 7376 Returns first insn emitted. */
32131a9c
RK
7377
7378rtx
5e03c156
RK
7379gen_reload (out, in, opnum, type)
7380 rtx out;
32131a9c 7381 rtx in;
546b63fb
RK
7382 int opnum;
7383 enum reload_type type;
32131a9c 7384{
546b63fb 7385 rtx last = get_last_insn ();
7a5b18b0
RK
7386 rtx tem;
7387
7388 /* If IN is a paradoxical SUBREG, remove it and try to put the
7389 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7390 if (GET_CODE (in) == SUBREG
7391 && (GET_MODE_SIZE (GET_MODE (in))
7392 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7393 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7394 in = SUBREG_REG (in), out = tem;
7395 else if (GET_CODE (out) == SUBREG
eceef4c9
BS
7396 && (GET_MODE_SIZE (GET_MODE (out))
7397 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7398 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7a5b18b0 7399 out = SUBREG_REG (out), in = tem;
32131a9c 7400
a8fdc208 7401 /* How to do this reload can get quite tricky. Normally, we are being
32131a9c
RK
7402 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7403 register that didn't get a hard register. In that case we can just
7404 call emit_move_insn.
7405
a7fd196c
JW
7406 We can also be asked to reload a PLUS that adds a register or a MEM to
7407 another register, constant or MEM. This can occur during frame pointer
7408 elimination and while reloading addresses. This case is handled by
7409 trying to emit a single insn to perform the add. If it is not valid,
7410 we use a two insn sequence.
32131a9c
RK
7411
7412 Finally, we could be called to handle an 'o' constraint by putting
7413 an address into a register. In that case, we first try to do this
7414 with a named pattern of "reload_load_address". If no such pattern
7415 exists, we just emit a SET insn and hope for the best (it will normally
7416 be valid on machines that use 'o').
7417
7418 This entire process is made complex because reload will never
7419 process the insns we generate here and so we must ensure that
7420 they will fit their constraints and also by the fact that parts of
7421 IN might be being reloaded separately and replaced with spill registers.
7422 Because of this, we are, in some sense, just guessing the right approach
7423 here. The one listed above seems to work.
7424
7425 ??? At some point, this whole thing needs to be rethought. */
7426
7427 if (GET_CODE (in) == PLUS
a7fd196c 7428 && (GET_CODE (XEXP (in, 0)) == REG
5c6b1bd2 7429 || GET_CODE (XEXP (in, 0)) == SUBREG
a7fd196c
JW
7430 || GET_CODE (XEXP (in, 0)) == MEM)
7431 && (GET_CODE (XEXP (in, 1)) == REG
5c6b1bd2 7432 || GET_CODE (XEXP (in, 1)) == SUBREG
a7fd196c
JW
7433 || CONSTANT_P (XEXP (in, 1))
7434 || GET_CODE (XEXP (in, 1)) == MEM))
32131a9c 7435 {
a7fd196c
JW
7436 /* We need to compute the sum of a register or a MEM and another
7437 register, constant, or MEM, and put it into the reload
3002e160
JW
7438 register. The best possible way of doing this is if the machine
7439 has a three-operand ADD insn that accepts the required operands.
32131a9c
RK
7440
7441 The simplest approach is to try to generate such an insn and see if it
7442 is recognized and matches its constraints. If so, it can be used.
7443
7444 It might be better not to actually emit the insn unless it is valid,
0009eff2 7445 but we need to pass the insn as an operand to `recog' and
0eadeb15 7446 `extract_insn' and it is simpler to emit and then delete the insn if
0009eff2 7447 not valid than to dummy things up. */
a8fdc208 7448
af929c62 7449 rtx op0, op1, tem, insn;
32131a9c 7450 int code;
a8fdc208 7451
af929c62
RK
7452 op0 = find_replacement (&XEXP (in, 0));
7453 op1 = find_replacement (&XEXP (in, 1));
7454
32131a9c
RK
7455 /* Since constraint checking is strict, commutativity won't be
7456 checked, so we need to do that here to avoid spurious failure
7457 if the add instruction is two-address and the second operand
7458 of the add is the same as the reload reg, which is frequently
7459 the case. If the insn would be A = B + A, rearrange it so
0f41302f 7460 it will be A = A + B as constrain_operands expects. */
a8fdc208 7461
32131a9c 7462 if (GET_CODE (XEXP (in, 1)) == REG
5e03c156 7463 && REGNO (out) == REGNO (XEXP (in, 1)))
af929c62
RK
7464 tem = op0, op0 = op1, op1 = tem;
7465
7466 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
38a448ca 7467 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
32131a9c 7468
38a448ca 7469 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
32131a9c
RK
7470 code = recog_memoized (insn);
7471
7472 if (code >= 0)
7473 {
0eadeb15 7474 extract_insn (insn);
32131a9c
RK
7475 /* We want constrain operands to treat this insn strictly in
7476 its validity determination, i.e., the way it would after reload
7477 has completed. */
0eadeb15 7478 if (constrain_operands (1))
32131a9c
RK
7479 return insn;
7480 }
7481
546b63fb 7482 delete_insns_since (last);
32131a9c
RK
7483
7484 /* If that failed, we must use a conservative two-insn sequence.
09522f21
FS
7485
7486 Use a move to copy one operand into the reload register. Prefer
7487 to reload a constant, MEM or pseudo since the move patterns can
7488 handle an arbitrary operand. If OP1 is not a constant, MEM or
7489 pseudo and OP1 is not a valid operand for an add instruction, then
7490 reload OP1.
7491
7492 After reloading one of the operands into the reload register, add
7493 the reload register to the output register.
32131a9c
RK
7494
7495 If there is another way to do this for a specific machine, a
7496 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7497 we emit below. */
7498
09522f21
FS
7499 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7500
5c6b1bd2 7501 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
af929c62 7502 || (GET_CODE (op1) == REG
09522f21
FS
7503 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7504 || (code != CODE_FOR_nothing
a995e389
RH
7505 && ! ((*insn_data[code].operand[2].predicate)
7506 (op1, insn_data[code].operand[2].mode))))
af929c62 7507 tem = op0, op0 = op1, op1 = tem;
32131a9c 7508
5c6b1bd2 7509 gen_reload (out, op0, opnum, type);
39b56c2a 7510
5e03c156 7511 /* If OP0 and OP1 are the same, we can use OUT for OP1.
39b56c2a
RK
7512 This fixes a problem on the 32K where the stack pointer cannot
7513 be used as an operand of an add insn. */
7514
7515 if (rtx_equal_p (op0, op1))
5e03c156 7516 op1 = out;
39b56c2a 7517
5e03c156 7518 insn = emit_insn (gen_add2_insn (out, op1));
c77c9766
RK
7519
7520 /* If that failed, copy the address register to the reload register.
0f41302f 7521 Then add the constant to the reload register. */
c77c9766
RK
7522
7523 code = recog_memoized (insn);
7524
7525 if (code >= 0)
7526 {
0eadeb15 7527 extract_insn (insn);
c77c9766
RK
7528 /* We want constrain operands to treat this insn strictly in
7529 its validity determination, i.e., the way it would after reload
7530 has completed. */
0eadeb15 7531 if (constrain_operands (1))
4117a96b
R
7532 {
7533 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7534 REG_NOTES (insn)
9e6a5703 7535 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
4117a96b
R
7536 return insn;
7537 }
c77c9766
RK
7538 }
7539
7540 delete_insns_since (last);
7541
5c6b1bd2 7542 gen_reload (out, op1, opnum, type);
4117a96b 7543 insn = emit_insn (gen_add2_insn (out, op0));
9e6a5703 7544 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
32131a9c
RK
7545 }
7546
0dadecf6
RK
7547#ifdef SECONDARY_MEMORY_NEEDED
7548 /* If we need a memory location to do the move, do it that way. */
344b78b8
JH
7549 else if ((GET_CODE (in) == REG || GET_CODE (in) == SUBREG)
7550 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7551 && (GET_CODE (out) == REG || GET_CODE (out) == SUBREG)
7552 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7553 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7554 REGNO_REG_CLASS (reg_or_subregno (out)),
5e03c156 7555 GET_MODE (out)))
0dadecf6
RK
7556 {
7557 /* Get the memory to use and rewrite both registers to its mode. */
5e03c156 7558 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
0dadecf6 7559
5e03c156 7560 if (GET_MODE (loc) != GET_MODE (out))
38a448ca 7561 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
0dadecf6
RK
7562
7563 if (GET_MODE (loc) != GET_MODE (in))
38a448ca 7564 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
0dadecf6 7565
5c6b1bd2
RK
7566 gen_reload (loc, in, opnum, type);
7567 gen_reload (out, loc, opnum, type);
0dadecf6
RK
7568 }
7569#endif
7570
32131a9c
RK
7571 /* If IN is a simple operand, use gen_move_insn. */
7572 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
5e03c156 7573 emit_insn (gen_move_insn (out, in));
32131a9c
RK
7574
7575#ifdef HAVE_reload_load_address
7576 else if (HAVE_reload_load_address)
5e03c156 7577 emit_insn (gen_reload_load_address (out, in));
32131a9c
RK
7578#endif
7579
5e03c156 7580 /* Otherwise, just write (set OUT IN) and hope for the best. */
32131a9c 7581 else
38a448ca 7582 emit_insn (gen_rtx_SET (VOIDmode, out, in));
32131a9c
RK
7583
7584 /* Return the first insn emitted.
546b63fb 7585 We can not just return get_last_insn, because there may have
32131a9c
RK
7586 been multiple instructions emitted. Also note that gen_move_insn may
7587 emit more than one insn itself, so we can not assume that there is one
7588 insn emitted per emit_insn_before call. */
7589
546b63fb 7590 return last ? NEXT_INSN (last) : get_insns ();
32131a9c
RK
7591}
7592\f
cda94cbb
RH
7593/* Delete a previously made output-reload whose result we now believe
7594 is not needed. First we double-check.
32131a9c
RK
7595
7596 INSN is the insn now being processed.
cb2afeb3
R
7597 LAST_RELOAD_REG is the hard register number for which we want to delete
7598 the last output reload.
7599 J is the reload-number that originally used REG. The caller has made
7600 certain that reload J doesn't use REG any longer for input. */
32131a9c
RK
7601
7602static void
cb2afeb3 7603delete_output_reload (insn, j, last_reload_reg)
32131a9c
RK
7604 rtx insn;
7605 int j;
cb2afeb3 7606 int last_reload_reg;
32131a9c 7607{
cb2afeb3
R
7608 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7609 rtx reg = spill_reg_stored_to[last_reload_reg];
7610 int k;
7611 int n_occurrences;
7612 int n_inherited = 0;
b3694847 7613 rtx i1;
cb2afeb3 7614 rtx substed;
05d10675 7615
32131a9c
RK
7616 /* Get the raw pseudo-register referred to. */
7617
32131a9c
RK
7618 while (GET_CODE (reg) == SUBREG)
7619 reg = SUBREG_REG (reg);
cb2afeb3
R
7620 substed = reg_equiv_memory_loc[REGNO (reg)];
7621
7622 /* This is unsafe if the operand occurs more often in the current
7623 insn than it is inherited. */
7624 for (k = n_reloads - 1; k >= 0; k--)
7625 {
eceef4c9 7626 rtx reg2 = rld[k].in;
cb2afeb3
R
7627 if (! reg2)
7628 continue;
7629 if (GET_CODE (reg2) == MEM || reload_override_in[k])
eceef4c9 7630 reg2 = rld[k].in_reg;
cb2afeb3 7631#ifdef AUTO_INC_DEC
eceef4c9
BS
7632 if (rld[k].out && ! rld[k].out_reg)
7633 reg2 = XEXP (rld[k].in_reg, 0);
cb2afeb3
R
7634#endif
7635 while (GET_CODE (reg2) == SUBREG)
7636 reg2 = SUBREG_REG (reg2);
7637 if (rtx_equal_p (reg2, reg))
2eb6dac7
AS
7638 {
7639 if (reload_inherited[k] || reload_override_in[k] || k == j)
7640 {
cb2afeb3 7641 n_inherited++;
eceef4c9 7642 reg2 = rld[k].out_reg;
2eb6dac7
AS
7643 if (! reg2)
7644 continue;
7645 while (GET_CODE (reg2) == SUBREG)
7646 reg2 = XEXP (reg2, 0);
7647 if (rtx_equal_p (reg2, reg))
7648 n_inherited++;
7649 }
7650 else
7651 return;
7652 }
cb2afeb3 7653 }
4b983fdc 7654 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
cb2afeb3 7655 if (substed)
5d7ef82a
BS
7656 n_occurrences += count_occurrences (PATTERN (insn),
7657 eliminate_regs (substed, 0,
7658 NULL_RTX), 0);
cb2afeb3
R
7659 if (n_occurrences > n_inherited)
7660 return;
32131a9c
RK
7661
7662 /* If the pseudo-reg we are reloading is no longer referenced
7663 anywhere between the store into it and here,
7664 and no jumps or labels intervene, then the value can get
7665 here through the reload reg alone.
7666 Otherwise, give up--return. */
7667 for (i1 = NEXT_INSN (output_reload_insn);
7668 i1 != insn; i1 = NEXT_INSN (i1))
7669 {
7670 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7671 return;
7672 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7673 && reg_mentioned_p (reg, PATTERN (i1)))
aa6498c2 7674 {
cb2afeb3
R
7675 /* If this is USE in front of INSN, we only have to check that
7676 there are no more references than accounted for by inheritance. */
7677 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
aa6498c2 7678 {
cb2afeb3 7679 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
aa6498c2
R
7680 i1 = NEXT_INSN (i1);
7681 }
cb2afeb3 7682 if (n_occurrences <= n_inherited && i1 == insn)
aa6498c2
R
7683 break;
7684 return;
7685 }
32131a9c
RK
7686 }
7687
cda94cbb
RH
7688 /* We will be deleting the insn. Remove the spill reg information. */
7689 for (k = HARD_REGNO_NREGS (last_reload_reg, GET_MODE (reg)); k-- > 0; )
7690 {
7691 spill_reg_store[last_reload_reg + k] = 0;
7692 spill_reg_stored_to[last_reload_reg + k] = 0;
7693 }
7694
aa6498c2 7695 /* The caller has already checked that REG dies or is set in INSN.
cda94cbb
RH
7696 It has also checked that we are optimizing, and thus some
7697 inaccurancies in the debugging information are acceptable.
7698 So we could just delete output_reload_insn. But in some cases
7699 we can improve the debugging information without sacrificing
7700 optimization - maybe even improving the code: See if the pseudo
7701 reg has been completely replaced with reload regs. If so, delete
7702 the store insn and forget we had a stack slot for the pseudo. */
eceef4c9 7703 if (rld[j].out != rld[j].in
aa6498c2 7704 && REG_N_DEATHS (REGNO (reg)) == 1
a3a24aa6 7705 && REG_N_SETS (REGNO (reg)) == 1
aa6498c2
R
7706 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7707 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
32131a9c
RK
7708 {
7709 rtx i2;
7710
cda94cbb
RH
7711 /* We know that it was used only between here and the beginning of
7712 the current basic block. (We also know that the last use before
7713 INSN was the output reload we are thinking of deleting, but never
7714 mind that.) Search that range; see if any ref remains. */
32131a9c
RK
7715 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7716 {
d445b551
RK
7717 rtx set = single_set (i2);
7718
32131a9c
RK
7719 /* Uses which just store in the pseudo don't count,
7720 since if they are the only uses, they are dead. */
d445b551 7721 if (set != 0 && SET_DEST (set) == reg)
32131a9c
RK
7722 continue;
7723 if (GET_CODE (i2) == CODE_LABEL
7724 || GET_CODE (i2) == JUMP_INSN)
7725 break;
7726 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7727 && reg_mentioned_p (reg, PATTERN (i2)))
aa6498c2
R
7728 {
7729 /* Some other ref remains; just delete the output reload we
7730 know to be dead. */
cb2afeb3 7731 delete_address_reloads (output_reload_insn, insn);
ca6c03ca 7732 delete_insn (output_reload_insn);
aa6498c2
R
7733 return;
7734 }
32131a9c
RK
7735 }
7736
cda94cbb
RH
7737 /* Delete the now-dead stores into this pseudo. Note that this
7738 loop also takes care of deleting output_reload_insn. */
32131a9c
RK
7739 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7740 {
d445b551
RK
7741 rtx set = single_set (i2);
7742
7743 if (set != 0 && SET_DEST (set) == reg)
5507b94b 7744 {
cb2afeb3 7745 delete_address_reloads (i2, insn);
ca6c03ca 7746 delete_insn (i2);
5507b94b 7747 }
32131a9c
RK
7748 if (GET_CODE (i2) == CODE_LABEL
7749 || GET_CODE (i2) == JUMP_INSN)
7750 break;
7751 }
7752
cda94cbb 7753 /* For the debugging info, say the pseudo lives in this reload reg. */
eceef4c9 7754 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
32131a9c
RK
7755 alter_reg (REGNO (reg), -1);
7756 }
cda94cbb
RH
7757 else
7758 {
7759 delete_address_reloads (output_reload_insn, insn);
7760 delete_insn (output_reload_insn);
7761 }
cb2afeb3
R
7762}
7763
7764/* We are going to delete DEAD_INSN. Recursively delete loads of
7765 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7766 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7767static void
7768delete_address_reloads (dead_insn, current_insn)
7769 rtx dead_insn, current_insn;
7770{
7771 rtx set = single_set (dead_insn);
7772 rtx set2, dst, prev, next;
7773 if (set)
7774 {
7775 rtx dst = SET_DEST (set);
7776 if (GET_CODE (dst) == MEM)
7777 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
7778 }
7779 /* If we deleted the store from a reloaded post_{in,de}c expression,
7780 we can delete the matching adds. */
7781 prev = PREV_INSN (dead_insn);
7782 next = NEXT_INSN (dead_insn);
7783 if (! prev || ! next)
7784 return;
7785 set = single_set (next);
7786 set2 = single_set (prev);
7787 if (! set || ! set2
7788 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
7789 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
7790 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
7791 return;
7792 dst = SET_DEST (set);
7793 if (! rtx_equal_p (dst, SET_DEST (set2))
7794 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
7795 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
7796 || (INTVAL (XEXP (SET_SRC (set), 1))
1d7254c5 7797 != -INTVAL (XEXP (SET_SRC (set2), 1))))
cb2afeb3 7798 return;
53c17031
JH
7799 delete_related_insns (prev);
7800 delete_related_insns (next);
cb2afeb3
R
7801}
7802
7803/* Subfunction of delete_address_reloads: process registers found in X. */
7804static void
7805delete_address_reloads_1 (dead_insn, x, current_insn)
7806 rtx dead_insn, x, current_insn;
7807{
7808 rtx prev, set, dst, i2;
7809 int i, j;
7810 enum rtx_code code = GET_CODE (x);
7811
7812 if (code != REG)
7813 {
1d7254c5 7814 const char *fmt = GET_RTX_FORMAT (code);
cb2afeb3
R
7815 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7816 {
7817 if (fmt[i] == 'e')
7818 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
7819 else if (fmt[i] == 'E')
7820 {
1d7254c5 7821 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
cb2afeb3
R
7822 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
7823 current_insn);
7824 }
7825 }
7826 return;
7827 }
7828
7829 if (spill_reg_order[REGNO (x)] < 0)
7830 return;
aa6498c2 7831
cb2afeb3
R
7832 /* Scan backwards for the insn that sets x. This might be a way back due
7833 to inheritance. */
7834 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
7835 {
7836 code = GET_CODE (prev);
7837 if (code == CODE_LABEL || code == JUMP_INSN)
7838 return;
7839 if (GET_RTX_CLASS (code) != 'i')
7840 continue;
7841 if (reg_set_p (x, PATTERN (prev)))
7842 break;
7843 if (reg_referenced_p (x, PATTERN (prev)))
7844 return;
7845 }
7846 if (! prev || INSN_UID (prev) < reload_first_uid)
7847 return;
7848 /* Check that PREV only sets the reload register. */
7849 set = single_set (prev);
7850 if (! set)
7851 return;
7852 dst = SET_DEST (set);
7853 if (GET_CODE (dst) != REG
7854 || ! rtx_equal_p (dst, x))
7855 return;
7856 if (! reg_set_p (dst, PATTERN (dead_insn)))
7857 {
7858 /* Check if DST was used in a later insn -
7859 it might have been inherited. */
7860 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
7861 {
7862 if (GET_CODE (i2) == CODE_LABEL)
7863 break;
2c3c49de 7864 if (! INSN_P (i2))
cb2afeb3
R
7865 continue;
7866 if (reg_referenced_p (dst, PATTERN (i2)))
7867 {
7868 /* If there is a reference to the register in the current insn,
7869 it might be loaded in a non-inherited reload. If no other
7870 reload uses it, that means the register is set before
7871 referenced. */
7872 if (i2 == current_insn)
7873 {
7874 for (j = n_reloads - 1; j >= 0; j--)
eceef4c9 7875 if ((rld[j].reg_rtx == dst && reload_inherited[j])
cb2afeb3
R
7876 || reload_override_in[j] == dst)
7877 return;
7878 for (j = n_reloads - 1; j >= 0; j--)
eceef4c9 7879 if (rld[j].in && rld[j].reg_rtx == dst)
cb2afeb3
R
7880 break;
7881 if (j >= 0)
7882 break;
7883 }
7884 return;
7885 }
7886 if (GET_CODE (i2) == JUMP_INSN)
7887 break;
cb2afeb3 7888 /* If DST is still live at CURRENT_INSN, check if it is used for
3900dc09
R
7889 any reload. Note that even if CURRENT_INSN sets DST, we still
7890 have to check the reloads. */
cb2afeb3
R
7891 if (i2 == current_insn)
7892 {
7893 for (j = n_reloads - 1; j >= 0; j--)
eceef4c9 7894 if ((rld[j].reg_rtx == dst && reload_inherited[j])
cb2afeb3
R
7895 || reload_override_in[j] == dst)
7896 return;
7897 /* ??? We can't finish the loop here, because dst might be
7898 allocated to a pseudo in this block if no reload in this
7899 block needs any of the clsses containing DST - see
7900 spill_hard_reg. There is no easy way to tell this, so we
7901 have to scan till the end of the basic block. */
7902 }
3900dc09
R
7903 if (reg_set_p (dst, PATTERN (i2)))
7904 break;
cb2afeb3
R
7905 }
7906 }
7907 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
7908 reg_reloaded_contents[REGNO (dst)] = -1;
ca6c03ca 7909 delete_insn (prev);
32131a9c 7910}
32131a9c 7911\f
a8fdc208 7912/* Output reload-insns to reload VALUE into RELOADREG.
858a47b1 7913 VALUE is an autoincrement or autodecrement RTX whose operand
32131a9c
RK
7914 is a register or memory location;
7915 so reloading involves incrementing that location.
cb2afeb3 7916 IN is either identical to VALUE, or some cheaper place to reload from.
32131a9c
RK
7917
7918 INC_AMOUNT is the number to increment or decrement by (always positive).
cb2afeb3 7919 This cannot be deduced from VALUE.
32131a9c 7920
cb2afeb3
R
7921 Return the instruction that stores into RELOADREG. */
7922
7923static rtx
7924inc_for_reload (reloadreg, in, value, inc_amount)
32131a9c 7925 rtx reloadreg;
cb2afeb3 7926 rtx in, value;
32131a9c 7927 int inc_amount;
32131a9c
RK
7928{
7929 /* REG or MEM to be copied and incremented. */
7930 rtx incloc = XEXP (value, 0);
7931 /* Nonzero if increment after copying. */
7932 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
546b63fb 7933 rtx last;
0009eff2
RK
7934 rtx inc;
7935 rtx add_insn;
7936 int code;
cb2afeb3
R
7937 rtx store;
7938 rtx real_in = in == value ? XEXP (in, 0) : in;
32131a9c
RK
7939
7940 /* No hard register is equivalent to this register after
40f03658 7941 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
32131a9c
RK
7942 we could inc/dec that register as well (maybe even using it for
7943 the source), but I'm not sure it's worth worrying about. */
7944 if (GET_CODE (incloc) == REG)
7945 reg_last_reload_reg[REGNO (incloc)] = 0;
7946
7947 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
1d7254c5 7948 inc_amount = -inc_amount;
32131a9c 7949
fb3821f7 7950 inc = GEN_INT (inc_amount);
0009eff2
RK
7951
7952 /* If this is post-increment, first copy the location to the reload reg. */
cb2afeb3
R
7953 if (post && real_in != reloadreg)
7954 emit_insn (gen_move_insn (reloadreg, real_in));
0009eff2 7955
cb2afeb3
R
7956 if (in == value)
7957 {
7958 /* See if we can directly increment INCLOC. Use a method similar to
7959 that in gen_reload. */
0009eff2 7960
cb2afeb3
R
7961 last = get_last_insn ();
7962 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7963 gen_rtx_PLUS (GET_MODE (incloc),
7964 incloc, inc)));
05d10675 7965
cb2afeb3
R
7966 code = recog_memoized (add_insn);
7967 if (code >= 0)
32131a9c 7968 {
0eadeb15
BS
7969 extract_insn (add_insn);
7970 if (constrain_operands (1))
cb2afeb3
R
7971 {
7972 /* If this is a pre-increment and we have incremented the value
7973 where it lives, copy the incremented value to RELOADREG to
7974 be used as an address. */
0009eff2 7975
cb2afeb3
R
7976 if (! post)
7977 emit_insn (gen_move_insn (reloadreg, incloc));
546b63fb 7978
cb2afeb3
R
7979 return add_insn;
7980 }
32131a9c 7981 }
cb2afeb3 7982 delete_insns_since (last);
32131a9c 7983 }
0009eff2 7984
0009eff2
RK
7985 /* If couldn't do the increment directly, must increment in RELOADREG.
7986 The way we do this depends on whether this is pre- or post-increment.
7987 For pre-increment, copy INCLOC to the reload register, increment it
7988 there, then save back. */
7989
7990 if (! post)
7991 {
cb2afeb3
R
7992 if (in != reloadreg)
7993 emit_insn (gen_move_insn (reloadreg, real_in));
546b63fb 7994 emit_insn (gen_add2_insn (reloadreg, inc));
cb2afeb3 7995 store = emit_insn (gen_move_insn (incloc, reloadreg));
0009eff2 7996 }
32131a9c
RK
7997 else
7998 {
0009eff2
RK
7999 /* Postincrement.
8000 Because this might be a jump insn or a compare, and because RELOADREG
8001 may not be available after the insn in an input reload, we must do
8002 the incrementation before the insn being reloaded for.
8003
cb2afeb3 8004 We have already copied IN to RELOADREG. Increment the copy in
0009eff2
RK
8005 RELOADREG, save that back, then decrement RELOADREG so it has
8006 the original value. */
8007
546b63fb 8008 emit_insn (gen_add2_insn (reloadreg, inc));
cb2afeb3 8009 store = emit_insn (gen_move_insn (incloc, reloadreg));
546b63fb 8010 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
32131a9c 8011 }
0009eff2 8012
cb2afeb3 8013 return store;
32131a9c
RK
8014}
8015\f
2a9fb548 8016
eab5c70a 8017/* See whether a single set SET is a noop. */
2a9fb548 8018static int
eab5c70a
BS
8019reload_cse_noop_set_p (set)
8020 rtx set;
2a9fb548 8021{
eab5c70a
BS
8022 return rtx_equal_for_cselib_p (SET_DEST (set), SET_SRC (set));
8023}
2a9fb548 8024
eab5c70a
BS
8025/* Try to simplify INSN. */
8026static void
bf1660a6 8027reload_cse_simplify (insn, testreg)
eab5c70a 8028 rtx insn;
bf1660a6 8029 rtx testreg;
eab5c70a
BS
8030{
8031 rtx body = PATTERN (insn);
2a9fb548 8032
eab5c70a 8033 if (GET_CODE (body) == SET)
2a9fb548 8034 {
eab5c70a 8035 int count = 0;
d5ae21aa
AH
8036
8037 /* Simplify even if we may think it is a no-op.
8038 We may think a memory load of a value smaller than WORD_SIZE
8039 is redundant because we haven't taken into account possible
8040 implicit extension. reload_cse_simplify_set() will bring
8041 this out, so it's safer to simplify before we delete. */
8042 count += reload_cse_simplify_set (body, insn);
8043
8044 if (!count && reload_cse_noop_set_p (body))
2a9fb548 8045 {
eab5c70a 8046 rtx value = SET_DEST (body);
e0a09fda
KW
8047 if (REG_P (value)
8048 && ! REG_FUNCTION_VALUE_P (value))
eab5c70a 8049 value = 0;
10d1bb36 8050 delete_insn_and_edges (insn);
eab5c70a 8051 return;
2a9fb548 8052 }
2a9fb548 8053
eab5c70a
BS
8054 if (count > 0)
8055 apply_change_group ();
8056 else
bf1660a6 8057 reload_cse_simplify_operands (insn, testreg);
eab5c70a
BS
8058 }
8059 else if (GET_CODE (body) == PARALLEL)
2a9fb548 8060 {
eab5c70a
BS
8061 int i;
8062 int count = 0;
8063 rtx value = NULL_RTX;
2a9fb548 8064
eab5c70a
BS
8065 /* If every action in a PARALLEL is a noop, we can delete
8066 the entire PARALLEL. */
8067 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2a9fb548 8068 {
eab5c70a
BS
8069 rtx part = XVECEXP (body, 0, i);
8070 if (GET_CODE (part) == SET)
2a9fb548 8071 {
eab5c70a
BS
8072 if (! reload_cse_noop_set_p (part))
8073 break;
cf87d551
HPN
8074 if (REG_P (SET_DEST (part))
8075 && REG_FUNCTION_VALUE_P (SET_DEST (part)))
2a9fb548 8076 {
eab5c70a
BS
8077 if (value)
8078 break;
8079 value = SET_DEST (part);
2a9fb548 8080 }
2a9fb548 8081 }
eab5c70a
BS
8082 else if (GET_CODE (part) != CLOBBER)
8083 break;
2a9fb548 8084 }
2a9fb548 8085
eab5c70a
BS
8086 if (i < 0)
8087 {
10d1bb36 8088 delete_insn_and_edges (insn);
eab5c70a
BS
8089 /* We're done with this insn. */
8090 return;
8091 }
2a9fb548 8092
eab5c70a
BS
8093 /* It's not a no-op, but we can try to simplify it. */
8094 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
8095 if (GET_CODE (XVECEXP (body, 0, i)) == SET)
8096 count += reload_cse_simplify_set (XVECEXP (body, 0, i), insn);
8097
8098 if (count > 0)
8099 apply_change_group ();
8100 else
bf1660a6 8101 reload_cse_simplify_operands (insn, testreg);
eab5c70a 8102 }
2a9fb548
ILT
8103}
8104
8105/* Do a very simple CSE pass over the hard registers.
8106
8107 This function detects no-op moves where we happened to assign two
8108 different pseudo-registers to the same hard register, and then
8109 copied one to the other. Reload will generate a useless
8110 instruction copying a register to itself.
8111
8112 This function also detects cases where we load a value from memory
8113 into two different registers, and (if memory is more expensive than
8114 registers) changes it to simply copy the first register into the
05d10675 8115 second register.
e9a25f70
JL
8116
8117 Another optimization is performed that scans the operands of each
8118 instruction to see whether the value is already available in a
8119 hard register. It then replaces the operand with the hard register
8120 if possible, much like an optional reload would. */
2a9fb548 8121
5adf6da0
R
8122static void
8123reload_cse_regs_1 (first)
2a9fb548
ILT
8124 rtx first;
8125{
2a9fb548 8126 rtx insn;
bf1660a6 8127 rtx testreg = gen_rtx_REG (VOIDmode, -1);
2a9fb548 8128
1d7254c5 8129 cselib_init ();
cbfc3ad3
RK
8130 init_alias_analysis ();
8131
2a9fb548
ILT
8132 for (insn = first; insn; insn = NEXT_INSN (insn))
8133 {
2c3c49de 8134 if (INSN_P (insn))
bf1660a6 8135 reload_cse_simplify (insn, testreg);
2a9fb548 8136
eab5c70a 8137 cselib_process_insn (insn);
2a9fb548
ILT
8138 }
8139
e05e2395
MM
8140 /* Clean up. */
8141 end_alias_analysis ();
eab5c70a 8142 cselib_finish ();
2a9fb548
ILT
8143}
8144
5adf6da0
R
8145/* Call cse / combine like post-reload optimization phases.
8146 FIRST is the first instruction. */
8147void
8148reload_cse_regs (first)
8149 rtx first;
8150{
8151 reload_cse_regs_1 (first);
8152 reload_combine ();
8153 reload_cse_move2add (first);
8154 if (flag_expensive_optimizations)
8155 reload_cse_regs_1 (first);
8156}
8157
2a9fb548 8158/* Try to simplify a single SET instruction. SET is the set pattern.
e9a25f70
JL
8159 INSN is the instruction it came from.
8160 This function only handles one case: if we set a register to a value
8161 which is not a register, we try to find that value in some other register
8162 and change the set into a register copy. */
2a9fb548 8163
e9a25f70 8164static int
2a9fb548
ILT
8165reload_cse_simplify_set (set, insn)
8166 rtx set;
8167 rtx insn;
8168{
eab5c70a 8169 int did_change = 0;
2a9fb548
ILT
8170 int dreg;
8171 rtx src;
2a9fb548 8172 enum reg_class dclass;
eab5c70a
BS
8173 int old_cost;
8174 cselib_val *val;
8175 struct elt_loc_list *l;
78adc5a0
RH
8176#ifdef LOAD_EXTEND_OP
8177 enum rtx_code extend_op = NIL;
8178#endif
2a9fb548 8179
2a9fb548
ILT
8180 dreg = true_regnum (SET_DEST (set));
8181 if (dreg < 0)
e9a25f70 8182 return 0;
2a9fb548
ILT
8183
8184 src = SET_SRC (set);
8185 if (side_effects_p (src) || true_regnum (src) >= 0)
e9a25f70 8186 return 0;
2a9fb548 8187
cbd5b9a2
KR
8188 dclass = REGNO_REG_CLASS (dreg);
8189
78adc5a0
RH
8190#ifdef LOAD_EXTEND_OP
8191 /* When replacing a memory with a register, we need to honor assumptions
8192 that combine made wrt the contents of sign bits. We'll do this by
a6a2274a 8193 generating an extend instruction instead of a reg->reg copy. Thus
78adc5a0
RH
8194 the destination must be a register that we can widen. */
8195 if (GET_CODE (src) == MEM
8196 && GET_MODE_BITSIZE (GET_MODE (src)) < BITS_PER_WORD
8197 && (extend_op = LOAD_EXTEND_OP (GET_MODE (src))) != NIL
8198 && GET_CODE (SET_DEST (set)) != REG)
8199 return 0;
8200#endif
8201
33ab8de0 8202 /* If memory loads are cheaper than register copies, don't change them. */
eab5c70a
BS
8203 if (GET_CODE (src) == MEM)
8204 old_cost = MEMORY_MOVE_COST (GET_MODE (src), dclass, 1);
8205 else if (CONSTANT_P (src))
8206 old_cost = rtx_cost (src, SET);
8207 else if (GET_CODE (src) == REG)
e56b4594
AO
8208 old_cost = REGISTER_MOVE_COST (GET_MODE (src),
8209 REGNO_REG_CLASS (REGNO (src)), dclass);
eab5c70a
BS
8210 else
8211 /* ??? */
8212 old_cost = rtx_cost (src, SET);
2a9fb548 8213
70bbeb8b 8214 val = cselib_lookup (src, GET_MODE (SET_DEST (set)), 0);
eab5c70a 8215 if (! val)
0254c561 8216 return 0;
eab5c70a 8217 for (l = val->locs; l; l = l->next)
2a9fb548 8218 {
78adc5a0 8219 rtx this_rtx = l->loc;
eab5c70a 8220 int this_cost;
78adc5a0
RH
8221
8222 if (CONSTANT_P (this_rtx) && ! references_value_p (this_rtx, 0))
8223 {
8224#ifdef LOAD_EXTEND_OP
8225 if (extend_op != NIL)
8226 {
8227 HOST_WIDE_INT this_val;
8228
8229 /* ??? I'm lazy and don't wish to handle CONST_DOUBLE. Other
8230 constants, such as SYMBOL_REF, cannot be extended. */
8231 if (GET_CODE (this_rtx) != CONST_INT)
8232 continue;
8233
8234 this_val = INTVAL (this_rtx);
8235 switch (extend_op)
8236 {
8237 case ZERO_EXTEND:
8238 this_val &= GET_MODE_MASK (GET_MODE (src));
8239 break;
8240 case SIGN_EXTEND:
8241 /* ??? In theory we're already extended. */
8242 if (this_val == trunc_int_for_mode (this_val, GET_MODE (src)))
8243 break;
8244 default:
8245 abort ();
8246 }
5cada064 8247 this_rtx = GEN_INT (this_val);
78adc5a0
RH
8248 }
8249#endif
8250 this_cost = rtx_cost (this_rtx, SET);
8251 }
8252 else if (GET_CODE (this_rtx) == REG)
8253 {
8254#ifdef LOAD_EXTEND_OP
8255 if (extend_op != NIL)
8256 {
8257 this_rtx = gen_rtx_fmt_e (extend_op, word_mode, this_rtx);
8258 this_cost = rtx_cost (this_rtx, SET);
8259 }
8260 else
8261#endif
8262 this_cost = REGISTER_MOVE_COST (GET_MODE (this_rtx),
8263 REGNO_REG_CLASS (REGNO (this_rtx)),
8264 dclass);
8265 }
eab5c70a
BS
8266 else
8267 continue;
78adc5a0
RH
8268
8269 /* If equal costs, prefer registers over anything else. That
8270 tends to lead to smaller instructions on some machines. */
8271 if (this_cost < old_cost
8272 || (this_cost == old_cost
8273 && GET_CODE (this_rtx) == REG
8274 && GET_CODE (SET_SRC (set)) != REG))
8275 {
8276#ifdef LOAD_EXTEND_OP
b216e516
JL
8277 if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (set))) < BITS_PER_WORD
8278 && extend_op != NIL)
8279 {
8280 rtx wide_dest = gen_rtx_REG (word_mode, REGNO (SET_DEST (set)));
8281 ORIGINAL_REGNO (wide_dest) = ORIGINAL_REGNO (SET_DEST (set));
8282 validate_change (insn, &SET_DEST (set), wide_dest, 1);
8283 }
78adc5a0
RH
8284#endif
8285
8286 validate_change (insn, &SET_SRC (set), copy_rtx (this_rtx), 1);
8287 old_cost = this_cost, did_change = 1;
8288 }
e9a25f70 8289 }
eab5c70a
BS
8290
8291 return did_change;
e9a25f70
JL
8292}
8293
8294/* Try to replace operands in INSN with equivalent values that are already
05d10675
BS
8295 in registers. This can be viewed as optional reloading.
8296
e9a25f70
JL
8297 For each non-register operand in the insn, see if any hard regs are
8298 known to be equivalent to that operand. Record the alternatives which
8299 can accept these hard registers. Among all alternatives, select the
8300 ones which are better or equal to the one currently matching, where
8301 "better" is in terms of '?' and '!' constraints. Among the remaining
8302 alternatives, select the one which replaces most operands with
8303 hard registers. */
8304
8305static int
bf1660a6 8306reload_cse_simplify_operands (insn, testreg)
e9a25f70 8307 rtx insn;
bf1660a6 8308 rtx testreg;
e9a25f70 8309{
1d7254c5 8310 int i, j;
e9a25f70 8311
eab5c70a
BS
8312 /* For each operand, all registers that are equivalent to it. */
8313 HARD_REG_SET equiv_regs[MAX_RECOG_OPERANDS];
8314
9b3142b3 8315 const char *constraints[MAX_RECOG_OPERANDS];
05d10675 8316
e9a25f70
JL
8317 /* Vector recording how bad an alternative is. */
8318 int *alternative_reject;
8319 /* Vector recording how many registers can be introduced by choosing
8320 this alternative. */
8321 int *alternative_nregs;
8322 /* Array of vectors recording, for each operand and each alternative,
8323 which hard register to substitute, or -1 if the operand should be
8324 left as it is. */
8325 int *op_alt_regno[MAX_RECOG_OPERANDS];
8326 /* Array of alternatives, sorted in order of decreasing desirability. */
8327 int *alternative_order;
05d10675 8328
0eadeb15 8329 extract_insn (insn);
e9a25f70 8330
1ccbefce 8331 if (recog_data.n_alternatives == 0 || recog_data.n_operands == 0)
1d300e19 8332 return 0;
e9a25f70
JL
8333
8334 /* Figure out which alternative currently matches. */
0eadeb15 8335 if (! constrain_operands (1))
b8705408 8336 fatal_insn_not_found (insn);
1d7254c5 8337
1ccbefce
RH
8338 alternative_reject = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8339 alternative_nregs = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8340 alternative_order = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8e2e89f7
KH
8341 memset ((char *) alternative_reject, 0, recog_data.n_alternatives * sizeof (int));
8342 memset ((char *) alternative_nregs, 0, recog_data.n_alternatives * sizeof (int));
e9a25f70 8343
eab5c70a
BS
8344 /* For each operand, find out which regs are equivalent. */
8345 for (i = 0; i < recog_data.n_operands; i++)
8346 {
8347 cselib_val *v;
8348 struct elt_loc_list *l;
8349
8350 CLEAR_HARD_REG_SET (equiv_regs[i]);
8351
8352 /* cselib blows up on CODE_LABELs. Trying to fix that doesn't seem
70bbeb8b
BS
8353 right, so avoid the problem here. Likewise if we have a constant
8354 and the insn pattern doesn't tell us the mode we need. */
8355 if (GET_CODE (recog_data.operand[i]) == CODE_LABEL
8356 || (CONSTANT_P (recog_data.operand[i])
8357 && recog_data.operand_mode[i] == VOIDmode))
eab5c70a
BS
8358 continue;
8359
8360 v = cselib_lookup (recog_data.operand[i], recog_data.operand_mode[i], 0);
8361 if (! v)
8362 continue;
8363
8364 for (l = v->locs; l; l = l->next)
8365 if (GET_CODE (l->loc) == REG)
8366 SET_HARD_REG_BIT (equiv_regs[i], REGNO (l->loc));
8367 }
8368
1ccbefce 8369 for (i = 0; i < recog_data.n_operands; i++)
e9a25f70
JL
8370 {
8371 enum machine_mode mode;
8372 int regno;
9b3142b3 8373 const char *p;
e9a25f70 8374
1ccbefce
RH
8375 op_alt_regno[i] = (int *) alloca (recog_data.n_alternatives * sizeof (int));
8376 for (j = 0; j < recog_data.n_alternatives; j++)
e9a25f70
JL
8377 op_alt_regno[i][j] = -1;
8378
1ccbefce
RH
8379 p = constraints[i] = recog_data.constraints[i];
8380 mode = recog_data.operand_mode[i];
e9a25f70
JL
8381
8382 /* Add the reject values for each alternative given by the constraints
8383 for this operand. */
8384 j = 0;
8385 while (*p != '\0')
8386 {
8387 char c = *p++;
8388 if (c == ',')
8389 j++;
8390 else if (c == '?')
8391 alternative_reject[j] += 3;
8392 else if (c == '!')
8393 alternative_reject[j] += 300;
8394 }
8395
8396 /* We won't change operands which are already registers. We
8397 also don't want to modify output operands. */
1ccbefce 8398 regno = true_regnum (recog_data.operand[i]);
e9a25f70
JL
8399 if (regno >= 0
8400 || constraints[i][0] == '='
8401 || constraints[i][0] == '+')
8402 continue;
8403
8404 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8405 {
8406 int class = (int) NO_REGS;
8407
eab5c70a 8408 if (! TEST_HARD_REG_BIT (equiv_regs[i], regno))
e9a25f70
JL
8409 continue;
8410
bf1660a6
JL
8411 REGNO (testreg) = regno;
8412 PUT_MODE (testreg, mode);
0254c561 8413
e9a25f70
JL
8414 /* We found a register equal to this operand. Now look for all
8415 alternatives that can accept this register and have not been
8416 assigned a register they can use yet. */
8417 j = 0;
8418 p = constraints[i];
8419 for (;;)
31418d35 8420 {
e9a25f70 8421 char c = *p++;
05d10675 8422
e9a25f70 8423 switch (c)
31418d35 8424 {
e9a25f70
JL
8425 case '=': case '+': case '?':
8426 case '#': case '&': case '!':
05d10675 8427 case '*': case '%':
e9a25f70 8428 case '0': case '1': case '2': case '3': case '4':
c5c76735 8429 case '5': case '6': case '7': case '8': case '9':
e9a25f70
JL
8430 case 'm': case '<': case '>': case 'V': case 'o':
8431 case 'E': case 'F': case 'G': case 'H':
8432 case 's': case 'i': case 'n':
8433 case 'I': case 'J': case 'K': case 'L':
8434 case 'M': case 'N': case 'O': case 'P':
e9a25f70
JL
8435 case 'p': case 'X':
8436 /* These don't say anything we care about. */
8437 break;
8438
8439 case 'g': case 'r':
8440 class = reg_class_subunion[(int) class][(int) GENERAL_REGS];
8441 break;
8442
8443 default:
8444 class
8e2e89f7 8445 = reg_class_subunion[(int) class][(int) REG_CLASS_FROM_LETTER ((unsigned char) c)];
e9a25f70 8446 break;
31418d35 8447
e9a25f70
JL
8448 case ',': case '\0':
8449 /* See if REGNO fits this alternative, and set it up as the
8450 replacement register if we don't have one for this
0254c561 8451 alternative yet and the operand being replaced is not
1d7254c5 8452 a cheap CONST_INT. */
e9a25f70 8453 if (op_alt_regno[i][j] == -1
bf1660a6 8454 && reg_fits_class_p (testreg, class, 0, mode)
1ccbefce
RH
8455 && (GET_CODE (recog_data.operand[i]) != CONST_INT
8456 || (rtx_cost (recog_data.operand[i], SET)
bf1660a6 8457 > rtx_cost (testreg, SET))))
31418d35 8458 {
e9a25f70
JL
8459 alternative_nregs[j]++;
8460 op_alt_regno[i][j] = regno;
31418d35 8461 }
e9a25f70
JL
8462 j++;
8463 break;
31418d35
ILT
8464 }
8465
e9a25f70
JL
8466 if (c == '\0')
8467 break;
8468 }
8469 }
8470 }
8471
8472 /* Record all alternatives which are better or equal to the currently
8473 matching one in the alternative_order array. */
1ccbefce 8474 for (i = j = 0; i < recog_data.n_alternatives; i++)
e9a25f70
JL
8475 if (alternative_reject[i] <= alternative_reject[which_alternative])
8476 alternative_order[j++] = i;
1ccbefce 8477 recog_data.n_alternatives = j;
e9a25f70
JL
8478
8479 /* Sort it. Given a small number of alternatives, a dumb algorithm
8480 won't hurt too much. */
1ccbefce 8481 for (i = 0; i < recog_data.n_alternatives - 1; i++)
e9a25f70
JL
8482 {
8483 int best = i;
8484 int best_reject = alternative_reject[alternative_order[i]];
8485 int best_nregs = alternative_nregs[alternative_order[i]];
8486 int tmp;
8487
1ccbefce 8488 for (j = i + 1; j < recog_data.n_alternatives; j++)
e9a25f70
JL
8489 {
8490 int this_reject = alternative_reject[alternative_order[j]];
8491 int this_nregs = alternative_nregs[alternative_order[j]];
8492
8493 if (this_reject < best_reject
8494 || (this_reject == best_reject && this_nregs < best_nregs))
8495 {
8496 best = j;
8497 best_reject = this_reject;
8498 best_nregs = this_nregs;
31418d35 8499 }
2a9fb548 8500 }
05d10675 8501
e9a25f70
JL
8502 tmp = alternative_order[best];
8503 alternative_order[best] = alternative_order[i];
8504 alternative_order[i] = tmp;
8505 }
05d10675 8506
e9a25f70
JL
8507 /* Substitute the operands as determined by op_alt_regno for the best
8508 alternative. */
8509 j = alternative_order[0];
e9a25f70 8510
1ccbefce 8511 for (i = 0; i < recog_data.n_operands; i++)
e9a25f70 8512 {
1ccbefce 8513 enum machine_mode mode = recog_data.operand_mode[i];
e9a25f70
JL
8514 if (op_alt_regno[i][j] == -1)
8515 continue;
8516
1ccbefce 8517 validate_change (insn, recog_data.operand_loc[i],
38a448ca 8518 gen_rtx_REG (mode, op_alt_regno[i][j]), 1);
e9a25f70
JL
8519 }
8520
1ccbefce 8521 for (i = recog_data.n_dups - 1; i >= 0; i--)
e9a25f70 8522 {
1ccbefce
RH
8523 int op = recog_data.dup_num[i];
8524 enum machine_mode mode = recog_data.operand_mode[op];
e9a25f70
JL
8525
8526 if (op_alt_regno[op][j] == -1)
8527 continue;
8528
1ccbefce 8529 validate_change (insn, recog_data.dup_loc[i],
38a448ca 8530 gen_rtx_REG (mode, op_alt_regno[op][j]), 1);
2a9fb548 8531 }
e9a25f70 8532
e9a25f70 8533 return apply_change_group ();
2a9fb548 8534}
5adf6da0
R
8535\f
8536/* If reload couldn't use reg+reg+offset addressing, try to use reg+reg
8537 addressing now.
8538 This code might also be useful when reload gave up on reg+reg addresssing
8539 because of clashes between the return register and INDEX_REG_CLASS. */
8540
8541/* The maximum number of uses of a register we can keep track of to
8542 replace them with reg+reg addressing. */
8543#define RELOAD_COMBINE_MAX_USES 6
8544
8545/* INSN is the insn where a register has ben used, and USEP points to the
8546 location of the register within the rtl. */
8547struct reg_use { rtx insn, *usep; };
8548
8549/* If the register is used in some unknown fashion, USE_INDEX is negative.
8550 If it is dead, USE_INDEX is RELOAD_COMBINE_MAX_USES, and STORE_RUID
8551 indicates where it becomes live again.
8552 Otherwise, USE_INDEX is the index of the last encountered use of the
8553 register (which is first among these we have seen since we scan backwards),
8554 OFFSET contains the constant offset that is added to the register in
8555 all encountered uses, and USE_RUID indicates the first encountered, i.e.
ed937a19
R
8556 last, of these uses.
8557 STORE_RUID is always meaningful if we only want to use a value in a
8558 register in a different place: it denotes the next insn in the insn
8559 stream (i.e. the last ecountered) that sets or clobbers the register. */
5adf6da0
R
8560static struct
8561 {
8562 struct reg_use reg_use[RELOAD_COMBINE_MAX_USES];
8563 int use_index;
8564 rtx offset;
8565 int store_ruid;
8566 int use_ruid;
8567 } reg_state[FIRST_PSEUDO_REGISTER];
8568
8569/* Reverse linear uid. This is increased in reload_combine while scanning
8570 the instructions from last to first. It is used to set last_label_ruid
8571 and the store_ruid / use_ruid fields in reg_state. */
8572static int reload_combine_ruid;
8573
b0634509
R
8574#define LABEL_LIVE(LABEL) \
8575 (label_live[CODE_LABEL_NUMBER (LABEL) - min_labelno])
8576
5adf6da0
R
8577static void
8578reload_combine ()
8579{
8580 rtx insn, set;
ae0ed63a
JM
8581 int first_index_reg = -1;
8582 int last_index_reg = 0;
5adf6da0 8583 int i;
e0082a72 8584 basic_block bb;
f8cd4126 8585 unsigned int r;
5adf6da0 8586 int last_label_ruid;
b0634509
R
8587 int min_labelno, n_labels;
8588 HARD_REG_SET ever_live_at_start, *label_live;
5adf6da0 8589
f63d1bf7 8590 /* If reg+reg can be used in offsetable memory addresses, the main chunk of
5adf6da0
R
8591 reload has already used it where appropriate, so there is no use in
8592 trying to generate it now. */
03acd8f8 8593 if (double_reg_address_ok && INDEX_REG_CLASS != NO_REGS)
5adf6da0
R
8594 return;
8595
8596 /* To avoid wasting too much time later searching for an index register,
8597 determine the minimum and maximum index register numbers. */
f8cd4126
RK
8598 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8599 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], r))
8600 {
881a8969 8601 if (first_index_reg == -1)
4c3f1588
RK
8602 first_index_reg = r;
8603
8604 last_index_reg = r;
f8cd4126
RK
8605 }
8606
5adf6da0 8607 /* If no index register is available, we can quit now. */
881a8969 8608 if (first_index_reg == -1)
5adf6da0
R
8609 return;
8610
b0634509
R
8611 /* Set up LABEL_LIVE and EVER_LIVE_AT_START. The register lifetime
8612 information is a bit fuzzy immediately after reload, but it's
8613 still good enough to determine which registers are live at a jump
8614 destination. */
8615 min_labelno = get_first_label_num ();
8616 n_labels = max_label_num () - min_labelno;
8617 label_live = (HARD_REG_SET *) xmalloc (n_labels * sizeof (HARD_REG_SET));
8618 CLEAR_HARD_REG_SET (ever_live_at_start);
f8cd4126 8619
e0082a72 8620 FOR_EACH_BB_REVERSE (bb)
b0634509 8621 {
e0082a72 8622 insn = bb->head;
b0634509
R
8623 if (GET_CODE (insn) == CODE_LABEL)
8624 {
8625 HARD_REG_SET live;
8626
f8cd4126 8627 REG_SET_TO_HARD_REG_SET (live,
e0082a72 8628 bb->global_live_at_start);
f8cd4126 8629 compute_use_by_pseudos (&live,
e0082a72 8630 bb->global_live_at_start);
b0634509
R
8631 COPY_HARD_REG_SET (LABEL_LIVE (insn), live);
8632 IOR_HARD_REG_SET (ever_live_at_start, live);
8633 }
8634 }
8635
5adf6da0
R
8636 /* Initialize last_label_ruid, reload_combine_ruid and reg_state. */
8637 last_label_ruid = reload_combine_ruid = 0;
f8cd4126 8638 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
5adf6da0 8639 {
f8cd4126
RK
8640 reg_state[r].store_ruid = reload_combine_ruid;
8641 if (fixed_regs[r])
8642 reg_state[r].use_index = -1;
5adf6da0 8643 else
f8cd4126 8644 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
5adf6da0
R
8645 }
8646
8647 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
8648 {
8649 rtx note;
8650
8651 /* We cannot do our optimization across labels. Invalidating all the use
8652 information we have would be costly, so we just note where the label
05d10675 8653 is and then later disable any optimization that would cross it. */
5adf6da0
R
8654 if (GET_CODE (insn) == CODE_LABEL)
8655 last_label_ruid = reload_combine_ruid;
f8cd4126
RK
8656 else if (GET_CODE (insn) == BARRIER)
8657 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8658 if (! fixed_regs[r])
8659 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8660
2c3c49de 8661 if (! INSN_P (insn))
5adf6da0 8662 continue;
f8cd4126 8663
5adf6da0
R
8664 reload_combine_ruid++;
8665
8666 /* Look for (set (REGX) (CONST_INT))
eceef4c9
BS
8667 (set (REGX) (PLUS (REGX) (REGY)))
8668 ...
8669 ... (MEM (REGX)) ...
5adf6da0 8670 and convert it to
eceef4c9
BS
8671 (set (REGZ) (CONST_INT))
8672 ...
8673 ... (MEM (PLUS (REGZ) (REGY)))... .
5adf6da0
R
8674
8675 First, check that we have (set (REGX) (PLUS (REGX) (REGY)))
8676 and that we know all uses of REGX before it dies. */
2abbc1bd
R
8677 set = single_set (insn);
8678 if (set != NULL_RTX
5adf6da0
R
8679 && GET_CODE (SET_DEST (set)) == REG
8680 && (HARD_REGNO_NREGS (REGNO (SET_DEST (set)),
8681 GET_MODE (SET_DEST (set)))
8682 == 1)
8683 && GET_CODE (SET_SRC (set)) == PLUS
8684 && GET_CODE (XEXP (SET_SRC (set), 1)) == REG
8685 && rtx_equal_p (XEXP (SET_SRC (set), 0), SET_DEST (set))
8686 && last_label_ruid < reg_state[REGNO (SET_DEST (set))].use_ruid)
8687 {
8688 rtx reg = SET_DEST (set);
8689 rtx plus = SET_SRC (set);
8690 rtx base = XEXP (plus, 1);
8691 rtx prev = prev_nonnote_insn (insn);
8692 rtx prev_set = prev ? single_set (prev) : NULL_RTX;
f8cd4126 8693 unsigned int regno = REGNO (reg);
6a651371 8694 rtx const_reg = NULL_RTX;
5adf6da0
R
8695 rtx reg_sum = NULL_RTX;
8696
8697 /* Now, we need an index register.
8698 We'll set index_reg to this index register, const_reg to the
8699 register that is to be loaded with the constant
8700 (denoted as REGZ in the substitution illustration above),
8701 and reg_sum to the register-register that we want to use to
8702 substitute uses of REG (typically in MEMs) with.
8703 First check REG and BASE for being index registers;
8704 we can use them even if they are not dead. */
8705 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS], regno)
8706 || TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
8707 REGNO (base)))
8708 {
8709 const_reg = reg;
8710 reg_sum = plus;
8711 }
8712 else
8713 {
05d10675
BS
8714 /* Otherwise, look for a free index register. Since we have
8715 checked above that neiter REG nor BASE are index registers,
8716 if we find anything at all, it will be different from these
8717 two registers. */
8718 for (i = first_index_reg; i <= last_index_reg; i++)
5adf6da0 8719 {
f8cd4126
RK
8720 if (TEST_HARD_REG_BIT (reg_class_contents[INDEX_REG_CLASS],
8721 i)
5adf6da0
R
8722 && reg_state[i].use_index == RELOAD_COMBINE_MAX_USES
8723 && reg_state[i].store_ruid <= reg_state[regno].use_ruid
8724 && HARD_REGNO_NREGS (i, GET_MODE (reg)) == 1)
8725 {
8726 rtx index_reg = gen_rtx_REG (GET_MODE (reg), i);
f8cd4126 8727
5adf6da0
R
8728 const_reg = index_reg;
8729 reg_sum = gen_rtx_PLUS (GET_MODE (reg), index_reg, base);
8730 break;
8731 }
8732 }
8733 }
f8cd4126 8734
ed937a19
R
8735 /* Check that PREV_SET is indeed (set (REGX) (CONST_INT)) and that
8736 (REGY), i.e. BASE, is not clobbered before the last use we'll
8737 create. */
f8cd4126 8738 if (prev_set != 0
5adf6da0
R
8739 && GET_CODE (SET_SRC (prev_set)) == CONST_INT
8740 && rtx_equal_p (SET_DEST (prev_set), reg)
8741 && reg_state[regno].use_index >= 0
f8cd4126
RK
8742 && (reg_state[REGNO (base)].store_ruid
8743 <= reg_state[regno].use_ruid)
8744 && reg_sum != 0)
5adf6da0
R
8745 {
8746 int i;
8747
f8cd4126 8748 /* Change destination register and, if necessary, the
5adf6da0
R
8749 constant value in PREV, the constant loading instruction. */
8750 validate_change (prev, &SET_DEST (prev_set), const_reg, 1);
8751 if (reg_state[regno].offset != const0_rtx)
8752 validate_change (prev,
8753 &SET_SRC (prev_set),
8754 GEN_INT (INTVAL (SET_SRC (prev_set))
8755 + INTVAL (reg_state[regno].offset)),
8756 1);
f8cd4126 8757
5adf6da0
R
8758 /* Now for every use of REG that we have recorded, replace REG
8759 with REG_SUM. */
8760 for (i = reg_state[regno].use_index;
8761 i < RELOAD_COMBINE_MAX_USES; i++)
8762 validate_change (reg_state[regno].reg_use[i].insn,
8763 reg_state[regno].reg_use[i].usep,
d300f51f
HPN
8764 /* Each change must have its own
8765 replacement. */
8766 copy_rtx (reg_sum), 1);
5adf6da0
R
8767
8768 if (apply_change_group ())
8769 {
8770 rtx *np;
8771
8772 /* Delete the reg-reg addition. */
ca6c03ca 8773 delete_insn (insn);
5adf6da0
R
8774
8775 if (reg_state[regno].offset != const0_rtx)
f8cd4126
RK
8776 /* Previous REG_EQUIV / REG_EQUAL notes for PREV
8777 are now invalid. */
1d7254c5 8778 for (np = &REG_NOTES (prev); *np;)
f8cd4126
RK
8779 {
8780 if (REG_NOTE_KIND (*np) == REG_EQUAL
8781 || REG_NOTE_KIND (*np) == REG_EQUIV)
8782 *np = XEXP (*np, 1);
8783 else
8784 np = &XEXP (*np, 1);
8785 }
8786
5adf6da0 8787 reg_state[regno].use_index = RELOAD_COMBINE_MAX_USES;
f8cd4126
RK
8788 reg_state[REGNO (const_reg)].store_ruid
8789 = reload_combine_ruid;
5adf6da0
R
8790 continue;
8791 }
8792 }
8793 }
f8cd4126 8794
1d7254c5 8795 note_stores (PATTERN (insn), reload_combine_note_store, NULL);
f8cd4126 8796
5adf6da0
R
8797 if (GET_CODE (insn) == CALL_INSN)
8798 {
8799 rtx link;
8800
f8cd4126
RK
8801 for (r = 0; r < FIRST_PSEUDO_REGISTER; r++)
8802 if (call_used_regs[r])
8803 {
8804 reg_state[r].use_index = RELOAD_COMBINE_MAX_USES;
8805 reg_state[r].store_ruid = reload_combine_ruid;
8806 }
8807
5adf6da0
R
8808 for (link = CALL_INSN_FUNCTION_USAGE (insn); link;
8809 link = XEXP (link, 1))
6a69653a
CM
8810 {
8811 rtx usage_rtx = XEXP (XEXP (link, 0), 0);
8812 if (GET_CODE (usage_rtx) == REG)
8813 {
ae0ed63a 8814 unsigned int i;
6a69653a
CM
8815 unsigned int start_reg = REGNO (usage_rtx);
8816 unsigned int num_regs =
8817 HARD_REGNO_NREGS (start_reg, GET_MODE (usage_rtx));
8818 unsigned int end_reg = start_reg + num_regs - 1;
8819 for (i = start_reg; i <= end_reg; i++)
8820 if (GET_CODE (XEXP (link, 0)) == CLOBBER)
8821 {
8822 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
8823 reg_state[i].store_ruid = reload_combine_ruid;
8824 }
8825 else
8826 reg_state[i].use_index = -1;
8827 }
8828 }
f8cd4126 8829
5adf6da0 8830 }
f8cd4126
RK
8831 else if (GET_CODE (insn) == JUMP_INSN
8832 && GET_CODE (PATTERN (insn)) != RETURN)
5adf6da0
R
8833 {
8834 /* Non-spill registers might be used at the call destination in
8835 some unknown fashion, so we have to mark the unknown use. */
b0634509 8836 HARD_REG_SET *live;
f8cd4126 8837
b0634509
R
8838 if ((condjump_p (insn) || condjump_in_parallel_p (insn))
8839 && JUMP_LABEL (insn))
8840 live = &LABEL_LIVE (JUMP_LABEL (insn));
8841 else
8842 live = &ever_live_at_start;
f8cd4126 8843
5adf6da0 8844 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; --i)
f8cd4126
RK
8845 if (TEST_HARD_REG_BIT (*live, i))
8846 reg_state[i].use_index = -1;
5adf6da0 8847 }
f8cd4126 8848
5adf6da0
R
8849 reload_combine_note_use (&PATTERN (insn), insn);
8850 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
8851 {
8852 if (REG_NOTE_KIND (note) == REG_INC
8853 && GET_CODE (XEXP (note, 0)) == REG)
ed937a19
R
8854 {
8855 int regno = REGNO (XEXP (note, 0));
8856
8857 reg_state[regno].store_ruid = reload_combine_ruid;
8858 reg_state[regno].use_index = -1;
8859 }
5adf6da0
R
8860 }
8861 }
f8cd4126 8862
b0634509 8863 free (label_live);
5adf6da0
R
8864}
8865
8866/* Check if DST is a register or a subreg of a register; if it is,
8867 update reg_state[regno].store_ruid and reg_state[regno].use_index
f93233bb 8868 accordingly. Called via note_stores from reload_combine. */
f8cd4126 8869
5adf6da0 8870static void
84832317 8871reload_combine_note_store (dst, set, data)
f93233bb 8872 rtx dst, set;
84832317 8873 void *data ATTRIBUTE_UNUSED;
5adf6da0
R
8874{
8875 int regno = 0;
8876 int i;
54ed0905 8877 enum machine_mode mode = GET_MODE (dst);
5adf6da0
R
8878
8879 if (GET_CODE (dst) == SUBREG)
8880 {
ddef6bc7
JJ
8881 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
8882 GET_MODE (SUBREG_REG (dst)),
8883 SUBREG_BYTE (dst),
8884 GET_MODE (dst));
5adf6da0
R
8885 dst = SUBREG_REG (dst);
8886 }
8887 if (GET_CODE (dst) != REG)
8888 return;
8889 regno += REGNO (dst);
54ca6ffa 8890
5adf6da0 8891 /* note_stores might have stripped a STRICT_LOW_PART, so we have to be
05d10675 8892 careful with registers / register parts that are not full words.
54ca6ffa
JL
8893
8894 Similarly for ZERO_EXTRACT and SIGN_EXTRACT. */
8895 if (GET_CODE (set) != SET
8896 || GET_CODE (SET_DEST (set)) == ZERO_EXTRACT
8897 || GET_CODE (SET_DEST (set)) == SIGN_EXTRACT
8898 || GET_CODE (SET_DEST (set)) == STRICT_LOW_PART)
ed937a19 8899 {
54ed0905 8900 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
f93233bb
JL
8901 {
8902 reg_state[i].use_index = -1;
8903 reg_state[i].store_ruid = reload_combine_ruid;
8904 }
ed937a19 8905 }
5adf6da0
R
8906 else
8907 {
54ed0905 8908 for (i = HARD_REGNO_NREGS (regno, mode) - 1 + regno; i >= regno; i--)
5adf6da0
R
8909 {
8910 reg_state[i].store_ruid = reload_combine_ruid;
8911 reg_state[i].use_index = RELOAD_COMBINE_MAX_USES;
8912 }
8913 }
8914}
8915
8916/* XP points to a piece of rtl that has to be checked for any uses of
8917 registers.
8918 *XP is the pattern of INSN, or a part of it.
8919 Called from reload_combine, and recursively by itself. */
8920static void
8921reload_combine_note_use (xp, insn)
8922 rtx *xp, insn;
8923{
8924 rtx x = *xp;
8925 enum rtx_code code = x->code;
6f7d635c 8926 const char *fmt;
5adf6da0
R
8927 int i, j;
8928 rtx offset = const0_rtx; /* For the REG case below. */
8929
8930 switch (code)
8931 {
8932 case SET:
8933 if (GET_CODE (SET_DEST (x)) == REG)
8934 {
8935 reload_combine_note_use (&SET_SRC (x), insn);
8936 return;
8937 }
8938 break;
8939
6ce7e0f9
R
8940 case USE:
8941 /* If this is the USE of a return value, we can't change it. */
8942 if (GET_CODE (XEXP (x, 0)) == REG && REG_FUNCTION_VALUE_P (XEXP (x, 0)))
8943 {
8944 /* Mark the return register as used in an unknown fashion. */
8945 rtx reg = XEXP (x, 0);
8946 int regno = REGNO (reg);
8947 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
8948
8949 while (--nregs >= 0)
8950 reg_state[regno + nregs].use_index = -1;
8951 return;
8952 }
8953 break;
8954
5adf6da0
R
8955 case CLOBBER:
8956 if (GET_CODE (SET_DEST (x)) == REG)
3d17d93d
AO
8957 {
8958 /* No spurious CLOBBERs of pseudo registers may remain. */
8959 if (REGNO (SET_DEST (x)) >= FIRST_PSEUDO_REGISTER)
8960 abort ();
8961 return;
8962 }
5adf6da0
R
8963 break;
8964
8965 case PLUS:
8966 /* We are interested in (plus (reg) (const_int)) . */
1d7254c5
KH
8967 if (GET_CODE (XEXP (x, 0)) != REG
8968 || GET_CODE (XEXP (x, 1)) != CONST_INT)
5adf6da0
R
8969 break;
8970 offset = XEXP (x, 1);
8971 x = XEXP (x, 0);
05d10675 8972 /* Fall through. */
5adf6da0
R
8973 case REG:
8974 {
8975 int regno = REGNO (x);
8976 int use_index;
6ce7e0f9 8977 int nregs;
5adf6da0 8978
3d17d93d 8979 /* No spurious USEs of pseudo registers may remain. */
5adf6da0 8980 if (regno >= FIRST_PSEUDO_REGISTER)
3d17d93d 8981 abort ();
5adf6da0 8982
6ce7e0f9
R
8983 nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
8984
8985 /* We can't substitute into multi-hard-reg uses. */
8986 if (nregs > 1)
8987 {
8988 while (--nregs >= 0)
8989 reg_state[regno + nregs].use_index = -1;
8990 return;
8991 }
8992
5adf6da0
R
8993 /* If this register is already used in some unknown fashion, we
8994 can't do anything.
8995 If we decrement the index from zero to -1, we can't store more
8996 uses, so this register becomes used in an unknown fashion. */
8997 use_index = --reg_state[regno].use_index;
8998 if (use_index < 0)
8999 return;
9000
9001 if (use_index != RELOAD_COMBINE_MAX_USES - 1)
9002 {
9003 /* We have found another use for a register that is already
9004 used later. Check if the offsets match; if not, mark the
9005 register as used in an unknown fashion. */
9006 if (! rtx_equal_p (offset, reg_state[regno].offset))
9007 {
9008 reg_state[regno].use_index = -1;
9009 return;
9010 }
9011 }
9012 else
9013 {
9014 /* This is the first use of this register we have seen since we
9015 marked it as dead. */
9016 reg_state[regno].offset = offset;
9017 reg_state[regno].use_ruid = reload_combine_ruid;
9018 }
9019 reg_state[regno].reg_use[use_index].insn = insn;
9020 reg_state[regno].reg_use[use_index].usep = xp;
9021 return;
9022 }
9023
9024 default:
9025 break;
9026 }
9027
9028 /* Recursively process the components of X. */
9029 fmt = GET_RTX_FORMAT (code);
9030 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9031 {
9032 if (fmt[i] == 'e')
9033 reload_combine_note_use (&XEXP (x, i), insn);
9034 else if (fmt[i] == 'E')
9035 {
9036 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9037 reload_combine_note_use (&XVECEXP (x, i, j), insn);
9038 }
9039 }
9040}
9041\f
61f5625b
AO
9042/* See if we can reduce the cost of a constant by replacing a move
9043 with an add. We track situations in which a register is set to a
9044 constant or to a register plus a constant. */
5adf6da0
R
9045/* We cannot do our optimization across labels. Invalidating all the
9046 information about register contents we have would be costly, so we
61f5625b
AO
9047 use move2add_last_label_luid to note where the label is and then
9048 later disable any optimization that would cross it.
5adf6da0 9049 reg_offset[n] / reg_base_reg[n] / reg_mode[n] are only valid if
61f5625b 9050 reg_set_luid[n] is greater than last_label_luid[n] . */
5adf6da0 9051static int reg_set_luid[FIRST_PSEUDO_REGISTER];
770ae6cc 9052
61f5625b
AO
9053/* If reg_base_reg[n] is negative, register n has been set to
9054 reg_offset[n] in mode reg_mode[n] .
9055 If reg_base_reg[n] is non-negative, register n has been set to the
9056 sum of reg_offset[n] and the value of register reg_base_reg[n]
dc297297 9057 before reg_set_luid[n], calculated in mode reg_mode[n] . */
61f5625b 9058static HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
5adf6da0
R
9059static int reg_base_reg[FIRST_PSEUDO_REGISTER];
9060static enum machine_mode reg_mode[FIRST_PSEUDO_REGISTER];
770ae6cc 9061
5adf6da0
R
9062/* move2add_luid is linearily increased while scanning the instructions
9063 from first to last. It is used to set reg_set_luid in
6764d250 9064 reload_cse_move2add and move2add_note_store. */
5adf6da0
R
9065static int move2add_luid;
9066
61f5625b
AO
9067/* move2add_last_label_luid is set whenever a label is found. Labels
9068 invalidate all previously collected reg_offset data. */
9069static int move2add_last_label_luid;
9070
ccc4ae07 9071/* Generate a CONST_INT and force it in the range of MODE. */
770ae6cc 9072
61f5625b
AO
9073static HOST_WIDE_INT
9074sext_for_mode (mode, value)
ccc4ae07
AS
9075 enum machine_mode mode;
9076 HOST_WIDE_INT value;
9077{
9078 HOST_WIDE_INT cval = value & GET_MODE_MASK (mode);
9079 int width = GET_MODE_BITSIZE (mode);
9080
9081 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative number,
9082 sign extend it. */
9083 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
9084 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
9085 cval |= (HOST_WIDE_INT) -1 << width;
9086
61f5625b 9087 return cval;
ccc4ae07
AS
9088}
9089
61f5625b
AO
9090/* ??? We don't know how zero / sign extension is handled, hence we
9091 can't go from a narrower to a wider mode. */
9092#define MODES_OK_FOR_MOVE2ADD(OUTMODE, INMODE) \
9093 (GET_MODE_SIZE (OUTMODE) == GET_MODE_SIZE (INMODE) \
9094 || (GET_MODE_SIZE (OUTMODE) <= GET_MODE_SIZE (INMODE) \
9095 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (OUTMODE), \
9096 GET_MODE_BITSIZE (INMODE))))
9097
5adf6da0
R
9098static void
9099reload_cse_move2add (first)
9100 rtx first;
9101{
9102 int i;
9103 rtx insn;
5adf6da0 9104
1d7254c5 9105 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
6764d250
BS
9106 reg_set_luid[i] = 0;
9107
61f5625b
AO
9108 move2add_last_label_luid = 0;
9109 move2add_luid = 2;
5adf6da0
R
9110 for (insn = first; insn; insn = NEXT_INSN (insn), move2add_luid++)
9111 {
9112 rtx pat, note;
9113
9114 if (GET_CODE (insn) == CODE_LABEL)
61f5625b
AO
9115 {
9116 move2add_last_label_luid = move2add_luid;
9117 /* We're going to increment move2add_luid twice after a
9118 label, so that we can use move2add_last_label_luid + 1 as
9119 the luid for constants. */
9120 move2add_luid++;
9121 continue;
9122 }
2c3c49de 9123 if (! INSN_P (insn))
5adf6da0
R
9124 continue;
9125 pat = PATTERN (insn);
9126 /* For simplicity, we only perform this optimization on
9127 straightforward SETs. */
9128 if (GET_CODE (pat) == SET
9129 && GET_CODE (SET_DEST (pat)) == REG)
9130 {
9131 rtx reg = SET_DEST (pat);
9132 int regno = REGNO (reg);
9133 rtx src = SET_SRC (pat);
9134
9135 /* Check if we have valid information on the contents of this
9136 register in the mode of REG. */
61f5625b
AO
9137 if (reg_set_luid[regno] > move2add_last_label_luid
9138 && MODES_OK_FOR_MOVE2ADD (GET_MODE (reg), reg_mode[regno]))
5adf6da0
R
9139 {
9140 /* Try to transform (set (REGX) (CONST_INT A))
9141 ...
9142 (set (REGX) (CONST_INT B))
9143 to
9144 (set (REGX) (CONST_INT A))
9145 ...
9146 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
9147
9148 if (GET_CODE (src) == CONST_INT && reg_base_reg[regno] < 0)
9149 {
9150 int success = 0;
61f5625b
AO
9151 rtx new_src = GEN_INT (sext_for_mode (GET_MODE (reg),
9152 INTVAL (src)
9153 - reg_offset[regno]));
5adf6da0
R
9154 /* (set (reg) (plus (reg) (const_int 0))) is not canonical;
9155 use (set (reg) (reg)) instead.
9156 We don't delete this insn, nor do we convert it into a
9157 note, to avoid losing register notes or the return
9158 value flag. jump2 already knowns how to get rid of
9159 no-op moves. */
9160 if (new_src == const0_rtx)
9161 success = validate_change (insn, &SET_SRC (pat), reg, 0);
9162 else if (rtx_cost (new_src, PLUS) < rtx_cost (src, SET)
fb7e77d7 9163 && have_add2_insn (reg, new_src))
5adf6da0
R
9164 success = validate_change (insn, &PATTERN (insn),
9165 gen_add2_insn (reg, new_src), 0);
5adf6da0
R
9166 reg_set_luid[regno] = move2add_luid;
9167 reg_mode[regno] = GET_MODE (reg);
61f5625b 9168 reg_offset[regno] = INTVAL (src);
5adf6da0
R
9169 continue;
9170 }
9171
9172 /* Try to transform (set (REGX) (REGY))
9173 (set (REGX) (PLUS (REGX) (CONST_INT A)))
9174 ...
9175 (set (REGX) (REGY))
9176 (set (REGX) (PLUS (REGX) (CONST_INT B)))
9177 to
9178 (REGX) (REGY))
9179 (set (REGX) (PLUS (REGX) (CONST_INT A)))
9180 ...
9181 (set (REGX) (plus (REGX) (CONST_INT B-A))) */
9182 else if (GET_CODE (src) == REG
61f5625b
AO
9183 && reg_set_luid[regno] == reg_set_luid[REGNO (src)]
9184 && reg_base_reg[regno] == reg_base_reg[REGNO (src)]
9185 && MODES_OK_FOR_MOVE2ADD (GET_MODE (reg),
9186 reg_mode[REGNO (src)]))
5adf6da0
R
9187 {
9188 rtx next = next_nonnote_insn (insn);
6a651371 9189 rtx set = NULL_RTX;
5adf6da0
R
9190 if (next)
9191 set = single_set (next);
61f5625b 9192 if (set
5adf6da0
R
9193 && SET_DEST (set) == reg
9194 && GET_CODE (SET_SRC (set)) == PLUS
9195 && XEXP (SET_SRC (set), 0) == reg
9196 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
9197 {
5adf6da0 9198 rtx src3 = XEXP (SET_SRC (set), 1);
61f5625b
AO
9199 HOST_WIDE_INT added_offset = INTVAL (src3);
9200 HOST_WIDE_INT base_offset = reg_offset[REGNO (src)];
9201 HOST_WIDE_INT regno_offset = reg_offset[regno];
9202 rtx new_src = GEN_INT (sext_for_mode (GET_MODE (reg),
9203 added_offset
9204 + base_offset
9205 - regno_offset));
5adf6da0
R
9206 int success = 0;
9207
9208 if (new_src == const0_rtx)
9209 /* See above why we create (set (reg) (reg)) here. */
9210 success
9211 = validate_change (next, &SET_SRC (set), reg, 0);
9212 else if ((rtx_cost (new_src, PLUS)
b437f1a7 9213 < COSTS_N_INSNS (1) + rtx_cost (src3, SET))
fb7e77d7 9214 && have_add2_insn (reg, new_src))
5adf6da0
R
9215 success
9216 = validate_change (next, &PATTERN (next),
9217 gen_add2_insn (reg, new_src), 0);
9218 if (success)
ca6c03ca 9219 delete_insn (insn);
5adf6da0 9220 insn = next;
5adf6da0 9221 reg_mode[regno] = GET_MODE (reg);
61f5625b
AO
9222 reg_offset[regno] = sext_for_mode (GET_MODE (reg),
9223 added_offset
9224 + base_offset);
5adf6da0
R
9225 continue;
9226 }
9227 }
9228 }
9229 }
9230
9231 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
9232 {
9233 if (REG_NOTE_KIND (note) == REG_INC
9234 && GET_CODE (XEXP (note, 0)) == REG)
9235 {
61f5625b 9236 /* Reset the information about this register. */
5adf6da0
R
9237 int regno = REGNO (XEXP (note, 0));
9238 if (regno < FIRST_PSEUDO_REGISTER)
61f5625b 9239 reg_set_luid[regno] = 0;
5adf6da0 9240 }
5adf6da0 9241 }
84832317 9242 note_stores (PATTERN (insn), move2add_note_store, NULL);
5adf6da0
R
9243 /* If this is a CALL_INSN, all call used registers are stored with
9244 unknown values. */
9245 if (GET_CODE (insn) == CALL_INSN)
9246 {
1d7254c5 9247 for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
5adf6da0
R
9248 {
9249 if (call_used_regs[i])
61f5625b
AO
9250 /* Reset the information about this register. */
9251 reg_set_luid[i] = 0;
5adf6da0
R
9252 }
9253 }
9254 }
9255}
9256
9257/* SET is a SET or CLOBBER that sets DST.
9258 Update reg_set_luid, reg_offset and reg_base_reg accordingly.
9259 Called from reload_cse_move2add via note_stores. */
770ae6cc 9260
5adf6da0 9261static void
84832317 9262move2add_note_store (dst, set, data)
5adf6da0 9263 rtx dst, set;
84832317 9264 void *data ATTRIBUTE_UNUSED;
5adf6da0 9265{
770ae6cc
RK
9266 unsigned int regno = 0;
9267 unsigned int i;
5adf6da0 9268 enum machine_mode mode = GET_MODE (dst);
770ae6cc 9269
5adf6da0
R
9270 if (GET_CODE (dst) == SUBREG)
9271 {
ddef6bc7
JJ
9272 regno = subreg_regno_offset (REGNO (SUBREG_REG (dst)),
9273 GET_MODE (SUBREG_REG (dst)),
9274 SUBREG_BYTE (dst),
9275 GET_MODE (dst));
5adf6da0
R
9276 dst = SUBREG_REG (dst);
9277 }
770ae6cc 9278
19ca869b
JR
9279 /* Some targets do argument pushes without adding REG_INC notes. */
9280
9281 if (GET_CODE (dst) == MEM)
9282 {
9283 dst = XEXP (dst, 0);
52fdbf26 9284 if (GET_CODE (dst) == PRE_INC || GET_CODE (dst) == POST_INC
19ca869b 9285 || GET_CODE (dst) == PRE_DEC || GET_CODE (dst) == POST_DEC)
61f5625b 9286 reg_set_luid[REGNO (XEXP (dst, 0))] = 0;
19ca869b 9287 return;
174fa2c4 9288 }
5adf6da0
R
9289 if (GET_CODE (dst) != REG)
9290 return;
9291
9292 regno += REGNO (dst);
9293
f93233bb
JL
9294 if (HARD_REGNO_NREGS (regno, mode) == 1 && GET_CODE (set) == SET
9295 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
9296 && GET_CODE (SET_DEST (set)) != SIGN_EXTRACT
9297 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
5adf6da0
R
9298 {
9299 rtx src = SET_SRC (set);
61f5625b
AO
9300 rtx base_reg;
9301 HOST_WIDE_INT offset;
9302 int base_regno;
9303 /* This may be different from mode, if SET_DEST (set) is a
9304 SUBREG. */
9305 enum machine_mode dst_mode = GET_MODE (dst);
5adf6da0 9306
5adf6da0
R
9307 switch (GET_CODE (src))
9308 {
9309 case PLUS:
61f5625b
AO
9310 if (GET_CODE (XEXP (src, 0)) == REG)
9311 {
9312 base_reg = XEXP (src, 0);
9313
9314 if (GET_CODE (XEXP (src, 1)) == CONST_INT)
9315 offset = INTVAL (XEXP (src, 1));
9316 else if (GET_CODE (XEXP (src, 1)) == REG
9317 && (reg_set_luid[REGNO (XEXP (src, 1))]
9318 > move2add_last_label_luid)
9319 && (MODES_OK_FOR_MOVE2ADD
9320 (dst_mode, reg_mode[REGNO (XEXP (src, 1))])))
9321 {
9322 if (reg_base_reg[REGNO (XEXP (src, 1))] < 0)
9323 offset = reg_offset[REGNO (XEXP (src, 1))];
9324 /* Maybe the first register is known to be a
9325 constant. */
9326 else if (reg_set_luid[REGNO (base_reg)]
9327 > move2add_last_label_luid
9328 && (MODES_OK_FOR_MOVE2ADD
9329 (dst_mode, reg_mode[REGNO (XEXP (src, 1))]))
9330 && reg_base_reg[REGNO (base_reg)] < 0)
9331 {
9332 offset = reg_offset[REGNO (base_reg)];
9333 base_reg = XEXP (src, 1);
9334 }
9335 else
9336 goto invalidate;
9337 }
9338 else
9339 goto invalidate;
770ae6cc 9340
61f5625b
AO
9341 break;
9342 }
770ae6cc 9343
61f5625b 9344 goto invalidate;
5adf6da0
R
9345
9346 case REG:
61f5625b
AO
9347 base_reg = src;
9348 offset = 0;
5adf6da0
R
9349 break;
9350
61f5625b
AO
9351 case CONST_INT:
9352 /* Start tracking the register as a constant. */
5adf6da0 9353 reg_base_reg[regno] = -1;
61f5625b
AO
9354 reg_offset[regno] = INTVAL (SET_SRC (set));
9355 /* We assign the same luid to all registers set to constants. */
9356 reg_set_luid[regno] = move2add_last_label_luid + 1;
9357 reg_mode[regno] = mode;
9358 return;
a6a2274a 9359
61f5625b
AO
9360 default:
9361 invalidate:
9362 /* Invalidate the contents of the register. */
9363 reg_set_luid[regno] = 0;
9364 return;
5adf6da0 9365 }
61f5625b
AO
9366
9367 base_regno = REGNO (base_reg);
9368 /* If information about the base register is not valid, set it
9369 up as a new base register, pretending its value is known
9370 starting from the current insn. */
9371 if (reg_set_luid[base_regno] <= move2add_last_label_luid)
9372 {
9373 reg_base_reg[base_regno] = base_regno;
9374 reg_offset[base_regno] = 0;
9375 reg_set_luid[base_regno] = move2add_luid;
9376 reg_mode[base_regno] = mode;
9377 }
9378 else if (! MODES_OK_FOR_MOVE2ADD (dst_mode,
9379 reg_mode[base_regno]))
9380 goto invalidate;
9381
9382 reg_mode[regno] = mode;
9383
9384 /* Copy base information from our base register. */
9385 reg_set_luid[regno] = reg_set_luid[base_regno];
9386 reg_base_reg[regno] = reg_base_reg[base_regno];
9387
9388 /* Compute the sum of the offsets or constants. */
9389 reg_offset[regno] = sext_for_mode (dst_mode,
9390 offset
9391 + reg_offset[base_regno]);
5adf6da0
R
9392 }
9393 else
9394 {
770ae6cc
RK
9395 unsigned int endregno = regno + HARD_REGNO_NREGS (regno, mode);
9396
9397 for (i = regno; i < endregno; i++)
61f5625b
AO
9398 /* Reset the information about this register. */
9399 reg_set_luid[i] = 0;
5adf6da0
R
9400 }
9401}
2dfa9a87
MH
9402
9403#ifdef AUTO_INC_DEC
9404static void
9405add_auto_inc_notes (insn, x)
9406 rtx insn;
9407 rtx x;
9408{
9409 enum rtx_code code = GET_CODE (x);
6f7d635c 9410 const char *fmt;
2dfa9a87
MH
9411 int i, j;
9412
9413 if (code == MEM && auto_inc_p (XEXP (x, 0)))
9414 {
9415 REG_NOTES (insn)
9416 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
9417 return;
9418 }
9419
9420 /* Scan all the operand sub-expressions. */
9421 fmt = GET_RTX_FORMAT (code);
9422 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9423 {
9424 if (fmt[i] == 'e')
9425 add_auto_inc_notes (insn, XEXP (x, i));
9426 else if (fmt[i] == 'E')
9427 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9428 add_auto_inc_notes (insn, XVECEXP (x, i, j));
9429 }
9430}
9431#endif
94bd63e5
AH
9432
9433/* Copy EH notes from an insn to its reloads. */
9434static void
9435copy_eh_notes (insn, x)
9436 rtx insn;
9437 rtx x;
9438{
9439 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
9440 if (eh_note)
9441 {
9442 for (; x != 0; x = NEXT_INSN (x))
9443 {
9444 if (may_trap_p (PATTERN (x)))
a6a2274a 9445 REG_NOTES (x)
94bd63e5
AH
9446 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
9447 REG_NOTES (x));
9448 }
9449 }
9450}
9451
f1330226
JH
9452/* This is used by reload pass, that does emit some instructions after
9453 abnormal calls moving basic block end, but in fact it wants to emit
9454 them on the edge. Looks for abnormal call edges, find backward the
9455 proper call and fix the damage.
a6a2274a 9456
f1330226 9457 Similar handle instructions throwing exceptions internally. */
068473ec 9458void
f1330226
JH
9459fixup_abnormal_edges ()
9460{
f1330226 9461 bool inserted = false;
e0082a72 9462 basic_block bb;
f1330226 9463
e0082a72 9464 FOR_EACH_BB (bb)
f1330226 9465 {
f1330226
JH
9466 edge e;
9467
9468 /* Look for cases we are interested in - an calls or instructions causing
9469 exceptions. */
9470 for (e = bb->succ; e; e = e->succ_next)
9471 {
9472 if (e->flags & EDGE_ABNORMAL_CALL)
9473 break;
9474 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
9475 == (EDGE_ABNORMAL | EDGE_EH))
9476 break;
9477 }
9478 if (e && GET_CODE (bb->end) != CALL_INSN && !can_throw_internal (bb->end))
9479 {
0c4992b0 9480 rtx insn = bb->end, stop = NEXT_INSN (bb->end);
f1330226
JH
9481 rtx next;
9482 for (e = bb->succ; e; e = e->succ_next)
9483 if (e->flags & EDGE_FALLTHRU)
9484 break;
39f95a2c
JH
9485 /* Get past the new insns generated. Allow notes, as the insns may
9486 be already deleted. */
9487 while ((GET_CODE (insn) == INSN || GET_CODE (insn) == NOTE)
9488 && !can_throw_internal (insn)
9489 && insn != bb->head)
f1330226
JH
9490 insn = PREV_INSN (insn);
9491 if (GET_CODE (insn) != CALL_INSN && !can_throw_internal (insn))
9492 abort ();
9493 bb->end = insn;
9494 inserted = true;
9495 insn = NEXT_INSN (insn);
0c4992b0 9496 while (insn && insn != stop)
f1330226
JH
9497 {
9498 next = NEXT_INSN (insn);
0c4992b0
JH
9499 if (INSN_P (insn))
9500 {
53c17031 9501 delete_insn (insn);
f8ed1958 9502
ed8d2920
MM
9503 /* Sometimes there's still the return value USE.
9504 If it's placed after a trapping call (i.e. that
9505 call is the last insn anyway), we have no fallthru
9506 edge. Simply delete this use and don't try to insert
9507 on the non-existant edge. */
9508 if (GET_CODE (PATTERN (insn)) != USE)
9509 {
ed8d2920
MM
9510 /* We're not deleting it, we're moving it. */
9511 INSN_DELETED_P (insn) = 0;
9512 PREV_INSN (insn) = NULL_RTX;
9513 NEXT_INSN (insn) = NULL_RTX;
f8ed1958 9514
ed8d2920
MM
9515 insert_insn_on_edge (insn, e);
9516 }
0c4992b0 9517 }
f1330226
JH
9518 insn = next;
9519 }
9520 }
9521 }
9522 if (inserted)
9523 commit_edge_insertions ();
9524}
This page took 3.832159 seconds and 5 git commands to generate.