* configure.in: Add ${libgcj} to noconfigdirs for xtensa-*-* targets.
[official-gcc.git] / gcc / reload1.c
blobfadc6d302814bad8977d31c44e2a210a6798d1f7
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
27 #include "machmode.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "obstack.h"
32 #include "insn-config.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "basic-block.h"
39 #include "reload.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "real.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "tree.h"
47 /* This file contains the reload pass of the compiler, which is
48 run after register allocation has been done. It checks that
49 each insn is valid (operands required to be in registers really
50 are in registers of the proper class) and fixes up invalid ones
51 by copying values temporarily into registers for the insns
52 that need them.
54 The results of register allocation are described by the vector
55 reg_renumber; the insns still contain pseudo regs, but reg_renumber
56 can be used to find which hard reg, if any, a pseudo reg is in.
58 The technique we always use is to free up a few hard regs that are
59 called ``reload regs'', and for each place where a pseudo reg
60 must be in a hard reg, copy it temporarily into one of the reload regs.
62 Reload regs are allocated locally for every instruction that needs
63 reloads. When there are pseudos which are allocated to a register that
64 has been chosen as a reload reg, such pseudos must be ``spilled''.
65 This means that they go to other hard regs, or to stack slots if no other
66 available hard regs can be found. Spilling can invalidate more
67 insns, requiring additional need for reloads, so we must keep checking
68 until the process stabilizes.
70 For machines with different classes of registers, we must keep track
71 of the register class needed for each reload, and make sure that
72 we allocate enough reload registers of each class.
74 The file reload.c contains the code that checks one insn for
75 validity and reports the reloads that it needs. This file
76 is in charge of scanning the entire rtl code, accumulating the
77 reload needs, spilling, assigning reload registers to use for
78 fixing up each insn, and generating the new insns to copy values
79 into the reload registers. */
81 #ifndef LOCAL_REGNO
82 #define LOCAL_REGNO(REGNO) 0
83 #endif
85 /* During reload_as_needed, element N contains a REG rtx for the hard reg
86 into which reg N has been reloaded (perhaps for a previous insn). */
87 static rtx *reg_last_reload_reg;
89 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
90 for an output reload that stores into reg N. */
91 static char *reg_has_output_reload;
93 /* Indicates which hard regs are reload-registers for an output reload
94 in the current insn. */
95 static HARD_REG_SET reg_is_output_reload;
97 /* Element N is the constant value to which pseudo reg N is equivalent,
98 or zero if pseudo reg N is not equivalent to a constant.
99 find_reloads looks at this in order to replace pseudo reg N
100 with the constant it stands for. */
101 rtx *reg_equiv_constant;
103 /* Element N is a memory location to which pseudo reg N is equivalent,
104 prior to any register elimination (such as frame pointer to stack
105 pointer). Depending on whether or not it is a valid address, this value
106 is transferred to either reg_equiv_address or reg_equiv_mem. */
107 rtx *reg_equiv_memory_loc;
109 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
110 This is used when the address is not valid as a memory address
111 (because its displacement is too big for the machine.) */
112 rtx *reg_equiv_address;
114 /* Element N is the memory slot to which pseudo reg N is equivalent,
115 or zero if pseudo reg N is not equivalent to a memory slot. */
116 rtx *reg_equiv_mem;
118 /* Widest width in which each pseudo reg is referred to (via subreg). */
119 static unsigned int *reg_max_ref_width;
121 /* Element N is the list of insns that initialized reg N from its equivalent
122 constant or memory slot. */
123 static rtx *reg_equiv_init;
125 /* Vector to remember old contents of reg_renumber before spilling. */
126 static short *reg_old_renumber;
128 /* During reload_as_needed, element N contains the last pseudo regno reloaded
129 into hard register N. If that pseudo reg occupied more than one register,
130 reg_reloaded_contents points to that pseudo for each spill register in
131 use; all of these must remain set for an inheritance to occur. */
132 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
134 /* During reload_as_needed, element N contains the insn for which
135 hard register N was last used. Its contents are significant only
136 when reg_reloaded_valid is set for this register. */
137 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
139 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
140 static HARD_REG_SET reg_reloaded_valid;
141 /* Indicate if the register was dead at the end of the reload.
142 This is only valid if reg_reloaded_contents is set and valid. */
143 static HARD_REG_SET reg_reloaded_dead;
145 /* Number of spill-regs so far; number of valid elements of spill_regs. */
146 static int n_spills;
148 /* In parallel with spill_regs, contains REG rtx's for those regs.
149 Holds the last rtx used for any given reg, or 0 if it has never
150 been used for spilling yet. This rtx is reused, provided it has
151 the proper mode. */
152 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
154 /* In parallel with spill_regs, contains nonzero for a spill reg
155 that was stored after the last time it was used.
156 The precise value is the insn generated to do the store. */
157 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
159 /* This is the register that was stored with spill_reg_store. This is a
160 copy of reload_out / reload_out_reg when the value was stored; if
161 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
162 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
164 /* This table is the inverse mapping of spill_regs:
165 indexed by hard reg number,
166 it contains the position of that reg in spill_regs,
167 or -1 for something that is not in spill_regs.
169 ?!? This is no longer accurate. */
170 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
172 /* This reg set indicates registers that can't be used as spill registers for
173 the currently processed insn. These are the hard registers which are live
174 during the insn, but not allocated to pseudos, as well as fixed
175 registers. */
176 static HARD_REG_SET bad_spill_regs;
178 /* These are the hard registers that can't be used as spill register for any
179 insn. This includes registers used for user variables and registers that
180 we can't eliminate. A register that appears in this set also can't be used
181 to retry register allocation. */
182 static HARD_REG_SET bad_spill_regs_global;
184 /* Describes order of use of registers for reloading
185 of spilled pseudo-registers. `n_spills' is the number of
186 elements that are actually valid; new ones are added at the end.
188 Both spill_regs and spill_reg_order are used on two occasions:
189 once during find_reload_regs, where they keep track of the spill registers
190 for a single insn, but also during reload_as_needed where they show all
191 the registers ever used by reload. For the latter case, the information
192 is calculated during finish_spills. */
193 static short spill_regs[FIRST_PSEUDO_REGISTER];
195 /* This vector of reg sets indicates, for each pseudo, which hard registers
196 may not be used for retrying global allocation because the register was
197 formerly spilled from one of them. If we allowed reallocating a pseudo to
198 a register that it was already allocated to, reload might not
199 terminate. */
200 static HARD_REG_SET *pseudo_previous_regs;
202 /* This vector of reg sets indicates, for each pseudo, which hard
203 registers may not be used for retrying global allocation because they
204 are used as spill registers during one of the insns in which the
205 pseudo is live. */
206 static HARD_REG_SET *pseudo_forbidden_regs;
208 /* All hard regs that have been used as spill registers for any insn are
209 marked in this set. */
210 static HARD_REG_SET used_spill_regs;
212 /* Index of last register assigned as a spill register. We allocate in
213 a round-robin fashion. */
214 static int last_spill_reg;
216 /* Nonzero if indirect addressing is supported on the machine; this means
217 that spilling (REG n) does not require reloading it into a register in
218 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
219 value indicates the level of indirect addressing supported, e.g., two
220 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
221 a hard register. */
222 static char spill_indirect_levels;
224 /* Nonzero if indirect addressing is supported when the innermost MEM is
225 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
226 which these are valid is the same as spill_indirect_levels, above. */
227 char indirect_symref_ok;
229 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
230 char double_reg_address_ok;
232 /* Record the stack slot for each spilled hard register. */
233 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
235 /* Width allocated so far for that stack slot. */
236 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
238 /* Record which pseudos needed to be spilled. */
239 static regset_head spilled_pseudos;
241 /* Used for communication between order_regs_for_reload and count_pseudo.
242 Used to avoid counting one pseudo twice. */
243 static regset_head pseudos_counted;
245 /* First uid used by insns created by reload in this function.
246 Used in find_equiv_reg. */
247 int reload_first_uid;
249 /* Flag set by local-alloc or global-alloc if anything is live in
250 a call-clobbered reg across calls. */
251 int caller_save_needed;
253 /* Set to 1 while reload_as_needed is operating.
254 Required by some machines to handle any generated moves differently. */
255 int reload_in_progress = 0;
257 /* These arrays record the insn_code of insns that may be needed to
258 perform input and output reloads of special objects. They provide a
259 place to pass a scratch register. */
260 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
261 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
263 /* This obstack is used for allocation of rtl during register elimination.
264 The allocated storage can be freed once find_reloads has processed the
265 insn. */
266 struct obstack reload_obstack;
268 /* Points to the beginning of the reload_obstack. All insn_chain structures
269 are allocated first. */
270 char *reload_startobj;
272 /* The point after all insn_chain structures. Used to quickly deallocate
273 memory allocated in copy_reloads during calculate_needs_all_insns. */
274 char *reload_firstobj;
276 /* This points before all local rtl generated by register elimination.
277 Used to quickly free all memory after processing one insn. */
278 static char *reload_insn_firstobj;
280 /* List of insn_chain instructions, one for every insn that reload needs to
281 examine. */
282 struct insn_chain *reload_insn_chain;
284 #ifdef TREE_CODE
285 extern tree current_function_decl;
286 #else
287 extern union tree_node *current_function_decl;
288 #endif
290 /* List of all insns needing reloads. */
291 static struct insn_chain *insns_need_reload;
293 /* This structure is used to record information about register eliminations.
294 Each array entry describes one possible way of eliminating a register
295 in favor of another. If there is more than one way of eliminating a
296 particular register, the most preferred should be specified first. */
298 struct elim_table
300 int from; /* Register number to be eliminated. */
301 int to; /* Register number used as replacement. */
302 int initial_offset; /* Initial difference between values. */
303 int can_eliminate; /* Nonzero if this elimination can be done. */
304 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
305 insns made by reload. */
306 int offset; /* Current offset between the two regs. */
307 int previous_offset; /* Offset at end of previous insn. */
308 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
309 rtx from_rtx; /* REG rtx for the register to be eliminated.
310 We cannot simply compare the number since
311 we might then spuriously replace a hard
312 register corresponding to a pseudo
313 assigned to the reg to be eliminated. */
314 rtx to_rtx; /* REG rtx for the replacement. */
317 static struct elim_table *reg_eliminate = 0;
319 /* This is an intermediate structure to initialize the table. It has
320 exactly the members provided by ELIMINABLE_REGS. */
321 static const struct elim_table_1
323 const int from;
324 const int to;
325 } reg_eliminate_1[] =
327 /* If a set of eliminable registers was specified, define the table from it.
328 Otherwise, default to the normal case of the frame pointer being
329 replaced by the stack pointer. */
331 #ifdef ELIMINABLE_REGS
332 ELIMINABLE_REGS;
333 #else
334 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
335 #endif
337 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
339 /* Record the number of pending eliminations that have an offset not equal
340 to their initial offset. If nonzero, we use a new copy of each
341 replacement result in any insns encountered. */
342 int num_not_at_initial_offset;
344 /* Count the number of registers that we may be able to eliminate. */
345 static int num_eliminable;
346 /* And the number of registers that are equivalent to a constant that
347 can be eliminated to frame_pointer / arg_pointer + constant. */
348 static int num_eliminable_invariants;
350 /* For each label, we record the offset of each elimination. If we reach
351 a label by more than one path and an offset differs, we cannot do the
352 elimination. This information is indexed by the difference of the
353 number of the label and the first label number. We can't offset the
354 pointer itself as this can cause problems on machines with segmented
355 memory. The first table is an array of flags that records whether we
356 have yet encountered a label and the second table is an array of arrays,
357 one entry in the latter array for each elimination. */
359 static int first_label_num;
360 static char *offsets_known_at;
361 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
363 /* Number of labels in the current function. */
365 static int num_labels;
367 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
368 static void maybe_fix_stack_asms (void);
369 static void copy_reloads (struct insn_chain *);
370 static void calculate_needs_all_insns (int);
371 static int find_reg (struct insn_chain *, int);
372 static void find_reload_regs (struct insn_chain *);
373 static void select_reload_regs (void);
374 static void delete_caller_save_insns (void);
376 static void spill_failure (rtx, enum reg_class);
377 static void count_spilled_pseudo (int, int, int);
378 static void delete_dead_insn (rtx);
379 static void alter_reg (int, int);
380 static void set_label_offsets (rtx, rtx, int);
381 static void check_eliminable_occurrences (rtx);
382 static void elimination_effects (rtx, enum machine_mode);
383 static int eliminate_regs_in_insn (rtx, int);
384 static void update_eliminable_offsets (void);
385 static void mark_not_eliminable (rtx, rtx, void *);
386 static void set_initial_elim_offsets (void);
387 static void verify_initial_elim_offsets (void);
388 static void set_initial_label_offsets (void);
389 static void set_offsets_for_label (rtx);
390 static void init_elim_table (void);
391 static void update_eliminables (HARD_REG_SET *);
392 static void spill_hard_reg (unsigned int, int);
393 static int finish_spills (int);
394 static void ior_hard_reg_set (HARD_REG_SET *, HARD_REG_SET *);
395 static void scan_paradoxical_subregs (rtx);
396 static void count_pseudo (int);
397 static void order_regs_for_reload (struct insn_chain *);
398 static void reload_as_needed (int);
399 static void forget_old_reloads_1 (rtx, rtx, void *);
400 static int reload_reg_class_lower (const void *, const void *);
401 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
402 enum machine_mode);
403 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
404 enum machine_mode);
405 static int reload_reg_free_p (unsigned int, int, enum reload_type);
406 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
407 rtx, rtx, int, int);
408 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
409 rtx, rtx, int, int);
410 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
411 static int allocate_reload_reg (struct insn_chain *, int, int);
412 static int conflicts_with_override (rtx);
413 static void failed_reload (rtx, int);
414 static int set_reload_reg (int, int);
415 static void choose_reload_regs_init (struct insn_chain *, rtx *);
416 static void choose_reload_regs (struct insn_chain *);
417 static void merge_assigned_reloads (rtx);
418 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
419 rtx, int);
420 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
421 int);
422 static void do_input_reload (struct insn_chain *, struct reload *, int);
423 static void do_output_reload (struct insn_chain *, struct reload *, int);
424 static void emit_reload_insns (struct insn_chain *);
425 static void delete_output_reload (rtx, int, int);
426 static void delete_address_reloads (rtx, rtx);
427 static void delete_address_reloads_1 (rtx, rtx, rtx);
428 static rtx inc_for_reload (rtx, rtx, rtx, int);
429 #ifdef AUTO_INC_DEC
430 static void add_auto_inc_notes (rtx, rtx);
431 #endif
432 static void copy_eh_notes (rtx, rtx);
434 /* Initialize the reload pass once per compilation. */
436 void
437 init_reload (void)
439 int i;
441 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
442 Set spill_indirect_levels to the number of levels such addressing is
443 permitted, zero if it is not permitted at all. */
445 rtx tem
446 = gen_rtx_MEM (Pmode,
447 gen_rtx_PLUS (Pmode,
448 gen_rtx_REG (Pmode,
449 LAST_VIRTUAL_REGISTER + 1),
450 GEN_INT (4)));
451 spill_indirect_levels = 0;
453 while (memory_address_p (QImode, tem))
455 spill_indirect_levels++;
456 tem = gen_rtx_MEM (Pmode, tem);
459 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
461 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
462 indirect_symref_ok = memory_address_p (QImode, tem);
464 /* See if reg+reg is a valid (and offsettable) address. */
466 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
468 tem = gen_rtx_PLUS (Pmode,
469 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
470 gen_rtx_REG (Pmode, i));
472 /* This way, we make sure that reg+reg is an offsettable address. */
473 tem = plus_constant (tem, 4);
475 if (memory_address_p (QImode, tem))
477 double_reg_address_ok = 1;
478 break;
482 /* Initialize obstack for our rtl allocation. */
483 gcc_obstack_init (&reload_obstack);
484 reload_startobj = (char *) obstack_alloc (&reload_obstack, 0);
486 INIT_REG_SET (&spilled_pseudos);
487 INIT_REG_SET (&pseudos_counted);
490 /* List of insn chains that are currently unused. */
491 static struct insn_chain *unused_insn_chains = 0;
493 /* Allocate an empty insn_chain structure. */
494 struct insn_chain *
495 new_insn_chain (void)
497 struct insn_chain *c;
499 if (unused_insn_chains == 0)
501 c = (struct insn_chain *)
502 obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
503 INIT_REG_SET (&c->live_throughout);
504 INIT_REG_SET (&c->dead_or_set);
506 else
508 c = unused_insn_chains;
509 unused_insn_chains = c->next;
511 c->is_caller_save_insn = 0;
512 c->need_operand_change = 0;
513 c->need_reload = 0;
514 c->need_elim = 0;
515 return c;
518 /* Small utility function to set all regs in hard reg set TO which are
519 allocated to pseudos in regset FROM. */
521 void
522 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
524 unsigned int regno;
526 EXECUTE_IF_SET_IN_REG_SET
527 (from, FIRST_PSEUDO_REGISTER, regno,
529 int r = reg_renumber[regno];
530 int nregs;
532 if (r < 0)
534 /* reload_combine uses the information from
535 BASIC_BLOCK->global_live_at_start, which might still
536 contain registers that have not actually been allocated
537 since they have an equivalence. */
538 if (! reload_completed)
539 abort ();
541 else
543 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (regno));
544 while (nregs-- > 0)
545 SET_HARD_REG_BIT (*to, r + nregs);
550 /* Replace all pseudos found in LOC with their corresponding
551 equivalences. */
553 static void
554 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
556 rtx x = *loc;
557 enum rtx_code code;
558 const char *fmt;
559 int i, j;
561 if (! x)
562 return;
564 code = GET_CODE (x);
565 if (code == REG)
567 unsigned int regno = REGNO (x);
569 if (regno < FIRST_PSEUDO_REGISTER)
570 return;
572 x = eliminate_regs (x, mem_mode, usage);
573 if (x != *loc)
575 *loc = x;
576 replace_pseudos_in (loc, mem_mode, usage);
577 return;
580 if (reg_equiv_constant[regno])
581 *loc = reg_equiv_constant[regno];
582 else if (reg_equiv_mem[regno])
583 *loc = reg_equiv_mem[regno];
584 else if (reg_equiv_address[regno])
585 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
586 else if (GET_CODE (regno_reg_rtx[regno]) != REG
587 || REGNO (regno_reg_rtx[regno]) != regno)
588 *loc = regno_reg_rtx[regno];
589 else
590 abort ();
592 return;
594 else if (code == MEM)
596 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
597 return;
600 /* Process each of our operands recursively. */
601 fmt = GET_RTX_FORMAT (code);
602 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
603 if (*fmt == 'e')
604 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
605 else if (*fmt == 'E')
606 for (j = 0; j < XVECLEN (x, i); j++)
607 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
611 /* Global variables used by reload and its subroutines. */
613 /* Set during calculate_needs if an insn needs register elimination. */
614 static int something_needs_elimination;
615 /* Set during calculate_needs if an insn needs an operand changed. */
616 int something_needs_operands_changed;
618 /* Nonzero means we couldn't get enough spill regs. */
619 static int failure;
621 /* Main entry point for the reload pass.
623 FIRST is the first insn of the function being compiled.
625 GLOBAL nonzero means we were called from global_alloc
626 and should attempt to reallocate any pseudoregs that we
627 displace from hard regs we will use for reloads.
628 If GLOBAL is zero, we do not have enough information to do that,
629 so any pseudo reg that is spilled must go to the stack.
631 Return value is nonzero if reload failed
632 and we must not do any more for this function. */
635 reload (rtx first, int global)
637 int i;
638 rtx insn;
639 struct elim_table *ep;
640 basic_block bb;
642 /* Make sure even insns with volatile mem refs are recognizable. */
643 init_recog ();
645 failure = 0;
647 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
649 /* Make sure that the last insn in the chain
650 is not something that needs reloading. */
651 emit_note (NOTE_INSN_DELETED);
653 /* Enable find_equiv_reg to distinguish insns made by reload. */
654 reload_first_uid = get_max_uid ();
656 #ifdef SECONDARY_MEMORY_NEEDED
657 /* Initialize the secondary memory table. */
658 clear_secondary_mem ();
659 #endif
661 /* We don't have a stack slot for any spill reg yet. */
662 memset ((char *) spill_stack_slot, 0, sizeof spill_stack_slot);
663 memset ((char *) spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
665 /* Initialize the save area information for caller-save, in case some
666 are needed. */
667 init_save_areas ();
669 /* Compute which hard registers are now in use
670 as homes for pseudo registers.
671 This is done here rather than (eg) in global_alloc
672 because this point is reached even if not optimizing. */
673 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
674 mark_home_live (i);
676 /* A function that receives a nonlocal goto must save all call-saved
677 registers. */
678 if (current_function_has_nonlocal_label)
679 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
680 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
681 regs_ever_live[i] = 1;
683 /* Find all the pseudo registers that didn't get hard regs
684 but do have known equivalent constants or memory slots.
685 These include parameters (known equivalent to parameter slots)
686 and cse'd or loop-moved constant memory addresses.
688 Record constant equivalents in reg_equiv_constant
689 so they will be substituted by find_reloads.
690 Record memory equivalents in reg_mem_equiv so they can
691 be substituted eventually by altering the REG-rtx's. */
693 reg_equiv_constant = (rtx *) xcalloc (max_regno, sizeof (rtx));
694 reg_equiv_mem = (rtx *) xcalloc (max_regno, sizeof (rtx));
695 reg_equiv_init = (rtx *) xcalloc (max_regno, sizeof (rtx));
696 reg_equiv_address = (rtx *) xcalloc (max_regno, sizeof (rtx));
697 reg_max_ref_width = (unsigned int *) xcalloc (max_regno, sizeof (int));
698 reg_old_renumber = (short *) xcalloc (max_regno, sizeof (short));
699 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
700 pseudo_forbidden_regs
701 = (HARD_REG_SET *) xmalloc (max_regno * sizeof (HARD_REG_SET));
702 pseudo_previous_regs
703 = (HARD_REG_SET *) xcalloc (max_regno, sizeof (HARD_REG_SET));
705 CLEAR_HARD_REG_SET (bad_spill_regs_global);
707 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
708 Also find all paradoxical subregs and find largest such for each pseudo.
709 On machines with small register classes, record hard registers that
710 are used for user variables. These can never be used for spills.
711 Also look for a "constant" REG_SETJMP. This means that all
712 caller-saved registers must be marked live. */
714 num_eliminable_invariants = 0;
715 for (insn = first; insn; insn = NEXT_INSN (insn))
717 rtx set = single_set (insn);
719 /* We may introduce USEs that we want to remove at the end, so
720 we'll mark them with QImode. Make sure there are no
721 previously-marked insns left by say regmove. */
722 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
723 && GET_MODE (insn) != VOIDmode)
724 PUT_MODE (insn, VOIDmode);
726 if (GET_CODE (insn) == CALL_INSN
727 && find_reg_note (insn, REG_SETJMP, NULL))
728 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
729 if (! call_used_regs[i])
730 regs_ever_live[i] = 1;
732 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
734 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
735 if (note
736 #ifdef LEGITIMATE_PIC_OPERAND_P
737 && (! function_invariant_p (XEXP (note, 0))
738 || ! flag_pic
739 /* A function invariant is often CONSTANT_P but may
740 include a register. We promise to only pass
741 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
742 || (CONSTANT_P (XEXP (note, 0))
743 && LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0))))
744 #endif
747 rtx x = XEXP (note, 0);
748 i = REGNO (SET_DEST (set));
749 if (i > LAST_VIRTUAL_REGISTER)
751 /* It can happen that a REG_EQUIV note contains a MEM
752 that is not a legitimate memory operand. As later
753 stages of reload assume that all addresses found
754 in the reg_equiv_* arrays were originally legitimate,
755 we ignore such REG_EQUIV notes. */
756 if (memory_operand (x, VOIDmode))
758 /* Always unshare the equivalence, so we can
759 substitute into this insn without touching the
760 equivalence. */
761 reg_equiv_memory_loc[i] = copy_rtx (x);
763 else if (function_invariant_p (x))
765 if (GET_CODE (x) == PLUS)
767 /* This is PLUS of frame pointer and a constant,
768 and might be shared. Unshare it. */
769 reg_equiv_constant[i] = copy_rtx (x);
770 num_eliminable_invariants++;
772 else if (x == frame_pointer_rtx
773 || x == arg_pointer_rtx)
775 reg_equiv_constant[i] = x;
776 num_eliminable_invariants++;
778 else if (LEGITIMATE_CONSTANT_P (x))
779 reg_equiv_constant[i] = x;
780 else
782 reg_equiv_memory_loc[i]
783 = force_const_mem (GET_MODE (SET_DEST (set)), x);
784 if (!reg_equiv_memory_loc[i])
785 continue;
788 else
789 continue;
791 /* If this register is being made equivalent to a MEM
792 and the MEM is not SET_SRC, the equivalencing insn
793 is one with the MEM as a SET_DEST and it occurs later.
794 So don't mark this insn now. */
795 if (GET_CODE (x) != MEM
796 || rtx_equal_p (SET_SRC (set), x))
797 reg_equiv_init[i]
798 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
803 /* If this insn is setting a MEM from a register equivalent to it,
804 this is the equivalencing insn. */
805 else if (set && GET_CODE (SET_DEST (set)) == MEM
806 && GET_CODE (SET_SRC (set)) == REG
807 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
808 && rtx_equal_p (SET_DEST (set),
809 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
810 reg_equiv_init[REGNO (SET_SRC (set))]
811 = gen_rtx_INSN_LIST (VOIDmode, insn,
812 reg_equiv_init[REGNO (SET_SRC (set))]);
814 if (INSN_P (insn))
815 scan_paradoxical_subregs (PATTERN (insn));
818 init_elim_table ();
820 first_label_num = get_first_label_num ();
821 num_labels = max_label_num () - first_label_num;
823 /* Allocate the tables used to store offset information at labels. */
824 /* We used to use alloca here, but the size of what it would try to
825 allocate would occasionally cause it to exceed the stack limit and
826 cause a core dump. */
827 offsets_known_at = xmalloc (num_labels);
828 offsets_at
829 = (int (*)[NUM_ELIMINABLE_REGS])
830 xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
832 /* Alter each pseudo-reg rtx to contain its hard reg number.
833 Assign stack slots to the pseudos that lack hard regs or equivalents.
834 Do not touch virtual registers. */
836 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
837 alter_reg (i, -1);
839 /* If we have some registers we think can be eliminated, scan all insns to
840 see if there is an insn that sets one of these registers to something
841 other than itself plus a constant. If so, the register cannot be
842 eliminated. Doing this scan here eliminates an extra pass through the
843 main reload loop in the most common case where register elimination
844 cannot be done. */
845 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
846 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
847 || GET_CODE (insn) == CALL_INSN)
848 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
850 maybe_fix_stack_asms ();
852 insns_need_reload = 0;
853 something_needs_elimination = 0;
855 /* Initialize to -1, which means take the first spill register. */
856 last_spill_reg = -1;
858 /* Spill any hard regs that we know we can't eliminate. */
859 CLEAR_HARD_REG_SET (used_spill_regs);
860 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
861 if (! ep->can_eliminate)
862 spill_hard_reg (ep->from, 1);
864 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
865 if (frame_pointer_needed)
866 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
867 #endif
868 finish_spills (global);
870 /* From now on, we may need to generate moves differently. We may also
871 allow modifications of insns which cause them to not be recognized.
872 Any such modifications will be cleaned up during reload itself. */
873 reload_in_progress = 1;
875 /* This loop scans the entire function each go-round
876 and repeats until one repetition spills no additional hard regs. */
877 for (;;)
879 int something_changed;
880 int did_spill;
882 HOST_WIDE_INT starting_frame_size;
884 /* Round size of stack frame to stack_alignment_needed. This must be done
885 here because the stack size may be a part of the offset computation
886 for register elimination, and there might have been new stack slots
887 created in the last iteration of this loop. */
888 if (cfun->stack_alignment_needed)
889 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
891 starting_frame_size = get_frame_size ();
893 set_initial_elim_offsets ();
894 set_initial_label_offsets ();
896 /* For each pseudo register that has an equivalent location defined,
897 try to eliminate any eliminable registers (such as the frame pointer)
898 assuming initial offsets for the replacement register, which
899 is the normal case.
901 If the resulting location is directly addressable, substitute
902 the MEM we just got directly for the old REG.
904 If it is not addressable but is a constant or the sum of a hard reg
905 and constant, it is probably not addressable because the constant is
906 out of range, in that case record the address; we will generate
907 hairy code to compute the address in a register each time it is
908 needed. Similarly if it is a hard register, but one that is not
909 valid as an address register.
911 If the location is not addressable, but does not have one of the
912 above forms, assign a stack slot. We have to do this to avoid the
913 potential of producing lots of reloads if, e.g., a location involves
914 a pseudo that didn't get a hard register and has an equivalent memory
915 location that also involves a pseudo that didn't get a hard register.
917 Perhaps at some point we will improve reload_when_needed handling
918 so this problem goes away. But that's very hairy. */
920 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
921 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
923 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
925 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
926 XEXP (x, 0)))
927 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
928 else if (CONSTANT_P (XEXP (x, 0))
929 || (GET_CODE (XEXP (x, 0)) == REG
930 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
931 || (GET_CODE (XEXP (x, 0)) == PLUS
932 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
933 && (REGNO (XEXP (XEXP (x, 0), 0))
934 < FIRST_PSEUDO_REGISTER)
935 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
936 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
937 else
939 /* Make a new stack slot. Then indicate that something
940 changed so we go back and recompute offsets for
941 eliminable registers because the allocation of memory
942 below might change some offset. reg_equiv_{mem,address}
943 will be set up for this pseudo on the next pass around
944 the loop. */
945 reg_equiv_memory_loc[i] = 0;
946 reg_equiv_init[i] = 0;
947 alter_reg (i, -1);
951 if (caller_save_needed)
952 setup_save_areas ();
954 /* If we allocated another stack slot, redo elimination bookkeeping. */
955 if (starting_frame_size != get_frame_size ())
956 continue;
958 if (caller_save_needed)
960 save_call_clobbered_regs ();
961 /* That might have allocated new insn_chain structures. */
962 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
965 calculate_needs_all_insns (global);
967 CLEAR_REG_SET (&spilled_pseudos);
968 did_spill = 0;
970 something_changed = 0;
972 /* If we allocated any new memory locations, make another pass
973 since it might have changed elimination offsets. */
974 if (starting_frame_size != get_frame_size ())
975 something_changed = 1;
978 HARD_REG_SET to_spill;
979 CLEAR_HARD_REG_SET (to_spill);
980 update_eliminables (&to_spill);
981 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
982 if (TEST_HARD_REG_BIT (to_spill, i))
984 spill_hard_reg (i, 1);
985 did_spill = 1;
987 /* Regardless of the state of spills, if we previously had
988 a register that we thought we could eliminate, but now can
989 not eliminate, we must run another pass.
991 Consider pseudos which have an entry in reg_equiv_* which
992 reference an eliminable register. We must make another pass
993 to update reg_equiv_* so that we do not substitute in the
994 old value from when we thought the elimination could be
995 performed. */
996 something_changed = 1;
1000 select_reload_regs ();
1001 if (failure)
1002 goto failed;
1004 if (insns_need_reload != 0 || did_spill)
1005 something_changed |= finish_spills (global);
1007 if (! something_changed)
1008 break;
1010 if (caller_save_needed)
1011 delete_caller_save_insns ();
1013 obstack_free (&reload_obstack, reload_firstobj);
1016 /* If global-alloc was run, notify it of any register eliminations we have
1017 done. */
1018 if (global)
1019 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1020 if (ep->can_eliminate)
1021 mark_elimination (ep->from, ep->to);
1023 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1024 If that insn didn't set the register (i.e., it copied the register to
1025 memory), just delete that insn instead of the equivalencing insn plus
1026 anything now dead. If we call delete_dead_insn on that insn, we may
1027 delete the insn that actually sets the register if the register dies
1028 there and that is incorrect. */
1030 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1032 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1034 rtx list;
1035 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1037 rtx equiv_insn = XEXP (list, 0);
1039 /* If we already deleted the insn or if it may trap, we can't
1040 delete it. The latter case shouldn't happen, but can
1041 if an insn has a variable address, gets a REG_EH_REGION
1042 note added to it, and then gets converted into an load
1043 from a constant address. */
1044 if (GET_CODE (equiv_insn) == NOTE
1045 || can_throw_internal (equiv_insn))
1047 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1048 delete_dead_insn (equiv_insn);
1049 else
1051 PUT_CODE (equiv_insn, NOTE);
1052 NOTE_SOURCE_FILE (equiv_insn) = 0;
1053 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1059 /* Use the reload registers where necessary
1060 by generating move instructions to move the must-be-register
1061 values into or out of the reload registers. */
1063 if (insns_need_reload != 0 || something_needs_elimination
1064 || something_needs_operands_changed)
1066 HOST_WIDE_INT old_frame_size = get_frame_size ();
1068 reload_as_needed (global);
1070 if (old_frame_size != get_frame_size ())
1071 abort ();
1073 if (num_eliminable)
1074 verify_initial_elim_offsets ();
1077 /* If we were able to eliminate the frame pointer, show that it is no
1078 longer live at the start of any basic block. If it ls live by
1079 virtue of being in a pseudo, that pseudo will be marked live
1080 and hence the frame pointer will be known to be live via that
1081 pseudo. */
1083 if (! frame_pointer_needed)
1084 FOR_EACH_BB (bb)
1085 CLEAR_REGNO_REG_SET (bb->global_live_at_start,
1086 HARD_FRAME_POINTER_REGNUM);
1088 /* Come here (with failure set nonzero) if we can't get enough spill regs
1089 and we decide not to abort about it. */
1090 failed:
1092 CLEAR_REG_SET (&spilled_pseudos);
1093 reload_in_progress = 0;
1095 /* Now eliminate all pseudo regs by modifying them into
1096 their equivalent memory references.
1097 The REG-rtx's for the pseudos are modified in place,
1098 so all insns that used to refer to them now refer to memory.
1100 For a reg that has a reg_equiv_address, all those insns
1101 were changed by reloading so that no insns refer to it any longer;
1102 but the DECL_RTL of a variable decl may refer to it,
1103 and if so this causes the debugging info to mention the variable. */
1105 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1107 rtx addr = 0;
1109 if (reg_equiv_mem[i])
1110 addr = XEXP (reg_equiv_mem[i], 0);
1112 if (reg_equiv_address[i])
1113 addr = reg_equiv_address[i];
1115 if (addr)
1117 if (reg_renumber[i] < 0)
1119 rtx reg = regno_reg_rtx[i];
1121 REG_USERVAR_P (reg) = 0;
1122 PUT_CODE (reg, MEM);
1123 XEXP (reg, 0) = addr;
1124 if (reg_equiv_memory_loc[i])
1125 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1126 else
1128 RTX_UNCHANGING_P (reg) = MEM_IN_STRUCT_P (reg)
1129 = MEM_SCALAR_P (reg) = 0;
1130 MEM_ATTRS (reg) = 0;
1133 else if (reg_equiv_mem[i])
1134 XEXP (reg_equiv_mem[i], 0) = addr;
1138 /* We must set reload_completed now since the cleanup_subreg_operands call
1139 below will re-recognize each insn and reload may have generated insns
1140 which are only valid during and after reload. */
1141 reload_completed = 1;
1143 /* Make a pass over all the insns and delete all USEs which we inserted
1144 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1145 notes. Delete all CLOBBER insns, except those that refer to the return
1146 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1147 from misarranging variable-array code, and simplify (subreg (reg))
1148 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1149 are no longer useful or accurate. Strip and regenerate REG_INC notes
1150 that may have been moved around. */
1152 for (insn = first; insn; insn = NEXT_INSN (insn))
1153 if (INSN_P (insn))
1155 rtx *pnote;
1157 if (GET_CODE (insn) == CALL_INSN)
1158 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1159 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1161 if ((GET_CODE (PATTERN (insn)) == USE
1162 /* We mark with QImode USEs introduced by reload itself. */
1163 && (GET_MODE (insn) == QImode
1164 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1165 || (GET_CODE (PATTERN (insn)) == CLOBBER
1166 && (GET_CODE (XEXP (PATTERN (insn), 0)) != MEM
1167 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1168 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1169 && XEXP (XEXP (PATTERN (insn), 0), 0)
1170 != stack_pointer_rtx))
1171 && (GET_CODE (XEXP (PATTERN (insn), 0)) != REG
1172 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1174 delete_insn (insn);
1175 continue;
1178 /* Some CLOBBERs may survive until here and still reference unassigned
1179 pseudos with const equivalent, which may in turn cause ICE in later
1180 passes if the reference remains in place. */
1181 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1182 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1183 VOIDmode, PATTERN (insn));
1185 pnote = &REG_NOTES (insn);
1186 while (*pnote != 0)
1188 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1189 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1190 || REG_NOTE_KIND (*pnote) == REG_INC
1191 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1192 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1193 *pnote = XEXP (*pnote, 1);
1194 else
1195 pnote = &XEXP (*pnote, 1);
1198 #ifdef AUTO_INC_DEC
1199 add_auto_inc_notes (insn, PATTERN (insn));
1200 #endif
1202 /* And simplify (subreg (reg)) if it appears as an operand. */
1203 cleanup_subreg_operands (insn);
1206 /* If we are doing stack checking, give a warning if this function's
1207 frame size is larger than we expect. */
1208 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1210 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1211 static int verbose_warned = 0;
1213 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1214 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1215 size += UNITS_PER_WORD;
1217 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1219 warning ("frame size too large for reliable stack checking");
1220 if (! verbose_warned)
1222 warning ("try reducing the number of local variables");
1223 verbose_warned = 1;
1228 /* Indicate that we no longer have known memory locations or constants. */
1229 if (reg_equiv_constant)
1230 free (reg_equiv_constant);
1231 reg_equiv_constant = 0;
1232 if (reg_equiv_memory_loc)
1233 free (reg_equiv_memory_loc);
1234 reg_equiv_memory_loc = 0;
1236 if (offsets_known_at)
1237 free (offsets_known_at);
1238 if (offsets_at)
1239 free (offsets_at);
1241 free (reg_equiv_mem);
1242 free (reg_equiv_init);
1243 free (reg_equiv_address);
1244 free (reg_max_ref_width);
1245 free (reg_old_renumber);
1246 free (pseudo_previous_regs);
1247 free (pseudo_forbidden_regs);
1249 CLEAR_HARD_REG_SET (used_spill_regs);
1250 for (i = 0; i < n_spills; i++)
1251 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1253 /* Free all the insn_chain structures at once. */
1254 obstack_free (&reload_obstack, reload_startobj);
1255 unused_insn_chains = 0;
1256 fixup_abnormal_edges ();
1258 /* Replacing pseudos with their memory equivalents might have
1259 created shared rtx. Subsequent passes would get confused
1260 by this, so unshare everything here. */
1261 unshare_all_rtl_again (first);
1263 return failure;
1266 /* Yet another special case. Unfortunately, reg-stack forces people to
1267 write incorrect clobbers in asm statements. These clobbers must not
1268 cause the register to appear in bad_spill_regs, otherwise we'll call
1269 fatal_insn later. We clear the corresponding regnos in the live
1270 register sets to avoid this.
1271 The whole thing is rather sick, I'm afraid. */
1273 static void
1274 maybe_fix_stack_asms (void)
1276 #ifdef STACK_REGS
1277 const char *constraints[MAX_RECOG_OPERANDS];
1278 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1279 struct insn_chain *chain;
1281 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1283 int i, noperands;
1284 HARD_REG_SET clobbered, allowed;
1285 rtx pat;
1287 if (! INSN_P (chain->insn)
1288 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1289 continue;
1290 pat = PATTERN (chain->insn);
1291 if (GET_CODE (pat) != PARALLEL)
1292 continue;
1294 CLEAR_HARD_REG_SET (clobbered);
1295 CLEAR_HARD_REG_SET (allowed);
1297 /* First, make a mask of all stack regs that are clobbered. */
1298 for (i = 0; i < XVECLEN (pat, 0); i++)
1300 rtx t = XVECEXP (pat, 0, i);
1301 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1302 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1305 /* Get the operand values and constraints out of the insn. */
1306 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1307 constraints, operand_mode);
1309 /* For every operand, see what registers are allowed. */
1310 for (i = 0; i < noperands; i++)
1312 const char *p = constraints[i];
1313 /* For every alternative, we compute the class of registers allowed
1314 for reloading in CLS, and merge its contents into the reg set
1315 ALLOWED. */
1316 int cls = (int) NO_REGS;
1318 for (;;)
1320 char c = *p;
1322 if (c == '\0' || c == ',' || c == '#')
1324 /* End of one alternative - mark the regs in the current
1325 class, and reset the class. */
1326 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1327 cls = NO_REGS;
1328 p++;
1329 if (c == '#')
1330 do {
1331 c = *p++;
1332 } while (c != '\0' && c != ',');
1333 if (c == '\0')
1334 break;
1335 continue;
1338 switch (c)
1340 case '=': case '+': case '*': case '%': case '?': case '!':
1341 case '0': case '1': case '2': case '3': case '4': case 'm':
1342 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1343 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1344 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1345 case 'P':
1346 break;
1348 case 'p':
1349 cls = (int) reg_class_subunion[cls]
1350 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1351 break;
1353 case 'g':
1354 case 'r':
1355 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1356 break;
1358 default:
1359 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1360 cls = (int) reg_class_subunion[cls]
1361 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1362 else
1363 cls = (int) reg_class_subunion[cls]
1364 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1366 p += CONSTRAINT_LEN (c, p);
1369 /* Those of the registers which are clobbered, but allowed by the
1370 constraints, must be usable as reload registers. So clear them
1371 out of the life information. */
1372 AND_HARD_REG_SET (allowed, clobbered);
1373 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1374 if (TEST_HARD_REG_BIT (allowed, i))
1376 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1377 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1381 #endif
1384 /* Copy the global variables n_reloads and rld into the corresponding elts
1385 of CHAIN. */
1386 static void
1387 copy_reloads (struct insn_chain *chain)
1389 chain->n_reloads = n_reloads;
1390 chain->rld
1391 = (struct reload *) obstack_alloc (&reload_obstack,
1392 n_reloads * sizeof (struct reload));
1393 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1394 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1397 /* Walk the chain of insns, and determine for each whether it needs reloads
1398 and/or eliminations. Build the corresponding insns_need_reload list, and
1399 set something_needs_elimination as appropriate. */
1400 static void
1401 calculate_needs_all_insns (int global)
1403 struct insn_chain **pprev_reload = &insns_need_reload;
1404 struct insn_chain *chain, *next = 0;
1406 something_needs_elimination = 0;
1408 reload_insn_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
1409 for (chain = reload_insn_chain; chain != 0; chain = next)
1411 rtx insn = chain->insn;
1413 next = chain->next;
1415 /* Clear out the shortcuts. */
1416 chain->n_reloads = 0;
1417 chain->need_elim = 0;
1418 chain->need_reload = 0;
1419 chain->need_operand_change = 0;
1421 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1422 include REG_LABEL), we need to see what effects this has on the
1423 known offsets at labels. */
1425 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1426 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1427 set_label_offsets (insn, insn, 0);
1429 if (INSN_P (insn))
1431 rtx old_body = PATTERN (insn);
1432 int old_code = INSN_CODE (insn);
1433 rtx old_notes = REG_NOTES (insn);
1434 int did_elimination = 0;
1435 int operands_changed = 0;
1436 rtx set = single_set (insn);
1438 /* Skip insns that only set an equivalence. */
1439 if (set && GET_CODE (SET_DEST (set)) == REG
1440 && reg_renumber[REGNO (SET_DEST (set))] < 0
1441 && reg_equiv_constant[REGNO (SET_DEST (set))])
1442 continue;
1444 /* If needed, eliminate any eliminable registers. */
1445 if (num_eliminable || num_eliminable_invariants)
1446 did_elimination = eliminate_regs_in_insn (insn, 0);
1448 /* Analyze the instruction. */
1449 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1450 global, spill_reg_order);
1452 /* If a no-op set needs more than one reload, this is likely
1453 to be something that needs input address reloads. We
1454 can't get rid of this cleanly later, and it is of no use
1455 anyway, so discard it now.
1456 We only do this when expensive_optimizations is enabled,
1457 since this complements reload inheritance / output
1458 reload deletion, and it can make debugging harder. */
1459 if (flag_expensive_optimizations && n_reloads > 1)
1461 rtx set = single_set (insn);
1462 if (set
1463 && SET_SRC (set) == SET_DEST (set)
1464 && GET_CODE (SET_SRC (set)) == REG
1465 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1467 delete_insn (insn);
1468 /* Delete it from the reload chain. */
1469 if (chain->prev)
1470 chain->prev->next = next;
1471 else
1472 reload_insn_chain = next;
1473 if (next)
1474 next->prev = chain->prev;
1475 chain->next = unused_insn_chains;
1476 unused_insn_chains = chain;
1477 continue;
1480 if (num_eliminable)
1481 update_eliminable_offsets ();
1483 /* Remember for later shortcuts which insns had any reloads or
1484 register eliminations. */
1485 chain->need_elim = did_elimination;
1486 chain->need_reload = n_reloads > 0;
1487 chain->need_operand_change = operands_changed;
1489 /* Discard any register replacements done. */
1490 if (did_elimination)
1492 obstack_free (&reload_obstack, reload_insn_firstobj);
1493 PATTERN (insn) = old_body;
1494 INSN_CODE (insn) = old_code;
1495 REG_NOTES (insn) = old_notes;
1496 something_needs_elimination = 1;
1499 something_needs_operands_changed |= operands_changed;
1501 if (n_reloads != 0)
1503 copy_reloads (chain);
1504 *pprev_reload = chain;
1505 pprev_reload = &chain->next_need_reload;
1509 *pprev_reload = 0;
1512 /* Comparison function for qsort to decide which of two reloads
1513 should be handled first. *P1 and *P2 are the reload numbers. */
1515 static int
1516 reload_reg_class_lower (const void *r1p, const void *r2p)
1518 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1519 int t;
1521 /* Consider required reloads before optional ones. */
1522 t = rld[r1].optional - rld[r2].optional;
1523 if (t != 0)
1524 return t;
1526 /* Count all solitary classes before non-solitary ones. */
1527 t = ((reg_class_size[(int) rld[r2].class] == 1)
1528 - (reg_class_size[(int) rld[r1].class] == 1));
1529 if (t != 0)
1530 return t;
1532 /* Aside from solitaires, consider all multi-reg groups first. */
1533 t = rld[r2].nregs - rld[r1].nregs;
1534 if (t != 0)
1535 return t;
1537 /* Consider reloads in order of increasing reg-class number. */
1538 t = (int) rld[r1].class - (int) rld[r2].class;
1539 if (t != 0)
1540 return t;
1542 /* If reloads are equally urgent, sort by reload number,
1543 so that the results of qsort leave nothing to chance. */
1544 return r1 - r2;
1547 /* The cost of spilling each hard reg. */
1548 static int spill_cost[FIRST_PSEUDO_REGISTER];
1550 /* When spilling multiple hard registers, we use SPILL_COST for the first
1551 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1552 only the first hard reg for a multi-reg pseudo. */
1553 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1555 /* Update the spill cost arrays, considering that pseudo REG is live. */
1557 static void
1558 count_pseudo (int reg)
1560 int freq = REG_FREQ (reg);
1561 int r = reg_renumber[reg];
1562 int nregs;
1564 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1565 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1566 return;
1568 SET_REGNO_REG_SET (&pseudos_counted, reg);
1570 if (r < 0)
1571 abort ();
1573 spill_add_cost[r] += freq;
1575 nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1576 while (nregs-- > 0)
1577 spill_cost[r + nregs] += freq;
1580 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1581 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1583 static void
1584 order_regs_for_reload (struct insn_chain *chain)
1586 int i;
1587 HARD_REG_SET used_by_pseudos;
1588 HARD_REG_SET used_by_pseudos2;
1590 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1592 memset (spill_cost, 0, sizeof spill_cost);
1593 memset (spill_add_cost, 0, sizeof spill_add_cost);
1595 /* Count number of uses of each hard reg by pseudo regs allocated to it
1596 and then order them by decreasing use. First exclude hard registers
1597 that are live in or across this insn. */
1599 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1600 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1601 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1602 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1604 /* Now find out which pseudos are allocated to it, and update
1605 hard_reg_n_uses. */
1606 CLEAR_REG_SET (&pseudos_counted);
1608 EXECUTE_IF_SET_IN_REG_SET
1609 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
1611 count_pseudo (i);
1613 EXECUTE_IF_SET_IN_REG_SET
1614 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
1616 count_pseudo (i);
1618 CLEAR_REG_SET (&pseudos_counted);
1621 /* Vector of reload-numbers showing the order in which the reloads should
1622 be processed. */
1623 static short reload_order[MAX_RELOADS];
1625 /* This is used to keep track of the spill regs used in one insn. */
1626 static HARD_REG_SET used_spill_regs_local;
1628 /* We decided to spill hard register SPILLED, which has a size of
1629 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1630 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1631 update SPILL_COST/SPILL_ADD_COST. */
1633 static void
1634 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1636 int r = reg_renumber[reg];
1637 int nregs = HARD_REGNO_NREGS (r, PSEUDO_REGNO_MODE (reg));
1639 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1640 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1641 return;
1643 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1645 spill_add_cost[r] -= REG_FREQ (reg);
1646 while (nregs-- > 0)
1647 spill_cost[r + nregs] -= REG_FREQ (reg);
1650 /* Find reload register to use for reload number ORDER. */
1652 static int
1653 find_reg (struct insn_chain *chain, int order)
1655 int rnum = reload_order[order];
1656 struct reload *rl = rld + rnum;
1657 int best_cost = INT_MAX;
1658 int best_reg = -1;
1659 unsigned int i, j;
1660 int k;
1661 HARD_REG_SET not_usable;
1662 HARD_REG_SET used_by_other_reload;
1664 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1665 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1666 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1668 CLEAR_HARD_REG_SET (used_by_other_reload);
1669 for (k = 0; k < order; k++)
1671 int other = reload_order[k];
1673 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1674 for (j = 0; j < rld[other].nregs; j++)
1675 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1678 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1680 unsigned int regno = i;
1682 if (! TEST_HARD_REG_BIT (not_usable, regno)
1683 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1684 && HARD_REGNO_MODE_OK (regno, rl->mode))
1686 int this_cost = spill_cost[regno];
1687 int ok = 1;
1688 unsigned int this_nregs = HARD_REGNO_NREGS (regno, rl->mode);
1690 for (j = 1; j < this_nregs; j++)
1692 this_cost += spill_add_cost[regno + j];
1693 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1694 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1695 ok = 0;
1697 if (! ok)
1698 continue;
1699 if (rl->in && GET_CODE (rl->in) == REG && REGNO (rl->in) == regno)
1700 this_cost--;
1701 if (rl->out && GET_CODE (rl->out) == REG && REGNO (rl->out) == regno)
1702 this_cost--;
1703 if (this_cost < best_cost
1704 /* Among registers with equal cost, prefer caller-saved ones, or
1705 use REG_ALLOC_ORDER if it is defined. */
1706 || (this_cost == best_cost
1707 #ifdef REG_ALLOC_ORDER
1708 && (inv_reg_alloc_order[regno]
1709 < inv_reg_alloc_order[best_reg])
1710 #else
1711 && call_used_regs[regno]
1712 && ! call_used_regs[best_reg]
1713 #endif
1716 best_reg = regno;
1717 best_cost = this_cost;
1721 if (best_reg == -1)
1722 return 0;
1724 if (rtl_dump_file)
1725 fprintf (rtl_dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1727 rl->nregs = HARD_REGNO_NREGS (best_reg, rl->mode);
1728 rl->regno = best_reg;
1730 EXECUTE_IF_SET_IN_REG_SET
1731 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j,
1733 count_spilled_pseudo (best_reg, rl->nregs, j);
1736 EXECUTE_IF_SET_IN_REG_SET
1737 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j,
1739 count_spilled_pseudo (best_reg, rl->nregs, j);
1742 for (i = 0; i < rl->nregs; i++)
1744 if (spill_cost[best_reg + i] != 0
1745 || spill_add_cost[best_reg + i] != 0)
1746 abort ();
1747 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1749 return 1;
1752 /* Find more reload regs to satisfy the remaining need of an insn, which
1753 is given by CHAIN.
1754 Do it by ascending class number, since otherwise a reg
1755 might be spilled for a big class and might fail to count
1756 for a smaller class even though it belongs to that class. */
1758 static void
1759 find_reload_regs (struct insn_chain *chain)
1761 int i;
1763 /* In order to be certain of getting the registers we need,
1764 we must sort the reloads into order of increasing register class.
1765 Then our grabbing of reload registers will parallel the process
1766 that provided the reload registers. */
1767 for (i = 0; i < chain->n_reloads; i++)
1769 /* Show whether this reload already has a hard reg. */
1770 if (chain->rld[i].reg_rtx)
1772 int regno = REGNO (chain->rld[i].reg_rtx);
1773 chain->rld[i].regno = regno;
1774 chain->rld[i].nregs
1775 = HARD_REGNO_NREGS (regno, GET_MODE (chain->rld[i].reg_rtx));
1777 else
1778 chain->rld[i].regno = -1;
1779 reload_order[i] = i;
1782 n_reloads = chain->n_reloads;
1783 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1785 CLEAR_HARD_REG_SET (used_spill_regs_local);
1787 if (rtl_dump_file)
1788 fprintf (rtl_dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1790 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1792 /* Compute the order of preference for hard registers to spill. */
1794 order_regs_for_reload (chain);
1796 for (i = 0; i < n_reloads; i++)
1798 int r = reload_order[i];
1800 /* Ignore reloads that got marked inoperative. */
1801 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1802 && ! rld[r].optional
1803 && rld[r].regno == -1)
1804 if (! find_reg (chain, i))
1806 spill_failure (chain->insn, rld[r].class);
1807 failure = 1;
1808 return;
1812 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1813 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1815 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1818 static void
1819 select_reload_regs (void)
1821 struct insn_chain *chain;
1823 /* Try to satisfy the needs for each insn. */
1824 for (chain = insns_need_reload; chain != 0;
1825 chain = chain->next_need_reload)
1826 find_reload_regs (chain);
1829 /* Delete all insns that were inserted by emit_caller_save_insns during
1830 this iteration. */
1831 static void
1832 delete_caller_save_insns (void)
1834 struct insn_chain *c = reload_insn_chain;
1836 while (c != 0)
1838 while (c != 0 && c->is_caller_save_insn)
1840 struct insn_chain *next = c->next;
1841 rtx insn = c->insn;
1843 if (c == reload_insn_chain)
1844 reload_insn_chain = next;
1845 delete_insn (insn);
1847 if (next)
1848 next->prev = c->prev;
1849 if (c->prev)
1850 c->prev->next = next;
1851 c->next = unused_insn_chains;
1852 unused_insn_chains = c;
1853 c = next;
1855 if (c != 0)
1856 c = c->next;
1860 /* Handle the failure to find a register to spill.
1861 INSN should be one of the insns which needed this particular spill reg. */
1863 static void
1864 spill_failure (rtx insn, enum reg_class class)
1866 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1867 if (asm_noperands (PATTERN (insn)) >= 0)
1868 error_for_asm (insn, "can't find a register in class `%s' while reloading `asm'",
1869 reg_class_names[class]);
1870 else
1872 error ("unable to find a register to spill in class `%s'",
1873 reg_class_names[class]);
1874 fatal_insn ("this is the insn:", insn);
1878 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1879 data that is dead in INSN. */
1881 static void
1882 delete_dead_insn (rtx insn)
1884 rtx prev = prev_real_insn (insn);
1885 rtx prev_dest;
1887 /* If the previous insn sets a register that dies in our insn, delete it
1888 too. */
1889 if (prev && GET_CODE (PATTERN (prev)) == SET
1890 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
1891 && reg_mentioned_p (prev_dest, PATTERN (insn))
1892 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1893 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1894 delete_dead_insn (prev);
1896 PUT_CODE (insn, NOTE);
1897 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1898 NOTE_SOURCE_FILE (insn) = 0;
1901 /* Modify the home of pseudo-reg I.
1902 The new home is present in reg_renumber[I].
1904 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1905 or it may be -1, meaning there is none or it is not relevant.
1906 This is used so that all pseudos spilled from a given hard reg
1907 can share one stack slot. */
1909 static void
1910 alter_reg (int i, int from_reg)
1912 /* When outputting an inline function, this can happen
1913 for a reg that isn't actually used. */
1914 if (regno_reg_rtx[i] == 0)
1915 return;
1917 /* If the reg got changed to a MEM at rtl-generation time,
1918 ignore it. */
1919 if (GET_CODE (regno_reg_rtx[i]) != REG)
1920 return;
1922 /* Modify the reg-rtx to contain the new hard reg
1923 number or else to contain its pseudo reg number. */
1924 REGNO (regno_reg_rtx[i])
1925 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1927 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1928 allocate a stack slot for it. */
1930 if (reg_renumber[i] < 0
1931 && REG_N_REFS (i) > 0
1932 && reg_equiv_constant[i] == 0
1933 && reg_equiv_memory_loc[i] == 0)
1935 rtx x;
1936 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1937 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
1938 int adjust = 0;
1940 /* Each pseudo reg has an inherent size which comes from its own mode,
1941 and a total size which provides room for paradoxical subregs
1942 which refer to the pseudo reg in wider modes.
1944 We can use a slot already allocated if it provides both
1945 enough inherent space and enough total space.
1946 Otherwise, we allocate a new slot, making sure that it has no less
1947 inherent space, and no less total space, then the previous slot. */
1948 if (from_reg == -1)
1950 /* No known place to spill from => no slot to reuse. */
1951 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1952 inherent_size == total_size ? 0 : -1);
1953 if (BYTES_BIG_ENDIAN)
1954 /* Cancel the big-endian correction done in assign_stack_local.
1955 Get the address of the beginning of the slot.
1956 This is so we can do a big-endian correction unconditionally
1957 below. */
1958 adjust = inherent_size - total_size;
1960 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
1962 /* Nothing can alias this slot except this pseudo. */
1963 set_mem_alias_set (x, new_alias_set ());
1966 /* Reuse a stack slot if possible. */
1967 else if (spill_stack_slot[from_reg] != 0
1968 && spill_stack_slot_width[from_reg] >= total_size
1969 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1970 >= inherent_size))
1971 x = spill_stack_slot[from_reg];
1973 /* Allocate a bigger slot. */
1974 else
1976 /* Compute maximum size needed, both for inherent size
1977 and for total size. */
1978 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
1979 rtx stack_slot;
1981 if (spill_stack_slot[from_reg])
1983 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1984 > inherent_size)
1985 mode = GET_MODE (spill_stack_slot[from_reg]);
1986 if (spill_stack_slot_width[from_reg] > total_size)
1987 total_size = spill_stack_slot_width[from_reg];
1990 /* Make a slot with that size. */
1991 x = assign_stack_local (mode, total_size,
1992 inherent_size == total_size ? 0 : -1);
1993 stack_slot = x;
1995 /* All pseudos mapped to this slot can alias each other. */
1996 if (spill_stack_slot[from_reg])
1997 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
1998 else
1999 set_mem_alias_set (x, new_alias_set ());
2001 if (BYTES_BIG_ENDIAN)
2003 /* Cancel the big-endian correction done in assign_stack_local.
2004 Get the address of the beginning of the slot.
2005 This is so we can do a big-endian correction unconditionally
2006 below. */
2007 adjust = GET_MODE_SIZE (mode) - total_size;
2008 if (adjust)
2009 stack_slot
2010 = adjust_address_nv (x, mode_for_size (total_size
2011 * BITS_PER_UNIT,
2012 MODE_INT, 1),
2013 adjust);
2016 spill_stack_slot[from_reg] = stack_slot;
2017 spill_stack_slot_width[from_reg] = total_size;
2020 /* On a big endian machine, the "address" of the slot
2021 is the address of the low part that fits its inherent mode. */
2022 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2023 adjust += (total_size - inherent_size);
2025 /* If we have any adjustment to make, or if the stack slot is the
2026 wrong mode, make a new stack slot. */
2027 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2029 /* If we have a decl for the original register, set it for the
2030 memory. If this is a shared MEM, make a copy. */
2031 if (REG_EXPR (regno_reg_rtx[i])
2032 && TREE_CODE_CLASS (TREE_CODE (REG_EXPR (regno_reg_rtx[i]))) == 'd')
2034 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2036 /* We can do this only for the DECLs home pseudo, not for
2037 any copies of it, since otherwise when the stack slot
2038 is reused, nonoverlapping_memrefs_p might think they
2039 cannot overlap. */
2040 if (decl && GET_CODE (decl) == REG && REGNO (decl) == (unsigned) i)
2042 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2043 x = copy_rtx (x);
2045 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2049 /* Save the stack slot for later. */
2050 reg_equiv_memory_loc[i] = x;
2054 /* Mark the slots in regs_ever_live for the hard regs
2055 used by pseudo-reg number REGNO. */
2057 void
2058 mark_home_live (int regno)
2060 int i, lim;
2062 i = reg_renumber[regno];
2063 if (i < 0)
2064 return;
2065 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2066 while (i < lim)
2067 regs_ever_live[i++] = 1;
2070 /* This function handles the tracking of elimination offsets around branches.
2072 X is a piece of RTL being scanned.
2074 INSN is the insn that it came from, if any.
2076 INITIAL_P is nonzero if we are to set the offset to be the initial
2077 offset and zero if we are setting the offset of the label to be the
2078 current offset. */
2080 static void
2081 set_label_offsets (rtx x, rtx insn, int initial_p)
2083 enum rtx_code code = GET_CODE (x);
2084 rtx tem;
2085 unsigned int i;
2086 struct elim_table *p;
2088 switch (code)
2090 case LABEL_REF:
2091 if (LABEL_REF_NONLOCAL_P (x))
2092 return;
2094 x = XEXP (x, 0);
2096 /* ... fall through ... */
2098 case CODE_LABEL:
2099 /* If we know nothing about this label, set the desired offsets. Note
2100 that this sets the offset at a label to be the offset before a label
2101 if we don't know anything about the label. This is not correct for
2102 the label after a BARRIER, but is the best guess we can make. If
2103 we guessed wrong, we will suppress an elimination that might have
2104 been possible had we been able to guess correctly. */
2106 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2108 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2109 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2110 = (initial_p ? reg_eliminate[i].initial_offset
2111 : reg_eliminate[i].offset);
2112 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2115 /* Otherwise, if this is the definition of a label and it is
2116 preceded by a BARRIER, set our offsets to the known offset of
2117 that label. */
2119 else if (x == insn
2120 && (tem = prev_nonnote_insn (insn)) != 0
2121 && GET_CODE (tem) == BARRIER)
2122 set_offsets_for_label (insn);
2123 else
2124 /* If neither of the above cases is true, compare each offset
2125 with those previously recorded and suppress any eliminations
2126 where the offsets disagree. */
2128 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2129 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2130 != (initial_p ? reg_eliminate[i].initial_offset
2131 : reg_eliminate[i].offset))
2132 reg_eliminate[i].can_eliminate = 0;
2134 return;
2136 case JUMP_INSN:
2137 set_label_offsets (PATTERN (insn), insn, initial_p);
2139 /* ... fall through ... */
2141 case INSN:
2142 case CALL_INSN:
2143 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2144 and hence must have all eliminations at their initial offsets. */
2145 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2146 if (REG_NOTE_KIND (tem) == REG_LABEL)
2147 set_label_offsets (XEXP (tem, 0), insn, 1);
2148 return;
2150 case PARALLEL:
2151 case ADDR_VEC:
2152 case ADDR_DIFF_VEC:
2153 /* Each of the labels in the parallel or address vector must be
2154 at their initial offsets. We want the first field for PARALLEL
2155 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2157 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2158 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2159 insn, initial_p);
2160 return;
2162 case SET:
2163 /* We only care about setting PC. If the source is not RETURN,
2164 IF_THEN_ELSE, or a label, disable any eliminations not at
2165 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2166 isn't one of those possibilities. For branches to a label,
2167 call ourselves recursively.
2169 Note that this can disable elimination unnecessarily when we have
2170 a non-local goto since it will look like a non-constant jump to
2171 someplace in the current function. This isn't a significant
2172 problem since such jumps will normally be when all elimination
2173 pairs are back to their initial offsets. */
2175 if (SET_DEST (x) != pc_rtx)
2176 return;
2178 switch (GET_CODE (SET_SRC (x)))
2180 case PC:
2181 case RETURN:
2182 return;
2184 case LABEL_REF:
2185 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2186 return;
2188 case IF_THEN_ELSE:
2189 tem = XEXP (SET_SRC (x), 1);
2190 if (GET_CODE (tem) == LABEL_REF)
2191 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2192 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2193 break;
2195 tem = XEXP (SET_SRC (x), 2);
2196 if (GET_CODE (tem) == LABEL_REF)
2197 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2198 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2199 break;
2200 return;
2202 default:
2203 break;
2206 /* If we reach here, all eliminations must be at their initial
2207 offset because we are doing a jump to a variable address. */
2208 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2209 if (p->offset != p->initial_offset)
2210 p->can_eliminate = 0;
2211 break;
2213 default:
2214 break;
2218 /* Scan X and replace any eliminable registers (such as fp) with a
2219 replacement (such as sp), plus an offset.
2221 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2222 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2223 MEM, we are allowed to replace a sum of a register and the constant zero
2224 with the register, which we cannot do outside a MEM. In addition, we need
2225 to record the fact that a register is referenced outside a MEM.
2227 If INSN is an insn, it is the insn containing X. If we replace a REG
2228 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2229 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2230 the REG is being modified.
2232 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2233 That's used when we eliminate in expressions stored in notes.
2234 This means, do not set ref_outside_mem even if the reference
2235 is outside of MEMs.
2237 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2238 replacements done assuming all offsets are at their initial values. If
2239 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2240 encounter, return the actual location so that find_reloads will do
2241 the proper thing. */
2244 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2246 enum rtx_code code = GET_CODE (x);
2247 struct elim_table *ep;
2248 int regno;
2249 rtx new;
2250 int i, j;
2251 const char *fmt;
2252 int copied = 0;
2254 if (! current_function_decl)
2255 return x;
2257 switch (code)
2259 case CONST_INT:
2260 case CONST_DOUBLE:
2261 case CONST_VECTOR:
2262 case CONST:
2263 case SYMBOL_REF:
2264 case CODE_LABEL:
2265 case PC:
2266 case CC0:
2267 case ASM_INPUT:
2268 case ADDR_VEC:
2269 case ADDR_DIFF_VEC:
2270 case RETURN:
2271 return x;
2273 case ADDRESSOF:
2274 /* This is only for the benefit of the debugging backends, which call
2275 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2276 removed after CSE. */
2277 new = eliminate_regs (XEXP (x, 0), 0, insn);
2278 if (GET_CODE (new) == MEM)
2279 return XEXP (new, 0);
2280 return x;
2282 case REG:
2283 regno = REGNO (x);
2285 /* First handle the case where we encounter a bare register that
2286 is eliminable. Replace it with a PLUS. */
2287 if (regno < FIRST_PSEUDO_REGISTER)
2289 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2290 ep++)
2291 if (ep->from_rtx == x && ep->can_eliminate)
2292 return plus_constant (ep->to_rtx, ep->previous_offset);
2295 else if (reg_renumber && reg_renumber[regno] < 0
2296 && reg_equiv_constant && reg_equiv_constant[regno]
2297 && ! CONSTANT_P (reg_equiv_constant[regno]))
2298 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2299 mem_mode, insn);
2300 return x;
2302 /* You might think handling MINUS in a manner similar to PLUS is a
2303 good idea. It is not. It has been tried multiple times and every
2304 time the change has had to have been reverted.
2306 Other parts of reload know a PLUS is special (gen_reload for example)
2307 and require special code to handle code a reloaded PLUS operand.
2309 Also consider backends where the flags register is clobbered by a
2310 MINUS, but we can emit a PLUS that does not clobber flags (ia32,
2311 lea instruction comes to mind). If we try to reload a MINUS, we
2312 may kill the flags register that was holding a useful value.
2314 So, please before trying to handle MINUS, consider reload as a
2315 whole instead of this little section as well as the backend issues. */
2316 case PLUS:
2317 /* If this is the sum of an eliminable register and a constant, rework
2318 the sum. */
2319 if (GET_CODE (XEXP (x, 0)) == REG
2320 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2321 && CONSTANT_P (XEXP (x, 1)))
2323 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2324 ep++)
2325 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2327 /* The only time we want to replace a PLUS with a REG (this
2328 occurs when the constant operand of the PLUS is the negative
2329 of the offset) is when we are inside a MEM. We won't want
2330 to do so at other times because that would change the
2331 structure of the insn in a way that reload can't handle.
2332 We special-case the commonest situation in
2333 eliminate_regs_in_insn, so just replace a PLUS with a
2334 PLUS here, unless inside a MEM. */
2335 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2336 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2337 return ep->to_rtx;
2338 else
2339 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2340 plus_constant (XEXP (x, 1),
2341 ep->previous_offset));
2344 /* If the register is not eliminable, we are done since the other
2345 operand is a constant. */
2346 return x;
2349 /* If this is part of an address, we want to bring any constant to the
2350 outermost PLUS. We will do this by doing register replacement in
2351 our operands and seeing if a constant shows up in one of them.
2353 Note that there is no risk of modifying the structure of the insn,
2354 since we only get called for its operands, thus we are either
2355 modifying the address inside a MEM, or something like an address
2356 operand of a load-address insn. */
2359 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2360 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2362 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2364 /* If one side is a PLUS and the other side is a pseudo that
2365 didn't get a hard register but has a reg_equiv_constant,
2366 we must replace the constant here since it may no longer
2367 be in the position of any operand. */
2368 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2369 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2370 && reg_renumber[REGNO (new1)] < 0
2371 && reg_equiv_constant != 0
2372 && reg_equiv_constant[REGNO (new1)] != 0)
2373 new1 = reg_equiv_constant[REGNO (new1)];
2374 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2375 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2376 && reg_renumber[REGNO (new0)] < 0
2377 && reg_equiv_constant[REGNO (new0)] != 0)
2378 new0 = reg_equiv_constant[REGNO (new0)];
2380 new = form_sum (new0, new1);
2382 /* As above, if we are not inside a MEM we do not want to
2383 turn a PLUS into something else. We might try to do so here
2384 for an addition of 0 if we aren't optimizing. */
2385 if (! mem_mode && GET_CODE (new) != PLUS)
2386 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2387 else
2388 return new;
2391 return x;
2393 case MULT:
2394 /* If this is the product of an eliminable register and a
2395 constant, apply the distribute law and move the constant out
2396 so that we have (plus (mult ..) ..). This is needed in order
2397 to keep load-address insns valid. This case is pathological.
2398 We ignore the possibility of overflow here. */
2399 if (GET_CODE (XEXP (x, 0)) == REG
2400 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2401 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2402 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2403 ep++)
2404 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2406 if (! mem_mode
2407 /* Refs inside notes don't count for this purpose. */
2408 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2409 || GET_CODE (insn) == INSN_LIST)))
2410 ep->ref_outside_mem = 1;
2412 return
2413 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2414 ep->previous_offset * INTVAL (XEXP (x, 1)));
2417 /* ... fall through ... */
2419 case CALL:
2420 case COMPARE:
2421 /* See comments before PLUS about handling MINUS. */
2422 case MINUS:
2423 case DIV: case UDIV:
2424 case MOD: case UMOD:
2425 case AND: case IOR: case XOR:
2426 case ROTATERT: case ROTATE:
2427 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2428 case NE: case EQ:
2429 case GE: case GT: case GEU: case GTU:
2430 case LE: case LT: case LEU: case LTU:
2432 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2433 rtx new1
2434 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2436 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2437 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2439 return x;
2441 case EXPR_LIST:
2442 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2443 if (XEXP (x, 0))
2445 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2446 if (new != XEXP (x, 0))
2448 /* If this is a REG_DEAD note, it is not valid anymore.
2449 Using the eliminated version could result in creating a
2450 REG_DEAD note for the stack or frame pointer. */
2451 if (GET_MODE (x) == REG_DEAD)
2452 return (XEXP (x, 1)
2453 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2454 : NULL_RTX);
2456 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2460 /* ... fall through ... */
2462 case INSN_LIST:
2463 /* Now do eliminations in the rest of the chain. If this was
2464 an EXPR_LIST, this might result in allocating more memory than is
2465 strictly needed, but it simplifies the code. */
2466 if (XEXP (x, 1))
2468 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2469 if (new != XEXP (x, 1))
2470 return
2471 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2473 return x;
2475 case PRE_INC:
2476 case POST_INC:
2477 case PRE_DEC:
2478 case POST_DEC:
2479 case STRICT_LOW_PART:
2480 case NEG: case NOT:
2481 case SIGN_EXTEND: case ZERO_EXTEND:
2482 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2483 case FLOAT: case FIX:
2484 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2485 case ABS:
2486 case SQRT:
2487 case FFS:
2488 case CLZ:
2489 case CTZ:
2490 case POPCOUNT:
2491 case PARITY:
2492 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2493 if (new != XEXP (x, 0))
2494 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2495 return x;
2497 case SUBREG:
2498 /* Similar to above processing, but preserve SUBREG_BYTE.
2499 Convert (subreg (mem)) to (mem) if not paradoxical.
2500 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2501 pseudo didn't get a hard reg, we must replace this with the
2502 eliminated version of the memory location because push_reload
2503 may do the replacement in certain circumstances. */
2504 if (GET_CODE (SUBREG_REG (x)) == REG
2505 && (GET_MODE_SIZE (GET_MODE (x))
2506 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2507 && reg_equiv_memory_loc != 0
2508 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2510 new = SUBREG_REG (x);
2512 else
2513 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2515 if (new != SUBREG_REG (x))
2517 int x_size = GET_MODE_SIZE (GET_MODE (x));
2518 int new_size = GET_MODE_SIZE (GET_MODE (new));
2520 if (GET_CODE (new) == MEM
2521 && ((x_size < new_size
2522 #ifdef WORD_REGISTER_OPERATIONS
2523 /* On these machines, combine can create rtl of the form
2524 (set (subreg:m1 (reg:m2 R) 0) ...)
2525 where m1 < m2, and expects something interesting to
2526 happen to the entire word. Moreover, it will use the
2527 (reg:m2 R) later, expecting all bits to be preserved.
2528 So if the number of words is the same, preserve the
2529 subreg so that push_reload can see it. */
2530 && ! ((x_size - 1) / UNITS_PER_WORD
2531 == (new_size -1 ) / UNITS_PER_WORD)
2532 #endif
2534 || x_size == new_size)
2536 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2537 else
2538 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2541 return x;
2543 case MEM:
2544 /* This is only for the benefit of the debugging backends, which call
2545 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2546 removed after CSE. */
2547 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2548 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2550 /* Our only special processing is to pass the mode of the MEM to our
2551 recursive call and copy the flags. While we are here, handle this
2552 case more efficiently. */
2553 return
2554 replace_equiv_address_nv (x,
2555 eliminate_regs (XEXP (x, 0),
2556 GET_MODE (x), insn));
2558 case USE:
2559 /* Handle insn_list USE that a call to a pure function may generate. */
2560 new = eliminate_regs (XEXP (x, 0), 0, insn);
2561 if (new != XEXP (x, 0))
2562 return gen_rtx_USE (GET_MODE (x), new);
2563 return x;
2565 case CLOBBER:
2566 case ASM_OPERANDS:
2567 case SET:
2568 abort ();
2570 default:
2571 break;
2574 /* Process each of our operands recursively. If any have changed, make a
2575 copy of the rtx. */
2576 fmt = GET_RTX_FORMAT (code);
2577 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2579 if (*fmt == 'e')
2581 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
2582 if (new != XEXP (x, i) && ! copied)
2584 rtx new_x = rtx_alloc (code);
2585 memcpy (new_x, x,
2586 (sizeof (*new_x) - sizeof (new_x->fld)
2587 + sizeof (new_x->fld[0]) * GET_RTX_LENGTH (code)));
2588 x = new_x;
2589 copied = 1;
2591 XEXP (x, i) = new;
2593 else if (*fmt == 'E')
2595 int copied_vec = 0;
2596 for (j = 0; j < XVECLEN (x, i); j++)
2598 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2599 if (new != XVECEXP (x, i, j) && ! copied_vec)
2601 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2602 XVEC (x, i)->elem);
2603 if (! copied)
2605 rtx new_x = rtx_alloc (code);
2606 memcpy (new_x, x,
2607 (sizeof (*new_x) - sizeof (new_x->fld)
2608 + (sizeof (new_x->fld[0])
2609 * GET_RTX_LENGTH (code))));
2610 x = new_x;
2611 copied = 1;
2613 XVEC (x, i) = new_v;
2614 copied_vec = 1;
2616 XVECEXP (x, i, j) = new;
2621 return x;
2624 /* Scan rtx X for modifications of elimination target registers. Update
2625 the table of eliminables to reflect the changed state. MEM_MODE is
2626 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2628 static void
2629 elimination_effects (rtx x, enum machine_mode mem_mode)
2631 enum rtx_code code = GET_CODE (x);
2632 struct elim_table *ep;
2633 int regno;
2634 int i, j;
2635 const char *fmt;
2637 switch (code)
2639 case CONST_INT:
2640 case CONST_DOUBLE:
2641 case CONST_VECTOR:
2642 case CONST:
2643 case SYMBOL_REF:
2644 case CODE_LABEL:
2645 case PC:
2646 case CC0:
2647 case ASM_INPUT:
2648 case ADDR_VEC:
2649 case ADDR_DIFF_VEC:
2650 case RETURN:
2651 return;
2653 case ADDRESSOF:
2654 abort ();
2656 case REG:
2657 regno = REGNO (x);
2659 /* First handle the case where we encounter a bare register that
2660 is eliminable. Replace it with a PLUS. */
2661 if (regno < FIRST_PSEUDO_REGISTER)
2663 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2664 ep++)
2665 if (ep->from_rtx == x && ep->can_eliminate)
2667 if (! mem_mode)
2668 ep->ref_outside_mem = 1;
2669 return;
2673 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2674 && reg_equiv_constant[regno]
2675 && ! function_invariant_p (reg_equiv_constant[regno]))
2676 elimination_effects (reg_equiv_constant[regno], mem_mode);
2677 return;
2679 case PRE_INC:
2680 case POST_INC:
2681 case PRE_DEC:
2682 case POST_DEC:
2683 case POST_MODIFY:
2684 case PRE_MODIFY:
2685 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2686 if (ep->to_rtx == XEXP (x, 0))
2688 int size = GET_MODE_SIZE (mem_mode);
2690 /* If more bytes than MEM_MODE are pushed, account for them. */
2691 #ifdef PUSH_ROUNDING
2692 if (ep->to_rtx == stack_pointer_rtx)
2693 size = PUSH_ROUNDING (size);
2694 #endif
2695 if (code == PRE_DEC || code == POST_DEC)
2696 ep->offset += size;
2697 else if (code == PRE_INC || code == POST_INC)
2698 ep->offset -= size;
2699 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2700 && GET_CODE (XEXP (x, 1)) == PLUS
2701 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2702 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2703 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2706 /* These two aren't unary operators. */
2707 if (code == POST_MODIFY || code == PRE_MODIFY)
2708 break;
2710 /* Fall through to generic unary operation case. */
2711 case STRICT_LOW_PART:
2712 case NEG: case NOT:
2713 case SIGN_EXTEND: case ZERO_EXTEND:
2714 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2715 case FLOAT: case FIX:
2716 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2717 case ABS:
2718 case SQRT:
2719 case FFS:
2720 case CLZ:
2721 case CTZ:
2722 case POPCOUNT:
2723 case PARITY:
2724 elimination_effects (XEXP (x, 0), mem_mode);
2725 return;
2727 case SUBREG:
2728 if (GET_CODE (SUBREG_REG (x)) == REG
2729 && (GET_MODE_SIZE (GET_MODE (x))
2730 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2731 && reg_equiv_memory_loc != 0
2732 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2733 return;
2735 elimination_effects (SUBREG_REG (x), mem_mode);
2736 return;
2738 case USE:
2739 /* If using a register that is the source of an eliminate we still
2740 think can be performed, note it cannot be performed since we don't
2741 know how this register is used. */
2742 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2743 if (ep->from_rtx == XEXP (x, 0))
2744 ep->can_eliminate = 0;
2746 elimination_effects (XEXP (x, 0), mem_mode);
2747 return;
2749 case CLOBBER:
2750 /* If clobbering a register that is the replacement register for an
2751 elimination we still think can be performed, note that it cannot
2752 be performed. Otherwise, we need not be concerned about it. */
2753 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2754 if (ep->to_rtx == XEXP (x, 0))
2755 ep->can_eliminate = 0;
2757 elimination_effects (XEXP (x, 0), mem_mode);
2758 return;
2760 case SET:
2761 /* Check for setting a register that we know about. */
2762 if (GET_CODE (SET_DEST (x)) == REG)
2764 /* See if this is setting the replacement register for an
2765 elimination.
2767 If DEST is the hard frame pointer, we do nothing because we
2768 assume that all assignments to the frame pointer are for
2769 non-local gotos and are being done at a time when they are valid
2770 and do not disturb anything else. Some machines want to
2771 eliminate a fake argument pointer (or even a fake frame pointer)
2772 with either the real frame or the stack pointer. Assignments to
2773 the hard frame pointer must not prevent this elimination. */
2775 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2776 ep++)
2777 if (ep->to_rtx == SET_DEST (x)
2778 && SET_DEST (x) != hard_frame_pointer_rtx)
2780 /* If it is being incremented, adjust the offset. Otherwise,
2781 this elimination can't be done. */
2782 rtx src = SET_SRC (x);
2784 if (GET_CODE (src) == PLUS
2785 && XEXP (src, 0) == SET_DEST (x)
2786 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2787 ep->offset -= INTVAL (XEXP (src, 1));
2788 else
2789 ep->can_eliminate = 0;
2793 elimination_effects (SET_DEST (x), 0);
2794 elimination_effects (SET_SRC (x), 0);
2795 return;
2797 case MEM:
2798 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2799 abort ();
2801 /* Our only special processing is to pass the mode of the MEM to our
2802 recursive call. */
2803 elimination_effects (XEXP (x, 0), GET_MODE (x));
2804 return;
2806 default:
2807 break;
2810 fmt = GET_RTX_FORMAT (code);
2811 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2813 if (*fmt == 'e')
2814 elimination_effects (XEXP (x, i), mem_mode);
2815 else if (*fmt == 'E')
2816 for (j = 0; j < XVECLEN (x, i); j++)
2817 elimination_effects (XVECEXP (x, i, j), mem_mode);
2821 /* Descend through rtx X and verify that no references to eliminable registers
2822 remain. If any do remain, mark the involved register as not
2823 eliminable. */
2825 static void
2826 check_eliminable_occurrences (rtx x)
2828 const char *fmt;
2829 int i;
2830 enum rtx_code code;
2832 if (x == 0)
2833 return;
2835 code = GET_CODE (x);
2837 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2839 struct elim_table *ep;
2841 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2842 if (ep->from_rtx == x && ep->can_eliminate)
2843 ep->can_eliminate = 0;
2844 return;
2847 fmt = GET_RTX_FORMAT (code);
2848 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2850 if (*fmt == 'e')
2851 check_eliminable_occurrences (XEXP (x, i));
2852 else if (*fmt == 'E')
2854 int j;
2855 for (j = 0; j < XVECLEN (x, i); j++)
2856 check_eliminable_occurrences (XVECEXP (x, i, j));
2861 /* Scan INSN and eliminate all eliminable registers in it.
2863 If REPLACE is nonzero, do the replacement destructively. Also
2864 delete the insn as dead it if it is setting an eliminable register.
2866 If REPLACE is zero, do all our allocations in reload_obstack.
2868 If no eliminations were done and this insn doesn't require any elimination
2869 processing (these are not identical conditions: it might be updating sp,
2870 but not referencing fp; this needs to be seen during reload_as_needed so
2871 that the offset between fp and sp can be taken into consideration), zero
2872 is returned. Otherwise, 1 is returned. */
2874 static int
2875 eliminate_regs_in_insn (rtx insn, int replace)
2877 int icode = recog_memoized (insn);
2878 rtx old_body = PATTERN (insn);
2879 int insn_is_asm = asm_noperands (old_body) >= 0;
2880 rtx old_set = single_set (insn);
2881 rtx new_body;
2882 int val = 0;
2883 int i;
2884 rtx substed_operand[MAX_RECOG_OPERANDS];
2885 rtx orig_operand[MAX_RECOG_OPERANDS];
2886 struct elim_table *ep;
2888 if (! insn_is_asm && icode < 0)
2890 if (GET_CODE (PATTERN (insn)) == USE
2891 || GET_CODE (PATTERN (insn)) == CLOBBER
2892 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2893 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2894 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
2895 return 0;
2896 abort ();
2899 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
2900 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2902 /* Check for setting an eliminable register. */
2903 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2904 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2906 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2907 /* If this is setting the frame pointer register to the
2908 hardware frame pointer register and this is an elimination
2909 that will be done (tested above), this insn is really
2910 adjusting the frame pointer downward to compensate for
2911 the adjustment done before a nonlocal goto. */
2912 if (ep->from == FRAME_POINTER_REGNUM
2913 && ep->to == HARD_FRAME_POINTER_REGNUM)
2915 rtx base = SET_SRC (old_set);
2916 rtx base_insn = insn;
2917 int offset = 0;
2919 while (base != ep->to_rtx)
2921 rtx prev_insn, prev_set;
2923 if (GET_CODE (base) == PLUS
2924 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2926 offset += INTVAL (XEXP (base, 1));
2927 base = XEXP (base, 0);
2929 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2930 && (prev_set = single_set (prev_insn)) != 0
2931 && rtx_equal_p (SET_DEST (prev_set), base))
2933 base = SET_SRC (prev_set);
2934 base_insn = prev_insn;
2936 else
2937 break;
2940 if (base == ep->to_rtx)
2942 rtx src
2943 = plus_constant (ep->to_rtx, offset - ep->offset);
2945 new_body = old_body;
2946 if (! replace)
2948 new_body = copy_insn (old_body);
2949 if (REG_NOTES (insn))
2950 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
2952 PATTERN (insn) = new_body;
2953 old_set = single_set (insn);
2955 /* First see if this insn remains valid when we
2956 make the change. If not, keep the INSN_CODE
2957 the same and let reload fit it up. */
2958 validate_change (insn, &SET_SRC (old_set), src, 1);
2959 validate_change (insn, &SET_DEST (old_set),
2960 ep->to_rtx, 1);
2961 if (! apply_change_group ())
2963 SET_SRC (old_set) = src;
2964 SET_DEST (old_set) = ep->to_rtx;
2967 val = 1;
2968 goto done;
2971 #endif
2973 /* In this case this insn isn't serving a useful purpose. We
2974 will delete it in reload_as_needed once we know that this
2975 elimination is, in fact, being done.
2977 If REPLACE isn't set, we can't delete this insn, but needn't
2978 process it since it won't be used unless something changes. */
2979 if (replace)
2981 delete_dead_insn (insn);
2982 return 1;
2984 val = 1;
2985 goto done;
2989 /* We allow one special case which happens to work on all machines we
2990 currently support: a single set with the source being a PLUS of an
2991 eliminable register and a constant. */
2992 if (old_set
2993 && GET_CODE (SET_DEST (old_set)) == REG
2994 && GET_CODE (SET_SRC (old_set)) == PLUS
2995 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
2996 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT
2997 && REGNO (XEXP (SET_SRC (old_set), 0)) < FIRST_PSEUDO_REGISTER)
2999 rtx reg = XEXP (SET_SRC (old_set), 0);
3000 int offset = INTVAL (XEXP (SET_SRC (old_set), 1));
3002 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3003 if (ep->from_rtx == reg && ep->can_eliminate)
3005 offset += ep->offset;
3007 if (offset == 0)
3009 int num_clobbers;
3010 /* We assume here that if we need a PARALLEL with
3011 CLOBBERs for this assignment, we can do with the
3012 MATCH_SCRATCHes that add_clobbers allocates.
3013 There's not much we can do if that doesn't work. */
3014 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3015 SET_DEST (old_set),
3016 ep->to_rtx);
3017 num_clobbers = 0;
3018 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3019 if (num_clobbers)
3021 rtvec vec = rtvec_alloc (num_clobbers + 1);
3023 vec->elem[0] = PATTERN (insn);
3024 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3025 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3027 if (INSN_CODE (insn) < 0)
3028 abort ();
3030 else
3032 new_body = old_body;
3033 if (! replace)
3035 new_body = copy_insn (old_body);
3036 if (REG_NOTES (insn))
3037 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3039 PATTERN (insn) = new_body;
3040 old_set = single_set (insn);
3042 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3043 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3045 val = 1;
3046 /* This can't have an effect on elimination offsets, so skip right
3047 to the end. */
3048 goto done;
3052 /* Determine the effects of this insn on elimination offsets. */
3053 elimination_effects (old_body, 0);
3055 /* Eliminate all eliminable registers occurring in operands that
3056 can be handled by reload. */
3057 extract_insn (insn);
3058 for (i = 0; i < recog_data.n_operands; i++)
3060 orig_operand[i] = recog_data.operand[i];
3061 substed_operand[i] = recog_data.operand[i];
3063 /* For an asm statement, every operand is eliminable. */
3064 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3066 /* Check for setting a register that we know about. */
3067 if (recog_data.operand_type[i] != OP_IN
3068 && GET_CODE (orig_operand[i]) == REG)
3070 /* If we are assigning to a register that can be eliminated, it
3071 must be as part of a PARALLEL, since the code above handles
3072 single SETs. We must indicate that we can no longer
3073 eliminate this reg. */
3074 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3075 ep++)
3076 if (ep->from_rtx == orig_operand[i] && ep->can_eliminate)
3077 ep->can_eliminate = 0;
3080 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3081 replace ? insn : NULL_RTX);
3082 if (substed_operand[i] != orig_operand[i])
3083 val = 1;
3084 /* Terminate the search in check_eliminable_occurrences at
3085 this point. */
3086 *recog_data.operand_loc[i] = 0;
3088 /* If an output operand changed from a REG to a MEM and INSN is an
3089 insn, write a CLOBBER insn. */
3090 if (recog_data.operand_type[i] != OP_IN
3091 && GET_CODE (orig_operand[i]) == REG
3092 && GET_CODE (substed_operand[i]) == MEM
3093 && replace)
3094 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3095 insn);
3099 for (i = 0; i < recog_data.n_dups; i++)
3100 *recog_data.dup_loc[i]
3101 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3103 /* If any eliminable remain, they aren't eliminable anymore. */
3104 check_eliminable_occurrences (old_body);
3106 /* Substitute the operands; the new values are in the substed_operand
3107 array. */
3108 for (i = 0; i < recog_data.n_operands; i++)
3109 *recog_data.operand_loc[i] = substed_operand[i];
3110 for (i = 0; i < recog_data.n_dups; i++)
3111 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3113 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3114 re-recognize the insn. We do this in case we had a simple addition
3115 but now can do this as a load-address. This saves an insn in this
3116 common case.
3117 If re-recognition fails, the old insn code number will still be used,
3118 and some register operands may have changed into PLUS expressions.
3119 These will be handled by find_reloads by loading them into a register
3120 again. */
3122 if (val)
3124 /* If we aren't replacing things permanently and we changed something,
3125 make another copy to ensure that all the RTL is new. Otherwise
3126 things can go wrong if find_reload swaps commutative operands
3127 and one is inside RTL that has been copied while the other is not. */
3128 new_body = old_body;
3129 if (! replace)
3131 new_body = copy_insn (old_body);
3132 if (REG_NOTES (insn))
3133 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3135 PATTERN (insn) = new_body;
3137 /* If we had a move insn but now we don't, rerecognize it. This will
3138 cause spurious re-recognition if the old move had a PARALLEL since
3139 the new one still will, but we can't call single_set without
3140 having put NEW_BODY into the insn and the re-recognition won't
3141 hurt in this rare case. */
3142 /* ??? Why this huge if statement - why don't we just rerecognize the
3143 thing always? */
3144 if (! insn_is_asm
3145 && old_set != 0
3146 && ((GET_CODE (SET_SRC (old_set)) == REG
3147 && (GET_CODE (new_body) != SET
3148 || GET_CODE (SET_SRC (new_body)) != REG))
3149 /* If this was a load from or store to memory, compare
3150 the MEM in recog_data.operand to the one in the insn.
3151 If they are not equal, then rerecognize the insn. */
3152 || (old_set != 0
3153 && ((GET_CODE (SET_SRC (old_set)) == MEM
3154 && SET_SRC (old_set) != recog_data.operand[1])
3155 || (GET_CODE (SET_DEST (old_set)) == MEM
3156 && SET_DEST (old_set) != recog_data.operand[0])))
3157 /* If this was an add insn before, rerecognize. */
3158 || GET_CODE (SET_SRC (old_set)) == PLUS))
3160 int new_icode = recog (PATTERN (insn), insn, 0);
3161 if (new_icode < 0)
3162 INSN_CODE (insn) = icode;
3166 /* Restore the old body. If there were any changes to it, we made a copy
3167 of it while the changes were still in place, so we'll correctly return
3168 a modified insn below. */
3169 if (! replace)
3171 /* Restore the old body. */
3172 for (i = 0; i < recog_data.n_operands; i++)
3173 *recog_data.operand_loc[i] = orig_operand[i];
3174 for (i = 0; i < recog_data.n_dups; i++)
3175 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3178 /* Update all elimination pairs to reflect the status after the current
3179 insn. The changes we make were determined by the earlier call to
3180 elimination_effects.
3182 We also detect cases where register elimination cannot be done,
3183 namely, if a register would be both changed and referenced outside a MEM
3184 in the resulting insn since such an insn is often undefined and, even if
3185 not, we cannot know what meaning will be given to it. Note that it is
3186 valid to have a register used in an address in an insn that changes it
3187 (presumably with a pre- or post-increment or decrement).
3189 If anything changes, return nonzero. */
3191 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3193 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3194 ep->can_eliminate = 0;
3196 ep->ref_outside_mem = 0;
3198 if (ep->previous_offset != ep->offset)
3199 val = 1;
3202 done:
3203 /* If we changed something, perform elimination in REG_NOTES. This is
3204 needed even when REPLACE is zero because a REG_DEAD note might refer
3205 to a register that we eliminate and could cause a different number
3206 of spill registers to be needed in the final reload pass than in
3207 the pre-passes. */
3208 if (val && REG_NOTES (insn) != 0)
3209 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3211 return val;
3214 /* Loop through all elimination pairs.
3215 Recalculate the number not at initial offset.
3217 Compute the maximum offset (minimum offset if the stack does not
3218 grow downward) for each elimination pair. */
3220 static void
3221 update_eliminable_offsets (void)
3223 struct elim_table *ep;
3225 num_not_at_initial_offset = 0;
3226 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3228 ep->previous_offset = ep->offset;
3229 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3230 num_not_at_initial_offset++;
3234 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3235 replacement we currently believe is valid, mark it as not eliminable if X
3236 modifies DEST in any way other than by adding a constant integer to it.
3238 If DEST is the frame pointer, we do nothing because we assume that
3239 all assignments to the hard frame pointer are nonlocal gotos and are being
3240 done at a time when they are valid and do not disturb anything else.
3241 Some machines want to eliminate a fake argument pointer with either the
3242 frame or stack pointer. Assignments to the hard frame pointer must not
3243 prevent this elimination.
3245 Called via note_stores from reload before starting its passes to scan
3246 the insns of the function. */
3248 static void
3249 mark_not_eliminable (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
3251 unsigned int i;
3253 /* A SUBREG of a hard register here is just changing its mode. We should
3254 not see a SUBREG of an eliminable hard register, but check just in
3255 case. */
3256 if (GET_CODE (dest) == SUBREG)
3257 dest = SUBREG_REG (dest);
3259 if (dest == hard_frame_pointer_rtx)
3260 return;
3262 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3263 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3264 && (GET_CODE (x) != SET
3265 || GET_CODE (SET_SRC (x)) != PLUS
3266 || XEXP (SET_SRC (x), 0) != dest
3267 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3269 reg_eliminate[i].can_eliminate_previous
3270 = reg_eliminate[i].can_eliminate = 0;
3271 num_eliminable--;
3275 /* Verify that the initial elimination offsets did not change since the
3276 last call to set_initial_elim_offsets. This is used to catch cases
3277 where something illegal happened during reload_as_needed that could
3278 cause incorrect code to be generated if we did not check for it. */
3280 static void
3281 verify_initial_elim_offsets (void)
3283 int t;
3285 #ifdef ELIMINABLE_REGS
3286 struct elim_table *ep;
3288 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3290 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3291 if (t != ep->initial_offset)
3292 abort ();
3294 #else
3295 INITIAL_FRAME_POINTER_OFFSET (t);
3296 if (t != reg_eliminate[0].initial_offset)
3297 abort ();
3298 #endif
3301 /* Reset all offsets on eliminable registers to their initial values. */
3303 static void
3304 set_initial_elim_offsets (void)
3306 struct elim_table *ep = reg_eliminate;
3308 #ifdef ELIMINABLE_REGS
3309 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3311 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3312 ep->previous_offset = ep->offset = ep->initial_offset;
3314 #else
3315 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3316 ep->previous_offset = ep->offset = ep->initial_offset;
3317 #endif
3319 num_not_at_initial_offset = 0;
3322 /* Initialize the known label offsets.
3323 Set a known offset for each forced label to be at the initial offset
3324 of each elimination. We do this because we assume that all
3325 computed jumps occur from a location where each elimination is
3326 at its initial offset.
3327 For all other labels, show that we don't know the offsets. */
3329 static void
3330 set_initial_label_offsets (void)
3332 rtx x;
3333 memset (offsets_known_at, 0, num_labels);
3335 for (x = forced_labels; x; x = XEXP (x, 1))
3336 if (XEXP (x, 0))
3337 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3340 /* Set all elimination offsets to the known values for the code label given
3341 by INSN. */
3343 static void
3344 set_offsets_for_label (rtx insn)
3346 unsigned int i;
3347 int label_nr = CODE_LABEL_NUMBER (insn);
3348 struct elim_table *ep;
3350 num_not_at_initial_offset = 0;
3351 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3353 ep->offset = ep->previous_offset
3354 = offsets_at[label_nr - first_label_num][i];
3355 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3356 num_not_at_initial_offset++;
3360 /* See if anything that happened changes which eliminations are valid.
3361 For example, on the SPARC, whether or not the frame pointer can
3362 be eliminated can depend on what registers have been used. We need
3363 not check some conditions again (such as flag_omit_frame_pointer)
3364 since they can't have changed. */
3366 static void
3367 update_eliminables (HARD_REG_SET *pset)
3369 int previous_frame_pointer_needed = frame_pointer_needed;
3370 struct elim_table *ep;
3372 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3373 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3374 #ifdef ELIMINABLE_REGS
3375 || ! CAN_ELIMINATE (ep->from, ep->to)
3376 #endif
3378 ep->can_eliminate = 0;
3380 /* Look for the case where we have discovered that we can't replace
3381 register A with register B and that means that we will now be
3382 trying to replace register A with register C. This means we can
3383 no longer replace register C with register B and we need to disable
3384 such an elimination, if it exists. This occurs often with A == ap,
3385 B == sp, and C == fp. */
3387 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3389 struct elim_table *op;
3390 int new_to = -1;
3392 if (! ep->can_eliminate && ep->can_eliminate_previous)
3394 /* Find the current elimination for ep->from, if there is a
3395 new one. */
3396 for (op = reg_eliminate;
3397 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3398 if (op->from == ep->from && op->can_eliminate)
3400 new_to = op->to;
3401 break;
3404 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3405 disable it. */
3406 for (op = reg_eliminate;
3407 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3408 if (op->from == new_to && op->to == ep->to)
3409 op->can_eliminate = 0;
3413 /* See if any registers that we thought we could eliminate the previous
3414 time are no longer eliminable. If so, something has changed and we
3415 must spill the register. Also, recompute the number of eliminable
3416 registers and see if the frame pointer is needed; it is if there is
3417 no elimination of the frame pointer that we can perform. */
3419 frame_pointer_needed = 1;
3420 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3422 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3423 && ep->to != HARD_FRAME_POINTER_REGNUM)
3424 frame_pointer_needed = 0;
3426 if (! ep->can_eliminate && ep->can_eliminate_previous)
3428 ep->can_eliminate_previous = 0;
3429 SET_HARD_REG_BIT (*pset, ep->from);
3430 num_eliminable--;
3434 /* If we didn't need a frame pointer last time, but we do now, spill
3435 the hard frame pointer. */
3436 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3437 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3440 /* Initialize the table of registers to eliminate. */
3442 static void
3443 init_elim_table (void)
3445 struct elim_table *ep;
3446 #ifdef ELIMINABLE_REGS
3447 const struct elim_table_1 *ep1;
3448 #endif
3450 if (!reg_eliminate)
3451 reg_eliminate = (struct elim_table *)
3452 xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3454 /* Does this function require a frame pointer? */
3456 frame_pointer_needed = (! flag_omit_frame_pointer
3457 #ifdef EXIT_IGNORE_STACK
3458 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3459 and restore sp for alloca. So we can't eliminate
3460 the frame pointer in that case. At some point,
3461 we should improve this by emitting the
3462 sp-adjusting insns for this case. */
3463 || (current_function_calls_alloca
3464 && EXIT_IGNORE_STACK)
3465 #endif
3466 || FRAME_POINTER_REQUIRED);
3468 num_eliminable = 0;
3470 #ifdef ELIMINABLE_REGS
3471 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3472 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3474 ep->from = ep1->from;
3475 ep->to = ep1->to;
3476 ep->can_eliminate = ep->can_eliminate_previous
3477 = (CAN_ELIMINATE (ep->from, ep->to)
3478 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3480 #else
3481 reg_eliminate[0].from = reg_eliminate_1[0].from;
3482 reg_eliminate[0].to = reg_eliminate_1[0].to;
3483 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3484 = ! frame_pointer_needed;
3485 #endif
3487 /* Count the number of eliminable registers and build the FROM and TO
3488 REG rtx's. Note that code in gen_rtx will cause, e.g.,
3489 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3490 We depend on this. */
3491 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3493 num_eliminable += ep->can_eliminate;
3494 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3495 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3499 /* Kick all pseudos out of hard register REGNO.
3501 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3502 because we found we can't eliminate some register. In the case, no pseudos
3503 are allowed to be in the register, even if they are only in a block that
3504 doesn't require spill registers, unlike the case when we are spilling this
3505 hard reg to produce another spill register.
3507 Return nonzero if any pseudos needed to be kicked out. */
3509 static void
3510 spill_hard_reg (unsigned int regno, int cant_eliminate)
3512 int i;
3514 if (cant_eliminate)
3516 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3517 regs_ever_live[regno] = 1;
3520 /* Spill every pseudo reg that was allocated to this reg
3521 or to something that overlaps this reg. */
3523 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3524 if (reg_renumber[i] >= 0
3525 && (unsigned int) reg_renumber[i] <= regno
3526 && ((unsigned int) reg_renumber[i]
3527 + HARD_REGNO_NREGS ((unsigned int) reg_renumber[i],
3528 PSEUDO_REGNO_MODE (i))
3529 > regno))
3530 SET_REGNO_REG_SET (&spilled_pseudos, i);
3533 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3534 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3536 static void
3537 ior_hard_reg_set (HARD_REG_SET *set1, HARD_REG_SET *set2)
3539 IOR_HARD_REG_SET (*set1, *set2);
3542 /* After find_reload_regs has been run for all insn that need reloads,
3543 and/or spill_hard_regs was called, this function is used to actually
3544 spill pseudo registers and try to reallocate them. It also sets up the
3545 spill_regs array for use by choose_reload_regs. */
3547 static int
3548 finish_spills (int global)
3550 struct insn_chain *chain;
3551 int something_changed = 0;
3552 int i;
3554 /* Build the spill_regs array for the function. */
3555 /* If there are some registers still to eliminate and one of the spill regs
3556 wasn't ever used before, additional stack space may have to be
3557 allocated to store this register. Thus, we may have changed the offset
3558 between the stack and frame pointers, so mark that something has changed.
3560 One might think that we need only set VAL to 1 if this is a call-used
3561 register. However, the set of registers that must be saved by the
3562 prologue is not identical to the call-used set. For example, the
3563 register used by the call insn for the return PC is a call-used register,
3564 but must be saved by the prologue. */
3566 n_spills = 0;
3567 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3568 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3570 spill_reg_order[i] = n_spills;
3571 spill_regs[n_spills++] = i;
3572 if (num_eliminable && ! regs_ever_live[i])
3573 something_changed = 1;
3574 regs_ever_live[i] = 1;
3576 else
3577 spill_reg_order[i] = -1;
3579 EXECUTE_IF_SET_IN_REG_SET
3580 (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i,
3582 /* Record the current hard register the pseudo is allocated to in
3583 pseudo_previous_regs so we avoid reallocating it to the same
3584 hard reg in a later pass. */
3585 if (reg_renumber[i] < 0)
3586 abort ();
3588 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3589 /* Mark it as no longer having a hard register home. */
3590 reg_renumber[i] = -1;
3591 /* We will need to scan everything again. */
3592 something_changed = 1;
3595 /* Retry global register allocation if possible. */
3596 if (global)
3598 memset ((char *) pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3599 /* For every insn that needs reloads, set the registers used as spill
3600 regs in pseudo_forbidden_regs for every pseudo live across the
3601 insn. */
3602 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3604 EXECUTE_IF_SET_IN_REG_SET
3605 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
3607 ior_hard_reg_set (pseudo_forbidden_regs + i,
3608 &chain->used_spill_regs);
3610 EXECUTE_IF_SET_IN_REG_SET
3611 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
3613 ior_hard_reg_set (pseudo_forbidden_regs + i,
3614 &chain->used_spill_regs);
3618 /* Retry allocating the spilled pseudos. For each reg, merge the
3619 various reg sets that indicate which hard regs can't be used,
3620 and call retry_global_alloc.
3621 We change spill_pseudos here to only contain pseudos that did not
3622 get a new hard register. */
3623 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3624 if (reg_old_renumber[i] != reg_renumber[i])
3626 HARD_REG_SET forbidden;
3627 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3628 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3629 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3630 retry_global_alloc (i, forbidden);
3631 if (reg_renumber[i] >= 0)
3632 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3636 /* Fix up the register information in the insn chain.
3637 This involves deleting those of the spilled pseudos which did not get
3638 a new hard register home from the live_{before,after} sets. */
3639 for (chain = reload_insn_chain; chain; chain = chain->next)
3641 HARD_REG_SET used_by_pseudos;
3642 HARD_REG_SET used_by_pseudos2;
3644 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3645 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3647 /* Mark any unallocated hard regs as available for spills. That
3648 makes inheritance work somewhat better. */
3649 if (chain->need_reload)
3651 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3652 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3653 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3655 /* Save the old value for the sanity test below. */
3656 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3658 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3659 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3660 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3661 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3663 /* Make sure we only enlarge the set. */
3664 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3665 abort ();
3666 ok:;
3670 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3671 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3673 int regno = reg_renumber[i];
3674 if (reg_old_renumber[i] == regno)
3675 continue;
3677 alter_reg (i, reg_old_renumber[i]);
3678 reg_old_renumber[i] = regno;
3679 if (rtl_dump_file)
3681 if (regno == -1)
3682 fprintf (rtl_dump_file, " Register %d now on stack.\n\n", i);
3683 else
3684 fprintf (rtl_dump_file, " Register %d now in %d.\n\n",
3685 i, reg_renumber[i]);
3689 return something_changed;
3692 /* Find all paradoxical subregs within X and update reg_max_ref_width.
3693 Also mark any hard registers used to store user variables as
3694 forbidden from being used for spill registers. */
3696 static void
3697 scan_paradoxical_subregs (rtx x)
3699 int i;
3700 const char *fmt;
3701 enum rtx_code code = GET_CODE (x);
3703 switch (code)
3705 case REG:
3706 #if 0
3707 if (SMALL_REGISTER_CLASSES && REGNO (x) < FIRST_PSEUDO_REGISTER
3708 && REG_USERVAR_P (x))
3709 SET_HARD_REG_BIT (bad_spill_regs_global, REGNO (x));
3710 #endif
3711 return;
3713 case CONST_INT:
3714 case CONST:
3715 case SYMBOL_REF:
3716 case LABEL_REF:
3717 case CONST_DOUBLE:
3718 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3719 case CC0:
3720 case PC:
3721 case USE:
3722 case CLOBBER:
3723 return;
3725 case SUBREG:
3726 if (GET_CODE (SUBREG_REG (x)) == REG
3727 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3728 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3729 = GET_MODE_SIZE (GET_MODE (x));
3730 return;
3732 default:
3733 break;
3736 fmt = GET_RTX_FORMAT (code);
3737 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3739 if (fmt[i] == 'e')
3740 scan_paradoxical_subregs (XEXP (x, i));
3741 else if (fmt[i] == 'E')
3743 int j;
3744 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3745 scan_paradoxical_subregs (XVECEXP (x, i, j));
3750 /* Reload pseudo-registers into hard regs around each insn as needed.
3751 Additional register load insns are output before the insn that needs it
3752 and perhaps store insns after insns that modify the reloaded pseudo reg.
3754 reg_last_reload_reg and reg_reloaded_contents keep track of
3755 which registers are already available in reload registers.
3756 We update these for the reloads that we perform,
3757 as the insns are scanned. */
3759 static void
3760 reload_as_needed (int live_known)
3762 struct insn_chain *chain;
3763 #if defined (AUTO_INC_DEC)
3764 int i;
3765 #endif
3766 rtx x;
3768 memset ((char *) spill_reg_rtx, 0, sizeof spill_reg_rtx);
3769 memset ((char *) spill_reg_store, 0, sizeof spill_reg_store);
3770 reg_last_reload_reg = (rtx *) xcalloc (max_regno, sizeof (rtx));
3771 reg_has_output_reload = (char *) xmalloc (max_regno);
3772 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3774 set_initial_elim_offsets ();
3776 for (chain = reload_insn_chain; chain; chain = chain->next)
3778 rtx prev = 0;
3779 rtx insn = chain->insn;
3780 rtx old_next = NEXT_INSN (insn);
3782 /* If we pass a label, copy the offsets from the label information
3783 into the current offsets of each elimination. */
3784 if (GET_CODE (insn) == CODE_LABEL)
3785 set_offsets_for_label (insn);
3787 else if (INSN_P (insn))
3789 rtx oldpat = copy_rtx (PATTERN (insn));
3791 /* If this is a USE and CLOBBER of a MEM, ensure that any
3792 references to eliminable registers have been removed. */
3794 if ((GET_CODE (PATTERN (insn)) == USE
3795 || GET_CODE (PATTERN (insn)) == CLOBBER)
3796 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3797 XEXP (XEXP (PATTERN (insn), 0), 0)
3798 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3799 GET_MODE (XEXP (PATTERN (insn), 0)),
3800 NULL_RTX);
3802 /* If we need to do register elimination processing, do so.
3803 This might delete the insn, in which case we are done. */
3804 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3806 eliminate_regs_in_insn (insn, 1);
3807 if (GET_CODE (insn) == NOTE)
3809 update_eliminable_offsets ();
3810 continue;
3814 /* If need_elim is nonzero but need_reload is zero, one might think
3815 that we could simply set n_reloads to 0. However, find_reloads
3816 could have done some manipulation of the insn (such as swapping
3817 commutative operands), and these manipulations are lost during
3818 the first pass for every insn that needs register elimination.
3819 So the actions of find_reloads must be redone here. */
3821 if (! chain->need_elim && ! chain->need_reload
3822 && ! chain->need_operand_change)
3823 n_reloads = 0;
3824 /* First find the pseudo regs that must be reloaded for this insn.
3825 This info is returned in the tables reload_... (see reload.h).
3826 Also modify the body of INSN by substituting RELOAD
3827 rtx's for those pseudo regs. */
3828 else
3830 memset (reg_has_output_reload, 0, max_regno);
3831 CLEAR_HARD_REG_SET (reg_is_output_reload);
3833 find_reloads (insn, 1, spill_indirect_levels, live_known,
3834 spill_reg_order);
3837 if (n_reloads > 0)
3839 rtx next = NEXT_INSN (insn);
3840 rtx p;
3842 prev = PREV_INSN (insn);
3844 /* Now compute which reload regs to reload them into. Perhaps
3845 reusing reload regs from previous insns, or else output
3846 load insns to reload them. Maybe output store insns too.
3847 Record the choices of reload reg in reload_reg_rtx. */
3848 choose_reload_regs (chain);
3850 /* Merge any reloads that we didn't combine for fear of
3851 increasing the number of spill registers needed but now
3852 discover can be safely merged. */
3853 if (SMALL_REGISTER_CLASSES)
3854 merge_assigned_reloads (insn);
3856 /* Generate the insns to reload operands into or out of
3857 their reload regs. */
3858 emit_reload_insns (chain);
3860 /* Substitute the chosen reload regs from reload_reg_rtx
3861 into the insn's body (or perhaps into the bodies of other
3862 load and store insn that we just made for reloading
3863 and that we moved the structure into). */
3864 subst_reloads (insn);
3866 /* If this was an ASM, make sure that all the reload insns
3867 we have generated are valid. If not, give an error
3868 and delete them. */
3870 if (asm_noperands (PATTERN (insn)) >= 0)
3871 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3872 if (p != insn && INSN_P (p)
3873 && GET_CODE (PATTERN (p)) != USE
3874 && (recog_memoized (p) < 0
3875 || (extract_insn (p), ! constrain_operands (1))))
3877 error_for_asm (insn,
3878 "`asm' operand requires impossible reload");
3879 delete_insn (p);
3883 if (num_eliminable && chain->need_elim)
3884 update_eliminable_offsets ();
3886 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3887 is no longer validly lying around to save a future reload.
3888 Note that this does not detect pseudos that were reloaded
3889 for this insn in order to be stored in
3890 (obeying register constraints). That is correct; such reload
3891 registers ARE still valid. */
3892 note_stores (oldpat, forget_old_reloads_1, NULL);
3894 /* There may have been CLOBBER insns placed after INSN. So scan
3895 between INSN and NEXT and use them to forget old reloads. */
3896 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
3897 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3898 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
3900 #ifdef AUTO_INC_DEC
3901 /* Likewise for regs altered by auto-increment in this insn.
3902 REG_INC notes have been changed by reloading:
3903 find_reloads_address_1 records substitutions for them,
3904 which have been performed by subst_reloads above. */
3905 for (i = n_reloads - 1; i >= 0; i--)
3907 rtx in_reg = rld[i].in_reg;
3908 if (in_reg)
3910 enum rtx_code code = GET_CODE (in_reg);
3911 /* PRE_INC / PRE_DEC will have the reload register ending up
3912 with the same value as the stack slot, but that doesn't
3913 hold true for POST_INC / POST_DEC. Either we have to
3914 convert the memory access to a true POST_INC / POST_DEC,
3915 or we can't use the reload register for inheritance. */
3916 if ((code == POST_INC || code == POST_DEC)
3917 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3918 REGNO (rld[i].reg_rtx))
3919 /* Make sure it is the inc/dec pseudo, and not
3920 some other (e.g. output operand) pseudo. */
3921 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3922 == REGNO (XEXP (in_reg, 0))))
3925 rtx reload_reg = rld[i].reg_rtx;
3926 enum machine_mode mode = GET_MODE (reload_reg);
3927 int n = 0;
3928 rtx p;
3930 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
3932 /* We really want to ignore REG_INC notes here, so
3933 use PATTERN (p) as argument to reg_set_p . */
3934 if (reg_set_p (reload_reg, PATTERN (p)))
3935 break;
3936 n = count_occurrences (PATTERN (p), reload_reg, 0);
3937 if (! n)
3938 continue;
3939 if (n == 1)
3941 n = validate_replace_rtx (reload_reg,
3942 gen_rtx (code, mode,
3943 reload_reg),
3946 /* We must also verify that the constraints
3947 are met after the replacement. */
3948 extract_insn (p);
3949 if (n)
3950 n = constrain_operands (1);
3951 else
3952 break;
3954 /* If the constraints were not met, then
3955 undo the replacement. */
3956 if (!n)
3958 validate_replace_rtx (gen_rtx (code, mode,
3959 reload_reg),
3960 reload_reg, p);
3961 break;
3965 break;
3967 if (n == 1)
3969 REG_NOTES (p)
3970 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
3971 REG_NOTES (p));
3972 /* Mark this as having an output reload so that the
3973 REG_INC processing code below won't invalidate
3974 the reload for inheritance. */
3975 SET_HARD_REG_BIT (reg_is_output_reload,
3976 REGNO (reload_reg));
3977 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
3979 else
3980 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
3981 NULL);
3983 else if ((code == PRE_INC || code == PRE_DEC)
3984 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3985 REGNO (rld[i].reg_rtx))
3986 /* Make sure it is the inc/dec pseudo, and not
3987 some other (e.g. output operand) pseudo. */
3988 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3989 == REGNO (XEXP (in_reg, 0))))
3991 SET_HARD_REG_BIT (reg_is_output_reload,
3992 REGNO (rld[i].reg_rtx));
3993 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
3997 /* If a pseudo that got a hard register is auto-incremented,
3998 we must purge records of copying it into pseudos without
3999 hard registers. */
4000 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4001 if (REG_NOTE_KIND (x) == REG_INC)
4003 /* See if this pseudo reg was reloaded in this insn.
4004 If so, its last-reload info is still valid
4005 because it is based on this insn's reload. */
4006 for (i = 0; i < n_reloads; i++)
4007 if (rld[i].out == XEXP (x, 0))
4008 break;
4010 if (i == n_reloads)
4011 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4013 #endif
4015 /* A reload reg's contents are unknown after a label. */
4016 if (GET_CODE (insn) == CODE_LABEL)
4017 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4019 /* Don't assume a reload reg is still good after a call insn
4020 if it is a call-used reg. */
4021 else if (GET_CODE (insn) == CALL_INSN)
4022 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4025 /* Clean up. */
4026 free (reg_last_reload_reg);
4027 free (reg_has_output_reload);
4030 /* Discard all record of any value reloaded from X,
4031 or reloaded in X from someplace else;
4032 unless X is an output reload reg of the current insn.
4034 X may be a hard reg (the reload reg)
4035 or it may be a pseudo reg that was reloaded from. */
4037 static void
4038 forget_old_reloads_1 (rtx x, rtx ignored ATTRIBUTE_UNUSED,
4039 void *data ATTRIBUTE_UNUSED)
4041 unsigned int regno;
4042 unsigned int nr;
4044 /* note_stores does give us subregs of hard regs,
4045 subreg_regno_offset will abort if it is not a hard reg. */
4046 while (GET_CODE (x) == SUBREG)
4048 /* We ignore the subreg offset when calculating the regno,
4049 because we are using the entire underlying hard register
4050 below. */
4051 x = SUBREG_REG (x);
4054 if (GET_CODE (x) != REG)
4055 return;
4057 regno = REGNO (x);
4059 if (regno >= FIRST_PSEUDO_REGISTER)
4060 nr = 1;
4061 else
4063 unsigned int i;
4065 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
4066 /* Storing into a spilled-reg invalidates its contents.
4067 This can happen if a block-local pseudo is allocated to that reg
4068 and it wasn't spilled because this block's total need is 0.
4069 Then some insn might have an optional reload and use this reg. */
4070 for (i = 0; i < nr; i++)
4071 /* But don't do this if the reg actually serves as an output
4072 reload reg in the current instruction. */
4073 if (n_reloads == 0
4074 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4076 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4077 spill_reg_store[regno + i] = 0;
4081 /* Since value of X has changed,
4082 forget any value previously copied from it. */
4084 while (nr-- > 0)
4085 /* But don't forget a copy if this is the output reload
4086 that establishes the copy's validity. */
4087 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4088 reg_last_reload_reg[regno + nr] = 0;
4091 /* The following HARD_REG_SETs indicate when each hard register is
4092 used for a reload of various parts of the current insn. */
4094 /* If reg is unavailable for all reloads. */
4095 static HARD_REG_SET reload_reg_unavailable;
4096 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4097 static HARD_REG_SET reload_reg_used;
4098 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4099 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4100 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4101 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4102 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4103 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4104 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4105 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4106 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4107 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4108 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4109 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4110 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4111 static HARD_REG_SET reload_reg_used_in_op_addr;
4112 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4113 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4114 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4115 static HARD_REG_SET reload_reg_used_in_insn;
4116 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4117 static HARD_REG_SET reload_reg_used_in_other_addr;
4119 /* If reg is in use as a reload reg for any sort of reload. */
4120 static HARD_REG_SET reload_reg_used_at_all;
4122 /* If reg is use as an inherited reload. We just mark the first register
4123 in the group. */
4124 static HARD_REG_SET reload_reg_used_for_inherit;
4126 /* Records which hard regs are used in any way, either as explicit use or
4127 by being allocated to a pseudo during any point of the current insn. */
4128 static HARD_REG_SET reg_used_in_insn;
4130 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4131 TYPE. MODE is used to indicate how many consecutive regs are
4132 actually used. */
4134 static void
4135 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4136 enum machine_mode mode)
4138 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4139 unsigned int i;
4141 for (i = regno; i < nregs + regno; i++)
4143 switch (type)
4145 case RELOAD_OTHER:
4146 SET_HARD_REG_BIT (reload_reg_used, i);
4147 break;
4149 case RELOAD_FOR_INPUT_ADDRESS:
4150 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4151 break;
4153 case RELOAD_FOR_INPADDR_ADDRESS:
4154 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4155 break;
4157 case RELOAD_FOR_OUTPUT_ADDRESS:
4158 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4159 break;
4161 case RELOAD_FOR_OUTADDR_ADDRESS:
4162 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4163 break;
4165 case RELOAD_FOR_OPERAND_ADDRESS:
4166 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4167 break;
4169 case RELOAD_FOR_OPADDR_ADDR:
4170 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4171 break;
4173 case RELOAD_FOR_OTHER_ADDRESS:
4174 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4175 break;
4177 case RELOAD_FOR_INPUT:
4178 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4179 break;
4181 case RELOAD_FOR_OUTPUT:
4182 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4183 break;
4185 case RELOAD_FOR_INSN:
4186 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4187 break;
4190 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4194 /* Similarly, but show REGNO is no longer in use for a reload. */
4196 static void
4197 clear_reload_reg_in_use (unsigned int regno, int opnum,
4198 enum reload_type type, enum machine_mode mode)
4200 unsigned int nregs = HARD_REGNO_NREGS (regno, mode);
4201 unsigned int start_regno, end_regno, r;
4202 int i;
4203 /* A complication is that for some reload types, inheritance might
4204 allow multiple reloads of the same types to share a reload register.
4205 We set check_opnum if we have to check only reloads with the same
4206 operand number, and check_any if we have to check all reloads. */
4207 int check_opnum = 0;
4208 int check_any = 0;
4209 HARD_REG_SET *used_in_set;
4211 switch (type)
4213 case RELOAD_OTHER:
4214 used_in_set = &reload_reg_used;
4215 break;
4217 case RELOAD_FOR_INPUT_ADDRESS:
4218 used_in_set = &reload_reg_used_in_input_addr[opnum];
4219 break;
4221 case RELOAD_FOR_INPADDR_ADDRESS:
4222 check_opnum = 1;
4223 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4224 break;
4226 case RELOAD_FOR_OUTPUT_ADDRESS:
4227 used_in_set = &reload_reg_used_in_output_addr[opnum];
4228 break;
4230 case RELOAD_FOR_OUTADDR_ADDRESS:
4231 check_opnum = 1;
4232 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4233 break;
4235 case RELOAD_FOR_OPERAND_ADDRESS:
4236 used_in_set = &reload_reg_used_in_op_addr;
4237 break;
4239 case RELOAD_FOR_OPADDR_ADDR:
4240 check_any = 1;
4241 used_in_set = &reload_reg_used_in_op_addr_reload;
4242 break;
4244 case RELOAD_FOR_OTHER_ADDRESS:
4245 used_in_set = &reload_reg_used_in_other_addr;
4246 check_any = 1;
4247 break;
4249 case RELOAD_FOR_INPUT:
4250 used_in_set = &reload_reg_used_in_input[opnum];
4251 break;
4253 case RELOAD_FOR_OUTPUT:
4254 used_in_set = &reload_reg_used_in_output[opnum];
4255 break;
4257 case RELOAD_FOR_INSN:
4258 used_in_set = &reload_reg_used_in_insn;
4259 break;
4260 default:
4261 abort ();
4263 /* We resolve conflicts with remaining reloads of the same type by
4264 excluding the intervals of reload registers by them from the
4265 interval of freed reload registers. Since we only keep track of
4266 one set of interval bounds, we might have to exclude somewhat
4267 more than what would be necessary if we used a HARD_REG_SET here.
4268 But this should only happen very infrequently, so there should
4269 be no reason to worry about it. */
4271 start_regno = regno;
4272 end_regno = regno + nregs;
4273 if (check_opnum || check_any)
4275 for (i = n_reloads - 1; i >= 0; i--)
4277 if (rld[i].when_needed == type
4278 && (check_any || rld[i].opnum == opnum)
4279 && rld[i].reg_rtx)
4281 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4282 unsigned int conflict_end
4283 = (conflict_start
4284 + HARD_REGNO_NREGS (conflict_start, rld[i].mode));
4286 /* If there is an overlap with the first to-be-freed register,
4287 adjust the interval start. */
4288 if (conflict_start <= start_regno && conflict_end > start_regno)
4289 start_regno = conflict_end;
4290 /* Otherwise, if there is a conflict with one of the other
4291 to-be-freed registers, adjust the interval end. */
4292 if (conflict_start > start_regno && conflict_start < end_regno)
4293 end_regno = conflict_start;
4298 for (r = start_regno; r < end_regno; r++)
4299 CLEAR_HARD_REG_BIT (*used_in_set, r);
4302 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4303 specified by OPNUM and TYPE. */
4305 static int
4306 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4308 int i;
4310 /* In use for a RELOAD_OTHER means it's not available for anything. */
4311 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4312 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4313 return 0;
4315 switch (type)
4317 case RELOAD_OTHER:
4318 /* In use for anything means we can't use it for RELOAD_OTHER. */
4319 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4320 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4321 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4322 return 0;
4324 for (i = 0; i < reload_n_operands; i++)
4325 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4326 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4327 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4328 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4329 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4330 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4331 return 0;
4333 return 1;
4335 case RELOAD_FOR_INPUT:
4336 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4337 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4338 return 0;
4340 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4341 return 0;
4343 /* If it is used for some other input, can't use it. */
4344 for (i = 0; i < reload_n_operands; i++)
4345 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4346 return 0;
4348 /* If it is used in a later operand's address, can't use it. */
4349 for (i = opnum + 1; i < reload_n_operands; i++)
4350 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4351 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4352 return 0;
4354 return 1;
4356 case RELOAD_FOR_INPUT_ADDRESS:
4357 /* Can't use a register if it is used for an input address for this
4358 operand or used as an input in an earlier one. */
4359 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4360 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4361 return 0;
4363 for (i = 0; i < opnum; i++)
4364 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4365 return 0;
4367 return 1;
4369 case RELOAD_FOR_INPADDR_ADDRESS:
4370 /* Can't use a register if it is used for an input address
4371 for this operand or used as an input in an earlier
4372 one. */
4373 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4374 return 0;
4376 for (i = 0; i < opnum; i++)
4377 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4378 return 0;
4380 return 1;
4382 case RELOAD_FOR_OUTPUT_ADDRESS:
4383 /* Can't use a register if it is used for an output address for this
4384 operand or used as an output in this or a later operand. Note
4385 that multiple output operands are emitted in reverse order, so
4386 the conflicting ones are those with lower indices. */
4387 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4388 return 0;
4390 for (i = 0; i <= opnum; i++)
4391 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4392 return 0;
4394 return 1;
4396 case RELOAD_FOR_OUTADDR_ADDRESS:
4397 /* Can't use a register if it is used for an output address
4398 for this operand or used as an output in this or a
4399 later operand. Note that multiple output operands are
4400 emitted in reverse order, so the conflicting ones are
4401 those with lower indices. */
4402 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4403 return 0;
4405 for (i = 0; i <= opnum; i++)
4406 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4407 return 0;
4409 return 1;
4411 case RELOAD_FOR_OPERAND_ADDRESS:
4412 for (i = 0; i < reload_n_operands; i++)
4413 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4414 return 0;
4416 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4417 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4419 case RELOAD_FOR_OPADDR_ADDR:
4420 for (i = 0; i < reload_n_operands; i++)
4421 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4422 return 0;
4424 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4426 case RELOAD_FOR_OUTPUT:
4427 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4428 outputs, or an operand address for this or an earlier output.
4429 Note that multiple output operands are emitted in reverse order,
4430 so the conflicting ones are those with higher indices. */
4431 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4432 return 0;
4434 for (i = 0; i < reload_n_operands; i++)
4435 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4436 return 0;
4438 for (i = opnum; i < reload_n_operands; i++)
4439 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4440 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4441 return 0;
4443 return 1;
4445 case RELOAD_FOR_INSN:
4446 for (i = 0; i < reload_n_operands; i++)
4447 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4448 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4449 return 0;
4451 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4452 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4454 case RELOAD_FOR_OTHER_ADDRESS:
4455 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4457 abort ();
4460 /* Return 1 if the value in reload reg REGNO, as used by a reload
4461 needed for the part of the insn specified by OPNUM and TYPE,
4462 is still available in REGNO at the end of the insn.
4464 We can assume that the reload reg was already tested for availability
4465 at the time it is needed, and we should not check this again,
4466 in case the reg has already been marked in use. */
4468 static int
4469 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4471 int i;
4473 switch (type)
4475 case RELOAD_OTHER:
4476 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4477 its value must reach the end. */
4478 return 1;
4480 /* If this use is for part of the insn,
4481 its value reaches if no subsequent part uses the same register.
4482 Just like the above function, don't try to do this with lots
4483 of fallthroughs. */
4485 case RELOAD_FOR_OTHER_ADDRESS:
4486 /* Here we check for everything else, since these don't conflict
4487 with anything else and everything comes later. */
4489 for (i = 0; i < reload_n_operands; i++)
4490 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4491 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4492 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4493 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4494 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4495 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4496 return 0;
4498 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4499 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4500 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4502 case RELOAD_FOR_INPUT_ADDRESS:
4503 case RELOAD_FOR_INPADDR_ADDRESS:
4504 /* Similar, except that we check only for this and subsequent inputs
4505 and the address of only subsequent inputs and we do not need
4506 to check for RELOAD_OTHER objects since they are known not to
4507 conflict. */
4509 for (i = opnum; i < reload_n_operands; i++)
4510 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4511 return 0;
4513 for (i = opnum + 1; i < reload_n_operands; i++)
4514 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4515 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4516 return 0;
4518 for (i = 0; i < reload_n_operands; i++)
4519 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4520 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4521 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4522 return 0;
4524 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4525 return 0;
4527 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4528 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4529 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4531 case RELOAD_FOR_INPUT:
4532 /* Similar to input address, except we start at the next operand for
4533 both input and input address and we do not check for
4534 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4535 would conflict. */
4537 for (i = opnum + 1; i < reload_n_operands; i++)
4538 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4539 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4540 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4541 return 0;
4543 /* ... fall through ... */
4545 case RELOAD_FOR_OPERAND_ADDRESS:
4546 /* Check outputs and their addresses. */
4548 for (i = 0; i < reload_n_operands; i++)
4549 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4550 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4551 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4552 return 0;
4554 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4556 case RELOAD_FOR_OPADDR_ADDR:
4557 for (i = 0; i < reload_n_operands; i++)
4558 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4559 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4560 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4561 return 0;
4563 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4564 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4565 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4567 case RELOAD_FOR_INSN:
4568 /* These conflict with other outputs with RELOAD_OTHER. So
4569 we need only check for output addresses. */
4571 opnum = reload_n_operands;
4573 /* ... fall through ... */
4575 case RELOAD_FOR_OUTPUT:
4576 case RELOAD_FOR_OUTPUT_ADDRESS:
4577 case RELOAD_FOR_OUTADDR_ADDRESS:
4578 /* We already know these can't conflict with a later output. So the
4579 only thing to check are later output addresses.
4580 Note that multiple output operands are emitted in reverse order,
4581 so the conflicting ones are those with lower indices. */
4582 for (i = 0; i < opnum; i++)
4583 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4584 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4585 return 0;
4587 return 1;
4590 abort ();
4593 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4594 Return 0 otherwise.
4596 This function uses the same algorithm as reload_reg_free_p above. */
4599 reloads_conflict (int r1, int r2)
4601 enum reload_type r1_type = rld[r1].when_needed;
4602 enum reload_type r2_type = rld[r2].when_needed;
4603 int r1_opnum = rld[r1].opnum;
4604 int r2_opnum = rld[r2].opnum;
4606 /* RELOAD_OTHER conflicts with everything. */
4607 if (r2_type == RELOAD_OTHER)
4608 return 1;
4610 /* Otherwise, check conflicts differently for each type. */
4612 switch (r1_type)
4614 case RELOAD_FOR_INPUT:
4615 return (r2_type == RELOAD_FOR_INSN
4616 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4617 || r2_type == RELOAD_FOR_OPADDR_ADDR
4618 || r2_type == RELOAD_FOR_INPUT
4619 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4620 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4621 && r2_opnum > r1_opnum));
4623 case RELOAD_FOR_INPUT_ADDRESS:
4624 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4625 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4627 case RELOAD_FOR_INPADDR_ADDRESS:
4628 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4629 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4631 case RELOAD_FOR_OUTPUT_ADDRESS:
4632 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4633 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4635 case RELOAD_FOR_OUTADDR_ADDRESS:
4636 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4637 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4639 case RELOAD_FOR_OPERAND_ADDRESS:
4640 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4641 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4643 case RELOAD_FOR_OPADDR_ADDR:
4644 return (r2_type == RELOAD_FOR_INPUT
4645 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4647 case RELOAD_FOR_OUTPUT:
4648 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4649 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4650 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4651 && r2_opnum >= r1_opnum));
4653 case RELOAD_FOR_INSN:
4654 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4655 || r2_type == RELOAD_FOR_INSN
4656 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4658 case RELOAD_FOR_OTHER_ADDRESS:
4659 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4661 case RELOAD_OTHER:
4662 return 1;
4664 default:
4665 abort ();
4669 /* Indexed by reload number, 1 if incoming value
4670 inherited from previous insns. */
4671 char reload_inherited[MAX_RELOADS];
4673 /* For an inherited reload, this is the insn the reload was inherited from,
4674 if we know it. Otherwise, this is 0. */
4675 rtx reload_inheritance_insn[MAX_RELOADS];
4677 /* If nonzero, this is a place to get the value of the reload,
4678 rather than using reload_in. */
4679 rtx reload_override_in[MAX_RELOADS];
4681 /* For each reload, the hard register number of the register used,
4682 or -1 if we did not need a register for this reload. */
4683 int reload_spill_index[MAX_RELOADS];
4685 /* Subroutine of free_for_value_p, used to check a single register.
4686 START_REGNO is the starting regno of the full reload register
4687 (possibly comprising multiple hard registers) that we are considering. */
4689 static int
4690 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
4691 enum reload_type type, rtx value, rtx out,
4692 int reloadnum, int ignore_address_reloads)
4694 int time1;
4695 /* Set if we see an input reload that must not share its reload register
4696 with any new earlyclobber, but might otherwise share the reload
4697 register with an output or input-output reload. */
4698 int check_earlyclobber = 0;
4699 int i;
4700 int copy = 0;
4702 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4703 return 0;
4705 if (out == const0_rtx)
4707 copy = 1;
4708 out = NULL_RTX;
4711 /* We use some pseudo 'time' value to check if the lifetimes of the
4712 new register use would overlap with the one of a previous reload
4713 that is not read-only or uses a different value.
4714 The 'time' used doesn't have to be linear in any shape or form, just
4715 monotonic.
4716 Some reload types use different 'buckets' for each operand.
4717 So there are MAX_RECOG_OPERANDS different time values for each
4718 such reload type.
4719 We compute TIME1 as the time when the register for the prospective
4720 new reload ceases to be live, and TIME2 for each existing
4721 reload as the time when that the reload register of that reload
4722 becomes live.
4723 Where there is little to be gained by exact lifetime calculations,
4724 we just make conservative assumptions, i.e. a longer lifetime;
4725 this is done in the 'default:' cases. */
4726 switch (type)
4728 case RELOAD_FOR_OTHER_ADDRESS:
4729 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4730 time1 = copy ? 0 : 1;
4731 break;
4732 case RELOAD_OTHER:
4733 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4734 break;
4735 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4736 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4737 respectively, to the time values for these, we get distinct time
4738 values. To get distinct time values for each operand, we have to
4739 multiply opnum by at least three. We round that up to four because
4740 multiply by four is often cheaper. */
4741 case RELOAD_FOR_INPADDR_ADDRESS:
4742 time1 = opnum * 4 + 2;
4743 break;
4744 case RELOAD_FOR_INPUT_ADDRESS:
4745 time1 = opnum * 4 + 3;
4746 break;
4747 case RELOAD_FOR_INPUT:
4748 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4749 executes (inclusive). */
4750 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
4751 break;
4752 case RELOAD_FOR_OPADDR_ADDR:
4753 /* opnum * 4 + 4
4754 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
4755 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4756 break;
4757 case RELOAD_FOR_OPERAND_ADDRESS:
4758 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4759 is executed. */
4760 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4761 break;
4762 case RELOAD_FOR_OUTADDR_ADDRESS:
4763 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
4764 break;
4765 case RELOAD_FOR_OUTPUT_ADDRESS:
4766 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
4767 break;
4768 default:
4769 time1 = MAX_RECOG_OPERANDS * 5 + 5;
4772 for (i = 0; i < n_reloads; i++)
4774 rtx reg = rld[i].reg_rtx;
4775 if (reg && GET_CODE (reg) == REG
4776 && ((unsigned) regno - true_regnum (reg)
4777 <= HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)) - (unsigned) 1)
4778 && i != reloadnum)
4780 rtx other_input = rld[i].in;
4782 /* If the other reload loads the same input value, that
4783 will not cause a conflict only if it's loading it into
4784 the same register. */
4785 if (true_regnum (reg) != start_regno)
4786 other_input = NULL_RTX;
4787 if (! other_input || ! rtx_equal_p (other_input, value)
4788 || rld[i].out || out)
4790 int time2;
4791 switch (rld[i].when_needed)
4793 case RELOAD_FOR_OTHER_ADDRESS:
4794 time2 = 0;
4795 break;
4796 case RELOAD_FOR_INPADDR_ADDRESS:
4797 /* find_reloads makes sure that a
4798 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4799 by at most one - the first -
4800 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4801 address reload is inherited, the address address reload
4802 goes away, so we can ignore this conflict. */
4803 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4804 && ignore_address_reloads
4805 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4806 Then the address address is still needed to store
4807 back the new address. */
4808 && ! rld[reloadnum].out)
4809 continue;
4810 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4811 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4812 reloads go away. */
4813 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4814 && ignore_address_reloads
4815 /* Unless we are reloading an auto_inc expression. */
4816 && ! rld[reloadnum].out)
4817 continue;
4818 time2 = rld[i].opnum * 4 + 2;
4819 break;
4820 case RELOAD_FOR_INPUT_ADDRESS:
4821 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4822 && ignore_address_reloads
4823 && ! rld[reloadnum].out)
4824 continue;
4825 time2 = rld[i].opnum * 4 + 3;
4826 break;
4827 case RELOAD_FOR_INPUT:
4828 time2 = rld[i].opnum * 4 + 4;
4829 check_earlyclobber = 1;
4830 break;
4831 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
4832 == MAX_RECOG_OPERAND * 4 */
4833 case RELOAD_FOR_OPADDR_ADDR:
4834 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4835 && ignore_address_reloads
4836 && ! rld[reloadnum].out)
4837 continue;
4838 time2 = MAX_RECOG_OPERANDS * 4 + 1;
4839 break;
4840 case RELOAD_FOR_OPERAND_ADDRESS:
4841 time2 = MAX_RECOG_OPERANDS * 4 + 2;
4842 check_earlyclobber = 1;
4843 break;
4844 case RELOAD_FOR_INSN:
4845 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4846 break;
4847 case RELOAD_FOR_OUTPUT:
4848 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4849 instruction is executed. */
4850 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4851 break;
4852 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4853 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4854 value. */
4855 case RELOAD_FOR_OUTADDR_ADDRESS:
4856 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4857 && ignore_address_reloads
4858 && ! rld[reloadnum].out)
4859 continue;
4860 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
4861 break;
4862 case RELOAD_FOR_OUTPUT_ADDRESS:
4863 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
4864 break;
4865 case RELOAD_OTHER:
4866 /* If there is no conflict in the input part, handle this
4867 like an output reload. */
4868 if (! rld[i].in || rtx_equal_p (other_input, value))
4870 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4871 /* Earlyclobbered outputs must conflict with inputs. */
4872 if (earlyclobber_operand_p (rld[i].out))
4873 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4875 break;
4877 time2 = 1;
4878 /* RELOAD_OTHER might be live beyond instruction execution,
4879 but this is not obvious when we set time2 = 1. So check
4880 here if there might be a problem with the new reload
4881 clobbering the register used by the RELOAD_OTHER. */
4882 if (out)
4883 return 0;
4884 break;
4885 default:
4886 return 0;
4888 if ((time1 >= time2
4889 && (! rld[i].in || rld[i].out
4890 || ! rtx_equal_p (other_input, value)))
4891 || (out && rld[reloadnum].out_reg
4892 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
4893 return 0;
4898 /* Earlyclobbered outputs must conflict with inputs. */
4899 if (check_earlyclobber && out && earlyclobber_operand_p (out))
4900 return 0;
4902 return 1;
4905 /* Return 1 if the value in reload reg REGNO, as used by a reload
4906 needed for the part of the insn specified by OPNUM and TYPE,
4907 may be used to load VALUE into it.
4909 MODE is the mode in which the register is used, this is needed to
4910 determine how many hard regs to test.
4912 Other read-only reloads with the same value do not conflict
4913 unless OUT is nonzero and these other reloads have to live while
4914 output reloads live.
4915 If OUT is CONST0_RTX, this is a special case: it means that the
4916 test should not be for using register REGNO as reload register, but
4917 for copying from register REGNO into the reload register.
4919 RELOADNUM is the number of the reload we want to load this value for;
4920 a reload does not conflict with itself.
4922 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
4923 reloads that load an address for the very reload we are considering.
4925 The caller has to make sure that there is no conflict with the return
4926 register. */
4928 static int
4929 free_for_value_p (int regno, enum machine_mode mode, int opnum,
4930 enum reload_type type, rtx value, rtx out, int reloadnum,
4931 int ignore_address_reloads)
4933 int nregs = HARD_REGNO_NREGS (regno, mode);
4934 while (nregs-- > 0)
4935 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
4936 value, out, reloadnum,
4937 ignore_address_reloads))
4938 return 0;
4939 return 1;
4942 /* Determine whether the reload reg X overlaps any rtx'es used for
4943 overriding inheritance. Return nonzero if so. */
4945 static int
4946 conflicts_with_override (rtx x)
4948 int i;
4949 for (i = 0; i < n_reloads; i++)
4950 if (reload_override_in[i]
4951 && reg_overlap_mentioned_p (x, reload_override_in[i]))
4952 return 1;
4953 return 0;
4956 /* Give an error message saying we failed to find a reload for INSN,
4957 and clear out reload R. */
4958 static void
4959 failed_reload (rtx insn, int r)
4961 if (asm_noperands (PATTERN (insn)) < 0)
4962 /* It's the compiler's fault. */
4963 fatal_insn ("could not find a spill register", insn);
4965 /* It's the user's fault; the operand's mode and constraint
4966 don't match. Disable this reload so we don't crash in final. */
4967 error_for_asm (insn,
4968 "`asm' operand constraint incompatible with operand size");
4969 rld[r].in = 0;
4970 rld[r].out = 0;
4971 rld[r].reg_rtx = 0;
4972 rld[r].optional = 1;
4973 rld[r].secondary_p = 1;
4976 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
4977 for reload R. If it's valid, get an rtx for it. Return nonzero if
4978 successful. */
4979 static int
4980 set_reload_reg (int i, int r)
4982 int regno;
4983 rtx reg = spill_reg_rtx[i];
4985 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
4986 spill_reg_rtx[i] = reg
4987 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
4989 regno = true_regnum (reg);
4991 /* Detect when the reload reg can't hold the reload mode.
4992 This used to be one `if', but Sequent compiler can't handle that. */
4993 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
4995 enum machine_mode test_mode = VOIDmode;
4996 if (rld[r].in)
4997 test_mode = GET_MODE (rld[r].in);
4998 /* If rld[r].in has VOIDmode, it means we will load it
4999 in whatever mode the reload reg has: to wit, rld[r].mode.
5000 We have already tested that for validity. */
5001 /* Aside from that, we need to test that the expressions
5002 to reload from or into have modes which are valid for this
5003 reload register. Otherwise the reload insns would be invalid. */
5004 if (! (rld[r].in != 0 && test_mode != VOIDmode
5005 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5006 if (! (rld[r].out != 0
5007 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5009 /* The reg is OK. */
5010 last_spill_reg = i;
5012 /* Mark as in use for this insn the reload regs we use
5013 for this. */
5014 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5015 rld[r].when_needed, rld[r].mode);
5017 rld[r].reg_rtx = reg;
5018 reload_spill_index[r] = spill_regs[i];
5019 return 1;
5022 return 0;
5025 /* Find a spill register to use as a reload register for reload R.
5026 LAST_RELOAD is nonzero if this is the last reload for the insn being
5027 processed.
5029 Set rld[R].reg_rtx to the register allocated.
5031 We return 1 if successful, or 0 if we couldn't find a spill reg and
5032 we didn't change anything. */
5034 static int
5035 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5036 int last_reload)
5038 int i, pass, count;
5040 /* If we put this reload ahead, thinking it is a group,
5041 then insist on finding a group. Otherwise we can grab a
5042 reg that some other reload needs.
5043 (That can happen when we have a 68000 DATA_OR_FP_REG
5044 which is a group of data regs or one fp reg.)
5045 We need not be so restrictive if there are no more reloads
5046 for this insn.
5048 ??? Really it would be nicer to have smarter handling
5049 for that kind of reg class, where a problem like this is normal.
5050 Perhaps those classes should be avoided for reloading
5051 by use of more alternatives. */
5053 int force_group = rld[r].nregs > 1 && ! last_reload;
5055 /* If we want a single register and haven't yet found one,
5056 take any reg in the right class and not in use.
5057 If we want a consecutive group, here is where we look for it.
5059 We use two passes so we can first look for reload regs to
5060 reuse, which are already in use for other reloads in this insn,
5061 and only then use additional registers.
5062 I think that maximizing reuse is needed to make sure we don't
5063 run out of reload regs. Suppose we have three reloads, and
5064 reloads A and B can share regs. These need two regs.
5065 Suppose A and B are given different regs.
5066 That leaves none for C. */
5067 for (pass = 0; pass < 2; pass++)
5069 /* I is the index in spill_regs.
5070 We advance it round-robin between insns to use all spill regs
5071 equally, so that inherited reloads have a chance
5072 of leapfrogging each other. */
5074 i = last_spill_reg;
5076 for (count = 0; count < n_spills; count++)
5078 int class = (int) rld[r].class;
5079 int regnum;
5081 i++;
5082 if (i >= n_spills)
5083 i -= n_spills;
5084 regnum = spill_regs[i];
5086 if ((reload_reg_free_p (regnum, rld[r].opnum,
5087 rld[r].when_needed)
5088 || (rld[r].in
5089 /* We check reload_reg_used to make sure we
5090 don't clobber the return register. */
5091 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5092 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5093 rld[r].when_needed, rld[r].in,
5094 rld[r].out, r, 1)))
5095 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5096 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5097 /* Look first for regs to share, then for unshared. But
5098 don't share regs used for inherited reloads; they are
5099 the ones we want to preserve. */
5100 && (pass
5101 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5102 regnum)
5103 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5104 regnum))))
5106 int nr = HARD_REGNO_NREGS (regnum, rld[r].mode);
5107 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5108 (on 68000) got us two FP regs. If NR is 1,
5109 we would reject both of them. */
5110 if (force_group)
5111 nr = rld[r].nregs;
5112 /* If we need only one reg, we have already won. */
5113 if (nr == 1)
5115 /* But reject a single reg if we demand a group. */
5116 if (force_group)
5117 continue;
5118 break;
5120 /* Otherwise check that as many consecutive regs as we need
5121 are available here. */
5122 while (nr > 1)
5124 int regno = regnum + nr - 1;
5125 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5126 && spill_reg_order[regno] >= 0
5127 && reload_reg_free_p (regno, rld[r].opnum,
5128 rld[r].when_needed)))
5129 break;
5130 nr--;
5132 if (nr == 1)
5133 break;
5137 /* If we found something on pass 1, omit pass 2. */
5138 if (count < n_spills)
5139 break;
5142 /* We should have found a spill register by now. */
5143 if (count >= n_spills)
5144 return 0;
5146 /* I is the index in SPILL_REG_RTX of the reload register we are to
5147 allocate. Get an rtx for it and find its register number. */
5149 return set_reload_reg (i, r);
5152 /* Initialize all the tables needed to allocate reload registers.
5153 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5154 is the array we use to restore the reg_rtx field for every reload. */
5156 static void
5157 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5159 int i;
5161 for (i = 0; i < n_reloads; i++)
5162 rld[i].reg_rtx = save_reload_reg_rtx[i];
5164 memset (reload_inherited, 0, MAX_RELOADS);
5165 memset ((char *) reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5166 memset ((char *) reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5168 CLEAR_HARD_REG_SET (reload_reg_used);
5169 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5170 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5171 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5172 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5173 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5175 CLEAR_HARD_REG_SET (reg_used_in_insn);
5177 HARD_REG_SET tmp;
5178 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5179 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5180 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5181 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5182 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5183 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5186 for (i = 0; i < reload_n_operands; i++)
5188 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5189 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5190 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5191 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5192 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5193 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5196 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5198 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5200 for (i = 0; i < n_reloads; i++)
5201 /* If we have already decided to use a certain register,
5202 don't use it in another way. */
5203 if (rld[i].reg_rtx)
5204 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5205 rld[i].when_needed, rld[i].mode);
5208 /* Assign hard reg targets for the pseudo-registers we must reload
5209 into hard regs for this insn.
5210 Also output the instructions to copy them in and out of the hard regs.
5212 For machines with register classes, we are responsible for
5213 finding a reload reg in the proper class. */
5215 static void
5216 choose_reload_regs (struct insn_chain *chain)
5218 rtx insn = chain->insn;
5219 int i, j;
5220 unsigned int max_group_size = 1;
5221 enum reg_class group_class = NO_REGS;
5222 int pass, win, inheritance;
5224 rtx save_reload_reg_rtx[MAX_RELOADS];
5226 /* In order to be certain of getting the registers we need,
5227 we must sort the reloads into order of increasing register class.
5228 Then our grabbing of reload registers will parallel the process
5229 that provided the reload registers.
5231 Also note whether any of the reloads wants a consecutive group of regs.
5232 If so, record the maximum size of the group desired and what
5233 register class contains all the groups needed by this insn. */
5235 for (j = 0; j < n_reloads; j++)
5237 reload_order[j] = j;
5238 reload_spill_index[j] = -1;
5240 if (rld[j].nregs > 1)
5242 max_group_size = MAX (rld[j].nregs, max_group_size);
5243 group_class
5244 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5247 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5250 if (n_reloads > 1)
5251 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5253 /* If -O, try first with inheritance, then turning it off.
5254 If not -O, don't do inheritance.
5255 Using inheritance when not optimizing leads to paradoxes
5256 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5257 because one side of the comparison might be inherited. */
5258 win = 0;
5259 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5261 choose_reload_regs_init (chain, save_reload_reg_rtx);
5263 /* Process the reloads in order of preference just found.
5264 Beyond this point, subregs can be found in reload_reg_rtx.
5266 This used to look for an existing reloaded home for all of the
5267 reloads, and only then perform any new reloads. But that could lose
5268 if the reloads were done out of reg-class order because a later
5269 reload with a looser constraint might have an old home in a register
5270 needed by an earlier reload with a tighter constraint.
5272 To solve this, we make two passes over the reloads, in the order
5273 described above. In the first pass we try to inherit a reload
5274 from a previous insn. If there is a later reload that needs a
5275 class that is a proper subset of the class being processed, we must
5276 also allocate a spill register during the first pass.
5278 Then make a second pass over the reloads to allocate any reloads
5279 that haven't been given registers yet. */
5281 for (j = 0; j < n_reloads; j++)
5283 int r = reload_order[j];
5284 rtx search_equiv = NULL_RTX;
5286 /* Ignore reloads that got marked inoperative. */
5287 if (rld[r].out == 0 && rld[r].in == 0
5288 && ! rld[r].secondary_p)
5289 continue;
5291 /* If find_reloads chose to use reload_in or reload_out as a reload
5292 register, we don't need to chose one. Otherwise, try even if it
5293 found one since we might save an insn if we find the value lying
5294 around.
5295 Try also when reload_in is a pseudo without a hard reg. */
5296 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5297 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5298 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5299 && GET_CODE (rld[r].in) != MEM
5300 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5301 continue;
5303 #if 0 /* No longer needed for correct operation.
5304 It might give better code, or might not; worth an experiment? */
5305 /* If this is an optional reload, we can't inherit from earlier insns
5306 until we are sure that any non-optional reloads have been allocated.
5307 The following code takes advantage of the fact that optional reloads
5308 are at the end of reload_order. */
5309 if (rld[r].optional != 0)
5310 for (i = 0; i < j; i++)
5311 if ((rld[reload_order[i]].out != 0
5312 || rld[reload_order[i]].in != 0
5313 || rld[reload_order[i]].secondary_p)
5314 && ! rld[reload_order[i]].optional
5315 && rld[reload_order[i]].reg_rtx == 0)
5316 allocate_reload_reg (chain, reload_order[i], 0);
5317 #endif
5319 /* First see if this pseudo is already available as reloaded
5320 for a previous insn. We cannot try to inherit for reloads
5321 that are smaller than the maximum number of registers needed
5322 for groups unless the register we would allocate cannot be used
5323 for the groups.
5325 We could check here to see if this is a secondary reload for
5326 an object that is already in a register of the desired class.
5327 This would avoid the need for the secondary reload register.
5328 But this is complex because we can't easily determine what
5329 objects might want to be loaded via this reload. So let a
5330 register be allocated here. In `emit_reload_insns' we suppress
5331 one of the loads in the case described above. */
5333 if (inheritance)
5335 int byte = 0;
5336 int regno = -1;
5337 enum machine_mode mode = VOIDmode;
5339 if (rld[r].in == 0)
5341 else if (GET_CODE (rld[r].in) == REG)
5343 regno = REGNO (rld[r].in);
5344 mode = GET_MODE (rld[r].in);
5346 else if (GET_CODE (rld[r].in_reg) == REG)
5348 regno = REGNO (rld[r].in_reg);
5349 mode = GET_MODE (rld[r].in_reg);
5351 else if (GET_CODE (rld[r].in_reg) == SUBREG
5352 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5354 byte = SUBREG_BYTE (rld[r].in_reg);
5355 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5356 if (regno < FIRST_PSEUDO_REGISTER)
5357 regno = subreg_regno (rld[r].in_reg);
5358 mode = GET_MODE (rld[r].in_reg);
5360 #ifdef AUTO_INC_DEC
5361 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5362 || GET_CODE (rld[r].in_reg) == PRE_DEC
5363 || GET_CODE (rld[r].in_reg) == POST_INC
5364 || GET_CODE (rld[r].in_reg) == POST_DEC)
5365 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5367 regno = REGNO (XEXP (rld[r].in_reg, 0));
5368 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5369 rld[r].out = rld[r].in;
5371 #endif
5372 #if 0
5373 /* This won't work, since REGNO can be a pseudo reg number.
5374 Also, it takes much more hair to keep track of all the things
5375 that can invalidate an inherited reload of part of a pseudoreg. */
5376 else if (GET_CODE (rld[r].in) == SUBREG
5377 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5378 regno = subreg_regno (rld[r].in);
5379 #endif
5381 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5383 enum reg_class class = rld[r].class, last_class;
5384 rtx last_reg = reg_last_reload_reg[regno];
5385 enum machine_mode need_mode;
5387 i = REGNO (last_reg);
5388 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5389 last_class = REGNO_REG_CLASS (i);
5391 if (byte == 0)
5392 need_mode = mode;
5393 else
5394 need_mode
5395 = smallest_mode_for_size (GET_MODE_SIZE (mode) + byte,
5396 GET_MODE_CLASS (mode));
5398 if (
5399 #ifdef CANNOT_CHANGE_MODE_CLASS
5400 (!REG_CANNOT_CHANGE_MODE_P (i, GET_MODE (last_reg),
5401 need_mode)
5403 #endif
5404 (GET_MODE_SIZE (GET_MODE (last_reg))
5405 >= GET_MODE_SIZE (need_mode))
5406 #ifdef CANNOT_CHANGE_MODE_CLASS
5408 #endif
5409 && reg_reloaded_contents[i] == regno
5410 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5411 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5412 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5413 /* Even if we can't use this register as a reload
5414 register, we might use it for reload_override_in,
5415 if copying it to the desired class is cheap
5416 enough. */
5417 || ((REGISTER_MOVE_COST (mode, last_class, class)
5418 < MEMORY_MOVE_COST (mode, class, 1))
5419 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5420 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5421 last_reg)
5422 == NO_REGS)
5423 #endif
5424 #ifdef SECONDARY_MEMORY_NEEDED
5425 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5426 mode)
5427 #endif
5430 && (rld[r].nregs == max_group_size
5431 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5433 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5434 rld[r].when_needed, rld[r].in,
5435 const0_rtx, r, 1))
5437 /* If a group is needed, verify that all the subsequent
5438 registers still have their values intact. */
5439 int nr = HARD_REGNO_NREGS (i, rld[r].mode);
5440 int k;
5442 for (k = 1; k < nr; k++)
5443 if (reg_reloaded_contents[i + k] != regno
5444 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5445 break;
5447 if (k == nr)
5449 int i1;
5450 int bad_for_class;
5452 last_reg = (GET_MODE (last_reg) == mode
5453 ? last_reg : gen_rtx_REG (mode, i));
5455 bad_for_class = 0;
5456 for (k = 0; k < nr; k++)
5457 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5458 i+k);
5460 /* We found a register that contains the
5461 value we need. If this register is the
5462 same as an `earlyclobber' operand of the
5463 current insn, just mark it as a place to
5464 reload from since we can't use it as the
5465 reload register itself. */
5467 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5468 if (reg_overlap_mentioned_for_reload_p
5469 (reg_last_reload_reg[regno],
5470 reload_earlyclobbers[i1]))
5471 break;
5473 if (i1 != n_earlyclobbers
5474 || ! (free_for_value_p (i, rld[r].mode,
5475 rld[r].opnum,
5476 rld[r].when_needed, rld[r].in,
5477 rld[r].out, r, 1))
5478 /* Don't use it if we'd clobber a pseudo reg. */
5479 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5480 && rld[r].out
5481 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5482 /* Don't clobber the frame pointer. */
5483 || (i == HARD_FRAME_POINTER_REGNUM
5484 && frame_pointer_needed
5485 && rld[r].out)
5486 /* Don't really use the inherited spill reg
5487 if we need it wider than we've got it. */
5488 || (GET_MODE_SIZE (rld[r].mode)
5489 > GET_MODE_SIZE (mode))
5490 || bad_for_class
5492 /* If find_reloads chose reload_out as reload
5493 register, stay with it - that leaves the
5494 inherited register for subsequent reloads. */
5495 || (rld[r].out && rld[r].reg_rtx
5496 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5498 if (! rld[r].optional)
5500 reload_override_in[r] = last_reg;
5501 reload_inheritance_insn[r]
5502 = reg_reloaded_insn[i];
5505 else
5507 int k;
5508 /* We can use this as a reload reg. */
5509 /* Mark the register as in use for this part of
5510 the insn. */
5511 mark_reload_reg_in_use (i,
5512 rld[r].opnum,
5513 rld[r].when_needed,
5514 rld[r].mode);
5515 rld[r].reg_rtx = last_reg;
5516 reload_inherited[r] = 1;
5517 reload_inheritance_insn[r]
5518 = reg_reloaded_insn[i];
5519 reload_spill_index[r] = i;
5520 for (k = 0; k < nr; k++)
5521 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5522 i + k);
5529 /* Here's another way to see if the value is already lying around. */
5530 if (inheritance
5531 && rld[r].in != 0
5532 && ! reload_inherited[r]
5533 && rld[r].out == 0
5534 && (CONSTANT_P (rld[r].in)
5535 || GET_CODE (rld[r].in) == PLUS
5536 || GET_CODE (rld[r].in) == REG
5537 || GET_CODE (rld[r].in) == MEM)
5538 && (rld[r].nregs == max_group_size
5539 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5540 search_equiv = rld[r].in;
5541 /* If this is an output reload from a simple move insn, look
5542 if an equivalence for the input is available. */
5543 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5545 rtx set = single_set (insn);
5547 if (set
5548 && rtx_equal_p (rld[r].out, SET_DEST (set))
5549 && CONSTANT_P (SET_SRC (set)))
5550 search_equiv = SET_SRC (set);
5553 if (search_equiv)
5555 rtx equiv
5556 = find_equiv_reg (search_equiv, insn, rld[r].class,
5557 -1, NULL, 0, rld[r].mode);
5558 int regno = 0;
5560 if (equiv != 0)
5562 if (GET_CODE (equiv) == REG)
5563 regno = REGNO (equiv);
5564 else if (GET_CODE (equiv) == SUBREG)
5566 /* This must be a SUBREG of a hard register.
5567 Make a new REG since this might be used in an
5568 address and not all machines support SUBREGs
5569 there. */
5570 regno = subreg_regno (equiv);
5571 equiv = gen_rtx_REG (rld[r].mode, regno);
5573 else
5574 abort ();
5577 /* If we found a spill reg, reject it unless it is free
5578 and of the desired class. */
5579 if (equiv != 0)
5581 int regs_used = 0;
5582 int bad_for_class = 0;
5583 int max_regno = regno + rld[r].nregs;
5585 for (i = regno; i < max_regno; i++)
5587 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
5589 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5593 if ((regs_used
5594 && ! free_for_value_p (regno, rld[r].mode,
5595 rld[r].opnum, rld[r].when_needed,
5596 rld[r].in, rld[r].out, r, 1))
5597 || bad_for_class)
5598 equiv = 0;
5601 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5602 equiv = 0;
5604 /* We found a register that contains the value we need.
5605 If this register is the same as an `earlyclobber' operand
5606 of the current insn, just mark it as a place to reload from
5607 since we can't use it as the reload register itself. */
5609 if (equiv != 0)
5610 for (i = 0; i < n_earlyclobbers; i++)
5611 if (reg_overlap_mentioned_for_reload_p (equiv,
5612 reload_earlyclobbers[i]))
5614 if (! rld[r].optional)
5615 reload_override_in[r] = equiv;
5616 equiv = 0;
5617 break;
5620 /* If the equiv register we have found is explicitly clobbered
5621 in the current insn, it depends on the reload type if we
5622 can use it, use it for reload_override_in, or not at all.
5623 In particular, we then can't use EQUIV for a
5624 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5626 if (equiv != 0)
5628 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5629 switch (rld[r].when_needed)
5631 case RELOAD_FOR_OTHER_ADDRESS:
5632 case RELOAD_FOR_INPADDR_ADDRESS:
5633 case RELOAD_FOR_INPUT_ADDRESS:
5634 case RELOAD_FOR_OPADDR_ADDR:
5635 break;
5636 case RELOAD_OTHER:
5637 case RELOAD_FOR_INPUT:
5638 case RELOAD_FOR_OPERAND_ADDRESS:
5639 if (! rld[r].optional)
5640 reload_override_in[r] = equiv;
5641 /* Fall through. */
5642 default:
5643 equiv = 0;
5644 break;
5646 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5647 switch (rld[r].when_needed)
5649 case RELOAD_FOR_OTHER_ADDRESS:
5650 case RELOAD_FOR_INPADDR_ADDRESS:
5651 case RELOAD_FOR_INPUT_ADDRESS:
5652 case RELOAD_FOR_OPADDR_ADDR:
5653 case RELOAD_FOR_OPERAND_ADDRESS:
5654 case RELOAD_FOR_INPUT:
5655 break;
5656 case RELOAD_OTHER:
5657 if (! rld[r].optional)
5658 reload_override_in[r] = equiv;
5659 /* Fall through. */
5660 default:
5661 equiv = 0;
5662 break;
5666 /* If we found an equivalent reg, say no code need be generated
5667 to load it, and use it as our reload reg. */
5668 if (equiv != 0
5669 && (regno != HARD_FRAME_POINTER_REGNUM
5670 || !frame_pointer_needed))
5672 int nr = HARD_REGNO_NREGS (regno, rld[r].mode);
5673 int k;
5674 rld[r].reg_rtx = equiv;
5675 reload_inherited[r] = 1;
5677 /* If reg_reloaded_valid is not set for this register,
5678 there might be a stale spill_reg_store lying around.
5679 We must clear it, since otherwise emit_reload_insns
5680 might delete the store. */
5681 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5682 spill_reg_store[regno] = NULL_RTX;
5683 /* If any of the hard registers in EQUIV are spill
5684 registers, mark them as in use for this insn. */
5685 for (k = 0; k < nr; k++)
5687 i = spill_reg_order[regno + k];
5688 if (i >= 0)
5690 mark_reload_reg_in_use (regno, rld[r].opnum,
5691 rld[r].when_needed,
5692 rld[r].mode);
5693 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5694 regno + k);
5700 /* If we found a register to use already, or if this is an optional
5701 reload, we are done. */
5702 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5703 continue;
5705 #if 0
5706 /* No longer needed for correct operation. Might or might
5707 not give better code on the average. Want to experiment? */
5709 /* See if there is a later reload that has a class different from our
5710 class that intersects our class or that requires less register
5711 than our reload. If so, we must allocate a register to this
5712 reload now, since that reload might inherit a previous reload
5713 and take the only available register in our class. Don't do this
5714 for optional reloads since they will force all previous reloads
5715 to be allocated. Also don't do this for reloads that have been
5716 turned off. */
5718 for (i = j + 1; i < n_reloads; i++)
5720 int s = reload_order[i];
5722 if ((rld[s].in == 0 && rld[s].out == 0
5723 && ! rld[s].secondary_p)
5724 || rld[s].optional)
5725 continue;
5727 if ((rld[s].class != rld[r].class
5728 && reg_classes_intersect_p (rld[r].class,
5729 rld[s].class))
5730 || rld[s].nregs < rld[r].nregs)
5731 break;
5734 if (i == n_reloads)
5735 continue;
5737 allocate_reload_reg (chain, r, j == n_reloads - 1);
5738 #endif
5741 /* Now allocate reload registers for anything non-optional that
5742 didn't get one yet. */
5743 for (j = 0; j < n_reloads; j++)
5745 int r = reload_order[j];
5747 /* Ignore reloads that got marked inoperative. */
5748 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
5749 continue;
5751 /* Skip reloads that already have a register allocated or are
5752 optional. */
5753 if (rld[r].reg_rtx != 0 || rld[r].optional)
5754 continue;
5756 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
5757 break;
5760 /* If that loop got all the way, we have won. */
5761 if (j == n_reloads)
5763 win = 1;
5764 break;
5767 /* Loop around and try without any inheritance. */
5770 if (! win)
5772 /* First undo everything done by the failed attempt
5773 to allocate with inheritance. */
5774 choose_reload_regs_init (chain, save_reload_reg_rtx);
5776 /* Some sanity tests to verify that the reloads found in the first
5777 pass are identical to the ones we have now. */
5778 if (chain->n_reloads != n_reloads)
5779 abort ();
5781 for (i = 0; i < n_reloads; i++)
5783 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5784 continue;
5785 if (chain->rld[i].when_needed != rld[i].when_needed)
5786 abort ();
5787 for (j = 0; j < n_spills; j++)
5788 if (spill_regs[j] == chain->rld[i].regno)
5789 if (! set_reload_reg (j, i))
5790 failed_reload (chain->insn, i);
5794 /* If we thought we could inherit a reload, because it seemed that
5795 nothing else wanted the same reload register earlier in the insn,
5796 verify that assumption, now that all reloads have been assigned.
5797 Likewise for reloads where reload_override_in has been set. */
5799 /* If doing expensive optimizations, do one preliminary pass that doesn't
5800 cancel any inheritance, but removes reloads that have been needed only
5801 for reloads that we know can be inherited. */
5802 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
5804 for (j = 0; j < n_reloads; j++)
5806 int r = reload_order[j];
5807 rtx check_reg;
5808 if (reload_inherited[r] && rld[r].reg_rtx)
5809 check_reg = rld[r].reg_rtx;
5810 else if (reload_override_in[r]
5811 && (GET_CODE (reload_override_in[r]) == REG
5812 || GET_CODE (reload_override_in[r]) == SUBREG))
5813 check_reg = reload_override_in[r];
5814 else
5815 continue;
5816 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5817 rld[r].opnum, rld[r].when_needed, rld[r].in,
5818 (reload_inherited[r]
5819 ? rld[r].out : const0_rtx),
5820 r, 1))
5822 if (pass)
5823 continue;
5824 reload_inherited[r] = 0;
5825 reload_override_in[r] = 0;
5827 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5828 reload_override_in, then we do not need its related
5829 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5830 likewise for other reload types.
5831 We handle this by removing a reload when its only replacement
5832 is mentioned in reload_in of the reload we are going to inherit.
5833 A special case are auto_inc expressions; even if the input is
5834 inherited, we still need the address for the output. We can
5835 recognize them because they have RELOAD_OUT set to RELOAD_IN.
5836 If we succeeded removing some reload and we are doing a preliminary
5837 pass just to remove such reloads, make another pass, since the
5838 removal of one reload might allow us to inherit another one. */
5839 else if (rld[r].in
5840 && rld[r].out != rld[r].in
5841 && remove_address_replacements (rld[r].in) && pass)
5842 pass = 2;
5846 /* Now that reload_override_in is known valid,
5847 actually override reload_in. */
5848 for (j = 0; j < n_reloads; j++)
5849 if (reload_override_in[j])
5850 rld[j].in = reload_override_in[j];
5852 /* If this reload won't be done because it has been canceled or is
5853 optional and not inherited, clear reload_reg_rtx so other
5854 routines (such as subst_reloads) don't get confused. */
5855 for (j = 0; j < n_reloads; j++)
5856 if (rld[j].reg_rtx != 0
5857 && ((rld[j].optional && ! reload_inherited[j])
5858 || (rld[j].in == 0 && rld[j].out == 0
5859 && ! rld[j].secondary_p)))
5861 int regno = true_regnum (rld[j].reg_rtx);
5863 if (spill_reg_order[regno] >= 0)
5864 clear_reload_reg_in_use (regno, rld[j].opnum,
5865 rld[j].when_needed, rld[j].mode);
5866 rld[j].reg_rtx = 0;
5867 reload_spill_index[j] = -1;
5870 /* Record which pseudos and which spill regs have output reloads. */
5871 for (j = 0; j < n_reloads; j++)
5873 int r = reload_order[j];
5875 i = reload_spill_index[r];
5877 /* I is nonneg if this reload uses a register.
5878 If rld[r].reg_rtx is 0, this is an optional reload
5879 that we opted to ignore. */
5880 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
5881 && rld[r].reg_rtx != 0)
5883 int nregno = REGNO (rld[r].out_reg);
5884 int nr = 1;
5886 if (nregno < FIRST_PSEUDO_REGISTER)
5887 nr = HARD_REGNO_NREGS (nregno, rld[r].mode);
5889 while (--nr >= 0)
5890 reg_has_output_reload[nregno + nr] = 1;
5892 if (i >= 0)
5894 nr = HARD_REGNO_NREGS (i, rld[r].mode);
5895 while (--nr >= 0)
5896 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
5899 if (rld[r].when_needed != RELOAD_OTHER
5900 && rld[r].when_needed != RELOAD_FOR_OUTPUT
5901 && rld[r].when_needed != RELOAD_FOR_INSN)
5902 abort ();
5907 /* Deallocate the reload register for reload R. This is called from
5908 remove_address_replacements. */
5910 void
5911 deallocate_reload_reg (int r)
5913 int regno;
5915 if (! rld[r].reg_rtx)
5916 return;
5917 regno = true_regnum (rld[r].reg_rtx);
5918 rld[r].reg_rtx = 0;
5919 if (spill_reg_order[regno] >= 0)
5920 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
5921 rld[r].mode);
5922 reload_spill_index[r] = -1;
5925 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
5926 reloads of the same item for fear that we might not have enough reload
5927 registers. However, normally they will get the same reload register
5928 and hence actually need not be loaded twice.
5930 Here we check for the most common case of this phenomenon: when we have
5931 a number of reloads for the same object, each of which were allocated
5932 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5933 reload, and is not modified in the insn itself. If we find such,
5934 merge all the reloads and set the resulting reload to RELOAD_OTHER.
5935 This will not increase the number of spill registers needed and will
5936 prevent redundant code. */
5938 static void
5939 merge_assigned_reloads (rtx insn)
5941 int i, j;
5943 /* Scan all the reloads looking for ones that only load values and
5944 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
5945 assigned and not modified by INSN. */
5947 for (i = 0; i < n_reloads; i++)
5949 int conflicting_input = 0;
5950 int max_input_address_opnum = -1;
5951 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
5953 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
5954 || rld[i].out != 0 || rld[i].reg_rtx == 0
5955 || reg_set_p (rld[i].reg_rtx, insn))
5956 continue;
5958 /* Look at all other reloads. Ensure that the only use of this
5959 reload_reg_rtx is in a reload that just loads the same value
5960 as we do. Note that any secondary reloads must be of the identical
5961 class since the values, modes, and result registers are the
5962 same, so we need not do anything with any secondary reloads. */
5964 for (j = 0; j < n_reloads; j++)
5966 if (i == j || rld[j].reg_rtx == 0
5967 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
5968 rld[i].reg_rtx))
5969 continue;
5971 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
5972 && rld[j].opnum > max_input_address_opnum)
5973 max_input_address_opnum = rld[j].opnum;
5975 /* If the reload regs aren't exactly the same (e.g, different modes)
5976 or if the values are different, we can't merge this reload.
5977 But if it is an input reload, we might still merge
5978 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
5980 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
5981 || rld[j].out != 0 || rld[j].in == 0
5982 || ! rtx_equal_p (rld[i].in, rld[j].in))
5984 if (rld[j].when_needed != RELOAD_FOR_INPUT
5985 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
5986 || rld[i].opnum > rld[j].opnum)
5987 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
5988 break;
5989 conflicting_input = 1;
5990 if (min_conflicting_input_opnum > rld[j].opnum)
5991 min_conflicting_input_opnum = rld[j].opnum;
5995 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
5996 we, in fact, found any matching reloads. */
5998 if (j == n_reloads
5999 && max_input_address_opnum <= min_conflicting_input_opnum)
6001 for (j = 0; j < n_reloads; j++)
6002 if (i != j && rld[j].reg_rtx != 0
6003 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6004 && (! conflicting_input
6005 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6006 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6008 rld[i].when_needed = RELOAD_OTHER;
6009 rld[j].in = 0;
6010 reload_spill_index[j] = -1;
6011 transfer_replacements (i, j);
6014 /* If this is now RELOAD_OTHER, look for any reloads that load
6015 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6016 if they were for inputs, RELOAD_OTHER for outputs. Note that
6017 this test is equivalent to looking for reloads for this operand
6018 number. */
6019 /* We must take special care when there are two or more reloads to
6020 be merged and a RELOAD_FOR_OUTPUT_ADDRESS reload that loads the
6021 same value or a part of it; we must not change its type if there
6022 is a conflicting input. */
6024 if (rld[i].when_needed == RELOAD_OTHER)
6025 for (j = 0; j < n_reloads; j++)
6026 if (rld[j].in != 0
6027 && rld[j].when_needed != RELOAD_OTHER
6028 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6029 && (! conflicting_input
6030 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6031 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6032 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6033 rld[i].in))
6035 int k;
6037 rld[j].when_needed
6038 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6039 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6040 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6042 /* Check to see if we accidentally converted two reloads
6043 that use the same reload register with different inputs
6044 to the same type. If so, the resulting code won't work,
6045 so abort. */
6046 if (rld[j].reg_rtx)
6047 for (k = 0; k < j; k++)
6048 if (rld[k].in != 0 && rld[k].reg_rtx != 0
6049 && rld[k].when_needed == rld[j].when_needed
6050 && rtx_equal_p (rld[k].reg_rtx, rld[j].reg_rtx)
6051 && ! rtx_equal_p (rld[k].in, rld[j].in))
6052 abort ();
6058 /* These arrays are filled by emit_reload_insns and its subroutines. */
6059 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6060 static rtx other_input_address_reload_insns = 0;
6061 static rtx other_input_reload_insns = 0;
6062 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6063 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6064 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6065 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6066 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6067 static rtx operand_reload_insns = 0;
6068 static rtx other_operand_reload_insns = 0;
6069 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6071 /* Values to be put in spill_reg_store are put here first. */
6072 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6073 static HARD_REG_SET reg_reloaded_died;
6075 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6076 has the number J. OLD contains the value to be used as input. */
6078 static void
6079 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6080 rtx old, int j)
6082 rtx insn = chain->insn;
6083 rtx reloadreg = rl->reg_rtx;
6084 rtx oldequiv_reg = 0;
6085 rtx oldequiv = 0;
6086 int special = 0;
6087 enum machine_mode mode;
6088 rtx *where;
6090 /* Determine the mode to reload in.
6091 This is very tricky because we have three to choose from.
6092 There is the mode the insn operand wants (rl->inmode).
6093 There is the mode of the reload register RELOADREG.
6094 There is the intrinsic mode of the operand, which we could find
6095 by stripping some SUBREGs.
6096 It turns out that RELOADREG's mode is irrelevant:
6097 we can change that arbitrarily.
6099 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6100 then the reload reg may not support QImode moves, so use SImode.
6101 If foo is in memory due to spilling a pseudo reg, this is safe,
6102 because the QImode value is in the least significant part of a
6103 slot big enough for a SImode. If foo is some other sort of
6104 memory reference, then it is impossible to reload this case,
6105 so previous passes had better make sure this never happens.
6107 Then consider a one-word union which has SImode and one of its
6108 members is a float, being fetched as (SUBREG:SF union:SI).
6109 We must fetch that as SFmode because we could be loading into
6110 a float-only register. In this case OLD's mode is correct.
6112 Consider an immediate integer: it has VOIDmode. Here we need
6113 to get a mode from something else.
6115 In some cases, there is a fourth mode, the operand's
6116 containing mode. If the insn specifies a containing mode for
6117 this operand, it overrides all others.
6119 I am not sure whether the algorithm here is always right,
6120 but it does the right things in those cases. */
6122 mode = GET_MODE (old);
6123 if (mode == VOIDmode)
6124 mode = rl->inmode;
6126 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6127 /* If we need a secondary register for this operation, see if
6128 the value is already in a register in that class. Don't
6129 do this if the secondary register will be used as a scratch
6130 register. */
6132 if (rl->secondary_in_reload >= 0
6133 && rl->secondary_in_icode == CODE_FOR_nothing
6134 && optimize)
6135 oldequiv
6136 = find_equiv_reg (old, insn,
6137 rld[rl->secondary_in_reload].class,
6138 -1, NULL, 0, mode);
6139 #endif
6141 /* If reloading from memory, see if there is a register
6142 that already holds the same value. If so, reload from there.
6143 We can pass 0 as the reload_reg_p argument because
6144 any other reload has either already been emitted,
6145 in which case find_equiv_reg will see the reload-insn,
6146 or has yet to be emitted, in which case it doesn't matter
6147 because we will use this equiv reg right away. */
6149 if (oldequiv == 0 && optimize
6150 && (GET_CODE (old) == MEM
6151 || (GET_CODE (old) == REG
6152 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6153 && reg_renumber[REGNO (old)] < 0)))
6154 oldequiv = find_equiv_reg (old, insn, ALL_REGS, -1, NULL, 0, mode);
6156 if (oldequiv)
6158 unsigned int regno = true_regnum (oldequiv);
6160 /* Don't use OLDEQUIV if any other reload changes it at an
6161 earlier stage of this insn or at this stage. */
6162 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6163 rl->in, const0_rtx, j, 0))
6164 oldequiv = 0;
6166 /* If it is no cheaper to copy from OLDEQUIV into the
6167 reload register than it would be to move from memory,
6168 don't use it. Likewise, if we need a secondary register
6169 or memory. */
6171 if (oldequiv != 0
6172 && (((enum reg_class) REGNO_REG_CLASS (regno) != rl->class
6173 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6174 rl->class)
6175 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6176 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6177 || (SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6178 mode, oldequiv)
6179 != NO_REGS)
6180 #endif
6181 #ifdef SECONDARY_MEMORY_NEEDED
6182 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6183 rl->class,
6184 mode)
6185 #endif
6187 oldequiv = 0;
6190 /* delete_output_reload is only invoked properly if old contains
6191 the original pseudo register. Since this is replaced with a
6192 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6193 find the pseudo in RELOAD_IN_REG. */
6194 if (oldequiv == 0
6195 && reload_override_in[j]
6196 && GET_CODE (rl->in_reg) == REG)
6198 oldequiv = old;
6199 old = rl->in_reg;
6201 if (oldequiv == 0)
6202 oldequiv = old;
6203 else if (GET_CODE (oldequiv) == REG)
6204 oldequiv_reg = oldequiv;
6205 else if (GET_CODE (oldequiv) == SUBREG)
6206 oldequiv_reg = SUBREG_REG (oldequiv);
6208 /* If we are reloading from a register that was recently stored in
6209 with an output-reload, see if we can prove there was
6210 actually no need to store the old value in it. */
6212 if (optimize && GET_CODE (oldequiv) == REG
6213 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6214 && spill_reg_store[REGNO (oldequiv)]
6215 && GET_CODE (old) == REG
6216 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6217 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6218 rl->out_reg)))
6219 delete_output_reload (insn, j, REGNO (oldequiv));
6221 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6222 then load RELOADREG from OLDEQUIV. Note that we cannot use
6223 gen_lowpart_common since it can do the wrong thing when
6224 RELOADREG has a multi-word mode. Note that RELOADREG
6225 must always be a REG here. */
6227 if (GET_MODE (reloadreg) != mode)
6228 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6229 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6230 oldequiv = SUBREG_REG (oldequiv);
6231 if (GET_MODE (oldequiv) != VOIDmode
6232 && mode != GET_MODE (oldequiv))
6233 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6235 /* Switch to the right place to emit the reload insns. */
6236 switch (rl->when_needed)
6238 case RELOAD_OTHER:
6239 where = &other_input_reload_insns;
6240 break;
6241 case RELOAD_FOR_INPUT:
6242 where = &input_reload_insns[rl->opnum];
6243 break;
6244 case RELOAD_FOR_INPUT_ADDRESS:
6245 where = &input_address_reload_insns[rl->opnum];
6246 break;
6247 case RELOAD_FOR_INPADDR_ADDRESS:
6248 where = &inpaddr_address_reload_insns[rl->opnum];
6249 break;
6250 case RELOAD_FOR_OUTPUT_ADDRESS:
6251 where = &output_address_reload_insns[rl->opnum];
6252 break;
6253 case RELOAD_FOR_OUTADDR_ADDRESS:
6254 where = &outaddr_address_reload_insns[rl->opnum];
6255 break;
6256 case RELOAD_FOR_OPERAND_ADDRESS:
6257 where = &operand_reload_insns;
6258 break;
6259 case RELOAD_FOR_OPADDR_ADDR:
6260 where = &other_operand_reload_insns;
6261 break;
6262 case RELOAD_FOR_OTHER_ADDRESS:
6263 where = &other_input_address_reload_insns;
6264 break;
6265 default:
6266 abort ();
6269 push_to_sequence (*where);
6271 /* Auto-increment addresses must be reloaded in a special way. */
6272 if (rl->out && ! rl->out_reg)
6274 /* We are not going to bother supporting the case where a
6275 incremented register can't be copied directly from
6276 OLDEQUIV since this seems highly unlikely. */
6277 if (rl->secondary_in_reload >= 0)
6278 abort ();
6280 if (reload_inherited[j])
6281 oldequiv = reloadreg;
6283 old = XEXP (rl->in_reg, 0);
6285 if (optimize && GET_CODE (oldequiv) == REG
6286 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6287 && spill_reg_store[REGNO (oldequiv)]
6288 && GET_CODE (old) == REG
6289 && (dead_or_set_p (insn,
6290 spill_reg_stored_to[REGNO (oldequiv)])
6291 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6292 old)))
6293 delete_output_reload (insn, j, REGNO (oldequiv));
6295 /* Prevent normal processing of this reload. */
6296 special = 1;
6297 /* Output a special code sequence for this case. */
6298 new_spill_reg_store[REGNO (reloadreg)]
6299 = inc_for_reload (reloadreg, oldequiv, rl->out,
6300 rl->inc);
6303 /* If we are reloading a pseudo-register that was set by the previous
6304 insn, see if we can get rid of that pseudo-register entirely
6305 by redirecting the previous insn into our reload register. */
6307 else if (optimize && GET_CODE (old) == REG
6308 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6309 && dead_or_set_p (insn, old)
6310 /* This is unsafe if some other reload
6311 uses the same reg first. */
6312 && ! conflicts_with_override (reloadreg)
6313 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6314 rl->when_needed, old, rl->out, j, 0))
6316 rtx temp = PREV_INSN (insn);
6317 while (temp && GET_CODE (temp) == NOTE)
6318 temp = PREV_INSN (temp);
6319 if (temp
6320 && GET_CODE (temp) == INSN
6321 && GET_CODE (PATTERN (temp)) == SET
6322 && SET_DEST (PATTERN (temp)) == old
6323 /* Make sure we can access insn_operand_constraint. */
6324 && asm_noperands (PATTERN (temp)) < 0
6325 /* This is unsafe if operand occurs more than once in current
6326 insn. Perhaps some occurrences aren't reloaded. */
6327 && count_occurrences (PATTERN (insn), old, 0) == 1)
6329 rtx old = SET_DEST (PATTERN (temp));
6330 /* Store into the reload register instead of the pseudo. */
6331 SET_DEST (PATTERN (temp)) = reloadreg;
6333 /* Verify that resulting insn is valid. */
6334 extract_insn (temp);
6335 if (constrain_operands (1))
6337 /* If the previous insn is an output reload, the source is
6338 a reload register, and its spill_reg_store entry will
6339 contain the previous destination. This is now
6340 invalid. */
6341 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6342 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6344 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6345 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6348 /* If these are the only uses of the pseudo reg,
6349 pretend for GDB it lives in the reload reg we used. */
6350 if (REG_N_DEATHS (REGNO (old)) == 1
6351 && REG_N_SETS (REGNO (old)) == 1)
6353 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6354 alter_reg (REGNO (old), -1);
6356 special = 1;
6358 else
6360 SET_DEST (PATTERN (temp)) = old;
6365 /* We can't do that, so output an insn to load RELOADREG. */
6367 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6368 /* If we have a secondary reload, pick up the secondary register
6369 and icode, if any. If OLDEQUIV and OLD are different or
6370 if this is an in-out reload, recompute whether or not we
6371 still need a secondary register and what the icode should
6372 be. If we still need a secondary register and the class or
6373 icode is different, go back to reloading from OLD if using
6374 OLDEQUIV means that we got the wrong type of register. We
6375 cannot have different class or icode due to an in-out reload
6376 because we don't make such reloads when both the input and
6377 output need secondary reload registers. */
6379 if (! special && rl->secondary_in_reload >= 0)
6381 rtx second_reload_reg = 0;
6382 int secondary_reload = rl->secondary_in_reload;
6383 rtx real_oldequiv = oldequiv;
6384 rtx real_old = old;
6385 rtx tmp;
6386 enum insn_code icode;
6388 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6389 and similarly for OLD.
6390 See comments in get_secondary_reload in reload.c. */
6391 /* If it is a pseudo that cannot be replaced with its
6392 equivalent MEM, we must fall back to reload_in, which
6393 will have all the necessary substitutions registered.
6394 Likewise for a pseudo that can't be replaced with its
6395 equivalent constant.
6397 Take extra care for subregs of such pseudos. Note that
6398 we cannot use reg_equiv_mem in this case because it is
6399 not in the right mode. */
6401 tmp = oldequiv;
6402 if (GET_CODE (tmp) == SUBREG)
6403 tmp = SUBREG_REG (tmp);
6404 if (GET_CODE (tmp) == REG
6405 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6406 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6407 || reg_equiv_constant[REGNO (tmp)] != 0))
6409 if (! reg_equiv_mem[REGNO (tmp)]
6410 || num_not_at_initial_offset
6411 || GET_CODE (oldequiv) == SUBREG)
6412 real_oldequiv = rl->in;
6413 else
6414 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6417 tmp = old;
6418 if (GET_CODE (tmp) == SUBREG)
6419 tmp = SUBREG_REG (tmp);
6420 if (GET_CODE (tmp) == REG
6421 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6422 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6423 || reg_equiv_constant[REGNO (tmp)] != 0))
6425 if (! reg_equiv_mem[REGNO (tmp)]
6426 || num_not_at_initial_offset
6427 || GET_CODE (old) == SUBREG)
6428 real_old = rl->in;
6429 else
6430 real_old = reg_equiv_mem[REGNO (tmp)];
6433 second_reload_reg = rld[secondary_reload].reg_rtx;
6434 icode = rl->secondary_in_icode;
6436 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6437 || (rl->in != 0 && rl->out != 0))
6439 enum reg_class new_class
6440 = SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6441 mode, real_oldequiv);
6443 if (new_class == NO_REGS)
6444 second_reload_reg = 0;
6445 else
6447 enum insn_code new_icode;
6448 enum machine_mode new_mode;
6450 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6451 REGNO (second_reload_reg)))
6452 oldequiv = old, real_oldequiv = real_old;
6453 else
6455 new_icode = reload_in_optab[(int) mode];
6456 if (new_icode != CODE_FOR_nothing
6457 && ((insn_data[(int) new_icode].operand[0].predicate
6458 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6459 (reloadreg, mode)))
6460 || (insn_data[(int) new_icode].operand[1].predicate
6461 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6462 (real_oldequiv, mode)))))
6463 new_icode = CODE_FOR_nothing;
6465 if (new_icode == CODE_FOR_nothing)
6466 new_mode = mode;
6467 else
6468 new_mode = insn_data[(int) new_icode].operand[2].mode;
6470 if (GET_MODE (second_reload_reg) != new_mode)
6472 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6473 new_mode))
6474 oldequiv = old, real_oldequiv = real_old;
6475 else
6476 second_reload_reg
6477 = reload_adjust_reg_for_mode (second_reload_reg,
6478 new_mode);
6484 /* If we still need a secondary reload register, check
6485 to see if it is being used as a scratch or intermediate
6486 register and generate code appropriately. If we need
6487 a scratch register, use REAL_OLDEQUIV since the form of
6488 the insn may depend on the actual address if it is
6489 a MEM. */
6491 if (second_reload_reg)
6493 if (icode != CODE_FOR_nothing)
6495 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6496 second_reload_reg));
6497 special = 1;
6499 else
6501 /* See if we need a scratch register to load the
6502 intermediate register (a tertiary reload). */
6503 enum insn_code tertiary_icode
6504 = rld[secondary_reload].secondary_in_icode;
6506 if (tertiary_icode != CODE_FOR_nothing)
6508 rtx third_reload_reg
6509 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
6511 emit_insn ((GEN_FCN (tertiary_icode)
6512 (second_reload_reg, real_oldequiv,
6513 third_reload_reg)));
6515 else
6516 gen_reload (second_reload_reg, real_oldequiv,
6517 rl->opnum,
6518 rl->when_needed);
6520 oldequiv = second_reload_reg;
6524 #endif
6526 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6528 rtx real_oldequiv = oldequiv;
6530 if ((GET_CODE (oldequiv) == REG
6531 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6532 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6533 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6534 || (GET_CODE (oldequiv) == SUBREG
6535 && GET_CODE (SUBREG_REG (oldequiv)) == REG
6536 && (REGNO (SUBREG_REG (oldequiv))
6537 >= FIRST_PSEUDO_REGISTER)
6538 && ((reg_equiv_memory_loc
6539 [REGNO (SUBREG_REG (oldequiv))] != 0)
6540 || (reg_equiv_constant
6541 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6542 || (CONSTANT_P (oldequiv)
6543 && (PREFERRED_RELOAD_CLASS (oldequiv,
6544 REGNO_REG_CLASS (REGNO (reloadreg)))
6545 == NO_REGS)))
6546 real_oldequiv = rl->in;
6547 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6548 rl->when_needed);
6551 if (flag_non_call_exceptions)
6552 copy_eh_notes (insn, get_insns ());
6554 /* End this sequence. */
6555 *where = get_insns ();
6556 end_sequence ();
6558 /* Update reload_override_in so that delete_address_reloads_1
6559 can see the actual register usage. */
6560 if (oldequiv_reg)
6561 reload_override_in[j] = oldequiv;
6564 /* Generate insns to for the output reload RL, which is for the insn described
6565 by CHAIN and has the number J. */
6566 static void
6567 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
6568 int j)
6570 rtx reloadreg = rl->reg_rtx;
6571 rtx insn = chain->insn;
6572 int special = 0;
6573 rtx old = rl->out;
6574 enum machine_mode mode = GET_MODE (old);
6575 rtx p;
6577 if (rl->when_needed == RELOAD_OTHER)
6578 start_sequence ();
6579 else
6580 push_to_sequence (output_reload_insns[rl->opnum]);
6582 /* Determine the mode to reload in.
6583 See comments above (for input reloading). */
6585 if (mode == VOIDmode)
6587 /* VOIDmode should never happen for an output. */
6588 if (asm_noperands (PATTERN (insn)) < 0)
6589 /* It's the compiler's fault. */
6590 fatal_insn ("VOIDmode on an output", insn);
6591 error_for_asm (insn, "output operand is constant in `asm'");
6592 /* Prevent crash--use something we know is valid. */
6593 mode = word_mode;
6594 old = gen_rtx_REG (mode, REGNO (reloadreg));
6597 if (GET_MODE (reloadreg) != mode)
6598 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6600 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6602 /* If we need two reload regs, set RELOADREG to the intermediate
6603 one, since it will be stored into OLD. We might need a secondary
6604 register only for an input reload, so check again here. */
6606 if (rl->secondary_out_reload >= 0)
6608 rtx real_old = old;
6610 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6611 && reg_equiv_mem[REGNO (old)] != 0)
6612 real_old = reg_equiv_mem[REGNO (old)];
6614 if ((SECONDARY_OUTPUT_RELOAD_CLASS (rl->class,
6615 mode, real_old)
6616 != NO_REGS))
6618 rtx second_reloadreg = reloadreg;
6619 reloadreg = rld[rl->secondary_out_reload].reg_rtx;
6621 /* See if RELOADREG is to be used as a scratch register
6622 or as an intermediate register. */
6623 if (rl->secondary_out_icode != CODE_FOR_nothing)
6625 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6626 (real_old, second_reloadreg, reloadreg)));
6627 special = 1;
6629 else
6631 /* See if we need both a scratch and intermediate reload
6632 register. */
6634 int secondary_reload = rl->secondary_out_reload;
6635 enum insn_code tertiary_icode
6636 = rld[secondary_reload].secondary_out_icode;
6638 if (GET_MODE (reloadreg) != mode)
6639 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6641 if (tertiary_icode != CODE_FOR_nothing)
6643 rtx third_reloadreg
6644 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
6645 rtx tem;
6647 /* Copy primary reload reg to secondary reload reg.
6648 (Note that these have been swapped above, then
6649 secondary reload reg to OLD using our insn.) */
6651 /* If REAL_OLD is a paradoxical SUBREG, remove it
6652 and try to put the opposite SUBREG on
6653 RELOADREG. */
6654 if (GET_CODE (real_old) == SUBREG
6655 && (GET_MODE_SIZE (GET_MODE (real_old))
6656 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6657 && 0 != (tem = gen_lowpart_common
6658 (GET_MODE (SUBREG_REG (real_old)),
6659 reloadreg)))
6660 real_old = SUBREG_REG (real_old), reloadreg = tem;
6662 gen_reload (reloadreg, second_reloadreg,
6663 rl->opnum, rl->when_needed);
6664 emit_insn ((GEN_FCN (tertiary_icode)
6665 (real_old, reloadreg, third_reloadreg)));
6666 special = 1;
6669 else
6670 /* Copy between the reload regs here and then to
6671 OUT later. */
6673 gen_reload (reloadreg, second_reloadreg,
6674 rl->opnum, rl->when_needed);
6678 #endif
6680 /* Output the last reload insn. */
6681 if (! special)
6683 rtx set;
6685 /* Don't output the last reload if OLD is not the dest of
6686 INSN and is in the src and is clobbered by INSN. */
6687 if (! flag_expensive_optimizations
6688 || GET_CODE (old) != REG
6689 || !(set = single_set (insn))
6690 || rtx_equal_p (old, SET_DEST (set))
6691 || !reg_mentioned_p (old, SET_SRC (set))
6692 || !regno_clobbered_p (REGNO (old), insn, rl->mode, 0))
6693 gen_reload (old, reloadreg, rl->opnum,
6694 rl->when_needed);
6697 /* Look at all insns we emitted, just to be safe. */
6698 for (p = get_insns (); p; p = NEXT_INSN (p))
6699 if (INSN_P (p))
6701 rtx pat = PATTERN (p);
6703 /* If this output reload doesn't come from a spill reg,
6704 clear any memory of reloaded copies of the pseudo reg.
6705 If this output reload comes from a spill reg,
6706 reg_has_output_reload will make this do nothing. */
6707 note_stores (pat, forget_old_reloads_1, NULL);
6709 if (reg_mentioned_p (rl->reg_rtx, pat))
6711 rtx set = single_set (insn);
6712 if (reload_spill_index[j] < 0
6713 && set
6714 && SET_SRC (set) == rl->reg_rtx)
6716 int src = REGNO (SET_SRC (set));
6718 reload_spill_index[j] = src;
6719 SET_HARD_REG_BIT (reg_is_output_reload, src);
6720 if (find_regno_note (insn, REG_DEAD, src))
6721 SET_HARD_REG_BIT (reg_reloaded_died, src);
6723 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6725 int s = rl->secondary_out_reload;
6726 set = single_set (p);
6727 /* If this reload copies only to the secondary reload
6728 register, the secondary reload does the actual
6729 store. */
6730 if (s >= 0 && set == NULL_RTX)
6731 /* We can't tell what function the secondary reload
6732 has and where the actual store to the pseudo is
6733 made; leave new_spill_reg_store alone. */
6735 else if (s >= 0
6736 && SET_SRC (set) == rl->reg_rtx
6737 && SET_DEST (set) == rld[s].reg_rtx)
6739 /* Usually the next instruction will be the
6740 secondary reload insn; if we can confirm
6741 that it is, setting new_spill_reg_store to
6742 that insn will allow an extra optimization. */
6743 rtx s_reg = rld[s].reg_rtx;
6744 rtx next = NEXT_INSN (p);
6745 rld[s].out = rl->out;
6746 rld[s].out_reg = rl->out_reg;
6747 set = single_set (next);
6748 if (set && SET_SRC (set) == s_reg
6749 && ! new_spill_reg_store[REGNO (s_reg)])
6751 SET_HARD_REG_BIT (reg_is_output_reload,
6752 REGNO (s_reg));
6753 new_spill_reg_store[REGNO (s_reg)] = next;
6756 else
6757 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6762 if (rl->when_needed == RELOAD_OTHER)
6764 emit_insn (other_output_reload_insns[rl->opnum]);
6765 other_output_reload_insns[rl->opnum] = get_insns ();
6767 else
6768 output_reload_insns[rl->opnum] = get_insns ();
6770 if (flag_non_call_exceptions)
6771 copy_eh_notes (insn, get_insns ());
6773 end_sequence ();
6776 /* Do input reloading for reload RL, which is for the insn described by CHAIN
6777 and has the number J. */
6778 static void
6779 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
6781 rtx insn = chain->insn;
6782 rtx old = (rl->in && GET_CODE (rl->in) == MEM
6783 ? rl->in_reg : rl->in);
6785 if (old != 0
6786 /* AUTO_INC reloads need to be handled even if inherited. We got an
6787 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6788 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
6789 && ! rtx_equal_p (rl->reg_rtx, old)
6790 && rl->reg_rtx != 0)
6791 emit_input_reload_insns (chain, rld + j, old, j);
6793 /* When inheriting a wider reload, we have a MEM in rl->in,
6794 e.g. inheriting a SImode output reload for
6795 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6796 if (optimize && reload_inherited[j] && rl->in
6797 && GET_CODE (rl->in) == MEM
6798 && GET_CODE (rl->in_reg) == MEM
6799 && reload_spill_index[j] >= 0
6800 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6801 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
6803 /* If we are reloading a register that was recently stored in with an
6804 output-reload, see if we can prove there was
6805 actually no need to store the old value in it. */
6807 if (optimize
6808 && (reload_inherited[j] || reload_override_in[j])
6809 && rl->reg_rtx
6810 && GET_CODE (rl->reg_rtx) == REG
6811 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
6812 #if 0
6813 /* There doesn't seem to be any reason to restrict this to pseudos
6814 and doing so loses in the case where we are copying from a
6815 register of the wrong class. */
6816 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
6817 >= FIRST_PSEUDO_REGISTER)
6818 #endif
6819 /* The insn might have already some references to stackslots
6820 replaced by MEMs, while reload_out_reg still names the
6821 original pseudo. */
6822 && (dead_or_set_p (insn,
6823 spill_reg_stored_to[REGNO (rl->reg_rtx)])
6824 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
6825 rl->out_reg)))
6826 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
6829 /* Do output reloading for reload RL, which is for the insn described by
6830 CHAIN and has the number J.
6831 ??? At some point we need to support handling output reloads of
6832 JUMP_INSNs or insns that set cc0. */
6833 static void
6834 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
6836 rtx note, old;
6837 rtx insn = chain->insn;
6838 /* If this is an output reload that stores something that is
6839 not loaded in this same reload, see if we can eliminate a previous
6840 store. */
6841 rtx pseudo = rl->out_reg;
6843 if (pseudo
6844 && optimize
6845 && GET_CODE (pseudo) == REG
6846 && ! rtx_equal_p (rl->in_reg, pseudo)
6847 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
6848 && reg_last_reload_reg[REGNO (pseudo)])
6850 int pseudo_no = REGNO (pseudo);
6851 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
6853 /* We don't need to test full validity of last_regno for
6854 inherit here; we only want to know if the store actually
6855 matches the pseudo. */
6856 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
6857 && reg_reloaded_contents[last_regno] == pseudo_no
6858 && spill_reg_store[last_regno]
6859 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
6860 delete_output_reload (insn, j, last_regno);
6863 old = rl->out_reg;
6864 if (old == 0
6865 || rl->reg_rtx == old
6866 || rl->reg_rtx == 0)
6867 return;
6869 /* An output operand that dies right away does need a reload,
6870 but need not be copied from it. Show the new location in the
6871 REG_UNUSED note. */
6872 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6873 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6875 XEXP (note, 0) = rl->reg_rtx;
6876 return;
6878 /* Likewise for a SUBREG of an operand that dies. */
6879 else if (GET_CODE (old) == SUBREG
6880 && GET_CODE (SUBREG_REG (old)) == REG
6881 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6882 SUBREG_REG (old))))
6884 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6885 rl->reg_rtx);
6886 return;
6888 else if (GET_CODE (old) == SCRATCH)
6889 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6890 but we don't want to make an output reload. */
6891 return;
6893 /* If is a JUMP_INSN, we can't support output reloads yet. */
6894 if (GET_CODE (insn) == JUMP_INSN)
6895 abort ();
6897 emit_output_reload_insns (chain, rld + j, j);
6900 /* Output insns to reload values in and out of the chosen reload regs. */
6902 static void
6903 emit_reload_insns (struct insn_chain *chain)
6905 rtx insn = chain->insn;
6907 int j;
6909 CLEAR_HARD_REG_SET (reg_reloaded_died);
6911 for (j = 0; j < reload_n_operands; j++)
6912 input_reload_insns[j] = input_address_reload_insns[j]
6913 = inpaddr_address_reload_insns[j]
6914 = output_reload_insns[j] = output_address_reload_insns[j]
6915 = outaddr_address_reload_insns[j]
6916 = other_output_reload_insns[j] = 0;
6917 other_input_address_reload_insns = 0;
6918 other_input_reload_insns = 0;
6919 operand_reload_insns = 0;
6920 other_operand_reload_insns = 0;
6922 /* Dump reloads into the dump file. */
6923 if (rtl_dump_file)
6925 fprintf (rtl_dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
6926 debug_reload_to_stream (rtl_dump_file);
6929 /* Now output the instructions to copy the data into and out of the
6930 reload registers. Do these in the order that the reloads were reported,
6931 since reloads of base and index registers precede reloads of operands
6932 and the operands may need the base and index registers reloaded. */
6934 for (j = 0; j < n_reloads; j++)
6936 if (rld[j].reg_rtx
6937 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6938 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6940 do_input_reload (chain, rld + j, j);
6941 do_output_reload (chain, rld + j, j);
6944 /* Now write all the insns we made for reloads in the order expected by
6945 the allocation functions. Prior to the insn being reloaded, we write
6946 the following reloads:
6948 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
6950 RELOAD_OTHER reloads.
6952 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
6953 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
6954 RELOAD_FOR_INPUT reload for the operand.
6956 RELOAD_FOR_OPADDR_ADDRS reloads.
6958 RELOAD_FOR_OPERAND_ADDRESS reloads.
6960 After the insn being reloaded, we write the following:
6962 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
6963 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
6964 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
6965 reloads for the operand. The RELOAD_OTHER output reloads are
6966 output in descending order by reload number. */
6968 emit_insn_before (other_input_address_reload_insns, insn);
6969 emit_insn_before (other_input_reload_insns, insn);
6971 for (j = 0; j < reload_n_operands; j++)
6973 emit_insn_before (inpaddr_address_reload_insns[j], insn);
6974 emit_insn_before (input_address_reload_insns[j], insn);
6975 emit_insn_before (input_reload_insns[j], insn);
6978 emit_insn_before (other_operand_reload_insns, insn);
6979 emit_insn_before (operand_reload_insns, insn);
6981 for (j = 0; j < reload_n_operands; j++)
6983 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
6984 x = emit_insn_after (output_address_reload_insns[j], x);
6985 x = emit_insn_after (output_reload_insns[j], x);
6986 emit_insn_after (other_output_reload_insns[j], x);
6989 /* For all the spill regs newly reloaded in this instruction,
6990 record what they were reloaded from, so subsequent instructions
6991 can inherit the reloads.
6993 Update spill_reg_store for the reloads of this insn.
6994 Copy the elements that were updated in the loop above. */
6996 for (j = 0; j < n_reloads; j++)
6998 int r = reload_order[j];
6999 int i = reload_spill_index[r];
7001 /* If this is a non-inherited input reload from a pseudo, we must
7002 clear any memory of a previous store to the same pseudo. Only do
7003 something if there will not be an output reload for the pseudo
7004 being reloaded. */
7005 if (rld[r].in_reg != 0
7006 && ! (reload_inherited[r] || reload_override_in[r]))
7008 rtx reg = rld[r].in_reg;
7010 if (GET_CODE (reg) == SUBREG)
7011 reg = SUBREG_REG (reg);
7013 if (GET_CODE (reg) == REG
7014 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7015 && ! reg_has_output_reload[REGNO (reg)])
7017 int nregno = REGNO (reg);
7019 if (reg_last_reload_reg[nregno])
7021 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7023 if (reg_reloaded_contents[last_regno] == nregno)
7024 spill_reg_store[last_regno] = 0;
7029 /* I is nonneg if this reload used a register.
7030 If rld[r].reg_rtx is 0, this is an optional reload
7031 that we opted to ignore. */
7033 if (i >= 0 && rld[r].reg_rtx != 0)
7035 int nr = HARD_REGNO_NREGS (i, GET_MODE (rld[r].reg_rtx));
7036 int k;
7037 int part_reaches_end = 0;
7038 int all_reaches_end = 1;
7040 /* For a multi register reload, we need to check if all or part
7041 of the value lives to the end. */
7042 for (k = 0; k < nr; k++)
7044 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7045 rld[r].when_needed))
7046 part_reaches_end = 1;
7047 else
7048 all_reaches_end = 0;
7051 /* Ignore reloads that don't reach the end of the insn in
7052 entirety. */
7053 if (all_reaches_end)
7055 /* First, clear out memory of what used to be in this spill reg.
7056 If consecutive registers are used, clear them all. */
7058 for (k = 0; k < nr; k++)
7059 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7061 /* Maybe the spill reg contains a copy of reload_out. */
7062 if (rld[r].out != 0
7063 && (GET_CODE (rld[r].out) == REG
7064 #ifdef AUTO_INC_DEC
7065 || ! rld[r].out_reg
7066 #endif
7067 || GET_CODE (rld[r].out_reg) == REG))
7069 rtx out = (GET_CODE (rld[r].out) == REG
7070 ? rld[r].out
7071 : rld[r].out_reg
7072 ? rld[r].out_reg
7073 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7074 int nregno = REGNO (out);
7075 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7076 : HARD_REGNO_NREGS (nregno,
7077 GET_MODE (rld[r].reg_rtx)));
7079 spill_reg_store[i] = new_spill_reg_store[i];
7080 spill_reg_stored_to[i] = out;
7081 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7083 /* If NREGNO is a hard register, it may occupy more than
7084 one register. If it does, say what is in the
7085 rest of the registers assuming that both registers
7086 agree on how many words the object takes. If not,
7087 invalidate the subsequent registers. */
7089 if (nregno < FIRST_PSEUDO_REGISTER)
7090 for (k = 1; k < nnr; k++)
7091 reg_last_reload_reg[nregno + k]
7092 = (nr == nnr
7093 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7094 : 0);
7096 /* Now do the inverse operation. */
7097 for (k = 0; k < nr; k++)
7099 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7100 reg_reloaded_contents[i + k]
7101 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7102 ? nregno
7103 : nregno + k);
7104 reg_reloaded_insn[i + k] = insn;
7105 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7109 /* Maybe the spill reg contains a copy of reload_in. Only do
7110 something if there will not be an output reload for
7111 the register being reloaded. */
7112 else if (rld[r].out_reg == 0
7113 && rld[r].in != 0
7114 && ((GET_CODE (rld[r].in) == REG
7115 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7116 && ! reg_has_output_reload[REGNO (rld[r].in)])
7117 || (GET_CODE (rld[r].in_reg) == REG
7118 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7119 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7121 int nregno;
7122 int nnr;
7124 if (GET_CODE (rld[r].in) == REG
7125 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7126 nregno = REGNO (rld[r].in);
7127 else if (GET_CODE (rld[r].in_reg) == REG)
7128 nregno = REGNO (rld[r].in_reg);
7129 else
7130 nregno = REGNO (XEXP (rld[r].in_reg, 0));
7132 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7133 : HARD_REGNO_NREGS (nregno,
7134 GET_MODE (rld[r].reg_rtx)));
7136 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7138 if (nregno < FIRST_PSEUDO_REGISTER)
7139 for (k = 1; k < nnr; k++)
7140 reg_last_reload_reg[nregno + k]
7141 = (nr == nnr
7142 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7143 : 0);
7145 /* Unless we inherited this reload, show we haven't
7146 recently done a store.
7147 Previous stores of inherited auto_inc expressions
7148 also have to be discarded. */
7149 if (! reload_inherited[r]
7150 || (rld[r].out && ! rld[r].out_reg))
7151 spill_reg_store[i] = 0;
7153 for (k = 0; k < nr; k++)
7155 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7156 reg_reloaded_contents[i + k]
7157 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7158 ? nregno
7159 : nregno + k);
7160 reg_reloaded_insn[i + k] = insn;
7161 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7166 /* However, if part of the reload reaches the end, then we must
7167 invalidate the old info for the part that survives to the end. */
7168 else if (part_reaches_end)
7170 for (k = 0; k < nr; k++)
7171 if (reload_reg_reaches_end_p (i + k,
7172 rld[r].opnum,
7173 rld[r].when_needed))
7174 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7178 /* The following if-statement was #if 0'd in 1.34 (or before...).
7179 It's reenabled in 1.35 because supposedly nothing else
7180 deals with this problem. */
7182 /* If a register gets output-reloaded from a non-spill register,
7183 that invalidates any previous reloaded copy of it.
7184 But forget_old_reloads_1 won't get to see it, because
7185 it thinks only about the original insn. So invalidate it here. */
7186 if (i < 0 && rld[r].out != 0
7187 && (GET_CODE (rld[r].out) == REG
7188 || (GET_CODE (rld[r].out) == MEM
7189 && GET_CODE (rld[r].out_reg) == REG)))
7191 rtx out = (GET_CODE (rld[r].out) == REG
7192 ? rld[r].out : rld[r].out_reg);
7193 int nregno = REGNO (out);
7194 if (nregno >= FIRST_PSEUDO_REGISTER)
7196 rtx src_reg, store_insn = NULL_RTX;
7198 reg_last_reload_reg[nregno] = 0;
7200 /* If we can find a hard register that is stored, record
7201 the storing insn so that we may delete this insn with
7202 delete_output_reload. */
7203 src_reg = rld[r].reg_rtx;
7205 /* If this is an optional reload, try to find the source reg
7206 from an input reload. */
7207 if (! src_reg)
7209 rtx set = single_set (insn);
7210 if (set && SET_DEST (set) == rld[r].out)
7212 int k;
7214 src_reg = SET_SRC (set);
7215 store_insn = insn;
7216 for (k = 0; k < n_reloads; k++)
7218 if (rld[k].in == src_reg)
7220 src_reg = rld[k].reg_rtx;
7221 break;
7226 else
7227 store_insn = new_spill_reg_store[REGNO (src_reg)];
7228 if (src_reg && GET_CODE (src_reg) == REG
7229 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7231 int src_regno = REGNO (src_reg);
7232 int nr = HARD_REGNO_NREGS (src_regno, rld[r].mode);
7233 /* The place where to find a death note varies with
7234 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7235 necessarily checked exactly in the code that moves
7236 notes, so just check both locations. */
7237 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7238 if (! note && store_insn)
7239 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7240 while (nr-- > 0)
7242 spill_reg_store[src_regno + nr] = store_insn;
7243 spill_reg_stored_to[src_regno + nr] = out;
7244 reg_reloaded_contents[src_regno + nr] = nregno;
7245 reg_reloaded_insn[src_regno + nr] = store_insn;
7246 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7247 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7248 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7249 if (note)
7250 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7251 else
7252 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7254 reg_last_reload_reg[nregno] = src_reg;
7257 else
7259 int num_regs = HARD_REGNO_NREGS (nregno, GET_MODE (rld[r].out));
7261 while (num_regs-- > 0)
7262 reg_last_reload_reg[nregno + num_regs] = 0;
7266 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7269 /* Emit code to perform a reload from IN (which may be a reload register) to
7270 OUT (which may also be a reload register). IN or OUT is from operand
7271 OPNUM with reload type TYPE.
7273 Returns first insn emitted. */
7276 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7278 rtx last = get_last_insn ();
7279 rtx tem;
7281 /* If IN is a paradoxical SUBREG, remove it and try to put the
7282 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7283 if (GET_CODE (in) == SUBREG
7284 && (GET_MODE_SIZE (GET_MODE (in))
7285 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7286 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7287 in = SUBREG_REG (in), out = tem;
7288 else if (GET_CODE (out) == SUBREG
7289 && (GET_MODE_SIZE (GET_MODE (out))
7290 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7291 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7292 out = SUBREG_REG (out), in = tem;
7294 /* How to do this reload can get quite tricky. Normally, we are being
7295 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7296 register that didn't get a hard register. In that case we can just
7297 call emit_move_insn.
7299 We can also be asked to reload a PLUS that adds a register or a MEM to
7300 another register, constant or MEM. This can occur during frame pointer
7301 elimination and while reloading addresses. This case is handled by
7302 trying to emit a single insn to perform the add. If it is not valid,
7303 we use a two insn sequence.
7305 Finally, we could be called to handle an 'o' constraint by putting
7306 an address into a register. In that case, we first try to do this
7307 with a named pattern of "reload_load_address". If no such pattern
7308 exists, we just emit a SET insn and hope for the best (it will normally
7309 be valid on machines that use 'o').
7311 This entire process is made complex because reload will never
7312 process the insns we generate here and so we must ensure that
7313 they will fit their constraints and also by the fact that parts of
7314 IN might be being reloaded separately and replaced with spill registers.
7315 Because of this, we are, in some sense, just guessing the right approach
7316 here. The one listed above seems to work.
7318 ??? At some point, this whole thing needs to be rethought. */
7320 if (GET_CODE (in) == PLUS
7321 && (GET_CODE (XEXP (in, 0)) == REG
7322 || GET_CODE (XEXP (in, 0)) == SUBREG
7323 || GET_CODE (XEXP (in, 0)) == MEM)
7324 && (GET_CODE (XEXP (in, 1)) == REG
7325 || GET_CODE (XEXP (in, 1)) == SUBREG
7326 || CONSTANT_P (XEXP (in, 1))
7327 || GET_CODE (XEXP (in, 1)) == MEM))
7329 /* We need to compute the sum of a register or a MEM and another
7330 register, constant, or MEM, and put it into the reload
7331 register. The best possible way of doing this is if the machine
7332 has a three-operand ADD insn that accepts the required operands.
7334 The simplest approach is to try to generate such an insn and see if it
7335 is recognized and matches its constraints. If so, it can be used.
7337 It might be better not to actually emit the insn unless it is valid,
7338 but we need to pass the insn as an operand to `recog' and
7339 `extract_insn' and it is simpler to emit and then delete the insn if
7340 not valid than to dummy things up. */
7342 rtx op0, op1, tem, insn;
7343 int code;
7345 op0 = find_replacement (&XEXP (in, 0));
7346 op1 = find_replacement (&XEXP (in, 1));
7348 /* Since constraint checking is strict, commutativity won't be
7349 checked, so we need to do that here to avoid spurious failure
7350 if the add instruction is two-address and the second operand
7351 of the add is the same as the reload reg, which is frequently
7352 the case. If the insn would be A = B + A, rearrange it so
7353 it will be A = A + B as constrain_operands expects. */
7355 if (GET_CODE (XEXP (in, 1)) == REG
7356 && REGNO (out) == REGNO (XEXP (in, 1)))
7357 tem = op0, op0 = op1, op1 = tem;
7359 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7360 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7362 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7363 code = recog_memoized (insn);
7365 if (code >= 0)
7367 extract_insn (insn);
7368 /* We want constrain operands to treat this insn strictly in
7369 its validity determination, i.e., the way it would after reload
7370 has completed. */
7371 if (constrain_operands (1))
7372 return insn;
7375 delete_insns_since (last);
7377 /* If that failed, we must use a conservative two-insn sequence.
7379 Use a move to copy one operand into the reload register. Prefer
7380 to reload a constant, MEM or pseudo since the move patterns can
7381 handle an arbitrary operand. If OP1 is not a constant, MEM or
7382 pseudo and OP1 is not a valid operand for an add instruction, then
7383 reload OP1.
7385 After reloading one of the operands into the reload register, add
7386 the reload register to the output register.
7388 If there is another way to do this for a specific machine, a
7389 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7390 we emit below. */
7392 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7394 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7395 || (GET_CODE (op1) == REG
7396 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7397 || (code != CODE_FOR_nothing
7398 && ! ((*insn_data[code].operand[2].predicate)
7399 (op1, insn_data[code].operand[2].mode))))
7400 tem = op0, op0 = op1, op1 = tem;
7402 gen_reload (out, op0, opnum, type);
7404 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7405 This fixes a problem on the 32K where the stack pointer cannot
7406 be used as an operand of an add insn. */
7408 if (rtx_equal_p (op0, op1))
7409 op1 = out;
7411 insn = emit_insn (gen_add2_insn (out, op1));
7413 /* If that failed, copy the address register to the reload register.
7414 Then add the constant to the reload register. */
7416 code = recog_memoized (insn);
7418 if (code >= 0)
7420 extract_insn (insn);
7421 /* We want constrain operands to treat this insn strictly in
7422 its validity determination, i.e., the way it would after reload
7423 has completed. */
7424 if (constrain_operands (1))
7426 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7427 REG_NOTES (insn)
7428 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7429 return insn;
7433 delete_insns_since (last);
7435 gen_reload (out, op1, opnum, type);
7436 insn = emit_insn (gen_add2_insn (out, op0));
7437 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7440 #ifdef SECONDARY_MEMORY_NEEDED
7441 /* If we need a memory location to do the move, do it that way. */
7442 else if ((GET_CODE (in) == REG || GET_CODE (in) == SUBREG)
7443 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7444 && (GET_CODE (out) == REG || GET_CODE (out) == SUBREG)
7445 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7446 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7447 REGNO_REG_CLASS (reg_or_subregno (out)),
7448 GET_MODE (out)))
7450 /* Get the memory to use and rewrite both registers to its mode. */
7451 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7453 if (GET_MODE (loc) != GET_MODE (out))
7454 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7456 if (GET_MODE (loc) != GET_MODE (in))
7457 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7459 gen_reload (loc, in, opnum, type);
7460 gen_reload (out, loc, opnum, type);
7462 #endif
7464 /* If IN is a simple operand, use gen_move_insn. */
7465 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
7466 emit_insn (gen_move_insn (out, in));
7468 #ifdef HAVE_reload_load_address
7469 else if (HAVE_reload_load_address)
7470 emit_insn (gen_reload_load_address (out, in));
7471 #endif
7473 /* Otherwise, just write (set OUT IN) and hope for the best. */
7474 else
7475 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7477 /* Return the first insn emitted.
7478 We can not just return get_last_insn, because there may have
7479 been multiple instructions emitted. Also note that gen_move_insn may
7480 emit more than one insn itself, so we can not assume that there is one
7481 insn emitted per emit_insn_before call. */
7483 return last ? NEXT_INSN (last) : get_insns ();
7486 /* Delete a previously made output-reload whose result we now believe
7487 is not needed. First we double-check.
7489 INSN is the insn now being processed.
7490 LAST_RELOAD_REG is the hard register number for which we want to delete
7491 the last output reload.
7492 J is the reload-number that originally used REG. The caller has made
7493 certain that reload J doesn't use REG any longer for input. */
7495 static void
7496 delete_output_reload (rtx insn, int j, int last_reload_reg)
7498 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7499 rtx reg = spill_reg_stored_to[last_reload_reg];
7500 int k;
7501 int n_occurrences;
7502 int n_inherited = 0;
7503 rtx i1;
7504 rtx substed;
7506 /* It is possible that this reload has been only used to set another reload
7507 we eliminated earlier and thus deleted this instruction too. */
7508 if (INSN_DELETED_P (output_reload_insn))
7509 return;
7511 /* Get the raw pseudo-register referred to. */
7513 while (GET_CODE (reg) == SUBREG)
7514 reg = SUBREG_REG (reg);
7515 substed = reg_equiv_memory_loc[REGNO (reg)];
7517 /* This is unsafe if the operand occurs more often in the current
7518 insn than it is inherited. */
7519 for (k = n_reloads - 1; k >= 0; k--)
7521 rtx reg2 = rld[k].in;
7522 if (! reg2)
7523 continue;
7524 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7525 reg2 = rld[k].in_reg;
7526 #ifdef AUTO_INC_DEC
7527 if (rld[k].out && ! rld[k].out_reg)
7528 reg2 = XEXP (rld[k].in_reg, 0);
7529 #endif
7530 while (GET_CODE (reg2) == SUBREG)
7531 reg2 = SUBREG_REG (reg2);
7532 if (rtx_equal_p (reg2, reg))
7534 if (reload_inherited[k] || reload_override_in[k] || k == j)
7536 n_inherited++;
7537 reg2 = rld[k].out_reg;
7538 if (! reg2)
7539 continue;
7540 while (GET_CODE (reg2) == SUBREG)
7541 reg2 = XEXP (reg2, 0);
7542 if (rtx_equal_p (reg2, reg))
7543 n_inherited++;
7545 else
7546 return;
7549 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7550 if (substed)
7551 n_occurrences += count_occurrences (PATTERN (insn),
7552 eliminate_regs (substed, 0,
7553 NULL_RTX), 0);
7554 if (n_occurrences > n_inherited)
7555 return;
7557 /* If the pseudo-reg we are reloading is no longer referenced
7558 anywhere between the store into it and here,
7559 and no jumps or labels intervene, then the value can get
7560 here through the reload reg alone.
7561 Otherwise, give up--return. */
7562 for (i1 = NEXT_INSN (output_reload_insn);
7563 i1 != insn; i1 = NEXT_INSN (i1))
7565 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7566 return;
7567 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7568 && reg_mentioned_p (reg, PATTERN (i1)))
7570 /* If this is USE in front of INSN, we only have to check that
7571 there are no more references than accounted for by inheritance. */
7572 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
7574 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7575 i1 = NEXT_INSN (i1);
7577 if (n_occurrences <= n_inherited && i1 == insn)
7578 break;
7579 return;
7583 /* We will be deleting the insn. Remove the spill reg information. */
7584 for (k = HARD_REGNO_NREGS (last_reload_reg, GET_MODE (reg)); k-- > 0; )
7586 spill_reg_store[last_reload_reg + k] = 0;
7587 spill_reg_stored_to[last_reload_reg + k] = 0;
7590 /* The caller has already checked that REG dies or is set in INSN.
7591 It has also checked that we are optimizing, and thus some
7592 inaccuracies in the debugging information are acceptable.
7593 So we could just delete output_reload_insn. But in some cases
7594 we can improve the debugging information without sacrificing
7595 optimization - maybe even improving the code: See if the pseudo
7596 reg has been completely replaced with reload regs. If so, delete
7597 the store insn and forget we had a stack slot for the pseudo. */
7598 if (rld[j].out != rld[j].in
7599 && REG_N_DEATHS (REGNO (reg)) == 1
7600 && REG_N_SETS (REGNO (reg)) == 1
7601 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7602 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7604 rtx i2;
7606 /* We know that it was used only between here and the beginning of
7607 the current basic block. (We also know that the last use before
7608 INSN was the output reload we are thinking of deleting, but never
7609 mind that.) Search that range; see if any ref remains. */
7610 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7612 rtx set = single_set (i2);
7614 /* Uses which just store in the pseudo don't count,
7615 since if they are the only uses, they are dead. */
7616 if (set != 0 && SET_DEST (set) == reg)
7617 continue;
7618 if (GET_CODE (i2) == CODE_LABEL
7619 || GET_CODE (i2) == JUMP_INSN)
7620 break;
7621 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7622 && reg_mentioned_p (reg, PATTERN (i2)))
7624 /* Some other ref remains; just delete the output reload we
7625 know to be dead. */
7626 delete_address_reloads (output_reload_insn, insn);
7627 delete_insn (output_reload_insn);
7628 return;
7632 /* Delete the now-dead stores into this pseudo. Note that this
7633 loop also takes care of deleting output_reload_insn. */
7634 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7636 rtx set = single_set (i2);
7638 if (set != 0 && SET_DEST (set) == reg)
7640 delete_address_reloads (i2, insn);
7641 delete_insn (i2);
7643 if (GET_CODE (i2) == CODE_LABEL
7644 || GET_CODE (i2) == JUMP_INSN)
7645 break;
7648 /* For the debugging info, say the pseudo lives in this reload reg. */
7649 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
7650 alter_reg (REGNO (reg), -1);
7652 else
7654 delete_address_reloads (output_reload_insn, insn);
7655 delete_insn (output_reload_insn);
7659 /* We are going to delete DEAD_INSN. Recursively delete loads of
7660 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7661 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7662 static void
7663 delete_address_reloads (rtx dead_insn, rtx current_insn)
7665 rtx set = single_set (dead_insn);
7666 rtx set2, dst, prev, next;
7667 if (set)
7669 rtx dst = SET_DEST (set);
7670 if (GET_CODE (dst) == MEM)
7671 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
7673 /* If we deleted the store from a reloaded post_{in,de}c expression,
7674 we can delete the matching adds. */
7675 prev = PREV_INSN (dead_insn);
7676 next = NEXT_INSN (dead_insn);
7677 if (! prev || ! next)
7678 return;
7679 set = single_set (next);
7680 set2 = single_set (prev);
7681 if (! set || ! set2
7682 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
7683 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
7684 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
7685 return;
7686 dst = SET_DEST (set);
7687 if (! rtx_equal_p (dst, SET_DEST (set2))
7688 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
7689 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
7690 || (INTVAL (XEXP (SET_SRC (set), 1))
7691 != -INTVAL (XEXP (SET_SRC (set2), 1))))
7692 return;
7693 delete_related_insns (prev);
7694 delete_related_insns (next);
7697 /* Subfunction of delete_address_reloads: process registers found in X. */
7698 static void
7699 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
7701 rtx prev, set, dst, i2;
7702 int i, j;
7703 enum rtx_code code = GET_CODE (x);
7705 if (code != REG)
7707 const char *fmt = GET_RTX_FORMAT (code);
7708 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7710 if (fmt[i] == 'e')
7711 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
7712 else if (fmt[i] == 'E')
7714 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7715 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
7716 current_insn);
7719 return;
7722 if (spill_reg_order[REGNO (x)] < 0)
7723 return;
7725 /* Scan backwards for the insn that sets x. This might be a way back due
7726 to inheritance. */
7727 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
7729 code = GET_CODE (prev);
7730 if (code == CODE_LABEL || code == JUMP_INSN)
7731 return;
7732 if (GET_RTX_CLASS (code) != 'i')
7733 continue;
7734 if (reg_set_p (x, PATTERN (prev)))
7735 break;
7736 if (reg_referenced_p (x, PATTERN (prev)))
7737 return;
7739 if (! prev || INSN_UID (prev) < reload_first_uid)
7740 return;
7741 /* Check that PREV only sets the reload register. */
7742 set = single_set (prev);
7743 if (! set)
7744 return;
7745 dst = SET_DEST (set);
7746 if (GET_CODE (dst) != REG
7747 || ! rtx_equal_p (dst, x))
7748 return;
7749 if (! reg_set_p (dst, PATTERN (dead_insn)))
7751 /* Check if DST was used in a later insn -
7752 it might have been inherited. */
7753 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
7755 if (GET_CODE (i2) == CODE_LABEL)
7756 break;
7757 if (! INSN_P (i2))
7758 continue;
7759 if (reg_referenced_p (dst, PATTERN (i2)))
7761 /* If there is a reference to the register in the current insn,
7762 it might be loaded in a non-inherited reload. If no other
7763 reload uses it, that means the register is set before
7764 referenced. */
7765 if (i2 == current_insn)
7767 for (j = n_reloads - 1; j >= 0; j--)
7768 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7769 || reload_override_in[j] == dst)
7770 return;
7771 for (j = n_reloads - 1; j >= 0; j--)
7772 if (rld[j].in && rld[j].reg_rtx == dst)
7773 break;
7774 if (j >= 0)
7775 break;
7777 return;
7779 if (GET_CODE (i2) == JUMP_INSN)
7780 break;
7781 /* If DST is still live at CURRENT_INSN, check if it is used for
7782 any reload. Note that even if CURRENT_INSN sets DST, we still
7783 have to check the reloads. */
7784 if (i2 == current_insn)
7786 for (j = n_reloads - 1; j >= 0; j--)
7787 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7788 || reload_override_in[j] == dst)
7789 return;
7790 /* ??? We can't finish the loop here, because dst might be
7791 allocated to a pseudo in this block if no reload in this
7792 block needs any of the classes containing DST - see
7793 spill_hard_reg. There is no easy way to tell this, so we
7794 have to scan till the end of the basic block. */
7796 if (reg_set_p (dst, PATTERN (i2)))
7797 break;
7800 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
7801 reg_reloaded_contents[REGNO (dst)] = -1;
7802 delete_insn (prev);
7805 /* Output reload-insns to reload VALUE into RELOADREG.
7806 VALUE is an autoincrement or autodecrement RTX whose operand
7807 is a register or memory location;
7808 so reloading involves incrementing that location.
7809 IN is either identical to VALUE, or some cheaper place to reload from.
7811 INC_AMOUNT is the number to increment or decrement by (always positive).
7812 This cannot be deduced from VALUE.
7814 Return the instruction that stores into RELOADREG. */
7816 static rtx
7817 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
7819 /* REG or MEM to be copied and incremented. */
7820 rtx incloc = XEXP (value, 0);
7821 /* Nonzero if increment after copying. */
7822 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7823 rtx last;
7824 rtx inc;
7825 rtx add_insn;
7826 int code;
7827 rtx store;
7828 rtx real_in = in == value ? XEXP (in, 0) : in;
7830 /* No hard register is equivalent to this register after
7831 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
7832 we could inc/dec that register as well (maybe even using it for
7833 the source), but I'm not sure it's worth worrying about. */
7834 if (GET_CODE (incloc) == REG)
7835 reg_last_reload_reg[REGNO (incloc)] = 0;
7837 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7838 inc_amount = -inc_amount;
7840 inc = GEN_INT (inc_amount);
7842 /* If this is post-increment, first copy the location to the reload reg. */
7843 if (post && real_in != reloadreg)
7844 emit_insn (gen_move_insn (reloadreg, real_in));
7846 if (in == value)
7848 /* See if we can directly increment INCLOC. Use a method similar to
7849 that in gen_reload. */
7851 last = get_last_insn ();
7852 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7853 gen_rtx_PLUS (GET_MODE (incloc),
7854 incloc, inc)));
7856 code = recog_memoized (add_insn);
7857 if (code >= 0)
7859 extract_insn (add_insn);
7860 if (constrain_operands (1))
7862 /* If this is a pre-increment and we have incremented the value
7863 where it lives, copy the incremented value to RELOADREG to
7864 be used as an address. */
7866 if (! post)
7867 emit_insn (gen_move_insn (reloadreg, incloc));
7869 return add_insn;
7872 delete_insns_since (last);
7875 /* If couldn't do the increment directly, must increment in RELOADREG.
7876 The way we do this depends on whether this is pre- or post-increment.
7877 For pre-increment, copy INCLOC to the reload register, increment it
7878 there, then save back. */
7880 if (! post)
7882 if (in != reloadreg)
7883 emit_insn (gen_move_insn (reloadreg, real_in));
7884 emit_insn (gen_add2_insn (reloadreg, inc));
7885 store = emit_insn (gen_move_insn (incloc, reloadreg));
7887 else
7889 /* Postincrement.
7890 Because this might be a jump insn or a compare, and because RELOADREG
7891 may not be available after the insn in an input reload, we must do
7892 the incrementation before the insn being reloaded for.
7894 We have already copied IN to RELOADREG. Increment the copy in
7895 RELOADREG, save that back, then decrement RELOADREG so it has
7896 the original value. */
7898 emit_insn (gen_add2_insn (reloadreg, inc));
7899 store = emit_insn (gen_move_insn (incloc, reloadreg));
7900 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
7903 return store;
7906 #ifdef AUTO_INC_DEC
7907 static void
7908 add_auto_inc_notes (rtx insn, rtx x)
7910 enum rtx_code code = GET_CODE (x);
7911 const char *fmt;
7912 int i, j;
7914 if (code == MEM && auto_inc_p (XEXP (x, 0)))
7916 REG_NOTES (insn)
7917 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
7918 return;
7921 /* Scan all the operand sub-expressions. */
7922 fmt = GET_RTX_FORMAT (code);
7923 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7925 if (fmt[i] == 'e')
7926 add_auto_inc_notes (insn, XEXP (x, i));
7927 else if (fmt[i] == 'E')
7928 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7929 add_auto_inc_notes (insn, XVECEXP (x, i, j));
7932 #endif
7934 /* Copy EH notes from an insn to its reloads. */
7935 static void
7936 copy_eh_notes (rtx insn, rtx x)
7938 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
7939 if (eh_note)
7941 for (; x != 0; x = NEXT_INSN (x))
7943 if (may_trap_p (PATTERN (x)))
7944 REG_NOTES (x)
7945 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
7946 REG_NOTES (x));
7951 /* This is used by reload pass, that does emit some instructions after
7952 abnormal calls moving basic block end, but in fact it wants to emit
7953 them on the edge. Looks for abnormal call edges, find backward the
7954 proper call and fix the damage.
7956 Similar handle instructions throwing exceptions internally. */
7957 void
7958 fixup_abnormal_edges (void)
7960 bool inserted = false;
7961 basic_block bb;
7963 FOR_EACH_BB (bb)
7965 edge e;
7967 /* Look for cases we are interested in - calls or instructions causing
7968 exceptions. */
7969 for (e = bb->succ; e; e = e->succ_next)
7971 if (e->flags & EDGE_ABNORMAL_CALL)
7972 break;
7973 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
7974 == (EDGE_ABNORMAL | EDGE_EH))
7975 break;
7977 if (e && GET_CODE (bb->end) != CALL_INSN && !can_throw_internal (bb->end))
7979 rtx insn = bb->end, stop = NEXT_INSN (bb->end);
7980 rtx next;
7981 for (e = bb->succ; e; e = e->succ_next)
7982 if (e->flags & EDGE_FALLTHRU)
7983 break;
7984 /* Get past the new insns generated. Allow notes, as the insns may
7985 be already deleted. */
7986 while ((GET_CODE (insn) == INSN || GET_CODE (insn) == NOTE)
7987 && !can_throw_internal (insn)
7988 && insn != bb->head)
7989 insn = PREV_INSN (insn);
7990 if (GET_CODE (insn) != CALL_INSN && !can_throw_internal (insn))
7991 abort ();
7992 bb->end = insn;
7993 inserted = true;
7994 insn = NEXT_INSN (insn);
7995 while (insn && insn != stop)
7997 next = NEXT_INSN (insn);
7998 if (INSN_P (insn))
8000 delete_insn (insn);
8002 /* Sometimes there's still the return value USE.
8003 If it's placed after a trapping call (i.e. that
8004 call is the last insn anyway), we have no fallthru
8005 edge. Simply delete this use and don't try to insert
8006 on the non-existent edge. */
8007 if (GET_CODE (PATTERN (insn)) != USE)
8009 /* We're not deleting it, we're moving it. */
8010 INSN_DELETED_P (insn) = 0;
8011 PREV_INSN (insn) = NULL_RTX;
8012 NEXT_INSN (insn) = NULL_RTX;
8014 insert_insn_on_edge (insn, e);
8017 insn = next;
8021 /* We've possibly turned single trapping insn into multiple ones. */
8022 if (flag_non_call_exceptions)
8024 sbitmap blocks;
8025 blocks = sbitmap_alloc (last_basic_block);
8026 sbitmap_ones (blocks);
8027 find_many_sub_basic_blocks (blocks);
8029 if (inserted)
8030 commit_edge_insertions ();