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