* config.gcc <arm>: Add --with-abi=
[official-gcc.git] / gcc / reload1.c
blobce7bf83526e5cfde079db5b1130acbcce13370bd
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, 2004 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 /* During reload_as_needed, element N contains a REG rtx for the hard reg
82 into which reg N has been reloaded (perhaps for a previous insn). */
83 static rtx *reg_last_reload_reg;
85 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
86 for an output reload that stores into reg N. */
87 static char *reg_has_output_reload;
89 /* Indicates which hard regs are reload-registers for an output reload
90 in the current insn. */
91 static HARD_REG_SET reg_is_output_reload;
93 /* Element N is the constant value to which pseudo reg N is equivalent,
94 or zero if pseudo reg N is not equivalent to a constant.
95 find_reloads looks at this in order to replace pseudo reg N
96 with the constant it stands for. */
97 rtx *reg_equiv_constant;
99 /* Element N is a memory location to which pseudo reg N is equivalent,
100 prior to any register elimination (such as frame pointer to stack
101 pointer). Depending on whether or not it is a valid address, this value
102 is transferred to either reg_equiv_address or reg_equiv_mem. */
103 rtx *reg_equiv_memory_loc;
105 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
106 This is used when the address is not valid as a memory address
107 (because its displacement is too big for the machine.) */
108 rtx *reg_equiv_address;
110 /* Element N is the memory slot to which pseudo reg N is equivalent,
111 or zero if pseudo reg N is not equivalent to a memory slot. */
112 rtx *reg_equiv_mem;
114 /* Widest width in which each pseudo reg is referred to (via subreg). */
115 static unsigned int *reg_max_ref_width;
117 /* Element N is the list of insns that initialized reg N from its equivalent
118 constant or memory slot. */
119 static rtx *reg_equiv_init;
121 /* Vector to remember old contents of reg_renumber before spilling. */
122 static short *reg_old_renumber;
124 /* During reload_as_needed, element N contains the last pseudo regno reloaded
125 into hard register N. If that pseudo reg occupied more than one register,
126 reg_reloaded_contents points to that pseudo for each spill register in
127 use; all of these must remain set for an inheritance to occur. */
128 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
130 /* During reload_as_needed, element N contains the insn for which
131 hard register N was last used. Its contents are significant only
132 when reg_reloaded_valid is set for this register. */
133 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
135 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
136 static HARD_REG_SET reg_reloaded_valid;
137 /* Indicate if the register was dead at the end of the reload.
138 This is only valid if reg_reloaded_contents is set and valid. */
139 static HARD_REG_SET reg_reloaded_dead;
141 /* Indicate whether the register's current value is one that is not
142 safe to retain across a call, even for registers that are normally
143 call-saved. */
144 static HARD_REG_SET reg_reloaded_call_part_clobbered;
146 /* Number of spill-regs so far; number of valid elements of spill_regs. */
147 static int n_spills;
149 /* In parallel with spill_regs, contains REG rtx's for those regs.
150 Holds the last rtx used for any given reg, or 0 if it has never
151 been used for spilling yet. This rtx is reused, provided it has
152 the proper mode. */
153 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
155 /* In parallel with spill_regs, contains nonzero for a spill reg
156 that was stored after the last time it was used.
157 The precise value is the insn generated to do the store. */
158 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
160 /* This is the register that was stored with spill_reg_store. This is a
161 copy of reload_out / reload_out_reg when the value was stored; if
162 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
163 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
165 /* This table is the inverse mapping of spill_regs:
166 indexed by hard reg number,
167 it contains the position of that reg in spill_regs,
168 or -1 for something that is not in spill_regs.
170 ?!? This is no longer accurate. */
171 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
173 /* This reg set indicates registers that can't be used as spill registers for
174 the currently processed insn. These are the hard registers which are live
175 during the insn, but not allocated to pseudos, as well as fixed
176 registers. */
177 static HARD_REG_SET bad_spill_regs;
179 /* These are the hard registers that can't be used as spill register for any
180 insn. This includes registers used for user variables and registers that
181 we can't eliminate. A register that appears in this set also can't be used
182 to retry register allocation. */
183 static HARD_REG_SET bad_spill_regs_global;
185 /* Describes order of use of registers for reloading
186 of spilled pseudo-registers. `n_spills' is the number of
187 elements that are actually valid; new ones are added at the end.
189 Both spill_regs and spill_reg_order are used on two occasions:
190 once during find_reload_regs, where they keep track of the spill registers
191 for a single insn, but also during reload_as_needed where they show all
192 the registers ever used by reload. For the latter case, the information
193 is calculated during finish_spills. */
194 static short spill_regs[FIRST_PSEUDO_REGISTER];
196 /* This vector of reg sets indicates, for each pseudo, which hard registers
197 may not be used for retrying global allocation because the register was
198 formerly spilled from one of them. If we allowed reallocating a pseudo to
199 a register that it was already allocated to, reload might not
200 terminate. */
201 static HARD_REG_SET *pseudo_previous_regs;
203 /* This vector of reg sets indicates, for each pseudo, which hard
204 registers may not be used for retrying global allocation because they
205 are used as spill registers during one of the insns in which the
206 pseudo is live. */
207 static HARD_REG_SET *pseudo_forbidden_regs;
209 /* All hard regs that have been used as spill registers for any insn are
210 marked in this set. */
211 static HARD_REG_SET used_spill_regs;
213 /* Index of last register assigned as a spill register. We allocate in
214 a round-robin fashion. */
215 static int last_spill_reg;
217 /* Nonzero if indirect addressing is supported on the machine; this means
218 that spilling (REG n) does not require reloading it into a register in
219 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
220 value indicates the level of indirect addressing supported, e.g., two
221 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
222 a hard register. */
223 static char spill_indirect_levels;
225 /* Nonzero if indirect addressing is supported when the innermost MEM is
226 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
227 which these are valid is the same as spill_indirect_levels, above. */
228 char indirect_symref_ok;
230 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
231 char double_reg_address_ok;
233 /* Record the stack slot for each spilled hard register. */
234 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
236 /* Width allocated so far for that stack slot. */
237 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
239 /* Record which pseudos needed to be spilled. */
240 static regset_head spilled_pseudos;
242 /* Used for communication between order_regs_for_reload and count_pseudo.
243 Used to avoid counting one pseudo twice. */
244 static regset_head pseudos_counted;
246 /* First uid used by insns created by reload in this function.
247 Used in find_equiv_reg. */
248 int reload_first_uid;
250 /* Flag set by local-alloc or global-alloc if anything is live in
251 a call-clobbered reg across calls. */
252 int caller_save_needed;
254 /* Set to 1 while reload_as_needed is operating.
255 Required by some machines to handle any generated moves differently. */
256 int reload_in_progress = 0;
258 /* These arrays record the insn_code of insns that may be needed to
259 perform input and output reloads of special objects. They provide a
260 place to pass a scratch register. */
261 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
262 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
264 /* This obstack is used for allocation of rtl during register elimination.
265 The allocated storage can be freed once find_reloads has processed the
266 insn. */
267 struct obstack reload_obstack;
269 /* Points to the beginning of the reload_obstack. All insn_chain structures
270 are allocated first. */
271 char *reload_startobj;
273 /* The point after all insn_chain structures. Used to quickly deallocate
274 memory allocated in copy_reloads during calculate_needs_all_insns. */
275 char *reload_firstobj;
277 /* This points before all local rtl generated by register elimination.
278 Used to quickly free all memory after processing one insn. */
279 static char *reload_insn_firstobj;
281 /* List of insn_chain instructions, one for every insn that reload needs to
282 examine. */
283 struct insn_chain *reload_insn_chain;
285 /* List of all insns needing reloads. */
286 static struct insn_chain *insns_need_reload;
288 /* This structure is used to record information about register eliminations.
289 Each array entry describes one possible way of eliminating a register
290 in favor of another. If there is more than one way of eliminating a
291 particular register, the most preferred should be specified first. */
293 struct elim_table
295 int from; /* Register number to be eliminated. */
296 int to; /* Register number used as replacement. */
297 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
298 int can_eliminate; /* Nonzero if this elimination can be done. */
299 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
300 insns made by reload. */
301 HOST_WIDE_INT offset; /* Current offset between the two regs. */
302 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
303 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
304 rtx from_rtx; /* REG rtx for the register to be eliminated.
305 We cannot simply compare the number since
306 we might then spuriously replace a hard
307 register corresponding to a pseudo
308 assigned to the reg to be eliminated. */
309 rtx to_rtx; /* REG rtx for the replacement. */
312 static struct elim_table *reg_eliminate = 0;
314 /* This is an intermediate structure to initialize the table. It has
315 exactly the members provided by ELIMINABLE_REGS. */
316 static const struct elim_table_1
318 const int from;
319 const int to;
320 } reg_eliminate_1[] =
322 /* If a set of eliminable registers was specified, define the table from it.
323 Otherwise, default to the normal case of the frame pointer being
324 replaced by the stack pointer. */
326 #ifdef ELIMINABLE_REGS
327 ELIMINABLE_REGS;
328 #else
329 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
330 #endif
332 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
334 /* Record the number of pending eliminations that have an offset not equal
335 to their initial offset. If nonzero, we use a new copy of each
336 replacement result in any insns encountered. */
337 int num_not_at_initial_offset;
339 /* Count the number of registers that we may be able to eliminate. */
340 static int num_eliminable;
341 /* And the number of registers that are equivalent to a constant that
342 can be eliminated to frame_pointer / arg_pointer + constant. */
343 static int num_eliminable_invariants;
345 /* For each label, we record the offset of each elimination. If we reach
346 a label by more than one path and an offset differs, we cannot do the
347 elimination. This information is indexed by the difference of the
348 number of the label and the first label number. We can't offset the
349 pointer itself as this can cause problems on machines with segmented
350 memory. The first table is an array of flags that records whether we
351 have yet encountered a label and the second table is an array of arrays,
352 one entry in the latter array for each elimination. */
354 static int first_label_num;
355 static char *offsets_known_at;
356 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
358 /* Number of labels in the current function. */
360 static int num_labels;
362 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
363 static void maybe_fix_stack_asms (void);
364 static void copy_reloads (struct insn_chain *);
365 static void calculate_needs_all_insns (int);
366 static int find_reg (struct insn_chain *, int);
367 static void find_reload_regs (struct insn_chain *);
368 static void select_reload_regs (void);
369 static void delete_caller_save_insns (void);
371 static void spill_failure (rtx, enum reg_class);
372 static void count_spilled_pseudo (int, int, int);
373 static void delete_dead_insn (rtx);
374 static void alter_reg (int, int);
375 static void set_label_offsets (rtx, rtx, int);
376 static void check_eliminable_occurrences (rtx);
377 static void elimination_effects (rtx, enum machine_mode);
378 static int eliminate_regs_in_insn (rtx, int);
379 static void update_eliminable_offsets (void);
380 static void mark_not_eliminable (rtx, rtx, void *);
381 static void set_initial_elim_offsets (void);
382 static void verify_initial_elim_offsets (void);
383 static void set_initial_label_offsets (void);
384 static void set_offsets_for_label (rtx);
385 static void init_elim_table (void);
386 static void update_eliminables (HARD_REG_SET *);
387 static void spill_hard_reg (unsigned int, int);
388 static int finish_spills (int);
389 static void ior_hard_reg_set (HARD_REG_SET *, HARD_REG_SET *);
390 static void scan_paradoxical_subregs (rtx);
391 static void count_pseudo (int);
392 static void order_regs_for_reload (struct insn_chain *);
393 static void reload_as_needed (int);
394 static void forget_old_reloads_1 (rtx, rtx, void *);
395 static int reload_reg_class_lower (const void *, const void *);
396 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
397 enum machine_mode);
398 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
399 enum machine_mode);
400 static int reload_reg_free_p (unsigned int, int, enum reload_type);
401 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
402 rtx, rtx, int, int);
403 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
404 rtx, rtx, int, int);
405 static int function_invariant_p (rtx);
406 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
407 static int allocate_reload_reg (struct insn_chain *, int, int);
408 static int conflicts_with_override (rtx);
409 static void failed_reload (rtx, int);
410 static int set_reload_reg (int, int);
411 static void choose_reload_regs_init (struct insn_chain *, rtx *);
412 static void choose_reload_regs (struct insn_chain *);
413 static void merge_assigned_reloads (rtx);
414 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
415 rtx, int);
416 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
417 int);
418 static void do_input_reload (struct insn_chain *, struct reload *, int);
419 static void do_output_reload (struct insn_chain *, struct reload *, int);
420 static void emit_reload_insns (struct insn_chain *);
421 static void delete_output_reload (rtx, int, int);
422 static void delete_address_reloads (rtx, rtx);
423 static void delete_address_reloads_1 (rtx, rtx, rtx);
424 static rtx inc_for_reload (rtx, rtx, rtx, int);
425 #ifdef AUTO_INC_DEC
426 static void add_auto_inc_notes (rtx, rtx);
427 #endif
428 static void copy_eh_notes (rtx, rtx);
430 /* Initialize the reload pass once per compilation. */
432 void
433 init_reload (void)
435 int i;
437 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
438 Set spill_indirect_levels to the number of levels such addressing is
439 permitted, zero if it is not permitted at all. */
441 rtx tem
442 = gen_rtx_MEM (Pmode,
443 gen_rtx_PLUS (Pmode,
444 gen_rtx_REG (Pmode,
445 LAST_VIRTUAL_REGISTER + 1),
446 GEN_INT (4)));
447 spill_indirect_levels = 0;
449 while (memory_address_p (QImode, tem))
451 spill_indirect_levels++;
452 tem = gen_rtx_MEM (Pmode, tem);
455 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
457 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
458 indirect_symref_ok = memory_address_p (QImode, tem);
460 /* See if reg+reg is a valid (and offsettable) address. */
462 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
464 tem = gen_rtx_PLUS (Pmode,
465 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
466 gen_rtx_REG (Pmode, i));
468 /* This way, we make sure that reg+reg is an offsettable address. */
469 tem = plus_constant (tem, 4);
471 if (memory_address_p (QImode, tem))
473 double_reg_address_ok = 1;
474 break;
478 /* Initialize obstack for our rtl allocation. */
479 gcc_obstack_init (&reload_obstack);
480 reload_startobj = obstack_alloc (&reload_obstack, 0);
482 INIT_REG_SET (&spilled_pseudos);
483 INIT_REG_SET (&pseudos_counted);
486 /* List of insn chains that are currently unused. */
487 static struct insn_chain *unused_insn_chains = 0;
489 /* Allocate an empty insn_chain structure. */
490 struct insn_chain *
491 new_insn_chain (void)
493 struct insn_chain *c;
495 if (unused_insn_chains == 0)
497 c = obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
498 INIT_REG_SET (&c->live_throughout);
499 INIT_REG_SET (&c->dead_or_set);
501 else
503 c = unused_insn_chains;
504 unused_insn_chains = c->next;
506 c->is_caller_save_insn = 0;
507 c->need_operand_change = 0;
508 c->need_reload = 0;
509 c->need_elim = 0;
510 return c;
513 /* Small utility function to set all regs in hard reg set TO which are
514 allocated to pseudos in regset FROM. */
516 void
517 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
519 unsigned int regno;
521 EXECUTE_IF_SET_IN_REG_SET
522 (from, FIRST_PSEUDO_REGISTER, regno,
524 int r = reg_renumber[regno];
525 int nregs;
527 if (r < 0)
529 /* reload_combine uses the information from
530 BASIC_BLOCK->global_live_at_start, which might still
531 contain registers that have not actually been allocated
532 since they have an equivalence. */
533 if (! reload_completed)
534 abort ();
536 else
538 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
539 while (nregs-- > 0)
540 SET_HARD_REG_BIT (*to, r + nregs);
545 /* Replace all pseudos found in LOC with their corresponding
546 equivalences. */
548 static void
549 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
551 rtx x = *loc;
552 enum rtx_code code;
553 const char *fmt;
554 int i, j;
556 if (! x)
557 return;
559 code = GET_CODE (x);
560 if (code == REG)
562 unsigned int regno = REGNO (x);
564 if (regno < FIRST_PSEUDO_REGISTER)
565 return;
567 x = eliminate_regs (x, mem_mode, usage);
568 if (x != *loc)
570 *loc = x;
571 replace_pseudos_in (loc, mem_mode, usage);
572 return;
575 if (reg_equiv_constant[regno])
576 *loc = reg_equiv_constant[regno];
577 else if (reg_equiv_mem[regno])
578 *loc = reg_equiv_mem[regno];
579 else if (reg_equiv_address[regno])
580 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
581 else if (GET_CODE (regno_reg_rtx[regno]) != REG
582 || REGNO (regno_reg_rtx[regno]) != regno)
583 *loc = regno_reg_rtx[regno];
584 else
585 abort ();
587 return;
589 else if (code == MEM)
591 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
592 return;
595 /* Process each of our operands recursively. */
596 fmt = GET_RTX_FORMAT (code);
597 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
598 if (*fmt == 'e')
599 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
600 else if (*fmt == 'E')
601 for (j = 0; j < XVECLEN (x, i); j++)
602 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
606 /* Global variables used by reload and its subroutines. */
608 /* Set during calculate_needs if an insn needs register elimination. */
609 static int something_needs_elimination;
610 /* Set during calculate_needs if an insn needs an operand changed. */
611 int something_needs_operands_changed;
613 /* Nonzero means we couldn't get enough spill regs. */
614 static int failure;
616 /* Main entry point for the reload pass.
618 FIRST is the first insn of the function being compiled.
620 GLOBAL nonzero means we were called from global_alloc
621 and should attempt to reallocate any pseudoregs that we
622 displace from hard regs we will use for reloads.
623 If GLOBAL is zero, we do not have enough information to do that,
624 so any pseudo reg that is spilled must go to the stack.
626 Return value is nonzero if reload failed
627 and we must not do any more for this function. */
630 reload (rtx first, int global)
632 int i;
633 rtx insn;
634 struct elim_table *ep;
635 basic_block bb;
637 /* Make sure even insns with volatile mem refs are recognizable. */
638 init_recog ();
640 failure = 0;
642 reload_firstobj = obstack_alloc (&reload_obstack, 0);
644 /* Make sure that the last insn in the chain
645 is not something that needs reloading. */
646 emit_note (NOTE_INSN_DELETED);
648 /* Enable find_equiv_reg to distinguish insns made by reload. */
649 reload_first_uid = get_max_uid ();
651 #ifdef SECONDARY_MEMORY_NEEDED
652 /* Initialize the secondary memory table. */
653 clear_secondary_mem ();
654 #endif
656 /* We don't have a stack slot for any spill reg yet. */
657 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
658 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
660 /* Initialize the save area information for caller-save, in case some
661 are needed. */
662 init_save_areas ();
664 /* Compute which hard registers are now in use
665 as homes for pseudo registers.
666 This is done here rather than (eg) in global_alloc
667 because this point is reached even if not optimizing. */
668 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
669 mark_home_live (i);
671 /* A function that receives a nonlocal goto must save all call-saved
672 registers. */
673 if (current_function_has_nonlocal_label)
674 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
675 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
676 regs_ever_live[i] = 1;
678 #ifdef NON_SAVING_SETJMP
679 /* A function that calls setjmp should save and restore all the
680 call-saved registers on a system where longjmp clobbers them. */
681 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
683 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
684 if (! call_used_regs[i])
685 regs_ever_live[i] = 1;
687 #endif
689 /* Find all the pseudo registers that didn't get hard regs
690 but do have known equivalent constants or memory slots.
691 These include parameters (known equivalent to parameter slots)
692 and cse'd or loop-moved constant memory addresses.
694 Record constant equivalents in reg_equiv_constant
695 so they will be substituted by find_reloads.
696 Record memory equivalents in reg_mem_equiv so they can
697 be substituted eventually by altering the REG-rtx's. */
699 reg_equiv_constant = xcalloc (max_regno, sizeof (rtx));
700 reg_equiv_mem = xcalloc (max_regno, sizeof (rtx));
701 reg_equiv_init = xcalloc (max_regno, sizeof (rtx));
702 reg_equiv_address = xcalloc (max_regno, sizeof (rtx));
703 reg_max_ref_width = xcalloc (max_regno, sizeof (int));
704 reg_old_renumber = xcalloc (max_regno, sizeof (short));
705 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
706 pseudo_forbidden_regs = xmalloc (max_regno * sizeof (HARD_REG_SET));
707 pseudo_previous_regs = xcalloc (max_regno, sizeof (HARD_REG_SET));
709 CLEAR_HARD_REG_SET (bad_spill_regs_global);
711 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
712 to. Also find all paradoxical subregs and find largest such for
713 each pseudo. */
715 num_eliminable_invariants = 0;
716 for (insn = first; insn; insn = NEXT_INSN (insn))
718 rtx set = single_set (insn);
720 /* We may introduce USEs that we want to remove at the end, so
721 we'll mark them with QImode. Make sure there are no
722 previously-marked insns left by say regmove. */
723 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
724 && GET_MODE (insn) != VOIDmode)
725 PUT_MODE (insn, VOIDmode);
727 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
729 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
730 if (note
731 #ifdef LEGITIMATE_PIC_OPERAND_P
732 && (! function_invariant_p (XEXP (note, 0))
733 || ! flag_pic
734 /* A function invariant is often CONSTANT_P but may
735 include a register. We promise to only pass
736 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
737 || (CONSTANT_P (XEXP (note, 0))
738 && LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0))))
739 #endif
742 rtx x = XEXP (note, 0);
743 i = REGNO (SET_DEST (set));
744 if (i > LAST_VIRTUAL_REGISTER)
746 /* It can happen that a REG_EQUIV note contains a MEM
747 that is not a legitimate memory operand. As later
748 stages of reload assume that all addresses found
749 in the reg_equiv_* arrays were originally legitimate,
750 we ignore such REG_EQUIV notes. */
751 if (memory_operand (x, VOIDmode))
753 /* Always unshare the equivalence, so we can
754 substitute into this insn without touching the
755 equivalence. */
756 reg_equiv_memory_loc[i] = copy_rtx (x);
758 else if (function_invariant_p (x))
760 if (GET_CODE (x) == PLUS)
762 /* This is PLUS of frame pointer and a constant,
763 and might be shared. Unshare it. */
764 reg_equiv_constant[i] = copy_rtx (x);
765 num_eliminable_invariants++;
767 else if (x == frame_pointer_rtx
768 || x == arg_pointer_rtx)
770 reg_equiv_constant[i] = x;
771 num_eliminable_invariants++;
773 else if (LEGITIMATE_CONSTANT_P (x))
774 reg_equiv_constant[i] = x;
775 else
777 reg_equiv_memory_loc[i]
778 = force_const_mem (GET_MODE (SET_DEST (set)), x);
779 if (!reg_equiv_memory_loc[i])
780 continue;
783 else
784 continue;
786 /* If this register is being made equivalent to a MEM
787 and the MEM is not SET_SRC, the equivalencing insn
788 is one with the MEM as a SET_DEST and it occurs later.
789 So don't mark this insn now. */
790 if (GET_CODE (x) != MEM
791 || rtx_equal_p (SET_SRC (set), x))
792 reg_equiv_init[i]
793 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
798 /* If this insn is setting a MEM from a register equivalent to it,
799 this is the equivalencing insn. */
800 else if (set && GET_CODE (SET_DEST (set)) == MEM
801 && GET_CODE (SET_SRC (set)) == REG
802 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
803 && rtx_equal_p (SET_DEST (set),
804 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
805 reg_equiv_init[REGNO (SET_SRC (set))]
806 = gen_rtx_INSN_LIST (VOIDmode, insn,
807 reg_equiv_init[REGNO (SET_SRC (set))]);
809 if (INSN_P (insn))
810 scan_paradoxical_subregs (PATTERN (insn));
813 init_elim_table ();
815 first_label_num = get_first_label_num ();
816 num_labels = max_label_num () - first_label_num;
818 /* Allocate the tables used to store offset information at labels. */
819 /* We used to use alloca here, but the size of what it would try to
820 allocate would occasionally cause it to exceed the stack limit and
821 cause a core dump. */
822 offsets_known_at = xmalloc (num_labels);
823 offsets_at = xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
825 /* Alter each pseudo-reg rtx to contain its hard reg number.
826 Assign stack slots to the pseudos that lack hard regs or equivalents.
827 Do not touch virtual registers. */
829 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
830 alter_reg (i, -1);
832 /* If we have some registers we think can be eliminated, scan all insns to
833 see if there is an insn that sets one of these registers to something
834 other than itself plus a constant. If so, the register cannot be
835 eliminated. Doing this scan here eliminates an extra pass through the
836 main reload loop in the most common case where register elimination
837 cannot be done. */
838 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
839 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
840 || GET_CODE (insn) == CALL_INSN)
841 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
843 maybe_fix_stack_asms ();
845 insns_need_reload = 0;
846 something_needs_elimination = 0;
848 /* Initialize to -1, which means take the first spill register. */
849 last_spill_reg = -1;
851 /* Spill any hard regs that we know we can't eliminate. */
852 CLEAR_HARD_REG_SET (used_spill_regs);
853 /* There can be multiple ways to eliminate a register;
854 they should be listed adjacently.
855 Elimination for any register fails only if all possible ways fail. */
856 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
858 int from = ep->from;
859 int can_eliminate = 0;
862 can_eliminate |= ep->can_eliminate;
863 ep++;
865 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
866 if (! can_eliminate)
867 spill_hard_reg (from, 1);
870 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
871 if (frame_pointer_needed)
872 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
873 #endif
874 finish_spills (global);
876 /* From now on, we may need to generate moves differently. We may also
877 allow modifications of insns which cause them to not be recognized.
878 Any such modifications will be cleaned up during reload itself. */
879 reload_in_progress = 1;
881 /* This loop scans the entire function each go-round
882 and repeats until one repetition spills no additional hard regs. */
883 for (;;)
885 int something_changed;
886 int did_spill;
888 HOST_WIDE_INT starting_frame_size;
890 /* Round size of stack frame to stack_alignment_needed. This must be done
891 here because the stack size may be a part of the offset computation
892 for register elimination, and there might have been new stack slots
893 created in the last iteration of this loop. */
894 if (cfun->stack_alignment_needed)
895 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
897 starting_frame_size = get_frame_size ();
899 set_initial_elim_offsets ();
900 set_initial_label_offsets ();
902 /* For each pseudo register that has an equivalent location defined,
903 try to eliminate any eliminable registers (such as the frame pointer)
904 assuming initial offsets for the replacement register, which
905 is the normal case.
907 If the resulting location is directly addressable, substitute
908 the MEM we just got directly for the old REG.
910 If it is not addressable but is a constant or the sum of a hard reg
911 and constant, it is probably not addressable because the constant is
912 out of range, in that case record the address; we will generate
913 hairy code to compute the address in a register each time it is
914 needed. Similarly if it is a hard register, but one that is not
915 valid as an address register.
917 If the location is not addressable, but does not have one of the
918 above forms, assign a stack slot. We have to do this to avoid the
919 potential of producing lots of reloads if, e.g., a location involves
920 a pseudo that didn't get a hard register and has an equivalent memory
921 location that also involves a pseudo that didn't get a hard register.
923 Perhaps at some point we will improve reload_when_needed handling
924 so this problem goes away. But that's very hairy. */
926 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
927 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
929 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
931 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
932 XEXP (x, 0)))
933 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
934 else if (CONSTANT_P (XEXP (x, 0))
935 || (GET_CODE (XEXP (x, 0)) == REG
936 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
937 || (GET_CODE (XEXP (x, 0)) == PLUS
938 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
939 && (REGNO (XEXP (XEXP (x, 0), 0))
940 < FIRST_PSEUDO_REGISTER)
941 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
942 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
943 else
945 /* Make a new stack slot. Then indicate that something
946 changed so we go back and recompute offsets for
947 eliminable registers because the allocation of memory
948 below might change some offset. reg_equiv_{mem,address}
949 will be set up for this pseudo on the next pass around
950 the loop. */
951 reg_equiv_memory_loc[i] = 0;
952 reg_equiv_init[i] = 0;
953 alter_reg (i, -1);
957 if (caller_save_needed)
958 setup_save_areas ();
960 /* If we allocated another stack slot, redo elimination bookkeeping. */
961 if (starting_frame_size != get_frame_size ())
962 continue;
964 if (caller_save_needed)
966 save_call_clobbered_regs ();
967 /* That might have allocated new insn_chain structures. */
968 reload_firstobj = obstack_alloc (&reload_obstack, 0);
971 calculate_needs_all_insns (global);
973 CLEAR_REG_SET (&spilled_pseudos);
974 did_spill = 0;
976 something_changed = 0;
978 /* If we allocated any new memory locations, make another pass
979 since it might have changed elimination offsets. */
980 if (starting_frame_size != get_frame_size ())
981 something_changed = 1;
984 HARD_REG_SET to_spill;
985 CLEAR_HARD_REG_SET (to_spill);
986 update_eliminables (&to_spill);
987 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
988 if (TEST_HARD_REG_BIT (to_spill, i))
990 spill_hard_reg (i, 1);
991 did_spill = 1;
993 /* Regardless of the state of spills, if we previously had
994 a register that we thought we could eliminate, but now can
995 not eliminate, we must run another pass.
997 Consider pseudos which have an entry in reg_equiv_* which
998 reference an eliminable register. We must make another pass
999 to update reg_equiv_* so that we do not substitute in the
1000 old value from when we thought the elimination could be
1001 performed. */
1002 something_changed = 1;
1006 select_reload_regs ();
1007 if (failure)
1008 goto failed;
1010 if (insns_need_reload != 0 || did_spill)
1011 something_changed |= finish_spills (global);
1013 if (! something_changed)
1014 break;
1016 if (caller_save_needed)
1017 delete_caller_save_insns ();
1019 obstack_free (&reload_obstack, reload_firstobj);
1022 /* If global-alloc was run, notify it of any register eliminations we have
1023 done. */
1024 if (global)
1025 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1026 if (ep->can_eliminate)
1027 mark_elimination (ep->from, ep->to);
1029 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1030 If that insn didn't set the register (i.e., it copied the register to
1031 memory), just delete that insn instead of the equivalencing insn plus
1032 anything now dead. If we call delete_dead_insn on that insn, we may
1033 delete the insn that actually sets the register if the register dies
1034 there and that is incorrect. */
1036 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1038 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1040 rtx list;
1041 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1043 rtx equiv_insn = XEXP (list, 0);
1045 /* If we already deleted the insn or if it may trap, we can't
1046 delete it. The latter case shouldn't happen, but can
1047 if an insn has a variable address, gets a REG_EH_REGION
1048 note added to it, and then gets converted into an load
1049 from a constant address. */
1050 if (GET_CODE (equiv_insn) == NOTE
1051 || can_throw_internal (equiv_insn))
1053 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1054 delete_dead_insn (equiv_insn);
1055 else
1057 PUT_CODE (equiv_insn, NOTE);
1058 NOTE_SOURCE_FILE (equiv_insn) = 0;
1059 NOTE_LINE_NUMBER (equiv_insn) = NOTE_INSN_DELETED;
1065 /* Use the reload registers where necessary
1066 by generating move instructions to move the must-be-register
1067 values into or out of the reload registers. */
1069 if (insns_need_reload != 0 || something_needs_elimination
1070 || something_needs_operands_changed)
1072 HOST_WIDE_INT old_frame_size = get_frame_size ();
1074 reload_as_needed (global);
1076 if (old_frame_size != get_frame_size ())
1077 abort ();
1079 if (num_eliminable)
1080 verify_initial_elim_offsets ();
1083 /* If we were able to eliminate the frame pointer, show that it is no
1084 longer live at the start of any basic block. If it ls live by
1085 virtue of being in a pseudo, that pseudo will be marked live
1086 and hence the frame pointer will be known to be live via that
1087 pseudo. */
1089 if (! frame_pointer_needed)
1090 FOR_EACH_BB (bb)
1091 CLEAR_REGNO_REG_SET (bb->global_live_at_start,
1092 HARD_FRAME_POINTER_REGNUM);
1094 /* Come here (with failure set nonzero) if we can't get enough spill regs
1095 and we decide not to abort about it. */
1096 failed:
1098 CLEAR_REG_SET (&spilled_pseudos);
1099 reload_in_progress = 0;
1101 /* Now eliminate all pseudo regs by modifying them into
1102 their equivalent memory references.
1103 The REG-rtx's for the pseudos are modified in place,
1104 so all insns that used to refer to them now refer to memory.
1106 For a reg that has a reg_equiv_address, all those insns
1107 were changed by reloading so that no insns refer to it any longer;
1108 but the DECL_RTL of a variable decl may refer to it,
1109 and if so this causes the debugging info to mention the variable. */
1111 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1113 rtx addr = 0;
1115 if (reg_equiv_mem[i])
1116 addr = XEXP (reg_equiv_mem[i], 0);
1118 if (reg_equiv_address[i])
1119 addr = reg_equiv_address[i];
1121 if (addr)
1123 if (reg_renumber[i] < 0)
1125 rtx reg = regno_reg_rtx[i];
1127 REG_USERVAR_P (reg) = 0;
1128 PUT_CODE (reg, MEM);
1129 XEXP (reg, 0) = addr;
1130 if (reg_equiv_memory_loc[i])
1131 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1132 else
1134 RTX_UNCHANGING_P (reg) = MEM_IN_STRUCT_P (reg)
1135 = MEM_SCALAR_P (reg) = 0;
1136 MEM_ATTRS (reg) = 0;
1139 else if (reg_equiv_mem[i])
1140 XEXP (reg_equiv_mem[i], 0) = addr;
1144 /* We must set reload_completed now since the cleanup_subreg_operands call
1145 below will re-recognize each insn and reload may have generated insns
1146 which are only valid during and after reload. */
1147 reload_completed = 1;
1149 /* Make a pass over all the insns and delete all USEs which we inserted
1150 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1151 notes. Delete all CLOBBER insns, except those that refer to the return
1152 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1153 from misarranging variable-array code, and simplify (subreg (reg))
1154 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1155 are no longer useful or accurate. Strip and regenerate REG_INC notes
1156 that may have been moved around. */
1158 for (insn = first; insn; insn = NEXT_INSN (insn))
1159 if (INSN_P (insn))
1161 rtx *pnote;
1163 if (GET_CODE (insn) == CALL_INSN)
1164 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1165 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1167 if ((GET_CODE (PATTERN (insn)) == USE
1168 /* We mark with QImode USEs introduced by reload itself. */
1169 && (GET_MODE (insn) == QImode
1170 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1171 || (GET_CODE (PATTERN (insn)) == CLOBBER
1172 && (GET_CODE (XEXP (PATTERN (insn), 0)) != MEM
1173 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1174 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1175 && XEXP (XEXP (PATTERN (insn), 0), 0)
1176 != stack_pointer_rtx))
1177 && (GET_CODE (XEXP (PATTERN (insn), 0)) != REG
1178 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1180 delete_insn (insn);
1181 continue;
1184 /* Some CLOBBERs may survive until here and still reference unassigned
1185 pseudos with const equivalent, which may in turn cause ICE in later
1186 passes if the reference remains in place. */
1187 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1188 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1189 VOIDmode, PATTERN (insn));
1191 pnote = &REG_NOTES (insn);
1192 while (*pnote != 0)
1194 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1195 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1196 || REG_NOTE_KIND (*pnote) == REG_INC
1197 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1198 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1199 *pnote = XEXP (*pnote, 1);
1200 else
1201 pnote = &XEXP (*pnote, 1);
1204 #ifdef AUTO_INC_DEC
1205 add_auto_inc_notes (insn, PATTERN (insn));
1206 #endif
1208 /* And simplify (subreg (reg)) if it appears as an operand. */
1209 cleanup_subreg_operands (insn);
1212 /* If we are doing stack checking, give a warning if this function's
1213 frame size is larger than we expect. */
1214 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1216 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1217 static int verbose_warned = 0;
1219 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1220 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1221 size += UNITS_PER_WORD;
1223 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1225 warning ("frame size too large for reliable stack checking");
1226 if (! verbose_warned)
1228 warning ("try reducing the number of local variables");
1229 verbose_warned = 1;
1234 /* Indicate that we no longer have known memory locations or constants. */
1235 if (reg_equiv_constant)
1236 free (reg_equiv_constant);
1237 reg_equiv_constant = 0;
1238 if (reg_equiv_memory_loc)
1239 free (reg_equiv_memory_loc);
1240 reg_equiv_memory_loc = 0;
1242 if (offsets_known_at)
1243 free (offsets_known_at);
1244 if (offsets_at)
1245 free (offsets_at);
1247 free (reg_equiv_mem);
1248 free (reg_equiv_init);
1249 free (reg_equiv_address);
1250 free (reg_max_ref_width);
1251 free (reg_old_renumber);
1252 free (pseudo_previous_regs);
1253 free (pseudo_forbidden_regs);
1255 CLEAR_HARD_REG_SET (used_spill_regs);
1256 for (i = 0; i < n_spills; i++)
1257 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1259 /* Free all the insn_chain structures at once. */
1260 obstack_free (&reload_obstack, reload_startobj);
1261 unused_insn_chains = 0;
1262 fixup_abnormal_edges ();
1264 /* Replacing pseudos with their memory equivalents might have
1265 created shared rtx. Subsequent passes would get confused
1266 by this, so unshare everything here. */
1267 unshare_all_rtl_again (first);
1269 #ifdef STACK_BOUNDARY
1270 /* init_emit has set the alignment of the hard frame pointer
1271 to STACK_BOUNDARY. It is very likely no longer valid if
1272 the hard frame pointer was used for register allocation. */
1273 if (!frame_pointer_needed)
1274 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1275 #endif
1277 return failure;
1280 /* Yet another special case. Unfortunately, reg-stack forces people to
1281 write incorrect clobbers in asm statements. These clobbers must not
1282 cause the register to appear in bad_spill_regs, otherwise we'll call
1283 fatal_insn later. We clear the corresponding regnos in the live
1284 register sets to avoid this.
1285 The whole thing is rather sick, I'm afraid. */
1287 static void
1288 maybe_fix_stack_asms (void)
1290 #ifdef STACK_REGS
1291 const char *constraints[MAX_RECOG_OPERANDS];
1292 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1293 struct insn_chain *chain;
1295 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1297 int i, noperands;
1298 HARD_REG_SET clobbered, allowed;
1299 rtx pat;
1301 if (! INSN_P (chain->insn)
1302 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1303 continue;
1304 pat = PATTERN (chain->insn);
1305 if (GET_CODE (pat) != PARALLEL)
1306 continue;
1308 CLEAR_HARD_REG_SET (clobbered);
1309 CLEAR_HARD_REG_SET (allowed);
1311 /* First, make a mask of all stack regs that are clobbered. */
1312 for (i = 0; i < XVECLEN (pat, 0); i++)
1314 rtx t = XVECEXP (pat, 0, i);
1315 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1316 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1319 /* Get the operand values and constraints out of the insn. */
1320 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1321 constraints, operand_mode);
1323 /* For every operand, see what registers are allowed. */
1324 for (i = 0; i < noperands; i++)
1326 const char *p = constraints[i];
1327 /* For every alternative, we compute the class of registers allowed
1328 for reloading in CLS, and merge its contents into the reg set
1329 ALLOWED. */
1330 int cls = (int) NO_REGS;
1332 for (;;)
1334 char c = *p;
1336 if (c == '\0' || c == ',' || c == '#')
1338 /* End of one alternative - mark the regs in the current
1339 class, and reset the class. */
1340 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1341 cls = NO_REGS;
1342 p++;
1343 if (c == '#')
1344 do {
1345 c = *p++;
1346 } while (c != '\0' && c != ',');
1347 if (c == '\0')
1348 break;
1349 continue;
1352 switch (c)
1354 case '=': case '+': case '*': case '%': case '?': case '!':
1355 case '0': case '1': case '2': case '3': case '4': case 'm':
1356 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1357 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1358 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1359 case 'P':
1360 break;
1362 case 'p':
1363 cls = (int) reg_class_subunion[cls]
1364 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1365 break;
1367 case 'g':
1368 case 'r':
1369 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1370 break;
1372 default:
1373 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1374 cls = (int) reg_class_subunion[cls]
1375 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1376 else
1377 cls = (int) reg_class_subunion[cls]
1378 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1380 p += CONSTRAINT_LEN (c, p);
1383 /* Those of the registers which are clobbered, but allowed by the
1384 constraints, must be usable as reload registers. So clear them
1385 out of the life information. */
1386 AND_HARD_REG_SET (allowed, clobbered);
1387 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1388 if (TEST_HARD_REG_BIT (allowed, i))
1390 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1391 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1395 #endif
1398 /* Copy the global variables n_reloads and rld into the corresponding elts
1399 of CHAIN. */
1400 static void
1401 copy_reloads (struct insn_chain *chain)
1403 chain->n_reloads = n_reloads;
1404 chain->rld = obstack_alloc (&reload_obstack,
1405 n_reloads * sizeof (struct reload));
1406 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1407 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1410 /* Walk the chain of insns, and determine for each whether it needs reloads
1411 and/or eliminations. Build the corresponding insns_need_reload list, and
1412 set something_needs_elimination as appropriate. */
1413 static void
1414 calculate_needs_all_insns (int global)
1416 struct insn_chain **pprev_reload = &insns_need_reload;
1417 struct insn_chain *chain, *next = 0;
1419 something_needs_elimination = 0;
1421 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1422 for (chain = reload_insn_chain; chain != 0; chain = next)
1424 rtx insn = chain->insn;
1426 next = chain->next;
1428 /* Clear out the shortcuts. */
1429 chain->n_reloads = 0;
1430 chain->need_elim = 0;
1431 chain->need_reload = 0;
1432 chain->need_operand_change = 0;
1434 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1435 include REG_LABEL), we need to see what effects this has on the
1436 known offsets at labels. */
1438 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
1439 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1440 set_label_offsets (insn, insn, 0);
1442 if (INSN_P (insn))
1444 rtx old_body = PATTERN (insn);
1445 int old_code = INSN_CODE (insn);
1446 rtx old_notes = REG_NOTES (insn);
1447 int did_elimination = 0;
1448 int operands_changed = 0;
1449 rtx set = single_set (insn);
1451 /* Skip insns that only set an equivalence. */
1452 if (set && GET_CODE (SET_DEST (set)) == REG
1453 && reg_renumber[REGNO (SET_DEST (set))] < 0
1454 && reg_equiv_constant[REGNO (SET_DEST (set))])
1455 continue;
1457 /* If needed, eliminate any eliminable registers. */
1458 if (num_eliminable || num_eliminable_invariants)
1459 did_elimination = eliminate_regs_in_insn (insn, 0);
1461 /* Analyze the instruction. */
1462 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1463 global, spill_reg_order);
1465 /* If a no-op set needs more than one reload, this is likely
1466 to be something that needs input address reloads. We
1467 can't get rid of this cleanly later, and it is of no use
1468 anyway, so discard it now.
1469 We only do this when expensive_optimizations is enabled,
1470 since this complements reload inheritance / output
1471 reload deletion, and it can make debugging harder. */
1472 if (flag_expensive_optimizations && n_reloads > 1)
1474 rtx set = single_set (insn);
1475 if (set
1476 && SET_SRC (set) == SET_DEST (set)
1477 && GET_CODE (SET_SRC (set)) == REG
1478 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1480 delete_insn (insn);
1481 /* Delete it from the reload chain. */
1482 if (chain->prev)
1483 chain->prev->next = next;
1484 else
1485 reload_insn_chain = next;
1486 if (next)
1487 next->prev = chain->prev;
1488 chain->next = unused_insn_chains;
1489 unused_insn_chains = chain;
1490 continue;
1493 if (num_eliminable)
1494 update_eliminable_offsets ();
1496 /* Remember for later shortcuts which insns had any reloads or
1497 register eliminations. */
1498 chain->need_elim = did_elimination;
1499 chain->need_reload = n_reloads > 0;
1500 chain->need_operand_change = operands_changed;
1502 /* Discard any register replacements done. */
1503 if (did_elimination)
1505 obstack_free (&reload_obstack, reload_insn_firstobj);
1506 PATTERN (insn) = old_body;
1507 INSN_CODE (insn) = old_code;
1508 REG_NOTES (insn) = old_notes;
1509 something_needs_elimination = 1;
1512 something_needs_operands_changed |= operands_changed;
1514 if (n_reloads != 0)
1516 copy_reloads (chain);
1517 *pprev_reload = chain;
1518 pprev_reload = &chain->next_need_reload;
1522 *pprev_reload = 0;
1525 /* Comparison function for qsort to decide which of two reloads
1526 should be handled first. *P1 and *P2 are the reload numbers. */
1528 static int
1529 reload_reg_class_lower (const void *r1p, const void *r2p)
1531 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1532 int t;
1534 /* Consider required reloads before optional ones. */
1535 t = rld[r1].optional - rld[r2].optional;
1536 if (t != 0)
1537 return t;
1539 /* Count all solitary classes before non-solitary ones. */
1540 t = ((reg_class_size[(int) rld[r2].class] == 1)
1541 - (reg_class_size[(int) rld[r1].class] == 1));
1542 if (t != 0)
1543 return t;
1545 /* Aside from solitaires, consider all multi-reg groups first. */
1546 t = rld[r2].nregs - rld[r1].nregs;
1547 if (t != 0)
1548 return t;
1550 /* Consider reloads in order of increasing reg-class number. */
1551 t = (int) rld[r1].class - (int) rld[r2].class;
1552 if (t != 0)
1553 return t;
1555 /* If reloads are equally urgent, sort by reload number,
1556 so that the results of qsort leave nothing to chance. */
1557 return r1 - r2;
1560 /* The cost of spilling each hard reg. */
1561 static int spill_cost[FIRST_PSEUDO_REGISTER];
1563 /* When spilling multiple hard registers, we use SPILL_COST for the first
1564 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1565 only the first hard reg for a multi-reg pseudo. */
1566 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1568 /* Update the spill cost arrays, considering that pseudo REG is live. */
1570 static void
1571 count_pseudo (int reg)
1573 int freq = REG_FREQ (reg);
1574 int r = reg_renumber[reg];
1575 int nregs;
1577 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1578 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1579 return;
1581 SET_REGNO_REG_SET (&pseudos_counted, reg);
1583 if (r < 0)
1584 abort ();
1586 spill_add_cost[r] += freq;
1588 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1589 while (nregs-- > 0)
1590 spill_cost[r + nregs] += freq;
1593 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1594 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1596 static void
1597 order_regs_for_reload (struct insn_chain *chain)
1599 int i;
1600 HARD_REG_SET used_by_pseudos;
1601 HARD_REG_SET used_by_pseudos2;
1603 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1605 memset (spill_cost, 0, sizeof spill_cost);
1606 memset (spill_add_cost, 0, sizeof spill_add_cost);
1608 /* Count number of uses of each hard reg by pseudo regs allocated to it
1609 and then order them by decreasing use. First exclude hard registers
1610 that are live in or across this insn. */
1612 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1613 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1614 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1615 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1617 /* Now find out which pseudos are allocated to it, and update
1618 hard_reg_n_uses. */
1619 CLEAR_REG_SET (&pseudos_counted);
1621 EXECUTE_IF_SET_IN_REG_SET
1622 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
1624 count_pseudo (i);
1626 EXECUTE_IF_SET_IN_REG_SET
1627 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
1629 count_pseudo (i);
1631 CLEAR_REG_SET (&pseudos_counted);
1634 /* Vector of reload-numbers showing the order in which the reloads should
1635 be processed. */
1636 static short reload_order[MAX_RELOADS];
1638 /* This is used to keep track of the spill regs used in one insn. */
1639 static HARD_REG_SET used_spill_regs_local;
1641 /* We decided to spill hard register SPILLED, which has a size of
1642 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1643 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1644 update SPILL_COST/SPILL_ADD_COST. */
1646 static void
1647 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1649 int r = reg_renumber[reg];
1650 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1652 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1653 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1654 return;
1656 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1658 spill_add_cost[r] -= REG_FREQ (reg);
1659 while (nregs-- > 0)
1660 spill_cost[r + nregs] -= REG_FREQ (reg);
1663 /* Find reload register to use for reload number ORDER. */
1665 static int
1666 find_reg (struct insn_chain *chain, int order)
1668 int rnum = reload_order[order];
1669 struct reload *rl = rld + rnum;
1670 int best_cost = INT_MAX;
1671 int best_reg = -1;
1672 unsigned int i, j;
1673 int k;
1674 HARD_REG_SET not_usable;
1675 HARD_REG_SET used_by_other_reload;
1677 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1678 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1679 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1681 CLEAR_HARD_REG_SET (used_by_other_reload);
1682 for (k = 0; k < order; k++)
1684 int other = reload_order[k];
1686 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1687 for (j = 0; j < rld[other].nregs; j++)
1688 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1691 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1693 unsigned int regno = i;
1695 if (! TEST_HARD_REG_BIT (not_usable, regno)
1696 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1697 && HARD_REGNO_MODE_OK (regno, rl->mode))
1699 int this_cost = spill_cost[regno];
1700 int ok = 1;
1701 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1703 for (j = 1; j < this_nregs; j++)
1705 this_cost += spill_add_cost[regno + j];
1706 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1707 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1708 ok = 0;
1710 if (! ok)
1711 continue;
1712 if (rl->in && GET_CODE (rl->in) == REG && REGNO (rl->in) == regno)
1713 this_cost--;
1714 if (rl->out && GET_CODE (rl->out) == REG && REGNO (rl->out) == regno)
1715 this_cost--;
1716 if (this_cost < best_cost
1717 /* Among registers with equal cost, prefer caller-saved ones, or
1718 use REG_ALLOC_ORDER if it is defined. */
1719 || (this_cost == best_cost
1720 #ifdef REG_ALLOC_ORDER
1721 && (inv_reg_alloc_order[regno]
1722 < inv_reg_alloc_order[best_reg])
1723 #else
1724 && call_used_regs[regno]
1725 && ! call_used_regs[best_reg]
1726 #endif
1729 best_reg = regno;
1730 best_cost = this_cost;
1734 if (best_reg == -1)
1735 return 0;
1737 if (dump_file)
1738 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1740 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1741 rl->regno = best_reg;
1743 EXECUTE_IF_SET_IN_REG_SET
1744 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j,
1746 count_spilled_pseudo (best_reg, rl->nregs, j);
1749 EXECUTE_IF_SET_IN_REG_SET
1750 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j,
1752 count_spilled_pseudo (best_reg, rl->nregs, j);
1755 for (i = 0; i < rl->nregs; i++)
1757 if (spill_cost[best_reg + i] != 0
1758 || spill_add_cost[best_reg + i] != 0)
1759 abort ();
1760 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1762 return 1;
1765 /* Find more reload regs to satisfy the remaining need of an insn, which
1766 is given by CHAIN.
1767 Do it by ascending class number, since otherwise a reg
1768 might be spilled for a big class and might fail to count
1769 for a smaller class even though it belongs to that class. */
1771 static void
1772 find_reload_regs (struct insn_chain *chain)
1774 int i;
1776 /* In order to be certain of getting the registers we need,
1777 we must sort the reloads into order of increasing register class.
1778 Then our grabbing of reload registers will parallel the process
1779 that provided the reload registers. */
1780 for (i = 0; i < chain->n_reloads; i++)
1782 /* Show whether this reload already has a hard reg. */
1783 if (chain->rld[i].reg_rtx)
1785 int regno = REGNO (chain->rld[i].reg_rtx);
1786 chain->rld[i].regno = regno;
1787 chain->rld[i].nregs
1788 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1790 else
1791 chain->rld[i].regno = -1;
1792 reload_order[i] = i;
1795 n_reloads = chain->n_reloads;
1796 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1798 CLEAR_HARD_REG_SET (used_spill_regs_local);
1800 if (dump_file)
1801 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1803 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1805 /* Compute the order of preference for hard registers to spill. */
1807 order_regs_for_reload (chain);
1809 for (i = 0; i < n_reloads; i++)
1811 int r = reload_order[i];
1813 /* Ignore reloads that got marked inoperative. */
1814 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1815 && ! rld[r].optional
1816 && rld[r].regno == -1)
1817 if (! find_reg (chain, i))
1819 spill_failure (chain->insn, rld[r].class);
1820 failure = 1;
1821 return;
1825 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1826 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1828 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1831 static void
1832 select_reload_regs (void)
1834 struct insn_chain *chain;
1836 /* Try to satisfy the needs for each insn. */
1837 for (chain = insns_need_reload; chain != 0;
1838 chain = chain->next_need_reload)
1839 find_reload_regs (chain);
1842 /* Delete all insns that were inserted by emit_caller_save_insns during
1843 this iteration. */
1844 static void
1845 delete_caller_save_insns (void)
1847 struct insn_chain *c = reload_insn_chain;
1849 while (c != 0)
1851 while (c != 0 && c->is_caller_save_insn)
1853 struct insn_chain *next = c->next;
1854 rtx insn = c->insn;
1856 if (c == reload_insn_chain)
1857 reload_insn_chain = next;
1858 delete_insn (insn);
1860 if (next)
1861 next->prev = c->prev;
1862 if (c->prev)
1863 c->prev->next = next;
1864 c->next = unused_insn_chains;
1865 unused_insn_chains = c;
1866 c = next;
1868 if (c != 0)
1869 c = c->next;
1873 /* Handle the failure to find a register to spill.
1874 INSN should be one of the insns which needed this particular spill reg. */
1876 static void
1877 spill_failure (rtx insn, enum reg_class class)
1879 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1880 if (asm_noperands (PATTERN (insn)) >= 0)
1881 error_for_asm (insn, "can't find a register in class `%s' while reloading `asm'",
1882 reg_class_names[class]);
1883 else
1885 error ("unable to find a register to spill in class `%s'",
1886 reg_class_names[class]);
1887 fatal_insn ("this is the insn:", insn);
1891 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1892 data that is dead in INSN. */
1894 static void
1895 delete_dead_insn (rtx insn)
1897 rtx prev = prev_real_insn (insn);
1898 rtx prev_dest;
1900 /* If the previous insn sets a register that dies in our insn, delete it
1901 too. */
1902 if (prev && GET_CODE (PATTERN (prev)) == SET
1903 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
1904 && reg_mentioned_p (prev_dest, PATTERN (insn))
1905 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1906 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1907 delete_dead_insn (prev);
1909 PUT_CODE (insn, NOTE);
1910 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1911 NOTE_SOURCE_FILE (insn) = 0;
1914 /* Modify the home of pseudo-reg I.
1915 The new home is present in reg_renumber[I].
1917 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1918 or it may be -1, meaning there is none or it is not relevant.
1919 This is used so that all pseudos spilled from a given hard reg
1920 can share one stack slot. */
1922 static void
1923 alter_reg (int i, int from_reg)
1925 /* When outputting an inline function, this can happen
1926 for a reg that isn't actually used. */
1927 if (regno_reg_rtx[i] == 0)
1928 return;
1930 /* If the reg got changed to a MEM at rtl-generation time,
1931 ignore it. */
1932 if (GET_CODE (regno_reg_rtx[i]) != REG)
1933 return;
1935 /* Modify the reg-rtx to contain the new hard reg
1936 number or else to contain its pseudo reg number. */
1937 REGNO (regno_reg_rtx[i])
1938 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1940 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1941 allocate a stack slot for it. */
1943 if (reg_renumber[i] < 0
1944 && REG_N_REFS (i) > 0
1945 && reg_equiv_constant[i] == 0
1946 && reg_equiv_memory_loc[i] == 0)
1948 rtx x;
1949 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1950 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
1951 int adjust = 0;
1953 /* Each pseudo reg has an inherent size which comes from its own mode,
1954 and a total size which provides room for paradoxical subregs
1955 which refer to the pseudo reg in wider modes.
1957 We can use a slot already allocated if it provides both
1958 enough inherent space and enough total space.
1959 Otherwise, we allocate a new slot, making sure that it has no less
1960 inherent space, and no less total space, then the previous slot. */
1961 if (from_reg == -1)
1963 /* No known place to spill from => no slot to reuse. */
1964 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1965 inherent_size == total_size ? 0 : -1);
1966 if (BYTES_BIG_ENDIAN)
1967 /* Cancel the big-endian correction done in assign_stack_local.
1968 Get the address of the beginning of the slot.
1969 This is so we can do a big-endian correction unconditionally
1970 below. */
1971 adjust = inherent_size - total_size;
1973 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
1975 /* Nothing can alias this slot except this pseudo. */
1976 set_mem_alias_set (x, new_alias_set ());
1979 /* Reuse a stack slot if possible. */
1980 else if (spill_stack_slot[from_reg] != 0
1981 && spill_stack_slot_width[from_reg] >= total_size
1982 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1983 >= inherent_size))
1984 x = spill_stack_slot[from_reg];
1986 /* Allocate a bigger slot. */
1987 else
1989 /* Compute maximum size needed, both for inherent size
1990 and for total size. */
1991 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
1992 rtx stack_slot;
1994 if (spill_stack_slot[from_reg])
1996 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1997 > inherent_size)
1998 mode = GET_MODE (spill_stack_slot[from_reg]);
1999 if (spill_stack_slot_width[from_reg] > total_size)
2000 total_size = spill_stack_slot_width[from_reg];
2003 /* Make a slot with that size. */
2004 x = assign_stack_local (mode, total_size,
2005 inherent_size == total_size ? 0 : -1);
2006 stack_slot = x;
2008 /* All pseudos mapped to this slot can alias each other. */
2009 if (spill_stack_slot[from_reg])
2010 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
2011 else
2012 set_mem_alias_set (x, new_alias_set ());
2014 if (BYTES_BIG_ENDIAN)
2016 /* Cancel the big-endian correction done in assign_stack_local.
2017 Get the address of the beginning of the slot.
2018 This is so we can do a big-endian correction unconditionally
2019 below. */
2020 adjust = GET_MODE_SIZE (mode) - total_size;
2021 if (adjust)
2022 stack_slot
2023 = adjust_address_nv (x, mode_for_size (total_size
2024 * BITS_PER_UNIT,
2025 MODE_INT, 1),
2026 adjust);
2029 spill_stack_slot[from_reg] = stack_slot;
2030 spill_stack_slot_width[from_reg] = total_size;
2033 /* On a big endian machine, the "address" of the slot
2034 is the address of the low part that fits its inherent mode. */
2035 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2036 adjust += (total_size - inherent_size);
2038 /* If we have any adjustment to make, or if the stack slot is the
2039 wrong mode, make a new stack slot. */
2040 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2042 /* If we have a decl for the original register, set it for the
2043 memory. If this is a shared MEM, make a copy. */
2044 if (REG_EXPR (regno_reg_rtx[i])
2045 && TREE_CODE_CLASS (TREE_CODE (REG_EXPR (regno_reg_rtx[i]))) == 'd')
2047 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2049 /* We can do this only for the DECLs home pseudo, not for
2050 any copies of it, since otherwise when the stack slot
2051 is reused, nonoverlapping_memrefs_p might think they
2052 cannot overlap. */
2053 if (decl && GET_CODE (decl) == REG && REGNO (decl) == (unsigned) i)
2055 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2056 x = copy_rtx (x);
2058 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2062 /* Save the stack slot for later. */
2063 reg_equiv_memory_loc[i] = x;
2067 /* Mark the slots in regs_ever_live for the hard regs
2068 used by pseudo-reg number REGNO. */
2070 void
2071 mark_home_live (int regno)
2073 int i, lim;
2075 i = reg_renumber[regno];
2076 if (i < 0)
2077 return;
2078 lim = i + hard_regno_nregs[i][PSEUDO_REGNO_MODE (regno)];
2079 while (i < lim)
2080 regs_ever_live[i++] = 1;
2083 /* This function handles the tracking of elimination offsets around branches.
2085 X is a piece of RTL being scanned.
2087 INSN is the insn that it came from, if any.
2089 INITIAL_P is nonzero if we are to set the offset to be the initial
2090 offset and zero if we are setting the offset of the label to be the
2091 current offset. */
2093 static void
2094 set_label_offsets (rtx x, rtx insn, int initial_p)
2096 enum rtx_code code = GET_CODE (x);
2097 rtx tem;
2098 unsigned int i;
2099 struct elim_table *p;
2101 switch (code)
2103 case LABEL_REF:
2104 if (LABEL_REF_NONLOCAL_P (x))
2105 return;
2107 x = XEXP (x, 0);
2109 /* ... fall through ... */
2111 case CODE_LABEL:
2112 /* If we know nothing about this label, set the desired offsets. Note
2113 that this sets the offset at a label to be the offset before a label
2114 if we don't know anything about the label. This is not correct for
2115 the label after a BARRIER, but is the best guess we can make. If
2116 we guessed wrong, we will suppress an elimination that might have
2117 been possible had we been able to guess correctly. */
2119 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2121 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2122 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2123 = (initial_p ? reg_eliminate[i].initial_offset
2124 : reg_eliminate[i].offset);
2125 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2128 /* Otherwise, if this is the definition of a label and it is
2129 preceded by a BARRIER, set our offsets to the known offset of
2130 that label. */
2132 else if (x == insn
2133 && (tem = prev_nonnote_insn (insn)) != 0
2134 && GET_CODE (tem) == BARRIER)
2135 set_offsets_for_label (insn);
2136 else
2137 /* If neither of the above cases is true, compare each offset
2138 with those previously recorded and suppress any eliminations
2139 where the offsets disagree. */
2141 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2142 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2143 != (initial_p ? reg_eliminate[i].initial_offset
2144 : reg_eliminate[i].offset))
2145 reg_eliminate[i].can_eliminate = 0;
2147 return;
2149 case JUMP_INSN:
2150 set_label_offsets (PATTERN (insn), insn, initial_p);
2152 /* ... fall through ... */
2154 case INSN:
2155 case CALL_INSN:
2156 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2157 and hence must have all eliminations at their initial offsets. */
2158 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2159 if (REG_NOTE_KIND (tem) == REG_LABEL)
2160 set_label_offsets (XEXP (tem, 0), insn, 1);
2161 return;
2163 case PARALLEL:
2164 case ADDR_VEC:
2165 case ADDR_DIFF_VEC:
2166 /* Each of the labels in the parallel or address vector must be
2167 at their initial offsets. We want the first field for PARALLEL
2168 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2170 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2171 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2172 insn, initial_p);
2173 return;
2175 case SET:
2176 /* We only care about setting PC. If the source is not RETURN,
2177 IF_THEN_ELSE, or a label, disable any eliminations not at
2178 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2179 isn't one of those possibilities. For branches to a label,
2180 call ourselves recursively.
2182 Note that this can disable elimination unnecessarily when we have
2183 a non-local goto since it will look like a non-constant jump to
2184 someplace in the current function. This isn't a significant
2185 problem since such jumps will normally be when all elimination
2186 pairs are back to their initial offsets. */
2188 if (SET_DEST (x) != pc_rtx)
2189 return;
2191 switch (GET_CODE (SET_SRC (x)))
2193 case PC:
2194 case RETURN:
2195 return;
2197 case LABEL_REF:
2198 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2199 return;
2201 case IF_THEN_ELSE:
2202 tem = XEXP (SET_SRC (x), 1);
2203 if (GET_CODE (tem) == LABEL_REF)
2204 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2205 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2206 break;
2208 tem = XEXP (SET_SRC (x), 2);
2209 if (GET_CODE (tem) == LABEL_REF)
2210 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2211 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2212 break;
2213 return;
2215 default:
2216 break;
2219 /* If we reach here, all eliminations must be at their initial
2220 offset because we are doing a jump to a variable address. */
2221 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2222 if (p->offset != p->initial_offset)
2223 p->can_eliminate = 0;
2224 break;
2226 default:
2227 break;
2231 /* Scan X and replace any eliminable registers (such as fp) with a
2232 replacement (such as sp), plus an offset.
2234 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2235 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2236 MEM, we are allowed to replace a sum of a register and the constant zero
2237 with the register, which we cannot do outside a MEM. In addition, we need
2238 to record the fact that a register is referenced outside a MEM.
2240 If INSN is an insn, it is the insn containing X. If we replace a REG
2241 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2242 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2243 the REG is being modified.
2245 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2246 That's used when we eliminate in expressions stored in notes.
2247 This means, do not set ref_outside_mem even if the reference
2248 is outside of MEMs.
2250 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2251 replacements done assuming all offsets are at their initial values. If
2252 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2253 encounter, return the actual location so that find_reloads will do
2254 the proper thing. */
2257 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2259 enum rtx_code code = GET_CODE (x);
2260 struct elim_table *ep;
2261 int regno;
2262 rtx new;
2263 int i, j;
2264 const char *fmt;
2265 int copied = 0;
2267 if (! current_function_decl)
2268 return x;
2270 switch (code)
2272 case CONST_INT:
2273 case CONST_DOUBLE:
2274 case CONST_VECTOR:
2275 case CONST:
2276 case SYMBOL_REF:
2277 case CODE_LABEL:
2278 case PC:
2279 case CC0:
2280 case ASM_INPUT:
2281 case ADDR_VEC:
2282 case ADDR_DIFF_VEC:
2283 case RETURN:
2284 return x;
2286 case ADDRESSOF:
2287 /* This is only for the benefit of the debugging backends, which call
2288 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2289 removed after CSE. */
2290 new = eliminate_regs (XEXP (x, 0), 0, insn);
2291 if (GET_CODE (new) == MEM)
2292 return XEXP (new, 0);
2293 return x;
2295 case REG:
2296 regno = REGNO (x);
2298 /* First handle the case where we encounter a bare register that
2299 is eliminable. Replace it with a PLUS. */
2300 if (regno < FIRST_PSEUDO_REGISTER)
2302 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2303 ep++)
2304 if (ep->from_rtx == x && ep->can_eliminate)
2305 return plus_constant (ep->to_rtx, ep->previous_offset);
2308 else if (reg_renumber && reg_renumber[regno] < 0
2309 && reg_equiv_constant && reg_equiv_constant[regno]
2310 && ! CONSTANT_P (reg_equiv_constant[regno]))
2311 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2312 mem_mode, insn);
2313 return x;
2315 /* You might think handling MINUS in a manner similar to PLUS is a
2316 good idea. It is not. It has been tried multiple times and every
2317 time the change has had to have been reverted.
2319 Other parts of reload know a PLUS is special (gen_reload for example)
2320 and require special code to handle code a reloaded PLUS operand.
2322 Also consider backends where the flags register is clobbered by a
2323 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2324 lea instruction comes to mind). If we try to reload a MINUS, we
2325 may kill the flags register that was holding a useful value.
2327 So, please before trying to handle MINUS, consider reload as a
2328 whole instead of this little section as well as the backend issues. */
2329 case PLUS:
2330 /* If this is the sum of an eliminable register and a constant, rework
2331 the sum. */
2332 if (GET_CODE (XEXP (x, 0)) == REG
2333 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2334 && CONSTANT_P (XEXP (x, 1)))
2336 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2337 ep++)
2338 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2340 /* The only time we want to replace a PLUS with a REG (this
2341 occurs when the constant operand of the PLUS is the negative
2342 of the offset) is when we are inside a MEM. We won't want
2343 to do so at other times because that would change the
2344 structure of the insn in a way that reload can't handle.
2345 We special-case the commonest situation in
2346 eliminate_regs_in_insn, so just replace a PLUS with a
2347 PLUS here, unless inside a MEM. */
2348 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2349 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2350 return ep->to_rtx;
2351 else
2352 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2353 plus_constant (XEXP (x, 1),
2354 ep->previous_offset));
2357 /* If the register is not eliminable, we are done since the other
2358 operand is a constant. */
2359 return x;
2362 /* If this is part of an address, we want to bring any constant to the
2363 outermost PLUS. We will do this by doing register replacement in
2364 our operands and seeing if a constant shows up in one of them.
2366 Note that there is no risk of modifying the structure of the insn,
2367 since we only get called for its operands, thus we are either
2368 modifying the address inside a MEM, or something like an address
2369 operand of a load-address insn. */
2372 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2373 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2375 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2377 /* If one side is a PLUS and the other side is a pseudo that
2378 didn't get a hard register but has a reg_equiv_constant,
2379 we must replace the constant here since it may no longer
2380 be in the position of any operand. */
2381 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2382 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2383 && reg_renumber[REGNO (new1)] < 0
2384 && reg_equiv_constant != 0
2385 && reg_equiv_constant[REGNO (new1)] != 0)
2386 new1 = reg_equiv_constant[REGNO (new1)];
2387 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2388 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2389 && reg_renumber[REGNO (new0)] < 0
2390 && reg_equiv_constant[REGNO (new0)] != 0)
2391 new0 = reg_equiv_constant[REGNO (new0)];
2393 new = form_sum (new0, new1);
2395 /* As above, if we are not inside a MEM we do not want to
2396 turn a PLUS into something else. We might try to do so here
2397 for an addition of 0 if we aren't optimizing. */
2398 if (! mem_mode && GET_CODE (new) != PLUS)
2399 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2400 else
2401 return new;
2404 return x;
2406 case MULT:
2407 /* If this is the product of an eliminable register and a
2408 constant, apply the distribute law and move the constant out
2409 so that we have (plus (mult ..) ..). This is needed in order
2410 to keep load-address insns valid. This case is pathological.
2411 We ignore the possibility of overflow here. */
2412 if (GET_CODE (XEXP (x, 0)) == REG
2413 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2414 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2415 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2416 ep++)
2417 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2419 if (! mem_mode
2420 /* Refs inside notes don't count for this purpose. */
2421 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2422 || GET_CODE (insn) == INSN_LIST)))
2423 ep->ref_outside_mem = 1;
2425 return
2426 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2427 ep->previous_offset * INTVAL (XEXP (x, 1)));
2430 /* ... fall through ... */
2432 case CALL:
2433 case COMPARE:
2434 /* See comments before PLUS about handling MINUS. */
2435 case MINUS:
2436 case DIV: case UDIV:
2437 case MOD: case UMOD:
2438 case AND: case IOR: case XOR:
2439 case ROTATERT: case ROTATE:
2440 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2441 case NE: case EQ:
2442 case GE: case GT: case GEU: case GTU:
2443 case LE: case LT: case LEU: case LTU:
2445 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2446 rtx new1
2447 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2449 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2450 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2452 return x;
2454 case EXPR_LIST:
2455 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2456 if (XEXP (x, 0))
2458 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2459 if (new != XEXP (x, 0))
2461 /* If this is a REG_DEAD note, it is not valid anymore.
2462 Using the eliminated version could result in creating a
2463 REG_DEAD note for the stack or frame pointer. */
2464 if (GET_MODE (x) == REG_DEAD)
2465 return (XEXP (x, 1)
2466 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2467 : NULL_RTX);
2469 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2473 /* ... fall through ... */
2475 case INSN_LIST:
2476 /* Now do eliminations in the rest of the chain. If this was
2477 an EXPR_LIST, this might result in allocating more memory than is
2478 strictly needed, but it simplifies the code. */
2479 if (XEXP (x, 1))
2481 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2482 if (new != XEXP (x, 1))
2483 return
2484 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2486 return x;
2488 case PRE_INC:
2489 case POST_INC:
2490 case PRE_DEC:
2491 case POST_DEC:
2492 case STRICT_LOW_PART:
2493 case NEG: case NOT:
2494 case SIGN_EXTEND: case ZERO_EXTEND:
2495 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2496 case FLOAT: case FIX:
2497 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2498 case ABS:
2499 case SQRT:
2500 case FFS:
2501 case CLZ:
2502 case CTZ:
2503 case POPCOUNT:
2504 case PARITY:
2505 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2506 if (new != XEXP (x, 0))
2507 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2508 return x;
2510 case SUBREG:
2511 /* Similar to above processing, but preserve SUBREG_BYTE.
2512 Convert (subreg (mem)) to (mem) if not paradoxical.
2513 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2514 pseudo didn't get a hard reg, we must replace this with the
2515 eliminated version of the memory location because push_reload
2516 may do the replacement in certain circumstances. */
2517 if (GET_CODE (SUBREG_REG (x)) == REG
2518 && (GET_MODE_SIZE (GET_MODE (x))
2519 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2520 && reg_equiv_memory_loc != 0
2521 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2523 new = SUBREG_REG (x);
2525 else
2526 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2528 if (new != SUBREG_REG (x))
2530 int x_size = GET_MODE_SIZE (GET_MODE (x));
2531 int new_size = GET_MODE_SIZE (GET_MODE (new));
2533 if (GET_CODE (new) == MEM
2534 && ((x_size < new_size
2535 #ifdef WORD_REGISTER_OPERATIONS
2536 /* On these machines, combine can create rtl of the form
2537 (set (subreg:m1 (reg:m2 R) 0) ...)
2538 where m1 < m2, and expects something interesting to
2539 happen to the entire word. Moreover, it will use the
2540 (reg:m2 R) later, expecting all bits to be preserved.
2541 So if the number of words is the same, preserve the
2542 subreg so that push_reload can see it. */
2543 && ! ((x_size - 1) / UNITS_PER_WORD
2544 == (new_size -1 ) / UNITS_PER_WORD)
2545 #endif
2547 || x_size == new_size)
2549 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2550 else
2551 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2554 return x;
2556 case MEM:
2557 /* This is only for the benefit of the debugging backends, which call
2558 eliminate_regs on DECL_RTL; any ADDRESSOFs in the actual insns are
2559 removed after CSE. */
2560 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2561 return eliminate_regs (XEXP (XEXP (x, 0), 0), 0, insn);
2563 /* Our only special processing is to pass the mode of the MEM to our
2564 recursive call and copy the flags. While we are here, handle this
2565 case more efficiently. */
2566 return
2567 replace_equiv_address_nv (x,
2568 eliminate_regs (XEXP (x, 0),
2569 GET_MODE (x), insn));
2571 case USE:
2572 /* Handle insn_list USE that a call to a pure function may generate. */
2573 new = eliminate_regs (XEXP (x, 0), 0, insn);
2574 if (new != XEXP (x, 0))
2575 return gen_rtx_USE (GET_MODE (x), new);
2576 return x;
2578 case CLOBBER:
2579 case ASM_OPERANDS:
2580 case SET:
2581 abort ();
2583 default:
2584 break;
2587 /* Process each of our operands recursively. If any have changed, make a
2588 copy of the rtx. */
2589 fmt = GET_RTX_FORMAT (code);
2590 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2592 if (*fmt == 'e')
2594 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
2595 if (new != XEXP (x, i) && ! copied)
2597 rtx new_x = rtx_alloc (code);
2598 memcpy (new_x, x, RTX_SIZE (code));
2599 x = new_x;
2600 copied = 1;
2602 XEXP (x, i) = new;
2604 else if (*fmt == 'E')
2606 int copied_vec = 0;
2607 for (j = 0; j < XVECLEN (x, i); j++)
2609 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2610 if (new != XVECEXP (x, i, j) && ! copied_vec)
2612 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2613 XVEC (x, i)->elem);
2614 if (! copied)
2616 rtx new_x = rtx_alloc (code);
2617 memcpy (new_x, x, RTX_SIZE (code));
2618 x = new_x;
2619 copied = 1;
2621 XVEC (x, i) = new_v;
2622 copied_vec = 1;
2624 XVECEXP (x, i, j) = new;
2629 return x;
2632 /* Scan rtx X for modifications of elimination target registers. Update
2633 the table of eliminables to reflect the changed state. MEM_MODE is
2634 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2636 static void
2637 elimination_effects (rtx x, enum machine_mode mem_mode)
2639 enum rtx_code code = GET_CODE (x);
2640 struct elim_table *ep;
2641 int regno;
2642 int i, j;
2643 const char *fmt;
2645 switch (code)
2647 case CONST_INT:
2648 case CONST_DOUBLE:
2649 case CONST_VECTOR:
2650 case CONST:
2651 case SYMBOL_REF:
2652 case CODE_LABEL:
2653 case PC:
2654 case CC0:
2655 case ASM_INPUT:
2656 case ADDR_VEC:
2657 case ADDR_DIFF_VEC:
2658 case RETURN:
2659 return;
2661 case ADDRESSOF:
2662 abort ();
2664 case REG:
2665 regno = REGNO (x);
2667 /* First handle the case where we encounter a bare register that
2668 is eliminable. Replace it with a PLUS. */
2669 if (regno < FIRST_PSEUDO_REGISTER)
2671 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2672 ep++)
2673 if (ep->from_rtx == x && ep->can_eliminate)
2675 if (! mem_mode)
2676 ep->ref_outside_mem = 1;
2677 return;
2681 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2682 && reg_equiv_constant[regno]
2683 && ! function_invariant_p (reg_equiv_constant[regno]))
2684 elimination_effects (reg_equiv_constant[regno], mem_mode);
2685 return;
2687 case PRE_INC:
2688 case POST_INC:
2689 case PRE_DEC:
2690 case POST_DEC:
2691 case POST_MODIFY:
2692 case PRE_MODIFY:
2693 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2694 if (ep->to_rtx == XEXP (x, 0))
2696 int size = GET_MODE_SIZE (mem_mode);
2698 /* If more bytes than MEM_MODE are pushed, account for them. */
2699 #ifdef PUSH_ROUNDING
2700 if (ep->to_rtx == stack_pointer_rtx)
2701 size = PUSH_ROUNDING (size);
2702 #endif
2703 if (code == PRE_DEC || code == POST_DEC)
2704 ep->offset += size;
2705 else if (code == PRE_INC || code == POST_INC)
2706 ep->offset -= size;
2707 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2708 && GET_CODE (XEXP (x, 1)) == PLUS
2709 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2710 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2711 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2714 /* These two aren't unary operators. */
2715 if (code == POST_MODIFY || code == PRE_MODIFY)
2716 break;
2718 /* Fall through to generic unary operation case. */
2719 case STRICT_LOW_PART:
2720 case NEG: case NOT:
2721 case SIGN_EXTEND: case ZERO_EXTEND:
2722 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2723 case FLOAT: case FIX:
2724 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2725 case ABS:
2726 case SQRT:
2727 case FFS:
2728 case CLZ:
2729 case CTZ:
2730 case POPCOUNT:
2731 case PARITY:
2732 elimination_effects (XEXP (x, 0), mem_mode);
2733 return;
2735 case SUBREG:
2736 if (GET_CODE (SUBREG_REG (x)) == REG
2737 && (GET_MODE_SIZE (GET_MODE (x))
2738 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2739 && reg_equiv_memory_loc != 0
2740 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2741 return;
2743 elimination_effects (SUBREG_REG (x), mem_mode);
2744 return;
2746 case USE:
2747 /* If using a register that is the source of an eliminate we still
2748 think can be performed, note it cannot be performed since we don't
2749 know how this register is used. */
2750 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2751 if (ep->from_rtx == XEXP (x, 0))
2752 ep->can_eliminate = 0;
2754 elimination_effects (XEXP (x, 0), mem_mode);
2755 return;
2757 case CLOBBER:
2758 /* If clobbering a register that is the replacement register for an
2759 elimination we still think can be performed, note that it cannot
2760 be performed. Otherwise, we need not be concerned about it. */
2761 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2762 if (ep->to_rtx == XEXP (x, 0))
2763 ep->can_eliminate = 0;
2765 elimination_effects (XEXP (x, 0), mem_mode);
2766 return;
2768 case SET:
2769 /* Check for setting a register that we know about. */
2770 if (GET_CODE (SET_DEST (x)) == REG)
2772 /* See if this is setting the replacement register for an
2773 elimination.
2775 If DEST is the hard frame pointer, we do nothing because we
2776 assume that all assignments to the frame pointer are for
2777 non-local gotos and are being done at a time when they are valid
2778 and do not disturb anything else. Some machines want to
2779 eliminate a fake argument pointer (or even a fake frame pointer)
2780 with either the real frame or the stack pointer. Assignments to
2781 the hard frame pointer must not prevent this elimination. */
2783 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2784 ep++)
2785 if (ep->to_rtx == SET_DEST (x)
2786 && SET_DEST (x) != hard_frame_pointer_rtx)
2788 /* If it is being incremented, adjust the offset. Otherwise,
2789 this elimination can't be done. */
2790 rtx src = SET_SRC (x);
2792 if (GET_CODE (src) == PLUS
2793 && XEXP (src, 0) == SET_DEST (x)
2794 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2795 ep->offset -= INTVAL (XEXP (src, 1));
2796 else
2797 ep->can_eliminate = 0;
2801 elimination_effects (SET_DEST (x), 0);
2802 elimination_effects (SET_SRC (x), 0);
2803 return;
2805 case MEM:
2806 if (GET_CODE (XEXP (x, 0)) == ADDRESSOF)
2807 abort ();
2809 /* Our only special processing is to pass the mode of the MEM to our
2810 recursive call. */
2811 elimination_effects (XEXP (x, 0), GET_MODE (x));
2812 return;
2814 default:
2815 break;
2818 fmt = GET_RTX_FORMAT (code);
2819 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2821 if (*fmt == 'e')
2822 elimination_effects (XEXP (x, i), mem_mode);
2823 else if (*fmt == 'E')
2824 for (j = 0; j < XVECLEN (x, i); j++)
2825 elimination_effects (XVECEXP (x, i, j), mem_mode);
2829 /* Descend through rtx X and verify that no references to eliminable registers
2830 remain. If any do remain, mark the involved register as not
2831 eliminable. */
2833 static void
2834 check_eliminable_occurrences (rtx x)
2836 const char *fmt;
2837 int i;
2838 enum rtx_code code;
2840 if (x == 0)
2841 return;
2843 code = GET_CODE (x);
2845 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2847 struct elim_table *ep;
2849 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2850 if (ep->from_rtx == x)
2851 ep->can_eliminate = 0;
2852 return;
2855 fmt = GET_RTX_FORMAT (code);
2856 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2858 if (*fmt == 'e')
2859 check_eliminable_occurrences (XEXP (x, i));
2860 else if (*fmt == 'E')
2862 int j;
2863 for (j = 0; j < XVECLEN (x, i); j++)
2864 check_eliminable_occurrences (XVECEXP (x, i, j));
2869 /* Scan INSN and eliminate all eliminable registers in it.
2871 If REPLACE is nonzero, do the replacement destructively. Also
2872 delete the insn as dead it if it is setting an eliminable register.
2874 If REPLACE is zero, do all our allocations in reload_obstack.
2876 If no eliminations were done and this insn doesn't require any elimination
2877 processing (these are not identical conditions: it might be updating sp,
2878 but not referencing fp; this needs to be seen during reload_as_needed so
2879 that the offset between fp and sp can be taken into consideration), zero
2880 is returned. Otherwise, 1 is returned. */
2882 static int
2883 eliminate_regs_in_insn (rtx insn, int replace)
2885 int icode = recog_memoized (insn);
2886 rtx old_body = PATTERN (insn);
2887 int insn_is_asm = asm_noperands (old_body) >= 0;
2888 rtx old_set = single_set (insn);
2889 rtx new_body;
2890 int val = 0;
2891 int i;
2892 rtx substed_operand[MAX_RECOG_OPERANDS];
2893 rtx orig_operand[MAX_RECOG_OPERANDS];
2894 struct elim_table *ep;
2895 rtx plus_src;
2897 if (! insn_is_asm && icode < 0)
2899 if (GET_CODE (PATTERN (insn)) == USE
2900 || GET_CODE (PATTERN (insn)) == CLOBBER
2901 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2902 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2903 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
2904 return 0;
2905 abort ();
2908 if (old_set != 0 && GET_CODE (SET_DEST (old_set)) == REG
2909 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2911 /* Check for setting an eliminable register. */
2912 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2913 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2915 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2916 /* If this is setting the frame pointer register to the
2917 hardware frame pointer register and this is an elimination
2918 that will be done (tested above), this insn is really
2919 adjusting the frame pointer downward to compensate for
2920 the adjustment done before a nonlocal goto. */
2921 if (ep->from == FRAME_POINTER_REGNUM
2922 && ep->to == HARD_FRAME_POINTER_REGNUM)
2924 rtx base = SET_SRC (old_set);
2925 rtx base_insn = insn;
2926 HOST_WIDE_INT offset = 0;
2928 while (base != ep->to_rtx)
2930 rtx prev_insn, prev_set;
2932 if (GET_CODE (base) == PLUS
2933 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2935 offset += INTVAL (XEXP (base, 1));
2936 base = XEXP (base, 0);
2938 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2939 && (prev_set = single_set (prev_insn)) != 0
2940 && rtx_equal_p (SET_DEST (prev_set), base))
2942 base = SET_SRC (prev_set);
2943 base_insn = prev_insn;
2945 else
2946 break;
2949 if (base == ep->to_rtx)
2951 rtx src
2952 = plus_constant (ep->to_rtx, offset - ep->offset);
2954 new_body = old_body;
2955 if (! replace)
2957 new_body = copy_insn (old_body);
2958 if (REG_NOTES (insn))
2959 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
2961 PATTERN (insn) = new_body;
2962 old_set = single_set (insn);
2964 /* First see if this insn remains valid when we
2965 make the change. If not, keep the INSN_CODE
2966 the same and let reload fit it up. */
2967 validate_change (insn, &SET_SRC (old_set), src, 1);
2968 validate_change (insn, &SET_DEST (old_set),
2969 ep->to_rtx, 1);
2970 if (! apply_change_group ())
2972 SET_SRC (old_set) = src;
2973 SET_DEST (old_set) = ep->to_rtx;
2976 val = 1;
2977 goto done;
2980 #endif
2982 /* In this case this insn isn't serving a useful purpose. We
2983 will delete it in reload_as_needed once we know that this
2984 elimination is, in fact, being done.
2986 If REPLACE isn't set, we can't delete this insn, but needn't
2987 process it since it won't be used unless something changes. */
2988 if (replace)
2990 delete_dead_insn (insn);
2991 return 1;
2993 val = 1;
2994 goto done;
2998 /* We allow one special case which happens to work on all machines we
2999 currently support: a single set with the source or a REG_EQUAL
3000 note being a PLUS of an eliminable register and a constant. */
3001 plus_src = 0;
3002 if (old_set && GET_CODE (SET_DEST (old_set)) == REG)
3004 /* First see if the source is of the form (plus (reg) CST). */
3005 if (GET_CODE (SET_SRC (old_set)) == PLUS
3006 && GET_CODE (XEXP (SET_SRC (old_set), 0)) == REG
3007 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT
3008 && REGNO (XEXP (SET_SRC (old_set), 0)) < FIRST_PSEUDO_REGISTER)
3009 plus_src = SET_SRC (old_set);
3010 else if (GET_CODE (SET_SRC (old_set)) == REG)
3012 /* Otherwise, see if we have a REG_EQUAL note of the form
3013 (plus (reg) CST). */
3014 rtx links;
3015 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3017 if (REG_NOTE_KIND (links) == REG_EQUAL
3018 && GET_CODE (XEXP (links, 0)) == PLUS
3019 && GET_CODE (XEXP (XEXP (links, 0), 0)) == REG
3020 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT
3021 && REGNO (XEXP (XEXP (links, 0), 0)) < FIRST_PSEUDO_REGISTER)
3023 plus_src = XEXP (links, 0);
3024 break;
3029 if (plus_src)
3031 rtx reg = XEXP (plus_src, 0);
3032 HOST_WIDE_INT offset = INTVAL (XEXP (plus_src, 1));
3034 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3035 if (ep->from_rtx == reg && ep->can_eliminate)
3037 offset += ep->offset;
3039 if (offset == 0)
3041 int num_clobbers;
3042 /* We assume here that if we need a PARALLEL with
3043 CLOBBERs for this assignment, we can do with the
3044 MATCH_SCRATCHes that add_clobbers allocates.
3045 There's not much we can do if that doesn't work. */
3046 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3047 SET_DEST (old_set),
3048 ep->to_rtx);
3049 num_clobbers = 0;
3050 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3051 if (num_clobbers)
3053 rtvec vec = rtvec_alloc (num_clobbers + 1);
3055 vec->elem[0] = PATTERN (insn);
3056 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3057 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3059 if (INSN_CODE (insn) < 0)
3060 abort ();
3062 /* If we have a nonzero offset, and the source is already
3063 a simple REG, the following transformation would
3064 increase the cost of the insn by replacing a simple REG
3065 with (plus (reg sp) CST). So try only when plus_src
3066 comes from old_set proper, not REG_NOTES. */
3067 else if (SET_SRC (old_set) == plus_src)
3069 new_body = old_body;
3070 if (! replace)
3072 new_body = copy_insn (old_body);
3073 if (REG_NOTES (insn))
3074 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3076 PATTERN (insn) = new_body;
3077 old_set = single_set (insn);
3079 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3080 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3082 else
3083 break;
3085 val = 1;
3086 /* This can't have an effect on elimination offsets, so skip right
3087 to the end. */
3088 goto done;
3092 /* Determine the effects of this insn on elimination offsets. */
3093 elimination_effects (old_body, 0);
3095 /* Eliminate all eliminable registers occurring in operands that
3096 can be handled by reload. */
3097 extract_insn (insn);
3098 for (i = 0; i < recog_data.n_operands; i++)
3100 orig_operand[i] = recog_data.operand[i];
3101 substed_operand[i] = recog_data.operand[i];
3103 /* For an asm statement, every operand is eliminable. */
3104 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3106 /* Check for setting a register that we know about. */
3107 if (recog_data.operand_type[i] != OP_IN
3108 && GET_CODE (orig_operand[i]) == REG)
3110 /* If we are assigning to a register that can be eliminated, it
3111 must be as part of a PARALLEL, since the code above handles
3112 single SETs. We must indicate that we can no longer
3113 eliminate this reg. */
3114 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3115 ep++)
3116 if (ep->from_rtx == orig_operand[i])
3117 ep->can_eliminate = 0;
3120 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3121 replace ? insn : NULL_RTX);
3122 if (substed_operand[i] != orig_operand[i])
3123 val = 1;
3124 /* Terminate the search in check_eliminable_occurrences at
3125 this point. */
3126 *recog_data.operand_loc[i] = 0;
3128 /* If an output operand changed from a REG to a MEM and INSN is an
3129 insn, write a CLOBBER insn. */
3130 if (recog_data.operand_type[i] != OP_IN
3131 && GET_CODE (orig_operand[i]) == REG
3132 && GET_CODE (substed_operand[i]) == MEM
3133 && replace)
3134 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3135 insn);
3139 for (i = 0; i < recog_data.n_dups; i++)
3140 *recog_data.dup_loc[i]
3141 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3143 /* If any eliminable remain, they aren't eliminable anymore. */
3144 check_eliminable_occurrences (old_body);
3146 /* Substitute the operands; the new values are in the substed_operand
3147 array. */
3148 for (i = 0; i < recog_data.n_operands; i++)
3149 *recog_data.operand_loc[i] = substed_operand[i];
3150 for (i = 0; i < recog_data.n_dups; i++)
3151 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3153 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3154 re-recognize the insn. We do this in case we had a simple addition
3155 but now can do this as a load-address. This saves an insn in this
3156 common case.
3157 If re-recognition fails, the old insn code number will still be used,
3158 and some register operands may have changed into PLUS expressions.
3159 These will be handled by find_reloads by loading them into a register
3160 again. */
3162 if (val)
3164 /* If we aren't replacing things permanently and we changed something,
3165 make another copy to ensure that all the RTL is new. Otherwise
3166 things can go wrong if find_reload swaps commutative operands
3167 and one is inside RTL that has been copied while the other is not. */
3168 new_body = old_body;
3169 if (! replace)
3171 new_body = copy_insn (old_body);
3172 if (REG_NOTES (insn))
3173 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3175 PATTERN (insn) = new_body;
3177 /* If we had a move insn but now we don't, rerecognize it. This will
3178 cause spurious re-recognition if the old move had a PARALLEL since
3179 the new one still will, but we can't call single_set without
3180 having put NEW_BODY into the insn and the re-recognition won't
3181 hurt in this rare case. */
3182 /* ??? Why this huge if statement - why don't we just rerecognize the
3183 thing always? */
3184 if (! insn_is_asm
3185 && old_set != 0
3186 && ((GET_CODE (SET_SRC (old_set)) == REG
3187 && (GET_CODE (new_body) != SET
3188 || GET_CODE (SET_SRC (new_body)) != REG))
3189 /* If this was a load from or store to memory, compare
3190 the MEM in recog_data.operand to the one in the insn.
3191 If they are not equal, then rerecognize the insn. */
3192 || (old_set != 0
3193 && ((GET_CODE (SET_SRC (old_set)) == MEM
3194 && SET_SRC (old_set) != recog_data.operand[1])
3195 || (GET_CODE (SET_DEST (old_set)) == MEM
3196 && SET_DEST (old_set) != recog_data.operand[0])))
3197 /* If this was an add insn before, rerecognize. */
3198 || GET_CODE (SET_SRC (old_set)) == PLUS))
3200 int new_icode = recog (PATTERN (insn), insn, 0);
3201 if (new_icode < 0)
3202 INSN_CODE (insn) = icode;
3206 /* Restore the old body. If there were any changes to it, we made a copy
3207 of it while the changes were still in place, so we'll correctly return
3208 a modified insn below. */
3209 if (! replace)
3211 /* Restore the old body. */
3212 for (i = 0; i < recog_data.n_operands; i++)
3213 *recog_data.operand_loc[i] = orig_operand[i];
3214 for (i = 0; i < recog_data.n_dups; i++)
3215 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3218 /* Update all elimination pairs to reflect the status after the current
3219 insn. The changes we make were determined by the earlier call to
3220 elimination_effects.
3222 We also detect cases where register elimination cannot be done,
3223 namely, if a register would be both changed and referenced outside a MEM
3224 in the resulting insn since such an insn is often undefined and, even if
3225 not, we cannot know what meaning will be given to it. Note that it is
3226 valid to have a register used in an address in an insn that changes it
3227 (presumably with a pre- or post-increment or decrement).
3229 If anything changes, return nonzero. */
3231 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3233 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3234 ep->can_eliminate = 0;
3236 ep->ref_outside_mem = 0;
3238 if (ep->previous_offset != ep->offset)
3239 val = 1;
3242 done:
3243 /* If we changed something, perform elimination in REG_NOTES. This is
3244 needed even when REPLACE is zero because a REG_DEAD note might refer
3245 to a register that we eliminate and could cause a different number
3246 of spill registers to be needed in the final reload pass than in
3247 the pre-passes. */
3248 if (val && REG_NOTES (insn) != 0)
3249 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3251 return val;
3254 /* Loop through all elimination pairs.
3255 Recalculate the number not at initial offset.
3257 Compute the maximum offset (minimum offset if the stack does not
3258 grow downward) for each elimination pair. */
3260 static void
3261 update_eliminable_offsets (void)
3263 struct elim_table *ep;
3265 num_not_at_initial_offset = 0;
3266 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3268 ep->previous_offset = ep->offset;
3269 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3270 num_not_at_initial_offset++;
3274 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3275 replacement we currently believe is valid, mark it as not eliminable if X
3276 modifies DEST in any way other than by adding a constant integer to it.
3278 If DEST is the frame pointer, we do nothing because we assume that
3279 all assignments to the hard frame pointer are nonlocal gotos and are being
3280 done at a time when they are valid and do not disturb anything else.
3281 Some machines want to eliminate a fake argument pointer with either the
3282 frame or stack pointer. Assignments to the hard frame pointer must not
3283 prevent this elimination.
3285 Called via note_stores from reload before starting its passes to scan
3286 the insns of the function. */
3288 static void
3289 mark_not_eliminable (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
3291 unsigned int i;
3293 /* A SUBREG of a hard register here is just changing its mode. We should
3294 not see a SUBREG of an eliminable hard register, but check just in
3295 case. */
3296 if (GET_CODE (dest) == SUBREG)
3297 dest = SUBREG_REG (dest);
3299 if (dest == hard_frame_pointer_rtx)
3300 return;
3302 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3303 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3304 && (GET_CODE (x) != SET
3305 || GET_CODE (SET_SRC (x)) != PLUS
3306 || XEXP (SET_SRC (x), 0) != dest
3307 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3309 reg_eliminate[i].can_eliminate_previous
3310 = reg_eliminate[i].can_eliminate = 0;
3311 num_eliminable--;
3315 /* Verify that the initial elimination offsets did not change since the
3316 last call to set_initial_elim_offsets. This is used to catch cases
3317 where something illegal happened during reload_as_needed that could
3318 cause incorrect code to be generated if we did not check for it. */
3320 static void
3321 verify_initial_elim_offsets (void)
3323 HOST_WIDE_INT t;
3325 #ifdef ELIMINABLE_REGS
3326 struct elim_table *ep;
3328 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3330 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3331 if (t != ep->initial_offset)
3332 abort ();
3334 #else
3335 INITIAL_FRAME_POINTER_OFFSET (t);
3336 if (t != reg_eliminate[0].initial_offset)
3337 abort ();
3338 #endif
3341 /* Reset all offsets on eliminable registers to their initial values. */
3343 static void
3344 set_initial_elim_offsets (void)
3346 struct elim_table *ep = reg_eliminate;
3348 #ifdef ELIMINABLE_REGS
3349 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3351 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3352 ep->previous_offset = ep->offset = ep->initial_offset;
3354 #else
3355 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3356 ep->previous_offset = ep->offset = ep->initial_offset;
3357 #endif
3359 num_not_at_initial_offset = 0;
3362 /* Initialize the known label offsets.
3363 Set a known offset for each forced label to be at the initial offset
3364 of each elimination. We do this because we assume that all
3365 computed jumps occur from a location where each elimination is
3366 at its initial offset.
3367 For all other labels, show that we don't know the offsets. */
3369 static void
3370 set_initial_label_offsets (void)
3372 rtx x;
3373 memset (offsets_known_at, 0, num_labels);
3375 for (x = forced_labels; x; x = XEXP (x, 1))
3376 if (XEXP (x, 0))
3377 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3380 /* Set all elimination offsets to the known values for the code label given
3381 by INSN. */
3383 static void
3384 set_offsets_for_label (rtx insn)
3386 unsigned int i;
3387 int label_nr = CODE_LABEL_NUMBER (insn);
3388 struct elim_table *ep;
3390 num_not_at_initial_offset = 0;
3391 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3393 ep->offset = ep->previous_offset
3394 = offsets_at[label_nr - first_label_num][i];
3395 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3396 num_not_at_initial_offset++;
3400 /* See if anything that happened changes which eliminations are valid.
3401 For example, on the SPARC, whether or not the frame pointer can
3402 be eliminated can depend on what registers have been used. We need
3403 not check some conditions again (such as flag_omit_frame_pointer)
3404 since they can't have changed. */
3406 static void
3407 update_eliminables (HARD_REG_SET *pset)
3409 int previous_frame_pointer_needed = frame_pointer_needed;
3410 struct elim_table *ep;
3412 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3413 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3414 #ifdef ELIMINABLE_REGS
3415 || ! CAN_ELIMINATE (ep->from, ep->to)
3416 #endif
3418 ep->can_eliminate = 0;
3420 /* Look for the case where we have discovered that we can't replace
3421 register A with register B and that means that we will now be
3422 trying to replace register A with register C. This means we can
3423 no longer replace register C with register B and we need to disable
3424 such an elimination, if it exists. This occurs often with A == ap,
3425 B == sp, and C == fp. */
3427 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3429 struct elim_table *op;
3430 int new_to = -1;
3432 if (! ep->can_eliminate && ep->can_eliminate_previous)
3434 /* Find the current elimination for ep->from, if there is a
3435 new one. */
3436 for (op = reg_eliminate;
3437 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3438 if (op->from == ep->from && op->can_eliminate)
3440 new_to = op->to;
3441 break;
3444 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3445 disable it. */
3446 for (op = reg_eliminate;
3447 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3448 if (op->from == new_to && op->to == ep->to)
3449 op->can_eliminate = 0;
3453 /* See if any registers that we thought we could eliminate the previous
3454 time are no longer eliminable. If so, something has changed and we
3455 must spill the register. Also, recompute the number of eliminable
3456 registers and see if the frame pointer is needed; it is if there is
3457 no elimination of the frame pointer that we can perform. */
3459 frame_pointer_needed = 1;
3460 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3462 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3463 && ep->to != HARD_FRAME_POINTER_REGNUM)
3464 frame_pointer_needed = 0;
3466 if (! ep->can_eliminate && ep->can_eliminate_previous)
3468 ep->can_eliminate_previous = 0;
3469 SET_HARD_REG_BIT (*pset, ep->from);
3470 num_eliminable--;
3474 /* If we didn't need a frame pointer last time, but we do now, spill
3475 the hard frame pointer. */
3476 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3477 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3480 /* Initialize the table of registers to eliminate. */
3482 static void
3483 init_elim_table (void)
3485 struct elim_table *ep;
3486 #ifdef ELIMINABLE_REGS
3487 const struct elim_table_1 *ep1;
3488 #endif
3490 if (!reg_eliminate)
3491 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3493 /* Does this function require a frame pointer? */
3495 frame_pointer_needed = (! flag_omit_frame_pointer
3496 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3497 and restore sp for alloca. So we can't eliminate
3498 the frame pointer in that case. At some point,
3499 we should improve this by emitting the
3500 sp-adjusting insns for this case. */
3501 || (current_function_calls_alloca
3502 && EXIT_IGNORE_STACK)
3503 || FRAME_POINTER_REQUIRED);
3505 num_eliminable = 0;
3507 #ifdef ELIMINABLE_REGS
3508 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3509 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3511 ep->from = ep1->from;
3512 ep->to = ep1->to;
3513 ep->can_eliminate = ep->can_eliminate_previous
3514 = (CAN_ELIMINATE (ep->from, ep->to)
3515 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3517 #else
3518 reg_eliminate[0].from = reg_eliminate_1[0].from;
3519 reg_eliminate[0].to = reg_eliminate_1[0].to;
3520 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3521 = ! frame_pointer_needed;
3522 #endif
3524 /* Count the number of eliminable registers and build the FROM and TO
3525 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3526 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3527 We depend on this. */
3528 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3530 num_eliminable += ep->can_eliminate;
3531 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3532 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3536 /* Kick all pseudos out of hard register REGNO.
3538 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3539 because we found we can't eliminate some register. In the case, no pseudos
3540 are allowed to be in the register, even if they are only in a block that
3541 doesn't require spill registers, unlike the case when we are spilling this
3542 hard reg to produce another spill register.
3544 Return nonzero if any pseudos needed to be kicked out. */
3546 static void
3547 spill_hard_reg (unsigned int regno, int cant_eliminate)
3549 int i;
3551 if (cant_eliminate)
3553 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3554 regs_ever_live[regno] = 1;
3557 /* Spill every pseudo reg that was allocated to this reg
3558 or to something that overlaps this reg. */
3560 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3561 if (reg_renumber[i] >= 0
3562 && (unsigned int) reg_renumber[i] <= regno
3563 && ((unsigned int) reg_renumber[i]
3564 + hard_regno_nregs[(unsigned int) reg_renumber[i]]
3565 [PSEUDO_REGNO_MODE (i)]
3566 > regno))
3567 SET_REGNO_REG_SET (&spilled_pseudos, i);
3570 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3571 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3573 static void
3574 ior_hard_reg_set (HARD_REG_SET *set1, HARD_REG_SET *set2)
3576 IOR_HARD_REG_SET (*set1, *set2);
3579 /* After find_reload_regs has been run for all insn that need reloads,
3580 and/or spill_hard_regs was called, this function is used to actually
3581 spill pseudo registers and try to reallocate them. It also sets up the
3582 spill_regs array for use by choose_reload_regs. */
3584 static int
3585 finish_spills (int global)
3587 struct insn_chain *chain;
3588 int something_changed = 0;
3589 int i;
3591 /* Build the spill_regs array for the function. */
3592 /* If there are some registers still to eliminate and one of the spill regs
3593 wasn't ever used before, additional stack space may have to be
3594 allocated to store this register. Thus, we may have changed the offset
3595 between the stack and frame pointers, so mark that something has changed.
3597 One might think that we need only set VAL to 1 if this is a call-used
3598 register. However, the set of registers that must be saved by the
3599 prologue is not identical to the call-used set. For example, the
3600 register used by the call insn for the return PC is a call-used register,
3601 but must be saved by the prologue. */
3603 n_spills = 0;
3604 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3605 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3607 spill_reg_order[i] = n_spills;
3608 spill_regs[n_spills++] = i;
3609 if (num_eliminable && ! regs_ever_live[i])
3610 something_changed = 1;
3611 regs_ever_live[i] = 1;
3613 else
3614 spill_reg_order[i] = -1;
3616 EXECUTE_IF_SET_IN_REG_SET
3617 (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i,
3619 /* Record the current hard register the pseudo is allocated to in
3620 pseudo_previous_regs so we avoid reallocating it to the same
3621 hard reg in a later pass. */
3622 if (reg_renumber[i] < 0)
3623 abort ();
3625 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3626 /* Mark it as no longer having a hard register home. */
3627 reg_renumber[i] = -1;
3628 /* We will need to scan everything again. */
3629 something_changed = 1;
3632 /* Retry global register allocation if possible. */
3633 if (global)
3635 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3636 /* For every insn that needs reloads, set the registers used as spill
3637 regs in pseudo_forbidden_regs for every pseudo live across the
3638 insn. */
3639 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3641 EXECUTE_IF_SET_IN_REG_SET
3642 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
3644 ior_hard_reg_set (pseudo_forbidden_regs + i,
3645 &chain->used_spill_regs);
3647 EXECUTE_IF_SET_IN_REG_SET
3648 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
3650 ior_hard_reg_set (pseudo_forbidden_regs + i,
3651 &chain->used_spill_regs);
3655 /* Retry allocating the spilled pseudos. For each reg, merge the
3656 various reg sets that indicate which hard regs can't be used,
3657 and call retry_global_alloc.
3658 We change spill_pseudos here to only contain pseudos that did not
3659 get a new hard register. */
3660 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3661 if (reg_old_renumber[i] != reg_renumber[i])
3663 HARD_REG_SET forbidden;
3664 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3665 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3666 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3667 retry_global_alloc (i, forbidden);
3668 if (reg_renumber[i] >= 0)
3669 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3673 /* Fix up the register information in the insn chain.
3674 This involves deleting those of the spilled pseudos which did not get
3675 a new hard register home from the live_{before,after} sets. */
3676 for (chain = reload_insn_chain; chain; chain = chain->next)
3678 HARD_REG_SET used_by_pseudos;
3679 HARD_REG_SET used_by_pseudos2;
3681 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3682 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3684 /* Mark any unallocated hard regs as available for spills. That
3685 makes inheritance work somewhat better. */
3686 if (chain->need_reload)
3688 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3689 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3690 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3692 /* Save the old value for the sanity test below. */
3693 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3695 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3696 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3697 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3698 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3700 /* Make sure we only enlarge the set. */
3701 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3702 abort ();
3703 ok:;
3707 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3708 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3710 int regno = reg_renumber[i];
3711 if (reg_old_renumber[i] == regno)
3712 continue;
3714 alter_reg (i, reg_old_renumber[i]);
3715 reg_old_renumber[i] = regno;
3716 if (dump_file)
3718 if (regno == -1)
3719 fprintf (dump_file, " Register %d now on stack.\n\n", i);
3720 else
3721 fprintf (dump_file, " Register %d now in %d.\n\n",
3722 i, reg_renumber[i]);
3726 return something_changed;
3729 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
3731 static void
3732 scan_paradoxical_subregs (rtx x)
3734 int i;
3735 const char *fmt;
3736 enum rtx_code code = GET_CODE (x);
3738 switch (code)
3740 case REG:
3741 case CONST_INT:
3742 case CONST:
3743 case SYMBOL_REF:
3744 case LABEL_REF:
3745 case CONST_DOUBLE:
3746 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3747 case CC0:
3748 case PC:
3749 case USE:
3750 case CLOBBER:
3751 return;
3753 case SUBREG:
3754 if (GET_CODE (SUBREG_REG (x)) == REG
3755 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3756 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3757 = GET_MODE_SIZE (GET_MODE (x));
3758 return;
3760 default:
3761 break;
3764 fmt = GET_RTX_FORMAT (code);
3765 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3767 if (fmt[i] == 'e')
3768 scan_paradoxical_subregs (XEXP (x, i));
3769 else if (fmt[i] == 'E')
3771 int j;
3772 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3773 scan_paradoxical_subregs (XVECEXP (x, i, j));
3778 /* Reload pseudo-registers into hard regs around each insn as needed.
3779 Additional register load insns are output before the insn that needs it
3780 and perhaps store insns after insns that modify the reloaded pseudo reg.
3782 reg_last_reload_reg and reg_reloaded_contents keep track of
3783 which registers are already available in reload registers.
3784 We update these for the reloads that we perform,
3785 as the insns are scanned. */
3787 static void
3788 reload_as_needed (int live_known)
3790 struct insn_chain *chain;
3791 #if defined (AUTO_INC_DEC)
3792 int i;
3793 #endif
3794 rtx x;
3796 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
3797 memset (spill_reg_store, 0, sizeof spill_reg_store);
3798 reg_last_reload_reg = xcalloc (max_regno, sizeof (rtx));
3799 reg_has_output_reload = xmalloc (max_regno);
3800 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3801 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
3803 set_initial_elim_offsets ();
3805 for (chain = reload_insn_chain; chain; chain = chain->next)
3807 rtx prev = 0;
3808 rtx insn = chain->insn;
3809 rtx old_next = NEXT_INSN (insn);
3811 /* If we pass a label, copy the offsets from the label information
3812 into the current offsets of each elimination. */
3813 if (GET_CODE (insn) == CODE_LABEL)
3814 set_offsets_for_label (insn);
3816 else if (INSN_P (insn))
3818 rtx oldpat = copy_rtx (PATTERN (insn));
3820 /* If this is a USE and CLOBBER of a MEM, ensure that any
3821 references to eliminable registers have been removed. */
3823 if ((GET_CODE (PATTERN (insn)) == USE
3824 || GET_CODE (PATTERN (insn)) == CLOBBER)
3825 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3826 XEXP (XEXP (PATTERN (insn), 0), 0)
3827 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3828 GET_MODE (XEXP (PATTERN (insn), 0)),
3829 NULL_RTX);
3831 /* If we need to do register elimination processing, do so.
3832 This might delete the insn, in which case we are done. */
3833 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3835 eliminate_regs_in_insn (insn, 1);
3836 if (GET_CODE (insn) == NOTE)
3838 update_eliminable_offsets ();
3839 continue;
3843 /* If need_elim is nonzero but need_reload is zero, one might think
3844 that we could simply set n_reloads to 0. However, find_reloads
3845 could have done some manipulation of the insn (such as swapping
3846 commutative operands), and these manipulations are lost during
3847 the first pass for every insn that needs register elimination.
3848 So the actions of find_reloads must be redone here. */
3850 if (! chain->need_elim && ! chain->need_reload
3851 && ! chain->need_operand_change)
3852 n_reloads = 0;
3853 /* First find the pseudo regs that must be reloaded for this insn.
3854 This info is returned in the tables reload_... (see reload.h).
3855 Also modify the body of INSN by substituting RELOAD
3856 rtx's for those pseudo regs. */
3857 else
3859 memset (reg_has_output_reload, 0, max_regno);
3860 CLEAR_HARD_REG_SET (reg_is_output_reload);
3862 find_reloads (insn, 1, spill_indirect_levels, live_known,
3863 spill_reg_order);
3866 if (n_reloads > 0)
3868 rtx next = NEXT_INSN (insn);
3869 rtx p;
3871 prev = PREV_INSN (insn);
3873 /* Now compute which reload regs to reload them into. Perhaps
3874 reusing reload regs from previous insns, or else output
3875 load insns to reload them. Maybe output store insns too.
3876 Record the choices of reload reg in reload_reg_rtx. */
3877 choose_reload_regs (chain);
3879 /* Merge any reloads that we didn't combine for fear of
3880 increasing the number of spill registers needed but now
3881 discover can be safely merged. */
3882 if (SMALL_REGISTER_CLASSES)
3883 merge_assigned_reloads (insn);
3885 /* Generate the insns to reload operands into or out of
3886 their reload regs. */
3887 emit_reload_insns (chain);
3889 /* Substitute the chosen reload regs from reload_reg_rtx
3890 into the insn's body (or perhaps into the bodies of other
3891 load and store insn that we just made for reloading
3892 and that we moved the structure into). */
3893 subst_reloads (insn);
3895 /* If this was an ASM, make sure that all the reload insns
3896 we have generated are valid. If not, give an error
3897 and delete them. */
3899 if (asm_noperands (PATTERN (insn)) >= 0)
3900 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3901 if (p != insn && INSN_P (p)
3902 && GET_CODE (PATTERN (p)) != USE
3903 && (recog_memoized (p) < 0
3904 || (extract_insn (p), ! constrain_operands (1))))
3906 error_for_asm (insn,
3907 "`asm' operand requires impossible reload");
3908 delete_insn (p);
3912 if (num_eliminable && chain->need_elim)
3913 update_eliminable_offsets ();
3915 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3916 is no longer validly lying around to save a future reload.
3917 Note that this does not detect pseudos that were reloaded
3918 for this insn in order to be stored in
3919 (obeying register constraints). That is correct; such reload
3920 registers ARE still valid. */
3921 note_stores (oldpat, forget_old_reloads_1, NULL);
3923 /* There may have been CLOBBER insns placed after INSN. So scan
3924 between INSN and NEXT and use them to forget old reloads. */
3925 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
3926 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3927 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
3929 #ifdef AUTO_INC_DEC
3930 /* Likewise for regs altered by auto-increment in this insn.
3931 REG_INC notes have been changed by reloading:
3932 find_reloads_address_1 records substitutions for them,
3933 which have been performed by subst_reloads above. */
3934 for (i = n_reloads - 1; i >= 0; i--)
3936 rtx in_reg = rld[i].in_reg;
3937 if (in_reg)
3939 enum rtx_code code = GET_CODE (in_reg);
3940 /* PRE_INC / PRE_DEC will have the reload register ending up
3941 with the same value as the stack slot, but that doesn't
3942 hold true for POST_INC / POST_DEC. Either we have to
3943 convert the memory access to a true POST_INC / POST_DEC,
3944 or we can't use the reload register for inheritance. */
3945 if ((code == POST_INC || code == POST_DEC)
3946 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3947 REGNO (rld[i].reg_rtx))
3948 /* Make sure it is the inc/dec pseudo, and not
3949 some other (e.g. output operand) pseudo. */
3950 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3951 == REGNO (XEXP (in_reg, 0))))
3954 rtx reload_reg = rld[i].reg_rtx;
3955 enum machine_mode mode = GET_MODE (reload_reg);
3956 int n = 0;
3957 rtx p;
3959 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
3961 /* We really want to ignore REG_INC notes here, so
3962 use PATTERN (p) as argument to reg_set_p . */
3963 if (reg_set_p (reload_reg, PATTERN (p)))
3964 break;
3965 n = count_occurrences (PATTERN (p), reload_reg, 0);
3966 if (! n)
3967 continue;
3968 if (n == 1)
3970 n = validate_replace_rtx (reload_reg,
3971 gen_rtx_fmt_e (code,
3972 mode,
3973 reload_reg),
3976 /* We must also verify that the constraints
3977 are met after the replacement. */
3978 extract_insn (p);
3979 if (n)
3980 n = constrain_operands (1);
3981 else
3982 break;
3984 /* If the constraints were not met, then
3985 undo the replacement. */
3986 if (!n)
3988 validate_replace_rtx (gen_rtx_fmt_e (code,
3989 mode,
3990 reload_reg),
3991 reload_reg, p);
3992 break;
3996 break;
3998 if (n == 1)
4000 REG_NOTES (p)
4001 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4002 REG_NOTES (p));
4003 /* Mark this as having an output reload so that the
4004 REG_INC processing code below won't invalidate
4005 the reload for inheritance. */
4006 SET_HARD_REG_BIT (reg_is_output_reload,
4007 REGNO (reload_reg));
4008 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4010 else
4011 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4012 NULL);
4014 else if ((code == PRE_INC || code == PRE_DEC)
4015 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4016 REGNO (rld[i].reg_rtx))
4017 /* Make sure it is the inc/dec pseudo, and not
4018 some other (e.g. output operand) pseudo. */
4019 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4020 == REGNO (XEXP (in_reg, 0))))
4022 SET_HARD_REG_BIT (reg_is_output_reload,
4023 REGNO (rld[i].reg_rtx));
4024 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4028 /* If a pseudo that got a hard register is auto-incremented,
4029 we must purge records of copying it into pseudos without
4030 hard registers. */
4031 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4032 if (REG_NOTE_KIND (x) == REG_INC)
4034 /* See if this pseudo reg was reloaded in this insn.
4035 If so, its last-reload info is still valid
4036 because it is based on this insn's reload. */
4037 for (i = 0; i < n_reloads; i++)
4038 if (rld[i].out == XEXP (x, 0))
4039 break;
4041 if (i == n_reloads)
4042 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4044 #endif
4046 /* A reload reg's contents are unknown after a label. */
4047 if (GET_CODE (insn) == CODE_LABEL)
4048 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4050 /* Don't assume a reload reg is still good after a call insn
4051 if it is a call-used reg, or if it contains a value that will
4052 be partially clobbered by the call. */
4053 else if (GET_CODE (insn) == CALL_INSN)
4055 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4056 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4060 /* Clean up. */
4061 free (reg_last_reload_reg);
4062 free (reg_has_output_reload);
4065 /* Discard all record of any value reloaded from X,
4066 or reloaded in X from someplace else;
4067 unless X is an output reload reg of the current insn.
4069 X may be a hard reg (the reload reg)
4070 or it may be a pseudo reg that was reloaded from. */
4072 static void
4073 forget_old_reloads_1 (rtx x, rtx ignored ATTRIBUTE_UNUSED,
4074 void *data ATTRIBUTE_UNUSED)
4076 unsigned int regno;
4077 unsigned int nr;
4079 /* note_stores does give us subregs of hard regs,
4080 subreg_regno_offset will abort if it is not a hard reg. */
4081 while (GET_CODE (x) == SUBREG)
4083 /* We ignore the subreg offset when calculating the regno,
4084 because we are using the entire underlying hard register
4085 below. */
4086 x = SUBREG_REG (x);
4089 if (GET_CODE (x) != REG)
4090 return;
4092 regno = REGNO (x);
4094 if (regno >= FIRST_PSEUDO_REGISTER)
4095 nr = 1;
4096 else
4098 unsigned int i;
4100 nr = hard_regno_nregs[regno][GET_MODE (x)];
4101 /* Storing into a spilled-reg invalidates its contents.
4102 This can happen if a block-local pseudo is allocated to that reg
4103 and it wasn't spilled because this block's total need is 0.
4104 Then some insn might have an optional reload and use this reg. */
4105 for (i = 0; i < nr; i++)
4106 /* But don't do this if the reg actually serves as an output
4107 reload reg in the current instruction. */
4108 if (n_reloads == 0
4109 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4111 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4112 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, regno + i);
4113 spill_reg_store[regno + i] = 0;
4117 /* Since value of X has changed,
4118 forget any value previously copied from it. */
4120 while (nr-- > 0)
4121 /* But don't forget a copy if this is the output reload
4122 that establishes the copy's validity. */
4123 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4124 reg_last_reload_reg[regno + nr] = 0;
4127 /* The following HARD_REG_SETs indicate when each hard register is
4128 used for a reload of various parts of the current insn. */
4130 /* If reg is unavailable for all reloads. */
4131 static HARD_REG_SET reload_reg_unavailable;
4132 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4133 static HARD_REG_SET reload_reg_used;
4134 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4135 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4136 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4137 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4138 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4139 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4140 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4141 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4142 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4143 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4144 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4145 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4146 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4147 static HARD_REG_SET reload_reg_used_in_op_addr;
4148 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4149 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4150 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4151 static HARD_REG_SET reload_reg_used_in_insn;
4152 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4153 static HARD_REG_SET reload_reg_used_in_other_addr;
4155 /* If reg is in use as a reload reg for any sort of reload. */
4156 static HARD_REG_SET reload_reg_used_at_all;
4158 /* If reg is use as an inherited reload. We just mark the first register
4159 in the group. */
4160 static HARD_REG_SET reload_reg_used_for_inherit;
4162 /* Records which hard regs are used in any way, either as explicit use or
4163 by being allocated to a pseudo during any point of the current insn. */
4164 static HARD_REG_SET reg_used_in_insn;
4166 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4167 TYPE. MODE is used to indicate how many consecutive regs are
4168 actually used. */
4170 static void
4171 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4172 enum machine_mode mode)
4174 unsigned int nregs = hard_regno_nregs[regno][mode];
4175 unsigned int i;
4177 for (i = regno; i < nregs + regno; i++)
4179 switch (type)
4181 case RELOAD_OTHER:
4182 SET_HARD_REG_BIT (reload_reg_used, i);
4183 break;
4185 case RELOAD_FOR_INPUT_ADDRESS:
4186 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4187 break;
4189 case RELOAD_FOR_INPADDR_ADDRESS:
4190 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4191 break;
4193 case RELOAD_FOR_OUTPUT_ADDRESS:
4194 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4195 break;
4197 case RELOAD_FOR_OUTADDR_ADDRESS:
4198 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4199 break;
4201 case RELOAD_FOR_OPERAND_ADDRESS:
4202 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4203 break;
4205 case RELOAD_FOR_OPADDR_ADDR:
4206 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4207 break;
4209 case RELOAD_FOR_OTHER_ADDRESS:
4210 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4211 break;
4213 case RELOAD_FOR_INPUT:
4214 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4215 break;
4217 case RELOAD_FOR_OUTPUT:
4218 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4219 break;
4221 case RELOAD_FOR_INSN:
4222 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4223 break;
4226 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4230 /* Similarly, but show REGNO is no longer in use for a reload. */
4232 static void
4233 clear_reload_reg_in_use (unsigned int regno, int opnum,
4234 enum reload_type type, enum machine_mode mode)
4236 unsigned int nregs = hard_regno_nregs[regno][mode];
4237 unsigned int start_regno, end_regno, r;
4238 int i;
4239 /* A complication is that for some reload types, inheritance might
4240 allow multiple reloads of the same types to share a reload register.
4241 We set check_opnum if we have to check only reloads with the same
4242 operand number, and check_any if we have to check all reloads. */
4243 int check_opnum = 0;
4244 int check_any = 0;
4245 HARD_REG_SET *used_in_set;
4247 switch (type)
4249 case RELOAD_OTHER:
4250 used_in_set = &reload_reg_used;
4251 break;
4253 case RELOAD_FOR_INPUT_ADDRESS:
4254 used_in_set = &reload_reg_used_in_input_addr[opnum];
4255 break;
4257 case RELOAD_FOR_INPADDR_ADDRESS:
4258 check_opnum = 1;
4259 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4260 break;
4262 case RELOAD_FOR_OUTPUT_ADDRESS:
4263 used_in_set = &reload_reg_used_in_output_addr[opnum];
4264 break;
4266 case RELOAD_FOR_OUTADDR_ADDRESS:
4267 check_opnum = 1;
4268 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4269 break;
4271 case RELOAD_FOR_OPERAND_ADDRESS:
4272 used_in_set = &reload_reg_used_in_op_addr;
4273 break;
4275 case RELOAD_FOR_OPADDR_ADDR:
4276 check_any = 1;
4277 used_in_set = &reload_reg_used_in_op_addr_reload;
4278 break;
4280 case RELOAD_FOR_OTHER_ADDRESS:
4281 used_in_set = &reload_reg_used_in_other_addr;
4282 check_any = 1;
4283 break;
4285 case RELOAD_FOR_INPUT:
4286 used_in_set = &reload_reg_used_in_input[opnum];
4287 break;
4289 case RELOAD_FOR_OUTPUT:
4290 used_in_set = &reload_reg_used_in_output[opnum];
4291 break;
4293 case RELOAD_FOR_INSN:
4294 used_in_set = &reload_reg_used_in_insn;
4295 break;
4296 default:
4297 abort ();
4299 /* We resolve conflicts with remaining reloads of the same type by
4300 excluding the intervals of reload registers by them from the
4301 interval of freed reload registers. Since we only keep track of
4302 one set of interval bounds, we might have to exclude somewhat
4303 more than what would be necessary if we used a HARD_REG_SET here.
4304 But this should only happen very infrequently, so there should
4305 be no reason to worry about it. */
4307 start_regno = regno;
4308 end_regno = regno + nregs;
4309 if (check_opnum || check_any)
4311 for (i = n_reloads - 1; i >= 0; i--)
4313 if (rld[i].when_needed == type
4314 && (check_any || rld[i].opnum == opnum)
4315 && rld[i].reg_rtx)
4317 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4318 unsigned int conflict_end
4319 = (conflict_start
4320 + hard_regno_nregs[conflict_start][rld[i].mode]);
4322 /* If there is an overlap with the first to-be-freed register,
4323 adjust the interval start. */
4324 if (conflict_start <= start_regno && conflict_end > start_regno)
4325 start_regno = conflict_end;
4326 /* Otherwise, if there is a conflict with one of the other
4327 to-be-freed registers, adjust the interval end. */
4328 if (conflict_start > start_regno && conflict_start < end_regno)
4329 end_regno = conflict_start;
4334 for (r = start_regno; r < end_regno; r++)
4335 CLEAR_HARD_REG_BIT (*used_in_set, r);
4338 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4339 specified by OPNUM and TYPE. */
4341 static int
4342 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4344 int i;
4346 /* In use for a RELOAD_OTHER means it's not available for anything. */
4347 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4348 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4349 return 0;
4351 switch (type)
4353 case RELOAD_OTHER:
4354 /* In use for anything means we can't use it for RELOAD_OTHER. */
4355 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4356 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4357 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4358 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4359 return 0;
4361 for (i = 0; i < reload_n_operands; i++)
4362 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4363 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4364 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4365 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4366 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4367 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4368 return 0;
4370 return 1;
4372 case RELOAD_FOR_INPUT:
4373 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4374 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4375 return 0;
4377 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4378 return 0;
4380 /* If it is used for some other input, can't use it. */
4381 for (i = 0; i < reload_n_operands; i++)
4382 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4383 return 0;
4385 /* If it is used in a later operand's address, can't use it. */
4386 for (i = opnum + 1; i < reload_n_operands; i++)
4387 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4388 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4389 return 0;
4391 return 1;
4393 case RELOAD_FOR_INPUT_ADDRESS:
4394 /* Can't use a register if it is used for an input address for this
4395 operand or used as an input in an earlier one. */
4396 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4397 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4398 return 0;
4400 for (i = 0; i < opnum; i++)
4401 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4402 return 0;
4404 return 1;
4406 case RELOAD_FOR_INPADDR_ADDRESS:
4407 /* Can't use a register if it is used for an input address
4408 for this operand or used as an input in an earlier
4409 one. */
4410 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4411 return 0;
4413 for (i = 0; i < opnum; i++)
4414 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4415 return 0;
4417 return 1;
4419 case RELOAD_FOR_OUTPUT_ADDRESS:
4420 /* Can't use a register if it is used for an output address for this
4421 operand or used as an output in this or a later operand. Note
4422 that multiple output operands are emitted in reverse order, so
4423 the conflicting ones are those with lower indices. */
4424 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4425 return 0;
4427 for (i = 0; i <= opnum; i++)
4428 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4429 return 0;
4431 return 1;
4433 case RELOAD_FOR_OUTADDR_ADDRESS:
4434 /* Can't use a register if it is used for an output address
4435 for this operand or used as an output in this or a
4436 later operand. Note that multiple output operands are
4437 emitted in reverse order, so the conflicting ones are
4438 those with lower indices. */
4439 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4440 return 0;
4442 for (i = 0; i <= opnum; i++)
4443 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4444 return 0;
4446 return 1;
4448 case RELOAD_FOR_OPERAND_ADDRESS:
4449 for (i = 0; i < reload_n_operands; i++)
4450 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4451 return 0;
4453 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4454 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4456 case RELOAD_FOR_OPADDR_ADDR:
4457 for (i = 0; i < reload_n_operands; i++)
4458 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4459 return 0;
4461 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4463 case RELOAD_FOR_OUTPUT:
4464 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4465 outputs, or an operand address for this or an earlier output.
4466 Note that multiple output operands are emitted in reverse order,
4467 so the conflicting ones are those with higher indices. */
4468 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4469 return 0;
4471 for (i = 0; i < reload_n_operands; i++)
4472 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4473 return 0;
4475 for (i = opnum; i < reload_n_operands; i++)
4476 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4477 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4478 return 0;
4480 return 1;
4482 case RELOAD_FOR_INSN:
4483 for (i = 0; i < reload_n_operands; i++)
4484 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4485 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4486 return 0;
4488 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4489 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4491 case RELOAD_FOR_OTHER_ADDRESS:
4492 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4494 abort ();
4497 /* Return 1 if the value in reload reg REGNO, as used by a reload
4498 needed for the part of the insn specified by OPNUM and TYPE,
4499 is still available in REGNO at the end of the insn.
4501 We can assume that the reload reg was already tested for availability
4502 at the time it is needed, and we should not check this again,
4503 in case the reg has already been marked in use. */
4505 static int
4506 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4508 int i;
4510 switch (type)
4512 case RELOAD_OTHER:
4513 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4514 its value must reach the end. */
4515 return 1;
4517 /* If this use is for part of the insn,
4518 its value reaches if no subsequent part uses the same register.
4519 Just like the above function, don't try to do this with lots
4520 of fallthroughs. */
4522 case RELOAD_FOR_OTHER_ADDRESS:
4523 /* Here we check for everything else, since these don't conflict
4524 with anything else and everything comes later. */
4526 for (i = 0; i < reload_n_operands; i++)
4527 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4528 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4529 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4530 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4531 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4532 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4533 return 0;
4535 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4536 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4537 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4538 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4540 case RELOAD_FOR_INPUT_ADDRESS:
4541 case RELOAD_FOR_INPADDR_ADDRESS:
4542 /* Similar, except that we check only for this and subsequent inputs
4543 and the address of only subsequent inputs and we do not need
4544 to check for RELOAD_OTHER objects since they are known not to
4545 conflict. */
4547 for (i = opnum; i < reload_n_operands; i++)
4548 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4549 return 0;
4551 for (i = opnum + 1; i < reload_n_operands; i++)
4552 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4553 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4554 return 0;
4556 for (i = 0; i < reload_n_operands; i++)
4557 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4558 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4559 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4560 return 0;
4562 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4563 return 0;
4565 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4566 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4567 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4569 case RELOAD_FOR_INPUT:
4570 /* Similar to input address, except we start at the next operand for
4571 both input and input address and we do not check for
4572 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4573 would conflict. */
4575 for (i = opnum + 1; i < reload_n_operands; i++)
4576 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4577 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4578 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4579 return 0;
4581 /* ... fall through ... */
4583 case RELOAD_FOR_OPERAND_ADDRESS:
4584 /* Check outputs and their addresses. */
4586 for (i = 0; i < reload_n_operands; i++)
4587 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4588 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4589 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4590 return 0;
4592 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4594 case RELOAD_FOR_OPADDR_ADDR:
4595 for (i = 0; i < reload_n_operands; i++)
4596 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4597 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4598 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4599 return 0;
4601 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4602 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4603 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4605 case RELOAD_FOR_INSN:
4606 /* These conflict with other outputs with RELOAD_OTHER. So
4607 we need only check for output addresses. */
4609 opnum = reload_n_operands;
4611 /* ... fall through ... */
4613 case RELOAD_FOR_OUTPUT:
4614 case RELOAD_FOR_OUTPUT_ADDRESS:
4615 case RELOAD_FOR_OUTADDR_ADDRESS:
4616 /* We already know these can't conflict with a later output. So the
4617 only thing to check are later output addresses.
4618 Note that multiple output operands are emitted in reverse order,
4619 so the conflicting ones are those with lower indices. */
4620 for (i = 0; i < opnum; i++)
4621 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4622 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4623 return 0;
4625 return 1;
4628 abort ();
4631 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4632 Return 0 otherwise.
4634 This function uses the same algorithm as reload_reg_free_p above. */
4637 reloads_conflict (int r1, int r2)
4639 enum reload_type r1_type = rld[r1].when_needed;
4640 enum reload_type r2_type = rld[r2].when_needed;
4641 int r1_opnum = rld[r1].opnum;
4642 int r2_opnum = rld[r2].opnum;
4644 /* RELOAD_OTHER conflicts with everything. */
4645 if (r2_type == RELOAD_OTHER)
4646 return 1;
4648 /* Otherwise, check conflicts differently for each type. */
4650 switch (r1_type)
4652 case RELOAD_FOR_INPUT:
4653 return (r2_type == RELOAD_FOR_INSN
4654 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4655 || r2_type == RELOAD_FOR_OPADDR_ADDR
4656 || r2_type == RELOAD_FOR_INPUT
4657 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4658 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4659 && r2_opnum > r1_opnum));
4661 case RELOAD_FOR_INPUT_ADDRESS:
4662 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4663 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4665 case RELOAD_FOR_INPADDR_ADDRESS:
4666 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4667 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4669 case RELOAD_FOR_OUTPUT_ADDRESS:
4670 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4671 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4673 case RELOAD_FOR_OUTADDR_ADDRESS:
4674 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4675 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4677 case RELOAD_FOR_OPERAND_ADDRESS:
4678 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4679 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4681 case RELOAD_FOR_OPADDR_ADDR:
4682 return (r2_type == RELOAD_FOR_INPUT
4683 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4685 case RELOAD_FOR_OUTPUT:
4686 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4687 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4688 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4689 && r2_opnum >= r1_opnum));
4691 case RELOAD_FOR_INSN:
4692 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4693 || r2_type == RELOAD_FOR_INSN
4694 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4696 case RELOAD_FOR_OTHER_ADDRESS:
4697 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4699 case RELOAD_OTHER:
4700 return 1;
4702 default:
4703 abort ();
4707 /* Indexed by reload number, 1 if incoming value
4708 inherited from previous insns. */
4709 char reload_inherited[MAX_RELOADS];
4711 /* For an inherited reload, this is the insn the reload was inherited from,
4712 if we know it. Otherwise, this is 0. */
4713 rtx reload_inheritance_insn[MAX_RELOADS];
4715 /* If nonzero, this is a place to get the value of the reload,
4716 rather than using reload_in. */
4717 rtx reload_override_in[MAX_RELOADS];
4719 /* For each reload, the hard register number of the register used,
4720 or -1 if we did not need a register for this reload. */
4721 int reload_spill_index[MAX_RELOADS];
4723 /* Subroutine of free_for_value_p, used to check a single register.
4724 START_REGNO is the starting regno of the full reload register
4725 (possibly comprising multiple hard registers) that we are considering. */
4727 static int
4728 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
4729 enum reload_type type, rtx value, rtx out,
4730 int reloadnum, int ignore_address_reloads)
4732 int time1;
4733 /* Set if we see an input reload that must not share its reload register
4734 with any new earlyclobber, but might otherwise share the reload
4735 register with an output or input-output reload. */
4736 int check_earlyclobber = 0;
4737 int i;
4738 int copy = 0;
4740 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4741 return 0;
4743 if (out == const0_rtx)
4745 copy = 1;
4746 out = NULL_RTX;
4749 /* We use some pseudo 'time' value to check if the lifetimes of the
4750 new register use would overlap with the one of a previous reload
4751 that is not read-only or uses a different value.
4752 The 'time' used doesn't have to be linear in any shape or form, just
4753 monotonic.
4754 Some reload types use different 'buckets' for each operand.
4755 So there are MAX_RECOG_OPERANDS different time values for each
4756 such reload type.
4757 We compute TIME1 as the time when the register for the prospective
4758 new reload ceases to be live, and TIME2 for each existing
4759 reload as the time when that the reload register of that reload
4760 becomes live.
4761 Where there is little to be gained by exact lifetime calculations,
4762 we just make conservative assumptions, i.e. a longer lifetime;
4763 this is done in the 'default:' cases. */
4764 switch (type)
4766 case RELOAD_FOR_OTHER_ADDRESS:
4767 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4768 time1 = copy ? 0 : 1;
4769 break;
4770 case RELOAD_OTHER:
4771 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4772 break;
4773 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4774 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4775 respectively, to the time values for these, we get distinct time
4776 values. To get distinct time values for each operand, we have to
4777 multiply opnum by at least three. We round that up to four because
4778 multiply by four is often cheaper. */
4779 case RELOAD_FOR_INPADDR_ADDRESS:
4780 time1 = opnum * 4 + 2;
4781 break;
4782 case RELOAD_FOR_INPUT_ADDRESS:
4783 time1 = opnum * 4 + 3;
4784 break;
4785 case RELOAD_FOR_INPUT:
4786 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4787 executes (inclusive). */
4788 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
4789 break;
4790 case RELOAD_FOR_OPADDR_ADDR:
4791 /* opnum * 4 + 4
4792 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
4793 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4794 break;
4795 case RELOAD_FOR_OPERAND_ADDRESS:
4796 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4797 is executed. */
4798 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4799 break;
4800 case RELOAD_FOR_OUTADDR_ADDRESS:
4801 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
4802 break;
4803 case RELOAD_FOR_OUTPUT_ADDRESS:
4804 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
4805 break;
4806 default:
4807 time1 = MAX_RECOG_OPERANDS * 5 + 5;
4810 for (i = 0; i < n_reloads; i++)
4812 rtx reg = rld[i].reg_rtx;
4813 if (reg && GET_CODE (reg) == REG
4814 && ((unsigned) regno - true_regnum (reg)
4815 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
4816 && i != reloadnum)
4818 rtx other_input = rld[i].in;
4820 /* If the other reload loads the same input value, that
4821 will not cause a conflict only if it's loading it into
4822 the same register. */
4823 if (true_regnum (reg) != start_regno)
4824 other_input = NULL_RTX;
4825 if (! other_input || ! rtx_equal_p (other_input, value)
4826 || rld[i].out || out)
4828 int time2;
4829 switch (rld[i].when_needed)
4831 case RELOAD_FOR_OTHER_ADDRESS:
4832 time2 = 0;
4833 break;
4834 case RELOAD_FOR_INPADDR_ADDRESS:
4835 /* find_reloads makes sure that a
4836 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4837 by at most one - the first -
4838 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4839 address reload is inherited, the address address reload
4840 goes away, so we can ignore this conflict. */
4841 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4842 && ignore_address_reloads
4843 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4844 Then the address address is still needed to store
4845 back the new address. */
4846 && ! rld[reloadnum].out)
4847 continue;
4848 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4849 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4850 reloads go away. */
4851 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4852 && ignore_address_reloads
4853 /* Unless we are reloading an auto_inc expression. */
4854 && ! rld[reloadnum].out)
4855 continue;
4856 time2 = rld[i].opnum * 4 + 2;
4857 break;
4858 case RELOAD_FOR_INPUT_ADDRESS:
4859 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4860 && ignore_address_reloads
4861 && ! rld[reloadnum].out)
4862 continue;
4863 time2 = rld[i].opnum * 4 + 3;
4864 break;
4865 case RELOAD_FOR_INPUT:
4866 time2 = rld[i].opnum * 4 + 4;
4867 check_earlyclobber = 1;
4868 break;
4869 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
4870 == MAX_RECOG_OPERAND * 4 */
4871 case RELOAD_FOR_OPADDR_ADDR:
4872 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4873 && ignore_address_reloads
4874 && ! rld[reloadnum].out)
4875 continue;
4876 time2 = MAX_RECOG_OPERANDS * 4 + 1;
4877 break;
4878 case RELOAD_FOR_OPERAND_ADDRESS:
4879 time2 = MAX_RECOG_OPERANDS * 4 + 2;
4880 check_earlyclobber = 1;
4881 break;
4882 case RELOAD_FOR_INSN:
4883 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4884 break;
4885 case RELOAD_FOR_OUTPUT:
4886 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4887 instruction is executed. */
4888 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4889 break;
4890 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4891 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4892 value. */
4893 case RELOAD_FOR_OUTADDR_ADDRESS:
4894 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4895 && ignore_address_reloads
4896 && ! rld[reloadnum].out)
4897 continue;
4898 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
4899 break;
4900 case RELOAD_FOR_OUTPUT_ADDRESS:
4901 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
4902 break;
4903 case RELOAD_OTHER:
4904 /* If there is no conflict in the input part, handle this
4905 like an output reload. */
4906 if (! rld[i].in || rtx_equal_p (other_input, value))
4908 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4909 /* Earlyclobbered outputs must conflict with inputs. */
4910 if (earlyclobber_operand_p (rld[i].out))
4911 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4913 break;
4915 time2 = 1;
4916 /* RELOAD_OTHER might be live beyond instruction execution,
4917 but this is not obvious when we set time2 = 1. So check
4918 here if there might be a problem with the new reload
4919 clobbering the register used by the RELOAD_OTHER. */
4920 if (out)
4921 return 0;
4922 break;
4923 default:
4924 return 0;
4926 if ((time1 >= time2
4927 && (! rld[i].in || rld[i].out
4928 || ! rtx_equal_p (other_input, value)))
4929 || (out && rld[reloadnum].out_reg
4930 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
4931 return 0;
4936 /* Earlyclobbered outputs must conflict with inputs. */
4937 if (check_earlyclobber && out && earlyclobber_operand_p (out))
4938 return 0;
4940 return 1;
4943 /* Return 1 if the value in reload reg REGNO, as used by a reload
4944 needed for the part of the insn specified by OPNUM and TYPE,
4945 may be used to load VALUE into it.
4947 MODE is the mode in which the register is used, this is needed to
4948 determine how many hard regs to test.
4950 Other read-only reloads with the same value do not conflict
4951 unless OUT is nonzero and these other reloads have to live while
4952 output reloads live.
4953 If OUT is CONST0_RTX, this is a special case: it means that the
4954 test should not be for using register REGNO as reload register, but
4955 for copying from register REGNO into the reload register.
4957 RELOADNUM is the number of the reload we want to load this value for;
4958 a reload does not conflict with itself.
4960 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
4961 reloads that load an address for the very reload we are considering.
4963 The caller has to make sure that there is no conflict with the return
4964 register. */
4966 static int
4967 free_for_value_p (int regno, enum machine_mode mode, int opnum,
4968 enum reload_type type, rtx value, rtx out, int reloadnum,
4969 int ignore_address_reloads)
4971 int nregs = hard_regno_nregs[regno][mode];
4972 while (nregs-- > 0)
4973 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
4974 value, out, reloadnum,
4975 ignore_address_reloads))
4976 return 0;
4977 return 1;
4980 /* Return nonzero if the rtx X is invariant over the current function. */
4981 /* ??? Actually, the places where we use this expect exactly what
4982 * is tested here, and not everything that is function invariant. In
4983 * particular, the frame pointer and arg pointer are special cased;
4984 * pic_offset_table_rtx is not, and this will cause aborts when we
4985 * go to spill these things to memory. */
4987 static int
4988 function_invariant_p (rtx x)
4990 if (CONSTANT_P (x))
4991 return 1;
4992 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
4993 return 1;
4994 if (GET_CODE (x) == PLUS
4995 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
4996 && CONSTANT_P (XEXP (x, 1)))
4997 return 1;
4998 return 0;
5001 /* Determine whether the reload reg X overlaps any rtx'es used for
5002 overriding inheritance. Return nonzero if so. */
5004 static int
5005 conflicts_with_override (rtx x)
5007 int i;
5008 for (i = 0; i < n_reloads; i++)
5009 if (reload_override_in[i]
5010 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5011 return 1;
5012 return 0;
5015 /* Give an error message saying we failed to find a reload for INSN,
5016 and clear out reload R. */
5017 static void
5018 failed_reload (rtx insn, int r)
5020 if (asm_noperands (PATTERN (insn)) < 0)
5021 /* It's the compiler's fault. */
5022 fatal_insn ("could not find a spill register", insn);
5024 /* It's the user's fault; the operand's mode and constraint
5025 don't match. Disable this reload so we don't crash in final. */
5026 error_for_asm (insn,
5027 "`asm' operand constraint incompatible with operand size");
5028 rld[r].in = 0;
5029 rld[r].out = 0;
5030 rld[r].reg_rtx = 0;
5031 rld[r].optional = 1;
5032 rld[r].secondary_p = 1;
5035 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5036 for reload R. If it's valid, get an rtx for it. Return nonzero if
5037 successful. */
5038 static int
5039 set_reload_reg (int i, int r)
5041 int regno;
5042 rtx reg = spill_reg_rtx[i];
5044 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5045 spill_reg_rtx[i] = reg
5046 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5048 regno = true_regnum (reg);
5050 /* Detect when the reload reg can't hold the reload mode.
5051 This used to be one `if', but Sequent compiler can't handle that. */
5052 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5054 enum machine_mode test_mode = VOIDmode;
5055 if (rld[r].in)
5056 test_mode = GET_MODE (rld[r].in);
5057 /* If rld[r].in has VOIDmode, it means we will load it
5058 in whatever mode the reload reg has: to wit, rld[r].mode.
5059 We have already tested that for validity. */
5060 /* Aside from that, we need to test that the expressions
5061 to reload from or into have modes which are valid for this
5062 reload register. Otherwise the reload insns would be invalid. */
5063 if (! (rld[r].in != 0 && test_mode != VOIDmode
5064 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5065 if (! (rld[r].out != 0
5066 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5068 /* The reg is OK. */
5069 last_spill_reg = i;
5071 /* Mark as in use for this insn the reload regs we use
5072 for this. */
5073 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5074 rld[r].when_needed, rld[r].mode);
5076 rld[r].reg_rtx = reg;
5077 reload_spill_index[r] = spill_regs[i];
5078 return 1;
5081 return 0;
5084 /* Find a spill register to use as a reload register for reload R.
5085 LAST_RELOAD is nonzero if this is the last reload for the insn being
5086 processed.
5088 Set rld[R].reg_rtx to the register allocated.
5090 We return 1 if successful, or 0 if we couldn't find a spill reg and
5091 we didn't change anything. */
5093 static int
5094 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5095 int last_reload)
5097 int i, pass, count;
5099 /* If we put this reload ahead, thinking it is a group,
5100 then insist on finding a group. Otherwise we can grab a
5101 reg that some other reload needs.
5102 (That can happen when we have a 68000 DATA_OR_FP_REG
5103 which is a group of data regs or one fp reg.)
5104 We need not be so restrictive if there are no more reloads
5105 for this insn.
5107 ??? Really it would be nicer to have smarter handling
5108 for that kind of reg class, where a problem like this is normal.
5109 Perhaps those classes should be avoided for reloading
5110 by use of more alternatives. */
5112 int force_group = rld[r].nregs > 1 && ! last_reload;
5114 /* If we want a single register and haven't yet found one,
5115 take any reg in the right class and not in use.
5116 If we want a consecutive group, here is where we look for it.
5118 We use two passes so we can first look for reload regs to
5119 reuse, which are already in use for other reloads in this insn,
5120 and only then use additional registers.
5121 I think that maximizing reuse is needed to make sure we don't
5122 run out of reload regs. Suppose we have three reloads, and
5123 reloads A and B can share regs. These need two regs.
5124 Suppose A and B are given different regs.
5125 That leaves none for C. */
5126 for (pass = 0; pass < 2; pass++)
5128 /* I is the index in spill_regs.
5129 We advance it round-robin between insns to use all spill regs
5130 equally, so that inherited reloads have a chance
5131 of leapfrogging each other. */
5133 i = last_spill_reg;
5135 for (count = 0; count < n_spills; count++)
5137 int class = (int) rld[r].class;
5138 int regnum;
5140 i++;
5141 if (i >= n_spills)
5142 i -= n_spills;
5143 regnum = spill_regs[i];
5145 if ((reload_reg_free_p (regnum, rld[r].opnum,
5146 rld[r].when_needed)
5147 || (rld[r].in
5148 /* We check reload_reg_used to make sure we
5149 don't clobber the return register. */
5150 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5151 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5152 rld[r].when_needed, rld[r].in,
5153 rld[r].out, r, 1)))
5154 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5155 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5156 /* Look first for regs to share, then for unshared. But
5157 don't share regs used for inherited reloads; they are
5158 the ones we want to preserve. */
5159 && (pass
5160 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5161 regnum)
5162 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5163 regnum))))
5165 int nr = hard_regno_nregs[regnum][rld[r].mode];
5166 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5167 (on 68000) got us two FP regs. If NR is 1,
5168 we would reject both of them. */
5169 if (force_group)
5170 nr = rld[r].nregs;
5171 /* If we need only one reg, we have already won. */
5172 if (nr == 1)
5174 /* But reject a single reg if we demand a group. */
5175 if (force_group)
5176 continue;
5177 break;
5179 /* Otherwise check that as many consecutive regs as we need
5180 are available here. */
5181 while (nr > 1)
5183 int regno = regnum + nr - 1;
5184 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5185 && spill_reg_order[regno] >= 0
5186 && reload_reg_free_p (regno, rld[r].opnum,
5187 rld[r].when_needed)))
5188 break;
5189 nr--;
5191 if (nr == 1)
5192 break;
5196 /* If we found something on pass 1, omit pass 2. */
5197 if (count < n_spills)
5198 break;
5201 /* We should have found a spill register by now. */
5202 if (count >= n_spills)
5203 return 0;
5205 /* I is the index in SPILL_REG_RTX of the reload register we are to
5206 allocate. Get an rtx for it and find its register number. */
5208 return set_reload_reg (i, r);
5211 /* Initialize all the tables needed to allocate reload registers.
5212 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5213 is the array we use to restore the reg_rtx field for every reload. */
5215 static void
5216 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5218 int i;
5220 for (i = 0; i < n_reloads; i++)
5221 rld[i].reg_rtx = save_reload_reg_rtx[i];
5223 memset (reload_inherited, 0, MAX_RELOADS);
5224 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5225 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5227 CLEAR_HARD_REG_SET (reload_reg_used);
5228 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5229 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5230 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5231 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5232 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5234 CLEAR_HARD_REG_SET (reg_used_in_insn);
5236 HARD_REG_SET tmp;
5237 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5238 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5239 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5240 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5241 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5242 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5245 for (i = 0; i < reload_n_operands; i++)
5247 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5248 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5249 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5250 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5251 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5252 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5255 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5257 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5259 for (i = 0; i < n_reloads; i++)
5260 /* If we have already decided to use a certain register,
5261 don't use it in another way. */
5262 if (rld[i].reg_rtx)
5263 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5264 rld[i].when_needed, rld[i].mode);
5267 /* Assign hard reg targets for the pseudo-registers we must reload
5268 into hard regs for this insn.
5269 Also output the instructions to copy them in and out of the hard regs.
5271 For machines with register classes, we are responsible for
5272 finding a reload reg in the proper class. */
5274 static void
5275 choose_reload_regs (struct insn_chain *chain)
5277 rtx insn = chain->insn;
5278 int i, j;
5279 unsigned int max_group_size = 1;
5280 enum reg_class group_class = NO_REGS;
5281 int pass, win, inheritance;
5283 rtx save_reload_reg_rtx[MAX_RELOADS];
5285 /* In order to be certain of getting the registers we need,
5286 we must sort the reloads into order of increasing register class.
5287 Then our grabbing of reload registers will parallel the process
5288 that provided the reload registers.
5290 Also note whether any of the reloads wants a consecutive group of regs.
5291 If so, record the maximum size of the group desired and what
5292 register class contains all the groups needed by this insn. */
5294 for (j = 0; j < n_reloads; j++)
5296 reload_order[j] = j;
5297 reload_spill_index[j] = -1;
5299 if (rld[j].nregs > 1)
5301 max_group_size = MAX (rld[j].nregs, max_group_size);
5302 group_class
5303 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5306 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5309 if (n_reloads > 1)
5310 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5312 /* If -O, try first with inheritance, then turning it off.
5313 If not -O, don't do inheritance.
5314 Using inheritance when not optimizing leads to paradoxes
5315 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5316 because one side of the comparison might be inherited. */
5317 win = 0;
5318 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5320 choose_reload_regs_init (chain, save_reload_reg_rtx);
5322 /* Process the reloads in order of preference just found.
5323 Beyond this point, subregs can be found in reload_reg_rtx.
5325 This used to look for an existing reloaded home for all of the
5326 reloads, and only then perform any new reloads. But that could lose
5327 if the reloads were done out of reg-class order because a later
5328 reload with a looser constraint might have an old home in a register
5329 needed by an earlier reload with a tighter constraint.
5331 To solve this, we make two passes over the reloads, in the order
5332 described above. In the first pass we try to inherit a reload
5333 from a previous insn. If there is a later reload that needs a
5334 class that is a proper subset of the class being processed, we must
5335 also allocate a spill register during the first pass.
5337 Then make a second pass over the reloads to allocate any reloads
5338 that haven't been given registers yet. */
5340 for (j = 0; j < n_reloads; j++)
5342 int r = reload_order[j];
5343 rtx search_equiv = NULL_RTX;
5345 /* Ignore reloads that got marked inoperative. */
5346 if (rld[r].out == 0 && rld[r].in == 0
5347 && ! rld[r].secondary_p)
5348 continue;
5350 /* If find_reloads chose to use reload_in or reload_out as a reload
5351 register, we don't need to chose one. Otherwise, try even if it
5352 found one since we might save an insn if we find the value lying
5353 around.
5354 Try also when reload_in is a pseudo without a hard reg. */
5355 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5356 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5357 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5358 && GET_CODE (rld[r].in) != MEM
5359 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5360 continue;
5362 #if 0 /* No longer needed for correct operation.
5363 It might give better code, or might not; worth an experiment? */
5364 /* If this is an optional reload, we can't inherit from earlier insns
5365 until we are sure that any non-optional reloads have been allocated.
5366 The following code takes advantage of the fact that optional reloads
5367 are at the end of reload_order. */
5368 if (rld[r].optional != 0)
5369 for (i = 0; i < j; i++)
5370 if ((rld[reload_order[i]].out != 0
5371 || rld[reload_order[i]].in != 0
5372 || rld[reload_order[i]].secondary_p)
5373 && ! rld[reload_order[i]].optional
5374 && rld[reload_order[i]].reg_rtx == 0)
5375 allocate_reload_reg (chain, reload_order[i], 0);
5376 #endif
5378 /* First see if this pseudo is already available as reloaded
5379 for a previous insn. We cannot try to inherit for reloads
5380 that are smaller than the maximum number of registers needed
5381 for groups unless the register we would allocate cannot be used
5382 for the groups.
5384 We could check here to see if this is a secondary reload for
5385 an object that is already in a register of the desired class.
5386 This would avoid the need for the secondary reload register.
5387 But this is complex because we can't easily determine what
5388 objects might want to be loaded via this reload. So let a
5389 register be allocated here. In `emit_reload_insns' we suppress
5390 one of the loads in the case described above. */
5392 if (inheritance)
5394 int byte = 0;
5395 int regno = -1;
5396 enum machine_mode mode = VOIDmode;
5398 if (rld[r].in == 0)
5400 else if (GET_CODE (rld[r].in) == REG)
5402 regno = REGNO (rld[r].in);
5403 mode = GET_MODE (rld[r].in);
5405 else if (GET_CODE (rld[r].in_reg) == REG)
5407 regno = REGNO (rld[r].in_reg);
5408 mode = GET_MODE (rld[r].in_reg);
5410 else if (GET_CODE (rld[r].in_reg) == SUBREG
5411 && GET_CODE (SUBREG_REG (rld[r].in_reg)) == REG)
5413 byte = SUBREG_BYTE (rld[r].in_reg);
5414 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5415 if (regno < FIRST_PSEUDO_REGISTER)
5416 regno = subreg_regno (rld[r].in_reg);
5417 mode = GET_MODE (rld[r].in_reg);
5419 #ifdef AUTO_INC_DEC
5420 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5421 || GET_CODE (rld[r].in_reg) == PRE_DEC
5422 || GET_CODE (rld[r].in_reg) == POST_INC
5423 || GET_CODE (rld[r].in_reg) == POST_DEC)
5424 && GET_CODE (XEXP (rld[r].in_reg, 0)) == REG)
5426 regno = REGNO (XEXP (rld[r].in_reg, 0));
5427 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5428 rld[r].out = rld[r].in;
5430 #endif
5431 #if 0
5432 /* This won't work, since REGNO can be a pseudo reg number.
5433 Also, it takes much more hair to keep track of all the things
5434 that can invalidate an inherited reload of part of a pseudoreg. */
5435 else if (GET_CODE (rld[r].in) == SUBREG
5436 && GET_CODE (SUBREG_REG (rld[r].in)) == REG)
5437 regno = subreg_regno (rld[r].in);
5438 #endif
5440 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5442 enum reg_class class = rld[r].class, last_class;
5443 rtx last_reg = reg_last_reload_reg[regno];
5444 enum machine_mode need_mode;
5446 i = REGNO (last_reg);
5447 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5448 last_class = REGNO_REG_CLASS (i);
5450 if (byte == 0)
5451 need_mode = mode;
5452 else
5453 need_mode
5454 = smallest_mode_for_size (GET_MODE_SIZE (mode) + byte,
5455 GET_MODE_CLASS (mode));
5457 if (
5458 #ifdef CANNOT_CHANGE_MODE_CLASS
5459 (!REG_CANNOT_CHANGE_MODE_P (i, GET_MODE (last_reg),
5460 need_mode)
5462 #endif
5463 (GET_MODE_SIZE (GET_MODE (last_reg))
5464 >= GET_MODE_SIZE (need_mode))
5465 #ifdef CANNOT_CHANGE_MODE_CLASS
5467 #endif
5468 && reg_reloaded_contents[i] == regno
5469 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5470 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5471 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5472 /* Even if we can't use this register as a reload
5473 register, we might use it for reload_override_in,
5474 if copying it to the desired class is cheap
5475 enough. */
5476 || ((REGISTER_MOVE_COST (mode, last_class, class)
5477 < MEMORY_MOVE_COST (mode, class, 1))
5478 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5479 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5480 last_reg)
5481 == NO_REGS)
5482 #endif
5483 #ifdef SECONDARY_MEMORY_NEEDED
5484 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5485 mode)
5486 #endif
5489 && (rld[r].nregs == max_group_size
5490 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5492 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5493 rld[r].when_needed, rld[r].in,
5494 const0_rtx, r, 1))
5496 /* If a group is needed, verify that all the subsequent
5497 registers still have their values intact. */
5498 int nr = hard_regno_nregs[i][rld[r].mode];
5499 int k;
5501 for (k = 1; k < nr; k++)
5502 if (reg_reloaded_contents[i + k] != regno
5503 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5504 break;
5506 if (k == nr)
5508 int i1;
5509 int bad_for_class;
5511 last_reg = (GET_MODE (last_reg) == mode
5512 ? last_reg : gen_rtx_REG (mode, i));
5514 bad_for_class = 0;
5515 for (k = 0; k < nr; k++)
5516 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5517 i+k);
5519 /* We found a register that contains the
5520 value we need. If this register is the
5521 same as an `earlyclobber' operand of the
5522 current insn, just mark it as a place to
5523 reload from since we can't use it as the
5524 reload register itself. */
5526 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5527 if (reg_overlap_mentioned_for_reload_p
5528 (reg_last_reload_reg[regno],
5529 reload_earlyclobbers[i1]))
5530 break;
5532 if (i1 != n_earlyclobbers
5533 || ! (free_for_value_p (i, rld[r].mode,
5534 rld[r].opnum,
5535 rld[r].when_needed, rld[r].in,
5536 rld[r].out, r, 1))
5537 /* Don't use it if we'd clobber a pseudo reg. */
5538 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5539 && rld[r].out
5540 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5541 /* Don't clobber the frame pointer. */
5542 || (i == HARD_FRAME_POINTER_REGNUM
5543 && frame_pointer_needed
5544 && rld[r].out)
5545 /* Don't really use the inherited spill reg
5546 if we need it wider than we've got it. */
5547 || (GET_MODE_SIZE (rld[r].mode)
5548 > GET_MODE_SIZE (mode))
5549 || bad_for_class
5551 /* If find_reloads chose reload_out as reload
5552 register, stay with it - that leaves the
5553 inherited register for subsequent reloads. */
5554 || (rld[r].out && rld[r].reg_rtx
5555 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5557 if (! rld[r].optional)
5559 reload_override_in[r] = last_reg;
5560 reload_inheritance_insn[r]
5561 = reg_reloaded_insn[i];
5564 else
5566 int k;
5567 /* We can use this as a reload reg. */
5568 /* Mark the register as in use for this part of
5569 the insn. */
5570 mark_reload_reg_in_use (i,
5571 rld[r].opnum,
5572 rld[r].when_needed,
5573 rld[r].mode);
5574 rld[r].reg_rtx = last_reg;
5575 reload_inherited[r] = 1;
5576 reload_inheritance_insn[r]
5577 = reg_reloaded_insn[i];
5578 reload_spill_index[r] = i;
5579 for (k = 0; k < nr; k++)
5580 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5581 i + k);
5588 /* Here's another way to see if the value is already lying around. */
5589 if (inheritance
5590 && rld[r].in != 0
5591 && ! reload_inherited[r]
5592 && rld[r].out == 0
5593 && (CONSTANT_P (rld[r].in)
5594 || GET_CODE (rld[r].in) == PLUS
5595 || GET_CODE (rld[r].in) == REG
5596 || GET_CODE (rld[r].in) == MEM)
5597 && (rld[r].nregs == max_group_size
5598 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5599 search_equiv = rld[r].in;
5600 /* If this is an output reload from a simple move insn, look
5601 if an equivalence for the input is available. */
5602 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5604 rtx set = single_set (insn);
5606 if (set
5607 && rtx_equal_p (rld[r].out, SET_DEST (set))
5608 && CONSTANT_P (SET_SRC (set)))
5609 search_equiv = SET_SRC (set);
5612 if (search_equiv)
5614 rtx equiv
5615 = find_equiv_reg (search_equiv, insn, rld[r].class,
5616 -1, NULL, 0, rld[r].mode);
5617 int regno = 0;
5619 if (equiv != 0)
5621 if (GET_CODE (equiv) == REG)
5622 regno = REGNO (equiv);
5623 else if (GET_CODE (equiv) == SUBREG)
5625 /* This must be a SUBREG of a hard register.
5626 Make a new REG since this might be used in an
5627 address and not all machines support SUBREGs
5628 there. */
5629 regno = subreg_regno (equiv);
5630 equiv = gen_rtx_REG (rld[r].mode, regno);
5632 else
5633 abort ();
5636 /* If we found a spill reg, reject it unless it is free
5637 and of the desired class. */
5638 if (equiv != 0)
5640 int regs_used = 0;
5641 int bad_for_class = 0;
5642 int max_regno = regno + rld[r].nregs;
5644 for (i = regno; i < max_regno; i++)
5646 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
5648 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5652 if ((regs_used
5653 && ! free_for_value_p (regno, rld[r].mode,
5654 rld[r].opnum, rld[r].when_needed,
5655 rld[r].in, rld[r].out, r, 1))
5656 || bad_for_class)
5657 equiv = 0;
5660 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5661 equiv = 0;
5663 /* We found a register that contains the value we need.
5664 If this register is the same as an `earlyclobber' operand
5665 of the current insn, just mark it as a place to reload from
5666 since we can't use it as the reload register itself. */
5668 if (equiv != 0)
5669 for (i = 0; i < n_earlyclobbers; i++)
5670 if (reg_overlap_mentioned_for_reload_p (equiv,
5671 reload_earlyclobbers[i]))
5673 if (! rld[r].optional)
5674 reload_override_in[r] = equiv;
5675 equiv = 0;
5676 break;
5679 /* If the equiv register we have found is explicitly clobbered
5680 in the current insn, it depends on the reload type if we
5681 can use it, use it for reload_override_in, or not at all.
5682 In particular, we then can't use EQUIV for a
5683 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5685 if (equiv != 0)
5687 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5688 switch (rld[r].when_needed)
5690 case RELOAD_FOR_OTHER_ADDRESS:
5691 case RELOAD_FOR_INPADDR_ADDRESS:
5692 case RELOAD_FOR_INPUT_ADDRESS:
5693 case RELOAD_FOR_OPADDR_ADDR:
5694 break;
5695 case RELOAD_OTHER:
5696 case RELOAD_FOR_INPUT:
5697 case RELOAD_FOR_OPERAND_ADDRESS:
5698 if (! rld[r].optional)
5699 reload_override_in[r] = equiv;
5700 /* Fall through. */
5701 default:
5702 equiv = 0;
5703 break;
5705 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5706 switch (rld[r].when_needed)
5708 case RELOAD_FOR_OTHER_ADDRESS:
5709 case RELOAD_FOR_INPADDR_ADDRESS:
5710 case RELOAD_FOR_INPUT_ADDRESS:
5711 case RELOAD_FOR_OPADDR_ADDR:
5712 case RELOAD_FOR_OPERAND_ADDRESS:
5713 case RELOAD_FOR_INPUT:
5714 break;
5715 case RELOAD_OTHER:
5716 if (! rld[r].optional)
5717 reload_override_in[r] = equiv;
5718 /* Fall through. */
5719 default:
5720 equiv = 0;
5721 break;
5725 /* If we found an equivalent reg, say no code need be generated
5726 to load it, and use it as our reload reg. */
5727 if (equiv != 0
5728 && (regno != HARD_FRAME_POINTER_REGNUM
5729 || !frame_pointer_needed))
5731 int nr = hard_regno_nregs[regno][rld[r].mode];
5732 int k;
5733 rld[r].reg_rtx = equiv;
5734 reload_inherited[r] = 1;
5736 /* If reg_reloaded_valid is not set for this register,
5737 there might be a stale spill_reg_store lying around.
5738 We must clear it, since otherwise emit_reload_insns
5739 might delete the store. */
5740 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5741 spill_reg_store[regno] = NULL_RTX;
5742 /* If any of the hard registers in EQUIV are spill
5743 registers, mark them as in use for this insn. */
5744 for (k = 0; k < nr; k++)
5746 i = spill_reg_order[regno + k];
5747 if (i >= 0)
5749 mark_reload_reg_in_use (regno, rld[r].opnum,
5750 rld[r].when_needed,
5751 rld[r].mode);
5752 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5753 regno + k);
5759 /* If we found a register to use already, or if this is an optional
5760 reload, we are done. */
5761 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5762 continue;
5764 #if 0
5765 /* No longer needed for correct operation. Might or might
5766 not give better code on the average. Want to experiment? */
5768 /* See if there is a later reload that has a class different from our
5769 class that intersects our class or that requires less register
5770 than our reload. If so, we must allocate a register to this
5771 reload now, since that reload might inherit a previous reload
5772 and take the only available register in our class. Don't do this
5773 for optional reloads since they will force all previous reloads
5774 to be allocated. Also don't do this for reloads that have been
5775 turned off. */
5777 for (i = j + 1; i < n_reloads; i++)
5779 int s = reload_order[i];
5781 if ((rld[s].in == 0 && rld[s].out == 0
5782 && ! rld[s].secondary_p)
5783 || rld[s].optional)
5784 continue;
5786 if ((rld[s].class != rld[r].class
5787 && reg_classes_intersect_p (rld[r].class,
5788 rld[s].class))
5789 || rld[s].nregs < rld[r].nregs)
5790 break;
5793 if (i == n_reloads)
5794 continue;
5796 allocate_reload_reg (chain, r, j == n_reloads - 1);
5797 #endif
5800 /* Now allocate reload registers for anything non-optional that
5801 didn't get one yet. */
5802 for (j = 0; j < n_reloads; j++)
5804 int r = reload_order[j];
5806 /* Ignore reloads that got marked inoperative. */
5807 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
5808 continue;
5810 /* Skip reloads that already have a register allocated or are
5811 optional. */
5812 if (rld[r].reg_rtx != 0 || rld[r].optional)
5813 continue;
5815 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
5816 break;
5819 /* If that loop got all the way, we have won. */
5820 if (j == n_reloads)
5822 win = 1;
5823 break;
5826 /* Loop around and try without any inheritance. */
5829 if (! win)
5831 /* First undo everything done by the failed attempt
5832 to allocate with inheritance. */
5833 choose_reload_regs_init (chain, save_reload_reg_rtx);
5835 /* Some sanity tests to verify that the reloads found in the first
5836 pass are identical to the ones we have now. */
5837 if (chain->n_reloads != n_reloads)
5838 abort ();
5840 for (i = 0; i < n_reloads; i++)
5842 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5843 continue;
5844 if (chain->rld[i].when_needed != rld[i].when_needed)
5845 abort ();
5846 for (j = 0; j < n_spills; j++)
5847 if (spill_regs[j] == chain->rld[i].regno)
5848 if (! set_reload_reg (j, i))
5849 failed_reload (chain->insn, i);
5853 /* If we thought we could inherit a reload, because it seemed that
5854 nothing else wanted the same reload register earlier in the insn,
5855 verify that assumption, now that all reloads have been assigned.
5856 Likewise for reloads where reload_override_in has been set. */
5858 /* If doing expensive optimizations, do one preliminary pass that doesn't
5859 cancel any inheritance, but removes reloads that have been needed only
5860 for reloads that we know can be inherited. */
5861 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
5863 for (j = 0; j < n_reloads; j++)
5865 int r = reload_order[j];
5866 rtx check_reg;
5867 if (reload_inherited[r] && rld[r].reg_rtx)
5868 check_reg = rld[r].reg_rtx;
5869 else if (reload_override_in[r]
5870 && (GET_CODE (reload_override_in[r]) == REG
5871 || GET_CODE (reload_override_in[r]) == SUBREG))
5872 check_reg = reload_override_in[r];
5873 else
5874 continue;
5875 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5876 rld[r].opnum, rld[r].when_needed, rld[r].in,
5877 (reload_inherited[r]
5878 ? rld[r].out : const0_rtx),
5879 r, 1))
5881 if (pass)
5882 continue;
5883 reload_inherited[r] = 0;
5884 reload_override_in[r] = 0;
5886 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5887 reload_override_in, then we do not need its related
5888 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5889 likewise for other reload types.
5890 We handle this by removing a reload when its only replacement
5891 is mentioned in reload_in of the reload we are going to inherit.
5892 A special case are auto_inc expressions; even if the input is
5893 inherited, we still need the address for the output. We can
5894 recognize them because they have RELOAD_OUT set to RELOAD_IN.
5895 If we succeeded removing some reload and we are doing a preliminary
5896 pass just to remove such reloads, make another pass, since the
5897 removal of one reload might allow us to inherit another one. */
5898 else if (rld[r].in
5899 && rld[r].out != rld[r].in
5900 && remove_address_replacements (rld[r].in) && pass)
5901 pass = 2;
5905 /* Now that reload_override_in is known valid,
5906 actually override reload_in. */
5907 for (j = 0; j < n_reloads; j++)
5908 if (reload_override_in[j])
5909 rld[j].in = reload_override_in[j];
5911 /* If this reload won't be done because it has been canceled or is
5912 optional and not inherited, clear reload_reg_rtx so other
5913 routines (such as subst_reloads) don't get confused. */
5914 for (j = 0; j < n_reloads; j++)
5915 if (rld[j].reg_rtx != 0
5916 && ((rld[j].optional && ! reload_inherited[j])
5917 || (rld[j].in == 0 && rld[j].out == 0
5918 && ! rld[j].secondary_p)))
5920 int regno = true_regnum (rld[j].reg_rtx);
5922 if (spill_reg_order[regno] >= 0)
5923 clear_reload_reg_in_use (regno, rld[j].opnum,
5924 rld[j].when_needed, rld[j].mode);
5925 rld[j].reg_rtx = 0;
5926 reload_spill_index[j] = -1;
5929 /* Record which pseudos and which spill regs have output reloads. */
5930 for (j = 0; j < n_reloads; j++)
5932 int r = reload_order[j];
5934 i = reload_spill_index[r];
5936 /* I is nonneg if this reload uses a register.
5937 If rld[r].reg_rtx is 0, this is an optional reload
5938 that we opted to ignore. */
5939 if (rld[r].out_reg != 0 && GET_CODE (rld[r].out_reg) == REG
5940 && rld[r].reg_rtx != 0)
5942 int nregno = REGNO (rld[r].out_reg);
5943 int nr = 1;
5945 if (nregno < FIRST_PSEUDO_REGISTER)
5946 nr = hard_regno_nregs[nregno][rld[r].mode];
5948 while (--nr >= 0)
5949 reg_has_output_reload[nregno + nr] = 1;
5951 if (i >= 0)
5953 nr = hard_regno_nregs[i][rld[r].mode];
5954 while (--nr >= 0)
5955 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
5958 if (rld[r].when_needed != RELOAD_OTHER
5959 && rld[r].when_needed != RELOAD_FOR_OUTPUT
5960 && rld[r].when_needed != RELOAD_FOR_INSN)
5961 abort ();
5966 /* Deallocate the reload register for reload R. This is called from
5967 remove_address_replacements. */
5969 void
5970 deallocate_reload_reg (int r)
5972 int regno;
5974 if (! rld[r].reg_rtx)
5975 return;
5976 regno = true_regnum (rld[r].reg_rtx);
5977 rld[r].reg_rtx = 0;
5978 if (spill_reg_order[regno] >= 0)
5979 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
5980 rld[r].mode);
5981 reload_spill_index[r] = -1;
5984 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
5985 reloads of the same item for fear that we might not have enough reload
5986 registers. However, normally they will get the same reload register
5987 and hence actually need not be loaded twice.
5989 Here we check for the most common case of this phenomenon: when we have
5990 a number of reloads for the same object, each of which were allocated
5991 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5992 reload, and is not modified in the insn itself. If we find such,
5993 merge all the reloads and set the resulting reload to RELOAD_OTHER.
5994 This will not increase the number of spill registers needed and will
5995 prevent redundant code. */
5997 static void
5998 merge_assigned_reloads (rtx insn)
6000 int i, j;
6002 /* Scan all the reloads looking for ones that only load values and
6003 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6004 assigned and not modified by INSN. */
6006 for (i = 0; i < n_reloads; i++)
6008 int conflicting_input = 0;
6009 int max_input_address_opnum = -1;
6010 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6012 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6013 || rld[i].out != 0 || rld[i].reg_rtx == 0
6014 || reg_set_p (rld[i].reg_rtx, insn))
6015 continue;
6017 /* Look at all other reloads. Ensure that the only use of this
6018 reload_reg_rtx is in a reload that just loads the same value
6019 as we do. Note that any secondary reloads must be of the identical
6020 class since the values, modes, and result registers are the
6021 same, so we need not do anything with any secondary reloads. */
6023 for (j = 0; j < n_reloads; j++)
6025 if (i == j || rld[j].reg_rtx == 0
6026 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6027 rld[i].reg_rtx))
6028 continue;
6030 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6031 && rld[j].opnum > max_input_address_opnum)
6032 max_input_address_opnum = rld[j].opnum;
6034 /* If the reload regs aren't exactly the same (e.g, different modes)
6035 or if the values are different, we can't merge this reload.
6036 But if it is an input reload, we might still merge
6037 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6039 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6040 || rld[j].out != 0 || rld[j].in == 0
6041 || ! rtx_equal_p (rld[i].in, rld[j].in))
6043 if (rld[j].when_needed != RELOAD_FOR_INPUT
6044 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6045 || rld[i].opnum > rld[j].opnum)
6046 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6047 break;
6048 conflicting_input = 1;
6049 if (min_conflicting_input_opnum > rld[j].opnum)
6050 min_conflicting_input_opnum = rld[j].opnum;
6054 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6055 we, in fact, found any matching reloads. */
6057 if (j == n_reloads
6058 && max_input_address_opnum <= min_conflicting_input_opnum)
6060 for (j = 0; j < n_reloads; j++)
6061 if (i != j && rld[j].reg_rtx != 0
6062 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6063 && (! conflicting_input
6064 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6065 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6067 rld[i].when_needed = RELOAD_OTHER;
6068 rld[j].in = 0;
6069 reload_spill_index[j] = -1;
6070 transfer_replacements (i, j);
6073 /* If this is now RELOAD_OTHER, look for any reloads that load
6074 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6075 if they were for inputs, RELOAD_OTHER for outputs. Note that
6076 this test is equivalent to looking for reloads for this operand
6077 number. */
6078 /* We must take special care when there are two or more reloads to
6079 be merged and a RELOAD_FOR_OUTPUT_ADDRESS reload that loads the
6080 same value or a part of it; we must not change its type if there
6081 is a conflicting input. */
6083 if (rld[i].when_needed == RELOAD_OTHER)
6084 for (j = 0; j < n_reloads; j++)
6085 if (rld[j].in != 0
6086 && rld[j].when_needed != RELOAD_OTHER
6087 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6088 && (! conflicting_input
6089 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6090 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6091 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6092 rld[i].in))
6094 int k;
6096 rld[j].when_needed
6097 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6098 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6099 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6101 /* Check to see if we accidentally converted two reloads
6102 that use the same reload register with different inputs
6103 to the same type. If so, the resulting code won't work,
6104 so abort. */
6105 if (rld[j].reg_rtx)
6106 for (k = 0; k < j; k++)
6107 if (rld[k].in != 0 && rld[k].reg_rtx != 0
6108 && rld[k].when_needed == rld[j].when_needed
6109 && rtx_equal_p (rld[k].reg_rtx, rld[j].reg_rtx)
6110 && ! rtx_equal_p (rld[k].in, rld[j].in))
6111 abort ();
6117 /* These arrays are filled by emit_reload_insns and its subroutines. */
6118 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6119 static rtx other_input_address_reload_insns = 0;
6120 static rtx other_input_reload_insns = 0;
6121 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6122 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6123 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6124 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6125 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6126 static rtx operand_reload_insns = 0;
6127 static rtx other_operand_reload_insns = 0;
6128 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6130 /* Values to be put in spill_reg_store are put here first. */
6131 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6132 static HARD_REG_SET reg_reloaded_died;
6134 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6135 has the number J. OLD contains the value to be used as input. */
6137 static void
6138 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6139 rtx old, int j)
6141 rtx insn = chain->insn;
6142 rtx reloadreg = rl->reg_rtx;
6143 rtx oldequiv_reg = 0;
6144 rtx oldequiv = 0;
6145 int special = 0;
6146 enum machine_mode mode;
6147 rtx *where;
6149 /* Determine the mode to reload in.
6150 This is very tricky because we have three to choose from.
6151 There is the mode the insn operand wants (rl->inmode).
6152 There is the mode of the reload register RELOADREG.
6153 There is the intrinsic mode of the operand, which we could find
6154 by stripping some SUBREGs.
6155 It turns out that RELOADREG's mode is irrelevant:
6156 we can change that arbitrarily.
6158 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6159 then the reload reg may not support QImode moves, so use SImode.
6160 If foo is in memory due to spilling a pseudo reg, this is safe,
6161 because the QImode value is in the least significant part of a
6162 slot big enough for a SImode. If foo is some other sort of
6163 memory reference, then it is impossible to reload this case,
6164 so previous passes had better make sure this never happens.
6166 Then consider a one-word union which has SImode and one of its
6167 members is a float, being fetched as (SUBREG:SF union:SI).
6168 We must fetch that as SFmode because we could be loading into
6169 a float-only register. In this case OLD's mode is correct.
6171 Consider an immediate integer: it has VOIDmode. Here we need
6172 to get a mode from something else.
6174 In some cases, there is a fourth mode, the operand's
6175 containing mode. If the insn specifies a containing mode for
6176 this operand, it overrides all others.
6178 I am not sure whether the algorithm here is always right,
6179 but it does the right things in those cases. */
6181 mode = GET_MODE (old);
6182 if (mode == VOIDmode)
6183 mode = rl->inmode;
6185 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6186 /* If we need a secondary register for this operation, see if
6187 the value is already in a register in that class. Don't
6188 do this if the secondary register will be used as a scratch
6189 register. */
6191 if (rl->secondary_in_reload >= 0
6192 && rl->secondary_in_icode == CODE_FOR_nothing
6193 && optimize)
6194 oldequiv
6195 = find_equiv_reg (old, insn,
6196 rld[rl->secondary_in_reload].class,
6197 -1, NULL, 0, mode);
6198 #endif
6200 /* If reloading from memory, see if there is a register
6201 that already holds the same value. If so, reload from there.
6202 We can pass 0 as the reload_reg_p argument because
6203 any other reload has either already been emitted,
6204 in which case find_equiv_reg will see the reload-insn,
6205 or has yet to be emitted, in which case it doesn't matter
6206 because we will use this equiv reg right away. */
6208 if (oldequiv == 0 && optimize
6209 && (GET_CODE (old) == MEM
6210 || (GET_CODE (old) == REG
6211 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6212 && reg_renumber[REGNO (old)] < 0)))
6213 oldequiv = find_equiv_reg (old, insn, ALL_REGS, -1, NULL, 0, mode);
6215 if (oldequiv)
6217 unsigned int regno = true_regnum (oldequiv);
6219 /* Don't use OLDEQUIV if any other reload changes it at an
6220 earlier stage of this insn or at this stage. */
6221 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6222 rl->in, const0_rtx, j, 0))
6223 oldequiv = 0;
6225 /* If it is no cheaper to copy from OLDEQUIV into the
6226 reload register than it would be to move from memory,
6227 don't use it. Likewise, if we need a secondary register
6228 or memory. */
6230 if (oldequiv != 0
6231 && (((enum reg_class) REGNO_REG_CLASS (regno) != rl->class
6232 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6233 rl->class)
6234 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6235 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6236 || (SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6237 mode, oldequiv)
6238 != NO_REGS)
6239 #endif
6240 #ifdef SECONDARY_MEMORY_NEEDED
6241 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6242 rl->class,
6243 mode)
6244 #endif
6246 oldequiv = 0;
6249 /* delete_output_reload is only invoked properly if old contains
6250 the original pseudo register. Since this is replaced with a
6251 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6252 find the pseudo in RELOAD_IN_REG. */
6253 if (oldequiv == 0
6254 && reload_override_in[j]
6255 && GET_CODE (rl->in_reg) == REG)
6257 oldequiv = old;
6258 old = rl->in_reg;
6260 if (oldequiv == 0)
6261 oldequiv = old;
6262 else if (GET_CODE (oldequiv) == REG)
6263 oldequiv_reg = oldequiv;
6264 else if (GET_CODE (oldequiv) == SUBREG)
6265 oldequiv_reg = SUBREG_REG (oldequiv);
6267 /* If we are reloading from a register that was recently stored in
6268 with an output-reload, see if we can prove there was
6269 actually no need to store the old value in it. */
6271 if (optimize && GET_CODE (oldequiv) == REG
6272 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6273 && spill_reg_store[REGNO (oldequiv)]
6274 && GET_CODE (old) == REG
6275 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6276 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6277 rl->out_reg)))
6278 delete_output_reload (insn, j, REGNO (oldequiv));
6280 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6281 then load RELOADREG from OLDEQUIV. Note that we cannot use
6282 gen_lowpart_common since it can do the wrong thing when
6283 RELOADREG has a multi-word mode. Note that RELOADREG
6284 must always be a REG here. */
6286 if (GET_MODE (reloadreg) != mode)
6287 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6288 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6289 oldequiv = SUBREG_REG (oldequiv);
6290 if (GET_MODE (oldequiv) != VOIDmode
6291 && mode != GET_MODE (oldequiv))
6292 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6294 /* Switch to the right place to emit the reload insns. */
6295 switch (rl->when_needed)
6297 case RELOAD_OTHER:
6298 where = &other_input_reload_insns;
6299 break;
6300 case RELOAD_FOR_INPUT:
6301 where = &input_reload_insns[rl->opnum];
6302 break;
6303 case RELOAD_FOR_INPUT_ADDRESS:
6304 where = &input_address_reload_insns[rl->opnum];
6305 break;
6306 case RELOAD_FOR_INPADDR_ADDRESS:
6307 where = &inpaddr_address_reload_insns[rl->opnum];
6308 break;
6309 case RELOAD_FOR_OUTPUT_ADDRESS:
6310 where = &output_address_reload_insns[rl->opnum];
6311 break;
6312 case RELOAD_FOR_OUTADDR_ADDRESS:
6313 where = &outaddr_address_reload_insns[rl->opnum];
6314 break;
6315 case RELOAD_FOR_OPERAND_ADDRESS:
6316 where = &operand_reload_insns;
6317 break;
6318 case RELOAD_FOR_OPADDR_ADDR:
6319 where = &other_operand_reload_insns;
6320 break;
6321 case RELOAD_FOR_OTHER_ADDRESS:
6322 where = &other_input_address_reload_insns;
6323 break;
6324 default:
6325 abort ();
6328 push_to_sequence (*where);
6330 /* Auto-increment addresses must be reloaded in a special way. */
6331 if (rl->out && ! rl->out_reg)
6333 /* We are not going to bother supporting the case where a
6334 incremented register can't be copied directly from
6335 OLDEQUIV since this seems highly unlikely. */
6336 if (rl->secondary_in_reload >= 0)
6337 abort ();
6339 if (reload_inherited[j])
6340 oldequiv = reloadreg;
6342 old = XEXP (rl->in_reg, 0);
6344 if (optimize && GET_CODE (oldequiv) == REG
6345 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6346 && spill_reg_store[REGNO (oldequiv)]
6347 && GET_CODE (old) == REG
6348 && (dead_or_set_p (insn,
6349 spill_reg_stored_to[REGNO (oldequiv)])
6350 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6351 old)))
6352 delete_output_reload (insn, j, REGNO (oldequiv));
6354 /* Prevent normal processing of this reload. */
6355 special = 1;
6356 /* Output a special code sequence for this case. */
6357 new_spill_reg_store[REGNO (reloadreg)]
6358 = inc_for_reload (reloadreg, oldequiv, rl->out,
6359 rl->inc);
6362 /* If we are reloading a pseudo-register that was set by the previous
6363 insn, see if we can get rid of that pseudo-register entirely
6364 by redirecting the previous insn into our reload register. */
6366 else if (optimize && GET_CODE (old) == REG
6367 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6368 && dead_or_set_p (insn, old)
6369 /* This is unsafe if some other reload
6370 uses the same reg first. */
6371 && ! conflicts_with_override (reloadreg)
6372 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6373 rl->when_needed, old, rl->out, j, 0))
6375 rtx temp = PREV_INSN (insn);
6376 while (temp && GET_CODE (temp) == NOTE)
6377 temp = PREV_INSN (temp);
6378 if (temp
6379 && GET_CODE (temp) == INSN
6380 && GET_CODE (PATTERN (temp)) == SET
6381 && SET_DEST (PATTERN (temp)) == old
6382 /* Make sure we can access insn_operand_constraint. */
6383 && asm_noperands (PATTERN (temp)) < 0
6384 /* This is unsafe if operand occurs more than once in current
6385 insn. Perhaps some occurrences aren't reloaded. */
6386 && count_occurrences (PATTERN (insn), old, 0) == 1)
6388 rtx old = SET_DEST (PATTERN (temp));
6389 /* Store into the reload register instead of the pseudo. */
6390 SET_DEST (PATTERN (temp)) = reloadreg;
6392 /* Verify that resulting insn is valid. */
6393 extract_insn (temp);
6394 if (constrain_operands (1))
6396 /* If the previous insn is an output reload, the source is
6397 a reload register, and its spill_reg_store entry will
6398 contain the previous destination. This is now
6399 invalid. */
6400 if (GET_CODE (SET_SRC (PATTERN (temp))) == REG
6401 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6403 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6404 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6407 /* If these are the only uses of the pseudo reg,
6408 pretend for GDB it lives in the reload reg we used. */
6409 if (REG_N_DEATHS (REGNO (old)) == 1
6410 && REG_N_SETS (REGNO (old)) == 1)
6412 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6413 alter_reg (REGNO (old), -1);
6415 special = 1;
6417 else
6419 SET_DEST (PATTERN (temp)) = old;
6424 /* We can't do that, so output an insn to load RELOADREG. */
6426 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6427 /* If we have a secondary reload, pick up the secondary register
6428 and icode, if any. If OLDEQUIV and OLD are different or
6429 if this is an in-out reload, recompute whether or not we
6430 still need a secondary register and what the icode should
6431 be. If we still need a secondary register and the class or
6432 icode is different, go back to reloading from OLD if using
6433 OLDEQUIV means that we got the wrong type of register. We
6434 cannot have different class or icode due to an in-out reload
6435 because we don't make such reloads when both the input and
6436 output need secondary reload registers. */
6438 if (! special && rl->secondary_in_reload >= 0)
6440 rtx second_reload_reg = 0;
6441 int secondary_reload = rl->secondary_in_reload;
6442 rtx real_oldequiv = oldequiv;
6443 rtx real_old = old;
6444 rtx tmp;
6445 enum insn_code icode;
6447 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6448 and similarly for OLD.
6449 See comments in get_secondary_reload in reload.c. */
6450 /* If it is a pseudo that cannot be replaced with its
6451 equivalent MEM, we must fall back to reload_in, which
6452 will have all the necessary substitutions registered.
6453 Likewise for a pseudo that can't be replaced with its
6454 equivalent constant.
6456 Take extra care for subregs of such pseudos. Note that
6457 we cannot use reg_equiv_mem in this case because it is
6458 not in the right mode. */
6460 tmp = oldequiv;
6461 if (GET_CODE (tmp) == SUBREG)
6462 tmp = SUBREG_REG (tmp);
6463 if (GET_CODE (tmp) == REG
6464 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6465 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6466 || reg_equiv_constant[REGNO (tmp)] != 0))
6468 if (! reg_equiv_mem[REGNO (tmp)]
6469 || num_not_at_initial_offset
6470 || GET_CODE (oldequiv) == SUBREG)
6471 real_oldequiv = rl->in;
6472 else
6473 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6476 tmp = old;
6477 if (GET_CODE (tmp) == SUBREG)
6478 tmp = SUBREG_REG (tmp);
6479 if (GET_CODE (tmp) == REG
6480 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6481 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6482 || reg_equiv_constant[REGNO (tmp)] != 0))
6484 if (! reg_equiv_mem[REGNO (tmp)]
6485 || num_not_at_initial_offset
6486 || GET_CODE (old) == SUBREG)
6487 real_old = rl->in;
6488 else
6489 real_old = reg_equiv_mem[REGNO (tmp)];
6492 second_reload_reg = rld[secondary_reload].reg_rtx;
6493 icode = rl->secondary_in_icode;
6495 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6496 || (rl->in != 0 && rl->out != 0))
6498 enum reg_class new_class
6499 = SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6500 mode, real_oldequiv);
6502 if (new_class == NO_REGS)
6503 second_reload_reg = 0;
6504 else
6506 enum insn_code new_icode;
6507 enum machine_mode new_mode;
6509 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6510 REGNO (second_reload_reg)))
6511 oldequiv = old, real_oldequiv = real_old;
6512 else
6514 new_icode = reload_in_optab[(int) mode];
6515 if (new_icode != CODE_FOR_nothing
6516 && ((insn_data[(int) new_icode].operand[0].predicate
6517 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6518 (reloadreg, mode)))
6519 || (insn_data[(int) new_icode].operand[1].predicate
6520 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6521 (real_oldequiv, mode)))))
6522 new_icode = CODE_FOR_nothing;
6524 if (new_icode == CODE_FOR_nothing)
6525 new_mode = mode;
6526 else
6527 new_mode = insn_data[(int) new_icode].operand[2].mode;
6529 if (GET_MODE (second_reload_reg) != new_mode)
6531 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6532 new_mode))
6533 oldequiv = old, real_oldequiv = real_old;
6534 else
6535 second_reload_reg
6536 = reload_adjust_reg_for_mode (second_reload_reg,
6537 new_mode);
6543 /* If we still need a secondary reload register, check
6544 to see if it is being used as a scratch or intermediate
6545 register and generate code appropriately. If we need
6546 a scratch register, use REAL_OLDEQUIV since the form of
6547 the insn may depend on the actual address if it is
6548 a MEM. */
6550 if (second_reload_reg)
6552 if (icode != CODE_FOR_nothing)
6554 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6555 second_reload_reg));
6556 special = 1;
6558 else
6560 /* See if we need a scratch register to load the
6561 intermediate register (a tertiary reload). */
6562 enum insn_code tertiary_icode
6563 = rld[secondary_reload].secondary_in_icode;
6565 if (tertiary_icode != CODE_FOR_nothing)
6567 rtx third_reload_reg
6568 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
6570 emit_insn ((GEN_FCN (tertiary_icode)
6571 (second_reload_reg, real_oldequiv,
6572 third_reload_reg)));
6574 else
6575 gen_reload (second_reload_reg, real_oldequiv,
6576 rl->opnum,
6577 rl->when_needed);
6579 oldequiv = second_reload_reg;
6583 #endif
6585 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6587 rtx real_oldequiv = oldequiv;
6589 if ((GET_CODE (oldequiv) == REG
6590 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6591 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6592 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6593 || (GET_CODE (oldequiv) == SUBREG
6594 && GET_CODE (SUBREG_REG (oldequiv)) == REG
6595 && (REGNO (SUBREG_REG (oldequiv))
6596 >= FIRST_PSEUDO_REGISTER)
6597 && ((reg_equiv_memory_loc
6598 [REGNO (SUBREG_REG (oldequiv))] != 0)
6599 || (reg_equiv_constant
6600 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6601 || (CONSTANT_P (oldequiv)
6602 && (PREFERRED_RELOAD_CLASS (oldequiv,
6603 REGNO_REG_CLASS (REGNO (reloadreg)))
6604 == NO_REGS)))
6605 real_oldequiv = rl->in;
6606 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6607 rl->when_needed);
6610 if (flag_non_call_exceptions)
6611 copy_eh_notes (insn, get_insns ());
6613 /* End this sequence. */
6614 *where = get_insns ();
6615 end_sequence ();
6617 /* Update reload_override_in so that delete_address_reloads_1
6618 can see the actual register usage. */
6619 if (oldequiv_reg)
6620 reload_override_in[j] = oldequiv;
6623 /* Generate insns to for the output reload RL, which is for the insn described
6624 by CHAIN and has the number J. */
6625 static void
6626 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
6627 int j)
6629 rtx reloadreg = rl->reg_rtx;
6630 rtx insn = chain->insn;
6631 int special = 0;
6632 rtx old = rl->out;
6633 enum machine_mode mode = GET_MODE (old);
6634 rtx p;
6636 if (rl->when_needed == RELOAD_OTHER)
6637 start_sequence ();
6638 else
6639 push_to_sequence (output_reload_insns[rl->opnum]);
6641 /* Determine the mode to reload in.
6642 See comments above (for input reloading). */
6644 if (mode == VOIDmode)
6646 /* VOIDmode should never happen for an output. */
6647 if (asm_noperands (PATTERN (insn)) < 0)
6648 /* It's the compiler's fault. */
6649 fatal_insn ("VOIDmode on an output", insn);
6650 error_for_asm (insn, "output operand is constant in `asm'");
6651 /* Prevent crash--use something we know is valid. */
6652 mode = word_mode;
6653 old = gen_rtx_REG (mode, REGNO (reloadreg));
6656 if (GET_MODE (reloadreg) != mode)
6657 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6659 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6661 /* If we need two reload regs, set RELOADREG to the intermediate
6662 one, since it will be stored into OLD. We might need a secondary
6663 register only for an input reload, so check again here. */
6665 if (rl->secondary_out_reload >= 0)
6667 rtx real_old = old;
6669 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
6670 && reg_equiv_mem[REGNO (old)] != 0)
6671 real_old = reg_equiv_mem[REGNO (old)];
6673 if ((SECONDARY_OUTPUT_RELOAD_CLASS (rl->class,
6674 mode, real_old)
6675 != NO_REGS))
6677 rtx second_reloadreg = reloadreg;
6678 reloadreg = rld[rl->secondary_out_reload].reg_rtx;
6680 /* See if RELOADREG is to be used as a scratch register
6681 or as an intermediate register. */
6682 if (rl->secondary_out_icode != CODE_FOR_nothing)
6684 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6685 (real_old, second_reloadreg, reloadreg)));
6686 special = 1;
6688 else
6690 /* See if we need both a scratch and intermediate reload
6691 register. */
6693 int secondary_reload = rl->secondary_out_reload;
6694 enum insn_code tertiary_icode
6695 = rld[secondary_reload].secondary_out_icode;
6697 if (GET_MODE (reloadreg) != mode)
6698 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6700 if (tertiary_icode != CODE_FOR_nothing)
6702 rtx third_reloadreg
6703 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
6704 rtx tem;
6706 /* Copy primary reload reg to secondary reload reg.
6707 (Note that these have been swapped above, then
6708 secondary reload reg to OLD using our insn.) */
6710 /* If REAL_OLD is a paradoxical SUBREG, remove it
6711 and try to put the opposite SUBREG on
6712 RELOADREG. */
6713 if (GET_CODE (real_old) == SUBREG
6714 && (GET_MODE_SIZE (GET_MODE (real_old))
6715 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6716 && 0 != (tem = gen_lowpart_common
6717 (GET_MODE (SUBREG_REG (real_old)),
6718 reloadreg)))
6719 real_old = SUBREG_REG (real_old), reloadreg = tem;
6721 gen_reload (reloadreg, second_reloadreg,
6722 rl->opnum, rl->when_needed);
6723 emit_insn ((GEN_FCN (tertiary_icode)
6724 (real_old, reloadreg, third_reloadreg)));
6725 special = 1;
6728 else
6729 /* Copy between the reload regs here and then to
6730 OUT later. */
6732 gen_reload (reloadreg, second_reloadreg,
6733 rl->opnum, rl->when_needed);
6737 #endif
6739 /* Output the last reload insn. */
6740 if (! special)
6742 rtx set;
6744 /* Don't output the last reload if OLD is not the dest of
6745 INSN and is in the src and is clobbered by INSN. */
6746 if (! flag_expensive_optimizations
6747 || GET_CODE (old) != REG
6748 || !(set = single_set (insn))
6749 || rtx_equal_p (old, SET_DEST (set))
6750 || !reg_mentioned_p (old, SET_SRC (set))
6751 || !regno_clobbered_p (REGNO (old), insn, rl->mode, 0))
6752 gen_reload (old, reloadreg, rl->opnum,
6753 rl->when_needed);
6756 /* Look at all insns we emitted, just to be safe. */
6757 for (p = get_insns (); p; p = NEXT_INSN (p))
6758 if (INSN_P (p))
6760 rtx pat = PATTERN (p);
6762 /* If this output reload doesn't come from a spill reg,
6763 clear any memory of reloaded copies of the pseudo reg.
6764 If this output reload comes from a spill reg,
6765 reg_has_output_reload will make this do nothing. */
6766 note_stores (pat, forget_old_reloads_1, NULL);
6768 if (reg_mentioned_p (rl->reg_rtx, pat))
6770 rtx set = single_set (insn);
6771 if (reload_spill_index[j] < 0
6772 && set
6773 && SET_SRC (set) == rl->reg_rtx)
6775 int src = REGNO (SET_SRC (set));
6777 reload_spill_index[j] = src;
6778 SET_HARD_REG_BIT (reg_is_output_reload, src);
6779 if (find_regno_note (insn, REG_DEAD, src))
6780 SET_HARD_REG_BIT (reg_reloaded_died, src);
6782 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6784 int s = rl->secondary_out_reload;
6785 set = single_set (p);
6786 /* If this reload copies only to the secondary reload
6787 register, the secondary reload does the actual
6788 store. */
6789 if (s >= 0 && set == NULL_RTX)
6790 /* We can't tell what function the secondary reload
6791 has and where the actual store to the pseudo is
6792 made; leave new_spill_reg_store alone. */
6794 else if (s >= 0
6795 && SET_SRC (set) == rl->reg_rtx
6796 && SET_DEST (set) == rld[s].reg_rtx)
6798 /* Usually the next instruction will be the
6799 secondary reload insn; if we can confirm
6800 that it is, setting new_spill_reg_store to
6801 that insn will allow an extra optimization. */
6802 rtx s_reg = rld[s].reg_rtx;
6803 rtx next = NEXT_INSN (p);
6804 rld[s].out = rl->out;
6805 rld[s].out_reg = rl->out_reg;
6806 set = single_set (next);
6807 if (set && SET_SRC (set) == s_reg
6808 && ! new_spill_reg_store[REGNO (s_reg)])
6810 SET_HARD_REG_BIT (reg_is_output_reload,
6811 REGNO (s_reg));
6812 new_spill_reg_store[REGNO (s_reg)] = next;
6815 else
6816 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6821 if (rl->when_needed == RELOAD_OTHER)
6823 emit_insn (other_output_reload_insns[rl->opnum]);
6824 other_output_reload_insns[rl->opnum] = get_insns ();
6826 else
6827 output_reload_insns[rl->opnum] = get_insns ();
6829 if (flag_non_call_exceptions)
6830 copy_eh_notes (insn, get_insns ());
6832 end_sequence ();
6835 /* Do input reloading for reload RL, which is for the insn described by CHAIN
6836 and has the number J. */
6837 static void
6838 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
6840 rtx insn = chain->insn;
6841 rtx old = (rl->in && GET_CODE (rl->in) == MEM
6842 ? rl->in_reg : rl->in);
6844 if (old != 0
6845 /* AUTO_INC reloads need to be handled even if inherited. We got an
6846 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6847 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
6848 && ! rtx_equal_p (rl->reg_rtx, old)
6849 && rl->reg_rtx != 0)
6850 emit_input_reload_insns (chain, rld + j, old, j);
6852 /* When inheriting a wider reload, we have a MEM in rl->in,
6853 e.g. inheriting a SImode output reload for
6854 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6855 if (optimize && reload_inherited[j] && rl->in
6856 && GET_CODE (rl->in) == MEM
6857 && GET_CODE (rl->in_reg) == MEM
6858 && reload_spill_index[j] >= 0
6859 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6860 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
6862 /* If we are reloading a register that was recently stored in with an
6863 output-reload, see if we can prove there was
6864 actually no need to store the old value in it. */
6866 if (optimize
6867 && (reload_inherited[j] || reload_override_in[j])
6868 && rl->reg_rtx
6869 && GET_CODE (rl->reg_rtx) == REG
6870 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
6871 #if 0
6872 /* There doesn't seem to be any reason to restrict this to pseudos
6873 and doing so loses in the case where we are copying from a
6874 register of the wrong class. */
6875 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
6876 >= FIRST_PSEUDO_REGISTER)
6877 #endif
6878 /* The insn might have already some references to stackslots
6879 replaced by MEMs, while reload_out_reg still names the
6880 original pseudo. */
6881 && (dead_or_set_p (insn,
6882 spill_reg_stored_to[REGNO (rl->reg_rtx)])
6883 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
6884 rl->out_reg)))
6885 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
6888 /* Do output reloading for reload RL, which is for the insn described by
6889 CHAIN and has the number J.
6890 ??? At some point we need to support handling output reloads of
6891 JUMP_INSNs or insns that set cc0. */
6892 static void
6893 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
6895 rtx note, old;
6896 rtx insn = chain->insn;
6897 /* If this is an output reload that stores something that is
6898 not loaded in this same reload, see if we can eliminate a previous
6899 store. */
6900 rtx pseudo = rl->out_reg;
6902 if (pseudo
6903 && optimize
6904 && GET_CODE (pseudo) == REG
6905 && ! rtx_equal_p (rl->in_reg, pseudo)
6906 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
6907 && reg_last_reload_reg[REGNO (pseudo)])
6909 int pseudo_no = REGNO (pseudo);
6910 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
6912 /* We don't need to test full validity of last_regno for
6913 inherit here; we only want to know if the store actually
6914 matches the pseudo. */
6915 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
6916 && reg_reloaded_contents[last_regno] == pseudo_no
6917 && spill_reg_store[last_regno]
6918 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
6919 delete_output_reload (insn, j, last_regno);
6922 old = rl->out_reg;
6923 if (old == 0
6924 || rl->reg_rtx == old
6925 || rl->reg_rtx == 0)
6926 return;
6928 /* An output operand that dies right away does need a reload,
6929 but need not be copied from it. Show the new location in the
6930 REG_UNUSED note. */
6931 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
6932 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6934 XEXP (note, 0) = rl->reg_rtx;
6935 return;
6937 /* Likewise for a SUBREG of an operand that dies. */
6938 else if (GET_CODE (old) == SUBREG
6939 && GET_CODE (SUBREG_REG (old)) == REG
6940 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6941 SUBREG_REG (old))))
6943 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6944 rl->reg_rtx);
6945 return;
6947 else if (GET_CODE (old) == SCRATCH)
6948 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6949 but we don't want to make an output reload. */
6950 return;
6952 /* If is a JUMP_INSN, we can't support output reloads yet. */
6953 if (GET_CODE (insn) == JUMP_INSN)
6954 abort ();
6956 emit_output_reload_insns (chain, rld + j, j);
6959 /* Output insns to reload values in and out of the chosen reload regs. */
6961 static void
6962 emit_reload_insns (struct insn_chain *chain)
6964 rtx insn = chain->insn;
6966 int j;
6968 CLEAR_HARD_REG_SET (reg_reloaded_died);
6970 for (j = 0; j < reload_n_operands; j++)
6971 input_reload_insns[j] = input_address_reload_insns[j]
6972 = inpaddr_address_reload_insns[j]
6973 = output_reload_insns[j] = output_address_reload_insns[j]
6974 = outaddr_address_reload_insns[j]
6975 = other_output_reload_insns[j] = 0;
6976 other_input_address_reload_insns = 0;
6977 other_input_reload_insns = 0;
6978 operand_reload_insns = 0;
6979 other_operand_reload_insns = 0;
6981 /* Dump reloads into the dump file. */
6982 if (dump_file)
6984 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
6985 debug_reload_to_stream (dump_file);
6988 /* Now output the instructions to copy the data into and out of the
6989 reload registers. Do these in the order that the reloads were reported,
6990 since reloads of base and index registers precede reloads of operands
6991 and the operands may need the base and index registers reloaded. */
6993 for (j = 0; j < n_reloads; j++)
6995 if (rld[j].reg_rtx
6996 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6997 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6999 do_input_reload (chain, rld + j, j);
7000 do_output_reload (chain, rld + j, j);
7003 /* Now write all the insns we made for reloads in the order expected by
7004 the allocation functions. Prior to the insn being reloaded, we write
7005 the following reloads:
7007 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7009 RELOAD_OTHER reloads.
7011 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7012 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7013 RELOAD_FOR_INPUT reload for the operand.
7015 RELOAD_FOR_OPADDR_ADDRS reloads.
7017 RELOAD_FOR_OPERAND_ADDRESS reloads.
7019 After the insn being reloaded, we write the following:
7021 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7022 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7023 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7024 reloads for the operand. The RELOAD_OTHER output reloads are
7025 output in descending order by reload number. */
7027 emit_insn_before_sameloc (other_input_address_reload_insns, insn);
7028 emit_insn_before_sameloc (other_input_reload_insns, insn);
7030 for (j = 0; j < reload_n_operands; j++)
7032 emit_insn_before_sameloc (inpaddr_address_reload_insns[j], insn);
7033 emit_insn_before_sameloc (input_address_reload_insns[j], insn);
7034 emit_insn_before_sameloc (input_reload_insns[j], insn);
7037 emit_insn_before_sameloc (other_operand_reload_insns, insn);
7038 emit_insn_before_sameloc (operand_reload_insns, insn);
7040 for (j = 0; j < reload_n_operands; j++)
7042 rtx x = emit_insn_after_sameloc (outaddr_address_reload_insns[j], insn);
7043 x = emit_insn_after_sameloc (output_address_reload_insns[j], x);
7044 x = emit_insn_after_sameloc (output_reload_insns[j], x);
7045 emit_insn_after_sameloc (other_output_reload_insns[j], x);
7048 /* For all the spill regs newly reloaded in this instruction,
7049 record what they were reloaded from, so subsequent instructions
7050 can inherit the reloads.
7052 Update spill_reg_store for the reloads of this insn.
7053 Copy the elements that were updated in the loop above. */
7055 for (j = 0; j < n_reloads; j++)
7057 int r = reload_order[j];
7058 int i = reload_spill_index[r];
7060 /* If this is a non-inherited input reload from a pseudo, we must
7061 clear any memory of a previous store to the same pseudo. Only do
7062 something if there will not be an output reload for the pseudo
7063 being reloaded. */
7064 if (rld[r].in_reg != 0
7065 && ! (reload_inherited[r] || reload_override_in[r]))
7067 rtx reg = rld[r].in_reg;
7069 if (GET_CODE (reg) == SUBREG)
7070 reg = SUBREG_REG (reg);
7072 if (GET_CODE (reg) == REG
7073 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7074 && ! reg_has_output_reload[REGNO (reg)])
7076 int nregno = REGNO (reg);
7078 if (reg_last_reload_reg[nregno])
7080 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7082 if (reg_reloaded_contents[last_regno] == nregno)
7083 spill_reg_store[last_regno] = 0;
7088 /* I is nonneg if this reload used a register.
7089 If rld[r].reg_rtx is 0, this is an optional reload
7090 that we opted to ignore. */
7092 if (i >= 0 && rld[r].reg_rtx != 0)
7094 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7095 int k;
7096 int part_reaches_end = 0;
7097 int all_reaches_end = 1;
7099 /* For a multi register reload, we need to check if all or part
7100 of the value lives to the end. */
7101 for (k = 0; k < nr; k++)
7103 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7104 rld[r].when_needed))
7105 part_reaches_end = 1;
7106 else
7107 all_reaches_end = 0;
7110 /* Ignore reloads that don't reach the end of the insn in
7111 entirety. */
7112 if (all_reaches_end)
7114 /* First, clear out memory of what used to be in this spill reg.
7115 If consecutive registers are used, clear them all. */
7117 for (k = 0; k < nr; k++)
7119 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7120 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7123 /* Maybe the spill reg contains a copy of reload_out. */
7124 if (rld[r].out != 0
7125 && (GET_CODE (rld[r].out) == REG
7126 #ifdef AUTO_INC_DEC
7127 || ! rld[r].out_reg
7128 #endif
7129 || GET_CODE (rld[r].out_reg) == REG))
7131 rtx out = (GET_CODE (rld[r].out) == REG
7132 ? rld[r].out
7133 : rld[r].out_reg
7134 ? rld[r].out_reg
7135 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7136 int nregno = REGNO (out);
7137 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7138 : hard_regno_nregs[nregno]
7139 [GET_MODE (rld[r].reg_rtx)]);
7141 spill_reg_store[i] = new_spill_reg_store[i];
7142 spill_reg_stored_to[i] = out;
7143 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7145 /* If NREGNO is a hard register, it may occupy more than
7146 one register. If it does, say what is in the
7147 rest of the registers assuming that both registers
7148 agree on how many words the object takes. If not,
7149 invalidate the subsequent registers. */
7151 if (nregno < FIRST_PSEUDO_REGISTER)
7152 for (k = 1; k < nnr; k++)
7153 reg_last_reload_reg[nregno + k]
7154 = (nr == nnr
7155 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7156 : 0);
7158 /* Now do the inverse operation. */
7159 for (k = 0; k < nr; k++)
7161 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7162 reg_reloaded_contents[i + k]
7163 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7164 ? nregno
7165 : nregno + k);
7166 reg_reloaded_insn[i + k] = insn;
7167 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7168 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (out)))
7169 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7173 /* Maybe the spill reg contains a copy of reload_in. Only do
7174 something if there will not be an output reload for
7175 the register being reloaded. */
7176 else if (rld[r].out_reg == 0
7177 && rld[r].in != 0
7178 && ((GET_CODE (rld[r].in) == REG
7179 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7180 && ! reg_has_output_reload[REGNO (rld[r].in)])
7181 || (GET_CODE (rld[r].in_reg) == REG
7182 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7183 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7185 int nregno;
7186 int nnr;
7187 rtx in;
7189 if (GET_CODE (rld[r].in) == REG
7190 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7191 in = rld[r].in;
7192 else if (GET_CODE (rld[r].in_reg) == REG)
7193 in = rld[r].in_reg;
7194 else
7195 in = XEXP (rld[r].in_reg, 0);
7196 nregno = REGNO (in);
7198 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7199 : hard_regno_nregs[nregno]
7200 [GET_MODE (rld[r].reg_rtx)]);
7202 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7204 if (nregno < FIRST_PSEUDO_REGISTER)
7205 for (k = 1; k < nnr; k++)
7206 reg_last_reload_reg[nregno + k]
7207 = (nr == nnr
7208 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7209 : 0);
7211 /* Unless we inherited this reload, show we haven't
7212 recently done a store.
7213 Previous stores of inherited auto_inc expressions
7214 also have to be discarded. */
7215 if (! reload_inherited[r]
7216 || (rld[r].out && ! rld[r].out_reg))
7217 spill_reg_store[i] = 0;
7219 for (k = 0; k < nr; k++)
7221 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7222 reg_reloaded_contents[i + k]
7223 = (nregno >= FIRST_PSEUDO_REGISTER || nr != nnr
7224 ? nregno
7225 : nregno + k);
7226 reg_reloaded_insn[i + k] = insn;
7227 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7228 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (in)))
7229 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7234 /* However, if part of the reload reaches the end, then we must
7235 invalidate the old info for the part that survives to the end. */
7236 else if (part_reaches_end)
7238 for (k = 0; k < nr; k++)
7239 if (reload_reg_reaches_end_p (i + k,
7240 rld[r].opnum,
7241 rld[r].when_needed))
7242 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7246 /* The following if-statement was #if 0'd in 1.34 (or before...).
7247 It's reenabled in 1.35 because supposedly nothing else
7248 deals with this problem. */
7250 /* If a register gets output-reloaded from a non-spill register,
7251 that invalidates any previous reloaded copy of it.
7252 But forget_old_reloads_1 won't get to see it, because
7253 it thinks only about the original insn. So invalidate it here. */
7254 if (i < 0 && rld[r].out != 0
7255 && (GET_CODE (rld[r].out) == REG
7256 || (GET_CODE (rld[r].out) == MEM
7257 && GET_CODE (rld[r].out_reg) == REG)))
7259 rtx out = (GET_CODE (rld[r].out) == REG
7260 ? rld[r].out : rld[r].out_reg);
7261 int nregno = REGNO (out);
7262 if (nregno >= FIRST_PSEUDO_REGISTER)
7264 rtx src_reg, store_insn = NULL_RTX;
7266 reg_last_reload_reg[nregno] = 0;
7268 /* If we can find a hard register that is stored, record
7269 the storing insn so that we may delete this insn with
7270 delete_output_reload. */
7271 src_reg = rld[r].reg_rtx;
7273 /* If this is an optional reload, try to find the source reg
7274 from an input reload. */
7275 if (! src_reg)
7277 rtx set = single_set (insn);
7278 if (set && SET_DEST (set) == rld[r].out)
7280 int k;
7282 src_reg = SET_SRC (set);
7283 store_insn = insn;
7284 for (k = 0; k < n_reloads; k++)
7286 if (rld[k].in == src_reg)
7288 src_reg = rld[k].reg_rtx;
7289 break;
7294 else
7295 store_insn = new_spill_reg_store[REGNO (src_reg)];
7296 if (src_reg && GET_CODE (src_reg) == REG
7297 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7299 int src_regno = REGNO (src_reg);
7300 int nr = hard_regno_nregs[src_regno][rld[r].mode];
7301 /* The place where to find a death note varies with
7302 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7303 necessarily checked exactly in the code that moves
7304 notes, so just check both locations. */
7305 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7306 if (! note && store_insn)
7307 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7308 while (nr-- > 0)
7310 spill_reg_store[src_regno + nr] = store_insn;
7311 spill_reg_stored_to[src_regno + nr] = out;
7312 reg_reloaded_contents[src_regno + nr] = nregno;
7313 reg_reloaded_insn[src_regno + nr] = store_insn;
7314 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7315 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7316 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + nr,
7317 GET_MODE (src_reg)))
7318 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7319 src_regno + nr);
7320 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7321 if (note)
7322 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7323 else
7324 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7326 reg_last_reload_reg[nregno] = src_reg;
7329 else
7331 int num_regs = hard_regno_nregs[nregno][GET_MODE (rld[r].out)];
7333 while (num_regs-- > 0)
7334 reg_last_reload_reg[nregno + num_regs] = 0;
7338 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7341 /* Emit code to perform a reload from IN (which may be a reload register) to
7342 OUT (which may also be a reload register). IN or OUT is from operand
7343 OPNUM with reload type TYPE.
7345 Returns first insn emitted. */
7348 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7350 rtx last = get_last_insn ();
7351 rtx tem;
7353 /* If IN is a paradoxical SUBREG, remove it and try to put the
7354 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7355 if (GET_CODE (in) == SUBREG
7356 && (GET_MODE_SIZE (GET_MODE (in))
7357 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7358 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7359 in = SUBREG_REG (in), out = tem;
7360 else if (GET_CODE (out) == SUBREG
7361 && (GET_MODE_SIZE (GET_MODE (out))
7362 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7363 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7364 out = SUBREG_REG (out), in = tem;
7366 /* How to do this reload can get quite tricky. Normally, we are being
7367 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7368 register that didn't get a hard register. In that case we can just
7369 call emit_move_insn.
7371 We can also be asked to reload a PLUS that adds a register or a MEM to
7372 another register, constant or MEM. This can occur during frame pointer
7373 elimination and while reloading addresses. This case is handled by
7374 trying to emit a single insn to perform the add. If it is not valid,
7375 we use a two insn sequence.
7377 Finally, we could be called to handle an 'o' constraint by putting
7378 an address into a register. In that case, we first try to do this
7379 with a named pattern of "reload_load_address". If no such pattern
7380 exists, we just emit a SET insn and hope for the best (it will normally
7381 be valid on machines that use 'o').
7383 This entire process is made complex because reload will never
7384 process the insns we generate here and so we must ensure that
7385 they will fit their constraints and also by the fact that parts of
7386 IN might be being reloaded separately and replaced with spill registers.
7387 Because of this, we are, in some sense, just guessing the right approach
7388 here. The one listed above seems to work.
7390 ??? At some point, this whole thing needs to be rethought. */
7392 if (GET_CODE (in) == PLUS
7393 && (GET_CODE (XEXP (in, 0)) == REG
7394 || GET_CODE (XEXP (in, 0)) == SUBREG
7395 || GET_CODE (XEXP (in, 0)) == MEM)
7396 && (GET_CODE (XEXP (in, 1)) == REG
7397 || GET_CODE (XEXP (in, 1)) == SUBREG
7398 || CONSTANT_P (XEXP (in, 1))
7399 || GET_CODE (XEXP (in, 1)) == MEM))
7401 /* We need to compute the sum of a register or a MEM and another
7402 register, constant, or MEM, and put it into the reload
7403 register. The best possible way of doing this is if the machine
7404 has a three-operand ADD insn that accepts the required operands.
7406 The simplest approach is to try to generate such an insn and see if it
7407 is recognized and matches its constraints. If so, it can be used.
7409 It might be better not to actually emit the insn unless it is valid,
7410 but we need to pass the insn as an operand to `recog' and
7411 `extract_insn' and it is simpler to emit and then delete the insn if
7412 not valid than to dummy things up. */
7414 rtx op0, op1, tem, insn;
7415 int code;
7417 op0 = find_replacement (&XEXP (in, 0));
7418 op1 = find_replacement (&XEXP (in, 1));
7420 /* Since constraint checking is strict, commutativity won't be
7421 checked, so we need to do that here to avoid spurious failure
7422 if the add instruction is two-address and the second operand
7423 of the add is the same as the reload reg, which is frequently
7424 the case. If the insn would be A = B + A, rearrange it so
7425 it will be A = A + B as constrain_operands expects. */
7427 if (GET_CODE (XEXP (in, 1)) == REG
7428 && REGNO (out) == REGNO (XEXP (in, 1)))
7429 tem = op0, op0 = op1, op1 = tem;
7431 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7432 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7434 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7435 code = recog_memoized (insn);
7437 if (code >= 0)
7439 extract_insn (insn);
7440 /* We want constrain operands to treat this insn strictly in
7441 its validity determination, i.e., the way it would after reload
7442 has completed. */
7443 if (constrain_operands (1))
7444 return insn;
7447 delete_insns_since (last);
7449 /* If that failed, we must use a conservative two-insn sequence.
7451 Use a move to copy one operand into the reload register. Prefer
7452 to reload a constant, MEM or pseudo since the move patterns can
7453 handle an arbitrary operand. If OP1 is not a constant, MEM or
7454 pseudo and OP1 is not a valid operand for an add instruction, then
7455 reload OP1.
7457 After reloading one of the operands into the reload register, add
7458 the reload register to the output register.
7460 If there is another way to do this for a specific machine, a
7461 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7462 we emit below. */
7464 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7466 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM || GET_CODE (op1) == SUBREG
7467 || (GET_CODE (op1) == REG
7468 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7469 || (code != CODE_FOR_nothing
7470 && ! ((*insn_data[code].operand[2].predicate)
7471 (op1, insn_data[code].operand[2].mode))))
7472 tem = op0, op0 = op1, op1 = tem;
7474 gen_reload (out, op0, opnum, type);
7476 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7477 This fixes a problem on the 32K where the stack pointer cannot
7478 be used as an operand of an add insn. */
7480 if (rtx_equal_p (op0, op1))
7481 op1 = out;
7483 insn = emit_insn (gen_add2_insn (out, op1));
7485 /* If that failed, copy the address register to the reload register.
7486 Then add the constant to the reload register. */
7488 code = recog_memoized (insn);
7490 if (code >= 0)
7492 extract_insn (insn);
7493 /* We want constrain operands to treat this insn strictly in
7494 its validity determination, i.e., the way it would after reload
7495 has completed. */
7496 if (constrain_operands (1))
7498 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7499 REG_NOTES (insn)
7500 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7501 return insn;
7505 delete_insns_since (last);
7507 gen_reload (out, op1, opnum, type);
7508 insn = emit_insn (gen_add2_insn (out, op0));
7509 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7512 #ifdef SECONDARY_MEMORY_NEEDED
7513 /* If we need a memory location to do the move, do it that way. */
7514 else if ((GET_CODE (in) == REG || GET_CODE (in) == SUBREG)
7515 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7516 && (GET_CODE (out) == REG || GET_CODE (out) == SUBREG)
7517 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7518 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7519 REGNO_REG_CLASS (reg_or_subregno (out)),
7520 GET_MODE (out)))
7522 /* Get the memory to use and rewrite both registers to its mode. */
7523 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7525 if (GET_MODE (loc) != GET_MODE (out))
7526 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7528 if (GET_MODE (loc) != GET_MODE (in))
7529 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7531 gen_reload (loc, in, opnum, type);
7532 gen_reload (out, loc, opnum, type);
7534 #endif
7536 /* If IN is a simple operand, use gen_move_insn. */
7537 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
7538 emit_insn (gen_move_insn (out, in));
7540 #ifdef HAVE_reload_load_address
7541 else if (HAVE_reload_load_address)
7542 emit_insn (gen_reload_load_address (out, in));
7543 #endif
7545 /* Otherwise, just write (set OUT IN) and hope for the best. */
7546 else
7547 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7549 /* Return the first insn emitted.
7550 We can not just return get_last_insn, because there may have
7551 been multiple instructions emitted. Also note that gen_move_insn may
7552 emit more than one insn itself, so we can not assume that there is one
7553 insn emitted per emit_insn_before call. */
7555 return last ? NEXT_INSN (last) : get_insns ();
7558 /* Delete a previously made output-reload whose result we now believe
7559 is not needed. First we double-check.
7561 INSN is the insn now being processed.
7562 LAST_RELOAD_REG is the hard register number for which we want to delete
7563 the last output reload.
7564 J is the reload-number that originally used REG. The caller has made
7565 certain that reload J doesn't use REG any longer for input. */
7567 static void
7568 delete_output_reload (rtx insn, int j, int last_reload_reg)
7570 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7571 rtx reg = spill_reg_stored_to[last_reload_reg];
7572 int k;
7573 int n_occurrences;
7574 int n_inherited = 0;
7575 rtx i1;
7576 rtx substed;
7578 /* It is possible that this reload has been only used to set another reload
7579 we eliminated earlier and thus deleted this instruction too. */
7580 if (INSN_DELETED_P (output_reload_insn))
7581 return;
7583 /* Get the raw pseudo-register referred to. */
7585 while (GET_CODE (reg) == SUBREG)
7586 reg = SUBREG_REG (reg);
7587 substed = reg_equiv_memory_loc[REGNO (reg)];
7589 /* This is unsafe if the operand occurs more often in the current
7590 insn than it is inherited. */
7591 for (k = n_reloads - 1; k >= 0; k--)
7593 rtx reg2 = rld[k].in;
7594 if (! reg2)
7595 continue;
7596 if (GET_CODE (reg2) == MEM || reload_override_in[k])
7597 reg2 = rld[k].in_reg;
7598 #ifdef AUTO_INC_DEC
7599 if (rld[k].out && ! rld[k].out_reg)
7600 reg2 = XEXP (rld[k].in_reg, 0);
7601 #endif
7602 while (GET_CODE (reg2) == SUBREG)
7603 reg2 = SUBREG_REG (reg2);
7604 if (rtx_equal_p (reg2, reg))
7606 if (reload_inherited[k] || reload_override_in[k] || k == j)
7608 n_inherited++;
7609 reg2 = rld[k].out_reg;
7610 if (! reg2)
7611 continue;
7612 while (GET_CODE (reg2) == SUBREG)
7613 reg2 = XEXP (reg2, 0);
7614 if (rtx_equal_p (reg2, reg))
7615 n_inherited++;
7617 else
7618 return;
7621 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7622 if (substed)
7623 n_occurrences += count_occurrences (PATTERN (insn),
7624 eliminate_regs (substed, 0,
7625 NULL_RTX), 0);
7626 if (n_occurrences > n_inherited)
7627 return;
7629 /* If the pseudo-reg we are reloading is no longer referenced
7630 anywhere between the store into it and here,
7631 and no jumps or labels intervene, then the value can get
7632 here through the reload reg alone.
7633 Otherwise, give up--return. */
7634 for (i1 = NEXT_INSN (output_reload_insn);
7635 i1 != insn; i1 = NEXT_INSN (i1))
7637 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
7638 return;
7639 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
7640 && reg_mentioned_p (reg, PATTERN (i1)))
7642 /* If this is USE in front of INSN, we only have to check that
7643 there are no more references than accounted for by inheritance. */
7644 while (GET_CODE (i1) == INSN && GET_CODE (PATTERN (i1)) == USE)
7646 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7647 i1 = NEXT_INSN (i1);
7649 if (n_occurrences <= n_inherited && i1 == insn)
7650 break;
7651 return;
7655 /* We will be deleting the insn. Remove the spill reg information. */
7656 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
7658 spill_reg_store[last_reload_reg + k] = 0;
7659 spill_reg_stored_to[last_reload_reg + k] = 0;
7662 /* The caller has already checked that REG dies or is set in INSN.
7663 It has also checked that we are optimizing, and thus some
7664 inaccuracies in the debugging information are acceptable.
7665 So we could just delete output_reload_insn. But in some cases
7666 we can improve the debugging information without sacrificing
7667 optimization - maybe even improving the code: See if the pseudo
7668 reg has been completely replaced with reload regs. If so, delete
7669 the store insn and forget we had a stack slot for the pseudo. */
7670 if (rld[j].out != rld[j].in
7671 && REG_N_DEATHS (REGNO (reg)) == 1
7672 && REG_N_SETS (REGNO (reg)) == 1
7673 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7674 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7676 rtx i2;
7678 /* We know that it was used only between here and the beginning of
7679 the current basic block. (We also know that the last use before
7680 INSN was the output reload we are thinking of deleting, but never
7681 mind that.) Search that range; see if any ref remains. */
7682 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7684 rtx set = single_set (i2);
7686 /* Uses which just store in the pseudo don't count,
7687 since if they are the only uses, they are dead. */
7688 if (set != 0 && SET_DEST (set) == reg)
7689 continue;
7690 if (GET_CODE (i2) == CODE_LABEL
7691 || GET_CODE (i2) == JUMP_INSN)
7692 break;
7693 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
7694 && reg_mentioned_p (reg, PATTERN (i2)))
7696 /* Some other ref remains; just delete the output reload we
7697 know to be dead. */
7698 delete_address_reloads (output_reload_insn, insn);
7699 delete_insn (output_reload_insn);
7700 return;
7704 /* Delete the now-dead stores into this pseudo. Note that this
7705 loop also takes care of deleting output_reload_insn. */
7706 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7708 rtx set = single_set (i2);
7710 if (set != 0 && SET_DEST (set) == reg)
7712 delete_address_reloads (i2, insn);
7713 delete_insn (i2);
7715 if (GET_CODE (i2) == CODE_LABEL
7716 || GET_CODE (i2) == JUMP_INSN)
7717 break;
7720 /* For the debugging info, say the pseudo lives in this reload reg. */
7721 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
7722 alter_reg (REGNO (reg), -1);
7724 else
7726 delete_address_reloads (output_reload_insn, insn);
7727 delete_insn (output_reload_insn);
7731 /* We are going to delete DEAD_INSN. Recursively delete loads of
7732 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7733 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7734 static void
7735 delete_address_reloads (rtx dead_insn, rtx current_insn)
7737 rtx set = single_set (dead_insn);
7738 rtx set2, dst, prev, next;
7739 if (set)
7741 rtx dst = SET_DEST (set);
7742 if (GET_CODE (dst) == MEM)
7743 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
7745 /* If we deleted the store from a reloaded post_{in,de}c expression,
7746 we can delete the matching adds. */
7747 prev = PREV_INSN (dead_insn);
7748 next = NEXT_INSN (dead_insn);
7749 if (! prev || ! next)
7750 return;
7751 set = single_set (next);
7752 set2 = single_set (prev);
7753 if (! set || ! set2
7754 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
7755 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
7756 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
7757 return;
7758 dst = SET_DEST (set);
7759 if (! rtx_equal_p (dst, SET_DEST (set2))
7760 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
7761 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
7762 || (INTVAL (XEXP (SET_SRC (set), 1))
7763 != -INTVAL (XEXP (SET_SRC (set2), 1))))
7764 return;
7765 delete_related_insns (prev);
7766 delete_related_insns (next);
7769 /* Subfunction of delete_address_reloads: process registers found in X. */
7770 static void
7771 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
7773 rtx prev, set, dst, i2;
7774 int i, j;
7775 enum rtx_code code = GET_CODE (x);
7777 if (code != REG)
7779 const char *fmt = GET_RTX_FORMAT (code);
7780 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7782 if (fmt[i] == 'e')
7783 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
7784 else if (fmt[i] == 'E')
7786 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7787 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
7788 current_insn);
7791 return;
7794 if (spill_reg_order[REGNO (x)] < 0)
7795 return;
7797 /* Scan backwards for the insn that sets x. This might be a way back due
7798 to inheritance. */
7799 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
7801 code = GET_CODE (prev);
7802 if (code == CODE_LABEL || code == JUMP_INSN)
7803 return;
7804 if (!INSN_P (prev))
7805 continue;
7806 if (reg_set_p (x, PATTERN (prev)))
7807 break;
7808 if (reg_referenced_p (x, PATTERN (prev)))
7809 return;
7811 if (! prev || INSN_UID (prev) < reload_first_uid)
7812 return;
7813 /* Check that PREV only sets the reload register. */
7814 set = single_set (prev);
7815 if (! set)
7816 return;
7817 dst = SET_DEST (set);
7818 if (GET_CODE (dst) != REG
7819 || ! rtx_equal_p (dst, x))
7820 return;
7821 if (! reg_set_p (dst, PATTERN (dead_insn)))
7823 /* Check if DST was used in a later insn -
7824 it might have been inherited. */
7825 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
7827 if (GET_CODE (i2) == CODE_LABEL)
7828 break;
7829 if (! INSN_P (i2))
7830 continue;
7831 if (reg_referenced_p (dst, PATTERN (i2)))
7833 /* If there is a reference to the register in the current insn,
7834 it might be loaded in a non-inherited reload. If no other
7835 reload uses it, that means the register is set before
7836 referenced. */
7837 if (i2 == current_insn)
7839 for (j = n_reloads - 1; j >= 0; j--)
7840 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7841 || reload_override_in[j] == dst)
7842 return;
7843 for (j = n_reloads - 1; j >= 0; j--)
7844 if (rld[j].in && rld[j].reg_rtx == dst)
7845 break;
7846 if (j >= 0)
7847 break;
7849 return;
7851 if (GET_CODE (i2) == JUMP_INSN)
7852 break;
7853 /* If DST is still live at CURRENT_INSN, check if it is used for
7854 any reload. Note that even if CURRENT_INSN sets DST, we still
7855 have to check the reloads. */
7856 if (i2 == current_insn)
7858 for (j = n_reloads - 1; j >= 0; j--)
7859 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7860 || reload_override_in[j] == dst)
7861 return;
7862 /* ??? We can't finish the loop here, because dst might be
7863 allocated to a pseudo in this block if no reload in this
7864 block needs any of the classes containing DST - see
7865 spill_hard_reg. There is no easy way to tell this, so we
7866 have to scan till the end of the basic block. */
7868 if (reg_set_p (dst, PATTERN (i2)))
7869 break;
7872 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
7873 reg_reloaded_contents[REGNO (dst)] = -1;
7874 delete_insn (prev);
7877 /* Output reload-insns to reload VALUE into RELOADREG.
7878 VALUE is an autoincrement or autodecrement RTX whose operand
7879 is a register or memory location;
7880 so reloading involves incrementing that location.
7881 IN is either identical to VALUE, or some cheaper place to reload from.
7883 INC_AMOUNT is the number to increment or decrement by (always positive).
7884 This cannot be deduced from VALUE.
7886 Return the instruction that stores into RELOADREG. */
7888 static rtx
7889 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
7891 /* REG or MEM to be copied and incremented. */
7892 rtx incloc = XEXP (value, 0);
7893 /* Nonzero if increment after copying. */
7894 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7895 rtx last;
7896 rtx inc;
7897 rtx add_insn;
7898 int code;
7899 rtx store;
7900 rtx real_in = in == value ? XEXP (in, 0) : in;
7902 /* No hard register is equivalent to this register after
7903 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
7904 we could inc/dec that register as well (maybe even using it for
7905 the source), but I'm not sure it's worth worrying about. */
7906 if (GET_CODE (incloc) == REG)
7907 reg_last_reload_reg[REGNO (incloc)] = 0;
7909 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7910 inc_amount = -inc_amount;
7912 inc = GEN_INT (inc_amount);
7914 /* If this is post-increment, first copy the location to the reload reg. */
7915 if (post && real_in != reloadreg)
7916 emit_insn (gen_move_insn (reloadreg, real_in));
7918 if (in == value)
7920 /* See if we can directly increment INCLOC. Use a method similar to
7921 that in gen_reload. */
7923 last = get_last_insn ();
7924 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7925 gen_rtx_PLUS (GET_MODE (incloc),
7926 incloc, inc)));
7928 code = recog_memoized (add_insn);
7929 if (code >= 0)
7931 extract_insn (add_insn);
7932 if (constrain_operands (1))
7934 /* If this is a pre-increment and we have incremented the value
7935 where it lives, copy the incremented value to RELOADREG to
7936 be used as an address. */
7938 if (! post)
7939 emit_insn (gen_move_insn (reloadreg, incloc));
7941 return add_insn;
7944 delete_insns_since (last);
7947 /* If couldn't do the increment directly, must increment in RELOADREG.
7948 The way we do this depends on whether this is pre- or post-increment.
7949 For pre-increment, copy INCLOC to the reload register, increment it
7950 there, then save back. */
7952 if (! post)
7954 if (in != reloadreg)
7955 emit_insn (gen_move_insn (reloadreg, real_in));
7956 emit_insn (gen_add2_insn (reloadreg, inc));
7957 store = emit_insn (gen_move_insn (incloc, reloadreg));
7959 else
7961 /* Postincrement.
7962 Because this might be a jump insn or a compare, and because RELOADREG
7963 may not be available after the insn in an input reload, we must do
7964 the incrementation before the insn being reloaded for.
7966 We have already copied IN to RELOADREG. Increment the copy in
7967 RELOADREG, save that back, then decrement RELOADREG so it has
7968 the original value. */
7970 emit_insn (gen_add2_insn (reloadreg, inc));
7971 store = emit_insn (gen_move_insn (incloc, reloadreg));
7972 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
7975 return store;
7978 #ifdef AUTO_INC_DEC
7979 static void
7980 add_auto_inc_notes (rtx insn, rtx x)
7982 enum rtx_code code = GET_CODE (x);
7983 const char *fmt;
7984 int i, j;
7986 if (code == MEM && auto_inc_p (XEXP (x, 0)))
7988 REG_NOTES (insn)
7989 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
7990 return;
7993 /* Scan all the operand sub-expressions. */
7994 fmt = GET_RTX_FORMAT (code);
7995 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7997 if (fmt[i] == 'e')
7998 add_auto_inc_notes (insn, XEXP (x, i));
7999 else if (fmt[i] == 'E')
8000 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8001 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8004 #endif
8006 /* Copy EH notes from an insn to its reloads. */
8007 static void
8008 copy_eh_notes (rtx insn, rtx x)
8010 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8011 if (eh_note)
8013 for (; x != 0; x = NEXT_INSN (x))
8015 if (may_trap_p (PATTERN (x)))
8016 REG_NOTES (x)
8017 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8018 REG_NOTES (x));
8023 /* This is used by reload pass, that does emit some instructions after
8024 abnormal calls moving basic block end, but in fact it wants to emit
8025 them on the edge. Looks for abnormal call edges, find backward the
8026 proper call and fix the damage.
8028 Similar handle instructions throwing exceptions internally. */
8029 void
8030 fixup_abnormal_edges (void)
8032 bool inserted = false;
8033 basic_block bb;
8035 FOR_EACH_BB (bb)
8037 edge e;
8039 /* Look for cases we are interested in - calls or instructions causing
8040 exceptions. */
8041 for (e = bb->succ; e; e = e->succ_next)
8043 if (e->flags & EDGE_ABNORMAL_CALL)
8044 break;
8045 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8046 == (EDGE_ABNORMAL | EDGE_EH))
8047 break;
8049 if (e && GET_CODE (BB_END (bb)) != CALL_INSN
8050 && !can_throw_internal (BB_END (bb)))
8052 rtx insn = BB_END (bb), stop = NEXT_INSN (BB_END (bb));
8053 rtx next;
8054 for (e = bb->succ; e; e = e->succ_next)
8055 if (e->flags & EDGE_FALLTHRU)
8056 break;
8057 /* Get past the new insns generated. Allow notes, as the insns may
8058 be already deleted. */
8059 while ((GET_CODE (insn) == INSN || GET_CODE (insn) == NOTE)
8060 && !can_throw_internal (insn)
8061 && insn != BB_HEAD (bb))
8062 insn = PREV_INSN (insn);
8063 if (GET_CODE (insn) != CALL_INSN && !can_throw_internal (insn))
8064 abort ();
8065 BB_END (bb) = insn;
8066 inserted = true;
8067 insn = NEXT_INSN (insn);
8068 while (insn && insn != stop)
8070 next = NEXT_INSN (insn);
8071 if (INSN_P (insn))
8073 delete_insn (insn);
8075 /* Sometimes there's still the return value USE.
8076 If it's placed after a trapping call (i.e. that
8077 call is the last insn anyway), we have no fallthru
8078 edge. Simply delete this use and don't try to insert
8079 on the non-existent edge. */
8080 if (GET_CODE (PATTERN (insn)) != USE)
8082 /* We're not deleting it, we're moving it. */
8083 INSN_DELETED_P (insn) = 0;
8084 PREV_INSN (insn) = NULL_RTX;
8085 NEXT_INSN (insn) = NULL_RTX;
8087 insert_insn_on_edge (insn, e);
8090 insn = next;
8094 /* We've possibly turned single trapping insn into multiple ones. */
8095 if (flag_non_call_exceptions)
8097 sbitmap blocks;
8098 blocks = sbitmap_alloc (last_basic_block);
8099 sbitmap_ones (blocks);
8100 find_many_sub_basic_blocks (blocks);
8102 if (inserted)
8103 commit_edge_insertions ();