* config/fp-bit.h (LSHIFT): Take shift count parameter.
[official-gcc.git] / gcc / reload1.c
blob970f5ec39bc52f0a29f15782eff24af09d4c2310
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, 2005 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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"
46 #include "target.h"
48 /* This file contains the reload pass of the compiler, which is
49 run after register allocation has been done. It checks that
50 each insn is valid (operands required to be in registers really
51 are in registers of the proper class) and fixes up invalid ones
52 by copying values temporarily into registers for the insns
53 that need them.
55 The results of register allocation are described by the vector
56 reg_renumber; the insns still contain pseudo regs, but reg_renumber
57 can be used to find which hard reg, if any, a pseudo reg is in.
59 The technique we always use is to free up a few hard regs that are
60 called ``reload regs'', and for each place where a pseudo reg
61 must be in a hard reg, copy it temporarily into one of the reload regs.
63 Reload regs are allocated locally for every instruction that needs
64 reloads. When there are pseudos which are allocated to a register that
65 has been chosen as a reload reg, such pseudos must be ``spilled''.
66 This means that they go to other hard regs, or to stack slots if no other
67 available hard regs can be found. Spilling can invalidate more
68 insns, requiring additional need for reloads, so we must keep checking
69 until the process stabilizes.
71 For machines with different classes of registers, we must keep track
72 of the register class needed for each reload, and make sure that
73 we allocate enough reload registers of each class.
75 The file reload.c contains the code that checks one insn for
76 validity and reports the reloads that it needs. This file
77 is in charge of scanning the entire rtl code, accumulating the
78 reload needs, spilling, assigning reload registers to use for
79 fixing up each insn, and generating the new insns to copy values
80 into the reload registers. */
82 /* During reload_as_needed, element N contains a REG rtx for the hard reg
83 into which reg N has been reloaded (perhaps for a previous insn). */
84 static rtx *reg_last_reload_reg;
86 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
87 for an output reload that stores into reg N. */
88 static char *reg_has_output_reload;
90 /* Indicates which hard regs are reload-registers for an output reload
91 in the current insn. */
92 static HARD_REG_SET reg_is_output_reload;
94 /* Element N is the constant value to which pseudo reg N is equivalent,
95 or zero if pseudo reg N is not equivalent to a constant.
96 find_reloads looks at this in order to replace pseudo reg N
97 with the constant it stands for. */
98 rtx *reg_equiv_constant;
100 /* Element N is an invariant value to which pseudo reg N is equivalent.
101 eliminate_regs_in_insn uses this to replace pseudos in particular
102 contexts. */
103 rtx *reg_equiv_invariant;
105 /* Element N is a memory location to which pseudo reg N is equivalent,
106 prior to any register elimination (such as frame pointer to stack
107 pointer). Depending on whether or not it is a valid address, this value
108 is transferred to either reg_equiv_address or reg_equiv_mem. */
109 rtx *reg_equiv_memory_loc;
111 /* We allocate reg_equiv_memory_loc inside a varray so that the garbage
112 collector can keep track of what is inside. */
113 varray_type reg_equiv_memory_loc_varray;
115 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
116 This is used when the address is not valid as a memory address
117 (because its displacement is too big for the machine.) */
118 rtx *reg_equiv_address;
120 /* Element N is the memory slot to which pseudo reg N is equivalent,
121 or zero if pseudo reg N is not equivalent to a memory slot. */
122 rtx *reg_equiv_mem;
124 /* Widest width in which each pseudo reg is referred to (via subreg). */
125 static unsigned int *reg_max_ref_width;
127 /* Element N is the list of insns that initialized reg N from its equivalent
128 constant or memory slot. */
129 rtx *reg_equiv_init;
130 int reg_equiv_init_size;
132 /* Vector to remember old contents of reg_renumber before spilling. */
133 static short *reg_old_renumber;
135 /* During reload_as_needed, element N contains the last pseudo regno reloaded
136 into hard register N. If that pseudo reg occupied more than one register,
137 reg_reloaded_contents points to that pseudo for each spill register in
138 use; all of these must remain set for an inheritance to occur. */
139 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
141 /* During reload_as_needed, element N contains the insn for which
142 hard register N was last used. Its contents are significant only
143 when reg_reloaded_valid is set for this register. */
144 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
146 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
147 static HARD_REG_SET reg_reloaded_valid;
148 /* Indicate if the register was dead at the end of the reload.
149 This is only valid if reg_reloaded_contents is set and valid. */
150 static HARD_REG_SET reg_reloaded_dead;
152 /* Indicate whether the register's current value is one that is not
153 safe to retain across a call, even for registers that are normally
154 call-saved. */
155 static HARD_REG_SET reg_reloaded_call_part_clobbered;
157 /* Number of spill-regs so far; number of valid elements of spill_regs. */
158 static int n_spills;
160 /* In parallel with spill_regs, contains REG rtx's for those regs.
161 Holds the last rtx used for any given reg, or 0 if it has never
162 been used for spilling yet. This rtx is reused, provided it has
163 the proper mode. */
164 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
166 /* In parallel with spill_regs, contains nonzero for a spill reg
167 that was stored after the last time it was used.
168 The precise value is the insn generated to do the store. */
169 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
171 /* This is the register that was stored with spill_reg_store. This is a
172 copy of reload_out / reload_out_reg when the value was stored; if
173 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
174 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
176 /* This table is the inverse mapping of spill_regs:
177 indexed by hard reg number,
178 it contains the position of that reg in spill_regs,
179 or -1 for something that is not in spill_regs.
181 ?!? This is no longer accurate. */
182 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
184 /* This reg set indicates registers that can't be used as spill registers for
185 the currently processed insn. These are the hard registers which are live
186 during the insn, but not allocated to pseudos, as well as fixed
187 registers. */
188 static HARD_REG_SET bad_spill_regs;
190 /* These are the hard registers that can't be used as spill register for any
191 insn. This includes registers used for user variables and registers that
192 we can't eliminate. A register that appears in this set also can't be used
193 to retry register allocation. */
194 static HARD_REG_SET bad_spill_regs_global;
196 /* Describes order of use of registers for reloading
197 of spilled pseudo-registers. `n_spills' is the number of
198 elements that are actually valid; new ones are added at the end.
200 Both spill_regs and spill_reg_order are used on two occasions:
201 once during find_reload_regs, where they keep track of the spill registers
202 for a single insn, but also during reload_as_needed where they show all
203 the registers ever used by reload. For the latter case, the information
204 is calculated during finish_spills. */
205 static short spill_regs[FIRST_PSEUDO_REGISTER];
207 /* This vector of reg sets indicates, for each pseudo, which hard registers
208 may not be used for retrying global allocation because the register was
209 formerly spilled from one of them. If we allowed reallocating a pseudo to
210 a register that it was already allocated to, reload might not
211 terminate. */
212 static HARD_REG_SET *pseudo_previous_regs;
214 /* This vector of reg sets indicates, for each pseudo, which hard
215 registers may not be used for retrying global allocation because they
216 are used as spill registers during one of the insns in which the
217 pseudo is live. */
218 static HARD_REG_SET *pseudo_forbidden_regs;
220 /* All hard regs that have been used as spill registers for any insn are
221 marked in this set. */
222 static HARD_REG_SET used_spill_regs;
224 /* Index of last register assigned as a spill register. We allocate in
225 a round-robin fashion. */
226 static int last_spill_reg;
228 /* Nonzero if indirect addressing is supported on the machine; this means
229 that spilling (REG n) does not require reloading it into a register in
230 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
231 value indicates the level of indirect addressing supported, e.g., two
232 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
233 a hard register. */
234 static char spill_indirect_levels;
236 /* Nonzero if indirect addressing is supported when the innermost MEM is
237 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
238 which these are valid is the same as spill_indirect_levels, above. */
239 char indirect_symref_ok;
241 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
242 char double_reg_address_ok;
244 /* Record the stack slot for each spilled hard register. */
245 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
247 /* Width allocated so far for that stack slot. */
248 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
250 /* Record which pseudos needed to be spilled. */
251 static regset_head spilled_pseudos;
253 /* Used for communication between order_regs_for_reload and count_pseudo.
254 Used to avoid counting one pseudo twice. */
255 static regset_head pseudos_counted;
257 /* First uid used by insns created by reload in this function.
258 Used in find_equiv_reg. */
259 int reload_first_uid;
261 /* Flag set by local-alloc or global-alloc if anything is live in
262 a call-clobbered reg across calls. */
263 int caller_save_needed;
265 /* Set to 1 while reload_as_needed is operating.
266 Required by some machines to handle any generated moves differently. */
267 int reload_in_progress = 0;
269 /* These arrays record the insn_code of insns that may be needed to
270 perform input and output reloads of special objects. They provide a
271 place to pass a scratch register. */
272 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
273 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
275 /* This obstack is used for allocation of rtl during register elimination.
276 The allocated storage can be freed once find_reloads has processed the
277 insn. */
278 static struct obstack reload_obstack;
280 /* Points to the beginning of the reload_obstack. All insn_chain structures
281 are allocated first. */
282 static char *reload_startobj;
284 /* The point after all insn_chain structures. Used to quickly deallocate
285 memory allocated in copy_reloads during calculate_needs_all_insns. */
286 static char *reload_firstobj;
288 /* This points before all local rtl generated by register elimination.
289 Used to quickly free all memory after processing one insn. */
290 static char *reload_insn_firstobj;
292 /* List of insn_chain instructions, one for every insn that reload needs to
293 examine. */
294 struct insn_chain *reload_insn_chain;
296 /* List of all insns needing reloads. */
297 static struct insn_chain *insns_need_reload;
299 /* This structure is used to record information about register eliminations.
300 Each array entry describes one possible way of eliminating a register
301 in favor of another. If there is more than one way of eliminating a
302 particular register, the most preferred should be specified first. */
304 struct elim_table
306 int from; /* Register number to be eliminated. */
307 int to; /* Register number used as replacement. */
308 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
309 int can_eliminate; /* Nonzero if this elimination can be done. */
310 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
311 insns made by reload. */
312 HOST_WIDE_INT offset; /* Current offset between the two regs. */
313 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
314 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
315 rtx from_rtx; /* REG rtx for the register to be eliminated.
316 We cannot simply compare the number since
317 we might then spuriously replace a hard
318 register corresponding to a pseudo
319 assigned to the reg to be eliminated. */
320 rtx to_rtx; /* REG rtx for the replacement. */
323 static struct elim_table *reg_eliminate = 0;
325 /* This is an intermediate structure to initialize the table. It has
326 exactly the members provided by ELIMINABLE_REGS. */
327 static const struct elim_table_1
329 const int from;
330 const int to;
331 } reg_eliminate_1[] =
333 /* If a set of eliminable registers was specified, define the table from it.
334 Otherwise, default to the normal case of the frame pointer being
335 replaced by the stack pointer. */
337 #ifdef ELIMINABLE_REGS
338 ELIMINABLE_REGS;
339 #else
340 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
341 #endif
343 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
345 /* Record the number of pending eliminations that have an offset not equal
346 to their initial offset. If nonzero, we use a new copy of each
347 replacement result in any insns encountered. */
348 int num_not_at_initial_offset;
350 /* Count the number of registers that we may be able to eliminate. */
351 static int num_eliminable;
352 /* And the number of registers that are equivalent to a constant that
353 can be eliminated to frame_pointer / arg_pointer + constant. */
354 static int num_eliminable_invariants;
356 /* For each label, we record the offset of each elimination. If we reach
357 a label by more than one path and an offset differs, we cannot do the
358 elimination. This information is indexed by the difference of the
359 number of the label and the first label number. We can't offset the
360 pointer itself as this can cause problems on machines with segmented
361 memory. The first table is an array of flags that records whether we
362 have yet encountered a label and the second table is an array of arrays,
363 one entry in the latter array for each elimination. */
365 static int first_label_num;
366 static char *offsets_known_at;
367 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
369 /* Number of labels in the current function. */
371 static int num_labels;
373 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
374 static void maybe_fix_stack_asms (void);
375 static void copy_reloads (struct insn_chain *);
376 static void calculate_needs_all_insns (int);
377 static int find_reg (struct insn_chain *, int);
378 static void find_reload_regs (struct insn_chain *);
379 static void select_reload_regs (void);
380 static void delete_caller_save_insns (void);
382 static void spill_failure (rtx, enum reg_class);
383 static void count_spilled_pseudo (int, int, int);
384 static void delete_dead_insn (rtx);
385 static void alter_reg (int, int);
386 static void set_label_offsets (rtx, rtx, int);
387 static void check_eliminable_occurrences (rtx);
388 static void elimination_effects (rtx, enum machine_mode);
389 static int eliminate_regs_in_insn (rtx, int);
390 static void update_eliminable_offsets (void);
391 static void mark_not_eliminable (rtx, rtx, void *);
392 static void set_initial_elim_offsets (void);
393 static bool verify_initial_elim_offsets (void);
394 static void set_initial_label_offsets (void);
395 static void set_offsets_for_label (rtx);
396 static void init_elim_table (void);
397 static void update_eliminables (HARD_REG_SET *);
398 static void spill_hard_reg (unsigned int, int);
399 static int finish_spills (int);
400 static void scan_paradoxical_subregs (rtx);
401 static void count_pseudo (int);
402 static void order_regs_for_reload (struct insn_chain *);
403 static void reload_as_needed (int);
404 static void forget_old_reloads_1 (rtx, rtx, void *);
405 static int reload_reg_class_lower (const void *, const void *);
406 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
407 enum machine_mode);
408 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
409 enum machine_mode);
410 static int reload_reg_free_p (unsigned int, int, enum reload_type);
411 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
412 rtx, rtx, int, int);
413 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
414 rtx, rtx, int, int);
415 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
416 static int allocate_reload_reg (struct insn_chain *, int, int);
417 static int conflicts_with_override (rtx);
418 static void failed_reload (rtx, int);
419 static int set_reload_reg (int, int);
420 static void choose_reload_regs_init (struct insn_chain *, rtx *);
421 static void choose_reload_regs (struct insn_chain *);
422 static void merge_assigned_reloads (rtx);
423 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
424 rtx, int);
425 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
426 int);
427 static void do_input_reload (struct insn_chain *, struct reload *, int);
428 static void do_output_reload (struct insn_chain *, struct reload *, int);
429 static bool inherit_piecemeal_p (int, int);
430 static void emit_reload_insns (struct insn_chain *);
431 static void delete_output_reload (rtx, int, int);
432 static void delete_address_reloads (rtx, rtx);
433 static void delete_address_reloads_1 (rtx, rtx, rtx);
434 static rtx inc_for_reload (rtx, rtx, rtx, int);
435 #ifdef AUTO_INC_DEC
436 static void add_auto_inc_notes (rtx, rtx);
437 #endif
438 static void copy_eh_notes (rtx, rtx);
439 static int reloads_conflict (int, int);
440 static rtx gen_reload (rtx, rtx, int, enum reload_type);
441 static rtx emit_insn_if_valid_for_reload (rtx);
443 /* Initialize the reload pass once per compilation. */
445 void
446 init_reload (void)
448 int i;
450 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
451 Set spill_indirect_levels to the number of levels such addressing is
452 permitted, zero if it is not permitted at all. */
454 rtx tem
455 = gen_rtx_MEM (Pmode,
456 gen_rtx_PLUS (Pmode,
457 gen_rtx_REG (Pmode,
458 LAST_VIRTUAL_REGISTER + 1),
459 GEN_INT (4)));
460 spill_indirect_levels = 0;
462 while (memory_address_p (QImode, tem))
464 spill_indirect_levels++;
465 tem = gen_rtx_MEM (Pmode, tem);
468 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
470 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
471 indirect_symref_ok = memory_address_p (QImode, tem);
473 /* See if reg+reg is a valid (and offsettable) address. */
475 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
477 tem = gen_rtx_PLUS (Pmode,
478 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
479 gen_rtx_REG (Pmode, i));
481 /* This way, we make sure that reg+reg is an offsettable address. */
482 tem = plus_constant (tem, 4);
484 if (memory_address_p (QImode, tem))
486 double_reg_address_ok = 1;
487 break;
491 /* Initialize obstack for our rtl allocation. */
492 gcc_obstack_init (&reload_obstack);
493 reload_startobj = obstack_alloc (&reload_obstack, 0);
495 INIT_REG_SET (&spilled_pseudos);
496 INIT_REG_SET (&pseudos_counted);
497 VARRAY_RTX_INIT (reg_equiv_memory_loc_varray, 0, "reg_equiv_memory_loc");
500 /* List of insn chains that are currently unused. */
501 static struct insn_chain *unused_insn_chains = 0;
503 /* Allocate an empty insn_chain structure. */
504 struct insn_chain *
505 new_insn_chain (void)
507 struct insn_chain *c;
509 if (unused_insn_chains == 0)
511 c = obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
512 INIT_REG_SET (&c->live_throughout);
513 INIT_REG_SET (&c->dead_or_set);
515 else
517 c = unused_insn_chains;
518 unused_insn_chains = c->next;
520 c->is_caller_save_insn = 0;
521 c->need_operand_change = 0;
522 c->need_reload = 0;
523 c->need_elim = 0;
524 return c;
527 /* Small utility function to set all regs in hard reg set TO which are
528 allocated to pseudos in regset FROM. */
530 void
531 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
533 unsigned int regno;
534 reg_set_iterator rsi;
536 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
538 int r = reg_renumber[regno];
539 int nregs;
541 if (r < 0)
543 /* reload_combine uses the information from
544 BASIC_BLOCK->global_live_at_start, which might still
545 contain registers that have not actually been allocated
546 since they have an equivalence. */
547 gcc_assert (reload_completed);
549 else
551 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
552 while (nregs-- > 0)
553 SET_HARD_REG_BIT (*to, r + nregs);
558 /* Replace all pseudos found in LOC with their corresponding
559 equivalences. */
561 static void
562 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
564 rtx x = *loc;
565 enum rtx_code code;
566 const char *fmt;
567 int i, j;
569 if (! x)
570 return;
572 code = GET_CODE (x);
573 if (code == REG)
575 unsigned int regno = REGNO (x);
577 if (regno < FIRST_PSEUDO_REGISTER)
578 return;
580 x = eliminate_regs (x, mem_mode, usage);
581 if (x != *loc)
583 *loc = x;
584 replace_pseudos_in (loc, mem_mode, usage);
585 return;
588 if (reg_equiv_constant[regno])
589 *loc = reg_equiv_constant[regno];
590 else if (reg_equiv_mem[regno])
591 *loc = reg_equiv_mem[regno];
592 else if (reg_equiv_address[regno])
593 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
594 else
596 gcc_assert (!REG_P (regno_reg_rtx[regno])
597 || REGNO (regno_reg_rtx[regno]) != regno);
598 *loc = regno_reg_rtx[regno];
601 return;
603 else if (code == MEM)
605 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
606 return;
609 /* Process each of our operands recursively. */
610 fmt = GET_RTX_FORMAT (code);
611 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
612 if (*fmt == 'e')
613 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
614 else if (*fmt == 'E')
615 for (j = 0; j < XVECLEN (x, i); j++)
616 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
620 /* Global variables used by reload and its subroutines. */
622 /* Set during calculate_needs if an insn needs register elimination. */
623 static int something_needs_elimination;
624 /* Set during calculate_needs if an insn needs an operand changed. */
625 static int something_needs_operands_changed;
627 /* Nonzero means we couldn't get enough spill regs. */
628 static int failure;
630 /* Main entry point for the reload pass.
632 FIRST is the first insn of the function being compiled.
634 GLOBAL nonzero means we were called from global_alloc
635 and should attempt to reallocate any pseudoregs that we
636 displace from hard regs we will use for reloads.
637 If GLOBAL is zero, we do not have enough information to do that,
638 so any pseudo reg that is spilled must go to the stack.
640 Return value is nonzero if reload failed
641 and we must not do any more for this function. */
644 reload (rtx first, int global)
646 int i;
647 rtx insn;
648 struct elim_table *ep;
649 basic_block bb;
651 /* Make sure even insns with volatile mem refs are recognizable. */
652 init_recog ();
654 failure = 0;
656 reload_firstobj = obstack_alloc (&reload_obstack, 0);
658 /* Make sure that the last insn in the chain
659 is not something that needs reloading. */
660 emit_note (NOTE_INSN_DELETED);
662 /* Enable find_equiv_reg to distinguish insns made by reload. */
663 reload_first_uid = get_max_uid ();
665 #ifdef SECONDARY_MEMORY_NEEDED
666 /* Initialize the secondary memory table. */
667 clear_secondary_mem ();
668 #endif
670 /* We don't have a stack slot for any spill reg yet. */
671 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
672 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
674 /* Initialize the save area information for caller-save, in case some
675 are needed. */
676 init_save_areas ();
678 /* Compute which hard registers are now in use
679 as homes for pseudo registers.
680 This is done here rather than (eg) in global_alloc
681 because this point is reached even if not optimizing. */
682 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
683 mark_home_live (i);
685 /* A function that receives a nonlocal goto must save all call-saved
686 registers. */
687 if (current_function_has_nonlocal_label)
688 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
689 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
690 regs_ever_live[i] = 1;
692 /* Find all the pseudo registers that didn't get hard regs
693 but do have known equivalent constants or memory slots.
694 These include parameters (known equivalent to parameter slots)
695 and cse'd or loop-moved constant memory addresses.
697 Record constant equivalents in reg_equiv_constant
698 so they will be substituted by find_reloads.
699 Record memory equivalents in reg_mem_equiv so they can
700 be substituted eventually by altering the REG-rtx's. */
702 reg_equiv_constant = xcalloc (max_regno, sizeof (rtx));
703 reg_equiv_invariant = xcalloc (max_regno, sizeof (rtx));
704 reg_equiv_mem = xcalloc (max_regno, sizeof (rtx));
705 reg_equiv_address = xcalloc (max_regno, sizeof (rtx));
706 reg_max_ref_width = xcalloc (max_regno, sizeof (int));
707 reg_old_renumber = xcalloc (max_regno, sizeof (short));
708 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
709 pseudo_forbidden_regs = xmalloc (max_regno * sizeof (HARD_REG_SET));
710 pseudo_previous_regs = xcalloc (max_regno, sizeof (HARD_REG_SET));
712 CLEAR_HARD_REG_SET (bad_spill_regs_global);
714 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
715 to. Also find all paradoxical subregs and find largest such for
716 each pseudo. */
718 num_eliminable_invariants = 0;
719 for (insn = first; insn; insn = NEXT_INSN (insn))
721 rtx set = single_set (insn);
723 /* We may introduce USEs that we want to remove at the end, so
724 we'll mark them with QImode. Make sure there are no
725 previously-marked insns left by say regmove. */
726 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
727 && GET_MODE (insn) != VOIDmode)
728 PUT_MODE (insn, VOIDmode);
730 if (INSN_P (insn))
731 scan_paradoxical_subregs (PATTERN (insn));
733 if (set != 0 && REG_P (SET_DEST (set)))
735 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
736 rtx x;
738 if (! note)
739 continue;
741 i = REGNO (SET_DEST (set));
742 x = XEXP (note, 0);
744 if (i <= LAST_VIRTUAL_REGISTER)
745 continue;
747 if (! function_invariant_p (x)
748 || ! flag_pic
749 /* A function invariant is often CONSTANT_P but may
750 include a register. We promise to only pass
751 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
752 || (CONSTANT_P (x)
753 && LEGITIMATE_PIC_OPERAND_P (x)))
755 /* It can happen that a REG_EQUIV note contains a MEM
756 that is not a legitimate memory operand. As later
757 stages of reload assume that all addresses found
758 in the reg_equiv_* arrays were originally legitimate,
759 we ignore such REG_EQUIV notes. */
760 if (memory_operand (x, VOIDmode))
762 /* Always unshare the equivalence, so we can
763 substitute into this insn without touching the
764 equivalence. */
765 reg_equiv_memory_loc[i] = copy_rtx (x);
767 else if (function_invariant_p (x))
769 if (GET_CODE (x) == PLUS)
771 /* This is PLUS of frame pointer and a constant,
772 and might be shared. Unshare it. */
773 reg_equiv_invariant[i] = copy_rtx (x);
774 num_eliminable_invariants++;
776 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
778 reg_equiv_invariant[i] = x;
779 num_eliminable_invariants++;
781 else if (LEGITIMATE_CONSTANT_P (x))
782 reg_equiv_constant[i] = x;
783 else
785 reg_equiv_memory_loc[i]
786 = force_const_mem (GET_MODE (SET_DEST (set)), x);
787 if (! reg_equiv_memory_loc[i])
788 reg_equiv_init[i] = NULL_RTX;
791 else
793 reg_equiv_init[i] = NULL_RTX;
794 continue;
797 else
798 reg_equiv_init[i] = NULL_RTX;
802 if (dump_file)
803 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
804 if (reg_equiv_init[i])
806 fprintf (dump_file, "init_insns for %u: ", i);
807 print_inline_rtx (dump_file, reg_equiv_init[i], 20);
808 fprintf (dump_file, "\n");
811 init_elim_table ();
813 first_label_num = get_first_label_num ();
814 num_labels = max_label_num () - first_label_num;
816 /* Allocate the tables used to store offset information at labels. */
817 /* We used to use alloca here, but the size of what it would try to
818 allocate would occasionally cause it to exceed the stack limit and
819 cause a core dump. */
820 offsets_known_at = xmalloc (num_labels);
821 offsets_at = xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
823 /* Alter each pseudo-reg rtx to contain its hard reg number.
824 Assign stack slots to the pseudos that lack hard regs or equivalents.
825 Do not touch virtual registers. */
827 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
828 alter_reg (i, -1);
830 /* If we have some registers we think can be eliminated, scan all insns to
831 see if there is an insn that sets one of these registers to something
832 other than itself plus a constant. If so, the register cannot be
833 eliminated. Doing this scan here eliminates an extra pass through the
834 main reload loop in the most common case where register elimination
835 cannot be done. */
836 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
837 if (INSN_P (insn))
838 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
840 maybe_fix_stack_asms ();
842 insns_need_reload = 0;
843 something_needs_elimination = 0;
845 /* Initialize to -1, which means take the first spill register. */
846 last_spill_reg = -1;
848 /* Spill any hard regs that we know we can't eliminate. */
849 CLEAR_HARD_REG_SET (used_spill_regs);
850 /* There can be multiple ways to eliminate a register;
851 they should be listed adjacently.
852 Elimination for any register fails only if all possible ways fail. */
853 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
855 int from = ep->from;
856 int can_eliminate = 0;
859 can_eliminate |= ep->can_eliminate;
860 ep++;
862 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
863 if (! can_eliminate)
864 spill_hard_reg (from, 1);
867 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
868 if (frame_pointer_needed)
869 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
870 #endif
871 finish_spills (global);
873 /* From now on, we may need to generate moves differently. We may also
874 allow modifications of insns which cause them to not be recognized.
875 Any such modifications will be cleaned up during reload itself. */
876 reload_in_progress = 1;
878 /* This loop scans the entire function each go-round
879 and repeats until one repetition spills no additional hard regs. */
880 for (;;)
882 int something_changed;
883 int did_spill;
885 HOST_WIDE_INT starting_frame_size;
887 /* Round size of stack frame to stack_alignment_needed. This must be done
888 here because the stack size may be a part of the offset computation
889 for register elimination, and there might have been new stack slots
890 created in the last iteration of this loop. */
891 if (cfun->stack_alignment_needed)
892 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
894 starting_frame_size = get_frame_size ();
896 set_initial_elim_offsets ();
897 set_initial_label_offsets ();
899 /* For each pseudo register that has an equivalent location defined,
900 try to eliminate any eliminable registers (such as the frame pointer)
901 assuming initial offsets for the replacement register, which
902 is the normal case.
904 If the resulting location is directly addressable, substitute
905 the MEM we just got directly for the old REG.
907 If it is not addressable but is a constant or the sum of a hard reg
908 and constant, it is probably not addressable because the constant is
909 out of range, in that case record the address; we will generate
910 hairy code to compute the address in a register each time it is
911 needed. Similarly if it is a hard register, but one that is not
912 valid as an address register.
914 If the location is not addressable, but does not have one of the
915 above forms, assign a stack slot. We have to do this to avoid the
916 potential of producing lots of reloads if, e.g., a location involves
917 a pseudo that didn't get a hard register and has an equivalent memory
918 location that also involves a pseudo that didn't get a hard register.
920 Perhaps at some point we will improve reload_when_needed handling
921 so this problem goes away. But that's very hairy. */
923 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
924 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
926 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
928 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
929 XEXP (x, 0)))
930 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
931 else if (CONSTANT_P (XEXP (x, 0))
932 || (REG_P (XEXP (x, 0))
933 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
934 || (GET_CODE (XEXP (x, 0)) == PLUS
935 && REG_P (XEXP (XEXP (x, 0), 0))
936 && (REGNO (XEXP (XEXP (x, 0), 0))
937 < FIRST_PSEUDO_REGISTER)
938 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
939 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
940 else
942 /* Make a new stack slot. Then indicate that something
943 changed so we go back and recompute offsets for
944 eliminable registers because the allocation of memory
945 below might change some offset. reg_equiv_{mem,address}
946 will be set up for this pseudo on the next pass around
947 the loop. */
948 reg_equiv_memory_loc[i] = 0;
949 reg_equiv_init[i] = 0;
950 alter_reg (i, -1);
954 if (caller_save_needed)
955 setup_save_areas ();
957 /* If we allocated another stack slot, redo elimination bookkeeping. */
958 if (starting_frame_size != get_frame_size ())
959 continue;
961 if (caller_save_needed)
963 save_call_clobbered_regs ();
964 /* That might have allocated new insn_chain structures. */
965 reload_firstobj = obstack_alloc (&reload_obstack, 0);
968 calculate_needs_all_insns (global);
970 CLEAR_REG_SET (&spilled_pseudos);
971 did_spill = 0;
973 something_changed = 0;
975 /* If we allocated any new memory locations, make another pass
976 since it might have changed elimination offsets. */
977 if (starting_frame_size != get_frame_size ())
978 something_changed = 1;
980 /* Even if the frame size remained the same, we might still have
981 changed elimination offsets, e.g. if find_reloads called
982 force_const_mem requiring the back end to allocate a constant
983 pool base register that needs to be saved on the stack. */
984 else if (!verify_initial_elim_offsets ())
985 something_changed = 1;
988 HARD_REG_SET to_spill;
989 CLEAR_HARD_REG_SET (to_spill);
990 update_eliminables (&to_spill);
991 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
992 if (TEST_HARD_REG_BIT (to_spill, i))
994 spill_hard_reg (i, 1);
995 did_spill = 1;
997 /* Regardless of the state of spills, if we previously had
998 a register that we thought we could eliminate, but now can
999 not eliminate, we must run another pass.
1001 Consider pseudos which have an entry in reg_equiv_* which
1002 reference an eliminable register. We must make another pass
1003 to update reg_equiv_* so that we do not substitute in the
1004 old value from when we thought the elimination could be
1005 performed. */
1006 something_changed = 1;
1010 select_reload_regs ();
1011 if (failure)
1012 goto failed;
1014 if (insns_need_reload != 0 || did_spill)
1015 something_changed |= finish_spills (global);
1017 if (! something_changed)
1018 break;
1020 if (caller_save_needed)
1021 delete_caller_save_insns ();
1023 obstack_free (&reload_obstack, reload_firstobj);
1026 /* If global-alloc was run, notify it of any register eliminations we have
1027 done. */
1028 if (global)
1029 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1030 if (ep->can_eliminate)
1031 mark_elimination (ep->from, ep->to);
1033 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1034 If that insn didn't set the register (i.e., it copied the register to
1035 memory), just delete that insn instead of the equivalencing insn plus
1036 anything now dead. If we call delete_dead_insn on that insn, we may
1037 delete the insn that actually sets the register if the register dies
1038 there and that is incorrect. */
1040 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1042 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1044 rtx list;
1045 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1047 rtx equiv_insn = XEXP (list, 0);
1049 /* If we already deleted the insn or if it may trap, we can't
1050 delete it. The latter case shouldn't happen, but can
1051 if an insn has a variable address, gets a REG_EH_REGION
1052 note added to it, and then gets converted into a load
1053 from a constant address. */
1054 if (NOTE_P (equiv_insn)
1055 || can_throw_internal (equiv_insn))
1057 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1058 delete_dead_insn (equiv_insn);
1059 else
1060 SET_INSN_DELETED (equiv_insn);
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 gcc_assert (old_frame_size == get_frame_size ());
1078 gcc_assert (verify_initial_elim_offsets ());
1081 /* If we were able to eliminate the frame pointer, show that it is no
1082 longer live at the start of any basic block. If it ls live by
1083 virtue of being in a pseudo, that pseudo will be marked live
1084 and hence the frame pointer will be known to be live via that
1085 pseudo. */
1087 if (! frame_pointer_needed)
1088 FOR_EACH_BB (bb)
1089 CLEAR_REGNO_REG_SET (bb->il.rtl->global_live_at_start,
1090 HARD_FRAME_POINTER_REGNUM);
1092 /* Come here (with failure set nonzero) if we can't get enough spill
1093 regs. */
1094 failed:
1096 CLEAR_REG_SET (&spilled_pseudos);
1097 reload_in_progress = 0;
1099 /* Now eliminate all pseudo regs by modifying them into
1100 their equivalent memory references.
1101 The REG-rtx's for the pseudos are modified in place,
1102 so all insns that used to refer to them now refer to memory.
1104 For a reg that has a reg_equiv_address, all those insns
1105 were changed by reloading so that no insns refer to it any longer;
1106 but the DECL_RTL of a variable decl may refer to it,
1107 and if so this causes the debugging info to mention the variable. */
1109 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1111 rtx addr = 0;
1113 if (reg_equiv_mem[i])
1114 addr = XEXP (reg_equiv_mem[i], 0);
1116 if (reg_equiv_address[i])
1117 addr = reg_equiv_address[i];
1119 if (addr)
1121 if (reg_renumber[i] < 0)
1123 rtx reg = regno_reg_rtx[i];
1125 REG_USERVAR_P (reg) = 0;
1126 PUT_CODE (reg, MEM);
1127 XEXP (reg, 0) = addr;
1128 if (reg_equiv_memory_loc[i])
1129 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1130 else
1132 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1133 MEM_ATTRS (reg) = 0;
1135 MEM_NOTRAP_P (reg) = 1;
1137 else if (reg_equiv_mem[i])
1138 XEXP (reg_equiv_mem[i], 0) = addr;
1142 /* We must set reload_completed now since the cleanup_subreg_operands call
1143 below will re-recognize each insn and reload may have generated insns
1144 which are only valid during and after reload. */
1145 reload_completed = 1;
1147 /* Make a pass over all the insns and delete all USEs which we inserted
1148 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1149 notes. Delete all CLOBBER insns, except those that refer to the return
1150 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1151 from misarranging variable-array code, and simplify (subreg (reg))
1152 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1153 are no longer useful or accurate. Strip and regenerate REG_INC notes
1154 that may have been moved around. */
1156 for (insn = first; insn; insn = NEXT_INSN (insn))
1157 if (INSN_P (insn))
1159 rtx *pnote;
1161 if (CALL_P (insn))
1162 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1163 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1165 if ((GET_CODE (PATTERN (insn)) == USE
1166 /* We mark with QImode USEs introduced by reload itself. */
1167 && (GET_MODE (insn) == QImode
1168 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1169 || (GET_CODE (PATTERN (insn)) == CLOBBER
1170 && (!MEM_P (XEXP (PATTERN (insn), 0))
1171 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1172 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1173 && XEXP (XEXP (PATTERN (insn), 0), 0)
1174 != stack_pointer_rtx))
1175 && (!REG_P (XEXP (PATTERN (insn), 0))
1176 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1178 delete_insn (insn);
1179 continue;
1182 /* Some CLOBBERs may survive until here and still reference unassigned
1183 pseudos with const equivalent, which may in turn cause ICE in later
1184 passes if the reference remains in place. */
1185 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1186 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1187 VOIDmode, PATTERN (insn));
1189 /* Discard obvious no-ops, even without -O. This optimization
1190 is fast and doesn't interfere with debugging. */
1191 if (NONJUMP_INSN_P (insn)
1192 && GET_CODE (PATTERN (insn)) == SET
1193 && REG_P (SET_SRC (PATTERN (insn)))
1194 && REG_P (SET_DEST (PATTERN (insn)))
1195 && (REGNO (SET_SRC (PATTERN (insn)))
1196 == REGNO (SET_DEST (PATTERN (insn)))))
1198 delete_insn (insn);
1199 continue;
1202 pnote = &REG_NOTES (insn);
1203 while (*pnote != 0)
1205 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1206 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1207 || REG_NOTE_KIND (*pnote) == REG_INC
1208 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1209 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1210 *pnote = XEXP (*pnote, 1);
1211 else
1212 pnote = &XEXP (*pnote, 1);
1215 #ifdef AUTO_INC_DEC
1216 add_auto_inc_notes (insn, PATTERN (insn));
1217 #endif
1219 /* And simplify (subreg (reg)) if it appears as an operand. */
1220 cleanup_subreg_operands (insn);
1223 /* If we are doing stack checking, give a warning if this function's
1224 frame size is larger than we expect. */
1225 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1227 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1228 static int verbose_warned = 0;
1230 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1231 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1232 size += UNITS_PER_WORD;
1234 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1236 warning (0, "frame size too large for reliable stack checking");
1237 if (! verbose_warned)
1239 warning (0, "try reducing the number of local variables");
1240 verbose_warned = 1;
1245 /* Indicate that we no longer have known memory locations or constants. */
1246 if (reg_equiv_constant)
1247 free (reg_equiv_constant);
1248 if (reg_equiv_invariant)
1249 free (reg_equiv_invariant);
1250 reg_equiv_constant = 0;
1251 reg_equiv_invariant = 0;
1252 VARRAY_GROW (reg_equiv_memory_loc_varray, 0);
1253 reg_equiv_memory_loc = 0;
1255 if (offsets_known_at)
1256 free (offsets_known_at);
1257 if (offsets_at)
1258 free (offsets_at);
1260 free (reg_equiv_mem);
1261 reg_equiv_init = 0;
1262 free (reg_equiv_address);
1263 free (reg_max_ref_width);
1264 free (reg_old_renumber);
1265 free (pseudo_previous_regs);
1266 free (pseudo_forbidden_regs);
1268 CLEAR_HARD_REG_SET (used_spill_regs);
1269 for (i = 0; i < n_spills; i++)
1270 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1272 /* Free all the insn_chain structures at once. */
1273 obstack_free (&reload_obstack, reload_startobj);
1274 unused_insn_chains = 0;
1275 fixup_abnormal_edges ();
1277 /* Replacing pseudos with their memory equivalents might have
1278 created shared rtx. Subsequent passes would get confused
1279 by this, so unshare everything here. */
1280 unshare_all_rtl_again (first);
1282 #ifdef STACK_BOUNDARY
1283 /* init_emit has set the alignment of the hard frame pointer
1284 to STACK_BOUNDARY. It is very likely no longer valid if
1285 the hard frame pointer was used for register allocation. */
1286 if (!frame_pointer_needed)
1287 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1288 #endif
1290 return failure;
1293 /* Yet another special case. Unfortunately, reg-stack forces people to
1294 write incorrect clobbers in asm statements. These clobbers must not
1295 cause the register to appear in bad_spill_regs, otherwise we'll call
1296 fatal_insn later. We clear the corresponding regnos in the live
1297 register sets to avoid this.
1298 The whole thing is rather sick, I'm afraid. */
1300 static void
1301 maybe_fix_stack_asms (void)
1303 #ifdef STACK_REGS
1304 const char *constraints[MAX_RECOG_OPERANDS];
1305 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1306 struct insn_chain *chain;
1308 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1310 int i, noperands;
1311 HARD_REG_SET clobbered, allowed;
1312 rtx pat;
1314 if (! INSN_P (chain->insn)
1315 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1316 continue;
1317 pat = PATTERN (chain->insn);
1318 if (GET_CODE (pat) != PARALLEL)
1319 continue;
1321 CLEAR_HARD_REG_SET (clobbered);
1322 CLEAR_HARD_REG_SET (allowed);
1324 /* First, make a mask of all stack regs that are clobbered. */
1325 for (i = 0; i < XVECLEN (pat, 0); i++)
1327 rtx t = XVECEXP (pat, 0, i);
1328 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1329 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1332 /* Get the operand values and constraints out of the insn. */
1333 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1334 constraints, operand_mode);
1336 /* For every operand, see what registers are allowed. */
1337 for (i = 0; i < noperands; i++)
1339 const char *p = constraints[i];
1340 /* For every alternative, we compute the class of registers allowed
1341 for reloading in CLS, and merge its contents into the reg set
1342 ALLOWED. */
1343 int cls = (int) NO_REGS;
1345 for (;;)
1347 char c = *p;
1349 if (c == '\0' || c == ',' || c == '#')
1351 /* End of one alternative - mark the regs in the current
1352 class, and reset the class. */
1353 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1354 cls = NO_REGS;
1355 p++;
1356 if (c == '#')
1357 do {
1358 c = *p++;
1359 } while (c != '\0' && c != ',');
1360 if (c == '\0')
1361 break;
1362 continue;
1365 switch (c)
1367 case '=': case '+': case '*': case '%': case '?': case '!':
1368 case '0': case '1': case '2': case '3': case '4': case 'm':
1369 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1370 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1371 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1372 case 'P':
1373 break;
1375 case 'p':
1376 cls = (int) reg_class_subunion[cls]
1377 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1378 break;
1380 case 'g':
1381 case 'r':
1382 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1383 break;
1385 default:
1386 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1387 cls = (int) reg_class_subunion[cls]
1388 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1389 else
1390 cls = (int) reg_class_subunion[cls]
1391 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1393 p += CONSTRAINT_LEN (c, p);
1396 /* Those of the registers which are clobbered, but allowed by the
1397 constraints, must be usable as reload registers. So clear them
1398 out of the life information. */
1399 AND_HARD_REG_SET (allowed, clobbered);
1400 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1401 if (TEST_HARD_REG_BIT (allowed, i))
1403 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1404 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1408 #endif
1411 /* Copy the global variables n_reloads and rld into the corresponding elts
1412 of CHAIN. */
1413 static void
1414 copy_reloads (struct insn_chain *chain)
1416 chain->n_reloads = n_reloads;
1417 chain->rld = obstack_alloc (&reload_obstack,
1418 n_reloads * sizeof (struct reload));
1419 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1420 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1423 /* Walk the chain of insns, and determine for each whether it needs reloads
1424 and/or eliminations. Build the corresponding insns_need_reload list, and
1425 set something_needs_elimination as appropriate. */
1426 static void
1427 calculate_needs_all_insns (int global)
1429 struct insn_chain **pprev_reload = &insns_need_reload;
1430 struct insn_chain *chain, *next = 0;
1432 something_needs_elimination = 0;
1434 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1435 for (chain = reload_insn_chain; chain != 0; chain = next)
1437 rtx insn = chain->insn;
1439 next = chain->next;
1441 /* Clear out the shortcuts. */
1442 chain->n_reloads = 0;
1443 chain->need_elim = 0;
1444 chain->need_reload = 0;
1445 chain->need_operand_change = 0;
1447 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1448 include REG_LABEL), we need to see what effects this has on the
1449 known offsets at labels. */
1451 if (LABEL_P (insn) || JUMP_P (insn)
1452 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1453 set_label_offsets (insn, insn, 0);
1455 if (INSN_P (insn))
1457 rtx old_body = PATTERN (insn);
1458 int old_code = INSN_CODE (insn);
1459 rtx old_notes = REG_NOTES (insn);
1460 int did_elimination = 0;
1461 int operands_changed = 0;
1462 rtx set = single_set (insn);
1464 /* Skip insns that only set an equivalence. */
1465 if (set && REG_P (SET_DEST (set))
1466 && reg_renumber[REGNO (SET_DEST (set))] < 0
1467 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1468 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1469 && reg_equiv_init[REGNO (SET_DEST (set))])
1470 continue;
1472 /* If needed, eliminate any eliminable registers. */
1473 if (num_eliminable || num_eliminable_invariants)
1474 did_elimination = eliminate_regs_in_insn (insn, 0);
1476 /* Analyze the instruction. */
1477 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1478 global, spill_reg_order);
1480 /* If a no-op set needs more than one reload, this is likely
1481 to be something that needs input address reloads. We
1482 can't get rid of this cleanly later, and it is of no use
1483 anyway, so discard it now.
1484 We only do this when expensive_optimizations is enabled,
1485 since this complements reload inheritance / output
1486 reload deletion, and it can make debugging harder. */
1487 if (flag_expensive_optimizations && n_reloads > 1)
1489 rtx set = single_set (insn);
1490 if (set
1491 && SET_SRC (set) == SET_DEST (set)
1492 && REG_P (SET_SRC (set))
1493 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1495 delete_insn (insn);
1496 /* Delete it from the reload chain. */
1497 if (chain->prev)
1498 chain->prev->next = next;
1499 else
1500 reload_insn_chain = next;
1501 if (next)
1502 next->prev = chain->prev;
1503 chain->next = unused_insn_chains;
1504 unused_insn_chains = chain;
1505 continue;
1508 if (num_eliminable)
1509 update_eliminable_offsets ();
1511 /* Remember for later shortcuts which insns had any reloads or
1512 register eliminations. */
1513 chain->need_elim = did_elimination;
1514 chain->need_reload = n_reloads > 0;
1515 chain->need_operand_change = operands_changed;
1517 /* Discard any register replacements done. */
1518 if (did_elimination)
1520 obstack_free (&reload_obstack, reload_insn_firstobj);
1521 PATTERN (insn) = old_body;
1522 INSN_CODE (insn) = old_code;
1523 REG_NOTES (insn) = old_notes;
1524 something_needs_elimination = 1;
1527 something_needs_operands_changed |= operands_changed;
1529 if (n_reloads != 0)
1531 copy_reloads (chain);
1532 *pprev_reload = chain;
1533 pprev_reload = &chain->next_need_reload;
1537 *pprev_reload = 0;
1540 /* Comparison function for qsort to decide which of two reloads
1541 should be handled first. *P1 and *P2 are the reload numbers. */
1543 static int
1544 reload_reg_class_lower (const void *r1p, const void *r2p)
1546 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1547 int t;
1549 /* Consider required reloads before optional ones. */
1550 t = rld[r1].optional - rld[r2].optional;
1551 if (t != 0)
1552 return t;
1554 /* Count all solitary classes before non-solitary ones. */
1555 t = ((reg_class_size[(int) rld[r2].class] == 1)
1556 - (reg_class_size[(int) rld[r1].class] == 1));
1557 if (t != 0)
1558 return t;
1560 /* Aside from solitaires, consider all multi-reg groups first. */
1561 t = rld[r2].nregs - rld[r1].nregs;
1562 if (t != 0)
1563 return t;
1565 /* Consider reloads in order of increasing reg-class number. */
1566 t = (int) rld[r1].class - (int) rld[r2].class;
1567 if (t != 0)
1568 return t;
1570 /* If reloads are equally urgent, sort by reload number,
1571 so that the results of qsort leave nothing to chance. */
1572 return r1 - r2;
1575 /* The cost of spilling each hard reg. */
1576 static int spill_cost[FIRST_PSEUDO_REGISTER];
1578 /* When spilling multiple hard registers, we use SPILL_COST for the first
1579 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1580 only the first hard reg for a multi-reg pseudo. */
1581 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1583 /* Update the spill cost arrays, considering that pseudo REG is live. */
1585 static void
1586 count_pseudo (int reg)
1588 int freq = REG_FREQ (reg);
1589 int r = reg_renumber[reg];
1590 int nregs;
1592 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1593 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1594 return;
1596 SET_REGNO_REG_SET (&pseudos_counted, reg);
1598 gcc_assert (r >= 0);
1600 spill_add_cost[r] += freq;
1602 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1603 while (nregs-- > 0)
1604 spill_cost[r + nregs] += freq;
1607 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1608 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1610 static void
1611 order_regs_for_reload (struct insn_chain *chain)
1613 unsigned i;
1614 HARD_REG_SET used_by_pseudos;
1615 HARD_REG_SET used_by_pseudos2;
1616 reg_set_iterator rsi;
1618 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1620 memset (spill_cost, 0, sizeof spill_cost);
1621 memset (spill_add_cost, 0, sizeof spill_add_cost);
1623 /* Count number of uses of each hard reg by pseudo regs allocated to it
1624 and then order them by decreasing use. First exclude hard registers
1625 that are live in or across this insn. */
1627 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1628 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1629 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1630 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1632 /* Now find out which pseudos are allocated to it, and update
1633 hard_reg_n_uses. */
1634 CLEAR_REG_SET (&pseudos_counted);
1636 EXECUTE_IF_SET_IN_REG_SET
1637 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1639 count_pseudo (i);
1641 EXECUTE_IF_SET_IN_REG_SET
1642 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1644 count_pseudo (i);
1646 CLEAR_REG_SET (&pseudos_counted);
1649 /* Vector of reload-numbers showing the order in which the reloads should
1650 be processed. */
1651 static short reload_order[MAX_RELOADS];
1653 /* This is used to keep track of the spill regs used in one insn. */
1654 static HARD_REG_SET used_spill_regs_local;
1656 /* We decided to spill hard register SPILLED, which has a size of
1657 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1658 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1659 update SPILL_COST/SPILL_ADD_COST. */
1661 static void
1662 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1664 int r = reg_renumber[reg];
1665 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1667 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1668 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1669 return;
1671 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1673 spill_add_cost[r] -= REG_FREQ (reg);
1674 while (nregs-- > 0)
1675 spill_cost[r + nregs] -= REG_FREQ (reg);
1678 /* Find reload register to use for reload number ORDER. */
1680 static int
1681 find_reg (struct insn_chain *chain, int order)
1683 int rnum = reload_order[order];
1684 struct reload *rl = rld + rnum;
1685 int best_cost = INT_MAX;
1686 int best_reg = -1;
1687 unsigned int i, j;
1688 int k;
1689 HARD_REG_SET not_usable;
1690 HARD_REG_SET used_by_other_reload;
1691 reg_set_iterator rsi;
1693 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1694 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1695 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1697 CLEAR_HARD_REG_SET (used_by_other_reload);
1698 for (k = 0; k < order; k++)
1700 int other = reload_order[k];
1702 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1703 for (j = 0; j < rld[other].nregs; j++)
1704 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1707 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1709 unsigned int regno = i;
1711 if (! TEST_HARD_REG_BIT (not_usable, regno)
1712 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1713 && HARD_REGNO_MODE_OK (regno, rl->mode))
1715 int this_cost = spill_cost[regno];
1716 int ok = 1;
1717 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1719 for (j = 1; j < this_nregs; j++)
1721 this_cost += spill_add_cost[regno + j];
1722 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1723 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1724 ok = 0;
1726 if (! ok)
1727 continue;
1728 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1729 this_cost--;
1730 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1731 this_cost--;
1732 if (this_cost < best_cost
1733 /* Among registers with equal cost, prefer caller-saved ones, or
1734 use REG_ALLOC_ORDER if it is defined. */
1735 || (this_cost == best_cost
1736 #ifdef REG_ALLOC_ORDER
1737 && (inv_reg_alloc_order[regno]
1738 < inv_reg_alloc_order[best_reg])
1739 #else
1740 && call_used_regs[regno]
1741 && ! call_used_regs[best_reg]
1742 #endif
1745 best_reg = regno;
1746 best_cost = this_cost;
1750 if (best_reg == -1)
1751 return 0;
1753 if (dump_file)
1754 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1756 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1757 rl->regno = best_reg;
1759 EXECUTE_IF_SET_IN_REG_SET
1760 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1762 count_spilled_pseudo (best_reg, rl->nregs, j);
1765 EXECUTE_IF_SET_IN_REG_SET
1766 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1768 count_spilled_pseudo (best_reg, rl->nregs, j);
1771 for (i = 0; i < rl->nregs; i++)
1773 gcc_assert (spill_cost[best_reg + i] == 0);
1774 gcc_assert (spill_add_cost[best_reg + i] == 0);
1775 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1777 return 1;
1780 /* Find more reload regs to satisfy the remaining need of an insn, which
1781 is given by CHAIN.
1782 Do it by ascending class number, since otherwise a reg
1783 might be spilled for a big class and might fail to count
1784 for a smaller class even though it belongs to that class. */
1786 static void
1787 find_reload_regs (struct insn_chain *chain)
1789 int i;
1791 /* In order to be certain of getting the registers we need,
1792 we must sort the reloads into order of increasing register class.
1793 Then our grabbing of reload registers will parallel the process
1794 that provided the reload registers. */
1795 for (i = 0; i < chain->n_reloads; i++)
1797 /* Show whether this reload already has a hard reg. */
1798 if (chain->rld[i].reg_rtx)
1800 int regno = REGNO (chain->rld[i].reg_rtx);
1801 chain->rld[i].regno = regno;
1802 chain->rld[i].nregs
1803 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1805 else
1806 chain->rld[i].regno = -1;
1807 reload_order[i] = i;
1810 n_reloads = chain->n_reloads;
1811 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1813 CLEAR_HARD_REG_SET (used_spill_regs_local);
1815 if (dump_file)
1816 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1818 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1820 /* Compute the order of preference for hard registers to spill. */
1822 order_regs_for_reload (chain);
1824 for (i = 0; i < n_reloads; i++)
1826 int r = reload_order[i];
1828 /* Ignore reloads that got marked inoperative. */
1829 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1830 && ! rld[r].optional
1831 && rld[r].regno == -1)
1832 if (! find_reg (chain, i))
1834 spill_failure (chain->insn, rld[r].class);
1835 failure = 1;
1836 return;
1840 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1841 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1843 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1846 static void
1847 select_reload_regs (void)
1849 struct insn_chain *chain;
1851 /* Try to satisfy the needs for each insn. */
1852 for (chain = insns_need_reload; chain != 0;
1853 chain = chain->next_need_reload)
1854 find_reload_regs (chain);
1857 /* Delete all insns that were inserted by emit_caller_save_insns during
1858 this iteration. */
1859 static void
1860 delete_caller_save_insns (void)
1862 struct insn_chain *c = reload_insn_chain;
1864 while (c != 0)
1866 while (c != 0 && c->is_caller_save_insn)
1868 struct insn_chain *next = c->next;
1869 rtx insn = c->insn;
1871 if (c == reload_insn_chain)
1872 reload_insn_chain = next;
1873 delete_insn (insn);
1875 if (next)
1876 next->prev = c->prev;
1877 if (c->prev)
1878 c->prev->next = next;
1879 c->next = unused_insn_chains;
1880 unused_insn_chains = c;
1881 c = next;
1883 if (c != 0)
1884 c = c->next;
1888 /* Handle the failure to find a register to spill.
1889 INSN should be one of the insns which needed this particular spill reg. */
1891 static void
1892 spill_failure (rtx insn, enum reg_class class)
1894 if (asm_noperands (PATTERN (insn)) >= 0)
1895 error_for_asm (insn, "can't find a register in class %qs while "
1896 "reloading %<asm%>",
1897 reg_class_names[class]);
1898 else
1900 error ("unable to find a register to spill in class %qs",
1901 reg_class_names[class]);
1902 fatal_insn ("this is the insn:", insn);
1906 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1907 data that is dead in INSN. */
1909 static void
1910 delete_dead_insn (rtx insn)
1912 rtx prev = prev_real_insn (insn);
1913 rtx prev_dest;
1915 /* If the previous insn sets a register that dies in our insn, delete it
1916 too. */
1917 if (prev && GET_CODE (PATTERN (prev)) == SET
1918 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
1919 && reg_mentioned_p (prev_dest, PATTERN (insn))
1920 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1921 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1922 delete_dead_insn (prev);
1924 SET_INSN_DELETED (insn);
1927 /* Modify the home of pseudo-reg I.
1928 The new home is present in reg_renumber[I].
1930 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1931 or it may be -1, meaning there is none or it is not relevant.
1932 This is used so that all pseudos spilled from a given hard reg
1933 can share one stack slot. */
1935 static void
1936 alter_reg (int i, int from_reg)
1938 /* When outputting an inline function, this can happen
1939 for a reg that isn't actually used. */
1940 if (regno_reg_rtx[i] == 0)
1941 return;
1943 /* If the reg got changed to a MEM at rtl-generation time,
1944 ignore it. */
1945 if (!REG_P (regno_reg_rtx[i]))
1946 return;
1948 /* Modify the reg-rtx to contain the new hard reg
1949 number or else to contain its pseudo reg number. */
1950 REGNO (regno_reg_rtx[i])
1951 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1953 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1954 allocate a stack slot for it. */
1956 if (reg_renumber[i] < 0
1957 && REG_N_REFS (i) > 0
1958 && reg_equiv_constant[i] == 0
1959 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
1960 && reg_equiv_memory_loc[i] == 0)
1962 rtx x;
1963 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1964 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
1965 int adjust = 0;
1967 /* Each pseudo reg has an inherent size which comes from its own mode,
1968 and a total size which provides room for paradoxical subregs
1969 which refer to the pseudo reg in wider modes.
1971 We can use a slot already allocated if it provides both
1972 enough inherent space and enough total space.
1973 Otherwise, we allocate a new slot, making sure that it has no less
1974 inherent space, and no less total space, then the previous slot. */
1975 if (from_reg == -1)
1977 /* No known place to spill from => no slot to reuse. */
1978 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1979 inherent_size == total_size ? 0 : -1);
1980 if (BYTES_BIG_ENDIAN)
1981 /* Cancel the big-endian correction done in assign_stack_local.
1982 Get the address of the beginning of the slot.
1983 This is so we can do a big-endian correction unconditionally
1984 below. */
1985 adjust = inherent_size - total_size;
1987 /* Nothing can alias this slot except this pseudo. */
1988 set_mem_alias_set (x, new_alias_set ());
1991 /* Reuse a stack slot if possible. */
1992 else if (spill_stack_slot[from_reg] != 0
1993 && spill_stack_slot_width[from_reg] >= total_size
1994 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1995 >= inherent_size))
1996 x = spill_stack_slot[from_reg];
1998 /* Allocate a bigger slot. */
1999 else
2001 /* Compute maximum size needed, both for inherent size
2002 and for total size. */
2003 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2004 rtx stack_slot;
2006 if (spill_stack_slot[from_reg])
2008 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2009 > inherent_size)
2010 mode = GET_MODE (spill_stack_slot[from_reg]);
2011 if (spill_stack_slot_width[from_reg] > total_size)
2012 total_size = spill_stack_slot_width[from_reg];
2015 /* Make a slot with that size. */
2016 x = assign_stack_local (mode, total_size,
2017 inherent_size == total_size ? 0 : -1);
2018 stack_slot = x;
2020 /* All pseudos mapped to this slot can alias each other. */
2021 if (spill_stack_slot[from_reg])
2022 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
2023 else
2024 set_mem_alias_set (x, new_alias_set ());
2026 if (BYTES_BIG_ENDIAN)
2028 /* Cancel the big-endian correction done in assign_stack_local.
2029 Get the address of the beginning of the slot.
2030 This is so we can do a big-endian correction unconditionally
2031 below. */
2032 adjust = GET_MODE_SIZE (mode) - total_size;
2033 if (adjust)
2034 stack_slot
2035 = adjust_address_nv (x, mode_for_size (total_size
2036 * BITS_PER_UNIT,
2037 MODE_INT, 1),
2038 adjust);
2041 spill_stack_slot[from_reg] = stack_slot;
2042 spill_stack_slot_width[from_reg] = total_size;
2045 /* On a big endian machine, the "address" of the slot
2046 is the address of the low part that fits its inherent mode. */
2047 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2048 adjust += (total_size - inherent_size);
2050 /* If we have any adjustment to make, or if the stack slot is the
2051 wrong mode, make a new stack slot. */
2052 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2054 /* If we have a decl for the original register, set it for the
2055 memory. If this is a shared MEM, make a copy. */
2056 if (REG_EXPR (regno_reg_rtx[i])
2057 && DECL_P (REG_EXPR (regno_reg_rtx[i])))
2059 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2061 /* We can do this only for the DECLs home pseudo, not for
2062 any copies of it, since otherwise when the stack slot
2063 is reused, nonoverlapping_memrefs_p might think they
2064 cannot overlap. */
2065 if (decl && REG_P (decl) && REGNO (decl) == (unsigned) i)
2067 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2068 x = copy_rtx (x);
2070 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2074 /* Save the stack slot for later. */
2075 reg_equiv_memory_loc[i] = x;
2079 /* Mark the slots in regs_ever_live for the hard regs
2080 used by pseudo-reg number REGNO. */
2082 void
2083 mark_home_live (int regno)
2085 int i, lim;
2087 i = reg_renumber[regno];
2088 if (i < 0)
2089 return;
2090 lim = i + hard_regno_nregs[i][PSEUDO_REGNO_MODE (regno)];
2091 while (i < lim)
2092 regs_ever_live[i++] = 1;
2095 /* This function handles the tracking of elimination offsets around branches.
2097 X is a piece of RTL being scanned.
2099 INSN is the insn that it came from, if any.
2101 INITIAL_P is nonzero if we are to set the offset to be the initial
2102 offset and zero if we are setting the offset of the label to be the
2103 current offset. */
2105 static void
2106 set_label_offsets (rtx x, rtx insn, int initial_p)
2108 enum rtx_code code = GET_CODE (x);
2109 rtx tem;
2110 unsigned int i;
2111 struct elim_table *p;
2113 switch (code)
2115 case LABEL_REF:
2116 if (LABEL_REF_NONLOCAL_P (x))
2117 return;
2119 x = XEXP (x, 0);
2121 /* ... fall through ... */
2123 case CODE_LABEL:
2124 /* If we know nothing about this label, set the desired offsets. Note
2125 that this sets the offset at a label to be the offset before a label
2126 if we don't know anything about the label. This is not correct for
2127 the label after a BARRIER, but is the best guess we can make. If
2128 we guessed wrong, we will suppress an elimination that might have
2129 been possible had we been able to guess correctly. */
2131 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2133 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2134 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2135 = (initial_p ? reg_eliminate[i].initial_offset
2136 : reg_eliminate[i].offset);
2137 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2140 /* Otherwise, if this is the definition of a label and it is
2141 preceded by a BARRIER, set our offsets to the known offset of
2142 that label. */
2144 else if (x == insn
2145 && (tem = prev_nonnote_insn (insn)) != 0
2146 && BARRIER_P (tem))
2147 set_offsets_for_label (insn);
2148 else
2149 /* If neither of the above cases is true, compare each offset
2150 with those previously recorded and suppress any eliminations
2151 where the offsets disagree. */
2153 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2154 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2155 != (initial_p ? reg_eliminate[i].initial_offset
2156 : reg_eliminate[i].offset))
2157 reg_eliminate[i].can_eliminate = 0;
2159 return;
2161 case JUMP_INSN:
2162 set_label_offsets (PATTERN (insn), insn, initial_p);
2164 /* ... fall through ... */
2166 case INSN:
2167 case CALL_INSN:
2168 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2169 and hence must have all eliminations at their initial offsets. */
2170 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2171 if (REG_NOTE_KIND (tem) == REG_LABEL)
2172 set_label_offsets (XEXP (tem, 0), insn, 1);
2173 return;
2175 case PARALLEL:
2176 case ADDR_VEC:
2177 case ADDR_DIFF_VEC:
2178 /* Each of the labels in the parallel or address vector must be
2179 at their initial offsets. We want the first field for PARALLEL
2180 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2182 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2183 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2184 insn, initial_p);
2185 return;
2187 case SET:
2188 /* We only care about setting PC. If the source is not RETURN,
2189 IF_THEN_ELSE, or a label, disable any eliminations not at
2190 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2191 isn't one of those possibilities. For branches to a label,
2192 call ourselves recursively.
2194 Note that this can disable elimination unnecessarily when we have
2195 a non-local goto since it will look like a non-constant jump to
2196 someplace in the current function. This isn't a significant
2197 problem since such jumps will normally be when all elimination
2198 pairs are back to their initial offsets. */
2200 if (SET_DEST (x) != pc_rtx)
2201 return;
2203 switch (GET_CODE (SET_SRC (x)))
2205 case PC:
2206 case RETURN:
2207 return;
2209 case LABEL_REF:
2210 set_label_offsets (SET_SRC (x), insn, initial_p);
2211 return;
2213 case IF_THEN_ELSE:
2214 tem = XEXP (SET_SRC (x), 1);
2215 if (GET_CODE (tem) == LABEL_REF)
2216 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2217 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2218 break;
2220 tem = XEXP (SET_SRC (x), 2);
2221 if (GET_CODE (tem) == LABEL_REF)
2222 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2223 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2224 break;
2225 return;
2227 default:
2228 break;
2231 /* If we reach here, all eliminations must be at their initial
2232 offset because we are doing a jump to a variable address. */
2233 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2234 if (p->offset != p->initial_offset)
2235 p->can_eliminate = 0;
2236 break;
2238 default:
2239 break;
2243 /* Scan X and replace any eliminable registers (such as fp) with a
2244 replacement (such as sp), plus an offset.
2246 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2247 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2248 MEM, we are allowed to replace a sum of a register and the constant zero
2249 with the register, which we cannot do outside a MEM. In addition, we need
2250 to record the fact that a register is referenced outside a MEM.
2252 If INSN is an insn, it is the insn containing X. If we replace a REG
2253 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2254 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2255 the REG is being modified.
2257 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2258 That's used when we eliminate in expressions stored in notes.
2259 This means, do not set ref_outside_mem even if the reference
2260 is outside of MEMs.
2262 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2263 replacements done assuming all offsets are at their initial values. If
2264 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2265 encounter, return the actual location so that find_reloads will do
2266 the proper thing. */
2268 static rtx
2269 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2270 bool may_use_invariant)
2272 enum rtx_code code = GET_CODE (x);
2273 struct elim_table *ep;
2274 int regno;
2275 rtx new;
2276 int i, j;
2277 const char *fmt;
2278 int copied = 0;
2280 if (! current_function_decl)
2281 return x;
2283 switch (code)
2285 case CONST_INT:
2286 case CONST_DOUBLE:
2287 case CONST_VECTOR:
2288 case CONST:
2289 case SYMBOL_REF:
2290 case CODE_LABEL:
2291 case PC:
2292 case CC0:
2293 case ASM_INPUT:
2294 case ADDR_VEC:
2295 case ADDR_DIFF_VEC:
2296 case RETURN:
2297 return x;
2299 case REG:
2300 regno = REGNO (x);
2302 /* First handle the case where we encounter a bare register that
2303 is eliminable. Replace it with a PLUS. */
2304 if (regno < FIRST_PSEUDO_REGISTER)
2306 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2307 ep++)
2308 if (ep->from_rtx == x && ep->can_eliminate)
2309 return plus_constant (ep->to_rtx, ep->previous_offset);
2312 else if (reg_renumber && reg_renumber[regno] < 0
2313 && reg_equiv_invariant && reg_equiv_invariant[regno])
2315 if (may_use_invariant)
2316 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2317 mem_mode, insn, true);
2318 /* There exists at least one use of REGNO that cannot be
2319 eliminated. Prevent the defining insn from being deleted. */
2320 reg_equiv_init[regno] = NULL_RTX;
2321 alter_reg (regno, -1);
2323 return x;
2325 /* You might think handling MINUS in a manner similar to PLUS is a
2326 good idea. It is not. It has been tried multiple times and every
2327 time the change has had to have been reverted.
2329 Other parts of reload know a PLUS is special (gen_reload for example)
2330 and require special code to handle code a reloaded PLUS operand.
2332 Also consider backends where the flags register is clobbered by a
2333 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2334 lea instruction comes to mind). If we try to reload a MINUS, we
2335 may kill the flags register that was holding a useful value.
2337 So, please before trying to handle MINUS, consider reload as a
2338 whole instead of this little section as well as the backend issues. */
2339 case PLUS:
2340 /* If this is the sum of an eliminable register and a constant, rework
2341 the sum. */
2342 if (REG_P (XEXP (x, 0))
2343 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2344 && CONSTANT_P (XEXP (x, 1)))
2346 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2347 ep++)
2348 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2350 /* The only time we want to replace a PLUS with a REG (this
2351 occurs when the constant operand of the PLUS is the negative
2352 of the offset) is when we are inside a MEM. We won't want
2353 to do so at other times because that would change the
2354 structure of the insn in a way that reload can't handle.
2355 We special-case the commonest situation in
2356 eliminate_regs_in_insn, so just replace a PLUS with a
2357 PLUS here, unless inside a MEM. */
2358 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2359 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2360 return ep->to_rtx;
2361 else
2362 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2363 plus_constant (XEXP (x, 1),
2364 ep->previous_offset));
2367 /* If the register is not eliminable, we are done since the other
2368 operand is a constant. */
2369 return x;
2372 /* If this is part of an address, we want to bring any constant to the
2373 outermost PLUS. We will do this by doing register replacement in
2374 our operands and seeing if a constant shows up in one of them.
2376 Note that there is no risk of modifying the structure of the insn,
2377 since we only get called for its operands, thus we are either
2378 modifying the address inside a MEM, or something like an address
2379 operand of a load-address insn. */
2382 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2383 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2385 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2387 /* If one side is a PLUS and the other side is a pseudo that
2388 didn't get a hard register but has a reg_equiv_constant,
2389 we must replace the constant here since it may no longer
2390 be in the position of any operand. */
2391 if (GET_CODE (new0) == PLUS && REG_P (new1)
2392 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2393 && reg_renumber[REGNO (new1)] < 0
2394 && reg_equiv_constant != 0
2395 && reg_equiv_constant[REGNO (new1)] != 0)
2396 new1 = reg_equiv_constant[REGNO (new1)];
2397 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2398 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2399 && reg_renumber[REGNO (new0)] < 0
2400 && reg_equiv_constant[REGNO (new0)] != 0)
2401 new0 = reg_equiv_constant[REGNO (new0)];
2403 new = form_sum (new0, new1);
2405 /* As above, if we are not inside a MEM we do not want to
2406 turn a PLUS into something else. We might try to do so here
2407 for an addition of 0 if we aren't optimizing. */
2408 if (! mem_mode && GET_CODE (new) != PLUS)
2409 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2410 else
2411 return new;
2414 return x;
2416 case MULT:
2417 /* If this is the product of an eliminable register and a
2418 constant, apply the distribute law and move the constant out
2419 so that we have (plus (mult ..) ..). This is needed in order
2420 to keep load-address insns valid. This case is pathological.
2421 We ignore the possibility of overflow here. */
2422 if (REG_P (XEXP (x, 0))
2423 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2424 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2425 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2426 ep++)
2427 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2429 if (! mem_mode
2430 /* Refs inside notes don't count for this purpose. */
2431 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2432 || GET_CODE (insn) == INSN_LIST)))
2433 ep->ref_outside_mem = 1;
2435 return
2436 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2437 ep->previous_offset * INTVAL (XEXP (x, 1)));
2440 /* ... fall through ... */
2442 case CALL:
2443 case COMPARE:
2444 /* See comments before PLUS about handling MINUS. */
2445 case MINUS:
2446 case DIV: case UDIV:
2447 case MOD: case UMOD:
2448 case AND: case IOR: case XOR:
2449 case ROTATERT: case ROTATE:
2450 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2451 case NE: case EQ:
2452 case GE: case GT: case GEU: case GTU:
2453 case LE: case LT: case LEU: case LTU:
2455 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2456 rtx new1 = XEXP (x, 1)
2457 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false) : 0;
2459 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2460 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2462 return x;
2464 case EXPR_LIST:
2465 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2466 if (XEXP (x, 0))
2468 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2469 if (new != XEXP (x, 0))
2471 /* If this is a REG_DEAD note, it is not valid anymore.
2472 Using the eliminated version could result in creating a
2473 REG_DEAD note for the stack or frame pointer. */
2474 if (GET_MODE (x) == REG_DEAD)
2475 return (XEXP (x, 1)
2476 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true)
2477 : NULL_RTX);
2479 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2483 /* ... fall through ... */
2485 case INSN_LIST:
2486 /* Now do eliminations in the rest of the chain. If this was
2487 an EXPR_LIST, this might result in allocating more memory than is
2488 strictly needed, but it simplifies the code. */
2489 if (XEXP (x, 1))
2491 new = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2492 if (new != XEXP (x, 1))
2493 return
2494 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2496 return x;
2498 case PRE_INC:
2499 case POST_INC:
2500 case PRE_DEC:
2501 case POST_DEC:
2502 case STRICT_LOW_PART:
2503 case NEG: case NOT:
2504 case SIGN_EXTEND: case ZERO_EXTEND:
2505 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2506 case FLOAT: case FIX:
2507 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2508 case ABS:
2509 case SQRT:
2510 case FFS:
2511 case CLZ:
2512 case CTZ:
2513 case POPCOUNT:
2514 case PARITY:
2515 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2516 if (new != XEXP (x, 0))
2517 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2518 return x;
2520 case SUBREG:
2521 /* Similar to above processing, but preserve SUBREG_BYTE.
2522 Convert (subreg (mem)) to (mem) if not paradoxical.
2523 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2524 pseudo didn't get a hard reg, we must replace this with the
2525 eliminated version of the memory location because push_reload
2526 may do the replacement in certain circumstances. */
2527 if (REG_P (SUBREG_REG (x))
2528 && (GET_MODE_SIZE (GET_MODE (x))
2529 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2530 && reg_equiv_memory_loc != 0
2531 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2533 new = SUBREG_REG (x);
2535 else
2536 new = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false);
2538 if (new != SUBREG_REG (x))
2540 int x_size = GET_MODE_SIZE (GET_MODE (x));
2541 int new_size = GET_MODE_SIZE (GET_MODE (new));
2543 if (MEM_P (new)
2544 && ((x_size < new_size
2545 #ifdef WORD_REGISTER_OPERATIONS
2546 /* On these machines, combine can create rtl of the form
2547 (set (subreg:m1 (reg:m2 R) 0) ...)
2548 where m1 < m2, and expects something interesting to
2549 happen to the entire word. Moreover, it will use the
2550 (reg:m2 R) later, expecting all bits to be preserved.
2551 So if the number of words is the same, preserve the
2552 subreg so that push_reload can see it. */
2553 && ! ((x_size - 1) / UNITS_PER_WORD
2554 == (new_size -1 ) / UNITS_PER_WORD)
2555 #endif
2557 || x_size == new_size)
2559 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2560 else
2561 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2564 return x;
2566 case MEM:
2567 /* Our only special processing is to pass the mode of the MEM to our
2568 recursive call and copy the flags. While we are here, handle this
2569 case more efficiently. */
2570 return
2571 replace_equiv_address_nv (x,
2572 eliminate_regs_1 (XEXP (x, 0), GET_MODE (x),
2573 insn, true));
2575 case USE:
2576 /* Handle insn_list USE that a call to a pure function may generate. */
2577 new = eliminate_regs_1 (XEXP (x, 0), 0, insn, false);
2578 if (new != XEXP (x, 0))
2579 return gen_rtx_USE (GET_MODE (x), new);
2580 return x;
2582 case CLOBBER:
2583 case ASM_OPERANDS:
2584 case SET:
2585 gcc_unreachable ();
2587 default:
2588 break;
2591 /* Process each of our operands recursively. If any have changed, make a
2592 copy of the rtx. */
2593 fmt = GET_RTX_FORMAT (code);
2594 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2596 if (*fmt == 'e')
2598 new = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false);
2599 if (new != XEXP (x, i) && ! copied)
2601 rtx new_x = rtx_alloc (code);
2602 memcpy (new_x, x, RTX_SIZE (code));
2603 x = new_x;
2604 copied = 1;
2606 XEXP (x, i) = new;
2608 else if (*fmt == 'E')
2610 int copied_vec = 0;
2611 for (j = 0; j < XVECLEN (x, i); j++)
2613 new = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false);
2614 if (new != XVECEXP (x, i, j) && ! copied_vec)
2616 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2617 XVEC (x, i)->elem);
2618 if (! copied)
2620 rtx new_x = rtx_alloc (code);
2621 memcpy (new_x, x, RTX_SIZE (code));
2622 x = new_x;
2623 copied = 1;
2625 XVEC (x, i) = new_v;
2626 copied_vec = 1;
2628 XVECEXP (x, i, j) = new;
2633 return x;
2637 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2639 return eliminate_regs_1 (x, mem_mode, insn, false);
2642 /* Scan rtx X for modifications of elimination target registers. Update
2643 the table of eliminables to reflect the changed state. MEM_MODE is
2644 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2646 static void
2647 elimination_effects (rtx x, enum machine_mode mem_mode)
2649 enum rtx_code code = GET_CODE (x);
2650 struct elim_table *ep;
2651 int regno;
2652 int i, j;
2653 const char *fmt;
2655 switch (code)
2657 case CONST_INT:
2658 case CONST_DOUBLE:
2659 case CONST_VECTOR:
2660 case CONST:
2661 case SYMBOL_REF:
2662 case CODE_LABEL:
2663 case PC:
2664 case CC0:
2665 case ASM_INPUT:
2666 case ADDR_VEC:
2667 case ADDR_DIFF_VEC:
2668 case RETURN:
2669 return;
2671 case REG:
2672 regno = REGNO (x);
2674 /* First handle the case where we encounter a bare register that
2675 is eliminable. Replace it with a PLUS. */
2676 if (regno < FIRST_PSEUDO_REGISTER)
2678 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2679 ep++)
2680 if (ep->from_rtx == x && ep->can_eliminate)
2682 if (! mem_mode)
2683 ep->ref_outside_mem = 1;
2684 return;
2688 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2689 && reg_equiv_constant[regno]
2690 && ! function_invariant_p (reg_equiv_constant[regno]))
2691 elimination_effects (reg_equiv_constant[regno], mem_mode);
2692 return;
2694 case PRE_INC:
2695 case POST_INC:
2696 case PRE_DEC:
2697 case POST_DEC:
2698 case POST_MODIFY:
2699 case PRE_MODIFY:
2700 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2701 if (ep->to_rtx == XEXP (x, 0))
2703 int size = GET_MODE_SIZE (mem_mode);
2705 /* If more bytes than MEM_MODE are pushed, account for them. */
2706 #ifdef PUSH_ROUNDING
2707 if (ep->to_rtx == stack_pointer_rtx)
2708 size = PUSH_ROUNDING (size);
2709 #endif
2710 if (code == PRE_DEC || code == POST_DEC)
2711 ep->offset += size;
2712 else if (code == PRE_INC || code == POST_INC)
2713 ep->offset -= size;
2714 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2715 && GET_CODE (XEXP (x, 1)) == PLUS
2716 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2717 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2718 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2721 /* These two aren't unary operators. */
2722 if (code == POST_MODIFY || code == PRE_MODIFY)
2723 break;
2725 /* Fall through to generic unary operation case. */
2726 case STRICT_LOW_PART:
2727 case NEG: case NOT:
2728 case SIGN_EXTEND: case ZERO_EXTEND:
2729 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2730 case FLOAT: case FIX:
2731 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2732 case ABS:
2733 case SQRT:
2734 case FFS:
2735 case CLZ:
2736 case CTZ:
2737 case POPCOUNT:
2738 case PARITY:
2739 elimination_effects (XEXP (x, 0), mem_mode);
2740 return;
2742 case SUBREG:
2743 if (REG_P (SUBREG_REG (x))
2744 && (GET_MODE_SIZE (GET_MODE (x))
2745 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2746 && reg_equiv_memory_loc != 0
2747 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2748 return;
2750 elimination_effects (SUBREG_REG (x), mem_mode);
2751 return;
2753 case USE:
2754 /* If using a register that is the source of an eliminate we still
2755 think can be performed, note it cannot be performed since we don't
2756 know how this register is used. */
2757 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2758 if (ep->from_rtx == XEXP (x, 0))
2759 ep->can_eliminate = 0;
2761 elimination_effects (XEXP (x, 0), mem_mode);
2762 return;
2764 case CLOBBER:
2765 /* If clobbering a register that is the replacement register for an
2766 elimination we still think can be performed, note that it cannot
2767 be performed. Otherwise, we need not be concerned about it. */
2768 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2769 if (ep->to_rtx == XEXP (x, 0))
2770 ep->can_eliminate = 0;
2772 elimination_effects (XEXP (x, 0), mem_mode);
2773 return;
2775 case SET:
2776 /* Check for setting a register that we know about. */
2777 if (REG_P (SET_DEST (x)))
2779 /* See if this is setting the replacement register for an
2780 elimination.
2782 If DEST is the hard frame pointer, we do nothing because we
2783 assume that all assignments to the frame pointer are for
2784 non-local gotos and are being done at a time when they are valid
2785 and do not disturb anything else. Some machines want to
2786 eliminate a fake argument pointer (or even a fake frame pointer)
2787 with either the real frame or the stack pointer. Assignments to
2788 the hard frame pointer must not prevent this elimination. */
2790 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2791 ep++)
2792 if (ep->to_rtx == SET_DEST (x)
2793 && SET_DEST (x) != hard_frame_pointer_rtx)
2795 /* If it is being incremented, adjust the offset. Otherwise,
2796 this elimination can't be done. */
2797 rtx src = SET_SRC (x);
2799 if (GET_CODE (src) == PLUS
2800 && XEXP (src, 0) == SET_DEST (x)
2801 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2802 ep->offset -= INTVAL (XEXP (src, 1));
2803 else
2804 ep->can_eliminate = 0;
2808 elimination_effects (SET_DEST (x), 0);
2809 elimination_effects (SET_SRC (x), 0);
2810 return;
2812 case MEM:
2813 /* Our only special processing is to pass the mode of the MEM to our
2814 recursive call. */
2815 elimination_effects (XEXP (x, 0), GET_MODE (x));
2816 return;
2818 default:
2819 break;
2822 fmt = GET_RTX_FORMAT (code);
2823 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2825 if (*fmt == 'e')
2826 elimination_effects (XEXP (x, i), mem_mode);
2827 else if (*fmt == 'E')
2828 for (j = 0; j < XVECLEN (x, i); j++)
2829 elimination_effects (XVECEXP (x, i, j), mem_mode);
2833 /* Descend through rtx X and verify that no references to eliminable registers
2834 remain. If any do remain, mark the involved register as not
2835 eliminable. */
2837 static void
2838 check_eliminable_occurrences (rtx x)
2840 const char *fmt;
2841 int i;
2842 enum rtx_code code;
2844 if (x == 0)
2845 return;
2847 code = GET_CODE (x);
2849 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2851 struct elim_table *ep;
2853 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2854 if (ep->from_rtx == x)
2855 ep->can_eliminate = 0;
2856 return;
2859 fmt = GET_RTX_FORMAT (code);
2860 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2862 if (*fmt == 'e')
2863 check_eliminable_occurrences (XEXP (x, i));
2864 else if (*fmt == 'E')
2866 int j;
2867 for (j = 0; j < XVECLEN (x, i); j++)
2868 check_eliminable_occurrences (XVECEXP (x, i, j));
2873 /* Scan INSN and eliminate all eliminable registers in it.
2875 If REPLACE is nonzero, do the replacement destructively. Also
2876 delete the insn as dead it if it is setting an eliminable register.
2878 If REPLACE is zero, do all our allocations in reload_obstack.
2880 If no eliminations were done and this insn doesn't require any elimination
2881 processing (these are not identical conditions: it might be updating sp,
2882 but not referencing fp; this needs to be seen during reload_as_needed so
2883 that the offset between fp and sp can be taken into consideration), zero
2884 is returned. Otherwise, 1 is returned. */
2886 static int
2887 eliminate_regs_in_insn (rtx insn, int replace)
2889 int icode = recog_memoized (insn);
2890 rtx old_body = PATTERN (insn);
2891 int insn_is_asm = asm_noperands (old_body) >= 0;
2892 rtx old_set = single_set (insn);
2893 rtx new_body;
2894 int val = 0;
2895 int i;
2896 rtx substed_operand[MAX_RECOG_OPERANDS];
2897 rtx orig_operand[MAX_RECOG_OPERANDS];
2898 struct elim_table *ep;
2899 rtx plus_src, plus_cst_src;
2901 if (! insn_is_asm && icode < 0)
2903 gcc_assert (GET_CODE (PATTERN (insn)) == USE
2904 || GET_CODE (PATTERN (insn)) == CLOBBER
2905 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2906 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2907 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
2908 return 0;
2911 if (old_set != 0 && REG_P (SET_DEST (old_set))
2912 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2914 /* Check for setting an eliminable register. */
2915 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2916 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2918 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2919 /* If this is setting the frame pointer register to the
2920 hardware frame pointer register and this is an elimination
2921 that will be done (tested above), this insn is really
2922 adjusting the frame pointer downward to compensate for
2923 the adjustment done before a nonlocal goto. */
2924 if (ep->from == FRAME_POINTER_REGNUM
2925 && ep->to == HARD_FRAME_POINTER_REGNUM)
2927 rtx base = SET_SRC (old_set);
2928 rtx base_insn = insn;
2929 HOST_WIDE_INT offset = 0;
2931 while (base != ep->to_rtx)
2933 rtx prev_insn, prev_set;
2935 if (GET_CODE (base) == PLUS
2936 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2938 offset += INTVAL (XEXP (base, 1));
2939 base = XEXP (base, 0);
2941 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2942 && (prev_set = single_set (prev_insn)) != 0
2943 && rtx_equal_p (SET_DEST (prev_set), base))
2945 base = SET_SRC (prev_set);
2946 base_insn = prev_insn;
2948 else
2949 break;
2952 if (base == ep->to_rtx)
2954 rtx src
2955 = plus_constant (ep->to_rtx, offset - ep->offset);
2957 new_body = old_body;
2958 if (! replace)
2960 new_body = copy_insn (old_body);
2961 if (REG_NOTES (insn))
2962 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
2964 PATTERN (insn) = new_body;
2965 old_set = single_set (insn);
2967 /* First see if this insn remains valid when we
2968 make the change. If not, keep the INSN_CODE
2969 the same and let reload fit it up. */
2970 validate_change (insn, &SET_SRC (old_set), src, 1);
2971 validate_change (insn, &SET_DEST (old_set),
2972 ep->to_rtx, 1);
2973 if (! apply_change_group ())
2975 SET_SRC (old_set) = src;
2976 SET_DEST (old_set) = ep->to_rtx;
2979 val = 1;
2980 goto done;
2983 #endif
2985 /* In this case this insn isn't serving a useful purpose. We
2986 will delete it in reload_as_needed once we know that this
2987 elimination is, in fact, being done.
2989 If REPLACE isn't set, we can't delete this insn, but needn't
2990 process it since it won't be used unless something changes. */
2991 if (replace)
2993 delete_dead_insn (insn);
2994 return 1;
2996 val = 1;
2997 goto done;
3001 /* We allow one special case which happens to work on all machines we
3002 currently support: a single set with the source or a REG_EQUAL
3003 note being a PLUS of an eliminable register and a constant. */
3004 plus_src = plus_cst_src = 0;
3005 if (old_set && REG_P (SET_DEST (old_set)))
3007 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3008 plus_src = SET_SRC (old_set);
3009 /* First see if the source is of the form (plus (reg) CST). */
3010 if (plus_src
3011 && REG_P (XEXP (plus_src, 0))
3012 && GET_CODE (XEXP (plus_src, 1)) == CONST_INT
3013 && REGNO (XEXP (plus_src, 0)) < FIRST_PSEUDO_REGISTER)
3014 plus_cst_src = plus_src;
3015 else if (REG_P (SET_SRC (old_set))
3016 || plus_src)
3018 /* Otherwise, see if we have a REG_EQUAL note of the form
3019 (plus (reg) CST). */
3020 rtx links;
3021 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3023 if (REG_NOTE_KIND (links) == REG_EQUAL
3024 && GET_CODE (XEXP (links, 0)) == PLUS
3025 && REG_P (XEXP (XEXP (links, 0), 0))
3026 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT
3027 && REGNO (XEXP (XEXP (links, 0), 0)) < FIRST_PSEUDO_REGISTER)
3029 plus_cst_src = XEXP (links, 0);
3030 break;
3035 if (plus_cst_src)
3037 rtx reg = XEXP (plus_cst_src, 0);
3038 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3040 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3041 if (ep->from_rtx == reg && ep->can_eliminate)
3043 offset += ep->offset;
3045 if (offset == 0)
3047 int num_clobbers;
3048 /* We assume here that if we need a PARALLEL with
3049 CLOBBERs for this assignment, we can do with the
3050 MATCH_SCRATCHes that add_clobbers allocates.
3051 There's not much we can do if that doesn't work. */
3052 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3053 SET_DEST (old_set),
3054 ep->to_rtx);
3055 num_clobbers = 0;
3056 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3057 if (num_clobbers)
3059 rtvec vec = rtvec_alloc (num_clobbers + 1);
3061 vec->elem[0] = PATTERN (insn);
3062 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3063 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3065 gcc_assert (INSN_CODE (insn) >= 0);
3067 /* If we have a nonzero offset, and the source is already
3068 a simple REG, the following transformation would
3069 increase the cost of the insn by replacing a simple REG
3070 with (plus (reg sp) CST). So try only when we already
3071 had a PLUS before. */
3072 else if (plus_src)
3074 new_body = old_body;
3075 if (! replace)
3077 new_body = copy_insn (old_body);
3078 if (REG_NOTES (insn))
3079 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3081 PATTERN (insn) = new_body;
3082 old_set = single_set (insn);
3084 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3085 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3087 else
3088 break;
3090 val = 1;
3091 /* This can't have an effect on elimination offsets, so skip right
3092 to the end. */
3093 goto done;
3097 /* Determine the effects of this insn on elimination offsets. */
3098 elimination_effects (old_body, 0);
3100 /* Eliminate all eliminable registers occurring in operands that
3101 can be handled by reload. */
3102 extract_insn (insn);
3103 for (i = 0; i < recog_data.n_operands; i++)
3105 orig_operand[i] = recog_data.operand[i];
3106 substed_operand[i] = recog_data.operand[i];
3108 /* For an asm statement, every operand is eliminable. */
3109 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3111 bool is_set_src, in_plus;
3113 /* Check for setting a register that we know about. */
3114 if (recog_data.operand_type[i] != OP_IN
3115 && REG_P (orig_operand[i]))
3117 /* If we are assigning to a register that can be eliminated, it
3118 must be as part of a PARALLEL, since the code above handles
3119 single SETs. We must indicate that we can no longer
3120 eliminate this reg. */
3121 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3122 ep++)
3123 if (ep->from_rtx == orig_operand[i])
3124 ep->can_eliminate = 0;
3127 /* Companion to the above plus substitution, we can allow
3128 invariants as the source of a plain move. */
3129 is_set_src = false;
3130 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3131 is_set_src = true;
3132 in_plus = false;
3133 if (plus_src
3134 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3135 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3136 in_plus = true;
3138 substed_operand[i]
3139 = eliminate_regs_1 (recog_data.operand[i], 0,
3140 replace ? insn : NULL_RTX,
3141 is_set_src || in_plus);
3142 if (substed_operand[i] != orig_operand[i])
3143 val = 1;
3144 /* Terminate the search in check_eliminable_occurrences at
3145 this point. */
3146 *recog_data.operand_loc[i] = 0;
3148 /* If an output operand changed from a REG to a MEM and INSN is an
3149 insn, write a CLOBBER insn. */
3150 if (recog_data.operand_type[i] != OP_IN
3151 && REG_P (orig_operand[i])
3152 && MEM_P (substed_operand[i])
3153 && replace)
3154 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3155 insn);
3159 for (i = 0; i < recog_data.n_dups; i++)
3160 *recog_data.dup_loc[i]
3161 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3163 /* If any eliminable remain, they aren't eliminable anymore. */
3164 check_eliminable_occurrences (old_body);
3166 /* Substitute the operands; the new values are in the substed_operand
3167 array. */
3168 for (i = 0; i < recog_data.n_operands; i++)
3169 *recog_data.operand_loc[i] = substed_operand[i];
3170 for (i = 0; i < recog_data.n_dups; i++)
3171 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3173 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3174 re-recognize the insn. We do this in case we had a simple addition
3175 but now can do this as a load-address. This saves an insn in this
3176 common case.
3177 If re-recognition fails, the old insn code number will still be used,
3178 and some register operands may have changed into PLUS expressions.
3179 These will be handled by find_reloads by loading them into a register
3180 again. */
3182 if (val)
3184 /* If we aren't replacing things permanently and we changed something,
3185 make another copy to ensure that all the RTL is new. Otherwise
3186 things can go wrong if find_reload swaps commutative operands
3187 and one is inside RTL that has been copied while the other is not. */
3188 new_body = old_body;
3189 if (! replace)
3191 new_body = copy_insn (old_body);
3192 if (REG_NOTES (insn))
3193 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3195 PATTERN (insn) = new_body;
3197 /* If we had a move insn but now we don't, rerecognize it. This will
3198 cause spurious re-recognition if the old move had a PARALLEL since
3199 the new one still will, but we can't call single_set without
3200 having put NEW_BODY into the insn and the re-recognition won't
3201 hurt in this rare case. */
3202 /* ??? Why this huge if statement - why don't we just rerecognize the
3203 thing always? */
3204 if (! insn_is_asm
3205 && old_set != 0
3206 && ((REG_P (SET_SRC (old_set))
3207 && (GET_CODE (new_body) != SET
3208 || !REG_P (SET_SRC (new_body))))
3209 /* If this was a load from or store to memory, compare
3210 the MEM in recog_data.operand to the one in the insn.
3211 If they are not equal, then rerecognize the insn. */
3212 || (old_set != 0
3213 && ((MEM_P (SET_SRC (old_set))
3214 && SET_SRC (old_set) != recog_data.operand[1])
3215 || (MEM_P (SET_DEST (old_set))
3216 && SET_DEST (old_set) != recog_data.operand[0])))
3217 /* If this was an add insn before, rerecognize. */
3218 || GET_CODE (SET_SRC (old_set)) == PLUS))
3220 int new_icode = recog (PATTERN (insn), insn, 0);
3221 if (new_icode < 0)
3222 INSN_CODE (insn) = icode;
3226 /* Restore the old body. If there were any changes to it, we made a copy
3227 of it while the changes were still in place, so we'll correctly return
3228 a modified insn below. */
3229 if (! replace)
3231 /* Restore the old body. */
3232 for (i = 0; i < recog_data.n_operands; i++)
3233 *recog_data.operand_loc[i] = orig_operand[i];
3234 for (i = 0; i < recog_data.n_dups; i++)
3235 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3238 /* Update all elimination pairs to reflect the status after the current
3239 insn. The changes we make were determined by the earlier call to
3240 elimination_effects.
3242 We also detect cases where register elimination cannot be done,
3243 namely, if a register would be both changed and referenced outside a MEM
3244 in the resulting insn since such an insn is often undefined and, even if
3245 not, we cannot know what meaning will be given to it. Note that it is
3246 valid to have a register used in an address in an insn that changes it
3247 (presumably with a pre- or post-increment or decrement).
3249 If anything changes, return nonzero. */
3251 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3253 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3254 ep->can_eliminate = 0;
3256 ep->ref_outside_mem = 0;
3258 if (ep->previous_offset != ep->offset)
3259 val = 1;
3262 done:
3263 /* If we changed something, perform elimination in REG_NOTES. This is
3264 needed even when REPLACE is zero because a REG_DEAD note might refer
3265 to a register that we eliminate and could cause a different number
3266 of spill registers to be needed in the final reload pass than in
3267 the pre-passes. */
3268 if (val && REG_NOTES (insn) != 0)
3269 REG_NOTES (insn)
3270 = eliminate_regs_1 (REG_NOTES (insn), 0, REG_NOTES (insn), true);
3272 return val;
3275 /* Loop through all elimination pairs.
3276 Recalculate the number not at initial offset.
3278 Compute the maximum offset (minimum offset if the stack does not
3279 grow downward) for each elimination pair. */
3281 static void
3282 update_eliminable_offsets (void)
3284 struct elim_table *ep;
3286 num_not_at_initial_offset = 0;
3287 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3289 ep->previous_offset = ep->offset;
3290 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3291 num_not_at_initial_offset++;
3295 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3296 replacement we currently believe is valid, mark it as not eliminable if X
3297 modifies DEST in any way other than by adding a constant integer to it.
3299 If DEST is the frame pointer, we do nothing because we assume that
3300 all assignments to the hard frame pointer are nonlocal gotos and are being
3301 done at a time when they are valid and do not disturb anything else.
3302 Some machines want to eliminate a fake argument pointer with either the
3303 frame or stack pointer. Assignments to the hard frame pointer must not
3304 prevent this elimination.
3306 Called via note_stores from reload before starting its passes to scan
3307 the insns of the function. */
3309 static void
3310 mark_not_eliminable (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
3312 unsigned int i;
3314 /* A SUBREG of a hard register here is just changing its mode. We should
3315 not see a SUBREG of an eliminable hard register, but check just in
3316 case. */
3317 if (GET_CODE (dest) == SUBREG)
3318 dest = SUBREG_REG (dest);
3320 if (dest == hard_frame_pointer_rtx)
3321 return;
3323 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3324 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3325 && (GET_CODE (x) != SET
3326 || GET_CODE (SET_SRC (x)) != PLUS
3327 || XEXP (SET_SRC (x), 0) != dest
3328 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3330 reg_eliminate[i].can_eliminate_previous
3331 = reg_eliminate[i].can_eliminate = 0;
3332 num_eliminable--;
3336 /* Verify that the initial elimination offsets did not change since the
3337 last call to set_initial_elim_offsets. This is used to catch cases
3338 where something illegal happened during reload_as_needed that could
3339 cause incorrect code to be generated if we did not check for it. */
3341 static bool
3342 verify_initial_elim_offsets (void)
3344 HOST_WIDE_INT t;
3346 if (!num_eliminable)
3347 return true;
3349 #ifdef ELIMINABLE_REGS
3351 struct elim_table *ep;
3353 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3355 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3356 if (t != ep->initial_offset)
3357 return false;
3360 #else
3361 INITIAL_FRAME_POINTER_OFFSET (t);
3362 if (t != reg_eliminate[0].initial_offset)
3363 return false;
3364 #endif
3366 return true;
3369 /* Reset all offsets on eliminable registers to their initial values. */
3371 static void
3372 set_initial_elim_offsets (void)
3374 struct elim_table *ep = reg_eliminate;
3376 #ifdef ELIMINABLE_REGS
3377 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3379 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3380 ep->previous_offset = ep->offset = ep->initial_offset;
3382 #else
3383 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3384 ep->previous_offset = ep->offset = ep->initial_offset;
3385 #endif
3387 num_not_at_initial_offset = 0;
3390 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3392 static void
3393 set_initial_eh_label_offset (rtx label)
3395 set_label_offsets (label, NULL_RTX, 1);
3398 /* Initialize the known label offsets.
3399 Set a known offset for each forced label to be at the initial offset
3400 of each elimination. We do this because we assume that all
3401 computed jumps occur from a location where each elimination is
3402 at its initial offset.
3403 For all other labels, show that we don't know the offsets. */
3405 static void
3406 set_initial_label_offsets (void)
3408 rtx x;
3409 memset (offsets_known_at, 0, num_labels);
3411 for (x = forced_labels; x; x = XEXP (x, 1))
3412 if (XEXP (x, 0))
3413 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3415 for_each_eh_label (set_initial_eh_label_offset);
3418 /* Set all elimination offsets to the known values for the code label given
3419 by INSN. */
3421 static void
3422 set_offsets_for_label (rtx insn)
3424 unsigned int i;
3425 int label_nr = CODE_LABEL_NUMBER (insn);
3426 struct elim_table *ep;
3428 num_not_at_initial_offset = 0;
3429 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3431 ep->offset = ep->previous_offset
3432 = offsets_at[label_nr - first_label_num][i];
3433 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3434 num_not_at_initial_offset++;
3438 /* See if anything that happened changes which eliminations are valid.
3439 For example, on the SPARC, whether or not the frame pointer can
3440 be eliminated can depend on what registers have been used. We need
3441 not check some conditions again (such as flag_omit_frame_pointer)
3442 since they can't have changed. */
3444 static void
3445 update_eliminables (HARD_REG_SET *pset)
3447 int previous_frame_pointer_needed = frame_pointer_needed;
3448 struct elim_table *ep;
3450 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3451 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3452 #ifdef ELIMINABLE_REGS
3453 || ! CAN_ELIMINATE (ep->from, ep->to)
3454 #endif
3456 ep->can_eliminate = 0;
3458 /* Look for the case where we have discovered that we can't replace
3459 register A with register B and that means that we will now be
3460 trying to replace register A with register C. This means we can
3461 no longer replace register C with register B and we need to disable
3462 such an elimination, if it exists. This occurs often with A == ap,
3463 B == sp, and C == fp. */
3465 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3467 struct elim_table *op;
3468 int new_to = -1;
3470 if (! ep->can_eliminate && ep->can_eliminate_previous)
3472 /* Find the current elimination for ep->from, if there is a
3473 new one. */
3474 for (op = reg_eliminate;
3475 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3476 if (op->from == ep->from && op->can_eliminate)
3478 new_to = op->to;
3479 break;
3482 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3483 disable it. */
3484 for (op = reg_eliminate;
3485 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3486 if (op->from == new_to && op->to == ep->to)
3487 op->can_eliminate = 0;
3491 /* See if any registers that we thought we could eliminate the previous
3492 time are no longer eliminable. If so, something has changed and we
3493 must spill the register. Also, recompute the number of eliminable
3494 registers and see if the frame pointer is needed; it is if there is
3495 no elimination of the frame pointer that we can perform. */
3497 frame_pointer_needed = 1;
3498 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3500 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3501 && ep->to != HARD_FRAME_POINTER_REGNUM)
3502 frame_pointer_needed = 0;
3504 if (! ep->can_eliminate && ep->can_eliminate_previous)
3506 ep->can_eliminate_previous = 0;
3507 SET_HARD_REG_BIT (*pset, ep->from);
3508 num_eliminable--;
3512 /* If we didn't need a frame pointer last time, but we do now, spill
3513 the hard frame pointer. */
3514 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3515 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3518 /* Initialize the table of registers to eliminate. */
3520 static void
3521 init_elim_table (void)
3523 struct elim_table *ep;
3524 #ifdef ELIMINABLE_REGS
3525 const struct elim_table_1 *ep1;
3526 #endif
3528 if (!reg_eliminate)
3529 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3531 /* Does this function require a frame pointer? */
3533 frame_pointer_needed = (! flag_omit_frame_pointer
3534 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3535 and restore sp for alloca. So we can't eliminate
3536 the frame pointer in that case. At some point,
3537 we should improve this by emitting the
3538 sp-adjusting insns for this case. */
3539 || (current_function_calls_alloca
3540 && EXIT_IGNORE_STACK)
3541 || current_function_accesses_prior_frames
3542 || FRAME_POINTER_REQUIRED);
3544 num_eliminable = 0;
3546 #ifdef ELIMINABLE_REGS
3547 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3548 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3550 ep->from = ep1->from;
3551 ep->to = ep1->to;
3552 ep->can_eliminate = ep->can_eliminate_previous
3553 = (CAN_ELIMINATE (ep->from, ep->to)
3554 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3556 #else
3557 reg_eliminate[0].from = reg_eliminate_1[0].from;
3558 reg_eliminate[0].to = reg_eliminate_1[0].to;
3559 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3560 = ! frame_pointer_needed;
3561 #endif
3563 /* Count the number of eliminable registers and build the FROM and TO
3564 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3565 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3566 We depend on this. */
3567 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3569 num_eliminable += ep->can_eliminate;
3570 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3571 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3575 /* Kick all pseudos out of hard register REGNO.
3577 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3578 because we found we can't eliminate some register. In the case, no pseudos
3579 are allowed to be in the register, even if they are only in a block that
3580 doesn't require spill registers, unlike the case when we are spilling this
3581 hard reg to produce another spill register.
3583 Return nonzero if any pseudos needed to be kicked out. */
3585 static void
3586 spill_hard_reg (unsigned int regno, int cant_eliminate)
3588 int i;
3590 if (cant_eliminate)
3592 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3593 regs_ever_live[regno] = 1;
3596 /* Spill every pseudo reg that was allocated to this reg
3597 or to something that overlaps this reg. */
3599 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3600 if (reg_renumber[i] >= 0
3601 && (unsigned int) reg_renumber[i] <= regno
3602 && ((unsigned int) reg_renumber[i]
3603 + hard_regno_nregs[(unsigned int) reg_renumber[i]]
3604 [PSEUDO_REGNO_MODE (i)]
3605 > regno))
3606 SET_REGNO_REG_SET (&spilled_pseudos, i);
3609 /* After find_reload_regs has been run for all insn that need reloads,
3610 and/or spill_hard_regs was called, this function is used to actually
3611 spill pseudo registers and try to reallocate them. It also sets up the
3612 spill_regs array for use by choose_reload_regs. */
3614 static int
3615 finish_spills (int global)
3617 struct insn_chain *chain;
3618 int something_changed = 0;
3619 unsigned i;
3620 reg_set_iterator rsi;
3622 /* Build the spill_regs array for the function. */
3623 /* If there are some registers still to eliminate and one of the spill regs
3624 wasn't ever used before, additional stack space may have to be
3625 allocated to store this register. Thus, we may have changed the offset
3626 between the stack and frame pointers, so mark that something has changed.
3628 One might think that we need only set VAL to 1 if this is a call-used
3629 register. However, the set of registers that must be saved by the
3630 prologue is not identical to the call-used set. For example, the
3631 register used by the call insn for the return PC is a call-used register,
3632 but must be saved by the prologue. */
3634 n_spills = 0;
3635 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3636 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3638 spill_reg_order[i] = n_spills;
3639 spill_regs[n_spills++] = i;
3640 if (num_eliminable && ! regs_ever_live[i])
3641 something_changed = 1;
3642 regs_ever_live[i] = 1;
3644 else
3645 spill_reg_order[i] = -1;
3647 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
3649 /* Record the current hard register the pseudo is allocated to in
3650 pseudo_previous_regs so we avoid reallocating it to the same
3651 hard reg in a later pass. */
3652 gcc_assert (reg_renumber[i] >= 0);
3654 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3655 /* Mark it as no longer having a hard register home. */
3656 reg_renumber[i] = -1;
3657 /* We will need to scan everything again. */
3658 something_changed = 1;
3661 /* Retry global register allocation if possible. */
3662 if (global)
3664 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3665 /* For every insn that needs reloads, set the registers used as spill
3666 regs in pseudo_forbidden_regs for every pseudo live across the
3667 insn. */
3668 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3670 EXECUTE_IF_SET_IN_REG_SET
3671 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
3673 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3674 chain->used_spill_regs);
3676 EXECUTE_IF_SET_IN_REG_SET
3677 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
3679 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3680 chain->used_spill_regs);
3684 /* Retry allocating the spilled pseudos. For each reg, merge the
3685 various reg sets that indicate which hard regs can't be used,
3686 and call retry_global_alloc.
3687 We change spill_pseudos here to only contain pseudos that did not
3688 get a new hard register. */
3689 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3690 if (reg_old_renumber[i] != reg_renumber[i])
3692 HARD_REG_SET forbidden;
3693 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3694 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3695 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3696 retry_global_alloc (i, forbidden);
3697 if (reg_renumber[i] >= 0)
3698 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3702 /* Fix up the register information in the insn chain.
3703 This involves deleting those of the spilled pseudos which did not get
3704 a new hard register home from the live_{before,after} sets. */
3705 for (chain = reload_insn_chain; chain; chain = chain->next)
3707 HARD_REG_SET used_by_pseudos;
3708 HARD_REG_SET used_by_pseudos2;
3710 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3711 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3713 /* Mark any unallocated hard regs as available for spills. That
3714 makes inheritance work somewhat better. */
3715 if (chain->need_reload)
3717 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3718 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3719 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3721 /* Save the old value for the sanity test below. */
3722 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3724 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3725 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3726 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3727 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3729 /* Make sure we only enlarge the set. */
3730 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3731 gcc_unreachable ();
3732 ok:;
3736 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3737 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3739 int regno = reg_renumber[i];
3740 if (reg_old_renumber[i] == regno)
3741 continue;
3743 alter_reg (i, reg_old_renumber[i]);
3744 reg_old_renumber[i] = regno;
3745 if (dump_file)
3747 if (regno == -1)
3748 fprintf (dump_file, " Register %d now on stack.\n\n", i);
3749 else
3750 fprintf (dump_file, " Register %d now in %d.\n\n",
3751 i, reg_renumber[i]);
3755 return something_changed;
3758 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
3760 static void
3761 scan_paradoxical_subregs (rtx x)
3763 int i;
3764 const char *fmt;
3765 enum rtx_code code = GET_CODE (x);
3767 switch (code)
3769 case REG:
3770 case CONST_INT:
3771 case CONST:
3772 case SYMBOL_REF:
3773 case LABEL_REF:
3774 case CONST_DOUBLE:
3775 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3776 case CC0:
3777 case PC:
3778 case USE:
3779 case CLOBBER:
3780 return;
3782 case SUBREG:
3783 if (REG_P (SUBREG_REG (x))
3784 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3785 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3786 = GET_MODE_SIZE (GET_MODE (x));
3787 return;
3789 default:
3790 break;
3793 fmt = GET_RTX_FORMAT (code);
3794 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3796 if (fmt[i] == 'e')
3797 scan_paradoxical_subregs (XEXP (x, i));
3798 else if (fmt[i] == 'E')
3800 int j;
3801 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3802 scan_paradoxical_subregs (XVECEXP (x, i, j));
3807 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
3808 examine all of the reload insns between PREV and NEXT exclusive, and
3809 annotate all that may trap. */
3811 static void
3812 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
3814 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3815 unsigned int trap_count;
3816 rtx i;
3818 if (note == NULL)
3819 return;
3821 if (may_trap_p (PATTERN (insn)))
3822 trap_count = 1;
3823 else
3825 remove_note (insn, note);
3826 trap_count = 0;
3829 for (i = NEXT_INSN (prev); i != next; i = NEXT_INSN (i))
3830 if (INSN_P (i) && i != insn && may_trap_p (PATTERN (i)))
3832 trap_count++;
3833 REG_NOTES (i)
3834 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (note, 0), REG_NOTES (i));
3838 /* Reload pseudo-registers into hard regs around each insn as needed.
3839 Additional register load insns are output before the insn that needs it
3840 and perhaps store insns after insns that modify the reloaded pseudo reg.
3842 reg_last_reload_reg and reg_reloaded_contents keep track of
3843 which registers are already available in reload registers.
3844 We update these for the reloads that we perform,
3845 as the insns are scanned. */
3847 static void
3848 reload_as_needed (int live_known)
3850 struct insn_chain *chain;
3851 #if defined (AUTO_INC_DEC)
3852 int i;
3853 #endif
3854 rtx x;
3856 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
3857 memset (spill_reg_store, 0, sizeof spill_reg_store);
3858 reg_last_reload_reg = xcalloc (max_regno, sizeof (rtx));
3859 reg_has_output_reload = xmalloc (max_regno);
3860 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3861 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
3863 set_initial_elim_offsets ();
3865 for (chain = reload_insn_chain; chain; chain = chain->next)
3867 rtx prev = 0;
3868 rtx insn = chain->insn;
3869 rtx old_next = NEXT_INSN (insn);
3871 /* If we pass a label, copy the offsets from the label information
3872 into the current offsets of each elimination. */
3873 if (LABEL_P (insn))
3874 set_offsets_for_label (insn);
3876 else if (INSN_P (insn))
3878 rtx oldpat = copy_rtx (PATTERN (insn));
3880 /* If this is a USE and CLOBBER of a MEM, ensure that any
3881 references to eliminable registers have been removed. */
3883 if ((GET_CODE (PATTERN (insn)) == USE
3884 || GET_CODE (PATTERN (insn)) == CLOBBER)
3885 && MEM_P (XEXP (PATTERN (insn), 0)))
3886 XEXP (XEXP (PATTERN (insn), 0), 0)
3887 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3888 GET_MODE (XEXP (PATTERN (insn), 0)),
3889 NULL_RTX);
3891 /* If we need to do register elimination processing, do so.
3892 This might delete the insn, in which case we are done. */
3893 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3895 eliminate_regs_in_insn (insn, 1);
3896 if (NOTE_P (insn))
3898 update_eliminable_offsets ();
3899 continue;
3903 /* If need_elim is nonzero but need_reload is zero, one might think
3904 that we could simply set n_reloads to 0. However, find_reloads
3905 could have done some manipulation of the insn (such as swapping
3906 commutative operands), and these manipulations are lost during
3907 the first pass for every insn that needs register elimination.
3908 So the actions of find_reloads must be redone here. */
3910 if (! chain->need_elim && ! chain->need_reload
3911 && ! chain->need_operand_change)
3912 n_reloads = 0;
3913 /* First find the pseudo regs that must be reloaded for this insn.
3914 This info is returned in the tables reload_... (see reload.h).
3915 Also modify the body of INSN by substituting RELOAD
3916 rtx's for those pseudo regs. */
3917 else
3919 memset (reg_has_output_reload, 0, max_regno);
3920 CLEAR_HARD_REG_SET (reg_is_output_reload);
3922 find_reloads (insn, 1, spill_indirect_levels, live_known,
3923 spill_reg_order);
3926 if (n_reloads > 0)
3928 rtx next = NEXT_INSN (insn);
3929 rtx p;
3931 prev = PREV_INSN (insn);
3933 /* Now compute which reload regs to reload them into. Perhaps
3934 reusing reload regs from previous insns, or else output
3935 load insns to reload them. Maybe output store insns too.
3936 Record the choices of reload reg in reload_reg_rtx. */
3937 choose_reload_regs (chain);
3939 /* Merge any reloads that we didn't combine for fear of
3940 increasing the number of spill registers needed but now
3941 discover can be safely merged. */
3942 if (SMALL_REGISTER_CLASSES)
3943 merge_assigned_reloads (insn);
3945 /* Generate the insns to reload operands into or out of
3946 their reload regs. */
3947 emit_reload_insns (chain);
3949 /* Substitute the chosen reload regs from reload_reg_rtx
3950 into the insn's body (or perhaps into the bodies of other
3951 load and store insn that we just made for reloading
3952 and that we moved the structure into). */
3953 subst_reloads (insn);
3955 /* Adjust the exception region notes for loads and stores. */
3956 if (flag_non_call_exceptions && !CALL_P (insn))
3957 fixup_eh_region_note (insn, prev, next);
3959 /* If this was an ASM, make sure that all the reload insns
3960 we have generated are valid. If not, give an error
3961 and delete them. */
3962 if (asm_noperands (PATTERN (insn)) >= 0)
3963 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3964 if (p != insn && INSN_P (p)
3965 && GET_CODE (PATTERN (p)) != USE
3966 && (recog_memoized (p) < 0
3967 || (extract_insn (p), ! constrain_operands (1))))
3969 error_for_asm (insn,
3970 "%<asm%> operand requires "
3971 "impossible reload");
3972 delete_insn (p);
3976 if (num_eliminable && chain->need_elim)
3977 update_eliminable_offsets ();
3979 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3980 is no longer validly lying around to save a future reload.
3981 Note that this does not detect pseudos that were reloaded
3982 for this insn in order to be stored in
3983 (obeying register constraints). That is correct; such reload
3984 registers ARE still valid. */
3985 note_stores (oldpat, forget_old_reloads_1, NULL);
3987 /* There may have been CLOBBER insns placed after INSN. So scan
3988 between INSN and NEXT and use them to forget old reloads. */
3989 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
3990 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
3991 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
3993 #ifdef AUTO_INC_DEC
3994 /* Likewise for regs altered by auto-increment in this insn.
3995 REG_INC notes have been changed by reloading:
3996 find_reloads_address_1 records substitutions for them,
3997 which have been performed by subst_reloads above. */
3998 for (i = n_reloads - 1; i >= 0; i--)
4000 rtx in_reg = rld[i].in_reg;
4001 if (in_reg)
4003 enum rtx_code code = GET_CODE (in_reg);
4004 /* PRE_INC / PRE_DEC will have the reload register ending up
4005 with the same value as the stack slot, but that doesn't
4006 hold true for POST_INC / POST_DEC. Either we have to
4007 convert the memory access to a true POST_INC / POST_DEC,
4008 or we can't use the reload register for inheritance. */
4009 if ((code == POST_INC || code == POST_DEC)
4010 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4011 REGNO (rld[i].reg_rtx))
4012 /* Make sure it is the inc/dec pseudo, and not
4013 some other (e.g. output operand) pseudo. */
4014 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4015 == REGNO (XEXP (in_reg, 0))))
4018 rtx reload_reg = rld[i].reg_rtx;
4019 enum machine_mode mode = GET_MODE (reload_reg);
4020 int n = 0;
4021 rtx p;
4023 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4025 /* We really want to ignore REG_INC notes here, so
4026 use PATTERN (p) as argument to reg_set_p . */
4027 if (reg_set_p (reload_reg, PATTERN (p)))
4028 break;
4029 n = count_occurrences (PATTERN (p), reload_reg, 0);
4030 if (! n)
4031 continue;
4032 if (n == 1)
4034 n = validate_replace_rtx (reload_reg,
4035 gen_rtx_fmt_e (code,
4036 mode,
4037 reload_reg),
4040 /* We must also verify that the constraints
4041 are met after the replacement. */
4042 extract_insn (p);
4043 if (n)
4044 n = constrain_operands (1);
4045 else
4046 break;
4048 /* If the constraints were not met, then
4049 undo the replacement. */
4050 if (!n)
4052 validate_replace_rtx (gen_rtx_fmt_e (code,
4053 mode,
4054 reload_reg),
4055 reload_reg, p);
4056 break;
4060 break;
4062 if (n == 1)
4064 REG_NOTES (p)
4065 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4066 REG_NOTES (p));
4067 /* Mark this as having an output reload so that the
4068 REG_INC processing code below won't invalidate
4069 the reload for inheritance. */
4070 SET_HARD_REG_BIT (reg_is_output_reload,
4071 REGNO (reload_reg));
4072 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4074 else
4075 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4076 NULL);
4078 else if ((code == PRE_INC || code == PRE_DEC)
4079 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4080 REGNO (rld[i].reg_rtx))
4081 /* Make sure it is the inc/dec pseudo, and not
4082 some other (e.g. output operand) pseudo. */
4083 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4084 == REGNO (XEXP (in_reg, 0))))
4086 SET_HARD_REG_BIT (reg_is_output_reload,
4087 REGNO (rld[i].reg_rtx));
4088 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4092 /* If a pseudo that got a hard register is auto-incremented,
4093 we must purge records of copying it into pseudos without
4094 hard registers. */
4095 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4096 if (REG_NOTE_KIND (x) == REG_INC)
4098 /* See if this pseudo reg was reloaded in this insn.
4099 If so, its last-reload info is still valid
4100 because it is based on this insn's reload. */
4101 for (i = 0; i < n_reloads; i++)
4102 if (rld[i].out == XEXP (x, 0))
4103 break;
4105 if (i == n_reloads)
4106 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4108 #endif
4110 /* A reload reg's contents are unknown after a label. */
4111 if (LABEL_P (insn))
4112 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4114 /* Don't assume a reload reg is still good after a call insn
4115 if it is a call-used reg, or if it contains a value that will
4116 be partially clobbered by the call. */
4117 else if (CALL_P (insn))
4119 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4120 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4124 /* Clean up. */
4125 free (reg_last_reload_reg);
4126 free (reg_has_output_reload);
4129 /* Discard all record of any value reloaded from X,
4130 or reloaded in X from someplace else;
4131 unless X is an output reload reg of the current insn.
4133 X may be a hard reg (the reload reg)
4134 or it may be a pseudo reg that was reloaded from. */
4136 static void
4137 forget_old_reloads_1 (rtx x, rtx ignored ATTRIBUTE_UNUSED,
4138 void *data ATTRIBUTE_UNUSED)
4140 unsigned int regno;
4141 unsigned int nr;
4143 /* note_stores does give us subregs of hard regs,
4144 subreg_regno_offset requires a hard reg. */
4145 while (GET_CODE (x) == SUBREG)
4147 /* We ignore the subreg offset when calculating the regno,
4148 because we are using the entire underlying hard register
4149 below. */
4150 x = SUBREG_REG (x);
4153 if (!REG_P (x))
4154 return;
4156 regno = REGNO (x);
4158 if (regno >= FIRST_PSEUDO_REGISTER)
4159 nr = 1;
4160 else
4162 unsigned int i;
4164 nr = hard_regno_nregs[regno][GET_MODE (x)];
4165 /* Storing into a spilled-reg invalidates its contents.
4166 This can happen if a block-local pseudo is allocated to that reg
4167 and it wasn't spilled because this block's total need is 0.
4168 Then some insn might have an optional reload and use this reg. */
4169 for (i = 0; i < nr; i++)
4170 /* But don't do this if the reg actually serves as an output
4171 reload reg in the current instruction. */
4172 if (n_reloads == 0
4173 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4175 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4176 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, regno + i);
4177 spill_reg_store[regno + i] = 0;
4181 /* Since value of X has changed,
4182 forget any value previously copied from it. */
4184 while (nr-- > 0)
4185 /* But don't forget a copy if this is the output reload
4186 that establishes the copy's validity. */
4187 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4188 reg_last_reload_reg[regno + nr] = 0;
4191 /* The following HARD_REG_SETs indicate when each hard register is
4192 used for a reload of various parts of the current insn. */
4194 /* If reg is unavailable for all reloads. */
4195 static HARD_REG_SET reload_reg_unavailable;
4196 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4197 static HARD_REG_SET reload_reg_used;
4198 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4199 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4200 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4201 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4202 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4203 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4204 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4205 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4206 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4207 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4208 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4209 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4210 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4211 static HARD_REG_SET reload_reg_used_in_op_addr;
4212 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4213 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4214 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4215 static HARD_REG_SET reload_reg_used_in_insn;
4216 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4217 static HARD_REG_SET reload_reg_used_in_other_addr;
4219 /* If reg is in use as a reload reg for any sort of reload. */
4220 static HARD_REG_SET reload_reg_used_at_all;
4222 /* If reg is use as an inherited reload. We just mark the first register
4223 in the group. */
4224 static HARD_REG_SET reload_reg_used_for_inherit;
4226 /* Records which hard regs are used in any way, either as explicit use or
4227 by being allocated to a pseudo during any point of the current insn. */
4228 static HARD_REG_SET reg_used_in_insn;
4230 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4231 TYPE. MODE is used to indicate how many consecutive regs are
4232 actually used. */
4234 static void
4235 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4236 enum machine_mode mode)
4238 unsigned int nregs = hard_regno_nregs[regno][mode];
4239 unsigned int i;
4241 for (i = regno; i < nregs + regno; i++)
4243 switch (type)
4245 case RELOAD_OTHER:
4246 SET_HARD_REG_BIT (reload_reg_used, i);
4247 break;
4249 case RELOAD_FOR_INPUT_ADDRESS:
4250 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4251 break;
4253 case RELOAD_FOR_INPADDR_ADDRESS:
4254 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4255 break;
4257 case RELOAD_FOR_OUTPUT_ADDRESS:
4258 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4259 break;
4261 case RELOAD_FOR_OUTADDR_ADDRESS:
4262 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4263 break;
4265 case RELOAD_FOR_OPERAND_ADDRESS:
4266 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4267 break;
4269 case RELOAD_FOR_OPADDR_ADDR:
4270 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4271 break;
4273 case RELOAD_FOR_OTHER_ADDRESS:
4274 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4275 break;
4277 case RELOAD_FOR_INPUT:
4278 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4279 break;
4281 case RELOAD_FOR_OUTPUT:
4282 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4283 break;
4285 case RELOAD_FOR_INSN:
4286 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4287 break;
4290 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4294 /* Similarly, but show REGNO is no longer in use for a reload. */
4296 static void
4297 clear_reload_reg_in_use (unsigned int regno, int opnum,
4298 enum reload_type type, enum machine_mode mode)
4300 unsigned int nregs = hard_regno_nregs[regno][mode];
4301 unsigned int start_regno, end_regno, r;
4302 int i;
4303 /* A complication is that for some reload types, inheritance might
4304 allow multiple reloads of the same types to share a reload register.
4305 We set check_opnum if we have to check only reloads with the same
4306 operand number, and check_any if we have to check all reloads. */
4307 int check_opnum = 0;
4308 int check_any = 0;
4309 HARD_REG_SET *used_in_set;
4311 switch (type)
4313 case RELOAD_OTHER:
4314 used_in_set = &reload_reg_used;
4315 break;
4317 case RELOAD_FOR_INPUT_ADDRESS:
4318 used_in_set = &reload_reg_used_in_input_addr[opnum];
4319 break;
4321 case RELOAD_FOR_INPADDR_ADDRESS:
4322 check_opnum = 1;
4323 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4324 break;
4326 case RELOAD_FOR_OUTPUT_ADDRESS:
4327 used_in_set = &reload_reg_used_in_output_addr[opnum];
4328 break;
4330 case RELOAD_FOR_OUTADDR_ADDRESS:
4331 check_opnum = 1;
4332 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4333 break;
4335 case RELOAD_FOR_OPERAND_ADDRESS:
4336 used_in_set = &reload_reg_used_in_op_addr;
4337 break;
4339 case RELOAD_FOR_OPADDR_ADDR:
4340 check_any = 1;
4341 used_in_set = &reload_reg_used_in_op_addr_reload;
4342 break;
4344 case RELOAD_FOR_OTHER_ADDRESS:
4345 used_in_set = &reload_reg_used_in_other_addr;
4346 check_any = 1;
4347 break;
4349 case RELOAD_FOR_INPUT:
4350 used_in_set = &reload_reg_used_in_input[opnum];
4351 break;
4353 case RELOAD_FOR_OUTPUT:
4354 used_in_set = &reload_reg_used_in_output[opnum];
4355 break;
4357 case RELOAD_FOR_INSN:
4358 used_in_set = &reload_reg_used_in_insn;
4359 break;
4360 default:
4361 gcc_unreachable ();
4363 /* We resolve conflicts with remaining reloads of the same type by
4364 excluding the intervals of reload registers by them from the
4365 interval of freed reload registers. Since we only keep track of
4366 one set of interval bounds, we might have to exclude somewhat
4367 more than what would be necessary if we used a HARD_REG_SET here.
4368 But this should only happen very infrequently, so there should
4369 be no reason to worry about it. */
4371 start_regno = regno;
4372 end_regno = regno + nregs;
4373 if (check_opnum || check_any)
4375 for (i = n_reloads - 1; i >= 0; i--)
4377 if (rld[i].when_needed == type
4378 && (check_any || rld[i].opnum == opnum)
4379 && rld[i].reg_rtx)
4381 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4382 unsigned int conflict_end
4383 = (conflict_start
4384 + hard_regno_nregs[conflict_start][rld[i].mode]);
4386 /* If there is an overlap with the first to-be-freed register,
4387 adjust the interval start. */
4388 if (conflict_start <= start_regno && conflict_end > start_regno)
4389 start_regno = conflict_end;
4390 /* Otherwise, if there is a conflict with one of the other
4391 to-be-freed registers, adjust the interval end. */
4392 if (conflict_start > start_regno && conflict_start < end_regno)
4393 end_regno = conflict_start;
4398 for (r = start_regno; r < end_regno; r++)
4399 CLEAR_HARD_REG_BIT (*used_in_set, r);
4402 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4403 specified by OPNUM and TYPE. */
4405 static int
4406 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4408 int i;
4410 /* In use for a RELOAD_OTHER means it's not available for anything. */
4411 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4412 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4413 return 0;
4415 switch (type)
4417 case RELOAD_OTHER:
4418 /* In use for anything means we can't use it for RELOAD_OTHER. */
4419 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4420 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4421 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4422 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4423 return 0;
4425 for (i = 0; i < reload_n_operands; i++)
4426 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4427 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4428 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4429 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4430 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4431 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4432 return 0;
4434 return 1;
4436 case RELOAD_FOR_INPUT:
4437 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4438 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4439 return 0;
4441 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4442 return 0;
4444 /* If it is used for some other input, can't use it. */
4445 for (i = 0; i < reload_n_operands; i++)
4446 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4447 return 0;
4449 /* If it is used in a later operand's address, can't use it. */
4450 for (i = opnum + 1; i < reload_n_operands; i++)
4451 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4452 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4453 return 0;
4455 return 1;
4457 case RELOAD_FOR_INPUT_ADDRESS:
4458 /* Can't use a register if it is used for an input address for this
4459 operand or used as an input in an earlier one. */
4460 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4461 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4462 return 0;
4464 for (i = 0; i < opnum; i++)
4465 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4466 return 0;
4468 return 1;
4470 case RELOAD_FOR_INPADDR_ADDRESS:
4471 /* Can't use a register if it is used for an input address
4472 for this operand or used as an input in an earlier
4473 one. */
4474 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4475 return 0;
4477 for (i = 0; i < opnum; i++)
4478 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4479 return 0;
4481 return 1;
4483 case RELOAD_FOR_OUTPUT_ADDRESS:
4484 /* Can't use a register if it is used for an output address for this
4485 operand or used as an output in this or a later operand. Note
4486 that multiple output operands are emitted in reverse order, so
4487 the conflicting ones are those with lower indices. */
4488 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4489 return 0;
4491 for (i = 0; i <= opnum; i++)
4492 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4493 return 0;
4495 return 1;
4497 case RELOAD_FOR_OUTADDR_ADDRESS:
4498 /* Can't use a register if it is used for an output address
4499 for this operand or used as an output in this or a
4500 later operand. Note that multiple output operands are
4501 emitted in reverse order, so the conflicting ones are
4502 those with lower indices. */
4503 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4504 return 0;
4506 for (i = 0; i <= opnum; i++)
4507 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4508 return 0;
4510 return 1;
4512 case RELOAD_FOR_OPERAND_ADDRESS:
4513 for (i = 0; i < reload_n_operands; i++)
4514 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4515 return 0;
4517 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4518 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4520 case RELOAD_FOR_OPADDR_ADDR:
4521 for (i = 0; i < reload_n_operands; i++)
4522 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4523 return 0;
4525 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4527 case RELOAD_FOR_OUTPUT:
4528 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4529 outputs, or an operand address for this or an earlier output.
4530 Note that multiple output operands are emitted in reverse order,
4531 so the conflicting ones are those with higher indices. */
4532 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4533 return 0;
4535 for (i = 0; i < reload_n_operands; i++)
4536 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4537 return 0;
4539 for (i = opnum; i < reload_n_operands; i++)
4540 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4541 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4542 return 0;
4544 return 1;
4546 case RELOAD_FOR_INSN:
4547 for (i = 0; i < reload_n_operands; i++)
4548 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4549 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4550 return 0;
4552 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4553 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4555 case RELOAD_FOR_OTHER_ADDRESS:
4556 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4558 default:
4559 gcc_unreachable ();
4563 /* Return 1 if the value in reload reg REGNO, as used by a reload
4564 needed for the part of the insn specified by OPNUM and TYPE,
4565 is still available in REGNO at the end of the insn.
4567 We can assume that the reload reg was already tested for availability
4568 at the time it is needed, and we should not check this again,
4569 in case the reg has already been marked in use. */
4571 static int
4572 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4574 int i;
4576 switch (type)
4578 case RELOAD_OTHER:
4579 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4580 its value must reach the end. */
4581 return 1;
4583 /* If this use is for part of the insn,
4584 its value reaches if no subsequent part uses the same register.
4585 Just like the above function, don't try to do this with lots
4586 of fallthroughs. */
4588 case RELOAD_FOR_OTHER_ADDRESS:
4589 /* Here we check for everything else, since these don't conflict
4590 with anything else and everything comes later. */
4592 for (i = 0; i < reload_n_operands; i++)
4593 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4594 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4595 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4596 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4597 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4598 || TEST_HARD_REG_BIT (reload_reg_used_in_input[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_op_addr_reload, regno)
4603 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4604 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4606 case RELOAD_FOR_INPUT_ADDRESS:
4607 case RELOAD_FOR_INPADDR_ADDRESS:
4608 /* Similar, except that we check only for this and subsequent inputs
4609 and the address of only subsequent inputs and we do not need
4610 to check for RELOAD_OTHER objects since they are known not to
4611 conflict. */
4613 for (i = opnum; i < reload_n_operands; i++)
4614 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4615 return 0;
4617 for (i = opnum + 1; i < reload_n_operands; i++)
4618 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4619 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4620 return 0;
4622 for (i = 0; i < reload_n_operands; i++)
4623 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4624 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4625 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4626 return 0;
4628 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4629 return 0;
4631 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4632 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4633 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4635 case RELOAD_FOR_INPUT:
4636 /* Similar to input address, except we start at the next operand for
4637 both input and input address and we do not check for
4638 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4639 would conflict. */
4641 for (i = opnum + 1; i < reload_n_operands; i++)
4642 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4643 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4644 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4645 return 0;
4647 /* ... fall through ... */
4649 case RELOAD_FOR_OPERAND_ADDRESS:
4650 /* Check outputs and their addresses. */
4652 for (i = 0; i < reload_n_operands; i++)
4653 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4654 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4655 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4656 return 0;
4658 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4660 case RELOAD_FOR_OPADDR_ADDR:
4661 for (i = 0; i < reload_n_operands; i++)
4662 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4663 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4664 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4665 return 0;
4667 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4668 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4669 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4671 case RELOAD_FOR_INSN:
4672 /* These conflict with other outputs with RELOAD_OTHER. So
4673 we need only check for output addresses. */
4675 opnum = reload_n_operands;
4677 /* ... fall through ... */
4679 case RELOAD_FOR_OUTPUT:
4680 case RELOAD_FOR_OUTPUT_ADDRESS:
4681 case RELOAD_FOR_OUTADDR_ADDRESS:
4682 /* We already know these can't conflict with a later output. So the
4683 only thing to check are later output addresses.
4684 Note that multiple output operands are emitted in reverse order,
4685 so the conflicting ones are those with lower indices. */
4686 for (i = 0; i < opnum; i++)
4687 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4688 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4689 return 0;
4691 return 1;
4693 default:
4694 gcc_unreachable ();
4698 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4699 Return 0 otherwise.
4701 This function uses the same algorithm as reload_reg_free_p above. */
4703 static int
4704 reloads_conflict (int r1, int r2)
4706 enum reload_type r1_type = rld[r1].when_needed;
4707 enum reload_type r2_type = rld[r2].when_needed;
4708 int r1_opnum = rld[r1].opnum;
4709 int r2_opnum = rld[r2].opnum;
4711 /* RELOAD_OTHER conflicts with everything. */
4712 if (r2_type == RELOAD_OTHER)
4713 return 1;
4715 /* Otherwise, check conflicts differently for each type. */
4717 switch (r1_type)
4719 case RELOAD_FOR_INPUT:
4720 return (r2_type == RELOAD_FOR_INSN
4721 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4722 || r2_type == RELOAD_FOR_OPADDR_ADDR
4723 || r2_type == RELOAD_FOR_INPUT
4724 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4725 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4726 && r2_opnum > r1_opnum));
4728 case RELOAD_FOR_INPUT_ADDRESS:
4729 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4730 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4732 case RELOAD_FOR_INPADDR_ADDRESS:
4733 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4734 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4736 case RELOAD_FOR_OUTPUT_ADDRESS:
4737 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4738 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4740 case RELOAD_FOR_OUTADDR_ADDRESS:
4741 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4742 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4744 case RELOAD_FOR_OPERAND_ADDRESS:
4745 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4746 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4748 case RELOAD_FOR_OPADDR_ADDR:
4749 return (r2_type == RELOAD_FOR_INPUT
4750 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4752 case RELOAD_FOR_OUTPUT:
4753 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4754 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4755 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4756 && r2_opnum >= r1_opnum));
4758 case RELOAD_FOR_INSN:
4759 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4760 || r2_type == RELOAD_FOR_INSN
4761 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4763 case RELOAD_FOR_OTHER_ADDRESS:
4764 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4766 case RELOAD_OTHER:
4767 return 1;
4769 default:
4770 gcc_unreachable ();
4774 /* Indexed by reload number, 1 if incoming value
4775 inherited from previous insns. */
4776 static char reload_inherited[MAX_RELOADS];
4778 /* For an inherited reload, this is the insn the reload was inherited from,
4779 if we know it. Otherwise, this is 0. */
4780 static rtx reload_inheritance_insn[MAX_RELOADS];
4782 /* If nonzero, this is a place to get the value of the reload,
4783 rather than using reload_in. */
4784 static rtx reload_override_in[MAX_RELOADS];
4786 /* For each reload, the hard register number of the register used,
4787 or -1 if we did not need a register for this reload. */
4788 static int reload_spill_index[MAX_RELOADS];
4790 /* Subroutine of free_for_value_p, used to check a single register.
4791 START_REGNO is the starting regno of the full reload register
4792 (possibly comprising multiple hard registers) that we are considering. */
4794 static int
4795 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
4796 enum reload_type type, rtx value, rtx out,
4797 int reloadnum, int ignore_address_reloads)
4799 int time1;
4800 /* Set if we see an input reload that must not share its reload register
4801 with any new earlyclobber, but might otherwise share the reload
4802 register with an output or input-output reload. */
4803 int check_earlyclobber = 0;
4804 int i;
4805 int copy = 0;
4807 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4808 return 0;
4810 if (out == const0_rtx)
4812 copy = 1;
4813 out = NULL_RTX;
4816 /* We use some pseudo 'time' value to check if the lifetimes of the
4817 new register use would overlap with the one of a previous reload
4818 that is not read-only or uses a different value.
4819 The 'time' used doesn't have to be linear in any shape or form, just
4820 monotonic.
4821 Some reload types use different 'buckets' for each operand.
4822 So there are MAX_RECOG_OPERANDS different time values for each
4823 such reload type.
4824 We compute TIME1 as the time when the register for the prospective
4825 new reload ceases to be live, and TIME2 for each existing
4826 reload as the time when that the reload register of that reload
4827 becomes live.
4828 Where there is little to be gained by exact lifetime calculations,
4829 we just make conservative assumptions, i.e. a longer lifetime;
4830 this is done in the 'default:' cases. */
4831 switch (type)
4833 case RELOAD_FOR_OTHER_ADDRESS:
4834 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4835 time1 = copy ? 0 : 1;
4836 break;
4837 case RELOAD_OTHER:
4838 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4839 break;
4840 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4841 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4842 respectively, to the time values for these, we get distinct time
4843 values. To get distinct time values for each operand, we have to
4844 multiply opnum by at least three. We round that up to four because
4845 multiply by four is often cheaper. */
4846 case RELOAD_FOR_INPADDR_ADDRESS:
4847 time1 = opnum * 4 + 2;
4848 break;
4849 case RELOAD_FOR_INPUT_ADDRESS:
4850 time1 = opnum * 4 + 3;
4851 break;
4852 case RELOAD_FOR_INPUT:
4853 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4854 executes (inclusive). */
4855 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
4856 break;
4857 case RELOAD_FOR_OPADDR_ADDR:
4858 /* opnum * 4 + 4
4859 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
4860 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4861 break;
4862 case RELOAD_FOR_OPERAND_ADDRESS:
4863 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4864 is executed. */
4865 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4866 break;
4867 case RELOAD_FOR_OUTADDR_ADDRESS:
4868 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
4869 break;
4870 case RELOAD_FOR_OUTPUT_ADDRESS:
4871 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
4872 break;
4873 default:
4874 time1 = MAX_RECOG_OPERANDS * 5 + 5;
4877 for (i = 0; i < n_reloads; i++)
4879 rtx reg = rld[i].reg_rtx;
4880 if (reg && REG_P (reg)
4881 && ((unsigned) regno - true_regnum (reg)
4882 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
4883 && i != reloadnum)
4885 rtx other_input = rld[i].in;
4887 /* If the other reload loads the same input value, that
4888 will not cause a conflict only if it's loading it into
4889 the same register. */
4890 if (true_regnum (reg) != start_regno)
4891 other_input = NULL_RTX;
4892 if (! other_input || ! rtx_equal_p (other_input, value)
4893 || rld[i].out || out)
4895 int time2;
4896 switch (rld[i].when_needed)
4898 case RELOAD_FOR_OTHER_ADDRESS:
4899 time2 = 0;
4900 break;
4901 case RELOAD_FOR_INPADDR_ADDRESS:
4902 /* find_reloads makes sure that a
4903 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4904 by at most one - the first -
4905 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4906 address reload is inherited, the address address reload
4907 goes away, so we can ignore this conflict. */
4908 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4909 && ignore_address_reloads
4910 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4911 Then the address address is still needed to store
4912 back the new address. */
4913 && ! rld[reloadnum].out)
4914 continue;
4915 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4916 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4917 reloads go away. */
4918 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4919 && ignore_address_reloads
4920 /* Unless we are reloading an auto_inc expression. */
4921 && ! rld[reloadnum].out)
4922 continue;
4923 time2 = rld[i].opnum * 4 + 2;
4924 break;
4925 case RELOAD_FOR_INPUT_ADDRESS:
4926 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4927 && ignore_address_reloads
4928 && ! rld[reloadnum].out)
4929 continue;
4930 time2 = rld[i].opnum * 4 + 3;
4931 break;
4932 case RELOAD_FOR_INPUT:
4933 time2 = rld[i].opnum * 4 + 4;
4934 check_earlyclobber = 1;
4935 break;
4936 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
4937 == MAX_RECOG_OPERAND * 4 */
4938 case RELOAD_FOR_OPADDR_ADDR:
4939 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4940 && ignore_address_reloads
4941 && ! rld[reloadnum].out)
4942 continue;
4943 time2 = MAX_RECOG_OPERANDS * 4 + 1;
4944 break;
4945 case RELOAD_FOR_OPERAND_ADDRESS:
4946 time2 = MAX_RECOG_OPERANDS * 4 + 2;
4947 check_earlyclobber = 1;
4948 break;
4949 case RELOAD_FOR_INSN:
4950 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4951 break;
4952 case RELOAD_FOR_OUTPUT:
4953 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4954 instruction is executed. */
4955 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4956 break;
4957 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4958 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4959 value. */
4960 case RELOAD_FOR_OUTADDR_ADDRESS:
4961 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4962 && ignore_address_reloads
4963 && ! rld[reloadnum].out)
4964 continue;
4965 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
4966 break;
4967 case RELOAD_FOR_OUTPUT_ADDRESS:
4968 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
4969 break;
4970 case RELOAD_OTHER:
4971 /* If there is no conflict in the input part, handle this
4972 like an output reload. */
4973 if (! rld[i].in || rtx_equal_p (other_input, value))
4975 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4976 /* Earlyclobbered outputs must conflict with inputs. */
4977 if (earlyclobber_operand_p (rld[i].out))
4978 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4980 break;
4982 time2 = 1;
4983 /* RELOAD_OTHER might be live beyond instruction execution,
4984 but this is not obvious when we set time2 = 1. So check
4985 here if there might be a problem with the new reload
4986 clobbering the register used by the RELOAD_OTHER. */
4987 if (out)
4988 return 0;
4989 break;
4990 default:
4991 return 0;
4993 if ((time1 >= time2
4994 && (! rld[i].in || rld[i].out
4995 || ! rtx_equal_p (other_input, value)))
4996 || (out && rld[reloadnum].out_reg
4997 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
4998 return 0;
5003 /* Earlyclobbered outputs must conflict with inputs. */
5004 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5005 return 0;
5007 return 1;
5010 /* Return 1 if the value in reload reg REGNO, as used by a reload
5011 needed for the part of the insn specified by OPNUM and TYPE,
5012 may be used to load VALUE into it.
5014 MODE is the mode in which the register is used, this is needed to
5015 determine how many hard regs to test.
5017 Other read-only reloads with the same value do not conflict
5018 unless OUT is nonzero and these other reloads have to live while
5019 output reloads live.
5020 If OUT is CONST0_RTX, this is a special case: it means that the
5021 test should not be for using register REGNO as reload register, but
5022 for copying from register REGNO into the reload register.
5024 RELOADNUM is the number of the reload we want to load this value for;
5025 a reload does not conflict with itself.
5027 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5028 reloads that load an address for the very reload we are considering.
5030 The caller has to make sure that there is no conflict with the return
5031 register. */
5033 static int
5034 free_for_value_p (int regno, enum machine_mode mode, int opnum,
5035 enum reload_type type, rtx value, rtx out, int reloadnum,
5036 int ignore_address_reloads)
5038 int nregs = hard_regno_nregs[regno][mode];
5039 while (nregs-- > 0)
5040 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5041 value, out, reloadnum,
5042 ignore_address_reloads))
5043 return 0;
5044 return 1;
5047 /* Return nonzero if the rtx X is invariant over the current function. */
5048 /* ??? Actually, the places where we use this expect exactly what is
5049 tested here, and not everything that is function invariant. In
5050 particular, the frame pointer and arg pointer are special cased;
5051 pic_offset_table_rtx is not, and we must not spill these things to
5052 memory. */
5055 function_invariant_p (rtx x)
5057 if (CONSTANT_P (x))
5058 return 1;
5059 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
5060 return 1;
5061 if (GET_CODE (x) == PLUS
5062 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
5063 && CONSTANT_P (XEXP (x, 1)))
5064 return 1;
5065 return 0;
5068 /* Determine whether the reload reg X overlaps any rtx'es used for
5069 overriding inheritance. Return nonzero if so. */
5071 static int
5072 conflicts_with_override (rtx x)
5074 int i;
5075 for (i = 0; i < n_reloads; i++)
5076 if (reload_override_in[i]
5077 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5078 return 1;
5079 return 0;
5082 /* Give an error message saying we failed to find a reload for INSN,
5083 and clear out reload R. */
5084 static void
5085 failed_reload (rtx insn, int r)
5087 if (asm_noperands (PATTERN (insn)) < 0)
5088 /* It's the compiler's fault. */
5089 fatal_insn ("could not find a spill register", insn);
5091 /* It's the user's fault; the operand's mode and constraint
5092 don't match. Disable this reload so we don't crash in final. */
5093 error_for_asm (insn,
5094 "%<asm%> operand constraint incompatible with operand size");
5095 rld[r].in = 0;
5096 rld[r].out = 0;
5097 rld[r].reg_rtx = 0;
5098 rld[r].optional = 1;
5099 rld[r].secondary_p = 1;
5102 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5103 for reload R. If it's valid, get an rtx for it. Return nonzero if
5104 successful. */
5105 static int
5106 set_reload_reg (int i, int r)
5108 int regno;
5109 rtx reg = spill_reg_rtx[i];
5111 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5112 spill_reg_rtx[i] = reg
5113 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5115 regno = true_regnum (reg);
5117 /* Detect when the reload reg can't hold the reload mode.
5118 This used to be one `if', but Sequent compiler can't handle that. */
5119 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5121 enum machine_mode test_mode = VOIDmode;
5122 if (rld[r].in)
5123 test_mode = GET_MODE (rld[r].in);
5124 /* If rld[r].in has VOIDmode, it means we will load it
5125 in whatever mode the reload reg has: to wit, rld[r].mode.
5126 We have already tested that for validity. */
5127 /* Aside from that, we need to test that the expressions
5128 to reload from or into have modes which are valid for this
5129 reload register. Otherwise the reload insns would be invalid. */
5130 if (! (rld[r].in != 0 && test_mode != VOIDmode
5131 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5132 if (! (rld[r].out != 0
5133 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5135 /* The reg is OK. */
5136 last_spill_reg = i;
5138 /* Mark as in use for this insn the reload regs we use
5139 for this. */
5140 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5141 rld[r].when_needed, rld[r].mode);
5143 rld[r].reg_rtx = reg;
5144 reload_spill_index[r] = spill_regs[i];
5145 return 1;
5148 return 0;
5151 /* Find a spill register to use as a reload register for reload R.
5152 LAST_RELOAD is nonzero if this is the last reload for the insn being
5153 processed.
5155 Set rld[R].reg_rtx to the register allocated.
5157 We return 1 if successful, or 0 if we couldn't find a spill reg and
5158 we didn't change anything. */
5160 static int
5161 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5162 int last_reload)
5164 int i, pass, count;
5166 /* If we put this reload ahead, thinking it is a group,
5167 then insist on finding a group. Otherwise we can grab a
5168 reg that some other reload needs.
5169 (That can happen when we have a 68000 DATA_OR_FP_REG
5170 which is a group of data regs or one fp reg.)
5171 We need not be so restrictive if there are no more reloads
5172 for this insn.
5174 ??? Really it would be nicer to have smarter handling
5175 for that kind of reg class, where a problem like this is normal.
5176 Perhaps those classes should be avoided for reloading
5177 by use of more alternatives. */
5179 int force_group = rld[r].nregs > 1 && ! last_reload;
5181 /* If we want a single register and haven't yet found one,
5182 take any reg in the right class and not in use.
5183 If we want a consecutive group, here is where we look for it.
5185 We use two passes so we can first look for reload regs to
5186 reuse, which are already in use for other reloads in this insn,
5187 and only then use additional registers.
5188 I think that maximizing reuse is needed to make sure we don't
5189 run out of reload regs. Suppose we have three reloads, and
5190 reloads A and B can share regs. These need two regs.
5191 Suppose A and B are given different regs.
5192 That leaves none for C. */
5193 for (pass = 0; pass < 2; pass++)
5195 /* I is the index in spill_regs.
5196 We advance it round-robin between insns to use all spill regs
5197 equally, so that inherited reloads have a chance
5198 of leapfrogging each other. */
5200 i = last_spill_reg;
5202 for (count = 0; count < n_spills; count++)
5204 int class = (int) rld[r].class;
5205 int regnum;
5207 i++;
5208 if (i >= n_spills)
5209 i -= n_spills;
5210 regnum = spill_regs[i];
5212 if ((reload_reg_free_p (regnum, rld[r].opnum,
5213 rld[r].when_needed)
5214 || (rld[r].in
5215 /* We check reload_reg_used to make sure we
5216 don't clobber the return register. */
5217 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5218 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5219 rld[r].when_needed, rld[r].in,
5220 rld[r].out, r, 1)))
5221 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5222 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5223 /* Look first for regs to share, then for unshared. But
5224 don't share regs used for inherited reloads; they are
5225 the ones we want to preserve. */
5226 && (pass
5227 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5228 regnum)
5229 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5230 regnum))))
5232 int nr = hard_regno_nregs[regnum][rld[r].mode];
5233 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5234 (on 68000) got us two FP regs. If NR is 1,
5235 we would reject both of them. */
5236 if (force_group)
5237 nr = rld[r].nregs;
5238 /* If we need only one reg, we have already won. */
5239 if (nr == 1)
5241 /* But reject a single reg if we demand a group. */
5242 if (force_group)
5243 continue;
5244 break;
5246 /* Otherwise check that as many consecutive regs as we need
5247 are available here. */
5248 while (nr > 1)
5250 int regno = regnum + nr - 1;
5251 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5252 && spill_reg_order[regno] >= 0
5253 && reload_reg_free_p (regno, rld[r].opnum,
5254 rld[r].when_needed)))
5255 break;
5256 nr--;
5258 if (nr == 1)
5259 break;
5263 /* If we found something on pass 1, omit pass 2. */
5264 if (count < n_spills)
5265 break;
5268 /* We should have found a spill register by now. */
5269 if (count >= n_spills)
5270 return 0;
5272 /* I is the index in SPILL_REG_RTX of the reload register we are to
5273 allocate. Get an rtx for it and find its register number. */
5275 return set_reload_reg (i, r);
5278 /* Initialize all the tables needed to allocate reload registers.
5279 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5280 is the array we use to restore the reg_rtx field for every reload. */
5282 static void
5283 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5285 int i;
5287 for (i = 0; i < n_reloads; i++)
5288 rld[i].reg_rtx = save_reload_reg_rtx[i];
5290 memset (reload_inherited, 0, MAX_RELOADS);
5291 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5292 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5294 CLEAR_HARD_REG_SET (reload_reg_used);
5295 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5296 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5297 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5298 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5299 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5301 CLEAR_HARD_REG_SET (reg_used_in_insn);
5303 HARD_REG_SET tmp;
5304 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5305 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5306 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5307 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5308 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5309 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5312 for (i = 0; i < reload_n_operands; i++)
5314 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5315 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5316 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5317 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5318 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5319 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5322 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5324 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5326 for (i = 0; i < n_reloads; i++)
5327 /* If we have already decided to use a certain register,
5328 don't use it in another way. */
5329 if (rld[i].reg_rtx)
5330 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5331 rld[i].when_needed, rld[i].mode);
5334 /* Assign hard reg targets for the pseudo-registers we must reload
5335 into hard regs for this insn.
5336 Also output the instructions to copy them in and out of the hard regs.
5338 For machines with register classes, we are responsible for
5339 finding a reload reg in the proper class. */
5341 static void
5342 choose_reload_regs (struct insn_chain *chain)
5344 rtx insn = chain->insn;
5345 int i, j;
5346 unsigned int max_group_size = 1;
5347 enum reg_class group_class = NO_REGS;
5348 int pass, win, inheritance;
5350 rtx save_reload_reg_rtx[MAX_RELOADS];
5352 /* In order to be certain of getting the registers we need,
5353 we must sort the reloads into order of increasing register class.
5354 Then our grabbing of reload registers will parallel the process
5355 that provided the reload registers.
5357 Also note whether any of the reloads wants a consecutive group of regs.
5358 If so, record the maximum size of the group desired and what
5359 register class contains all the groups needed by this insn. */
5361 for (j = 0; j < n_reloads; j++)
5363 reload_order[j] = j;
5364 reload_spill_index[j] = -1;
5366 if (rld[j].nregs > 1)
5368 max_group_size = MAX (rld[j].nregs, max_group_size);
5369 group_class
5370 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5373 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5376 if (n_reloads > 1)
5377 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5379 /* If -O, try first with inheritance, then turning it off.
5380 If not -O, don't do inheritance.
5381 Using inheritance when not optimizing leads to paradoxes
5382 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5383 because one side of the comparison might be inherited. */
5384 win = 0;
5385 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5387 choose_reload_regs_init (chain, save_reload_reg_rtx);
5389 /* Process the reloads in order of preference just found.
5390 Beyond this point, subregs can be found in reload_reg_rtx.
5392 This used to look for an existing reloaded home for all of the
5393 reloads, and only then perform any new reloads. But that could lose
5394 if the reloads were done out of reg-class order because a later
5395 reload with a looser constraint might have an old home in a register
5396 needed by an earlier reload with a tighter constraint.
5398 To solve this, we make two passes over the reloads, in the order
5399 described above. In the first pass we try to inherit a reload
5400 from a previous insn. If there is a later reload that needs a
5401 class that is a proper subset of the class being processed, we must
5402 also allocate a spill register during the first pass.
5404 Then make a second pass over the reloads to allocate any reloads
5405 that haven't been given registers yet. */
5407 for (j = 0; j < n_reloads; j++)
5409 int r = reload_order[j];
5410 rtx search_equiv = NULL_RTX;
5412 /* Ignore reloads that got marked inoperative. */
5413 if (rld[r].out == 0 && rld[r].in == 0
5414 && ! rld[r].secondary_p)
5415 continue;
5417 /* If find_reloads chose to use reload_in or reload_out as a reload
5418 register, we don't need to chose one. Otherwise, try even if it
5419 found one since we might save an insn if we find the value lying
5420 around.
5421 Try also when reload_in is a pseudo without a hard reg. */
5422 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5423 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5424 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5425 && !MEM_P (rld[r].in)
5426 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5427 continue;
5429 #if 0 /* No longer needed for correct operation.
5430 It might give better code, or might not; worth an experiment? */
5431 /* If this is an optional reload, we can't inherit from earlier insns
5432 until we are sure that any non-optional reloads have been allocated.
5433 The following code takes advantage of the fact that optional reloads
5434 are at the end of reload_order. */
5435 if (rld[r].optional != 0)
5436 for (i = 0; i < j; i++)
5437 if ((rld[reload_order[i]].out != 0
5438 || rld[reload_order[i]].in != 0
5439 || rld[reload_order[i]].secondary_p)
5440 && ! rld[reload_order[i]].optional
5441 && rld[reload_order[i]].reg_rtx == 0)
5442 allocate_reload_reg (chain, reload_order[i], 0);
5443 #endif
5445 /* First see if this pseudo is already available as reloaded
5446 for a previous insn. We cannot try to inherit for reloads
5447 that are smaller than the maximum number of registers needed
5448 for groups unless the register we would allocate cannot be used
5449 for the groups.
5451 We could check here to see if this is a secondary reload for
5452 an object that is already in a register of the desired class.
5453 This would avoid the need for the secondary reload register.
5454 But this is complex because we can't easily determine what
5455 objects might want to be loaded via this reload. So let a
5456 register be allocated here. In `emit_reload_insns' we suppress
5457 one of the loads in the case described above. */
5459 if (inheritance)
5461 int byte = 0;
5462 int regno = -1;
5463 enum machine_mode mode = VOIDmode;
5465 if (rld[r].in == 0)
5467 else if (REG_P (rld[r].in))
5469 regno = REGNO (rld[r].in);
5470 mode = GET_MODE (rld[r].in);
5472 else if (REG_P (rld[r].in_reg))
5474 regno = REGNO (rld[r].in_reg);
5475 mode = GET_MODE (rld[r].in_reg);
5477 else if (GET_CODE (rld[r].in_reg) == SUBREG
5478 && REG_P (SUBREG_REG (rld[r].in_reg)))
5480 byte = SUBREG_BYTE (rld[r].in_reg);
5481 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5482 if (regno < FIRST_PSEUDO_REGISTER)
5483 regno = subreg_regno (rld[r].in_reg);
5484 mode = GET_MODE (rld[r].in_reg);
5486 #ifdef AUTO_INC_DEC
5487 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5488 || GET_CODE (rld[r].in_reg) == PRE_DEC
5489 || GET_CODE (rld[r].in_reg) == POST_INC
5490 || GET_CODE (rld[r].in_reg) == POST_DEC)
5491 && REG_P (XEXP (rld[r].in_reg, 0)))
5493 regno = REGNO (XEXP (rld[r].in_reg, 0));
5494 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5495 rld[r].out = rld[r].in;
5497 #endif
5498 #if 0
5499 /* This won't work, since REGNO can be a pseudo reg number.
5500 Also, it takes much more hair to keep track of all the things
5501 that can invalidate an inherited reload of part of a pseudoreg. */
5502 else if (GET_CODE (rld[r].in) == SUBREG
5503 && REG_P (SUBREG_REG (rld[r].in)))
5504 regno = subreg_regno (rld[r].in);
5505 #endif
5507 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5509 enum reg_class class = rld[r].class, last_class;
5510 rtx last_reg = reg_last_reload_reg[regno];
5511 enum machine_mode need_mode;
5513 i = REGNO (last_reg);
5514 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5515 last_class = REGNO_REG_CLASS (i);
5517 if (byte == 0)
5518 need_mode = mode;
5519 else
5520 need_mode
5521 = smallest_mode_for_size (GET_MODE_BITSIZE (mode)
5522 + byte * BITS_PER_UNIT,
5523 GET_MODE_CLASS (mode));
5525 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5526 >= GET_MODE_SIZE (need_mode))
5527 #ifdef CANNOT_CHANGE_MODE_CLASS
5528 /* Verify that the register in "i" can be obtained
5529 from LAST_REG. */
5530 && !REG_CANNOT_CHANGE_MODE_P (REGNO (last_reg),
5531 GET_MODE (last_reg),
5532 mode)
5533 #endif
5534 && reg_reloaded_contents[i] == regno
5535 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5536 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5537 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5538 /* Even if we can't use this register as a reload
5539 register, we might use it for reload_override_in,
5540 if copying it to the desired class is cheap
5541 enough. */
5542 || ((REGISTER_MOVE_COST (mode, last_class, class)
5543 < MEMORY_MOVE_COST (mode, class, 1))
5544 && (secondary_reload_class (1, class, mode,
5545 last_reg)
5546 == NO_REGS)
5547 #ifdef SECONDARY_MEMORY_NEEDED
5548 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5549 mode)
5550 #endif
5553 && (rld[r].nregs == max_group_size
5554 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5556 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5557 rld[r].when_needed, rld[r].in,
5558 const0_rtx, r, 1))
5560 /* If a group is needed, verify that all the subsequent
5561 registers still have their values intact. */
5562 int nr = hard_regno_nregs[i][rld[r].mode];
5563 int k;
5565 for (k = 1; k < nr; k++)
5566 if (reg_reloaded_contents[i + k] != regno
5567 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5568 break;
5570 if (k == nr)
5572 int i1;
5573 int bad_for_class;
5575 last_reg = (GET_MODE (last_reg) == mode
5576 ? last_reg : gen_rtx_REG (mode, i));
5578 bad_for_class = 0;
5579 for (k = 0; k < nr; k++)
5580 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5581 i+k);
5583 /* We found a register that contains the
5584 value we need. If this register is the
5585 same as an `earlyclobber' operand of the
5586 current insn, just mark it as a place to
5587 reload from since we can't use it as the
5588 reload register itself. */
5590 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5591 if (reg_overlap_mentioned_for_reload_p
5592 (reg_last_reload_reg[regno],
5593 reload_earlyclobbers[i1]))
5594 break;
5596 if (i1 != n_earlyclobbers
5597 || ! (free_for_value_p (i, rld[r].mode,
5598 rld[r].opnum,
5599 rld[r].when_needed, rld[r].in,
5600 rld[r].out, r, 1))
5601 /* Don't use it if we'd clobber a pseudo reg. */
5602 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5603 && rld[r].out
5604 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5605 /* Don't clobber the frame pointer. */
5606 || (i == HARD_FRAME_POINTER_REGNUM
5607 && frame_pointer_needed
5608 && rld[r].out)
5609 /* Don't really use the inherited spill reg
5610 if we need it wider than we've got it. */
5611 || (GET_MODE_SIZE (rld[r].mode)
5612 > GET_MODE_SIZE (mode))
5613 || bad_for_class
5615 /* If find_reloads chose reload_out as reload
5616 register, stay with it - that leaves the
5617 inherited register for subsequent reloads. */
5618 || (rld[r].out && rld[r].reg_rtx
5619 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5621 if (! rld[r].optional)
5623 reload_override_in[r] = last_reg;
5624 reload_inheritance_insn[r]
5625 = reg_reloaded_insn[i];
5628 else
5630 int k;
5631 /* We can use this as a reload reg. */
5632 /* Mark the register as in use for this part of
5633 the insn. */
5634 mark_reload_reg_in_use (i,
5635 rld[r].opnum,
5636 rld[r].when_needed,
5637 rld[r].mode);
5638 rld[r].reg_rtx = last_reg;
5639 reload_inherited[r] = 1;
5640 reload_inheritance_insn[r]
5641 = reg_reloaded_insn[i];
5642 reload_spill_index[r] = i;
5643 for (k = 0; k < nr; k++)
5644 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5645 i + k);
5652 /* Here's another way to see if the value is already lying around. */
5653 if (inheritance
5654 && rld[r].in != 0
5655 && ! reload_inherited[r]
5656 && rld[r].out == 0
5657 && (CONSTANT_P (rld[r].in)
5658 || GET_CODE (rld[r].in) == PLUS
5659 || REG_P (rld[r].in)
5660 || MEM_P (rld[r].in))
5661 && (rld[r].nregs == max_group_size
5662 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5663 search_equiv = rld[r].in;
5664 /* If this is an output reload from a simple move insn, look
5665 if an equivalence for the input is available. */
5666 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5668 rtx set = single_set (insn);
5670 if (set
5671 && rtx_equal_p (rld[r].out, SET_DEST (set))
5672 && CONSTANT_P (SET_SRC (set)))
5673 search_equiv = SET_SRC (set);
5676 if (search_equiv)
5678 rtx equiv
5679 = find_equiv_reg (search_equiv, insn, rld[r].class,
5680 -1, NULL, 0, rld[r].mode);
5681 int regno = 0;
5683 if (equiv != 0)
5685 if (REG_P (equiv))
5686 regno = REGNO (equiv);
5687 else
5689 /* This must be a SUBREG of a hard register.
5690 Make a new REG since this might be used in an
5691 address and not all machines support SUBREGs
5692 there. */
5693 gcc_assert (GET_CODE (equiv) == SUBREG);
5694 regno = subreg_regno (equiv);
5695 equiv = gen_rtx_REG (rld[r].mode, regno);
5696 /* If we choose EQUIV as the reload register, but the
5697 loop below decides to cancel the inheritance, we'll
5698 end up reloading EQUIV in rld[r].mode, not the mode
5699 it had originally. That isn't safe when EQUIV isn't
5700 available as a spill register since its value might
5701 still be live at this point. */
5702 for (i = regno; i < regno + (int) rld[r].nregs; i++)
5703 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
5704 equiv = 0;
5708 /* If we found a spill reg, reject it unless it is free
5709 and of the desired class. */
5710 if (equiv != 0)
5712 int regs_used = 0;
5713 int bad_for_class = 0;
5714 int max_regno = regno + rld[r].nregs;
5716 for (i = regno; i < max_regno; i++)
5718 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
5720 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5724 if ((regs_used
5725 && ! free_for_value_p (regno, rld[r].mode,
5726 rld[r].opnum, rld[r].when_needed,
5727 rld[r].in, rld[r].out, r, 1))
5728 || bad_for_class)
5729 equiv = 0;
5732 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5733 equiv = 0;
5735 /* We found a register that contains the value we need.
5736 If this register is the same as an `earlyclobber' operand
5737 of the current insn, just mark it as a place to reload from
5738 since we can't use it as the reload register itself. */
5740 if (equiv != 0)
5741 for (i = 0; i < n_earlyclobbers; i++)
5742 if (reg_overlap_mentioned_for_reload_p (equiv,
5743 reload_earlyclobbers[i]))
5745 if (! rld[r].optional)
5746 reload_override_in[r] = equiv;
5747 equiv = 0;
5748 break;
5751 /* If the equiv register we have found is explicitly clobbered
5752 in the current insn, it depends on the reload type if we
5753 can use it, use it for reload_override_in, or not at all.
5754 In particular, we then can't use EQUIV for a
5755 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5757 if (equiv != 0)
5759 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5760 switch (rld[r].when_needed)
5762 case RELOAD_FOR_OTHER_ADDRESS:
5763 case RELOAD_FOR_INPADDR_ADDRESS:
5764 case RELOAD_FOR_INPUT_ADDRESS:
5765 case RELOAD_FOR_OPADDR_ADDR:
5766 break;
5767 case RELOAD_OTHER:
5768 case RELOAD_FOR_INPUT:
5769 case RELOAD_FOR_OPERAND_ADDRESS:
5770 if (! rld[r].optional)
5771 reload_override_in[r] = equiv;
5772 /* Fall through. */
5773 default:
5774 equiv = 0;
5775 break;
5777 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5778 switch (rld[r].when_needed)
5780 case RELOAD_FOR_OTHER_ADDRESS:
5781 case RELOAD_FOR_INPADDR_ADDRESS:
5782 case RELOAD_FOR_INPUT_ADDRESS:
5783 case RELOAD_FOR_OPADDR_ADDR:
5784 case RELOAD_FOR_OPERAND_ADDRESS:
5785 case RELOAD_FOR_INPUT:
5786 break;
5787 case RELOAD_OTHER:
5788 if (! rld[r].optional)
5789 reload_override_in[r] = equiv;
5790 /* Fall through. */
5791 default:
5792 equiv = 0;
5793 break;
5797 /* If we found an equivalent reg, say no code need be generated
5798 to load it, and use it as our reload reg. */
5799 if (equiv != 0
5800 && (regno != HARD_FRAME_POINTER_REGNUM
5801 || !frame_pointer_needed))
5803 int nr = hard_regno_nregs[regno][rld[r].mode];
5804 int k;
5805 rld[r].reg_rtx = equiv;
5806 reload_inherited[r] = 1;
5808 /* If reg_reloaded_valid is not set for this register,
5809 there might be a stale spill_reg_store lying around.
5810 We must clear it, since otherwise emit_reload_insns
5811 might delete the store. */
5812 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5813 spill_reg_store[regno] = NULL_RTX;
5814 /* If any of the hard registers in EQUIV are spill
5815 registers, mark them as in use for this insn. */
5816 for (k = 0; k < nr; k++)
5818 i = spill_reg_order[regno + k];
5819 if (i >= 0)
5821 mark_reload_reg_in_use (regno, rld[r].opnum,
5822 rld[r].when_needed,
5823 rld[r].mode);
5824 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5825 regno + k);
5831 /* If we found a register to use already, or if this is an optional
5832 reload, we are done. */
5833 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5834 continue;
5836 #if 0
5837 /* No longer needed for correct operation. Might or might
5838 not give better code on the average. Want to experiment? */
5840 /* See if there is a later reload that has a class different from our
5841 class that intersects our class or that requires less register
5842 than our reload. If so, we must allocate a register to this
5843 reload now, since that reload might inherit a previous reload
5844 and take the only available register in our class. Don't do this
5845 for optional reloads since they will force all previous reloads
5846 to be allocated. Also don't do this for reloads that have been
5847 turned off. */
5849 for (i = j + 1; i < n_reloads; i++)
5851 int s = reload_order[i];
5853 if ((rld[s].in == 0 && rld[s].out == 0
5854 && ! rld[s].secondary_p)
5855 || rld[s].optional)
5856 continue;
5858 if ((rld[s].class != rld[r].class
5859 && reg_classes_intersect_p (rld[r].class,
5860 rld[s].class))
5861 || rld[s].nregs < rld[r].nregs)
5862 break;
5865 if (i == n_reloads)
5866 continue;
5868 allocate_reload_reg (chain, r, j == n_reloads - 1);
5869 #endif
5872 /* Now allocate reload registers for anything non-optional that
5873 didn't get one yet. */
5874 for (j = 0; j < n_reloads; j++)
5876 int r = reload_order[j];
5878 /* Ignore reloads that got marked inoperative. */
5879 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
5880 continue;
5882 /* Skip reloads that already have a register allocated or are
5883 optional. */
5884 if (rld[r].reg_rtx != 0 || rld[r].optional)
5885 continue;
5887 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
5888 break;
5891 /* If that loop got all the way, we have won. */
5892 if (j == n_reloads)
5894 win = 1;
5895 break;
5898 /* Loop around and try without any inheritance. */
5901 if (! win)
5903 /* First undo everything done by the failed attempt
5904 to allocate with inheritance. */
5905 choose_reload_regs_init (chain, save_reload_reg_rtx);
5907 /* Some sanity tests to verify that the reloads found in the first
5908 pass are identical to the ones we have now. */
5909 gcc_assert (chain->n_reloads == n_reloads);
5911 for (i = 0; i < n_reloads; i++)
5913 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5914 continue;
5915 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
5916 for (j = 0; j < n_spills; j++)
5917 if (spill_regs[j] == chain->rld[i].regno)
5918 if (! set_reload_reg (j, i))
5919 failed_reload (chain->insn, i);
5923 /* If we thought we could inherit a reload, because it seemed that
5924 nothing else wanted the same reload register earlier in the insn,
5925 verify that assumption, now that all reloads have been assigned.
5926 Likewise for reloads where reload_override_in has been set. */
5928 /* If doing expensive optimizations, do one preliminary pass that doesn't
5929 cancel any inheritance, but removes reloads that have been needed only
5930 for reloads that we know can be inherited. */
5931 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
5933 for (j = 0; j < n_reloads; j++)
5935 int r = reload_order[j];
5936 rtx check_reg;
5937 if (reload_inherited[r] && rld[r].reg_rtx)
5938 check_reg = rld[r].reg_rtx;
5939 else if (reload_override_in[r]
5940 && (REG_P (reload_override_in[r])
5941 || GET_CODE (reload_override_in[r]) == SUBREG))
5942 check_reg = reload_override_in[r];
5943 else
5944 continue;
5945 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5946 rld[r].opnum, rld[r].when_needed, rld[r].in,
5947 (reload_inherited[r]
5948 ? rld[r].out : const0_rtx),
5949 r, 1))
5951 if (pass)
5952 continue;
5953 reload_inherited[r] = 0;
5954 reload_override_in[r] = 0;
5956 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5957 reload_override_in, then we do not need its related
5958 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5959 likewise for other reload types.
5960 We handle this by removing a reload when its only replacement
5961 is mentioned in reload_in of the reload we are going to inherit.
5962 A special case are auto_inc expressions; even if the input is
5963 inherited, we still need the address for the output. We can
5964 recognize them because they have RELOAD_OUT set to RELOAD_IN.
5965 If we succeeded removing some reload and we are doing a preliminary
5966 pass just to remove such reloads, make another pass, since the
5967 removal of one reload might allow us to inherit another one. */
5968 else if (rld[r].in
5969 && rld[r].out != rld[r].in
5970 && remove_address_replacements (rld[r].in) && pass)
5971 pass = 2;
5975 /* Now that reload_override_in is known valid,
5976 actually override reload_in. */
5977 for (j = 0; j < n_reloads; j++)
5978 if (reload_override_in[j])
5979 rld[j].in = reload_override_in[j];
5981 /* If this reload won't be done because it has been canceled or is
5982 optional and not inherited, clear reload_reg_rtx so other
5983 routines (such as subst_reloads) don't get confused. */
5984 for (j = 0; j < n_reloads; j++)
5985 if (rld[j].reg_rtx != 0
5986 && ((rld[j].optional && ! reload_inherited[j])
5987 || (rld[j].in == 0 && rld[j].out == 0
5988 && ! rld[j].secondary_p)))
5990 int regno = true_regnum (rld[j].reg_rtx);
5992 if (spill_reg_order[regno] >= 0)
5993 clear_reload_reg_in_use (regno, rld[j].opnum,
5994 rld[j].when_needed, rld[j].mode);
5995 rld[j].reg_rtx = 0;
5996 reload_spill_index[j] = -1;
5999 /* Record which pseudos and which spill regs have output reloads. */
6000 for (j = 0; j < n_reloads; j++)
6002 int r = reload_order[j];
6004 i = reload_spill_index[r];
6006 /* I is nonneg if this reload uses a register.
6007 If rld[r].reg_rtx is 0, this is an optional reload
6008 that we opted to ignore. */
6009 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
6010 && rld[r].reg_rtx != 0)
6012 int nregno = REGNO (rld[r].out_reg);
6013 int nr = 1;
6015 if (nregno < FIRST_PSEUDO_REGISTER)
6016 nr = hard_regno_nregs[nregno][rld[r].mode];
6018 while (--nr >= 0)
6019 reg_has_output_reload[nregno + nr] = 1;
6021 if (i >= 0)
6023 nr = hard_regno_nregs[i][rld[r].mode];
6024 while (--nr >= 0)
6025 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6028 gcc_assert (rld[r].when_needed == RELOAD_OTHER
6029 || rld[r].when_needed == RELOAD_FOR_OUTPUT
6030 || rld[r].when_needed == RELOAD_FOR_INSN);
6035 /* Deallocate the reload register for reload R. This is called from
6036 remove_address_replacements. */
6038 void
6039 deallocate_reload_reg (int r)
6041 int regno;
6043 if (! rld[r].reg_rtx)
6044 return;
6045 regno = true_regnum (rld[r].reg_rtx);
6046 rld[r].reg_rtx = 0;
6047 if (spill_reg_order[regno] >= 0)
6048 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6049 rld[r].mode);
6050 reload_spill_index[r] = -1;
6053 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
6054 reloads of the same item for fear that we might not have enough reload
6055 registers. However, normally they will get the same reload register
6056 and hence actually need not be loaded twice.
6058 Here we check for the most common case of this phenomenon: when we have
6059 a number of reloads for the same object, each of which were allocated
6060 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6061 reload, and is not modified in the insn itself. If we find such,
6062 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6063 This will not increase the number of spill registers needed and will
6064 prevent redundant code. */
6066 static void
6067 merge_assigned_reloads (rtx insn)
6069 int i, j;
6071 /* Scan all the reloads looking for ones that only load values and
6072 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6073 assigned and not modified by INSN. */
6075 for (i = 0; i < n_reloads; i++)
6077 int conflicting_input = 0;
6078 int max_input_address_opnum = -1;
6079 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6081 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6082 || rld[i].out != 0 || rld[i].reg_rtx == 0
6083 || reg_set_p (rld[i].reg_rtx, insn))
6084 continue;
6086 /* Look at all other reloads. Ensure that the only use of this
6087 reload_reg_rtx is in a reload that just loads the same value
6088 as we do. Note that any secondary reloads must be of the identical
6089 class since the values, modes, and result registers are the
6090 same, so we need not do anything with any secondary reloads. */
6092 for (j = 0; j < n_reloads; j++)
6094 if (i == j || rld[j].reg_rtx == 0
6095 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6096 rld[i].reg_rtx))
6097 continue;
6099 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6100 && rld[j].opnum > max_input_address_opnum)
6101 max_input_address_opnum = rld[j].opnum;
6103 /* If the reload regs aren't exactly the same (e.g, different modes)
6104 or if the values are different, we can't merge this reload.
6105 But if it is an input reload, we might still merge
6106 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6108 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6109 || rld[j].out != 0 || rld[j].in == 0
6110 || ! rtx_equal_p (rld[i].in, rld[j].in))
6112 if (rld[j].when_needed != RELOAD_FOR_INPUT
6113 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6114 || rld[i].opnum > rld[j].opnum)
6115 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6116 break;
6117 conflicting_input = 1;
6118 if (min_conflicting_input_opnum > rld[j].opnum)
6119 min_conflicting_input_opnum = rld[j].opnum;
6123 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6124 we, in fact, found any matching reloads. */
6126 if (j == n_reloads
6127 && max_input_address_opnum <= min_conflicting_input_opnum)
6129 gcc_assert (rld[i].when_needed != RELOAD_FOR_OUTPUT);
6131 for (j = 0; j < n_reloads; j++)
6132 if (i != j && rld[j].reg_rtx != 0
6133 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6134 && (! conflicting_input
6135 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6136 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6138 rld[i].when_needed = RELOAD_OTHER;
6139 rld[j].in = 0;
6140 reload_spill_index[j] = -1;
6141 transfer_replacements (i, j);
6144 /* If this is now RELOAD_OTHER, look for any reloads that load
6145 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6146 if they were for inputs, RELOAD_OTHER for outputs. Note that
6147 this test is equivalent to looking for reloads for this operand
6148 number. */
6149 /* We must take special care with RELOAD_FOR_OUTPUT_ADDRESS; it may
6150 share registers with a RELOAD_FOR_INPUT, so we can not change it
6151 to RELOAD_FOR_OTHER_ADDRESS. We should never need to, since we
6152 do not modify RELOAD_FOR_OUTPUT. */
6154 if (rld[i].when_needed == RELOAD_OTHER)
6155 for (j = 0; j < n_reloads; j++)
6156 if (rld[j].in != 0
6157 && rld[j].when_needed != RELOAD_OTHER
6158 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6159 && rld[j].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
6160 && (! conflicting_input
6161 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6162 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6163 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6164 rld[i].in))
6166 int k;
6168 rld[j].when_needed
6169 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6170 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6171 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6173 /* Check to see if we accidentally converted two
6174 reloads that use the same reload register with
6175 different inputs to the same type. If so, the
6176 resulting code won't work. */
6177 if (rld[j].reg_rtx)
6178 for (k = 0; k < j; k++)
6179 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6180 || rld[k].when_needed != rld[j].when_needed
6181 || !rtx_equal_p (rld[k].reg_rtx,
6182 rld[j].reg_rtx)
6183 || rtx_equal_p (rld[k].in,
6184 rld[j].in));
6190 /* These arrays are filled by emit_reload_insns and its subroutines. */
6191 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6192 static rtx other_input_address_reload_insns = 0;
6193 static rtx other_input_reload_insns = 0;
6194 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6195 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6196 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6197 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6198 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6199 static rtx operand_reload_insns = 0;
6200 static rtx other_operand_reload_insns = 0;
6201 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6203 /* Values to be put in spill_reg_store are put here first. */
6204 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6205 static HARD_REG_SET reg_reloaded_died;
6207 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
6208 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
6209 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
6210 adjusted register, and return true. Otherwise, return false. */
6211 static bool
6212 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
6213 enum reg_class new_class,
6214 enum machine_mode new_mode)
6217 rtx reg;
6219 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
6221 unsigned regno = REGNO (reg);
6223 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
6224 continue;
6225 if (GET_MODE (reg) != new_mode)
6227 if (!HARD_REGNO_MODE_OK (regno, new_mode))
6228 continue;
6229 if (hard_regno_nregs[regno][new_mode]
6230 > hard_regno_nregs[regno][GET_MODE (reg)])
6231 continue;
6232 reg = reload_adjust_reg_for_mode (reg, new_mode);
6234 *reload_reg = reg;
6235 return true;
6237 return false;
6240 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
6241 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
6242 nonzero, if that is suitable. On success, change *RELOAD_REG to the
6243 adjusted register, and return true. Otherwise, return false. */
6244 static bool
6245 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
6246 enum insn_code icode)
6249 enum reg_class new_class = scratch_reload_class (icode);
6250 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
6252 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
6253 new_class, new_mode);
6256 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6257 has the number J. OLD contains the value to be used as input. */
6259 static void
6260 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6261 rtx old, int j)
6263 rtx insn = chain->insn;
6264 rtx reloadreg = rl->reg_rtx;
6265 rtx oldequiv_reg = 0;
6266 rtx oldequiv = 0;
6267 int special = 0;
6268 enum machine_mode mode;
6269 rtx *where;
6271 /* Determine the mode to reload in.
6272 This is very tricky because we have three to choose from.
6273 There is the mode the insn operand wants (rl->inmode).
6274 There is the mode of the reload register RELOADREG.
6275 There is the intrinsic mode of the operand, which we could find
6276 by stripping some SUBREGs.
6277 It turns out that RELOADREG's mode is irrelevant:
6278 we can change that arbitrarily.
6280 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6281 then the reload reg may not support QImode moves, so use SImode.
6282 If foo is in memory due to spilling a pseudo reg, this is safe,
6283 because the QImode value is in the least significant part of a
6284 slot big enough for a SImode. If foo is some other sort of
6285 memory reference, then it is impossible to reload this case,
6286 so previous passes had better make sure this never happens.
6288 Then consider a one-word union which has SImode and one of its
6289 members is a float, being fetched as (SUBREG:SF union:SI).
6290 We must fetch that as SFmode because we could be loading into
6291 a float-only register. In this case OLD's mode is correct.
6293 Consider an immediate integer: it has VOIDmode. Here we need
6294 to get a mode from something else.
6296 In some cases, there is a fourth mode, the operand's
6297 containing mode. If the insn specifies a containing mode for
6298 this operand, it overrides all others.
6300 I am not sure whether the algorithm here is always right,
6301 but it does the right things in those cases. */
6303 mode = GET_MODE (old);
6304 if (mode == VOIDmode)
6305 mode = rl->inmode;
6307 /* If we need a secondary register for this operation, see if
6308 the value is already in a register in that class. Don't
6309 do this if the secondary register will be used as a scratch
6310 register. */
6312 if (rl->secondary_in_reload >= 0
6313 && rl->secondary_in_icode == CODE_FOR_nothing
6314 && optimize)
6315 oldequiv
6316 = find_equiv_reg (old, insn,
6317 rld[rl->secondary_in_reload].class,
6318 -1, NULL, 0, mode);
6320 /* If reloading from memory, see if there is a register
6321 that already holds the same value. If so, reload from there.
6322 We can pass 0 as the reload_reg_p argument because
6323 any other reload has either already been emitted,
6324 in which case find_equiv_reg will see the reload-insn,
6325 or has yet to be emitted, in which case it doesn't matter
6326 because we will use this equiv reg right away. */
6328 if (oldequiv == 0 && optimize
6329 && (MEM_P (old)
6330 || (REG_P (old)
6331 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6332 && reg_renumber[REGNO (old)] < 0)))
6333 oldequiv = find_equiv_reg (old, insn, ALL_REGS, -1, NULL, 0, mode);
6335 if (oldequiv)
6337 unsigned int regno = true_regnum (oldequiv);
6339 /* Don't use OLDEQUIV if any other reload changes it at an
6340 earlier stage of this insn or at this stage. */
6341 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6342 rl->in, const0_rtx, j, 0))
6343 oldequiv = 0;
6345 /* If it is no cheaper to copy from OLDEQUIV into the
6346 reload register than it would be to move from memory,
6347 don't use it. Likewise, if we need a secondary register
6348 or memory. */
6350 if (oldequiv != 0
6351 && (((enum reg_class) REGNO_REG_CLASS (regno) != rl->class
6352 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6353 rl->class)
6354 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6355 || (secondary_reload_class (1, rl->class, mode, oldequiv)
6356 != NO_REGS)
6357 #ifdef SECONDARY_MEMORY_NEEDED
6358 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6359 rl->class,
6360 mode)
6361 #endif
6363 oldequiv = 0;
6366 /* delete_output_reload is only invoked properly if old contains
6367 the original pseudo register. Since this is replaced with a
6368 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6369 find the pseudo in RELOAD_IN_REG. */
6370 if (oldequiv == 0
6371 && reload_override_in[j]
6372 && REG_P (rl->in_reg))
6374 oldequiv = old;
6375 old = rl->in_reg;
6377 if (oldequiv == 0)
6378 oldequiv = old;
6379 else if (REG_P (oldequiv))
6380 oldequiv_reg = oldequiv;
6381 else if (GET_CODE (oldequiv) == SUBREG)
6382 oldequiv_reg = SUBREG_REG (oldequiv);
6384 /* If we are reloading from a register that was recently stored in
6385 with an output-reload, see if we can prove there was
6386 actually no need to store the old value in it. */
6388 if (optimize && REG_P (oldequiv)
6389 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6390 && spill_reg_store[REGNO (oldequiv)]
6391 && REG_P (old)
6392 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6393 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6394 rl->out_reg)))
6395 delete_output_reload (insn, j, REGNO (oldequiv));
6397 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6398 then load RELOADREG from OLDEQUIV. Note that we cannot use
6399 gen_lowpart_common since it can do the wrong thing when
6400 RELOADREG has a multi-word mode. Note that RELOADREG
6401 must always be a REG here. */
6403 if (GET_MODE (reloadreg) != mode)
6404 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6405 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6406 oldequiv = SUBREG_REG (oldequiv);
6407 if (GET_MODE (oldequiv) != VOIDmode
6408 && mode != GET_MODE (oldequiv))
6409 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6411 /* Switch to the right place to emit the reload insns. */
6412 switch (rl->when_needed)
6414 case RELOAD_OTHER:
6415 where = &other_input_reload_insns;
6416 break;
6417 case RELOAD_FOR_INPUT:
6418 where = &input_reload_insns[rl->opnum];
6419 break;
6420 case RELOAD_FOR_INPUT_ADDRESS:
6421 where = &input_address_reload_insns[rl->opnum];
6422 break;
6423 case RELOAD_FOR_INPADDR_ADDRESS:
6424 where = &inpaddr_address_reload_insns[rl->opnum];
6425 break;
6426 case RELOAD_FOR_OUTPUT_ADDRESS:
6427 where = &output_address_reload_insns[rl->opnum];
6428 break;
6429 case RELOAD_FOR_OUTADDR_ADDRESS:
6430 where = &outaddr_address_reload_insns[rl->opnum];
6431 break;
6432 case RELOAD_FOR_OPERAND_ADDRESS:
6433 where = &operand_reload_insns;
6434 break;
6435 case RELOAD_FOR_OPADDR_ADDR:
6436 where = &other_operand_reload_insns;
6437 break;
6438 case RELOAD_FOR_OTHER_ADDRESS:
6439 where = &other_input_address_reload_insns;
6440 break;
6441 default:
6442 gcc_unreachable ();
6445 push_to_sequence (*where);
6447 /* Auto-increment addresses must be reloaded in a special way. */
6448 if (rl->out && ! rl->out_reg)
6450 /* We are not going to bother supporting the case where a
6451 incremented register can't be copied directly from
6452 OLDEQUIV since this seems highly unlikely. */
6453 gcc_assert (rl->secondary_in_reload < 0);
6455 if (reload_inherited[j])
6456 oldequiv = reloadreg;
6458 old = XEXP (rl->in_reg, 0);
6460 if (optimize && REG_P (oldequiv)
6461 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6462 && spill_reg_store[REGNO (oldequiv)]
6463 && REG_P (old)
6464 && (dead_or_set_p (insn,
6465 spill_reg_stored_to[REGNO (oldequiv)])
6466 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6467 old)))
6468 delete_output_reload (insn, j, REGNO (oldequiv));
6470 /* Prevent normal processing of this reload. */
6471 special = 1;
6472 /* Output a special code sequence for this case. */
6473 new_spill_reg_store[REGNO (reloadreg)]
6474 = inc_for_reload (reloadreg, oldequiv, rl->out,
6475 rl->inc);
6478 /* If we are reloading a pseudo-register that was set by the previous
6479 insn, see if we can get rid of that pseudo-register entirely
6480 by redirecting the previous insn into our reload register. */
6482 else if (optimize && REG_P (old)
6483 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6484 && dead_or_set_p (insn, old)
6485 /* This is unsafe if some other reload
6486 uses the same reg first. */
6487 && ! conflicts_with_override (reloadreg)
6488 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6489 rl->when_needed, old, rl->out, j, 0))
6491 rtx temp = PREV_INSN (insn);
6492 while (temp && NOTE_P (temp))
6493 temp = PREV_INSN (temp);
6494 if (temp
6495 && NONJUMP_INSN_P (temp)
6496 && GET_CODE (PATTERN (temp)) == SET
6497 && SET_DEST (PATTERN (temp)) == old
6498 /* Make sure we can access insn_operand_constraint. */
6499 && asm_noperands (PATTERN (temp)) < 0
6500 /* This is unsafe if operand occurs more than once in current
6501 insn. Perhaps some occurrences aren't reloaded. */
6502 && count_occurrences (PATTERN (insn), old, 0) == 1)
6504 rtx old = SET_DEST (PATTERN (temp));
6505 /* Store into the reload register instead of the pseudo. */
6506 SET_DEST (PATTERN (temp)) = reloadreg;
6508 /* Verify that resulting insn is valid. */
6509 extract_insn (temp);
6510 if (constrain_operands (1))
6512 /* If the previous insn is an output reload, the source is
6513 a reload register, and its spill_reg_store entry will
6514 contain the previous destination. This is now
6515 invalid. */
6516 if (REG_P (SET_SRC (PATTERN (temp)))
6517 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6519 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6520 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6523 /* If these are the only uses of the pseudo reg,
6524 pretend for GDB it lives in the reload reg we used. */
6525 if (REG_N_DEATHS (REGNO (old)) == 1
6526 && REG_N_SETS (REGNO (old)) == 1)
6528 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6529 alter_reg (REGNO (old), -1);
6531 special = 1;
6533 else
6535 SET_DEST (PATTERN (temp)) = old;
6540 /* We can't do that, so output an insn to load RELOADREG. */
6542 /* If we have a secondary reload, pick up the secondary register
6543 and icode, if any. If OLDEQUIV and OLD are different or
6544 if this is an in-out reload, recompute whether or not we
6545 still need a secondary register and what the icode should
6546 be. If we still need a secondary register and the class or
6547 icode is different, go back to reloading from OLD if using
6548 OLDEQUIV means that we got the wrong type of register. We
6549 cannot have different class or icode due to an in-out reload
6550 because we don't make such reloads when both the input and
6551 output need secondary reload registers. */
6553 if (! special && rl->secondary_in_reload >= 0)
6555 rtx second_reload_reg = 0;
6556 rtx third_reload_reg = 0;
6557 int secondary_reload = rl->secondary_in_reload;
6558 rtx real_oldequiv = oldequiv;
6559 rtx real_old = old;
6560 rtx tmp;
6561 enum insn_code icode;
6562 enum insn_code tertiary_icode = CODE_FOR_nothing;
6564 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6565 and similarly for OLD.
6566 See comments in get_secondary_reload in reload.c. */
6567 /* If it is a pseudo that cannot be replaced with its
6568 equivalent MEM, we must fall back to reload_in, which
6569 will have all the necessary substitutions registered.
6570 Likewise for a pseudo that can't be replaced with its
6571 equivalent constant.
6573 Take extra care for subregs of such pseudos. Note that
6574 we cannot use reg_equiv_mem in this case because it is
6575 not in the right mode. */
6577 tmp = oldequiv;
6578 if (GET_CODE (tmp) == SUBREG)
6579 tmp = SUBREG_REG (tmp);
6580 if (REG_P (tmp)
6581 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6582 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6583 || reg_equiv_constant[REGNO (tmp)] != 0))
6585 if (! reg_equiv_mem[REGNO (tmp)]
6586 || num_not_at_initial_offset
6587 || GET_CODE (oldequiv) == SUBREG)
6588 real_oldequiv = rl->in;
6589 else
6590 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6593 tmp = old;
6594 if (GET_CODE (tmp) == SUBREG)
6595 tmp = SUBREG_REG (tmp);
6596 if (REG_P (tmp)
6597 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6598 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6599 || reg_equiv_constant[REGNO (tmp)] != 0))
6601 if (! reg_equiv_mem[REGNO (tmp)]
6602 || num_not_at_initial_offset
6603 || GET_CODE (old) == SUBREG)
6604 real_old = rl->in;
6605 else
6606 real_old = reg_equiv_mem[REGNO (tmp)];
6609 second_reload_reg = rld[secondary_reload].reg_rtx;
6610 if (rld[secondary_reload].secondary_in_reload >= 0)
6612 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
6614 third_reload_reg = rld[tertiary_reload].reg_rtx;
6615 tertiary_icode = rld[secondary_reload].secondary_in_icode;
6616 /* We'd have to add more code for quartary reloads. */
6617 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
6619 icode = rl->secondary_in_icode;
6621 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6622 || (rl->in != 0 && rl->out != 0))
6624 secondary_reload_info sri, sri2;
6625 enum reg_class new_class, new_t_class;
6627 sri.icode = CODE_FOR_nothing;
6628 sri.prev_sri = NULL;
6629 new_class = targetm.secondary_reload (1, real_oldequiv, rl->class,
6630 mode, &sri);
6632 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
6633 second_reload_reg = 0;
6634 else if (new_class == NO_REGS)
6636 if (reload_adjust_reg_for_icode (&second_reload_reg,
6637 third_reload_reg, sri.icode))
6638 icode = sri.icode, third_reload_reg = 0;
6639 else
6640 oldequiv = old, real_oldequiv = real_old;
6642 else if (sri.icode != CODE_FOR_nothing)
6643 /* We currently lack a way to express this in reloads. */
6644 gcc_unreachable ();
6645 else
6647 sri2.icode = CODE_FOR_nothing;
6648 sri2.prev_sri = &sri;
6649 new_t_class = targetm.secondary_reload (1, real_oldequiv,
6650 new_class, mode, &sri);
6651 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
6653 if (reload_adjust_reg_for_temp (&second_reload_reg,
6654 third_reload_reg,
6655 new_class, mode))
6656 third_reload_reg = 0, tertiary_icode = sri2.icode;
6657 else
6658 oldequiv = old, real_oldequiv = real_old;
6660 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
6662 rtx intermediate = second_reload_reg;
6664 if (reload_adjust_reg_for_temp (&intermediate, NULL,
6665 new_class, mode)
6666 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
6667 sri2.icode))
6669 second_reload_reg = intermediate;
6670 tertiary_icode = sri2.icode;
6672 else
6673 oldequiv = old, real_oldequiv = real_old;
6675 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
6677 rtx intermediate = second_reload_reg;
6679 if (reload_adjust_reg_for_temp (&intermediate, NULL,
6680 new_class, mode)
6681 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
6682 new_t_class, mode))
6684 second_reload_reg = intermediate;
6685 tertiary_icode = sri2.icode;
6687 else
6688 oldequiv = old, real_oldequiv = real_old;
6690 else
6691 /* This could be handled more intelligently too. */
6692 oldequiv = old, real_oldequiv = real_old;
6696 /* If we still need a secondary reload register, check
6697 to see if it is being used as a scratch or intermediate
6698 register and generate code appropriately. If we need
6699 a scratch register, use REAL_OLDEQUIV since the form of
6700 the insn may depend on the actual address if it is
6701 a MEM. */
6703 if (second_reload_reg)
6705 if (icode != CODE_FOR_nothing)
6707 /* We'd have to add extra code to handle this case. */
6708 gcc_assert (!third_reload_reg);
6710 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6711 second_reload_reg));
6712 special = 1;
6714 else
6716 /* See if we need a scratch register to load the
6717 intermediate register (a tertiary reload). */
6718 if (tertiary_icode != CODE_FOR_nothing)
6720 emit_insn ((GEN_FCN (tertiary_icode)
6721 (second_reload_reg, real_oldequiv,
6722 third_reload_reg)));
6724 else if (third_reload_reg)
6726 gen_reload (third_reload_reg, real_oldequiv,
6727 rl->opnum,
6728 rl->when_needed);
6729 gen_reload (second_reload_reg, third_reload_reg,
6730 rl->opnum,
6731 rl->when_needed);
6733 else
6734 gen_reload (second_reload_reg, real_oldequiv,
6735 rl->opnum,
6736 rl->when_needed);
6738 oldequiv = second_reload_reg;
6743 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6745 rtx real_oldequiv = oldequiv;
6747 if ((REG_P (oldequiv)
6748 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6749 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6750 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6751 || (GET_CODE (oldequiv) == SUBREG
6752 && REG_P (SUBREG_REG (oldequiv))
6753 && (REGNO (SUBREG_REG (oldequiv))
6754 >= FIRST_PSEUDO_REGISTER)
6755 && ((reg_equiv_memory_loc
6756 [REGNO (SUBREG_REG (oldequiv))] != 0)
6757 || (reg_equiv_constant
6758 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6759 || (CONSTANT_P (oldequiv)
6760 && (PREFERRED_RELOAD_CLASS (oldequiv,
6761 REGNO_REG_CLASS (REGNO (reloadreg)))
6762 == NO_REGS)))
6763 real_oldequiv = rl->in;
6764 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6765 rl->when_needed);
6768 if (flag_non_call_exceptions)
6769 copy_eh_notes (insn, get_insns ());
6771 /* End this sequence. */
6772 *where = get_insns ();
6773 end_sequence ();
6775 /* Update reload_override_in so that delete_address_reloads_1
6776 can see the actual register usage. */
6777 if (oldequiv_reg)
6778 reload_override_in[j] = oldequiv;
6781 /* Generate insns to for the output reload RL, which is for the insn described
6782 by CHAIN and has the number J. */
6783 static void
6784 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
6785 int j)
6787 rtx reloadreg = rl->reg_rtx;
6788 rtx insn = chain->insn;
6789 int special = 0;
6790 rtx old = rl->out;
6791 enum machine_mode mode = GET_MODE (old);
6792 rtx p;
6794 if (rl->when_needed == RELOAD_OTHER)
6795 start_sequence ();
6796 else
6797 push_to_sequence (output_reload_insns[rl->opnum]);
6799 /* Determine the mode to reload in.
6800 See comments above (for input reloading). */
6802 if (mode == VOIDmode)
6804 /* VOIDmode should never happen for an output. */
6805 if (asm_noperands (PATTERN (insn)) < 0)
6806 /* It's the compiler's fault. */
6807 fatal_insn ("VOIDmode on an output", insn);
6808 error_for_asm (insn, "output operand is constant in %<asm%>");
6809 /* Prevent crash--use something we know is valid. */
6810 mode = word_mode;
6811 old = gen_rtx_REG (mode, REGNO (reloadreg));
6814 if (GET_MODE (reloadreg) != mode)
6815 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6817 /* If we need two reload regs, set RELOADREG to the intermediate
6818 one, since it will be stored into OLD. We might need a secondary
6819 register only for an input reload, so check again here. */
6821 if (rl->secondary_out_reload >= 0)
6823 rtx real_old = old;
6824 int secondary_reload = rl->secondary_out_reload;
6825 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
6827 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
6828 && reg_equiv_mem[REGNO (old)] != 0)
6829 real_old = reg_equiv_mem[REGNO (old)];
6831 if (secondary_reload_class (0, rl->class, mode, real_old) != NO_REGS)
6833 rtx second_reloadreg = reloadreg;
6834 reloadreg = rld[secondary_reload].reg_rtx;
6836 /* See if RELOADREG is to be used as a scratch register
6837 or as an intermediate register. */
6838 if (rl->secondary_out_icode != CODE_FOR_nothing)
6840 /* We'd have to add extra code to handle this case. */
6841 gcc_assert (tertiary_reload < 0);
6843 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6844 (real_old, second_reloadreg, reloadreg)));
6845 special = 1;
6847 else
6849 /* See if we need both a scratch and intermediate reload
6850 register. */
6852 enum insn_code tertiary_icode
6853 = rld[secondary_reload].secondary_out_icode;
6855 /* We'd have to add more code for quartary reloads. */
6856 gcc_assert (tertiary_reload < 0
6857 || rld[tertiary_reload].secondary_out_reload < 0);
6859 if (GET_MODE (reloadreg) != mode)
6860 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6862 if (tertiary_icode != CODE_FOR_nothing)
6864 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
6865 rtx tem;
6867 /* Copy primary reload reg to secondary reload reg.
6868 (Note that these have been swapped above, then
6869 secondary reload reg to OLD using our insn.) */
6871 /* If REAL_OLD is a paradoxical SUBREG, remove it
6872 and try to put the opposite SUBREG on
6873 RELOADREG. */
6874 if (GET_CODE (real_old) == SUBREG
6875 && (GET_MODE_SIZE (GET_MODE (real_old))
6876 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6877 && 0 != (tem = gen_lowpart_common
6878 (GET_MODE (SUBREG_REG (real_old)),
6879 reloadreg)))
6880 real_old = SUBREG_REG (real_old), reloadreg = tem;
6882 gen_reload (reloadreg, second_reloadreg,
6883 rl->opnum, rl->when_needed);
6884 emit_insn ((GEN_FCN (tertiary_icode)
6885 (real_old, reloadreg, third_reloadreg)));
6886 special = 1;
6889 else
6891 /* Copy between the reload regs here and then to
6892 OUT later. */
6894 gen_reload (reloadreg, second_reloadreg,
6895 rl->opnum, rl->when_needed);
6896 if (tertiary_reload >= 0)
6898 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
6900 gen_reload (third_reloadreg, reloadreg,
6901 rl->opnum, rl->when_needed);
6902 reloadreg = third_reloadreg;
6909 /* Output the last reload insn. */
6910 if (! special)
6912 rtx set;
6914 /* Don't output the last reload if OLD is not the dest of
6915 INSN and is in the src and is clobbered by INSN. */
6916 if (! flag_expensive_optimizations
6917 || !REG_P (old)
6918 || !(set = single_set (insn))
6919 || rtx_equal_p (old, SET_DEST (set))
6920 || !reg_mentioned_p (old, SET_SRC (set))
6921 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
6922 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
6923 gen_reload (old, reloadreg, rl->opnum,
6924 rl->when_needed);
6927 /* Look at all insns we emitted, just to be safe. */
6928 for (p = get_insns (); p; p = NEXT_INSN (p))
6929 if (INSN_P (p))
6931 rtx pat = PATTERN (p);
6933 /* If this output reload doesn't come from a spill reg,
6934 clear any memory of reloaded copies of the pseudo reg.
6935 If this output reload comes from a spill reg,
6936 reg_has_output_reload will make this do nothing. */
6937 note_stores (pat, forget_old_reloads_1, NULL);
6939 if (reg_mentioned_p (rl->reg_rtx, pat))
6941 rtx set = single_set (insn);
6942 if (reload_spill_index[j] < 0
6943 && set
6944 && SET_SRC (set) == rl->reg_rtx)
6946 int src = REGNO (SET_SRC (set));
6948 reload_spill_index[j] = src;
6949 SET_HARD_REG_BIT (reg_is_output_reload, src);
6950 if (find_regno_note (insn, REG_DEAD, src))
6951 SET_HARD_REG_BIT (reg_reloaded_died, src);
6953 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6955 int s = rl->secondary_out_reload;
6956 set = single_set (p);
6957 /* If this reload copies only to the secondary reload
6958 register, the secondary reload does the actual
6959 store. */
6960 if (s >= 0 && set == NULL_RTX)
6961 /* We can't tell what function the secondary reload
6962 has and where the actual store to the pseudo is
6963 made; leave new_spill_reg_store alone. */
6965 else if (s >= 0
6966 && SET_SRC (set) == rl->reg_rtx
6967 && SET_DEST (set) == rld[s].reg_rtx)
6969 /* Usually the next instruction will be the
6970 secondary reload insn; if we can confirm
6971 that it is, setting new_spill_reg_store to
6972 that insn will allow an extra optimization. */
6973 rtx s_reg = rld[s].reg_rtx;
6974 rtx next = NEXT_INSN (p);
6975 rld[s].out = rl->out;
6976 rld[s].out_reg = rl->out_reg;
6977 set = single_set (next);
6978 if (set && SET_SRC (set) == s_reg
6979 && ! new_spill_reg_store[REGNO (s_reg)])
6981 SET_HARD_REG_BIT (reg_is_output_reload,
6982 REGNO (s_reg));
6983 new_spill_reg_store[REGNO (s_reg)] = next;
6986 else
6987 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6992 if (rl->when_needed == RELOAD_OTHER)
6994 emit_insn (other_output_reload_insns[rl->opnum]);
6995 other_output_reload_insns[rl->opnum] = get_insns ();
6997 else
6998 output_reload_insns[rl->opnum] = get_insns ();
7000 if (flag_non_call_exceptions)
7001 copy_eh_notes (insn, get_insns ());
7003 end_sequence ();
7006 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7007 and has the number J. */
7008 static void
7009 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7011 rtx insn = chain->insn;
7012 rtx old = (rl->in && MEM_P (rl->in)
7013 ? rl->in_reg : rl->in);
7015 if (old != 0
7016 /* AUTO_INC reloads need to be handled even if inherited. We got an
7017 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7018 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7019 && ! rtx_equal_p (rl->reg_rtx, old)
7020 && rl->reg_rtx != 0)
7021 emit_input_reload_insns (chain, rld + j, old, j);
7023 /* When inheriting a wider reload, we have a MEM in rl->in,
7024 e.g. inheriting a SImode output reload for
7025 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7026 if (optimize && reload_inherited[j] && rl->in
7027 && MEM_P (rl->in)
7028 && MEM_P (rl->in_reg)
7029 && reload_spill_index[j] >= 0
7030 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7031 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7033 /* If we are reloading a register that was recently stored in with an
7034 output-reload, see if we can prove there was
7035 actually no need to store the old value in it. */
7037 if (optimize
7038 /* Only attempt this for input reloads; for RELOAD_OTHER we miss
7039 that there may be multiple uses of the previous output reload.
7040 Restricting to RELOAD_FOR_INPUT is mostly paranoia. */
7041 && rl->when_needed == RELOAD_FOR_INPUT
7042 && (reload_inherited[j] || reload_override_in[j])
7043 && rl->reg_rtx
7044 && REG_P (rl->reg_rtx)
7045 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
7046 #if 0
7047 /* There doesn't seem to be any reason to restrict this to pseudos
7048 and doing so loses in the case where we are copying from a
7049 register of the wrong class. */
7050 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
7051 >= FIRST_PSEUDO_REGISTER)
7052 #endif
7053 /* The insn might have already some references to stackslots
7054 replaced by MEMs, while reload_out_reg still names the
7055 original pseudo. */
7056 && (dead_or_set_p (insn,
7057 spill_reg_stored_to[REGNO (rl->reg_rtx)])
7058 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
7059 rl->out_reg)))
7060 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
7063 /* Do output reloading for reload RL, which is for the insn described by
7064 CHAIN and has the number J.
7065 ??? At some point we need to support handling output reloads of
7066 JUMP_INSNs or insns that set cc0. */
7067 static void
7068 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7070 rtx note, old;
7071 rtx insn = chain->insn;
7072 /* If this is an output reload that stores something that is
7073 not loaded in this same reload, see if we can eliminate a previous
7074 store. */
7075 rtx pseudo = rl->out_reg;
7077 if (pseudo
7078 && optimize
7079 && REG_P (pseudo)
7080 && ! rtx_equal_p (rl->in_reg, pseudo)
7081 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7082 && reg_last_reload_reg[REGNO (pseudo)])
7084 int pseudo_no = REGNO (pseudo);
7085 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7087 /* We don't need to test full validity of last_regno for
7088 inherit here; we only want to know if the store actually
7089 matches the pseudo. */
7090 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7091 && reg_reloaded_contents[last_regno] == pseudo_no
7092 && spill_reg_store[last_regno]
7093 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7094 delete_output_reload (insn, j, last_regno);
7097 old = rl->out_reg;
7098 if (old == 0
7099 || rl->reg_rtx == old
7100 || rl->reg_rtx == 0)
7101 return;
7103 /* An output operand that dies right away does need a reload,
7104 but need not be copied from it. Show the new location in the
7105 REG_UNUSED note. */
7106 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7107 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7109 XEXP (note, 0) = rl->reg_rtx;
7110 return;
7112 /* Likewise for a SUBREG of an operand that dies. */
7113 else if (GET_CODE (old) == SUBREG
7114 && REG_P (SUBREG_REG (old))
7115 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7116 SUBREG_REG (old))))
7118 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7119 rl->reg_rtx);
7120 return;
7122 else if (GET_CODE (old) == SCRATCH)
7123 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7124 but we don't want to make an output reload. */
7125 return;
7127 /* If is a JUMP_INSN, we can't support output reloads yet. */
7128 gcc_assert (!JUMP_P (insn));
7130 emit_output_reload_insns (chain, rld + j, j);
7133 /* Reload number R reloads from or to a group of hard registers starting at
7134 register REGNO. Return true if it can be treated for inheritance purposes
7135 like a group of reloads, each one reloading a single hard register.
7136 The caller has already checked that the spill register and REGNO use
7137 the same number of registers to store the reload value. */
7139 static bool
7140 inherit_piecemeal_p (int r ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED)
7142 #ifdef CANNOT_CHANGE_MODE_CLASS
7143 return (!REG_CANNOT_CHANGE_MODE_P (reload_spill_index[r],
7144 GET_MODE (rld[r].reg_rtx),
7145 reg_raw_mode[reload_spill_index[r]])
7146 && !REG_CANNOT_CHANGE_MODE_P (regno,
7147 GET_MODE (rld[r].reg_rtx),
7148 reg_raw_mode[regno]));
7149 #else
7150 return true;
7151 #endif
7154 /* Output insns to reload values in and out of the chosen reload regs. */
7156 static void
7157 emit_reload_insns (struct insn_chain *chain)
7159 rtx insn = chain->insn;
7161 int j;
7163 CLEAR_HARD_REG_SET (reg_reloaded_died);
7165 for (j = 0; j < reload_n_operands; j++)
7166 input_reload_insns[j] = input_address_reload_insns[j]
7167 = inpaddr_address_reload_insns[j]
7168 = output_reload_insns[j] = output_address_reload_insns[j]
7169 = outaddr_address_reload_insns[j]
7170 = other_output_reload_insns[j] = 0;
7171 other_input_address_reload_insns = 0;
7172 other_input_reload_insns = 0;
7173 operand_reload_insns = 0;
7174 other_operand_reload_insns = 0;
7176 /* Dump reloads into the dump file. */
7177 if (dump_file)
7179 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7180 debug_reload_to_stream (dump_file);
7183 /* Now output the instructions to copy the data into and out of the
7184 reload registers. Do these in the order that the reloads were reported,
7185 since reloads of base and index registers precede reloads of operands
7186 and the operands may need the base and index registers reloaded. */
7188 for (j = 0; j < n_reloads; j++)
7190 if (rld[j].reg_rtx
7191 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7192 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
7194 do_input_reload (chain, rld + j, j);
7195 do_output_reload (chain, rld + j, j);
7198 /* Now write all the insns we made for reloads in the order expected by
7199 the allocation functions. Prior to the insn being reloaded, we write
7200 the following reloads:
7202 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7204 RELOAD_OTHER reloads.
7206 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7207 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7208 RELOAD_FOR_INPUT reload for the operand.
7210 RELOAD_FOR_OPADDR_ADDRS reloads.
7212 RELOAD_FOR_OPERAND_ADDRESS reloads.
7214 After the insn being reloaded, we write the following:
7216 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7217 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7218 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7219 reloads for the operand. The RELOAD_OTHER output reloads are
7220 output in descending order by reload number. */
7222 emit_insn_before (other_input_address_reload_insns, insn);
7223 emit_insn_before (other_input_reload_insns, insn);
7225 for (j = 0; j < reload_n_operands; j++)
7227 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7228 emit_insn_before (input_address_reload_insns[j], insn);
7229 emit_insn_before (input_reload_insns[j], insn);
7232 emit_insn_before (other_operand_reload_insns, insn);
7233 emit_insn_before (operand_reload_insns, insn);
7235 for (j = 0; j < reload_n_operands; j++)
7237 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7238 x = emit_insn_after (output_address_reload_insns[j], x);
7239 x = emit_insn_after (output_reload_insns[j], x);
7240 emit_insn_after (other_output_reload_insns[j], x);
7243 /* For all the spill regs newly reloaded in this instruction,
7244 record what they were reloaded from, so subsequent instructions
7245 can inherit the reloads.
7247 Update spill_reg_store for the reloads of this insn.
7248 Copy the elements that were updated in the loop above. */
7250 for (j = 0; j < n_reloads; j++)
7252 int r = reload_order[j];
7253 int i = reload_spill_index[r];
7255 /* If this is a non-inherited input reload from a pseudo, we must
7256 clear any memory of a previous store to the same pseudo. Only do
7257 something if there will not be an output reload for the pseudo
7258 being reloaded. */
7259 if (rld[r].in_reg != 0
7260 && ! (reload_inherited[r] || reload_override_in[r]))
7262 rtx reg = rld[r].in_reg;
7264 if (GET_CODE (reg) == SUBREG)
7265 reg = SUBREG_REG (reg);
7267 if (REG_P (reg)
7268 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7269 && ! reg_has_output_reload[REGNO (reg)])
7271 int nregno = REGNO (reg);
7273 if (reg_last_reload_reg[nregno])
7275 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7277 if (reg_reloaded_contents[last_regno] == nregno)
7278 spill_reg_store[last_regno] = 0;
7283 /* I is nonneg if this reload used a register.
7284 If rld[r].reg_rtx is 0, this is an optional reload
7285 that we opted to ignore. */
7287 if (i >= 0 && rld[r].reg_rtx != 0)
7289 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7290 int k;
7291 int part_reaches_end = 0;
7292 int all_reaches_end = 1;
7294 /* For a multi register reload, we need to check if all or part
7295 of the value lives to the end. */
7296 for (k = 0; k < nr; k++)
7298 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7299 rld[r].when_needed))
7300 part_reaches_end = 1;
7301 else
7302 all_reaches_end = 0;
7305 /* Ignore reloads that don't reach the end of the insn in
7306 entirety. */
7307 if (all_reaches_end)
7309 /* First, clear out memory of what used to be in this spill reg.
7310 If consecutive registers are used, clear them all. */
7312 for (k = 0; k < nr; k++)
7314 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7315 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7318 /* Maybe the spill reg contains a copy of reload_out. */
7319 if (rld[r].out != 0
7320 && (REG_P (rld[r].out)
7321 #ifdef AUTO_INC_DEC
7322 || ! rld[r].out_reg
7323 #endif
7324 || REG_P (rld[r].out_reg)))
7326 rtx out = (REG_P (rld[r].out)
7327 ? rld[r].out
7328 : rld[r].out_reg
7329 ? rld[r].out_reg
7330 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7331 int nregno = REGNO (out);
7332 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7333 : hard_regno_nregs[nregno]
7334 [GET_MODE (rld[r].reg_rtx)]);
7335 bool piecemeal;
7337 spill_reg_store[i] = new_spill_reg_store[i];
7338 spill_reg_stored_to[i] = out;
7339 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7341 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7342 && nr == nnr
7343 && inherit_piecemeal_p (r, nregno));
7345 /* If NREGNO is a hard register, it may occupy more than
7346 one register. If it does, say what is in the
7347 rest of the registers assuming that both registers
7348 agree on how many words the object takes. If not,
7349 invalidate the subsequent registers. */
7351 if (nregno < FIRST_PSEUDO_REGISTER)
7352 for (k = 1; k < nnr; k++)
7353 reg_last_reload_reg[nregno + k]
7354 = (piecemeal
7355 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7356 : 0);
7358 /* Now do the inverse operation. */
7359 for (k = 0; k < nr; k++)
7361 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7362 reg_reloaded_contents[i + k]
7363 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7364 ? nregno
7365 : nregno + k);
7366 reg_reloaded_insn[i + k] = insn;
7367 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7368 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (out)))
7369 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7373 /* Maybe the spill reg contains a copy of reload_in. Only do
7374 something if there will not be an output reload for
7375 the register being reloaded. */
7376 else if (rld[r].out_reg == 0
7377 && rld[r].in != 0
7378 && ((REG_P (rld[r].in)
7379 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7380 && ! reg_has_output_reload[REGNO (rld[r].in)])
7381 || (REG_P (rld[r].in_reg)
7382 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7383 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7385 int nregno;
7386 int nnr;
7387 rtx in;
7388 bool piecemeal;
7390 if (REG_P (rld[r].in)
7391 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7392 in = rld[r].in;
7393 else if (REG_P (rld[r].in_reg))
7394 in = rld[r].in_reg;
7395 else
7396 in = XEXP (rld[r].in_reg, 0);
7397 nregno = REGNO (in);
7399 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7400 : hard_regno_nregs[nregno]
7401 [GET_MODE (rld[r].reg_rtx)]);
7403 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7405 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7406 && nr == nnr
7407 && inherit_piecemeal_p (r, nregno));
7409 if (nregno < FIRST_PSEUDO_REGISTER)
7410 for (k = 1; k < nnr; k++)
7411 reg_last_reload_reg[nregno + k]
7412 = (piecemeal
7413 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7414 : 0);
7416 /* Unless we inherited this reload, show we haven't
7417 recently done a store.
7418 Previous stores of inherited auto_inc expressions
7419 also have to be discarded. */
7420 if (! reload_inherited[r]
7421 || (rld[r].out && ! rld[r].out_reg))
7422 spill_reg_store[i] = 0;
7424 for (k = 0; k < nr; k++)
7426 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7427 reg_reloaded_contents[i + k]
7428 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7429 ? nregno
7430 : nregno + k);
7431 reg_reloaded_insn[i + k] = insn;
7432 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7433 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (in)))
7434 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7439 /* However, if part of the reload reaches the end, then we must
7440 invalidate the old info for the part that survives to the end. */
7441 else if (part_reaches_end)
7443 for (k = 0; k < nr; k++)
7444 if (reload_reg_reaches_end_p (i + k,
7445 rld[r].opnum,
7446 rld[r].when_needed))
7447 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7451 /* The following if-statement was #if 0'd in 1.34 (or before...).
7452 It's reenabled in 1.35 because supposedly nothing else
7453 deals with this problem. */
7455 /* If a register gets output-reloaded from a non-spill register,
7456 that invalidates any previous reloaded copy of it.
7457 But forget_old_reloads_1 won't get to see it, because
7458 it thinks only about the original insn. So invalidate it here. */
7459 if (i < 0 && rld[r].out != 0
7460 && (REG_P (rld[r].out)
7461 || (MEM_P (rld[r].out)
7462 && REG_P (rld[r].out_reg))))
7464 rtx out = (REG_P (rld[r].out)
7465 ? rld[r].out : rld[r].out_reg);
7466 int nregno = REGNO (out);
7467 if (nregno >= FIRST_PSEUDO_REGISTER)
7469 rtx src_reg, store_insn = NULL_RTX;
7471 reg_last_reload_reg[nregno] = 0;
7473 /* If we can find a hard register that is stored, record
7474 the storing insn so that we may delete this insn with
7475 delete_output_reload. */
7476 src_reg = rld[r].reg_rtx;
7478 /* If this is an optional reload, try to find the source reg
7479 from an input reload. */
7480 if (! src_reg)
7482 rtx set = single_set (insn);
7483 if (set && SET_DEST (set) == rld[r].out)
7485 int k;
7487 src_reg = SET_SRC (set);
7488 store_insn = insn;
7489 for (k = 0; k < n_reloads; k++)
7491 if (rld[k].in == src_reg)
7493 src_reg = rld[k].reg_rtx;
7494 break;
7499 else
7500 store_insn = new_spill_reg_store[REGNO (src_reg)];
7501 if (src_reg && REG_P (src_reg)
7502 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7504 int src_regno = REGNO (src_reg);
7505 int nr = hard_regno_nregs[src_regno][rld[r].mode];
7506 /* The place where to find a death note varies with
7507 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7508 necessarily checked exactly in the code that moves
7509 notes, so just check both locations. */
7510 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7511 if (! note && store_insn)
7512 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7513 while (nr-- > 0)
7515 spill_reg_store[src_regno + nr] = store_insn;
7516 spill_reg_stored_to[src_regno + nr] = out;
7517 reg_reloaded_contents[src_regno + nr] = nregno;
7518 reg_reloaded_insn[src_regno + nr] = store_insn;
7519 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7520 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7521 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + nr,
7522 GET_MODE (src_reg)))
7523 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7524 src_regno + nr);
7525 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7526 if (note)
7527 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7528 else
7529 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7531 reg_last_reload_reg[nregno] = src_reg;
7532 /* We have to set reg_has_output_reload here, or else
7533 forget_old_reloads_1 will clear reg_last_reload_reg
7534 right away. */
7535 reg_has_output_reload[nregno] = 1;
7538 else
7540 int num_regs = hard_regno_nregs[nregno][GET_MODE (rld[r].out)];
7542 while (num_regs-- > 0)
7543 reg_last_reload_reg[nregno + num_regs] = 0;
7547 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7550 /* Go through the motions to emit INSN and test if it is strictly valid.
7551 Return the emitted insn if valid, else return NULL. */
7553 static rtx
7554 emit_insn_if_valid_for_reload (rtx insn)
7556 rtx last = get_last_insn ();
7557 int code;
7559 insn = emit_insn (insn);
7560 code = recog_memoized (insn);
7562 if (code >= 0)
7564 extract_insn (insn);
7565 /* We want constrain operands to treat this insn strictly in its
7566 validity determination, i.e., the way it would after reload has
7567 completed. */
7568 if (constrain_operands (1))
7569 return insn;
7572 delete_insns_since (last);
7573 return NULL;
7576 /* Emit code to perform a reload from IN (which may be a reload register) to
7577 OUT (which may also be a reload register). IN or OUT is from operand
7578 OPNUM with reload type TYPE.
7580 Returns first insn emitted. */
7582 static rtx
7583 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7585 rtx last = get_last_insn ();
7586 rtx tem;
7588 /* If IN is a paradoxical SUBREG, remove it and try to put the
7589 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7590 if (GET_CODE (in) == SUBREG
7591 && (GET_MODE_SIZE (GET_MODE (in))
7592 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7593 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7594 in = SUBREG_REG (in), out = tem;
7595 else if (GET_CODE (out) == SUBREG
7596 && (GET_MODE_SIZE (GET_MODE (out))
7597 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7598 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7599 out = SUBREG_REG (out), in = tem;
7601 /* How to do this reload can get quite tricky. Normally, we are being
7602 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7603 register that didn't get a hard register. In that case we can just
7604 call emit_move_insn.
7606 We can also be asked to reload a PLUS that adds a register or a MEM to
7607 another register, constant or MEM. This can occur during frame pointer
7608 elimination and while reloading addresses. This case is handled by
7609 trying to emit a single insn to perform the add. If it is not valid,
7610 we use a two insn sequence.
7612 Or we can be asked to reload an unary operand that was a fragment of
7613 an addressing mode, into a register. If it isn't recognized as-is,
7614 we try making the unop operand and the reload-register the same:
7615 (set reg:X (unop:X expr:Y))
7616 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
7618 Finally, we could be called to handle an 'o' constraint by putting
7619 an address into a register. In that case, we first try to do this
7620 with a named pattern of "reload_load_address". If no such pattern
7621 exists, we just emit a SET insn and hope for the best (it will normally
7622 be valid on machines that use 'o').
7624 This entire process is made complex because reload will never
7625 process the insns we generate here and so we must ensure that
7626 they will fit their constraints and also by the fact that parts of
7627 IN might be being reloaded separately and replaced with spill registers.
7628 Because of this, we are, in some sense, just guessing the right approach
7629 here. The one listed above seems to work.
7631 ??? At some point, this whole thing needs to be rethought. */
7633 if (GET_CODE (in) == PLUS
7634 && (REG_P (XEXP (in, 0))
7635 || GET_CODE (XEXP (in, 0)) == SUBREG
7636 || MEM_P (XEXP (in, 0)))
7637 && (REG_P (XEXP (in, 1))
7638 || GET_CODE (XEXP (in, 1)) == SUBREG
7639 || CONSTANT_P (XEXP (in, 1))
7640 || MEM_P (XEXP (in, 1))))
7642 /* We need to compute the sum of a register or a MEM and another
7643 register, constant, or MEM, and put it into the reload
7644 register. The best possible way of doing this is if the machine
7645 has a three-operand ADD insn that accepts the required operands.
7647 The simplest approach is to try to generate such an insn and see if it
7648 is recognized and matches its constraints. If so, it can be used.
7650 It might be better not to actually emit the insn unless it is valid,
7651 but we need to pass the insn as an operand to `recog' and
7652 `extract_insn' and it is simpler to emit and then delete the insn if
7653 not valid than to dummy things up. */
7655 rtx op0, op1, tem, insn;
7656 int code;
7658 op0 = find_replacement (&XEXP (in, 0));
7659 op1 = find_replacement (&XEXP (in, 1));
7661 /* Since constraint checking is strict, commutativity won't be
7662 checked, so we need to do that here to avoid spurious failure
7663 if the add instruction is two-address and the second operand
7664 of the add is the same as the reload reg, which is frequently
7665 the case. If the insn would be A = B + A, rearrange it so
7666 it will be A = A + B as constrain_operands expects. */
7668 if (REG_P (XEXP (in, 1))
7669 && REGNO (out) == REGNO (XEXP (in, 1)))
7670 tem = op0, op0 = op1, op1 = tem;
7672 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7673 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7675 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
7676 if (insn)
7677 return insn;
7679 /* If that failed, we must use a conservative two-insn sequence.
7681 Use a move to copy one operand into the reload register. Prefer
7682 to reload a constant, MEM or pseudo since the move patterns can
7683 handle an arbitrary operand. If OP1 is not a constant, MEM or
7684 pseudo and OP1 is not a valid operand for an add instruction, then
7685 reload OP1.
7687 After reloading one of the operands into the reload register, add
7688 the reload register to the output register.
7690 If there is another way to do this for a specific machine, a
7691 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7692 we emit below. */
7694 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7696 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
7697 || (REG_P (op1)
7698 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7699 || (code != CODE_FOR_nothing
7700 && ! ((*insn_data[code].operand[2].predicate)
7701 (op1, insn_data[code].operand[2].mode))))
7702 tem = op0, op0 = op1, op1 = tem;
7704 gen_reload (out, op0, opnum, type);
7706 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7707 This fixes a problem on the 32K where the stack pointer cannot
7708 be used as an operand of an add insn. */
7710 if (rtx_equal_p (op0, op1))
7711 op1 = out;
7713 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
7714 if (insn)
7716 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7717 REG_NOTES (insn)
7718 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7719 return insn;
7722 /* If that failed, copy the address register to the reload register.
7723 Then add the constant to the reload register. */
7725 gen_reload (out, op1, opnum, type);
7726 insn = emit_insn (gen_add2_insn (out, op0));
7727 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7730 #ifdef SECONDARY_MEMORY_NEEDED
7731 /* If we need a memory location to do the move, do it that way. */
7732 else if ((REG_P (in) || GET_CODE (in) == SUBREG)
7733 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7734 && (REG_P (out) || GET_CODE (out) == SUBREG)
7735 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7736 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7737 REGNO_REG_CLASS (reg_or_subregno (out)),
7738 GET_MODE (out)))
7740 /* Get the memory to use and rewrite both registers to its mode. */
7741 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7743 if (GET_MODE (loc) != GET_MODE (out))
7744 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7746 if (GET_MODE (loc) != GET_MODE (in))
7747 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7749 gen_reload (loc, in, opnum, type);
7750 gen_reload (out, loc, opnum, type);
7752 #endif
7753 else if (REG_P (out) && UNARY_P (in))
7755 rtx insn;
7756 rtx op1;
7757 rtx out_moded;
7758 rtx set;
7760 /* First, try a plain SET. */
7761 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
7762 if (set)
7763 return set;
7765 /* If that failed, move the inner operand to the reload
7766 register, and try the same unop with the inner expression
7767 replaced with the reload register. */
7768 op1 = XEXP (in, 0);
7770 if (GET_MODE (op1) != GET_MODE (out))
7771 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
7772 else
7773 out_moded = out;
7775 gen_reload (out_moded, op1, opnum, type);
7777 insn
7778 = gen_rtx_SET (VOIDmode, out,
7779 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
7780 out_moded));
7781 insn = emit_insn_if_valid_for_reload (insn);
7782 if (insn)
7784 REG_NOTES (insn)
7785 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7786 return insn;
7789 fatal_insn ("Failure trying to reload:", set);
7791 /* If IN is a simple operand, use gen_move_insn. */
7792 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
7793 emit_insn (gen_move_insn (out, in));
7795 #ifdef HAVE_reload_load_address
7796 else if (HAVE_reload_load_address)
7797 emit_insn (gen_reload_load_address (out, in));
7798 #endif
7800 /* Otherwise, just write (set OUT IN) and hope for the best. */
7801 else
7802 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7804 /* Return the first insn emitted.
7805 We can not just return get_last_insn, because there may have
7806 been multiple instructions emitted. Also note that gen_move_insn may
7807 emit more than one insn itself, so we can not assume that there is one
7808 insn emitted per emit_insn_before call. */
7810 return last ? NEXT_INSN (last) : get_insns ();
7813 /* Delete a previously made output-reload whose result we now believe
7814 is not needed. First we double-check.
7816 INSN is the insn now being processed.
7817 LAST_RELOAD_REG is the hard register number for which we want to delete
7818 the last output reload.
7819 J is the reload-number that originally used REG. The caller has made
7820 certain that reload J doesn't use REG any longer for input. */
7822 static void
7823 delete_output_reload (rtx insn, int j, int last_reload_reg)
7825 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7826 rtx reg = spill_reg_stored_to[last_reload_reg];
7827 int k;
7828 int n_occurrences;
7829 int n_inherited = 0;
7830 rtx i1;
7831 rtx substed;
7833 /* It is possible that this reload has been only used to set another reload
7834 we eliminated earlier and thus deleted this instruction too. */
7835 if (INSN_DELETED_P (output_reload_insn))
7836 return;
7838 /* Get the raw pseudo-register referred to. */
7840 while (GET_CODE (reg) == SUBREG)
7841 reg = SUBREG_REG (reg);
7842 substed = reg_equiv_memory_loc[REGNO (reg)];
7844 /* This is unsafe if the operand occurs more often in the current
7845 insn than it is inherited. */
7846 for (k = n_reloads - 1; k >= 0; k--)
7848 rtx reg2 = rld[k].in;
7849 if (! reg2)
7850 continue;
7851 if (MEM_P (reg2) || reload_override_in[k])
7852 reg2 = rld[k].in_reg;
7853 #ifdef AUTO_INC_DEC
7854 if (rld[k].out && ! rld[k].out_reg)
7855 reg2 = XEXP (rld[k].in_reg, 0);
7856 #endif
7857 while (GET_CODE (reg2) == SUBREG)
7858 reg2 = SUBREG_REG (reg2);
7859 if (rtx_equal_p (reg2, reg))
7861 if (reload_inherited[k] || reload_override_in[k] || k == j)
7863 n_inherited++;
7864 reg2 = rld[k].out_reg;
7865 if (! reg2)
7866 continue;
7867 while (GET_CODE (reg2) == SUBREG)
7868 reg2 = XEXP (reg2, 0);
7869 if (rtx_equal_p (reg2, reg))
7870 n_inherited++;
7872 else
7873 return;
7876 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7877 if (substed)
7878 n_occurrences += count_occurrences (PATTERN (insn),
7879 eliminate_regs (substed, 0,
7880 NULL_RTX), 0);
7881 if (n_occurrences > n_inherited)
7882 return;
7884 /* If the pseudo-reg we are reloading is no longer referenced
7885 anywhere between the store into it and here,
7886 and we're within the same basic block, then the value can only
7887 pass through the reload reg and end up here.
7888 Otherwise, give up--return. */
7889 for (i1 = NEXT_INSN (output_reload_insn);
7890 i1 != insn; i1 = NEXT_INSN (i1))
7892 if (NOTE_INSN_BASIC_BLOCK_P (i1))
7893 return;
7894 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
7895 && reg_mentioned_p (reg, PATTERN (i1)))
7897 /* If this is USE in front of INSN, we only have to check that
7898 there are no more references than accounted for by inheritance. */
7899 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
7901 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7902 i1 = NEXT_INSN (i1);
7904 if (n_occurrences <= n_inherited && i1 == insn)
7905 break;
7906 return;
7910 /* We will be deleting the insn. Remove the spill reg information. */
7911 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
7913 spill_reg_store[last_reload_reg + k] = 0;
7914 spill_reg_stored_to[last_reload_reg + k] = 0;
7917 /* The caller has already checked that REG dies or is set in INSN.
7918 It has also checked that we are optimizing, and thus some
7919 inaccuracies in the debugging information are acceptable.
7920 So we could just delete output_reload_insn. But in some cases
7921 we can improve the debugging information without sacrificing
7922 optimization - maybe even improving the code: See if the pseudo
7923 reg has been completely replaced with reload regs. If so, delete
7924 the store insn and forget we had a stack slot for the pseudo. */
7925 if (rld[j].out != rld[j].in
7926 && REG_N_DEATHS (REGNO (reg)) == 1
7927 && REG_N_SETS (REGNO (reg)) == 1
7928 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7929 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7931 rtx i2;
7933 /* We know that it was used only between here and the beginning of
7934 the current basic block. (We also know that the last use before
7935 INSN was the output reload we are thinking of deleting, but never
7936 mind that.) Search that range; see if any ref remains. */
7937 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7939 rtx set = single_set (i2);
7941 /* Uses which just store in the pseudo don't count,
7942 since if they are the only uses, they are dead. */
7943 if (set != 0 && SET_DEST (set) == reg)
7944 continue;
7945 if (LABEL_P (i2)
7946 || JUMP_P (i2))
7947 break;
7948 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
7949 && reg_mentioned_p (reg, PATTERN (i2)))
7951 /* Some other ref remains; just delete the output reload we
7952 know to be dead. */
7953 delete_address_reloads (output_reload_insn, insn);
7954 delete_insn (output_reload_insn);
7955 return;
7959 /* Delete the now-dead stores into this pseudo. Note that this
7960 loop also takes care of deleting output_reload_insn. */
7961 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7963 rtx set = single_set (i2);
7965 if (set != 0 && SET_DEST (set) == reg)
7967 delete_address_reloads (i2, insn);
7968 delete_insn (i2);
7970 if (LABEL_P (i2)
7971 || JUMP_P (i2))
7972 break;
7975 /* For the debugging info, say the pseudo lives in this reload reg. */
7976 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
7977 alter_reg (REGNO (reg), -1);
7979 else
7981 delete_address_reloads (output_reload_insn, insn);
7982 delete_insn (output_reload_insn);
7986 /* We are going to delete DEAD_INSN. Recursively delete loads of
7987 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7988 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7989 static void
7990 delete_address_reloads (rtx dead_insn, rtx current_insn)
7992 rtx set = single_set (dead_insn);
7993 rtx set2, dst, prev, next;
7994 if (set)
7996 rtx dst = SET_DEST (set);
7997 if (MEM_P (dst))
7998 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8000 /* If we deleted the store from a reloaded post_{in,de}c expression,
8001 we can delete the matching adds. */
8002 prev = PREV_INSN (dead_insn);
8003 next = NEXT_INSN (dead_insn);
8004 if (! prev || ! next)
8005 return;
8006 set = single_set (next);
8007 set2 = single_set (prev);
8008 if (! set || ! set2
8009 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8010 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8011 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8012 return;
8013 dst = SET_DEST (set);
8014 if (! rtx_equal_p (dst, SET_DEST (set2))
8015 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8016 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8017 || (INTVAL (XEXP (SET_SRC (set), 1))
8018 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8019 return;
8020 delete_related_insns (prev);
8021 delete_related_insns (next);
8024 /* Subfunction of delete_address_reloads: process registers found in X. */
8025 static void
8026 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8028 rtx prev, set, dst, i2;
8029 int i, j;
8030 enum rtx_code code = GET_CODE (x);
8032 if (code != REG)
8034 const char *fmt = GET_RTX_FORMAT (code);
8035 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8037 if (fmt[i] == 'e')
8038 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8039 else if (fmt[i] == 'E')
8041 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8042 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8043 current_insn);
8046 return;
8049 if (spill_reg_order[REGNO (x)] < 0)
8050 return;
8052 /* Scan backwards for the insn that sets x. This might be a way back due
8053 to inheritance. */
8054 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8056 code = GET_CODE (prev);
8057 if (code == CODE_LABEL || code == JUMP_INSN)
8058 return;
8059 if (!INSN_P (prev))
8060 continue;
8061 if (reg_set_p (x, PATTERN (prev)))
8062 break;
8063 if (reg_referenced_p (x, PATTERN (prev)))
8064 return;
8066 if (! prev || INSN_UID (prev) < reload_first_uid)
8067 return;
8068 /* Check that PREV only sets the reload register. */
8069 set = single_set (prev);
8070 if (! set)
8071 return;
8072 dst = SET_DEST (set);
8073 if (!REG_P (dst)
8074 || ! rtx_equal_p (dst, x))
8075 return;
8076 if (! reg_set_p (dst, PATTERN (dead_insn)))
8078 /* Check if DST was used in a later insn -
8079 it might have been inherited. */
8080 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8082 if (LABEL_P (i2))
8083 break;
8084 if (! INSN_P (i2))
8085 continue;
8086 if (reg_referenced_p (dst, PATTERN (i2)))
8088 /* If there is a reference to the register in the current insn,
8089 it might be loaded in a non-inherited reload. If no other
8090 reload uses it, that means the register is set before
8091 referenced. */
8092 if (i2 == current_insn)
8094 for (j = n_reloads - 1; j >= 0; j--)
8095 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8096 || reload_override_in[j] == dst)
8097 return;
8098 for (j = n_reloads - 1; j >= 0; j--)
8099 if (rld[j].in && rld[j].reg_rtx == dst)
8100 break;
8101 if (j >= 0)
8102 break;
8104 return;
8106 if (JUMP_P (i2))
8107 break;
8108 /* If DST is still live at CURRENT_INSN, check if it is used for
8109 any reload. Note that even if CURRENT_INSN sets DST, we still
8110 have to check the reloads. */
8111 if (i2 == current_insn)
8113 for (j = n_reloads - 1; j >= 0; j--)
8114 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8115 || reload_override_in[j] == dst)
8116 return;
8117 /* ??? We can't finish the loop here, because dst might be
8118 allocated to a pseudo in this block if no reload in this
8119 block needs any of the classes containing DST - see
8120 spill_hard_reg. There is no easy way to tell this, so we
8121 have to scan till the end of the basic block. */
8123 if (reg_set_p (dst, PATTERN (i2)))
8124 break;
8127 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8128 reg_reloaded_contents[REGNO (dst)] = -1;
8129 delete_insn (prev);
8132 /* Output reload-insns to reload VALUE into RELOADREG.
8133 VALUE is an autoincrement or autodecrement RTX whose operand
8134 is a register or memory location;
8135 so reloading involves incrementing that location.
8136 IN is either identical to VALUE, or some cheaper place to reload from.
8138 INC_AMOUNT is the number to increment or decrement by (always positive).
8139 This cannot be deduced from VALUE.
8141 Return the instruction that stores into RELOADREG. */
8143 static rtx
8144 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
8146 /* REG or MEM to be copied and incremented. */
8147 rtx incloc = XEXP (value, 0);
8148 /* Nonzero if increment after copying. */
8149 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
8150 rtx last;
8151 rtx inc;
8152 rtx add_insn;
8153 int code;
8154 rtx store;
8155 rtx real_in = in == value ? XEXP (in, 0) : in;
8157 /* No hard register is equivalent to this register after
8158 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
8159 we could inc/dec that register as well (maybe even using it for
8160 the source), but I'm not sure it's worth worrying about. */
8161 if (REG_P (incloc))
8162 reg_last_reload_reg[REGNO (incloc)] = 0;
8164 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8165 inc_amount = -inc_amount;
8167 inc = GEN_INT (inc_amount);
8169 /* If this is post-increment, first copy the location to the reload reg. */
8170 if (post && real_in != reloadreg)
8171 emit_insn (gen_move_insn (reloadreg, real_in));
8173 if (in == value)
8175 /* See if we can directly increment INCLOC. Use a method similar to
8176 that in gen_reload. */
8178 last = get_last_insn ();
8179 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8180 gen_rtx_PLUS (GET_MODE (incloc),
8181 incloc, inc)));
8183 code = recog_memoized (add_insn);
8184 if (code >= 0)
8186 extract_insn (add_insn);
8187 if (constrain_operands (1))
8189 /* If this is a pre-increment and we have incremented the value
8190 where it lives, copy the incremented value to RELOADREG to
8191 be used as an address. */
8193 if (! post)
8194 emit_insn (gen_move_insn (reloadreg, incloc));
8196 return add_insn;
8199 delete_insns_since (last);
8202 /* If couldn't do the increment directly, must increment in RELOADREG.
8203 The way we do this depends on whether this is pre- or post-increment.
8204 For pre-increment, copy INCLOC to the reload register, increment it
8205 there, then save back. */
8207 if (! post)
8209 if (in != reloadreg)
8210 emit_insn (gen_move_insn (reloadreg, real_in));
8211 emit_insn (gen_add2_insn (reloadreg, inc));
8212 store = emit_insn (gen_move_insn (incloc, reloadreg));
8214 else
8216 /* Postincrement.
8217 Because this might be a jump insn or a compare, and because RELOADREG
8218 may not be available after the insn in an input reload, we must do
8219 the incrementation before the insn being reloaded for.
8221 We have already copied IN to RELOADREG. Increment the copy in
8222 RELOADREG, save that back, then decrement RELOADREG so it has
8223 the original value. */
8225 emit_insn (gen_add2_insn (reloadreg, inc));
8226 store = emit_insn (gen_move_insn (incloc, reloadreg));
8227 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8230 return store;
8233 #ifdef AUTO_INC_DEC
8234 static void
8235 add_auto_inc_notes (rtx insn, rtx x)
8237 enum rtx_code code = GET_CODE (x);
8238 const char *fmt;
8239 int i, j;
8241 if (code == MEM && auto_inc_p (XEXP (x, 0)))
8243 REG_NOTES (insn)
8244 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
8245 return;
8248 /* Scan all the operand sub-expressions. */
8249 fmt = GET_RTX_FORMAT (code);
8250 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8252 if (fmt[i] == 'e')
8253 add_auto_inc_notes (insn, XEXP (x, i));
8254 else if (fmt[i] == 'E')
8255 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8256 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8259 #endif
8261 /* Copy EH notes from an insn to its reloads. */
8262 static void
8263 copy_eh_notes (rtx insn, rtx x)
8265 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8266 if (eh_note)
8268 for (; x != 0; x = NEXT_INSN (x))
8270 if (may_trap_p (PATTERN (x)))
8271 REG_NOTES (x)
8272 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8273 REG_NOTES (x));
8278 /* This is used by reload pass, that does emit some instructions after
8279 abnormal calls moving basic block end, but in fact it wants to emit
8280 them on the edge. Looks for abnormal call edges, find backward the
8281 proper call and fix the damage.
8283 Similar handle instructions throwing exceptions internally. */
8284 void
8285 fixup_abnormal_edges (void)
8287 bool inserted = false;
8288 basic_block bb;
8290 FOR_EACH_BB (bb)
8292 edge e;
8293 edge_iterator ei;
8295 /* Look for cases we are interested in - calls or instructions causing
8296 exceptions. */
8297 FOR_EACH_EDGE (e, ei, bb->succs)
8299 if (e->flags & EDGE_ABNORMAL_CALL)
8300 break;
8301 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8302 == (EDGE_ABNORMAL | EDGE_EH))
8303 break;
8305 if (e && !CALL_P (BB_END (bb))
8306 && !can_throw_internal (BB_END (bb)))
8308 rtx insn;
8310 /* Get past the new insns generated. Allow notes, as the insns
8311 may be already deleted. */
8312 insn = BB_END (bb);
8313 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8314 && !can_throw_internal (insn)
8315 && insn != BB_HEAD (bb))
8316 insn = PREV_INSN (insn);
8318 if (CALL_P (insn) || can_throw_internal (insn))
8320 rtx stop, next;
8322 stop = NEXT_INSN (BB_END (bb));
8323 BB_END (bb) = insn;
8324 insn = NEXT_INSN (insn);
8326 FOR_EACH_EDGE (e, ei, bb->succs)
8327 if (e->flags & EDGE_FALLTHRU)
8328 break;
8330 while (insn && insn != stop)
8332 next = NEXT_INSN (insn);
8333 if (INSN_P (insn))
8335 delete_insn (insn);
8337 /* Sometimes there's still the return value USE.
8338 If it's placed after a trapping call (i.e. that
8339 call is the last insn anyway), we have no fallthru
8340 edge. Simply delete this use and don't try to insert
8341 on the non-existent edge. */
8342 if (GET_CODE (PATTERN (insn)) != USE)
8344 /* We're not deleting it, we're moving it. */
8345 INSN_DELETED_P (insn) = 0;
8346 PREV_INSN (insn) = NULL_RTX;
8347 NEXT_INSN (insn) = NULL_RTX;
8349 insert_insn_on_edge (insn, e);
8350 inserted = true;
8353 insn = next;
8357 /* It may be that we don't find any such trapping insn. In this
8358 case we discovered quite late that the insn that had been
8359 marked as can_throw_internal in fact couldn't trap at all.
8360 So we should in fact delete the EH edges out of the block. */
8361 else
8362 purge_dead_edges (bb);
8366 /* We've possibly turned single trapping insn into multiple ones. */
8367 if (flag_non_call_exceptions)
8369 sbitmap blocks;
8370 blocks = sbitmap_alloc (last_basic_block);
8371 sbitmap_ones (blocks);
8372 find_many_sub_basic_blocks (blocks);
8375 if (inserted)
8376 commit_edge_insertions ();
8378 #ifdef ENABLE_CHECKING
8379 /* Verify that we didn't turn one trapping insn into many, and that
8380 we found and corrected all of the problems wrt fixups on the
8381 fallthru edge. */
8382 verify_flow_info ();
8383 #endif