* config/i386/i386.c (ix86_expand_prologue): If the function uses a
[official-gcc.git] / gcc / reload1.c
blob3eb20b02ff80bedb030ac556938c06babf153aae
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
27 #include "machmode.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "obstack.h"
32 #include "insn-config.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "basic-block.h"
39 #include "reload.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "real.h"
43 #include "toplev.h"
44 #include "except.h"
45 #include "tree.h"
47 /* This file contains the reload pass of the compiler, which is
48 run after register allocation has been done. It checks that
49 each insn is valid (operands required to be in registers really
50 are in registers of the proper class) and fixes up invalid ones
51 by copying values temporarily into registers for the insns
52 that need them.
54 The results of register allocation are described by the vector
55 reg_renumber; the insns still contain pseudo regs, but reg_renumber
56 can be used to find which hard reg, if any, a pseudo reg is in.
58 The technique we always use is to free up a few hard regs that are
59 called ``reload regs'', and for each place where a pseudo reg
60 must be in a hard reg, copy it temporarily into one of the reload regs.
62 Reload regs are allocated locally for every instruction that needs
63 reloads. When there are pseudos which are allocated to a register that
64 has been chosen as a reload reg, such pseudos must be ``spilled''.
65 This means that they go to other hard regs, or to stack slots if no other
66 available hard regs can be found. Spilling can invalidate more
67 insns, requiring additional need for reloads, so we must keep checking
68 until the process stabilizes.
70 For machines with different classes of registers, we must keep track
71 of the register class needed for each reload, and make sure that
72 we allocate enough reload registers of each class.
74 The file reload.c contains the code that checks one insn for
75 validity and reports the reloads that it needs. This file
76 is in charge of scanning the entire rtl code, accumulating the
77 reload needs, spilling, assigning reload registers to use for
78 fixing up each insn, and generating the new insns to copy values
79 into the reload registers. */
81 /* During reload_as_needed, element N contains a REG rtx for the hard reg
82 into which reg N has been reloaded (perhaps for a previous insn). */
83 static rtx *reg_last_reload_reg;
85 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
86 for an output reload that stores into reg N. */
87 static char *reg_has_output_reload;
89 /* Indicates which hard regs are reload-registers for an output reload
90 in the current insn. */
91 static HARD_REG_SET reg_is_output_reload;
93 /* Element N is the constant value to which pseudo reg N is equivalent,
94 or zero if pseudo reg N is not equivalent to a constant.
95 find_reloads looks at this in order to replace pseudo reg N
96 with the constant it stands for. */
97 rtx *reg_equiv_constant;
99 /* Element N is a memory location to which pseudo reg N is equivalent,
100 prior to any register elimination (such as frame pointer to stack
101 pointer). Depending on whether or not it is a valid address, this value
102 is transferred to either reg_equiv_address or reg_equiv_mem. */
103 rtx *reg_equiv_memory_loc;
105 /* We allocate reg_equiv_memory_loc inside a varray so that the garbage
106 collector can keep track of what is inside. */
107 varray_type reg_equiv_memory_loc_varray;
109 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
110 This is used when the address is not valid as a memory address
111 (because its displacement is too big for the machine.) */
112 rtx *reg_equiv_address;
114 /* Element N is the memory slot to which pseudo reg N is equivalent,
115 or zero if pseudo reg N is not equivalent to a memory slot. */
116 rtx *reg_equiv_mem;
118 /* Widest width in which each pseudo reg is referred to (via subreg). */
119 static unsigned int *reg_max_ref_width;
121 /* Element N is the list of insns that initialized reg N from its equivalent
122 constant or memory slot. */
123 static rtx *reg_equiv_init;
125 /* Vector to remember old contents of reg_renumber before spilling. */
126 static short *reg_old_renumber;
128 /* During reload_as_needed, element N contains the last pseudo regno reloaded
129 into hard register N. If that pseudo reg occupied more than one register,
130 reg_reloaded_contents points to that pseudo for each spill register in
131 use; all of these must remain set for an inheritance to occur. */
132 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
134 /* During reload_as_needed, element N contains the insn for which
135 hard register N was last used. Its contents are significant only
136 when reg_reloaded_valid is set for this register. */
137 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
139 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
140 static HARD_REG_SET reg_reloaded_valid;
141 /* Indicate if the register was dead at the end of the reload.
142 This is only valid if reg_reloaded_contents is set and valid. */
143 static HARD_REG_SET reg_reloaded_dead;
145 /* Indicate whether the register's current value is one that is not
146 safe to retain across a call, even for registers that are normally
147 call-saved. */
148 static HARD_REG_SET reg_reloaded_call_part_clobbered;
150 /* Number of spill-regs so far; number of valid elements of spill_regs. */
151 static int n_spills;
153 /* In parallel with spill_regs, contains REG rtx's for those regs.
154 Holds the last rtx used for any given reg, or 0 if it has never
155 been used for spilling yet. This rtx is reused, provided it has
156 the proper mode. */
157 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
159 /* In parallel with spill_regs, contains nonzero for a spill reg
160 that was stored after the last time it was used.
161 The precise value is the insn generated to do the store. */
162 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
164 /* This is the register that was stored with spill_reg_store. This is a
165 copy of reload_out / reload_out_reg when the value was stored; if
166 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
167 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
169 /* This table is the inverse mapping of spill_regs:
170 indexed by hard reg number,
171 it contains the position of that reg in spill_regs,
172 or -1 for something that is not in spill_regs.
174 ?!? This is no longer accurate. */
175 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
177 /* This reg set indicates registers that can't be used as spill registers for
178 the currently processed insn. These are the hard registers which are live
179 during the insn, but not allocated to pseudos, as well as fixed
180 registers. */
181 static HARD_REG_SET bad_spill_regs;
183 /* These are the hard registers that can't be used as spill register for any
184 insn. This includes registers used for user variables and registers that
185 we can't eliminate. A register that appears in this set also can't be used
186 to retry register allocation. */
187 static HARD_REG_SET bad_spill_regs_global;
189 /* Describes order of use of registers for reloading
190 of spilled pseudo-registers. `n_spills' is the number of
191 elements that are actually valid; new ones are added at the end.
193 Both spill_regs and spill_reg_order are used on two occasions:
194 once during find_reload_regs, where they keep track of the spill registers
195 for a single insn, but also during reload_as_needed where they show all
196 the registers ever used by reload. For the latter case, the information
197 is calculated during finish_spills. */
198 static short spill_regs[FIRST_PSEUDO_REGISTER];
200 /* This vector of reg sets indicates, for each pseudo, which hard registers
201 may not be used for retrying global allocation because the register was
202 formerly spilled from one of them. If we allowed reallocating a pseudo to
203 a register that it was already allocated to, reload might not
204 terminate. */
205 static HARD_REG_SET *pseudo_previous_regs;
207 /* This vector of reg sets indicates, for each pseudo, which hard
208 registers may not be used for retrying global allocation because they
209 are used as spill registers during one of the insns in which the
210 pseudo is live. */
211 static HARD_REG_SET *pseudo_forbidden_regs;
213 /* All hard regs that have been used as spill registers for any insn are
214 marked in this set. */
215 static HARD_REG_SET used_spill_regs;
217 /* Index of last register assigned as a spill register. We allocate in
218 a round-robin fashion. */
219 static int last_spill_reg;
221 /* Nonzero if indirect addressing is supported on the machine; this means
222 that spilling (REG n) does not require reloading it into a register in
223 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
224 value indicates the level of indirect addressing supported, e.g., two
225 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
226 a hard register. */
227 static char spill_indirect_levels;
229 /* Nonzero if indirect addressing is supported when the innermost MEM is
230 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
231 which these are valid is the same as spill_indirect_levels, above. */
232 char indirect_symref_ok;
234 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
235 char double_reg_address_ok;
237 /* Record the stack slot for each spilled hard register. */
238 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
240 /* Width allocated so far for that stack slot. */
241 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
243 /* Record which pseudos needed to be spilled. */
244 static regset_head spilled_pseudos;
246 /* Used for communication between order_regs_for_reload and count_pseudo.
247 Used to avoid counting one pseudo twice. */
248 static regset_head pseudos_counted;
250 /* First uid used by insns created by reload in this function.
251 Used in find_equiv_reg. */
252 int reload_first_uid;
254 /* Flag set by local-alloc or global-alloc if anything is live in
255 a call-clobbered reg across calls. */
256 int caller_save_needed;
258 /* Set to 1 while reload_as_needed is operating.
259 Required by some machines to handle any generated moves differently. */
260 int reload_in_progress = 0;
262 /* These arrays record the insn_code of insns that may be needed to
263 perform input and output reloads of special objects. They provide a
264 place to pass a scratch register. */
265 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
266 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
268 /* This obstack is used for allocation of rtl during register elimination.
269 The allocated storage can be freed once find_reloads has processed the
270 insn. */
271 struct obstack reload_obstack;
273 /* Points to the beginning of the reload_obstack. All insn_chain structures
274 are allocated first. */
275 char *reload_startobj;
277 /* The point after all insn_chain structures. Used to quickly deallocate
278 memory allocated in copy_reloads during calculate_needs_all_insns. */
279 char *reload_firstobj;
281 /* This points before all local rtl generated by register elimination.
282 Used to quickly free all memory after processing one insn. */
283 static char *reload_insn_firstobj;
285 /* List of insn_chain instructions, one for every insn that reload needs to
286 examine. */
287 struct insn_chain *reload_insn_chain;
289 /* List of all insns needing reloads. */
290 static struct insn_chain *insns_need_reload;
292 /* This structure is used to record information about register eliminations.
293 Each array entry describes one possible way of eliminating a register
294 in favor of another. If there is more than one way of eliminating a
295 particular register, the most preferred should be specified first. */
297 struct elim_table
299 int from; /* Register number to be eliminated. */
300 int to; /* Register number used as replacement. */
301 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
302 int can_eliminate; /* Nonzero if this elimination can be done. */
303 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
304 insns made by reload. */
305 HOST_WIDE_INT offset; /* Current offset between the two regs. */
306 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
307 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
308 rtx from_rtx; /* REG rtx for the register to be eliminated.
309 We cannot simply compare the number since
310 we might then spuriously replace a hard
311 register corresponding to a pseudo
312 assigned to the reg to be eliminated. */
313 rtx to_rtx; /* REG rtx for the replacement. */
316 static struct elim_table *reg_eliminate = 0;
318 /* This is an intermediate structure to initialize the table. It has
319 exactly the members provided by ELIMINABLE_REGS. */
320 static const struct elim_table_1
322 const int from;
323 const int to;
324 } reg_eliminate_1[] =
326 /* If a set of eliminable registers was specified, define the table from it.
327 Otherwise, default to the normal case of the frame pointer being
328 replaced by the stack pointer. */
330 #ifdef ELIMINABLE_REGS
331 ELIMINABLE_REGS;
332 #else
333 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
334 #endif
336 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
338 /* Record the number of pending eliminations that have an offset not equal
339 to their initial offset. If nonzero, we use a new copy of each
340 replacement result in any insns encountered. */
341 int num_not_at_initial_offset;
343 /* Count the number of registers that we may be able to eliminate. */
344 static int num_eliminable;
345 /* And the number of registers that are equivalent to a constant that
346 can be eliminated to frame_pointer / arg_pointer + constant. */
347 static int num_eliminable_invariants;
349 /* For each label, we record the offset of each elimination. If we reach
350 a label by more than one path and an offset differs, we cannot do the
351 elimination. This information is indexed by the difference of the
352 number of the label and the first label number. We can't offset the
353 pointer itself as this can cause problems on machines with segmented
354 memory. The first table is an array of flags that records whether we
355 have yet encountered a label and the second table is an array of arrays,
356 one entry in the latter array for each elimination. */
358 static int first_label_num;
359 static char *offsets_known_at;
360 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
362 /* Number of labels in the current function. */
364 static int num_labels;
366 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
367 static void maybe_fix_stack_asms (void);
368 static void copy_reloads (struct insn_chain *);
369 static void calculate_needs_all_insns (int);
370 static int find_reg (struct insn_chain *, int);
371 static void find_reload_regs (struct insn_chain *);
372 static void select_reload_regs (void);
373 static void delete_caller_save_insns (void);
375 static void spill_failure (rtx, enum reg_class);
376 static void count_spilled_pseudo (int, int, int);
377 static void delete_dead_insn (rtx);
378 static void alter_reg (int, int);
379 static void set_label_offsets (rtx, rtx, int);
380 static void check_eliminable_occurrences (rtx);
381 static void elimination_effects (rtx, enum machine_mode);
382 static int eliminate_regs_in_insn (rtx, int);
383 static void update_eliminable_offsets (void);
384 static void mark_not_eliminable (rtx, rtx, void *);
385 static void set_initial_elim_offsets (void);
386 static void verify_initial_elim_offsets (void);
387 static void set_initial_label_offsets (void);
388 static void set_offsets_for_label (rtx);
389 static void init_elim_table (void);
390 static void update_eliminables (HARD_REG_SET *);
391 static void spill_hard_reg (unsigned int, int);
392 static int finish_spills (int);
393 static void ior_hard_reg_set (HARD_REG_SET *, HARD_REG_SET *);
394 static void scan_paradoxical_subregs (rtx);
395 static void count_pseudo (int);
396 static void order_regs_for_reload (struct insn_chain *);
397 static void reload_as_needed (int);
398 static void forget_old_reloads_1 (rtx, rtx, void *);
399 static int reload_reg_class_lower (const void *, const void *);
400 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
401 enum machine_mode);
402 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
403 enum machine_mode);
404 static int reload_reg_free_p (unsigned int, int, enum reload_type);
405 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
406 rtx, rtx, int, int);
407 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
408 rtx, rtx, int, int);
409 static int function_invariant_p (rtx);
410 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
411 static int allocate_reload_reg (struct insn_chain *, int, int);
412 static int conflicts_with_override (rtx);
413 static void failed_reload (rtx, int);
414 static int set_reload_reg (int, int);
415 static void choose_reload_regs_init (struct insn_chain *, rtx *);
416 static void choose_reload_regs (struct insn_chain *);
417 static void merge_assigned_reloads (rtx);
418 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
419 rtx, int);
420 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
421 int);
422 static void do_input_reload (struct insn_chain *, struct reload *, int);
423 static void do_output_reload (struct insn_chain *, struct reload *, int);
424 static bool inherit_piecemeal_p (int, int);
425 static void emit_reload_insns (struct insn_chain *);
426 static void delete_output_reload (rtx, int, int);
427 static void delete_address_reloads (rtx, rtx);
428 static void delete_address_reloads_1 (rtx, rtx, rtx);
429 static rtx inc_for_reload (rtx, rtx, rtx, int);
430 #ifdef AUTO_INC_DEC
431 static void add_auto_inc_notes (rtx, rtx);
432 #endif
433 static void copy_eh_notes (rtx, rtx);
435 /* Initialize the reload pass once per compilation. */
437 void
438 init_reload (void)
440 int i;
442 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
443 Set spill_indirect_levels to the number of levels such addressing is
444 permitted, zero if it is not permitted at all. */
446 rtx tem
447 = gen_rtx_MEM (Pmode,
448 gen_rtx_PLUS (Pmode,
449 gen_rtx_REG (Pmode,
450 LAST_VIRTUAL_REGISTER + 1),
451 GEN_INT (4)));
452 spill_indirect_levels = 0;
454 while (memory_address_p (QImode, tem))
456 spill_indirect_levels++;
457 tem = gen_rtx_MEM (Pmode, tem);
460 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
462 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
463 indirect_symref_ok = memory_address_p (QImode, tem);
465 /* See if reg+reg is a valid (and offsettable) address. */
467 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
469 tem = gen_rtx_PLUS (Pmode,
470 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
471 gen_rtx_REG (Pmode, i));
473 /* This way, we make sure that reg+reg is an offsettable address. */
474 tem = plus_constant (tem, 4);
476 if (memory_address_p (QImode, tem))
478 double_reg_address_ok = 1;
479 break;
483 /* Initialize obstack for our rtl allocation. */
484 gcc_obstack_init (&reload_obstack);
485 reload_startobj = obstack_alloc (&reload_obstack, 0);
487 INIT_REG_SET (&spilled_pseudos);
488 INIT_REG_SET (&pseudos_counted);
489 VARRAY_RTX_INIT (reg_equiv_memory_loc_varray, 0, "reg_equiv_memory_loc");
492 /* List of insn chains that are currently unused. */
493 static struct insn_chain *unused_insn_chains = 0;
495 /* Allocate an empty insn_chain structure. */
496 struct insn_chain *
497 new_insn_chain (void)
499 struct insn_chain *c;
501 if (unused_insn_chains == 0)
503 c = obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
504 INIT_REG_SET (&c->live_throughout);
505 INIT_REG_SET (&c->dead_or_set);
507 else
509 c = unused_insn_chains;
510 unused_insn_chains = c->next;
512 c->is_caller_save_insn = 0;
513 c->need_operand_change = 0;
514 c->need_reload = 0;
515 c->need_elim = 0;
516 return c;
519 /* Small utility function to set all regs in hard reg set TO which are
520 allocated to pseudos in regset FROM. */
522 void
523 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
525 unsigned int regno;
527 EXECUTE_IF_SET_IN_REG_SET
528 (from, FIRST_PSEUDO_REGISTER, regno,
530 int r = reg_renumber[regno];
531 int nregs;
533 if (r < 0)
535 /* reload_combine uses the information from
536 BASIC_BLOCK->global_live_at_start, which might still
537 contain registers that have not actually been allocated
538 since they have an equivalence. */
539 if (! reload_completed)
540 abort ();
542 else
544 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
545 while (nregs-- > 0)
546 SET_HARD_REG_BIT (*to, r + nregs);
551 /* Replace all pseudos found in LOC with their corresponding
552 equivalences. */
554 static void
555 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
557 rtx x = *loc;
558 enum rtx_code code;
559 const char *fmt;
560 int i, j;
562 if (! x)
563 return;
565 code = GET_CODE (x);
566 if (code == REG)
568 unsigned int regno = REGNO (x);
570 if (regno < FIRST_PSEUDO_REGISTER)
571 return;
573 x = eliminate_regs (x, mem_mode, usage);
574 if (x != *loc)
576 *loc = x;
577 replace_pseudos_in (loc, mem_mode, usage);
578 return;
581 if (reg_equiv_constant[regno])
582 *loc = reg_equiv_constant[regno];
583 else if (reg_equiv_mem[regno])
584 *loc = reg_equiv_mem[regno];
585 else if (reg_equiv_address[regno])
586 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
587 else if (!REG_P (regno_reg_rtx[regno])
588 || REGNO (regno_reg_rtx[regno]) != regno)
589 *loc = regno_reg_rtx[regno];
590 else
591 abort ();
593 return;
595 else if (code == MEM)
597 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
598 return;
601 /* Process each of our operands recursively. */
602 fmt = GET_RTX_FORMAT (code);
603 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
604 if (*fmt == 'e')
605 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
606 else if (*fmt == 'E')
607 for (j = 0; j < XVECLEN (x, i); j++)
608 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
612 /* Global variables used by reload and its subroutines. */
614 /* Set during calculate_needs if an insn needs register elimination. */
615 static int something_needs_elimination;
616 /* Set during calculate_needs if an insn needs an operand changed. */
617 int something_needs_operands_changed;
619 /* Nonzero means we couldn't get enough spill regs. */
620 static int failure;
622 /* Main entry point for the reload pass.
624 FIRST is the first insn of the function being compiled.
626 GLOBAL nonzero means we were called from global_alloc
627 and should attempt to reallocate any pseudoregs that we
628 displace from hard regs we will use for reloads.
629 If GLOBAL is zero, we do not have enough information to do that,
630 so any pseudo reg that is spilled must go to the stack.
632 Return value is nonzero if reload failed
633 and we must not do any more for this function. */
636 reload (rtx first, int global)
638 int i;
639 rtx insn;
640 struct elim_table *ep;
641 basic_block bb;
643 /* Make sure even insns with volatile mem refs are recognizable. */
644 init_recog ();
646 failure = 0;
648 reload_firstobj = obstack_alloc (&reload_obstack, 0);
650 /* Make sure that the last insn in the chain
651 is not something that needs reloading. */
652 emit_note (NOTE_INSN_DELETED);
654 /* Enable find_equiv_reg to distinguish insns made by reload. */
655 reload_first_uid = get_max_uid ();
657 #ifdef SECONDARY_MEMORY_NEEDED
658 /* Initialize the secondary memory table. */
659 clear_secondary_mem ();
660 #endif
662 /* We don't have a stack slot for any spill reg yet. */
663 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
664 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
666 /* Initialize the save area information for caller-save, in case some
667 are needed. */
668 init_save_areas ();
670 /* Compute which hard registers are now in use
671 as homes for pseudo registers.
672 This is done here rather than (eg) in global_alloc
673 because this point is reached even if not optimizing. */
674 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
675 mark_home_live (i);
677 /* A function that receives a nonlocal goto must save all call-saved
678 registers. */
679 if (current_function_has_nonlocal_label)
680 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
681 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
682 regs_ever_live[i] = 1;
684 #ifdef NON_SAVING_SETJMP
685 /* A function that calls setjmp should save and restore all the
686 call-saved registers on a system where longjmp clobbers them. */
687 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
689 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
690 if (! call_used_regs[i])
691 regs_ever_live[i] = 1;
693 #endif
695 /* Find all the pseudo registers that didn't get hard regs
696 but do have known equivalent constants or memory slots.
697 These include parameters (known equivalent to parameter slots)
698 and cse'd or loop-moved constant memory addresses.
700 Record constant equivalents in reg_equiv_constant
701 so they will be substituted by find_reloads.
702 Record memory equivalents in reg_mem_equiv so they can
703 be substituted eventually by altering the REG-rtx's. */
705 reg_equiv_constant = xcalloc (max_regno, sizeof (rtx));
706 reg_equiv_mem = xcalloc (max_regno, sizeof (rtx));
707 reg_equiv_init = xcalloc (max_regno, sizeof (rtx));
708 reg_equiv_address = xcalloc (max_regno, sizeof (rtx));
709 reg_max_ref_width = xcalloc (max_regno, sizeof (int));
710 reg_old_renumber = xcalloc (max_regno, sizeof (short));
711 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
712 pseudo_forbidden_regs = xmalloc (max_regno * sizeof (HARD_REG_SET));
713 pseudo_previous_regs = xcalloc (max_regno, sizeof (HARD_REG_SET));
715 CLEAR_HARD_REG_SET (bad_spill_regs_global);
717 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
718 to. Also find all paradoxical subregs and find largest such for
719 each pseudo. */
721 num_eliminable_invariants = 0;
722 for (insn = first; insn; insn = NEXT_INSN (insn))
724 rtx set = single_set (insn);
726 /* We may introduce USEs that we want to remove at the end, so
727 we'll mark them with QImode. Make sure there are no
728 previously-marked insns left by say regmove. */
729 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
730 && GET_MODE (insn) != VOIDmode)
731 PUT_MODE (insn, VOIDmode);
733 if (set != 0 && REG_P (SET_DEST (set)))
735 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
736 if (note
737 && (! function_invariant_p (XEXP (note, 0))
738 || ! flag_pic
739 /* A function invariant is often CONSTANT_P but may
740 include a register. We promise to only pass
741 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
742 || (CONSTANT_P (XEXP (note, 0))
743 && LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))))
745 rtx x = XEXP (note, 0);
746 i = REGNO (SET_DEST (set));
747 if (i > LAST_VIRTUAL_REGISTER)
749 /* It can happen that a REG_EQUIV note contains a MEM
750 that is not a legitimate memory operand. As later
751 stages of reload assume that all addresses found
752 in the reg_equiv_* arrays were originally legitimate,
753 we ignore such REG_EQUIV notes. */
754 if (memory_operand (x, VOIDmode))
756 /* Always unshare the equivalence, so we can
757 substitute into this insn without touching the
758 equivalence. */
759 reg_equiv_memory_loc[i] = copy_rtx (x);
761 else if (function_invariant_p (x))
763 if (GET_CODE (x) == PLUS)
765 /* This is PLUS of frame pointer and a constant,
766 and might be shared. Unshare it. */
767 reg_equiv_constant[i] = copy_rtx (x);
768 num_eliminable_invariants++;
770 else if (x == frame_pointer_rtx
771 || x == arg_pointer_rtx)
773 reg_equiv_constant[i] = x;
774 num_eliminable_invariants++;
776 else if (LEGITIMATE_CONSTANT_P (x))
777 reg_equiv_constant[i] = x;
778 else
780 reg_equiv_memory_loc[i]
781 = force_const_mem (GET_MODE (SET_DEST (set)), x);
782 if (!reg_equiv_memory_loc[i])
783 continue;
786 else
787 continue;
789 /* If this register is being made equivalent to a MEM
790 and the MEM is not SET_SRC, the equivalencing insn
791 is one with the MEM as a SET_DEST and it occurs later.
792 So don't mark this insn now. */
793 if (!MEM_P (x)
794 || rtx_equal_p (SET_SRC (set), x))
795 reg_equiv_init[i]
796 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
801 /* If this insn is setting a MEM from a register equivalent to it,
802 this is the equivalencing insn. */
803 else if (set && MEM_P (SET_DEST (set))
804 && REG_P (SET_SRC (set))
805 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
806 && rtx_equal_p (SET_DEST (set),
807 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
808 reg_equiv_init[REGNO (SET_SRC (set))]
809 = gen_rtx_INSN_LIST (VOIDmode, insn,
810 reg_equiv_init[REGNO (SET_SRC (set))]);
812 if (INSN_P (insn))
813 scan_paradoxical_subregs (PATTERN (insn));
816 init_elim_table ();
818 first_label_num = get_first_label_num ();
819 num_labels = max_label_num () - first_label_num;
821 /* Allocate the tables used to store offset information at labels. */
822 /* We used to use alloca here, but the size of what it would try to
823 allocate would occasionally cause it to exceed the stack limit and
824 cause a core dump. */
825 offsets_known_at = xmalloc (num_labels);
826 offsets_at = xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
828 /* Alter each pseudo-reg rtx to contain its hard reg number.
829 Assign stack slots to the pseudos that lack hard regs or equivalents.
830 Do not touch virtual registers. */
832 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
833 alter_reg (i, -1);
835 /* If we have some registers we think can be eliminated, scan all insns to
836 see if there is an insn that sets one of these registers to something
837 other than itself plus a constant. If so, the register cannot be
838 eliminated. Doing this scan here eliminates an extra pass through the
839 main reload loop in the most common case where register elimination
840 cannot be done. */
841 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
842 if (INSN_P (insn))
843 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
845 maybe_fix_stack_asms ();
847 insns_need_reload = 0;
848 something_needs_elimination = 0;
850 /* Initialize to -1, which means take the first spill register. */
851 last_spill_reg = -1;
853 /* Spill any hard regs that we know we can't eliminate. */
854 CLEAR_HARD_REG_SET (used_spill_regs);
855 /* There can be multiple ways to eliminate a register;
856 they should be listed adjacently.
857 Elimination for any register fails only if all possible ways fail. */
858 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
860 int from = ep->from;
861 int can_eliminate = 0;
864 can_eliminate |= ep->can_eliminate;
865 ep++;
867 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
868 if (! can_eliminate)
869 spill_hard_reg (from, 1);
872 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
873 if (frame_pointer_needed)
874 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
875 #endif
876 finish_spills (global);
878 /* From now on, we may need to generate moves differently. We may also
879 allow modifications of insns which cause them to not be recognized.
880 Any such modifications will be cleaned up during reload itself. */
881 reload_in_progress = 1;
883 /* This loop scans the entire function each go-round
884 and repeats until one repetition spills no additional hard regs. */
885 for (;;)
887 int something_changed;
888 int did_spill;
890 HOST_WIDE_INT starting_frame_size;
892 /* Round size of stack frame to stack_alignment_needed. This must be done
893 here because the stack size may be a part of the offset computation
894 for register elimination, and there might have been new stack slots
895 created in the last iteration of this loop. */
896 if (cfun->stack_alignment_needed)
897 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
899 starting_frame_size = get_frame_size ();
901 set_initial_elim_offsets ();
902 set_initial_label_offsets ();
904 /* For each pseudo register that has an equivalent location defined,
905 try to eliminate any eliminable registers (such as the frame pointer)
906 assuming initial offsets for the replacement register, which
907 is the normal case.
909 If the resulting location is directly addressable, substitute
910 the MEM we just got directly for the old REG.
912 If it is not addressable but is a constant or the sum of a hard reg
913 and constant, it is probably not addressable because the constant is
914 out of range, in that case record the address; we will generate
915 hairy code to compute the address in a register each time it is
916 needed. Similarly if it is a hard register, but one that is not
917 valid as an address register.
919 If the location is not addressable, but does not have one of the
920 above forms, assign a stack slot. We have to do this to avoid the
921 potential of producing lots of reloads if, e.g., a location involves
922 a pseudo that didn't get a hard register and has an equivalent memory
923 location that also involves a pseudo that didn't get a hard register.
925 Perhaps at some point we will improve reload_when_needed handling
926 so this problem goes away. But that's very hairy. */
928 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
929 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
931 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
933 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
934 XEXP (x, 0)))
935 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
936 else if (CONSTANT_P (XEXP (x, 0))
937 || (REG_P (XEXP (x, 0))
938 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
939 || (GET_CODE (XEXP (x, 0)) == PLUS
940 && REG_P (XEXP (XEXP (x, 0), 0))
941 && (REGNO (XEXP (XEXP (x, 0), 0))
942 < FIRST_PSEUDO_REGISTER)
943 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
944 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
945 else
947 /* Make a new stack slot. Then indicate that something
948 changed so we go back and recompute offsets for
949 eliminable registers because the allocation of memory
950 below might change some offset. reg_equiv_{mem,address}
951 will be set up for this pseudo on the next pass around
952 the loop. */
953 reg_equiv_memory_loc[i] = 0;
954 reg_equiv_init[i] = 0;
955 alter_reg (i, -1);
959 if (caller_save_needed)
960 setup_save_areas ();
962 /* If we allocated another stack slot, redo elimination bookkeeping. */
963 if (starting_frame_size != get_frame_size ())
964 continue;
966 if (caller_save_needed)
968 save_call_clobbered_regs ();
969 /* That might have allocated new insn_chain structures. */
970 reload_firstobj = obstack_alloc (&reload_obstack, 0);
973 calculate_needs_all_insns (global);
975 CLEAR_REG_SET (&spilled_pseudos);
976 did_spill = 0;
978 something_changed = 0;
980 /* If we allocated any new memory locations, make another pass
981 since it might have changed elimination offsets. */
982 if (starting_frame_size != get_frame_size ())
983 something_changed = 1;
986 HARD_REG_SET to_spill;
987 CLEAR_HARD_REG_SET (to_spill);
988 update_eliminables (&to_spill);
989 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
990 if (TEST_HARD_REG_BIT (to_spill, i))
992 spill_hard_reg (i, 1);
993 did_spill = 1;
995 /* Regardless of the state of spills, if we previously had
996 a register that we thought we could eliminate, but now can
997 not eliminate, we must run another pass.
999 Consider pseudos which have an entry in reg_equiv_* which
1000 reference an eliminable register. We must make another pass
1001 to update reg_equiv_* so that we do not substitute in the
1002 old value from when we thought the elimination could be
1003 performed. */
1004 something_changed = 1;
1008 select_reload_regs ();
1009 if (failure)
1010 goto failed;
1012 if (insns_need_reload != 0 || did_spill)
1013 something_changed |= finish_spills (global);
1015 if (! something_changed)
1016 break;
1018 if (caller_save_needed)
1019 delete_caller_save_insns ();
1021 obstack_free (&reload_obstack, reload_firstobj);
1024 /* If global-alloc was run, notify it of any register eliminations we have
1025 done. */
1026 if (global)
1027 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1028 if (ep->can_eliminate)
1029 mark_elimination (ep->from, ep->to);
1031 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1032 If that insn didn't set the register (i.e., it copied the register to
1033 memory), just delete that insn instead of the equivalencing insn plus
1034 anything now dead. If we call delete_dead_insn on that insn, we may
1035 delete the insn that actually sets the register if the register dies
1036 there and that is incorrect. */
1038 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1040 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1042 rtx list;
1043 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1045 rtx equiv_insn = XEXP (list, 0);
1047 /* If we already deleted the insn or if it may trap, we can't
1048 delete it. The latter case shouldn't happen, but can
1049 if an insn has a variable address, gets a REG_EH_REGION
1050 note added to it, and then gets converted into an load
1051 from a constant address. */
1052 if (NOTE_P (equiv_insn)
1053 || can_throw_internal (equiv_insn))
1055 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1056 delete_dead_insn (equiv_insn);
1057 else
1058 SET_INSN_DELETED (equiv_insn);
1063 /* Use the reload registers where necessary
1064 by generating move instructions to move the must-be-register
1065 values into or out of the reload registers. */
1067 if (insns_need_reload != 0 || something_needs_elimination
1068 || something_needs_operands_changed)
1070 HOST_WIDE_INT old_frame_size = get_frame_size ();
1072 reload_as_needed (global);
1074 if (old_frame_size != get_frame_size ())
1075 abort ();
1077 if (num_eliminable)
1078 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->global_live_at_start,
1090 HARD_FRAME_POINTER_REGNUM);
1092 /* Come here (with failure set nonzero) if we can't get enough spill regs
1093 and we decide not to abort about it. */
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 RTX_UNCHANGING_P (reg) = MEM_IN_STRUCT_P (reg)
1133 = MEM_SCALAR_P (reg) = 0;
1134 MEM_ATTRS (reg) = 0;
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 pnote = &REG_NOTES (insn);
1190 while (*pnote != 0)
1192 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1193 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1194 || REG_NOTE_KIND (*pnote) == REG_INC
1195 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1196 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1197 *pnote = XEXP (*pnote, 1);
1198 else
1199 pnote = &XEXP (*pnote, 1);
1202 #ifdef AUTO_INC_DEC
1203 add_auto_inc_notes (insn, PATTERN (insn));
1204 #endif
1206 /* And simplify (subreg (reg)) if it appears as an operand. */
1207 cleanup_subreg_operands (insn);
1210 /* If we are doing stack checking, give a warning if this function's
1211 frame size is larger than we expect. */
1212 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1214 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1215 static int verbose_warned = 0;
1217 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1218 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1219 size += UNITS_PER_WORD;
1221 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1223 warning ("frame size too large for reliable stack checking");
1224 if (! verbose_warned)
1226 warning ("try reducing the number of local variables");
1227 verbose_warned = 1;
1232 /* Indicate that we no longer have known memory locations or constants. */
1233 if (reg_equiv_constant)
1234 free (reg_equiv_constant);
1235 reg_equiv_constant = 0;
1236 VARRAY_GROW (reg_equiv_memory_loc_varray, 0);
1237 reg_equiv_memory_loc = 0;
1239 if (offsets_known_at)
1240 free (offsets_known_at);
1241 if (offsets_at)
1242 free (offsets_at);
1244 free (reg_equiv_mem);
1245 free (reg_equiv_init);
1246 free (reg_equiv_address);
1247 free (reg_max_ref_width);
1248 free (reg_old_renumber);
1249 free (pseudo_previous_regs);
1250 free (pseudo_forbidden_regs);
1252 CLEAR_HARD_REG_SET (used_spill_regs);
1253 for (i = 0; i < n_spills; i++)
1254 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1256 /* Free all the insn_chain structures at once. */
1257 obstack_free (&reload_obstack, reload_startobj);
1258 unused_insn_chains = 0;
1259 fixup_abnormal_edges ();
1261 /* Replacing pseudos with their memory equivalents might have
1262 created shared rtx. Subsequent passes would get confused
1263 by this, so unshare everything here. */
1264 unshare_all_rtl_again (first);
1266 #ifdef STACK_BOUNDARY
1267 /* init_emit has set the alignment of the hard frame pointer
1268 to STACK_BOUNDARY. It is very likely no longer valid if
1269 the hard frame pointer was used for register allocation. */
1270 if (!frame_pointer_needed)
1271 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1272 #endif
1274 return failure;
1277 /* Yet another special case. Unfortunately, reg-stack forces people to
1278 write incorrect clobbers in asm statements. These clobbers must not
1279 cause the register to appear in bad_spill_regs, otherwise we'll call
1280 fatal_insn later. We clear the corresponding regnos in the live
1281 register sets to avoid this.
1282 The whole thing is rather sick, I'm afraid. */
1284 static void
1285 maybe_fix_stack_asms (void)
1287 #ifdef STACK_REGS
1288 const char *constraints[MAX_RECOG_OPERANDS];
1289 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1290 struct insn_chain *chain;
1292 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1294 int i, noperands;
1295 HARD_REG_SET clobbered, allowed;
1296 rtx pat;
1298 if (! INSN_P (chain->insn)
1299 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1300 continue;
1301 pat = PATTERN (chain->insn);
1302 if (GET_CODE (pat) != PARALLEL)
1303 continue;
1305 CLEAR_HARD_REG_SET (clobbered);
1306 CLEAR_HARD_REG_SET (allowed);
1308 /* First, make a mask of all stack regs that are clobbered. */
1309 for (i = 0; i < XVECLEN (pat, 0); i++)
1311 rtx t = XVECEXP (pat, 0, i);
1312 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1313 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1316 /* Get the operand values and constraints out of the insn. */
1317 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1318 constraints, operand_mode);
1320 /* For every operand, see what registers are allowed. */
1321 for (i = 0; i < noperands; i++)
1323 const char *p = constraints[i];
1324 /* For every alternative, we compute the class of registers allowed
1325 for reloading in CLS, and merge its contents into the reg set
1326 ALLOWED. */
1327 int cls = (int) NO_REGS;
1329 for (;;)
1331 char c = *p;
1333 if (c == '\0' || c == ',' || c == '#')
1335 /* End of one alternative - mark the regs in the current
1336 class, and reset the class. */
1337 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1338 cls = NO_REGS;
1339 p++;
1340 if (c == '#')
1341 do {
1342 c = *p++;
1343 } while (c != '\0' && c != ',');
1344 if (c == '\0')
1345 break;
1346 continue;
1349 switch (c)
1351 case '=': case '+': case '*': case '%': case '?': case '!':
1352 case '0': case '1': case '2': case '3': case '4': case 'm':
1353 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1354 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1355 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1356 case 'P':
1357 break;
1359 case 'p':
1360 cls = (int) reg_class_subunion[cls]
1361 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1362 break;
1364 case 'g':
1365 case 'r':
1366 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1367 break;
1369 default:
1370 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1371 cls = (int) reg_class_subunion[cls]
1372 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1373 else
1374 cls = (int) reg_class_subunion[cls]
1375 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1377 p += CONSTRAINT_LEN (c, p);
1380 /* Those of the registers which are clobbered, but allowed by the
1381 constraints, must be usable as reload registers. So clear them
1382 out of the life information. */
1383 AND_HARD_REG_SET (allowed, clobbered);
1384 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1385 if (TEST_HARD_REG_BIT (allowed, i))
1387 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1388 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1392 #endif
1395 /* Copy the global variables n_reloads and rld into the corresponding elts
1396 of CHAIN. */
1397 static void
1398 copy_reloads (struct insn_chain *chain)
1400 chain->n_reloads = n_reloads;
1401 chain->rld = obstack_alloc (&reload_obstack,
1402 n_reloads * sizeof (struct reload));
1403 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1404 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1407 /* Walk the chain of insns, and determine for each whether it needs reloads
1408 and/or eliminations. Build the corresponding insns_need_reload list, and
1409 set something_needs_elimination as appropriate. */
1410 static void
1411 calculate_needs_all_insns (int global)
1413 struct insn_chain **pprev_reload = &insns_need_reload;
1414 struct insn_chain *chain, *next = 0;
1416 something_needs_elimination = 0;
1418 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1419 for (chain = reload_insn_chain; chain != 0; chain = next)
1421 rtx insn = chain->insn;
1423 next = chain->next;
1425 /* Clear out the shortcuts. */
1426 chain->n_reloads = 0;
1427 chain->need_elim = 0;
1428 chain->need_reload = 0;
1429 chain->need_operand_change = 0;
1431 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1432 include REG_LABEL), we need to see what effects this has on the
1433 known offsets at labels. */
1435 if (LABEL_P (insn) || JUMP_P (insn)
1436 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1437 set_label_offsets (insn, insn, 0);
1439 if (INSN_P (insn))
1441 rtx old_body = PATTERN (insn);
1442 int old_code = INSN_CODE (insn);
1443 rtx old_notes = REG_NOTES (insn);
1444 int did_elimination = 0;
1445 int operands_changed = 0;
1446 rtx set = single_set (insn);
1448 /* Skip insns that only set an equivalence. */
1449 if (set && REG_P (SET_DEST (set))
1450 && reg_renumber[REGNO (SET_DEST (set))] < 0
1451 && reg_equiv_constant[REGNO (SET_DEST (set))])
1452 continue;
1454 /* If needed, eliminate any eliminable registers. */
1455 if (num_eliminable || num_eliminable_invariants)
1456 did_elimination = eliminate_regs_in_insn (insn, 0);
1458 /* Analyze the instruction. */
1459 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1460 global, spill_reg_order);
1462 /* If a no-op set needs more than one reload, this is likely
1463 to be something that needs input address reloads. We
1464 can't get rid of this cleanly later, and it is of no use
1465 anyway, so discard it now.
1466 We only do this when expensive_optimizations is enabled,
1467 since this complements reload inheritance / output
1468 reload deletion, and it can make debugging harder. */
1469 if (flag_expensive_optimizations && n_reloads > 1)
1471 rtx set = single_set (insn);
1472 if (set
1473 && SET_SRC (set) == SET_DEST (set)
1474 && REG_P (SET_SRC (set))
1475 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1477 delete_insn (insn);
1478 /* Delete it from the reload chain. */
1479 if (chain->prev)
1480 chain->prev->next = next;
1481 else
1482 reload_insn_chain = next;
1483 if (next)
1484 next->prev = chain->prev;
1485 chain->next = unused_insn_chains;
1486 unused_insn_chains = chain;
1487 continue;
1490 if (num_eliminable)
1491 update_eliminable_offsets ();
1493 /* Remember for later shortcuts which insns had any reloads or
1494 register eliminations. */
1495 chain->need_elim = did_elimination;
1496 chain->need_reload = n_reloads > 0;
1497 chain->need_operand_change = operands_changed;
1499 /* Discard any register replacements done. */
1500 if (did_elimination)
1502 obstack_free (&reload_obstack, reload_insn_firstobj);
1503 PATTERN (insn) = old_body;
1504 INSN_CODE (insn) = old_code;
1505 REG_NOTES (insn) = old_notes;
1506 something_needs_elimination = 1;
1509 something_needs_operands_changed |= operands_changed;
1511 if (n_reloads != 0)
1513 copy_reloads (chain);
1514 *pprev_reload = chain;
1515 pprev_reload = &chain->next_need_reload;
1519 *pprev_reload = 0;
1522 /* Comparison function for qsort to decide which of two reloads
1523 should be handled first. *P1 and *P2 are the reload numbers. */
1525 static int
1526 reload_reg_class_lower (const void *r1p, const void *r2p)
1528 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1529 int t;
1531 /* Consider required reloads before optional ones. */
1532 t = rld[r1].optional - rld[r2].optional;
1533 if (t != 0)
1534 return t;
1536 /* Count all solitary classes before non-solitary ones. */
1537 t = ((reg_class_size[(int) rld[r2].class] == 1)
1538 - (reg_class_size[(int) rld[r1].class] == 1));
1539 if (t != 0)
1540 return t;
1542 /* Aside from solitaires, consider all multi-reg groups first. */
1543 t = rld[r2].nregs - rld[r1].nregs;
1544 if (t != 0)
1545 return t;
1547 /* Consider reloads in order of increasing reg-class number. */
1548 t = (int) rld[r1].class - (int) rld[r2].class;
1549 if (t != 0)
1550 return t;
1552 /* If reloads are equally urgent, sort by reload number,
1553 so that the results of qsort leave nothing to chance. */
1554 return r1 - r2;
1557 /* The cost of spilling each hard reg. */
1558 static int spill_cost[FIRST_PSEUDO_REGISTER];
1560 /* When spilling multiple hard registers, we use SPILL_COST for the first
1561 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1562 only the first hard reg for a multi-reg pseudo. */
1563 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1565 /* Update the spill cost arrays, considering that pseudo REG is live. */
1567 static void
1568 count_pseudo (int reg)
1570 int freq = REG_FREQ (reg);
1571 int r = reg_renumber[reg];
1572 int nregs;
1574 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1575 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1576 return;
1578 SET_REGNO_REG_SET (&pseudos_counted, reg);
1580 if (r < 0)
1581 abort ();
1583 spill_add_cost[r] += freq;
1585 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1586 while (nregs-- > 0)
1587 spill_cost[r + nregs] += freq;
1590 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1591 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1593 static void
1594 order_regs_for_reload (struct insn_chain *chain)
1596 int i;
1597 HARD_REG_SET used_by_pseudos;
1598 HARD_REG_SET used_by_pseudos2;
1600 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1602 memset (spill_cost, 0, sizeof spill_cost);
1603 memset (spill_add_cost, 0, sizeof spill_add_cost);
1605 /* Count number of uses of each hard reg by pseudo regs allocated to it
1606 and then order them by decreasing use. First exclude hard registers
1607 that are live in or across this insn. */
1609 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1610 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1611 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1612 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1614 /* Now find out which pseudos are allocated to it, and update
1615 hard_reg_n_uses. */
1616 CLEAR_REG_SET (&pseudos_counted);
1618 EXECUTE_IF_SET_IN_REG_SET
1619 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
1621 count_pseudo (i);
1623 EXECUTE_IF_SET_IN_REG_SET
1624 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
1626 count_pseudo (i);
1628 CLEAR_REG_SET (&pseudos_counted);
1631 /* Vector of reload-numbers showing the order in which the reloads should
1632 be processed. */
1633 static short reload_order[MAX_RELOADS];
1635 /* This is used to keep track of the spill regs used in one insn. */
1636 static HARD_REG_SET used_spill_regs_local;
1638 /* We decided to spill hard register SPILLED, which has a size of
1639 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1640 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1641 update SPILL_COST/SPILL_ADD_COST. */
1643 static void
1644 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1646 int r = reg_renumber[reg];
1647 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1649 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1650 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1651 return;
1653 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1655 spill_add_cost[r] -= REG_FREQ (reg);
1656 while (nregs-- > 0)
1657 spill_cost[r + nregs] -= REG_FREQ (reg);
1660 /* Find reload register to use for reload number ORDER. */
1662 static int
1663 find_reg (struct insn_chain *chain, int order)
1665 int rnum = reload_order[order];
1666 struct reload *rl = rld + rnum;
1667 int best_cost = INT_MAX;
1668 int best_reg = -1;
1669 unsigned int i, j;
1670 int k;
1671 HARD_REG_SET not_usable;
1672 HARD_REG_SET used_by_other_reload;
1674 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1675 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1676 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1678 CLEAR_HARD_REG_SET (used_by_other_reload);
1679 for (k = 0; k < order; k++)
1681 int other = reload_order[k];
1683 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1684 for (j = 0; j < rld[other].nregs; j++)
1685 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1688 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1690 unsigned int regno = i;
1692 if (! TEST_HARD_REG_BIT (not_usable, regno)
1693 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1694 && HARD_REGNO_MODE_OK (regno, rl->mode))
1696 int this_cost = spill_cost[regno];
1697 int ok = 1;
1698 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1700 for (j = 1; j < this_nregs; j++)
1702 this_cost += spill_add_cost[regno + j];
1703 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1704 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1705 ok = 0;
1707 if (! ok)
1708 continue;
1709 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1710 this_cost--;
1711 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1712 this_cost--;
1713 if (this_cost < best_cost
1714 /* Among registers with equal cost, prefer caller-saved ones, or
1715 use REG_ALLOC_ORDER if it is defined. */
1716 || (this_cost == best_cost
1717 #ifdef REG_ALLOC_ORDER
1718 && (inv_reg_alloc_order[regno]
1719 < inv_reg_alloc_order[best_reg])
1720 #else
1721 && call_used_regs[regno]
1722 && ! call_used_regs[best_reg]
1723 #endif
1726 best_reg = regno;
1727 best_cost = this_cost;
1731 if (best_reg == -1)
1732 return 0;
1734 if (dump_file)
1735 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1737 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1738 rl->regno = best_reg;
1740 EXECUTE_IF_SET_IN_REG_SET
1741 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j,
1743 count_spilled_pseudo (best_reg, rl->nregs, j);
1746 EXECUTE_IF_SET_IN_REG_SET
1747 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j,
1749 count_spilled_pseudo (best_reg, rl->nregs, j);
1752 for (i = 0; i < rl->nregs; i++)
1754 if (spill_cost[best_reg + i] != 0
1755 || spill_add_cost[best_reg + i] != 0)
1756 abort ();
1757 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1759 return 1;
1762 /* Find more reload regs to satisfy the remaining need of an insn, which
1763 is given by CHAIN.
1764 Do it by ascending class number, since otherwise a reg
1765 might be spilled for a big class and might fail to count
1766 for a smaller class even though it belongs to that class. */
1768 static void
1769 find_reload_regs (struct insn_chain *chain)
1771 int i;
1773 /* In order to be certain of getting the registers we need,
1774 we must sort the reloads into order of increasing register class.
1775 Then our grabbing of reload registers will parallel the process
1776 that provided the reload registers. */
1777 for (i = 0; i < chain->n_reloads; i++)
1779 /* Show whether this reload already has a hard reg. */
1780 if (chain->rld[i].reg_rtx)
1782 int regno = REGNO (chain->rld[i].reg_rtx);
1783 chain->rld[i].regno = regno;
1784 chain->rld[i].nregs
1785 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1787 else
1788 chain->rld[i].regno = -1;
1789 reload_order[i] = i;
1792 n_reloads = chain->n_reloads;
1793 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1795 CLEAR_HARD_REG_SET (used_spill_regs_local);
1797 if (dump_file)
1798 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1800 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1802 /* Compute the order of preference for hard registers to spill. */
1804 order_regs_for_reload (chain);
1806 for (i = 0; i < n_reloads; i++)
1808 int r = reload_order[i];
1810 /* Ignore reloads that got marked inoperative. */
1811 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1812 && ! rld[r].optional
1813 && rld[r].regno == -1)
1814 if (! find_reg (chain, i))
1816 spill_failure (chain->insn, rld[r].class);
1817 failure = 1;
1818 return;
1822 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1823 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1825 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1828 static void
1829 select_reload_regs (void)
1831 struct insn_chain *chain;
1833 /* Try to satisfy the needs for each insn. */
1834 for (chain = insns_need_reload; chain != 0;
1835 chain = chain->next_need_reload)
1836 find_reload_regs (chain);
1839 /* Delete all insns that were inserted by emit_caller_save_insns during
1840 this iteration. */
1841 static void
1842 delete_caller_save_insns (void)
1844 struct insn_chain *c = reload_insn_chain;
1846 while (c != 0)
1848 while (c != 0 && c->is_caller_save_insn)
1850 struct insn_chain *next = c->next;
1851 rtx insn = c->insn;
1853 if (c == reload_insn_chain)
1854 reload_insn_chain = next;
1855 delete_insn (insn);
1857 if (next)
1858 next->prev = c->prev;
1859 if (c->prev)
1860 c->prev->next = next;
1861 c->next = unused_insn_chains;
1862 unused_insn_chains = c;
1863 c = next;
1865 if (c != 0)
1866 c = c->next;
1870 /* Handle the failure to find a register to spill.
1871 INSN should be one of the insns which needed this particular spill reg. */
1873 static void
1874 spill_failure (rtx insn, enum reg_class class)
1876 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1877 if (asm_noperands (PATTERN (insn)) >= 0)
1878 error_for_asm (insn, "can't find a register in class `%s' while reloading `asm'",
1879 reg_class_names[class]);
1880 else
1882 error ("unable to find a register to spill in class `%s'",
1883 reg_class_names[class]);
1884 fatal_insn ("this is the insn:", insn);
1888 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1889 data that is dead in INSN. */
1891 static void
1892 delete_dead_insn (rtx insn)
1894 rtx prev = prev_real_insn (insn);
1895 rtx prev_dest;
1897 /* If the previous insn sets a register that dies in our insn, delete it
1898 too. */
1899 if (prev && GET_CODE (PATTERN (prev)) == SET
1900 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
1901 && reg_mentioned_p (prev_dest, PATTERN (insn))
1902 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1903 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1904 delete_dead_insn (prev);
1906 SET_INSN_DELETED (insn);
1909 /* Modify the home of pseudo-reg I.
1910 The new home is present in reg_renumber[I].
1912 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1913 or it may be -1, meaning there is none or it is not relevant.
1914 This is used so that all pseudos spilled from a given hard reg
1915 can share one stack slot. */
1917 static void
1918 alter_reg (int i, int from_reg)
1920 /* When outputting an inline function, this can happen
1921 for a reg that isn't actually used. */
1922 if (regno_reg_rtx[i] == 0)
1923 return;
1925 /* If the reg got changed to a MEM at rtl-generation time,
1926 ignore it. */
1927 if (!REG_P (regno_reg_rtx[i]))
1928 return;
1930 /* Modify the reg-rtx to contain the new hard reg
1931 number or else to contain its pseudo reg number. */
1932 REGNO (regno_reg_rtx[i])
1933 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1935 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1936 allocate a stack slot for it. */
1938 if (reg_renumber[i] < 0
1939 && REG_N_REFS (i) > 0
1940 && reg_equiv_constant[i] == 0
1941 && reg_equiv_memory_loc[i] == 0)
1943 rtx x;
1944 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1945 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
1946 int adjust = 0;
1948 /* Each pseudo reg has an inherent size which comes from its own mode,
1949 and a total size which provides room for paradoxical subregs
1950 which refer to the pseudo reg in wider modes.
1952 We can use a slot already allocated if it provides both
1953 enough inherent space and enough total space.
1954 Otherwise, we allocate a new slot, making sure that it has no less
1955 inherent space, and no less total space, then the previous slot. */
1956 if (from_reg == -1)
1958 /* No known place to spill from => no slot to reuse. */
1959 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1960 inherent_size == total_size ? 0 : -1);
1961 if (BYTES_BIG_ENDIAN)
1962 /* Cancel the big-endian correction done in assign_stack_local.
1963 Get the address of the beginning of the slot.
1964 This is so we can do a big-endian correction unconditionally
1965 below. */
1966 adjust = inherent_size - total_size;
1968 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
1970 /* Nothing can alias this slot except this pseudo. */
1971 set_mem_alias_set (x, new_alias_set ());
1974 /* Reuse a stack slot if possible. */
1975 else if (spill_stack_slot[from_reg] != 0
1976 && spill_stack_slot_width[from_reg] >= total_size
1977 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1978 >= inherent_size))
1979 x = spill_stack_slot[from_reg];
1981 /* Allocate a bigger slot. */
1982 else
1984 /* Compute maximum size needed, both for inherent size
1985 and for total size. */
1986 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
1987 rtx stack_slot;
1989 if (spill_stack_slot[from_reg])
1991 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1992 > inherent_size)
1993 mode = GET_MODE (spill_stack_slot[from_reg]);
1994 if (spill_stack_slot_width[from_reg] > total_size)
1995 total_size = spill_stack_slot_width[from_reg];
1998 /* Make a slot with that size. */
1999 x = assign_stack_local (mode, total_size,
2000 inherent_size == total_size ? 0 : -1);
2001 stack_slot = x;
2003 /* All pseudos mapped to this slot can alias each other. */
2004 if (spill_stack_slot[from_reg])
2005 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
2006 else
2007 set_mem_alias_set (x, new_alias_set ());
2009 if (BYTES_BIG_ENDIAN)
2011 /* Cancel the big-endian correction done in assign_stack_local.
2012 Get the address of the beginning of the slot.
2013 This is so we can do a big-endian correction unconditionally
2014 below. */
2015 adjust = GET_MODE_SIZE (mode) - total_size;
2016 if (adjust)
2017 stack_slot
2018 = adjust_address_nv (x, mode_for_size (total_size
2019 * BITS_PER_UNIT,
2020 MODE_INT, 1),
2021 adjust);
2024 spill_stack_slot[from_reg] = stack_slot;
2025 spill_stack_slot_width[from_reg] = total_size;
2028 /* On a big endian machine, the "address" of the slot
2029 is the address of the low part that fits its inherent mode. */
2030 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2031 adjust += (total_size - inherent_size);
2033 /* If we have any adjustment to make, or if the stack slot is the
2034 wrong mode, make a new stack slot. */
2035 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2037 /* If we have a decl for the original register, set it for the
2038 memory. If this is a shared MEM, make a copy. */
2039 if (REG_EXPR (regno_reg_rtx[i])
2040 && TREE_CODE_CLASS (TREE_CODE (REG_EXPR (regno_reg_rtx[i]))) == 'd')
2042 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2044 /* We can do this only for the DECLs home pseudo, not for
2045 any copies of it, since otherwise when the stack slot
2046 is reused, nonoverlapping_memrefs_p might think they
2047 cannot overlap. */
2048 if (decl && REG_P (decl) && REGNO (decl) == (unsigned) i)
2050 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2051 x = copy_rtx (x);
2053 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2057 /* Save the stack slot for later. */
2058 reg_equiv_memory_loc[i] = x;
2062 /* Mark the slots in regs_ever_live for the hard regs
2063 used by pseudo-reg number REGNO. */
2065 void
2066 mark_home_live (int regno)
2068 int i, lim;
2070 i = reg_renumber[regno];
2071 if (i < 0)
2072 return;
2073 lim = i + hard_regno_nregs[i][PSEUDO_REGNO_MODE (regno)];
2074 while (i < lim)
2075 regs_ever_live[i++] = 1;
2078 /* This function handles the tracking of elimination offsets around branches.
2080 X is a piece of RTL being scanned.
2082 INSN is the insn that it came from, if any.
2084 INITIAL_P is nonzero if we are to set the offset to be the initial
2085 offset and zero if we are setting the offset of the label to be the
2086 current offset. */
2088 static void
2089 set_label_offsets (rtx x, rtx insn, int initial_p)
2091 enum rtx_code code = GET_CODE (x);
2092 rtx tem;
2093 unsigned int i;
2094 struct elim_table *p;
2096 switch (code)
2098 case LABEL_REF:
2099 if (LABEL_REF_NONLOCAL_P (x))
2100 return;
2102 x = XEXP (x, 0);
2104 /* ... fall through ... */
2106 case CODE_LABEL:
2107 /* If we know nothing about this label, set the desired offsets. Note
2108 that this sets the offset at a label to be the offset before a label
2109 if we don't know anything about the label. This is not correct for
2110 the label after a BARRIER, but is the best guess we can make. If
2111 we guessed wrong, we will suppress an elimination that might have
2112 been possible had we been able to guess correctly. */
2114 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2116 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2117 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2118 = (initial_p ? reg_eliminate[i].initial_offset
2119 : reg_eliminate[i].offset);
2120 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2123 /* Otherwise, if this is the definition of a label and it is
2124 preceded by a BARRIER, set our offsets to the known offset of
2125 that label. */
2127 else if (x == insn
2128 && (tem = prev_nonnote_insn (insn)) != 0
2129 && BARRIER_P (tem))
2130 set_offsets_for_label (insn);
2131 else
2132 /* If neither of the above cases is true, compare each offset
2133 with those previously recorded and suppress any eliminations
2134 where the offsets disagree. */
2136 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2137 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2138 != (initial_p ? reg_eliminate[i].initial_offset
2139 : reg_eliminate[i].offset))
2140 reg_eliminate[i].can_eliminate = 0;
2142 return;
2144 case JUMP_INSN:
2145 set_label_offsets (PATTERN (insn), insn, initial_p);
2147 /* ... fall through ... */
2149 case INSN:
2150 case CALL_INSN:
2151 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2152 and hence must have all eliminations at their initial offsets. */
2153 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2154 if (REG_NOTE_KIND (tem) == REG_LABEL)
2155 set_label_offsets (XEXP (tem, 0), insn, 1);
2156 return;
2158 case PARALLEL:
2159 case ADDR_VEC:
2160 case ADDR_DIFF_VEC:
2161 /* Each of the labels in the parallel or address vector must be
2162 at their initial offsets. We want the first field for PARALLEL
2163 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2165 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2166 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2167 insn, initial_p);
2168 return;
2170 case SET:
2171 /* We only care about setting PC. If the source is not RETURN,
2172 IF_THEN_ELSE, or a label, disable any eliminations not at
2173 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2174 isn't one of those possibilities. For branches to a label,
2175 call ourselves recursively.
2177 Note that this can disable elimination unnecessarily when we have
2178 a non-local goto since it will look like a non-constant jump to
2179 someplace in the current function. This isn't a significant
2180 problem since such jumps will normally be when all elimination
2181 pairs are back to their initial offsets. */
2183 if (SET_DEST (x) != pc_rtx)
2184 return;
2186 switch (GET_CODE (SET_SRC (x)))
2188 case PC:
2189 case RETURN:
2190 return;
2192 case LABEL_REF:
2193 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2194 return;
2196 case IF_THEN_ELSE:
2197 tem = XEXP (SET_SRC (x), 1);
2198 if (GET_CODE (tem) == LABEL_REF)
2199 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2200 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2201 break;
2203 tem = XEXP (SET_SRC (x), 2);
2204 if (GET_CODE (tem) == LABEL_REF)
2205 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2206 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2207 break;
2208 return;
2210 default:
2211 break;
2214 /* If we reach here, all eliminations must be at their initial
2215 offset because we are doing a jump to a variable address. */
2216 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2217 if (p->offset != p->initial_offset)
2218 p->can_eliminate = 0;
2219 break;
2221 default:
2222 break;
2226 /* Scan X and replace any eliminable registers (such as fp) with a
2227 replacement (such as sp), plus an offset.
2229 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2230 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2231 MEM, we are allowed to replace a sum of a register and the constant zero
2232 with the register, which we cannot do outside a MEM. In addition, we need
2233 to record the fact that a register is referenced outside a MEM.
2235 If INSN is an insn, it is the insn containing X. If we replace a REG
2236 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2237 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2238 the REG is being modified.
2240 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2241 That's used when we eliminate in expressions stored in notes.
2242 This means, do not set ref_outside_mem even if the reference
2243 is outside of MEMs.
2245 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2246 replacements done assuming all offsets are at their initial values. If
2247 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2248 encounter, return the actual location so that find_reloads will do
2249 the proper thing. */
2252 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2254 enum rtx_code code = GET_CODE (x);
2255 struct elim_table *ep;
2256 int regno;
2257 rtx new;
2258 int i, j;
2259 const char *fmt;
2260 int copied = 0;
2262 if (! current_function_decl)
2263 return x;
2265 switch (code)
2267 case CONST_INT:
2268 case CONST_DOUBLE:
2269 case CONST_VECTOR:
2270 case CONST:
2271 case SYMBOL_REF:
2272 case CODE_LABEL:
2273 case PC:
2274 case CC0:
2275 case ASM_INPUT:
2276 case ADDR_VEC:
2277 case ADDR_DIFF_VEC:
2278 case RETURN:
2279 return x;
2281 case REG:
2282 regno = REGNO (x);
2284 /* First handle the case where we encounter a bare register that
2285 is eliminable. Replace it with a PLUS. */
2286 if (regno < FIRST_PSEUDO_REGISTER)
2288 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2289 ep++)
2290 if (ep->from_rtx == x && ep->can_eliminate)
2291 return plus_constant (ep->to_rtx, ep->previous_offset);
2294 else if (reg_renumber && reg_renumber[regno] < 0
2295 && reg_equiv_constant && reg_equiv_constant[regno]
2296 && ! CONSTANT_P (reg_equiv_constant[regno]))
2297 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2298 mem_mode, insn);
2299 return x;
2301 /* You might think handling MINUS in a manner similar to PLUS is a
2302 good idea. It is not. It has been tried multiple times and every
2303 time the change has had to have been reverted.
2305 Other parts of reload know a PLUS is special (gen_reload for example)
2306 and require special code to handle code a reloaded PLUS operand.
2308 Also consider backends where the flags register is clobbered by a
2309 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2310 lea instruction comes to mind). If we try to reload a MINUS, we
2311 may kill the flags register that was holding a useful value.
2313 So, please before trying to handle MINUS, consider reload as a
2314 whole instead of this little section as well as the backend issues. */
2315 case PLUS:
2316 /* If this is the sum of an eliminable register and a constant, rework
2317 the sum. */
2318 if (REG_P (XEXP (x, 0))
2319 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2320 && CONSTANT_P (XEXP (x, 1)))
2322 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2323 ep++)
2324 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2326 /* The only time we want to replace a PLUS with a REG (this
2327 occurs when the constant operand of the PLUS is the negative
2328 of the offset) is when we are inside a MEM. We won't want
2329 to do so at other times because that would change the
2330 structure of the insn in a way that reload can't handle.
2331 We special-case the commonest situation in
2332 eliminate_regs_in_insn, so just replace a PLUS with a
2333 PLUS here, unless inside a MEM. */
2334 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2335 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2336 return ep->to_rtx;
2337 else
2338 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2339 plus_constant (XEXP (x, 1),
2340 ep->previous_offset));
2343 /* If the register is not eliminable, we are done since the other
2344 operand is a constant. */
2345 return x;
2348 /* If this is part of an address, we want to bring any constant to the
2349 outermost PLUS. We will do this by doing register replacement in
2350 our operands and seeing if a constant shows up in one of them.
2352 Note that there is no risk of modifying the structure of the insn,
2353 since we only get called for its operands, thus we are either
2354 modifying the address inside a MEM, or something like an address
2355 operand of a load-address insn. */
2358 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2359 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2361 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2363 /* If one side is a PLUS and the other side is a pseudo that
2364 didn't get a hard register but has a reg_equiv_constant,
2365 we must replace the constant here since it may no longer
2366 be in the position of any operand. */
2367 if (GET_CODE (new0) == PLUS && REG_P (new1)
2368 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2369 && reg_renumber[REGNO (new1)] < 0
2370 && reg_equiv_constant != 0
2371 && reg_equiv_constant[REGNO (new1)] != 0)
2372 new1 = reg_equiv_constant[REGNO (new1)];
2373 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2374 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2375 && reg_renumber[REGNO (new0)] < 0
2376 && reg_equiv_constant[REGNO (new0)] != 0)
2377 new0 = reg_equiv_constant[REGNO (new0)];
2379 new = form_sum (new0, new1);
2381 /* As above, if we are not inside a MEM we do not want to
2382 turn a PLUS into something else. We might try to do so here
2383 for an addition of 0 if we aren't optimizing. */
2384 if (! mem_mode && GET_CODE (new) != PLUS)
2385 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2386 else
2387 return new;
2390 return x;
2392 case MULT:
2393 /* If this is the product of an eliminable register and a
2394 constant, apply the distribute law and move the constant out
2395 so that we have (plus (mult ..) ..). This is needed in order
2396 to keep load-address insns valid. This case is pathological.
2397 We ignore the possibility of overflow here. */
2398 if (REG_P (XEXP (x, 0))
2399 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2400 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2401 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2402 ep++)
2403 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2405 if (! mem_mode
2406 /* Refs inside notes don't count for this purpose. */
2407 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2408 || GET_CODE (insn) == INSN_LIST)))
2409 ep->ref_outside_mem = 1;
2411 return
2412 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2413 ep->previous_offset * INTVAL (XEXP (x, 1)));
2416 /* ... fall through ... */
2418 case CALL:
2419 case COMPARE:
2420 /* See comments before PLUS about handling MINUS. */
2421 case MINUS:
2422 case DIV: case UDIV:
2423 case MOD: case UMOD:
2424 case AND: case IOR: case XOR:
2425 case ROTATERT: case ROTATE:
2426 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2427 case NE: case EQ:
2428 case GE: case GT: case GEU: case GTU:
2429 case LE: case LT: case LEU: case LTU:
2431 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2432 rtx new1
2433 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2435 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2436 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2438 return x;
2440 case EXPR_LIST:
2441 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2442 if (XEXP (x, 0))
2444 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2445 if (new != XEXP (x, 0))
2447 /* If this is a REG_DEAD note, it is not valid anymore.
2448 Using the eliminated version could result in creating a
2449 REG_DEAD note for the stack or frame pointer. */
2450 if (GET_MODE (x) == REG_DEAD)
2451 return (XEXP (x, 1)
2452 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2453 : NULL_RTX);
2455 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2459 /* ... fall through ... */
2461 case INSN_LIST:
2462 /* Now do eliminations in the rest of the chain. If this was
2463 an EXPR_LIST, this might result in allocating more memory than is
2464 strictly needed, but it simplifies the code. */
2465 if (XEXP (x, 1))
2467 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2468 if (new != XEXP (x, 1))
2469 return
2470 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2472 return x;
2474 case PRE_INC:
2475 case POST_INC:
2476 case PRE_DEC:
2477 case POST_DEC:
2478 case STRICT_LOW_PART:
2479 case NEG: case NOT:
2480 case SIGN_EXTEND: case ZERO_EXTEND:
2481 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2482 case FLOAT: case FIX:
2483 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2484 case ABS:
2485 case SQRT:
2486 case FFS:
2487 case CLZ:
2488 case CTZ:
2489 case POPCOUNT:
2490 case PARITY:
2491 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2492 if (new != XEXP (x, 0))
2493 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2494 return x;
2496 case SUBREG:
2497 /* Similar to above processing, but preserve SUBREG_BYTE.
2498 Convert (subreg (mem)) to (mem) if not paradoxical.
2499 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2500 pseudo didn't get a hard reg, we must replace this with the
2501 eliminated version of the memory location because push_reload
2502 may do the replacement in certain circumstances. */
2503 if (REG_P (SUBREG_REG (x))
2504 && (GET_MODE_SIZE (GET_MODE (x))
2505 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2506 && reg_equiv_memory_loc != 0
2507 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2509 new = SUBREG_REG (x);
2511 else
2512 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2514 if (new != SUBREG_REG (x))
2516 int x_size = GET_MODE_SIZE (GET_MODE (x));
2517 int new_size = GET_MODE_SIZE (GET_MODE (new));
2519 if (MEM_P (new)
2520 && ((x_size < new_size
2521 #ifdef WORD_REGISTER_OPERATIONS
2522 /* On these machines, combine can create rtl of the form
2523 (set (subreg:m1 (reg:m2 R) 0) ...)
2524 where m1 < m2, and expects something interesting to
2525 happen to the entire word. Moreover, it will use the
2526 (reg:m2 R) later, expecting all bits to be preserved.
2527 So if the number of words is the same, preserve the
2528 subreg so that push_reload can see it. */
2529 && ! ((x_size - 1) / UNITS_PER_WORD
2530 == (new_size -1 ) / UNITS_PER_WORD)
2531 #endif
2533 || x_size == new_size)
2535 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2536 else
2537 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2540 return x;
2542 case MEM:
2543 /* Our only special processing is to pass the mode of the MEM to our
2544 recursive call and copy the flags. While we are here, handle this
2545 case more efficiently. */
2546 return
2547 replace_equiv_address_nv (x,
2548 eliminate_regs (XEXP (x, 0),
2549 GET_MODE (x), insn));
2551 case USE:
2552 /* Handle insn_list USE that a call to a pure function may generate. */
2553 new = eliminate_regs (XEXP (x, 0), 0, insn);
2554 if (new != XEXP (x, 0))
2555 return gen_rtx_USE (GET_MODE (x), new);
2556 return x;
2558 case CLOBBER:
2559 case ASM_OPERANDS:
2560 case SET:
2561 abort ();
2563 default:
2564 break;
2567 /* Process each of our operands recursively. If any have changed, make a
2568 copy of the rtx. */
2569 fmt = GET_RTX_FORMAT (code);
2570 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2572 if (*fmt == 'e')
2574 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
2575 if (new != XEXP (x, i) && ! copied)
2577 rtx new_x = rtx_alloc (code);
2578 memcpy (new_x, x, RTX_SIZE (code));
2579 x = new_x;
2580 copied = 1;
2582 XEXP (x, i) = new;
2584 else if (*fmt == 'E')
2586 int copied_vec = 0;
2587 for (j = 0; j < XVECLEN (x, i); j++)
2589 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2590 if (new != XVECEXP (x, i, j) && ! copied_vec)
2592 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2593 XVEC (x, i)->elem);
2594 if (! copied)
2596 rtx new_x = rtx_alloc (code);
2597 memcpy (new_x, x, RTX_SIZE (code));
2598 x = new_x;
2599 copied = 1;
2601 XVEC (x, i) = new_v;
2602 copied_vec = 1;
2604 XVECEXP (x, i, j) = new;
2609 return x;
2612 /* Scan rtx X for modifications of elimination target registers. Update
2613 the table of eliminables to reflect the changed state. MEM_MODE is
2614 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2616 static void
2617 elimination_effects (rtx x, enum machine_mode mem_mode)
2619 enum rtx_code code = GET_CODE (x);
2620 struct elim_table *ep;
2621 int regno;
2622 int i, j;
2623 const char *fmt;
2625 switch (code)
2627 case CONST_INT:
2628 case CONST_DOUBLE:
2629 case CONST_VECTOR:
2630 case CONST:
2631 case SYMBOL_REF:
2632 case CODE_LABEL:
2633 case PC:
2634 case CC0:
2635 case ASM_INPUT:
2636 case ADDR_VEC:
2637 case ADDR_DIFF_VEC:
2638 case RETURN:
2639 return;
2641 case REG:
2642 regno = REGNO (x);
2644 /* First handle the case where we encounter a bare register that
2645 is eliminable. Replace it with a PLUS. */
2646 if (regno < FIRST_PSEUDO_REGISTER)
2648 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2649 ep++)
2650 if (ep->from_rtx == x && ep->can_eliminate)
2652 if (! mem_mode)
2653 ep->ref_outside_mem = 1;
2654 return;
2658 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2659 && reg_equiv_constant[regno]
2660 && ! function_invariant_p (reg_equiv_constant[regno]))
2661 elimination_effects (reg_equiv_constant[regno], mem_mode);
2662 return;
2664 case PRE_INC:
2665 case POST_INC:
2666 case PRE_DEC:
2667 case POST_DEC:
2668 case POST_MODIFY:
2669 case PRE_MODIFY:
2670 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2671 if (ep->to_rtx == XEXP (x, 0))
2673 int size = GET_MODE_SIZE (mem_mode);
2675 /* If more bytes than MEM_MODE are pushed, account for them. */
2676 #ifdef PUSH_ROUNDING
2677 if (ep->to_rtx == stack_pointer_rtx)
2678 size = PUSH_ROUNDING (size);
2679 #endif
2680 if (code == PRE_DEC || code == POST_DEC)
2681 ep->offset += size;
2682 else if (code == PRE_INC || code == POST_INC)
2683 ep->offset -= size;
2684 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2685 && GET_CODE (XEXP (x, 1)) == PLUS
2686 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2687 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2688 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2691 /* These two aren't unary operators. */
2692 if (code == POST_MODIFY || code == PRE_MODIFY)
2693 break;
2695 /* Fall through to generic unary operation case. */
2696 case STRICT_LOW_PART:
2697 case NEG: case NOT:
2698 case SIGN_EXTEND: case ZERO_EXTEND:
2699 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2700 case FLOAT: case FIX:
2701 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2702 case ABS:
2703 case SQRT:
2704 case FFS:
2705 case CLZ:
2706 case CTZ:
2707 case POPCOUNT:
2708 case PARITY:
2709 elimination_effects (XEXP (x, 0), mem_mode);
2710 return;
2712 case SUBREG:
2713 if (REG_P (SUBREG_REG (x))
2714 && (GET_MODE_SIZE (GET_MODE (x))
2715 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2716 && reg_equiv_memory_loc != 0
2717 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2718 return;
2720 elimination_effects (SUBREG_REG (x), mem_mode);
2721 return;
2723 case USE:
2724 /* If using a register that is the source of an eliminate we still
2725 think can be performed, note it cannot be performed since we don't
2726 know how this register is used. */
2727 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2728 if (ep->from_rtx == XEXP (x, 0))
2729 ep->can_eliminate = 0;
2731 elimination_effects (XEXP (x, 0), mem_mode);
2732 return;
2734 case CLOBBER:
2735 /* If clobbering a register that is the replacement register for an
2736 elimination we still think can be performed, note that it cannot
2737 be performed. Otherwise, we need not be concerned about it. */
2738 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2739 if (ep->to_rtx == XEXP (x, 0))
2740 ep->can_eliminate = 0;
2742 elimination_effects (XEXP (x, 0), mem_mode);
2743 return;
2745 case SET:
2746 /* Check for setting a register that we know about. */
2747 if (REG_P (SET_DEST (x)))
2749 /* See if this is setting the replacement register for an
2750 elimination.
2752 If DEST is the hard frame pointer, we do nothing because we
2753 assume that all assignments to the frame pointer are for
2754 non-local gotos and are being done at a time when they are valid
2755 and do not disturb anything else. Some machines want to
2756 eliminate a fake argument pointer (or even a fake frame pointer)
2757 with either the real frame or the stack pointer. Assignments to
2758 the hard frame pointer must not prevent this elimination. */
2760 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2761 ep++)
2762 if (ep->to_rtx == SET_DEST (x)
2763 && SET_DEST (x) != hard_frame_pointer_rtx)
2765 /* If it is being incremented, adjust the offset. Otherwise,
2766 this elimination can't be done. */
2767 rtx src = SET_SRC (x);
2769 if (GET_CODE (src) == PLUS
2770 && XEXP (src, 0) == SET_DEST (x)
2771 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2772 ep->offset -= INTVAL (XEXP (src, 1));
2773 else
2774 ep->can_eliminate = 0;
2778 elimination_effects (SET_DEST (x), 0);
2779 elimination_effects (SET_SRC (x), 0);
2780 return;
2782 case MEM:
2783 /* Our only special processing is to pass the mode of the MEM to our
2784 recursive call. */
2785 elimination_effects (XEXP (x, 0), GET_MODE (x));
2786 return;
2788 default:
2789 break;
2792 fmt = GET_RTX_FORMAT (code);
2793 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2795 if (*fmt == 'e')
2796 elimination_effects (XEXP (x, i), mem_mode);
2797 else if (*fmt == 'E')
2798 for (j = 0; j < XVECLEN (x, i); j++)
2799 elimination_effects (XVECEXP (x, i, j), mem_mode);
2803 /* Descend through rtx X and verify that no references to eliminable registers
2804 remain. If any do remain, mark the involved register as not
2805 eliminable. */
2807 static void
2808 check_eliminable_occurrences (rtx x)
2810 const char *fmt;
2811 int i;
2812 enum rtx_code code;
2814 if (x == 0)
2815 return;
2817 code = GET_CODE (x);
2819 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2821 struct elim_table *ep;
2823 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2824 if (ep->from_rtx == x)
2825 ep->can_eliminate = 0;
2826 return;
2829 fmt = GET_RTX_FORMAT (code);
2830 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2832 if (*fmt == 'e')
2833 check_eliminable_occurrences (XEXP (x, i));
2834 else if (*fmt == 'E')
2836 int j;
2837 for (j = 0; j < XVECLEN (x, i); j++)
2838 check_eliminable_occurrences (XVECEXP (x, i, j));
2843 /* Scan INSN and eliminate all eliminable registers in it.
2845 If REPLACE is nonzero, do the replacement destructively. Also
2846 delete the insn as dead it if it is setting an eliminable register.
2848 If REPLACE is zero, do all our allocations in reload_obstack.
2850 If no eliminations were done and this insn doesn't require any elimination
2851 processing (these are not identical conditions: it might be updating sp,
2852 but not referencing fp; this needs to be seen during reload_as_needed so
2853 that the offset between fp and sp can be taken into consideration), zero
2854 is returned. Otherwise, 1 is returned. */
2856 static int
2857 eliminate_regs_in_insn (rtx insn, int replace)
2859 int icode = recog_memoized (insn);
2860 rtx old_body = PATTERN (insn);
2861 int insn_is_asm = asm_noperands (old_body) >= 0;
2862 rtx old_set = single_set (insn);
2863 rtx new_body;
2864 int val = 0;
2865 int i;
2866 rtx substed_operand[MAX_RECOG_OPERANDS];
2867 rtx orig_operand[MAX_RECOG_OPERANDS];
2868 struct elim_table *ep;
2869 rtx plus_src;
2871 if (! insn_is_asm && icode < 0)
2873 if (GET_CODE (PATTERN (insn)) == USE
2874 || GET_CODE (PATTERN (insn)) == CLOBBER
2875 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2876 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2877 || GET_CODE (PATTERN (insn)) == ASM_INPUT)
2878 return 0;
2879 abort ();
2882 if (old_set != 0 && REG_P (SET_DEST (old_set))
2883 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2885 /* Check for setting an eliminable register. */
2886 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2887 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2889 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2890 /* If this is setting the frame pointer register to the
2891 hardware frame pointer register and this is an elimination
2892 that will be done (tested above), this insn is really
2893 adjusting the frame pointer downward to compensate for
2894 the adjustment done before a nonlocal goto. */
2895 if (ep->from == FRAME_POINTER_REGNUM
2896 && ep->to == HARD_FRAME_POINTER_REGNUM)
2898 rtx base = SET_SRC (old_set);
2899 rtx base_insn = insn;
2900 HOST_WIDE_INT offset = 0;
2902 while (base != ep->to_rtx)
2904 rtx prev_insn, prev_set;
2906 if (GET_CODE (base) == PLUS
2907 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2909 offset += INTVAL (XEXP (base, 1));
2910 base = XEXP (base, 0);
2912 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2913 && (prev_set = single_set (prev_insn)) != 0
2914 && rtx_equal_p (SET_DEST (prev_set), base))
2916 base = SET_SRC (prev_set);
2917 base_insn = prev_insn;
2919 else
2920 break;
2923 if (base == ep->to_rtx)
2925 rtx src
2926 = plus_constant (ep->to_rtx, offset - ep->offset);
2928 new_body = old_body;
2929 if (! replace)
2931 new_body = copy_insn (old_body);
2932 if (REG_NOTES (insn))
2933 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
2935 PATTERN (insn) = new_body;
2936 old_set = single_set (insn);
2938 /* First see if this insn remains valid when we
2939 make the change. If not, keep the INSN_CODE
2940 the same and let reload fit it up. */
2941 validate_change (insn, &SET_SRC (old_set), src, 1);
2942 validate_change (insn, &SET_DEST (old_set),
2943 ep->to_rtx, 1);
2944 if (! apply_change_group ())
2946 SET_SRC (old_set) = src;
2947 SET_DEST (old_set) = ep->to_rtx;
2950 val = 1;
2951 goto done;
2954 #endif
2956 /* In this case this insn isn't serving a useful purpose. We
2957 will delete it in reload_as_needed once we know that this
2958 elimination is, in fact, being done.
2960 If REPLACE isn't set, we can't delete this insn, but needn't
2961 process it since it won't be used unless something changes. */
2962 if (replace)
2964 delete_dead_insn (insn);
2965 return 1;
2967 val = 1;
2968 goto done;
2972 /* We allow one special case which happens to work on all machines we
2973 currently support: a single set with the source or a REG_EQUAL
2974 note being a PLUS of an eliminable register and a constant. */
2975 plus_src = 0;
2976 if (old_set && REG_P (SET_DEST (old_set)))
2978 /* First see if the source is of the form (plus (reg) CST). */
2979 if (GET_CODE (SET_SRC (old_set)) == PLUS
2980 && REG_P (XEXP (SET_SRC (old_set), 0))
2981 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT
2982 && REGNO (XEXP (SET_SRC (old_set), 0)) < FIRST_PSEUDO_REGISTER)
2983 plus_src = SET_SRC (old_set);
2984 else if (REG_P (SET_SRC (old_set)))
2986 /* Otherwise, see if we have a REG_EQUAL note of the form
2987 (plus (reg) CST). */
2988 rtx links;
2989 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
2991 if (REG_NOTE_KIND (links) == REG_EQUAL
2992 && GET_CODE (XEXP (links, 0)) == PLUS
2993 && REG_P (XEXP (XEXP (links, 0), 0))
2994 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT
2995 && REGNO (XEXP (XEXP (links, 0), 0)) < FIRST_PSEUDO_REGISTER)
2997 plus_src = XEXP (links, 0);
2998 break;
3003 if (plus_src)
3005 rtx reg = XEXP (plus_src, 0);
3006 HOST_WIDE_INT offset = INTVAL (XEXP (plus_src, 1));
3008 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3009 if (ep->from_rtx == reg && ep->can_eliminate)
3011 offset += ep->offset;
3013 if (offset == 0)
3015 int num_clobbers;
3016 /* We assume here that if we need a PARALLEL with
3017 CLOBBERs for this assignment, we can do with the
3018 MATCH_SCRATCHes that add_clobbers allocates.
3019 There's not much we can do if that doesn't work. */
3020 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3021 SET_DEST (old_set),
3022 ep->to_rtx);
3023 num_clobbers = 0;
3024 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3025 if (num_clobbers)
3027 rtvec vec = rtvec_alloc (num_clobbers + 1);
3029 vec->elem[0] = PATTERN (insn);
3030 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3031 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3033 if (INSN_CODE (insn) < 0)
3034 abort ();
3036 /* If we have a nonzero offset, and the source is already
3037 a simple REG, the following transformation would
3038 increase the cost of the insn by replacing a simple REG
3039 with (plus (reg sp) CST). So try only when plus_src
3040 comes from old_set proper, not REG_NOTES. */
3041 else if (SET_SRC (old_set) == plus_src)
3043 new_body = old_body;
3044 if (! replace)
3046 new_body = copy_insn (old_body);
3047 if (REG_NOTES (insn))
3048 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3050 PATTERN (insn) = new_body;
3051 old_set = single_set (insn);
3053 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3054 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3056 else
3057 break;
3059 val = 1;
3060 /* This can't have an effect on elimination offsets, so skip right
3061 to the end. */
3062 goto done;
3066 /* Determine the effects of this insn on elimination offsets. */
3067 elimination_effects (old_body, 0);
3069 /* Eliminate all eliminable registers occurring in operands that
3070 can be handled by reload. */
3071 extract_insn (insn);
3072 for (i = 0; i < recog_data.n_operands; i++)
3074 orig_operand[i] = recog_data.operand[i];
3075 substed_operand[i] = recog_data.operand[i];
3077 /* For an asm statement, every operand is eliminable. */
3078 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3080 /* Check for setting a register that we know about. */
3081 if (recog_data.operand_type[i] != OP_IN
3082 && REG_P (orig_operand[i]))
3084 /* If we are assigning to a register that can be eliminated, it
3085 must be as part of a PARALLEL, since the code above handles
3086 single SETs. We must indicate that we can no longer
3087 eliminate this reg. */
3088 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3089 ep++)
3090 if (ep->from_rtx == orig_operand[i])
3091 ep->can_eliminate = 0;
3094 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3095 replace ? insn : NULL_RTX);
3096 if (substed_operand[i] != orig_operand[i])
3097 val = 1;
3098 /* Terminate the search in check_eliminable_occurrences at
3099 this point. */
3100 *recog_data.operand_loc[i] = 0;
3102 /* If an output operand changed from a REG to a MEM and INSN is an
3103 insn, write a CLOBBER insn. */
3104 if (recog_data.operand_type[i] != OP_IN
3105 && REG_P (orig_operand[i])
3106 && MEM_P (substed_operand[i])
3107 && replace)
3108 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3109 insn);
3113 for (i = 0; i < recog_data.n_dups; i++)
3114 *recog_data.dup_loc[i]
3115 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3117 /* If any eliminable remain, they aren't eliminable anymore. */
3118 check_eliminable_occurrences (old_body);
3120 /* Substitute the operands; the new values are in the substed_operand
3121 array. */
3122 for (i = 0; i < recog_data.n_operands; i++)
3123 *recog_data.operand_loc[i] = substed_operand[i];
3124 for (i = 0; i < recog_data.n_dups; i++)
3125 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3127 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3128 re-recognize the insn. We do this in case we had a simple addition
3129 but now can do this as a load-address. This saves an insn in this
3130 common case.
3131 If re-recognition fails, the old insn code number will still be used,
3132 and some register operands may have changed into PLUS expressions.
3133 These will be handled by find_reloads by loading them into a register
3134 again. */
3136 if (val)
3138 /* If we aren't replacing things permanently and we changed something,
3139 make another copy to ensure that all the RTL is new. Otherwise
3140 things can go wrong if find_reload swaps commutative operands
3141 and one is inside RTL that has been copied while the other is not. */
3142 new_body = old_body;
3143 if (! replace)
3145 new_body = copy_insn (old_body);
3146 if (REG_NOTES (insn))
3147 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3149 PATTERN (insn) = new_body;
3151 /* If we had a move insn but now we don't, rerecognize it. This will
3152 cause spurious re-recognition if the old move had a PARALLEL since
3153 the new one still will, but we can't call single_set without
3154 having put NEW_BODY into the insn and the re-recognition won't
3155 hurt in this rare case. */
3156 /* ??? Why this huge if statement - why don't we just rerecognize the
3157 thing always? */
3158 if (! insn_is_asm
3159 && old_set != 0
3160 && ((REG_P (SET_SRC (old_set))
3161 && (GET_CODE (new_body) != SET
3162 || !REG_P (SET_SRC (new_body))))
3163 /* If this was a load from or store to memory, compare
3164 the MEM in recog_data.operand to the one in the insn.
3165 If they are not equal, then rerecognize the insn. */
3166 || (old_set != 0
3167 && ((MEM_P (SET_SRC (old_set))
3168 && SET_SRC (old_set) != recog_data.operand[1])
3169 || (MEM_P (SET_DEST (old_set))
3170 && SET_DEST (old_set) != recog_data.operand[0])))
3171 /* If this was an add insn before, rerecognize. */
3172 || GET_CODE (SET_SRC (old_set)) == PLUS))
3174 int new_icode = recog (PATTERN (insn), insn, 0);
3175 if (new_icode < 0)
3176 INSN_CODE (insn) = icode;
3180 /* Restore the old body. If there were any changes to it, we made a copy
3181 of it while the changes were still in place, so we'll correctly return
3182 a modified insn below. */
3183 if (! replace)
3185 /* Restore the old body. */
3186 for (i = 0; i < recog_data.n_operands; i++)
3187 *recog_data.operand_loc[i] = orig_operand[i];
3188 for (i = 0; i < recog_data.n_dups; i++)
3189 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3192 /* Update all elimination pairs to reflect the status after the current
3193 insn. The changes we make were determined by the earlier call to
3194 elimination_effects.
3196 We also detect cases where register elimination cannot be done,
3197 namely, if a register would be both changed and referenced outside a MEM
3198 in the resulting insn since such an insn is often undefined and, even if
3199 not, we cannot know what meaning will be given to it. Note that it is
3200 valid to have a register used in an address in an insn that changes it
3201 (presumably with a pre- or post-increment or decrement).
3203 If anything changes, return nonzero. */
3205 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3207 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3208 ep->can_eliminate = 0;
3210 ep->ref_outside_mem = 0;
3212 if (ep->previous_offset != ep->offset)
3213 val = 1;
3216 done:
3217 /* If we changed something, perform elimination in REG_NOTES. This is
3218 needed even when REPLACE is zero because a REG_DEAD note might refer
3219 to a register that we eliminate and could cause a different number
3220 of spill registers to be needed in the final reload pass than in
3221 the pre-passes. */
3222 if (val && REG_NOTES (insn) != 0)
3223 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3225 return val;
3228 /* Loop through all elimination pairs.
3229 Recalculate the number not at initial offset.
3231 Compute the maximum offset (minimum offset if the stack does not
3232 grow downward) for each elimination pair. */
3234 static void
3235 update_eliminable_offsets (void)
3237 struct elim_table *ep;
3239 num_not_at_initial_offset = 0;
3240 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3242 ep->previous_offset = ep->offset;
3243 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3244 num_not_at_initial_offset++;
3248 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3249 replacement we currently believe is valid, mark it as not eliminable if X
3250 modifies DEST in any way other than by adding a constant integer to it.
3252 If DEST is the frame pointer, we do nothing because we assume that
3253 all assignments to the hard frame pointer are nonlocal gotos and are being
3254 done at a time when they are valid and do not disturb anything else.
3255 Some machines want to eliminate a fake argument pointer with either the
3256 frame or stack pointer. Assignments to the hard frame pointer must not
3257 prevent this elimination.
3259 Called via note_stores from reload before starting its passes to scan
3260 the insns of the function. */
3262 static void
3263 mark_not_eliminable (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
3265 unsigned int i;
3267 /* A SUBREG of a hard register here is just changing its mode. We should
3268 not see a SUBREG of an eliminable hard register, but check just in
3269 case. */
3270 if (GET_CODE (dest) == SUBREG)
3271 dest = SUBREG_REG (dest);
3273 if (dest == hard_frame_pointer_rtx)
3274 return;
3276 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3277 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3278 && (GET_CODE (x) != SET
3279 || GET_CODE (SET_SRC (x)) != PLUS
3280 || XEXP (SET_SRC (x), 0) != dest
3281 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3283 reg_eliminate[i].can_eliminate_previous
3284 = reg_eliminate[i].can_eliminate = 0;
3285 num_eliminable--;
3289 /* Verify that the initial elimination offsets did not change since the
3290 last call to set_initial_elim_offsets. This is used to catch cases
3291 where something illegal happened during reload_as_needed that could
3292 cause incorrect code to be generated if we did not check for it. */
3294 static void
3295 verify_initial_elim_offsets (void)
3297 HOST_WIDE_INT t;
3299 #ifdef ELIMINABLE_REGS
3300 struct elim_table *ep;
3302 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3304 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3305 if (t != ep->initial_offset)
3306 abort ();
3308 #else
3309 INITIAL_FRAME_POINTER_OFFSET (t);
3310 if (t != reg_eliminate[0].initial_offset)
3311 abort ();
3312 #endif
3315 /* Reset all offsets on eliminable registers to their initial values. */
3317 static void
3318 set_initial_elim_offsets (void)
3320 struct elim_table *ep = reg_eliminate;
3322 #ifdef ELIMINABLE_REGS
3323 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3325 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3326 ep->previous_offset = ep->offset = ep->initial_offset;
3328 #else
3329 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3330 ep->previous_offset = ep->offset = ep->initial_offset;
3331 #endif
3333 num_not_at_initial_offset = 0;
3336 /* Initialize the known label offsets.
3337 Set a known offset for each forced label to be at the initial offset
3338 of each elimination. We do this because we assume that all
3339 computed jumps occur from a location where each elimination is
3340 at its initial offset.
3341 For all other labels, show that we don't know the offsets. */
3343 static void
3344 set_initial_label_offsets (void)
3346 rtx x;
3347 memset (offsets_known_at, 0, num_labels);
3349 for (x = forced_labels; x; x = XEXP (x, 1))
3350 if (XEXP (x, 0))
3351 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3354 /* Set all elimination offsets to the known values for the code label given
3355 by INSN. */
3357 static void
3358 set_offsets_for_label (rtx insn)
3360 unsigned int i;
3361 int label_nr = CODE_LABEL_NUMBER (insn);
3362 struct elim_table *ep;
3364 num_not_at_initial_offset = 0;
3365 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3367 ep->offset = ep->previous_offset
3368 = offsets_at[label_nr - first_label_num][i];
3369 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3370 num_not_at_initial_offset++;
3374 /* See if anything that happened changes which eliminations are valid.
3375 For example, on the SPARC, whether or not the frame pointer can
3376 be eliminated can depend on what registers have been used. We need
3377 not check some conditions again (such as flag_omit_frame_pointer)
3378 since they can't have changed. */
3380 static void
3381 update_eliminables (HARD_REG_SET *pset)
3383 int previous_frame_pointer_needed = frame_pointer_needed;
3384 struct elim_table *ep;
3386 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3387 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3388 #ifdef ELIMINABLE_REGS
3389 || ! CAN_ELIMINATE (ep->from, ep->to)
3390 #endif
3392 ep->can_eliminate = 0;
3394 /* Look for the case where we have discovered that we can't replace
3395 register A with register B and that means that we will now be
3396 trying to replace register A with register C. This means we can
3397 no longer replace register C with register B and we need to disable
3398 such an elimination, if it exists. This occurs often with A == ap,
3399 B == sp, and C == fp. */
3401 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3403 struct elim_table *op;
3404 int new_to = -1;
3406 if (! ep->can_eliminate && ep->can_eliminate_previous)
3408 /* Find the current elimination for ep->from, if there is a
3409 new one. */
3410 for (op = reg_eliminate;
3411 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3412 if (op->from == ep->from && op->can_eliminate)
3414 new_to = op->to;
3415 break;
3418 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3419 disable it. */
3420 for (op = reg_eliminate;
3421 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3422 if (op->from == new_to && op->to == ep->to)
3423 op->can_eliminate = 0;
3427 /* See if any registers that we thought we could eliminate the previous
3428 time are no longer eliminable. If so, something has changed and we
3429 must spill the register. Also, recompute the number of eliminable
3430 registers and see if the frame pointer is needed; it is if there is
3431 no elimination of the frame pointer that we can perform. */
3433 frame_pointer_needed = 1;
3434 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3436 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3437 && ep->to != HARD_FRAME_POINTER_REGNUM)
3438 frame_pointer_needed = 0;
3440 if (! ep->can_eliminate && ep->can_eliminate_previous)
3442 ep->can_eliminate_previous = 0;
3443 SET_HARD_REG_BIT (*pset, ep->from);
3444 num_eliminable--;
3448 /* If we didn't need a frame pointer last time, but we do now, spill
3449 the hard frame pointer. */
3450 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3451 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3454 /* Initialize the table of registers to eliminate. */
3456 static void
3457 init_elim_table (void)
3459 struct elim_table *ep;
3460 #ifdef ELIMINABLE_REGS
3461 const struct elim_table_1 *ep1;
3462 #endif
3464 if (!reg_eliminate)
3465 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3467 /* Does this function require a frame pointer? */
3469 frame_pointer_needed = (! flag_omit_frame_pointer
3470 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3471 and restore sp for alloca. So we can't eliminate
3472 the frame pointer in that case. At some point,
3473 we should improve this by emitting the
3474 sp-adjusting insns for this case. */
3475 || (current_function_calls_alloca
3476 && EXIT_IGNORE_STACK)
3477 || FRAME_POINTER_REQUIRED);
3479 num_eliminable = 0;
3481 #ifdef ELIMINABLE_REGS
3482 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3483 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3485 ep->from = ep1->from;
3486 ep->to = ep1->to;
3487 ep->can_eliminate = ep->can_eliminate_previous
3488 = (CAN_ELIMINATE (ep->from, ep->to)
3489 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3491 #else
3492 reg_eliminate[0].from = reg_eliminate_1[0].from;
3493 reg_eliminate[0].to = reg_eliminate_1[0].to;
3494 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3495 = ! frame_pointer_needed;
3496 #endif
3498 /* Count the number of eliminable registers and build the FROM and TO
3499 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3500 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3501 We depend on this. */
3502 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3504 num_eliminable += ep->can_eliminate;
3505 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3506 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3510 /* Kick all pseudos out of hard register REGNO.
3512 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3513 because we found we can't eliminate some register. In the case, no pseudos
3514 are allowed to be in the register, even if they are only in a block that
3515 doesn't require spill registers, unlike the case when we are spilling this
3516 hard reg to produce another spill register.
3518 Return nonzero if any pseudos needed to be kicked out. */
3520 static void
3521 spill_hard_reg (unsigned int regno, int cant_eliminate)
3523 int i;
3525 if (cant_eliminate)
3527 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3528 regs_ever_live[regno] = 1;
3531 /* Spill every pseudo reg that was allocated to this reg
3532 or to something that overlaps this reg. */
3534 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3535 if (reg_renumber[i] >= 0
3536 && (unsigned int) reg_renumber[i] <= regno
3537 && ((unsigned int) reg_renumber[i]
3538 + hard_regno_nregs[(unsigned int) reg_renumber[i]]
3539 [PSEUDO_REGNO_MODE (i)]
3540 > regno))
3541 SET_REGNO_REG_SET (&spilled_pseudos, i);
3544 /* I'm getting weird preprocessor errors if I use IOR_HARD_REG_SET
3545 from within EXECUTE_IF_SET_IN_REG_SET. Hence this awkwardness. */
3547 static void
3548 ior_hard_reg_set (HARD_REG_SET *set1, HARD_REG_SET *set2)
3550 IOR_HARD_REG_SET (*set1, *set2);
3553 /* After find_reload_regs has been run for all insn that need reloads,
3554 and/or spill_hard_regs was called, this function is used to actually
3555 spill pseudo registers and try to reallocate them. It also sets up the
3556 spill_regs array for use by choose_reload_regs. */
3558 static int
3559 finish_spills (int global)
3561 struct insn_chain *chain;
3562 int something_changed = 0;
3563 int i;
3565 /* Build the spill_regs array for the function. */
3566 /* If there are some registers still to eliminate and one of the spill regs
3567 wasn't ever used before, additional stack space may have to be
3568 allocated to store this register. Thus, we may have changed the offset
3569 between the stack and frame pointers, so mark that something has changed.
3571 One might think that we need only set VAL to 1 if this is a call-used
3572 register. However, the set of registers that must be saved by the
3573 prologue is not identical to the call-used set. For example, the
3574 register used by the call insn for the return PC is a call-used register,
3575 but must be saved by the prologue. */
3577 n_spills = 0;
3578 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3579 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3581 spill_reg_order[i] = n_spills;
3582 spill_regs[n_spills++] = i;
3583 if (num_eliminable && ! regs_ever_live[i])
3584 something_changed = 1;
3585 regs_ever_live[i] = 1;
3587 else
3588 spill_reg_order[i] = -1;
3590 EXECUTE_IF_SET_IN_REG_SET
3591 (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i,
3593 /* Record the current hard register the pseudo is allocated to in
3594 pseudo_previous_regs so we avoid reallocating it to the same
3595 hard reg in a later pass. */
3596 if (reg_renumber[i] < 0)
3597 abort ();
3599 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3600 /* Mark it as no longer having a hard register home. */
3601 reg_renumber[i] = -1;
3602 /* We will need to scan everything again. */
3603 something_changed = 1;
3606 /* Retry global register allocation if possible. */
3607 if (global)
3609 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3610 /* For every insn that needs reloads, set the registers used as spill
3611 regs in pseudo_forbidden_regs for every pseudo live across the
3612 insn. */
3613 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3615 EXECUTE_IF_SET_IN_REG_SET
3616 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i,
3618 ior_hard_reg_set (pseudo_forbidden_regs + i,
3619 &chain->used_spill_regs);
3621 EXECUTE_IF_SET_IN_REG_SET
3622 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i,
3624 ior_hard_reg_set (pseudo_forbidden_regs + i,
3625 &chain->used_spill_regs);
3629 /* Retry allocating the spilled pseudos. For each reg, merge the
3630 various reg sets that indicate which hard regs can't be used,
3631 and call retry_global_alloc.
3632 We change spill_pseudos here to only contain pseudos that did not
3633 get a new hard register. */
3634 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3635 if (reg_old_renumber[i] != reg_renumber[i])
3637 HARD_REG_SET forbidden;
3638 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3639 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3640 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3641 retry_global_alloc (i, forbidden);
3642 if (reg_renumber[i] >= 0)
3643 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3647 /* Fix up the register information in the insn chain.
3648 This involves deleting those of the spilled pseudos which did not get
3649 a new hard register home from the live_{before,after} sets. */
3650 for (chain = reload_insn_chain; chain; chain = chain->next)
3652 HARD_REG_SET used_by_pseudos;
3653 HARD_REG_SET used_by_pseudos2;
3655 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3656 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3658 /* Mark any unallocated hard regs as available for spills. That
3659 makes inheritance work somewhat better. */
3660 if (chain->need_reload)
3662 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3663 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3664 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3666 /* Save the old value for the sanity test below. */
3667 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3669 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3670 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3671 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3672 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3674 /* Make sure we only enlarge the set. */
3675 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3676 abort ();
3677 ok:;
3681 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3682 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3684 int regno = reg_renumber[i];
3685 if (reg_old_renumber[i] == regno)
3686 continue;
3688 alter_reg (i, reg_old_renumber[i]);
3689 reg_old_renumber[i] = regno;
3690 if (dump_file)
3692 if (regno == -1)
3693 fprintf (dump_file, " Register %d now on stack.\n\n", i);
3694 else
3695 fprintf (dump_file, " Register %d now in %d.\n\n",
3696 i, reg_renumber[i]);
3700 return something_changed;
3703 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
3705 static void
3706 scan_paradoxical_subregs (rtx x)
3708 int i;
3709 const char *fmt;
3710 enum rtx_code code = GET_CODE (x);
3712 switch (code)
3714 case REG:
3715 case CONST_INT:
3716 case CONST:
3717 case SYMBOL_REF:
3718 case LABEL_REF:
3719 case CONST_DOUBLE:
3720 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3721 case CC0:
3722 case PC:
3723 case USE:
3724 case CLOBBER:
3725 return;
3727 case SUBREG:
3728 if (REG_P (SUBREG_REG (x))
3729 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3730 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3731 = GET_MODE_SIZE (GET_MODE (x));
3732 return;
3734 default:
3735 break;
3738 fmt = GET_RTX_FORMAT (code);
3739 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3741 if (fmt[i] == 'e')
3742 scan_paradoxical_subregs (XEXP (x, i));
3743 else if (fmt[i] == 'E')
3745 int j;
3746 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3747 scan_paradoxical_subregs (XVECEXP (x, i, j));
3752 /* Reload pseudo-registers into hard regs around each insn as needed.
3753 Additional register load insns are output before the insn that needs it
3754 and perhaps store insns after insns that modify the reloaded pseudo reg.
3756 reg_last_reload_reg and reg_reloaded_contents keep track of
3757 which registers are already available in reload registers.
3758 We update these for the reloads that we perform,
3759 as the insns are scanned. */
3761 static void
3762 reload_as_needed (int live_known)
3764 struct insn_chain *chain;
3765 #if defined (AUTO_INC_DEC)
3766 int i;
3767 #endif
3768 rtx x;
3770 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
3771 memset (spill_reg_store, 0, sizeof spill_reg_store);
3772 reg_last_reload_reg = xcalloc (max_regno, sizeof (rtx));
3773 reg_has_output_reload = xmalloc (max_regno);
3774 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3775 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
3777 set_initial_elim_offsets ();
3779 for (chain = reload_insn_chain; chain; chain = chain->next)
3781 rtx prev = 0;
3782 rtx insn = chain->insn;
3783 rtx old_next = NEXT_INSN (insn);
3785 /* If we pass a label, copy the offsets from the label information
3786 into the current offsets of each elimination. */
3787 if (LABEL_P (insn))
3788 set_offsets_for_label (insn);
3790 else if (INSN_P (insn))
3792 rtx oldpat = copy_rtx (PATTERN (insn));
3794 /* If this is a USE and CLOBBER of a MEM, ensure that any
3795 references to eliminable registers have been removed. */
3797 if ((GET_CODE (PATTERN (insn)) == USE
3798 || GET_CODE (PATTERN (insn)) == CLOBBER)
3799 && MEM_P (XEXP (PATTERN (insn), 0)))
3800 XEXP (XEXP (PATTERN (insn), 0), 0)
3801 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3802 GET_MODE (XEXP (PATTERN (insn), 0)),
3803 NULL_RTX);
3805 /* If we need to do register elimination processing, do so.
3806 This might delete the insn, in which case we are done. */
3807 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3809 eliminate_regs_in_insn (insn, 1);
3810 if (NOTE_P (insn))
3812 update_eliminable_offsets ();
3813 continue;
3817 /* If need_elim is nonzero but need_reload is zero, one might think
3818 that we could simply set n_reloads to 0. However, find_reloads
3819 could have done some manipulation of the insn (such as swapping
3820 commutative operands), and these manipulations are lost during
3821 the first pass for every insn that needs register elimination.
3822 So the actions of find_reloads must be redone here. */
3824 if (! chain->need_elim && ! chain->need_reload
3825 && ! chain->need_operand_change)
3826 n_reloads = 0;
3827 /* First find the pseudo regs that must be reloaded for this insn.
3828 This info is returned in the tables reload_... (see reload.h).
3829 Also modify the body of INSN by substituting RELOAD
3830 rtx's for those pseudo regs. */
3831 else
3833 memset (reg_has_output_reload, 0, max_regno);
3834 CLEAR_HARD_REG_SET (reg_is_output_reload);
3836 find_reloads (insn, 1, spill_indirect_levels, live_known,
3837 spill_reg_order);
3840 if (n_reloads > 0)
3842 rtx next = NEXT_INSN (insn);
3843 rtx p;
3845 prev = PREV_INSN (insn);
3847 /* Now compute which reload regs to reload them into. Perhaps
3848 reusing reload regs from previous insns, or else output
3849 load insns to reload them. Maybe output store insns too.
3850 Record the choices of reload reg in reload_reg_rtx. */
3851 choose_reload_regs (chain);
3853 /* Merge any reloads that we didn't combine for fear of
3854 increasing the number of spill registers needed but now
3855 discover can be safely merged. */
3856 if (SMALL_REGISTER_CLASSES)
3857 merge_assigned_reloads (insn);
3859 /* Generate the insns to reload operands into or out of
3860 their reload regs. */
3861 emit_reload_insns (chain);
3863 /* Substitute the chosen reload regs from reload_reg_rtx
3864 into the insn's body (or perhaps into the bodies of other
3865 load and store insn that we just made for reloading
3866 and that we moved the structure into). */
3867 subst_reloads (insn);
3869 /* If this was an ASM, make sure that all the reload insns
3870 we have generated are valid. If not, give an error
3871 and delete them. */
3873 if (asm_noperands (PATTERN (insn)) >= 0)
3874 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3875 if (p != insn && INSN_P (p)
3876 && GET_CODE (PATTERN (p)) != USE
3877 && (recog_memoized (p) < 0
3878 || (extract_insn (p), ! constrain_operands (1))))
3880 error_for_asm (insn,
3881 "`asm' operand requires impossible reload");
3882 delete_insn (p);
3886 if (num_eliminable && chain->need_elim)
3887 update_eliminable_offsets ();
3889 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3890 is no longer validly lying around to save a future reload.
3891 Note that this does not detect pseudos that were reloaded
3892 for this insn in order to be stored in
3893 (obeying register constraints). That is correct; such reload
3894 registers ARE still valid. */
3895 note_stores (oldpat, forget_old_reloads_1, NULL);
3897 /* There may have been CLOBBER insns placed after INSN. So scan
3898 between INSN and NEXT and use them to forget old reloads. */
3899 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
3900 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
3901 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
3903 #ifdef AUTO_INC_DEC
3904 /* Likewise for regs altered by auto-increment in this insn.
3905 REG_INC notes have been changed by reloading:
3906 find_reloads_address_1 records substitutions for them,
3907 which have been performed by subst_reloads above. */
3908 for (i = n_reloads - 1; i >= 0; i--)
3910 rtx in_reg = rld[i].in_reg;
3911 if (in_reg)
3913 enum rtx_code code = GET_CODE (in_reg);
3914 /* PRE_INC / PRE_DEC will have the reload register ending up
3915 with the same value as the stack slot, but that doesn't
3916 hold true for POST_INC / POST_DEC. Either we have to
3917 convert the memory access to a true POST_INC / POST_DEC,
3918 or we can't use the reload register for inheritance. */
3919 if ((code == POST_INC || code == POST_DEC)
3920 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3921 REGNO (rld[i].reg_rtx))
3922 /* Make sure it is the inc/dec pseudo, and not
3923 some other (e.g. output operand) pseudo. */
3924 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3925 == REGNO (XEXP (in_reg, 0))))
3928 rtx reload_reg = rld[i].reg_rtx;
3929 enum machine_mode mode = GET_MODE (reload_reg);
3930 int n = 0;
3931 rtx p;
3933 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
3935 /* We really want to ignore REG_INC notes here, so
3936 use PATTERN (p) as argument to reg_set_p . */
3937 if (reg_set_p (reload_reg, PATTERN (p)))
3938 break;
3939 n = count_occurrences (PATTERN (p), reload_reg, 0);
3940 if (! n)
3941 continue;
3942 if (n == 1)
3944 n = validate_replace_rtx (reload_reg,
3945 gen_rtx_fmt_e (code,
3946 mode,
3947 reload_reg),
3950 /* We must also verify that the constraints
3951 are met after the replacement. */
3952 extract_insn (p);
3953 if (n)
3954 n = constrain_operands (1);
3955 else
3956 break;
3958 /* If the constraints were not met, then
3959 undo the replacement. */
3960 if (!n)
3962 validate_replace_rtx (gen_rtx_fmt_e (code,
3963 mode,
3964 reload_reg),
3965 reload_reg, p);
3966 break;
3970 break;
3972 if (n == 1)
3974 REG_NOTES (p)
3975 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
3976 REG_NOTES (p));
3977 /* Mark this as having an output reload so that the
3978 REG_INC processing code below won't invalidate
3979 the reload for inheritance. */
3980 SET_HARD_REG_BIT (reg_is_output_reload,
3981 REGNO (reload_reg));
3982 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
3984 else
3985 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
3986 NULL);
3988 else if ((code == PRE_INC || code == PRE_DEC)
3989 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3990 REGNO (rld[i].reg_rtx))
3991 /* Make sure it is the inc/dec pseudo, and not
3992 some other (e.g. output operand) pseudo. */
3993 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3994 == REGNO (XEXP (in_reg, 0))))
3996 SET_HARD_REG_BIT (reg_is_output_reload,
3997 REGNO (rld[i].reg_rtx));
3998 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4002 /* If a pseudo that got a hard register is auto-incremented,
4003 we must purge records of copying it into pseudos without
4004 hard registers. */
4005 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4006 if (REG_NOTE_KIND (x) == REG_INC)
4008 /* See if this pseudo reg was reloaded in this insn.
4009 If so, its last-reload info is still valid
4010 because it is based on this insn's reload. */
4011 for (i = 0; i < n_reloads; i++)
4012 if (rld[i].out == XEXP (x, 0))
4013 break;
4015 if (i == n_reloads)
4016 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4018 #endif
4020 /* A reload reg's contents are unknown after a label. */
4021 if (LABEL_P (insn))
4022 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4024 /* Don't assume a reload reg is still good after a call insn
4025 if it is a call-used reg, or if it contains a value that will
4026 be partially clobbered by the call. */
4027 else if (CALL_P (insn))
4029 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4030 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4034 /* Clean up. */
4035 free (reg_last_reload_reg);
4036 free (reg_has_output_reload);
4039 /* Discard all record of any value reloaded from X,
4040 or reloaded in X from someplace else;
4041 unless X is an output reload reg of the current insn.
4043 X may be a hard reg (the reload reg)
4044 or it may be a pseudo reg that was reloaded from. */
4046 static void
4047 forget_old_reloads_1 (rtx x, rtx ignored ATTRIBUTE_UNUSED,
4048 void *data ATTRIBUTE_UNUSED)
4050 unsigned int regno;
4051 unsigned int nr;
4053 /* note_stores does give us subregs of hard regs,
4054 subreg_regno_offset will abort if it is not a hard reg. */
4055 while (GET_CODE (x) == SUBREG)
4057 /* We ignore the subreg offset when calculating the regno,
4058 because we are using the entire underlying hard register
4059 below. */
4060 x = SUBREG_REG (x);
4063 if (!REG_P (x))
4064 return;
4066 regno = REGNO (x);
4068 if (regno >= FIRST_PSEUDO_REGISTER)
4069 nr = 1;
4070 else
4072 unsigned int i;
4074 nr = hard_regno_nregs[regno][GET_MODE (x)];
4075 /* Storing into a spilled-reg invalidates its contents.
4076 This can happen if a block-local pseudo is allocated to that reg
4077 and it wasn't spilled because this block's total need is 0.
4078 Then some insn might have an optional reload and use this reg. */
4079 for (i = 0; i < nr; i++)
4080 /* But don't do this if the reg actually serves as an output
4081 reload reg in the current instruction. */
4082 if (n_reloads == 0
4083 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4085 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4086 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, regno + i);
4087 spill_reg_store[regno + i] = 0;
4091 /* Since value of X has changed,
4092 forget any value previously copied from it. */
4094 while (nr-- > 0)
4095 /* But don't forget a copy if this is the output reload
4096 that establishes the copy's validity. */
4097 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4098 reg_last_reload_reg[regno + nr] = 0;
4101 /* The following HARD_REG_SETs indicate when each hard register is
4102 used for a reload of various parts of the current insn. */
4104 /* If reg is unavailable for all reloads. */
4105 static HARD_REG_SET reload_reg_unavailable;
4106 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4107 static HARD_REG_SET reload_reg_used;
4108 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4109 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4110 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4111 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4112 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4113 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4114 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4115 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4116 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4117 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4118 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4119 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4120 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4121 static HARD_REG_SET reload_reg_used_in_op_addr;
4122 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4123 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4124 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4125 static HARD_REG_SET reload_reg_used_in_insn;
4126 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4127 static HARD_REG_SET reload_reg_used_in_other_addr;
4129 /* If reg is in use as a reload reg for any sort of reload. */
4130 static HARD_REG_SET reload_reg_used_at_all;
4132 /* If reg is use as an inherited reload. We just mark the first register
4133 in the group. */
4134 static HARD_REG_SET reload_reg_used_for_inherit;
4136 /* Records which hard regs are used in any way, either as explicit use or
4137 by being allocated to a pseudo during any point of the current insn. */
4138 static HARD_REG_SET reg_used_in_insn;
4140 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4141 TYPE. MODE is used to indicate how many consecutive regs are
4142 actually used. */
4144 static void
4145 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4146 enum machine_mode mode)
4148 unsigned int nregs = hard_regno_nregs[regno][mode];
4149 unsigned int i;
4151 for (i = regno; i < nregs + regno; i++)
4153 switch (type)
4155 case RELOAD_OTHER:
4156 SET_HARD_REG_BIT (reload_reg_used, i);
4157 break;
4159 case RELOAD_FOR_INPUT_ADDRESS:
4160 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4161 break;
4163 case RELOAD_FOR_INPADDR_ADDRESS:
4164 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4165 break;
4167 case RELOAD_FOR_OUTPUT_ADDRESS:
4168 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4169 break;
4171 case RELOAD_FOR_OUTADDR_ADDRESS:
4172 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4173 break;
4175 case RELOAD_FOR_OPERAND_ADDRESS:
4176 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4177 break;
4179 case RELOAD_FOR_OPADDR_ADDR:
4180 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4181 break;
4183 case RELOAD_FOR_OTHER_ADDRESS:
4184 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4185 break;
4187 case RELOAD_FOR_INPUT:
4188 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4189 break;
4191 case RELOAD_FOR_OUTPUT:
4192 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4193 break;
4195 case RELOAD_FOR_INSN:
4196 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4197 break;
4200 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4204 /* Similarly, but show REGNO is no longer in use for a reload. */
4206 static void
4207 clear_reload_reg_in_use (unsigned int regno, int opnum,
4208 enum reload_type type, enum machine_mode mode)
4210 unsigned int nregs = hard_regno_nregs[regno][mode];
4211 unsigned int start_regno, end_regno, r;
4212 int i;
4213 /* A complication is that for some reload types, inheritance might
4214 allow multiple reloads of the same types to share a reload register.
4215 We set check_opnum if we have to check only reloads with the same
4216 operand number, and check_any if we have to check all reloads. */
4217 int check_opnum = 0;
4218 int check_any = 0;
4219 HARD_REG_SET *used_in_set;
4221 switch (type)
4223 case RELOAD_OTHER:
4224 used_in_set = &reload_reg_used;
4225 break;
4227 case RELOAD_FOR_INPUT_ADDRESS:
4228 used_in_set = &reload_reg_used_in_input_addr[opnum];
4229 break;
4231 case RELOAD_FOR_INPADDR_ADDRESS:
4232 check_opnum = 1;
4233 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4234 break;
4236 case RELOAD_FOR_OUTPUT_ADDRESS:
4237 used_in_set = &reload_reg_used_in_output_addr[opnum];
4238 break;
4240 case RELOAD_FOR_OUTADDR_ADDRESS:
4241 check_opnum = 1;
4242 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4243 break;
4245 case RELOAD_FOR_OPERAND_ADDRESS:
4246 used_in_set = &reload_reg_used_in_op_addr;
4247 break;
4249 case RELOAD_FOR_OPADDR_ADDR:
4250 check_any = 1;
4251 used_in_set = &reload_reg_used_in_op_addr_reload;
4252 break;
4254 case RELOAD_FOR_OTHER_ADDRESS:
4255 used_in_set = &reload_reg_used_in_other_addr;
4256 check_any = 1;
4257 break;
4259 case RELOAD_FOR_INPUT:
4260 used_in_set = &reload_reg_used_in_input[opnum];
4261 break;
4263 case RELOAD_FOR_OUTPUT:
4264 used_in_set = &reload_reg_used_in_output[opnum];
4265 break;
4267 case RELOAD_FOR_INSN:
4268 used_in_set = &reload_reg_used_in_insn;
4269 break;
4270 default:
4271 abort ();
4273 /* We resolve conflicts with remaining reloads of the same type by
4274 excluding the intervals of reload registers by them from the
4275 interval of freed reload registers. Since we only keep track of
4276 one set of interval bounds, we might have to exclude somewhat
4277 more than what would be necessary if we used a HARD_REG_SET here.
4278 But this should only happen very infrequently, so there should
4279 be no reason to worry about it. */
4281 start_regno = regno;
4282 end_regno = regno + nregs;
4283 if (check_opnum || check_any)
4285 for (i = n_reloads - 1; i >= 0; i--)
4287 if (rld[i].when_needed == type
4288 && (check_any || rld[i].opnum == opnum)
4289 && rld[i].reg_rtx)
4291 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4292 unsigned int conflict_end
4293 = (conflict_start
4294 + hard_regno_nregs[conflict_start][rld[i].mode]);
4296 /* If there is an overlap with the first to-be-freed register,
4297 adjust the interval start. */
4298 if (conflict_start <= start_regno && conflict_end > start_regno)
4299 start_regno = conflict_end;
4300 /* Otherwise, if there is a conflict with one of the other
4301 to-be-freed registers, adjust the interval end. */
4302 if (conflict_start > start_regno && conflict_start < end_regno)
4303 end_regno = conflict_start;
4308 for (r = start_regno; r < end_regno; r++)
4309 CLEAR_HARD_REG_BIT (*used_in_set, r);
4312 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4313 specified by OPNUM and TYPE. */
4315 static int
4316 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4318 int i;
4320 /* In use for a RELOAD_OTHER means it's not available for anything. */
4321 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4322 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4323 return 0;
4325 switch (type)
4327 case RELOAD_OTHER:
4328 /* In use for anything means we can't use it for RELOAD_OTHER. */
4329 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4330 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4331 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4332 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4333 return 0;
4335 for (i = 0; i < reload_n_operands; i++)
4336 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4337 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4338 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4339 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4340 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4341 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4342 return 0;
4344 return 1;
4346 case RELOAD_FOR_INPUT:
4347 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4348 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4349 return 0;
4351 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4352 return 0;
4354 /* If it is used for some other input, can't use it. */
4355 for (i = 0; i < reload_n_operands; i++)
4356 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4357 return 0;
4359 /* If it is used in a later operand's address, can't use it. */
4360 for (i = opnum + 1; i < reload_n_operands; i++)
4361 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4362 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4363 return 0;
4365 return 1;
4367 case RELOAD_FOR_INPUT_ADDRESS:
4368 /* Can't use a register if it is used for an input address for this
4369 operand or used as an input in an earlier one. */
4370 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4371 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4372 return 0;
4374 for (i = 0; i < opnum; i++)
4375 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4376 return 0;
4378 return 1;
4380 case RELOAD_FOR_INPADDR_ADDRESS:
4381 /* Can't use a register if it is used for an input address
4382 for this operand or used as an input in an earlier
4383 one. */
4384 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4385 return 0;
4387 for (i = 0; i < opnum; i++)
4388 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4389 return 0;
4391 return 1;
4393 case RELOAD_FOR_OUTPUT_ADDRESS:
4394 /* Can't use a register if it is used for an output address for this
4395 operand or used as an output in this or a later operand. Note
4396 that multiple output operands are emitted in reverse order, so
4397 the conflicting ones are those with lower indices. */
4398 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4399 return 0;
4401 for (i = 0; i <= opnum; i++)
4402 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4403 return 0;
4405 return 1;
4407 case RELOAD_FOR_OUTADDR_ADDRESS:
4408 /* Can't use a register if it is used for an output address
4409 for this operand or used as an output in this or a
4410 later operand. Note that multiple output operands are
4411 emitted in reverse order, so the conflicting ones are
4412 those with lower indices. */
4413 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4414 return 0;
4416 for (i = 0; i <= opnum; i++)
4417 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4418 return 0;
4420 return 1;
4422 case RELOAD_FOR_OPERAND_ADDRESS:
4423 for (i = 0; i < reload_n_operands; i++)
4424 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4425 return 0;
4427 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4428 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4430 case RELOAD_FOR_OPADDR_ADDR:
4431 for (i = 0; i < reload_n_operands; i++)
4432 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4433 return 0;
4435 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4437 case RELOAD_FOR_OUTPUT:
4438 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4439 outputs, or an operand address for this or an earlier output.
4440 Note that multiple output operands are emitted in reverse order,
4441 so the conflicting ones are those with higher indices. */
4442 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4443 return 0;
4445 for (i = 0; i < reload_n_operands; i++)
4446 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4447 return 0;
4449 for (i = opnum; i < reload_n_operands; i++)
4450 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4451 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4452 return 0;
4454 return 1;
4456 case RELOAD_FOR_INSN:
4457 for (i = 0; i < reload_n_operands; i++)
4458 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4459 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4460 return 0;
4462 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4463 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4465 case RELOAD_FOR_OTHER_ADDRESS:
4466 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4468 abort ();
4471 /* Return 1 if the value in reload reg REGNO, as used by a reload
4472 needed for the part of the insn specified by OPNUM and TYPE,
4473 is still available in REGNO at the end of the insn.
4475 We can assume that the reload reg was already tested for availability
4476 at the time it is needed, and we should not check this again,
4477 in case the reg has already been marked in use. */
4479 static int
4480 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4482 int i;
4484 switch (type)
4486 case RELOAD_OTHER:
4487 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4488 its value must reach the end. */
4489 return 1;
4491 /* If this use is for part of the insn,
4492 its value reaches if no subsequent part uses the same register.
4493 Just like the above function, don't try to do this with lots
4494 of fallthroughs. */
4496 case RELOAD_FOR_OTHER_ADDRESS:
4497 /* Here we check for everything else, since these don't conflict
4498 with anything else and everything comes later. */
4500 for (i = 0; i < reload_n_operands; i++)
4501 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4502 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4503 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4504 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4505 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4506 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4507 return 0;
4509 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4510 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4511 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4512 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4514 case RELOAD_FOR_INPUT_ADDRESS:
4515 case RELOAD_FOR_INPADDR_ADDRESS:
4516 /* Similar, except that we check only for this and subsequent inputs
4517 and the address of only subsequent inputs and we do not need
4518 to check for RELOAD_OTHER objects since they are known not to
4519 conflict. */
4521 for (i = opnum; i < reload_n_operands; i++)
4522 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4523 return 0;
4525 for (i = opnum + 1; i < reload_n_operands; i++)
4526 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4527 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4528 return 0;
4530 for (i = 0; i < reload_n_operands; i++)
4531 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4532 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4533 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4534 return 0;
4536 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4537 return 0;
4539 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4540 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4541 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4543 case RELOAD_FOR_INPUT:
4544 /* Similar to input address, except we start at the next operand for
4545 both input and input address and we do not check for
4546 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4547 would conflict. */
4549 for (i = opnum + 1; i < reload_n_operands; i++)
4550 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4551 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4552 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4553 return 0;
4555 /* ... fall through ... */
4557 case RELOAD_FOR_OPERAND_ADDRESS:
4558 /* Check outputs and their addresses. */
4560 for (i = 0; i < reload_n_operands; i++)
4561 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4562 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4563 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4564 return 0;
4566 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4568 case RELOAD_FOR_OPADDR_ADDR:
4569 for (i = 0; i < reload_n_operands; i++)
4570 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4571 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4572 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4573 return 0;
4575 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4576 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4577 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4579 case RELOAD_FOR_INSN:
4580 /* These conflict with other outputs with RELOAD_OTHER. So
4581 we need only check for output addresses. */
4583 opnum = reload_n_operands;
4585 /* ... fall through ... */
4587 case RELOAD_FOR_OUTPUT:
4588 case RELOAD_FOR_OUTPUT_ADDRESS:
4589 case RELOAD_FOR_OUTADDR_ADDRESS:
4590 /* We already know these can't conflict with a later output. So the
4591 only thing to check are later output addresses.
4592 Note that multiple output operands are emitted in reverse order,
4593 so the conflicting ones are those with lower indices. */
4594 for (i = 0; i < opnum; i++)
4595 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4596 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4597 return 0;
4599 return 1;
4602 abort ();
4605 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4606 Return 0 otherwise.
4608 This function uses the same algorithm as reload_reg_free_p above. */
4611 reloads_conflict (int r1, int r2)
4613 enum reload_type r1_type = rld[r1].when_needed;
4614 enum reload_type r2_type = rld[r2].when_needed;
4615 int r1_opnum = rld[r1].opnum;
4616 int r2_opnum = rld[r2].opnum;
4618 /* RELOAD_OTHER conflicts with everything. */
4619 if (r2_type == RELOAD_OTHER)
4620 return 1;
4622 /* Otherwise, check conflicts differently for each type. */
4624 switch (r1_type)
4626 case RELOAD_FOR_INPUT:
4627 return (r2_type == RELOAD_FOR_INSN
4628 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4629 || r2_type == RELOAD_FOR_OPADDR_ADDR
4630 || r2_type == RELOAD_FOR_INPUT
4631 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4632 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4633 && r2_opnum > r1_opnum));
4635 case RELOAD_FOR_INPUT_ADDRESS:
4636 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4637 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4639 case RELOAD_FOR_INPADDR_ADDRESS:
4640 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4641 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4643 case RELOAD_FOR_OUTPUT_ADDRESS:
4644 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4645 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4647 case RELOAD_FOR_OUTADDR_ADDRESS:
4648 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4649 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4651 case RELOAD_FOR_OPERAND_ADDRESS:
4652 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4653 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4655 case RELOAD_FOR_OPADDR_ADDR:
4656 return (r2_type == RELOAD_FOR_INPUT
4657 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4659 case RELOAD_FOR_OUTPUT:
4660 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4661 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4662 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4663 && r2_opnum >= r1_opnum));
4665 case RELOAD_FOR_INSN:
4666 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4667 || r2_type == RELOAD_FOR_INSN
4668 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4670 case RELOAD_FOR_OTHER_ADDRESS:
4671 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4673 case RELOAD_OTHER:
4674 return 1;
4676 default:
4677 abort ();
4681 /* Indexed by reload number, 1 if incoming value
4682 inherited from previous insns. */
4683 char reload_inherited[MAX_RELOADS];
4685 /* For an inherited reload, this is the insn the reload was inherited from,
4686 if we know it. Otherwise, this is 0. */
4687 rtx reload_inheritance_insn[MAX_RELOADS];
4689 /* If nonzero, this is a place to get the value of the reload,
4690 rather than using reload_in. */
4691 rtx reload_override_in[MAX_RELOADS];
4693 /* For each reload, the hard register number of the register used,
4694 or -1 if we did not need a register for this reload. */
4695 int reload_spill_index[MAX_RELOADS];
4697 /* Subroutine of free_for_value_p, used to check a single register.
4698 START_REGNO is the starting regno of the full reload register
4699 (possibly comprising multiple hard registers) that we are considering. */
4701 static int
4702 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
4703 enum reload_type type, rtx value, rtx out,
4704 int reloadnum, int ignore_address_reloads)
4706 int time1;
4707 /* Set if we see an input reload that must not share its reload register
4708 with any new earlyclobber, but might otherwise share the reload
4709 register with an output or input-output reload. */
4710 int check_earlyclobber = 0;
4711 int i;
4712 int copy = 0;
4714 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4715 return 0;
4717 if (out == const0_rtx)
4719 copy = 1;
4720 out = NULL_RTX;
4723 /* We use some pseudo 'time' value to check if the lifetimes of the
4724 new register use would overlap with the one of a previous reload
4725 that is not read-only or uses a different value.
4726 The 'time' used doesn't have to be linear in any shape or form, just
4727 monotonic.
4728 Some reload types use different 'buckets' for each operand.
4729 So there are MAX_RECOG_OPERANDS different time values for each
4730 such reload type.
4731 We compute TIME1 as the time when the register for the prospective
4732 new reload ceases to be live, and TIME2 for each existing
4733 reload as the time when that the reload register of that reload
4734 becomes live.
4735 Where there is little to be gained by exact lifetime calculations,
4736 we just make conservative assumptions, i.e. a longer lifetime;
4737 this is done in the 'default:' cases. */
4738 switch (type)
4740 case RELOAD_FOR_OTHER_ADDRESS:
4741 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4742 time1 = copy ? 0 : 1;
4743 break;
4744 case RELOAD_OTHER:
4745 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4746 break;
4747 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4748 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4749 respectively, to the time values for these, we get distinct time
4750 values. To get distinct time values for each operand, we have to
4751 multiply opnum by at least three. We round that up to four because
4752 multiply by four is often cheaper. */
4753 case RELOAD_FOR_INPADDR_ADDRESS:
4754 time1 = opnum * 4 + 2;
4755 break;
4756 case RELOAD_FOR_INPUT_ADDRESS:
4757 time1 = opnum * 4 + 3;
4758 break;
4759 case RELOAD_FOR_INPUT:
4760 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4761 executes (inclusive). */
4762 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
4763 break;
4764 case RELOAD_FOR_OPADDR_ADDR:
4765 /* opnum * 4 + 4
4766 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
4767 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4768 break;
4769 case RELOAD_FOR_OPERAND_ADDRESS:
4770 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4771 is executed. */
4772 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4773 break;
4774 case RELOAD_FOR_OUTADDR_ADDRESS:
4775 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
4776 break;
4777 case RELOAD_FOR_OUTPUT_ADDRESS:
4778 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
4779 break;
4780 default:
4781 time1 = MAX_RECOG_OPERANDS * 5 + 5;
4784 for (i = 0; i < n_reloads; i++)
4786 rtx reg = rld[i].reg_rtx;
4787 if (reg && REG_P (reg)
4788 && ((unsigned) regno - true_regnum (reg)
4789 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
4790 && i != reloadnum)
4792 rtx other_input = rld[i].in;
4794 /* If the other reload loads the same input value, that
4795 will not cause a conflict only if it's loading it into
4796 the same register. */
4797 if (true_regnum (reg) != start_regno)
4798 other_input = NULL_RTX;
4799 if (! other_input || ! rtx_equal_p (other_input, value)
4800 || rld[i].out || out)
4802 int time2;
4803 switch (rld[i].when_needed)
4805 case RELOAD_FOR_OTHER_ADDRESS:
4806 time2 = 0;
4807 break;
4808 case RELOAD_FOR_INPADDR_ADDRESS:
4809 /* find_reloads makes sure that a
4810 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4811 by at most one - the first -
4812 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4813 address reload is inherited, the address address reload
4814 goes away, so we can ignore this conflict. */
4815 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4816 && ignore_address_reloads
4817 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4818 Then the address address is still needed to store
4819 back the new address. */
4820 && ! rld[reloadnum].out)
4821 continue;
4822 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4823 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4824 reloads go away. */
4825 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4826 && ignore_address_reloads
4827 /* Unless we are reloading an auto_inc expression. */
4828 && ! rld[reloadnum].out)
4829 continue;
4830 time2 = rld[i].opnum * 4 + 2;
4831 break;
4832 case RELOAD_FOR_INPUT_ADDRESS:
4833 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4834 && ignore_address_reloads
4835 && ! rld[reloadnum].out)
4836 continue;
4837 time2 = rld[i].opnum * 4 + 3;
4838 break;
4839 case RELOAD_FOR_INPUT:
4840 time2 = rld[i].opnum * 4 + 4;
4841 check_earlyclobber = 1;
4842 break;
4843 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
4844 == MAX_RECOG_OPERAND * 4 */
4845 case RELOAD_FOR_OPADDR_ADDR:
4846 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4847 && ignore_address_reloads
4848 && ! rld[reloadnum].out)
4849 continue;
4850 time2 = MAX_RECOG_OPERANDS * 4 + 1;
4851 break;
4852 case RELOAD_FOR_OPERAND_ADDRESS:
4853 time2 = MAX_RECOG_OPERANDS * 4 + 2;
4854 check_earlyclobber = 1;
4855 break;
4856 case RELOAD_FOR_INSN:
4857 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4858 break;
4859 case RELOAD_FOR_OUTPUT:
4860 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4861 instruction is executed. */
4862 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4863 break;
4864 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4865 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4866 value. */
4867 case RELOAD_FOR_OUTADDR_ADDRESS:
4868 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4869 && ignore_address_reloads
4870 && ! rld[reloadnum].out)
4871 continue;
4872 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
4873 break;
4874 case RELOAD_FOR_OUTPUT_ADDRESS:
4875 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
4876 break;
4877 case RELOAD_OTHER:
4878 /* If there is no conflict in the input part, handle this
4879 like an output reload. */
4880 if (! rld[i].in || rtx_equal_p (other_input, value))
4882 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4883 /* Earlyclobbered outputs must conflict with inputs. */
4884 if (earlyclobber_operand_p (rld[i].out))
4885 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4887 break;
4889 time2 = 1;
4890 /* RELOAD_OTHER might be live beyond instruction execution,
4891 but this is not obvious when we set time2 = 1. So check
4892 here if there might be a problem with the new reload
4893 clobbering the register used by the RELOAD_OTHER. */
4894 if (out)
4895 return 0;
4896 break;
4897 default:
4898 return 0;
4900 if ((time1 >= time2
4901 && (! rld[i].in || rld[i].out
4902 || ! rtx_equal_p (other_input, value)))
4903 || (out && rld[reloadnum].out_reg
4904 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
4905 return 0;
4910 /* Earlyclobbered outputs must conflict with inputs. */
4911 if (check_earlyclobber && out && earlyclobber_operand_p (out))
4912 return 0;
4914 return 1;
4917 /* Return 1 if the value in reload reg REGNO, as used by a reload
4918 needed for the part of the insn specified by OPNUM and TYPE,
4919 may be used to load VALUE into it.
4921 MODE is the mode in which the register is used, this is needed to
4922 determine how many hard regs to test.
4924 Other read-only reloads with the same value do not conflict
4925 unless OUT is nonzero and these other reloads have to live while
4926 output reloads live.
4927 If OUT is CONST0_RTX, this is a special case: it means that the
4928 test should not be for using register REGNO as reload register, but
4929 for copying from register REGNO into the reload register.
4931 RELOADNUM is the number of the reload we want to load this value for;
4932 a reload does not conflict with itself.
4934 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
4935 reloads that load an address for the very reload we are considering.
4937 The caller has to make sure that there is no conflict with the return
4938 register. */
4940 static int
4941 free_for_value_p (int regno, enum machine_mode mode, int opnum,
4942 enum reload_type type, rtx value, rtx out, int reloadnum,
4943 int ignore_address_reloads)
4945 int nregs = hard_regno_nregs[regno][mode];
4946 while (nregs-- > 0)
4947 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
4948 value, out, reloadnum,
4949 ignore_address_reloads))
4950 return 0;
4951 return 1;
4954 /* Return nonzero if the rtx X is invariant over the current function. */
4955 /* ??? Actually, the places where we use this expect exactly what
4956 * is tested here, and not everything that is function invariant. In
4957 * particular, the frame pointer and arg pointer are special cased;
4958 * pic_offset_table_rtx is not, and this will cause aborts when we
4959 * go to spill these things to memory. */
4961 static int
4962 function_invariant_p (rtx x)
4964 if (CONSTANT_P (x))
4965 return 1;
4966 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
4967 return 1;
4968 if (GET_CODE (x) == PLUS
4969 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
4970 && CONSTANT_P (XEXP (x, 1)))
4971 return 1;
4972 return 0;
4975 /* Determine whether the reload reg X overlaps any rtx'es used for
4976 overriding inheritance. Return nonzero if so. */
4978 static int
4979 conflicts_with_override (rtx x)
4981 int i;
4982 for (i = 0; i < n_reloads; i++)
4983 if (reload_override_in[i]
4984 && reg_overlap_mentioned_p (x, reload_override_in[i]))
4985 return 1;
4986 return 0;
4989 /* Give an error message saying we failed to find a reload for INSN,
4990 and clear out reload R. */
4991 static void
4992 failed_reload (rtx insn, int r)
4994 if (asm_noperands (PATTERN (insn)) < 0)
4995 /* It's the compiler's fault. */
4996 fatal_insn ("could not find a spill register", insn);
4998 /* It's the user's fault; the operand's mode and constraint
4999 don't match. Disable this reload so we don't crash in final. */
5000 error_for_asm (insn,
5001 "`asm' operand constraint incompatible with operand size");
5002 rld[r].in = 0;
5003 rld[r].out = 0;
5004 rld[r].reg_rtx = 0;
5005 rld[r].optional = 1;
5006 rld[r].secondary_p = 1;
5009 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5010 for reload R. If it's valid, get an rtx for it. Return nonzero if
5011 successful. */
5012 static int
5013 set_reload_reg (int i, int r)
5015 int regno;
5016 rtx reg = spill_reg_rtx[i];
5018 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5019 spill_reg_rtx[i] = reg
5020 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5022 regno = true_regnum (reg);
5024 /* Detect when the reload reg can't hold the reload mode.
5025 This used to be one `if', but Sequent compiler can't handle that. */
5026 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5028 enum machine_mode test_mode = VOIDmode;
5029 if (rld[r].in)
5030 test_mode = GET_MODE (rld[r].in);
5031 /* If rld[r].in has VOIDmode, it means we will load it
5032 in whatever mode the reload reg has: to wit, rld[r].mode.
5033 We have already tested that for validity. */
5034 /* Aside from that, we need to test that the expressions
5035 to reload from or into have modes which are valid for this
5036 reload register. Otherwise the reload insns would be invalid. */
5037 if (! (rld[r].in != 0 && test_mode != VOIDmode
5038 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5039 if (! (rld[r].out != 0
5040 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5042 /* The reg is OK. */
5043 last_spill_reg = i;
5045 /* Mark as in use for this insn the reload regs we use
5046 for this. */
5047 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5048 rld[r].when_needed, rld[r].mode);
5050 rld[r].reg_rtx = reg;
5051 reload_spill_index[r] = spill_regs[i];
5052 return 1;
5055 return 0;
5058 /* Find a spill register to use as a reload register for reload R.
5059 LAST_RELOAD is nonzero if this is the last reload for the insn being
5060 processed.
5062 Set rld[R].reg_rtx to the register allocated.
5064 We return 1 if successful, or 0 if we couldn't find a spill reg and
5065 we didn't change anything. */
5067 static int
5068 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5069 int last_reload)
5071 int i, pass, count;
5073 /* If we put this reload ahead, thinking it is a group,
5074 then insist on finding a group. Otherwise we can grab a
5075 reg that some other reload needs.
5076 (That can happen when we have a 68000 DATA_OR_FP_REG
5077 which is a group of data regs or one fp reg.)
5078 We need not be so restrictive if there are no more reloads
5079 for this insn.
5081 ??? Really it would be nicer to have smarter handling
5082 for that kind of reg class, where a problem like this is normal.
5083 Perhaps those classes should be avoided for reloading
5084 by use of more alternatives. */
5086 int force_group = rld[r].nregs > 1 && ! last_reload;
5088 /* If we want a single register and haven't yet found one,
5089 take any reg in the right class and not in use.
5090 If we want a consecutive group, here is where we look for it.
5092 We use two passes so we can first look for reload regs to
5093 reuse, which are already in use for other reloads in this insn,
5094 and only then use additional registers.
5095 I think that maximizing reuse is needed to make sure we don't
5096 run out of reload regs. Suppose we have three reloads, and
5097 reloads A and B can share regs. These need two regs.
5098 Suppose A and B are given different regs.
5099 That leaves none for C. */
5100 for (pass = 0; pass < 2; pass++)
5102 /* I is the index in spill_regs.
5103 We advance it round-robin between insns to use all spill regs
5104 equally, so that inherited reloads have a chance
5105 of leapfrogging each other. */
5107 i = last_spill_reg;
5109 for (count = 0; count < n_spills; count++)
5111 int class = (int) rld[r].class;
5112 int regnum;
5114 i++;
5115 if (i >= n_spills)
5116 i -= n_spills;
5117 regnum = spill_regs[i];
5119 if ((reload_reg_free_p (regnum, rld[r].opnum,
5120 rld[r].when_needed)
5121 || (rld[r].in
5122 /* We check reload_reg_used to make sure we
5123 don't clobber the return register. */
5124 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5125 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5126 rld[r].when_needed, rld[r].in,
5127 rld[r].out, r, 1)))
5128 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5129 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5130 /* Look first for regs to share, then for unshared. But
5131 don't share regs used for inherited reloads; they are
5132 the ones we want to preserve. */
5133 && (pass
5134 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5135 regnum)
5136 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5137 regnum))))
5139 int nr = hard_regno_nregs[regnum][rld[r].mode];
5140 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5141 (on 68000) got us two FP regs. If NR is 1,
5142 we would reject both of them. */
5143 if (force_group)
5144 nr = rld[r].nregs;
5145 /* If we need only one reg, we have already won. */
5146 if (nr == 1)
5148 /* But reject a single reg if we demand a group. */
5149 if (force_group)
5150 continue;
5151 break;
5153 /* Otherwise check that as many consecutive regs as we need
5154 are available here. */
5155 while (nr > 1)
5157 int regno = regnum + nr - 1;
5158 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5159 && spill_reg_order[regno] >= 0
5160 && reload_reg_free_p (regno, rld[r].opnum,
5161 rld[r].when_needed)))
5162 break;
5163 nr--;
5165 if (nr == 1)
5166 break;
5170 /* If we found something on pass 1, omit pass 2. */
5171 if (count < n_spills)
5172 break;
5175 /* We should have found a spill register by now. */
5176 if (count >= n_spills)
5177 return 0;
5179 /* I is the index in SPILL_REG_RTX of the reload register we are to
5180 allocate. Get an rtx for it and find its register number. */
5182 return set_reload_reg (i, r);
5185 /* Initialize all the tables needed to allocate reload registers.
5186 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5187 is the array we use to restore the reg_rtx field for every reload. */
5189 static void
5190 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5192 int i;
5194 for (i = 0; i < n_reloads; i++)
5195 rld[i].reg_rtx = save_reload_reg_rtx[i];
5197 memset (reload_inherited, 0, MAX_RELOADS);
5198 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5199 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5201 CLEAR_HARD_REG_SET (reload_reg_used);
5202 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5203 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5204 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5205 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5206 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5208 CLEAR_HARD_REG_SET (reg_used_in_insn);
5210 HARD_REG_SET tmp;
5211 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5212 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5213 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5214 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5215 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5216 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5219 for (i = 0; i < reload_n_operands; i++)
5221 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5222 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5223 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5224 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5225 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5226 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5229 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5231 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5233 for (i = 0; i < n_reloads; i++)
5234 /* If we have already decided to use a certain register,
5235 don't use it in another way. */
5236 if (rld[i].reg_rtx)
5237 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5238 rld[i].when_needed, rld[i].mode);
5241 /* Assign hard reg targets for the pseudo-registers we must reload
5242 into hard regs for this insn.
5243 Also output the instructions to copy them in and out of the hard regs.
5245 For machines with register classes, we are responsible for
5246 finding a reload reg in the proper class. */
5248 static void
5249 choose_reload_regs (struct insn_chain *chain)
5251 rtx insn = chain->insn;
5252 int i, j;
5253 unsigned int max_group_size = 1;
5254 enum reg_class group_class = NO_REGS;
5255 int pass, win, inheritance;
5257 rtx save_reload_reg_rtx[MAX_RELOADS];
5259 /* In order to be certain of getting the registers we need,
5260 we must sort the reloads into order of increasing register class.
5261 Then our grabbing of reload registers will parallel the process
5262 that provided the reload registers.
5264 Also note whether any of the reloads wants a consecutive group of regs.
5265 If so, record the maximum size of the group desired and what
5266 register class contains all the groups needed by this insn. */
5268 for (j = 0; j < n_reloads; j++)
5270 reload_order[j] = j;
5271 reload_spill_index[j] = -1;
5273 if (rld[j].nregs > 1)
5275 max_group_size = MAX (rld[j].nregs, max_group_size);
5276 group_class
5277 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5280 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5283 if (n_reloads > 1)
5284 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5286 /* If -O, try first with inheritance, then turning it off.
5287 If not -O, don't do inheritance.
5288 Using inheritance when not optimizing leads to paradoxes
5289 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5290 because one side of the comparison might be inherited. */
5291 win = 0;
5292 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5294 choose_reload_regs_init (chain, save_reload_reg_rtx);
5296 /* Process the reloads in order of preference just found.
5297 Beyond this point, subregs can be found in reload_reg_rtx.
5299 This used to look for an existing reloaded home for all of the
5300 reloads, and only then perform any new reloads. But that could lose
5301 if the reloads were done out of reg-class order because a later
5302 reload with a looser constraint might have an old home in a register
5303 needed by an earlier reload with a tighter constraint.
5305 To solve this, we make two passes over the reloads, in the order
5306 described above. In the first pass we try to inherit a reload
5307 from a previous insn. If there is a later reload that needs a
5308 class that is a proper subset of the class being processed, we must
5309 also allocate a spill register during the first pass.
5311 Then make a second pass over the reloads to allocate any reloads
5312 that haven't been given registers yet. */
5314 for (j = 0; j < n_reloads; j++)
5316 int r = reload_order[j];
5317 rtx search_equiv = NULL_RTX;
5319 /* Ignore reloads that got marked inoperative. */
5320 if (rld[r].out == 0 && rld[r].in == 0
5321 && ! rld[r].secondary_p)
5322 continue;
5324 /* If find_reloads chose to use reload_in or reload_out as a reload
5325 register, we don't need to chose one. Otherwise, try even if it
5326 found one since we might save an insn if we find the value lying
5327 around.
5328 Try also when reload_in is a pseudo without a hard reg. */
5329 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5330 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5331 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5332 && !MEM_P (rld[r].in)
5333 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5334 continue;
5336 #if 0 /* No longer needed for correct operation.
5337 It might give better code, or might not; worth an experiment? */
5338 /* If this is an optional reload, we can't inherit from earlier insns
5339 until we are sure that any non-optional reloads have been allocated.
5340 The following code takes advantage of the fact that optional reloads
5341 are at the end of reload_order. */
5342 if (rld[r].optional != 0)
5343 for (i = 0; i < j; i++)
5344 if ((rld[reload_order[i]].out != 0
5345 || rld[reload_order[i]].in != 0
5346 || rld[reload_order[i]].secondary_p)
5347 && ! rld[reload_order[i]].optional
5348 && rld[reload_order[i]].reg_rtx == 0)
5349 allocate_reload_reg (chain, reload_order[i], 0);
5350 #endif
5352 /* First see if this pseudo is already available as reloaded
5353 for a previous insn. We cannot try to inherit for reloads
5354 that are smaller than the maximum number of registers needed
5355 for groups unless the register we would allocate cannot be used
5356 for the groups.
5358 We could check here to see if this is a secondary reload for
5359 an object that is already in a register of the desired class.
5360 This would avoid the need for the secondary reload register.
5361 But this is complex because we can't easily determine what
5362 objects might want to be loaded via this reload. So let a
5363 register be allocated here. In `emit_reload_insns' we suppress
5364 one of the loads in the case described above. */
5366 if (inheritance)
5368 int byte = 0;
5369 int regno = -1;
5370 enum machine_mode mode = VOIDmode;
5372 if (rld[r].in == 0)
5374 else if (REG_P (rld[r].in))
5376 regno = REGNO (rld[r].in);
5377 mode = GET_MODE (rld[r].in);
5379 else if (REG_P (rld[r].in_reg))
5381 regno = REGNO (rld[r].in_reg);
5382 mode = GET_MODE (rld[r].in_reg);
5384 else if (GET_CODE (rld[r].in_reg) == SUBREG
5385 && REG_P (SUBREG_REG (rld[r].in_reg)))
5387 byte = SUBREG_BYTE (rld[r].in_reg);
5388 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5389 if (regno < FIRST_PSEUDO_REGISTER)
5390 regno = subreg_regno (rld[r].in_reg);
5391 mode = GET_MODE (rld[r].in_reg);
5393 #ifdef AUTO_INC_DEC
5394 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5395 || GET_CODE (rld[r].in_reg) == PRE_DEC
5396 || GET_CODE (rld[r].in_reg) == POST_INC
5397 || GET_CODE (rld[r].in_reg) == POST_DEC)
5398 && REG_P (XEXP (rld[r].in_reg, 0)))
5400 regno = REGNO (XEXP (rld[r].in_reg, 0));
5401 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5402 rld[r].out = rld[r].in;
5404 #endif
5405 #if 0
5406 /* This won't work, since REGNO can be a pseudo reg number.
5407 Also, it takes much more hair to keep track of all the things
5408 that can invalidate an inherited reload of part of a pseudoreg. */
5409 else if (GET_CODE (rld[r].in) == SUBREG
5410 && REG_P (SUBREG_REG (rld[r].in)))
5411 regno = subreg_regno (rld[r].in);
5412 #endif
5414 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5416 enum reg_class class = rld[r].class, last_class;
5417 rtx last_reg = reg_last_reload_reg[regno];
5418 enum machine_mode need_mode;
5420 i = REGNO (last_reg);
5421 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5422 last_class = REGNO_REG_CLASS (i);
5424 if (byte == 0)
5425 need_mode = mode;
5426 else
5427 need_mode
5428 = smallest_mode_for_size (GET_MODE_SIZE (mode) + byte,
5429 GET_MODE_CLASS (mode));
5431 if (
5432 #ifdef CANNOT_CHANGE_MODE_CLASS
5433 (!REG_CANNOT_CHANGE_MODE_P (i, GET_MODE (last_reg),
5434 need_mode)
5436 #endif
5437 (GET_MODE_SIZE (GET_MODE (last_reg))
5438 >= GET_MODE_SIZE (need_mode))
5439 #ifdef CANNOT_CHANGE_MODE_CLASS
5441 #endif
5442 && reg_reloaded_contents[i] == regno
5443 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5444 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5445 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5446 /* Even if we can't use this register as a reload
5447 register, we might use it for reload_override_in,
5448 if copying it to the desired class is cheap
5449 enough. */
5450 || ((REGISTER_MOVE_COST (mode, last_class, class)
5451 < MEMORY_MOVE_COST (mode, class, 1))
5452 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5453 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5454 last_reg)
5455 == NO_REGS)
5456 #endif
5457 #ifdef SECONDARY_MEMORY_NEEDED
5458 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5459 mode)
5460 #endif
5463 && (rld[r].nregs == max_group_size
5464 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5466 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5467 rld[r].when_needed, rld[r].in,
5468 const0_rtx, r, 1))
5470 /* If a group is needed, verify that all the subsequent
5471 registers still have their values intact. */
5472 int nr = hard_regno_nregs[i][rld[r].mode];
5473 int k;
5475 for (k = 1; k < nr; k++)
5476 if (reg_reloaded_contents[i + k] != regno
5477 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5478 break;
5480 if (k == nr)
5482 int i1;
5483 int bad_for_class;
5485 last_reg = (GET_MODE (last_reg) == mode
5486 ? last_reg : gen_rtx_REG (mode, i));
5488 bad_for_class = 0;
5489 for (k = 0; k < nr; k++)
5490 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5491 i+k);
5493 /* We found a register that contains the
5494 value we need. If this register is the
5495 same as an `earlyclobber' operand of the
5496 current insn, just mark it as a place to
5497 reload from since we can't use it as the
5498 reload register itself. */
5500 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5501 if (reg_overlap_mentioned_for_reload_p
5502 (reg_last_reload_reg[regno],
5503 reload_earlyclobbers[i1]))
5504 break;
5506 if (i1 != n_earlyclobbers
5507 || ! (free_for_value_p (i, rld[r].mode,
5508 rld[r].opnum,
5509 rld[r].when_needed, rld[r].in,
5510 rld[r].out, r, 1))
5511 /* Don't use it if we'd clobber a pseudo reg. */
5512 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5513 && rld[r].out
5514 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5515 /* Don't clobber the frame pointer. */
5516 || (i == HARD_FRAME_POINTER_REGNUM
5517 && frame_pointer_needed
5518 && rld[r].out)
5519 /* Don't really use the inherited spill reg
5520 if we need it wider than we've got it. */
5521 || (GET_MODE_SIZE (rld[r].mode)
5522 > GET_MODE_SIZE (mode))
5523 || bad_for_class
5525 /* If find_reloads chose reload_out as reload
5526 register, stay with it - that leaves the
5527 inherited register for subsequent reloads. */
5528 || (rld[r].out && rld[r].reg_rtx
5529 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5531 if (! rld[r].optional)
5533 reload_override_in[r] = last_reg;
5534 reload_inheritance_insn[r]
5535 = reg_reloaded_insn[i];
5538 else
5540 int k;
5541 /* We can use this as a reload reg. */
5542 /* Mark the register as in use for this part of
5543 the insn. */
5544 mark_reload_reg_in_use (i,
5545 rld[r].opnum,
5546 rld[r].when_needed,
5547 rld[r].mode);
5548 rld[r].reg_rtx = last_reg;
5549 reload_inherited[r] = 1;
5550 reload_inheritance_insn[r]
5551 = reg_reloaded_insn[i];
5552 reload_spill_index[r] = i;
5553 for (k = 0; k < nr; k++)
5554 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5555 i + k);
5562 /* Here's another way to see if the value is already lying around. */
5563 if (inheritance
5564 && rld[r].in != 0
5565 && ! reload_inherited[r]
5566 && rld[r].out == 0
5567 && (CONSTANT_P (rld[r].in)
5568 || GET_CODE (rld[r].in) == PLUS
5569 || REG_P (rld[r].in)
5570 || MEM_P (rld[r].in))
5571 && (rld[r].nregs == max_group_size
5572 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5573 search_equiv = rld[r].in;
5574 /* If this is an output reload from a simple move insn, look
5575 if an equivalence for the input is available. */
5576 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5578 rtx set = single_set (insn);
5580 if (set
5581 && rtx_equal_p (rld[r].out, SET_DEST (set))
5582 && CONSTANT_P (SET_SRC (set)))
5583 search_equiv = SET_SRC (set);
5586 if (search_equiv)
5588 rtx equiv
5589 = find_equiv_reg (search_equiv, insn, rld[r].class,
5590 -1, NULL, 0, rld[r].mode);
5591 int regno = 0;
5593 if (equiv != 0)
5595 if (REG_P (equiv))
5596 regno = REGNO (equiv);
5597 else if (GET_CODE (equiv) == SUBREG)
5599 /* This must be a SUBREG of a hard register.
5600 Make a new REG since this might be used in an
5601 address and not all machines support SUBREGs
5602 there. */
5603 regno = subreg_regno (equiv);
5604 equiv = gen_rtx_REG (rld[r].mode, regno);
5606 else
5607 abort ();
5610 /* If we found a spill reg, reject it unless it is free
5611 and of the desired class. */
5612 if (equiv != 0)
5614 int regs_used = 0;
5615 int bad_for_class = 0;
5616 int max_regno = regno + rld[r].nregs;
5618 for (i = regno; i < max_regno; i++)
5620 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
5622 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5626 if ((regs_used
5627 && ! free_for_value_p (regno, rld[r].mode,
5628 rld[r].opnum, rld[r].when_needed,
5629 rld[r].in, rld[r].out, r, 1))
5630 || bad_for_class)
5631 equiv = 0;
5634 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5635 equiv = 0;
5637 /* We found a register that contains the value we need.
5638 If this register is the same as an `earlyclobber' operand
5639 of the current insn, just mark it as a place to reload from
5640 since we can't use it as the reload register itself. */
5642 if (equiv != 0)
5643 for (i = 0; i < n_earlyclobbers; i++)
5644 if (reg_overlap_mentioned_for_reload_p (equiv,
5645 reload_earlyclobbers[i]))
5647 if (! rld[r].optional)
5648 reload_override_in[r] = equiv;
5649 equiv = 0;
5650 break;
5653 /* If the equiv register we have found is explicitly clobbered
5654 in the current insn, it depends on the reload type if we
5655 can use it, use it for reload_override_in, or not at all.
5656 In particular, we then can't use EQUIV for a
5657 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5659 if (equiv != 0)
5661 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5662 switch (rld[r].when_needed)
5664 case RELOAD_FOR_OTHER_ADDRESS:
5665 case RELOAD_FOR_INPADDR_ADDRESS:
5666 case RELOAD_FOR_INPUT_ADDRESS:
5667 case RELOAD_FOR_OPADDR_ADDR:
5668 break;
5669 case RELOAD_OTHER:
5670 case RELOAD_FOR_INPUT:
5671 case RELOAD_FOR_OPERAND_ADDRESS:
5672 if (! rld[r].optional)
5673 reload_override_in[r] = equiv;
5674 /* Fall through. */
5675 default:
5676 equiv = 0;
5677 break;
5679 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5680 switch (rld[r].when_needed)
5682 case RELOAD_FOR_OTHER_ADDRESS:
5683 case RELOAD_FOR_INPADDR_ADDRESS:
5684 case RELOAD_FOR_INPUT_ADDRESS:
5685 case RELOAD_FOR_OPADDR_ADDR:
5686 case RELOAD_FOR_OPERAND_ADDRESS:
5687 case RELOAD_FOR_INPUT:
5688 break;
5689 case RELOAD_OTHER:
5690 if (! rld[r].optional)
5691 reload_override_in[r] = equiv;
5692 /* Fall through. */
5693 default:
5694 equiv = 0;
5695 break;
5699 /* If we found an equivalent reg, say no code need be generated
5700 to load it, and use it as our reload reg. */
5701 if (equiv != 0
5702 && (regno != HARD_FRAME_POINTER_REGNUM
5703 || !frame_pointer_needed))
5705 int nr = hard_regno_nregs[regno][rld[r].mode];
5706 int k;
5707 rld[r].reg_rtx = equiv;
5708 reload_inherited[r] = 1;
5710 /* If reg_reloaded_valid is not set for this register,
5711 there might be a stale spill_reg_store lying around.
5712 We must clear it, since otherwise emit_reload_insns
5713 might delete the store. */
5714 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5715 spill_reg_store[regno] = NULL_RTX;
5716 /* If any of the hard registers in EQUIV are spill
5717 registers, mark them as in use for this insn. */
5718 for (k = 0; k < nr; k++)
5720 i = spill_reg_order[regno + k];
5721 if (i >= 0)
5723 mark_reload_reg_in_use (regno, rld[r].opnum,
5724 rld[r].when_needed,
5725 rld[r].mode);
5726 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5727 regno + k);
5733 /* If we found a register to use already, or if this is an optional
5734 reload, we are done. */
5735 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5736 continue;
5738 #if 0
5739 /* No longer needed for correct operation. Might or might
5740 not give better code on the average. Want to experiment? */
5742 /* See if there is a later reload that has a class different from our
5743 class that intersects our class or that requires less register
5744 than our reload. If so, we must allocate a register to this
5745 reload now, since that reload might inherit a previous reload
5746 and take the only available register in our class. Don't do this
5747 for optional reloads since they will force all previous reloads
5748 to be allocated. Also don't do this for reloads that have been
5749 turned off. */
5751 for (i = j + 1; i < n_reloads; i++)
5753 int s = reload_order[i];
5755 if ((rld[s].in == 0 && rld[s].out == 0
5756 && ! rld[s].secondary_p)
5757 || rld[s].optional)
5758 continue;
5760 if ((rld[s].class != rld[r].class
5761 && reg_classes_intersect_p (rld[r].class,
5762 rld[s].class))
5763 || rld[s].nregs < rld[r].nregs)
5764 break;
5767 if (i == n_reloads)
5768 continue;
5770 allocate_reload_reg (chain, r, j == n_reloads - 1);
5771 #endif
5774 /* Now allocate reload registers for anything non-optional that
5775 didn't get one yet. */
5776 for (j = 0; j < n_reloads; j++)
5778 int r = reload_order[j];
5780 /* Ignore reloads that got marked inoperative. */
5781 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
5782 continue;
5784 /* Skip reloads that already have a register allocated or are
5785 optional. */
5786 if (rld[r].reg_rtx != 0 || rld[r].optional)
5787 continue;
5789 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
5790 break;
5793 /* If that loop got all the way, we have won. */
5794 if (j == n_reloads)
5796 win = 1;
5797 break;
5800 /* Loop around and try without any inheritance. */
5803 if (! win)
5805 /* First undo everything done by the failed attempt
5806 to allocate with inheritance. */
5807 choose_reload_regs_init (chain, save_reload_reg_rtx);
5809 /* Some sanity tests to verify that the reloads found in the first
5810 pass are identical to the ones we have now. */
5811 if (chain->n_reloads != n_reloads)
5812 abort ();
5814 for (i = 0; i < n_reloads; i++)
5816 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5817 continue;
5818 if (chain->rld[i].when_needed != rld[i].when_needed)
5819 abort ();
5820 for (j = 0; j < n_spills; j++)
5821 if (spill_regs[j] == chain->rld[i].regno)
5822 if (! set_reload_reg (j, i))
5823 failed_reload (chain->insn, i);
5827 /* If we thought we could inherit a reload, because it seemed that
5828 nothing else wanted the same reload register earlier in the insn,
5829 verify that assumption, now that all reloads have been assigned.
5830 Likewise for reloads where reload_override_in has been set. */
5832 /* If doing expensive optimizations, do one preliminary pass that doesn't
5833 cancel any inheritance, but removes reloads that have been needed only
5834 for reloads that we know can be inherited. */
5835 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
5837 for (j = 0; j < n_reloads; j++)
5839 int r = reload_order[j];
5840 rtx check_reg;
5841 if (reload_inherited[r] && rld[r].reg_rtx)
5842 check_reg = rld[r].reg_rtx;
5843 else if (reload_override_in[r]
5844 && (REG_P (reload_override_in[r])
5845 || GET_CODE (reload_override_in[r]) == SUBREG))
5846 check_reg = reload_override_in[r];
5847 else
5848 continue;
5849 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5850 rld[r].opnum, rld[r].when_needed, rld[r].in,
5851 (reload_inherited[r]
5852 ? rld[r].out : const0_rtx),
5853 r, 1))
5855 if (pass)
5856 continue;
5857 reload_inherited[r] = 0;
5858 reload_override_in[r] = 0;
5860 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5861 reload_override_in, then we do not need its related
5862 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5863 likewise for other reload types.
5864 We handle this by removing a reload when its only replacement
5865 is mentioned in reload_in of the reload we are going to inherit.
5866 A special case are auto_inc expressions; even if the input is
5867 inherited, we still need the address for the output. We can
5868 recognize them because they have RELOAD_OUT set to RELOAD_IN.
5869 If we succeeded removing some reload and we are doing a preliminary
5870 pass just to remove such reloads, make another pass, since the
5871 removal of one reload might allow us to inherit another one. */
5872 else if (rld[r].in
5873 && rld[r].out != rld[r].in
5874 && remove_address_replacements (rld[r].in) && pass)
5875 pass = 2;
5879 /* Now that reload_override_in is known valid,
5880 actually override reload_in. */
5881 for (j = 0; j < n_reloads; j++)
5882 if (reload_override_in[j])
5883 rld[j].in = reload_override_in[j];
5885 /* If this reload won't be done because it has been canceled or is
5886 optional and not inherited, clear reload_reg_rtx so other
5887 routines (such as subst_reloads) don't get confused. */
5888 for (j = 0; j < n_reloads; j++)
5889 if (rld[j].reg_rtx != 0
5890 && ((rld[j].optional && ! reload_inherited[j])
5891 || (rld[j].in == 0 && rld[j].out == 0
5892 && ! rld[j].secondary_p)))
5894 int regno = true_regnum (rld[j].reg_rtx);
5896 if (spill_reg_order[regno] >= 0)
5897 clear_reload_reg_in_use (regno, rld[j].opnum,
5898 rld[j].when_needed, rld[j].mode);
5899 rld[j].reg_rtx = 0;
5900 reload_spill_index[j] = -1;
5903 /* Record which pseudos and which spill regs have output reloads. */
5904 for (j = 0; j < n_reloads; j++)
5906 int r = reload_order[j];
5908 i = reload_spill_index[r];
5910 /* I is nonneg if this reload uses a register.
5911 If rld[r].reg_rtx is 0, this is an optional reload
5912 that we opted to ignore. */
5913 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
5914 && rld[r].reg_rtx != 0)
5916 int nregno = REGNO (rld[r].out_reg);
5917 int nr = 1;
5919 if (nregno < FIRST_PSEUDO_REGISTER)
5920 nr = hard_regno_nregs[nregno][rld[r].mode];
5922 while (--nr >= 0)
5923 reg_has_output_reload[nregno + nr] = 1;
5925 if (i >= 0)
5927 nr = hard_regno_nregs[i][rld[r].mode];
5928 while (--nr >= 0)
5929 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
5932 if (rld[r].when_needed != RELOAD_OTHER
5933 && rld[r].when_needed != RELOAD_FOR_OUTPUT
5934 && rld[r].when_needed != RELOAD_FOR_INSN)
5935 abort ();
5940 /* Deallocate the reload register for reload R. This is called from
5941 remove_address_replacements. */
5943 void
5944 deallocate_reload_reg (int r)
5946 int regno;
5948 if (! rld[r].reg_rtx)
5949 return;
5950 regno = true_regnum (rld[r].reg_rtx);
5951 rld[r].reg_rtx = 0;
5952 if (spill_reg_order[regno] >= 0)
5953 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
5954 rld[r].mode);
5955 reload_spill_index[r] = -1;
5958 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
5959 reloads of the same item for fear that we might not have enough reload
5960 registers. However, normally they will get the same reload register
5961 and hence actually need not be loaded twice.
5963 Here we check for the most common case of this phenomenon: when we have
5964 a number of reloads for the same object, each of which were allocated
5965 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5966 reload, and is not modified in the insn itself. If we find such,
5967 merge all the reloads and set the resulting reload to RELOAD_OTHER.
5968 This will not increase the number of spill registers needed and will
5969 prevent redundant code. */
5971 static void
5972 merge_assigned_reloads (rtx insn)
5974 int i, j;
5976 /* Scan all the reloads looking for ones that only load values and
5977 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
5978 assigned and not modified by INSN. */
5980 for (i = 0; i < n_reloads; i++)
5982 int conflicting_input = 0;
5983 int max_input_address_opnum = -1;
5984 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
5986 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
5987 || rld[i].out != 0 || rld[i].reg_rtx == 0
5988 || reg_set_p (rld[i].reg_rtx, insn))
5989 continue;
5991 /* Look at all other reloads. Ensure that the only use of this
5992 reload_reg_rtx is in a reload that just loads the same value
5993 as we do. Note that any secondary reloads must be of the identical
5994 class since the values, modes, and result registers are the
5995 same, so we need not do anything with any secondary reloads. */
5997 for (j = 0; j < n_reloads; j++)
5999 if (i == j || rld[j].reg_rtx == 0
6000 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6001 rld[i].reg_rtx))
6002 continue;
6004 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6005 && rld[j].opnum > max_input_address_opnum)
6006 max_input_address_opnum = rld[j].opnum;
6008 /* If the reload regs aren't exactly the same (e.g, different modes)
6009 or if the values are different, we can't merge this reload.
6010 But if it is an input reload, we might still merge
6011 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6013 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6014 || rld[j].out != 0 || rld[j].in == 0
6015 || ! rtx_equal_p (rld[i].in, rld[j].in))
6017 if (rld[j].when_needed != RELOAD_FOR_INPUT
6018 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6019 || rld[i].opnum > rld[j].opnum)
6020 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6021 break;
6022 conflicting_input = 1;
6023 if (min_conflicting_input_opnum > rld[j].opnum)
6024 min_conflicting_input_opnum = rld[j].opnum;
6028 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6029 we, in fact, found any matching reloads. */
6031 if (j == n_reloads
6032 && max_input_address_opnum <= min_conflicting_input_opnum)
6034 for (j = 0; j < n_reloads; j++)
6035 if (i != j && rld[j].reg_rtx != 0
6036 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6037 && (! conflicting_input
6038 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6039 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6041 rld[i].when_needed = RELOAD_OTHER;
6042 rld[j].in = 0;
6043 reload_spill_index[j] = -1;
6044 transfer_replacements (i, j);
6047 /* If this is now RELOAD_OTHER, look for any reloads that load
6048 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6049 if they were for inputs, RELOAD_OTHER for outputs. Note that
6050 this test is equivalent to looking for reloads for this operand
6051 number. */
6052 /* We must take special care when there are two or more reloads to
6053 be merged and a RELOAD_FOR_OUTPUT_ADDRESS reload that loads the
6054 same value or a part of it; we must not change its type if there
6055 is a conflicting input. */
6057 if (rld[i].when_needed == RELOAD_OTHER)
6058 for (j = 0; j < n_reloads; j++)
6059 if (rld[j].in != 0
6060 && rld[j].when_needed != RELOAD_OTHER
6061 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6062 && (! conflicting_input
6063 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6064 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6065 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6066 rld[i].in))
6068 int k;
6070 rld[j].when_needed
6071 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6072 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6073 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6075 /* Check to see if we accidentally converted two reloads
6076 that use the same reload register with different inputs
6077 to the same type. If so, the resulting code won't work,
6078 so abort. */
6079 if (rld[j].reg_rtx)
6080 for (k = 0; k < j; k++)
6081 if (rld[k].in != 0 && rld[k].reg_rtx != 0
6082 && rld[k].when_needed == rld[j].when_needed
6083 && rtx_equal_p (rld[k].reg_rtx, rld[j].reg_rtx)
6084 && ! rtx_equal_p (rld[k].in, rld[j].in))
6085 abort ();
6091 /* These arrays are filled by emit_reload_insns and its subroutines. */
6092 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6093 static rtx other_input_address_reload_insns = 0;
6094 static rtx other_input_reload_insns = 0;
6095 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6096 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6097 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6098 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6099 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6100 static rtx operand_reload_insns = 0;
6101 static rtx other_operand_reload_insns = 0;
6102 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6104 /* Values to be put in spill_reg_store are put here first. */
6105 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6106 static HARD_REG_SET reg_reloaded_died;
6108 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6109 has the number J. OLD contains the value to be used as input. */
6111 static void
6112 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6113 rtx old, int j)
6115 rtx insn = chain->insn;
6116 rtx reloadreg = rl->reg_rtx;
6117 rtx oldequiv_reg = 0;
6118 rtx oldequiv = 0;
6119 int special = 0;
6120 enum machine_mode mode;
6121 rtx *where;
6123 /* Determine the mode to reload in.
6124 This is very tricky because we have three to choose from.
6125 There is the mode the insn operand wants (rl->inmode).
6126 There is the mode of the reload register RELOADREG.
6127 There is the intrinsic mode of the operand, which we could find
6128 by stripping some SUBREGs.
6129 It turns out that RELOADREG's mode is irrelevant:
6130 we can change that arbitrarily.
6132 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6133 then the reload reg may not support QImode moves, so use SImode.
6134 If foo is in memory due to spilling a pseudo reg, this is safe,
6135 because the QImode value is in the least significant part of a
6136 slot big enough for a SImode. If foo is some other sort of
6137 memory reference, then it is impossible to reload this case,
6138 so previous passes had better make sure this never happens.
6140 Then consider a one-word union which has SImode and one of its
6141 members is a float, being fetched as (SUBREG:SF union:SI).
6142 We must fetch that as SFmode because we could be loading into
6143 a float-only register. In this case OLD's mode is correct.
6145 Consider an immediate integer: it has VOIDmode. Here we need
6146 to get a mode from something else.
6148 In some cases, there is a fourth mode, the operand's
6149 containing mode. If the insn specifies a containing mode for
6150 this operand, it overrides all others.
6152 I am not sure whether the algorithm here is always right,
6153 but it does the right things in those cases. */
6155 mode = GET_MODE (old);
6156 if (mode == VOIDmode)
6157 mode = rl->inmode;
6159 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6160 /* If we need a secondary register for this operation, see if
6161 the value is already in a register in that class. Don't
6162 do this if the secondary register will be used as a scratch
6163 register. */
6165 if (rl->secondary_in_reload >= 0
6166 && rl->secondary_in_icode == CODE_FOR_nothing
6167 && optimize)
6168 oldequiv
6169 = find_equiv_reg (old, insn,
6170 rld[rl->secondary_in_reload].class,
6171 -1, NULL, 0, mode);
6172 #endif
6174 /* If reloading from memory, see if there is a register
6175 that already holds the same value. If so, reload from there.
6176 We can pass 0 as the reload_reg_p argument because
6177 any other reload has either already been emitted,
6178 in which case find_equiv_reg will see the reload-insn,
6179 or has yet to be emitted, in which case it doesn't matter
6180 because we will use this equiv reg right away. */
6182 if (oldequiv == 0 && optimize
6183 && (MEM_P (old)
6184 || (REG_P (old)
6185 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6186 && reg_renumber[REGNO (old)] < 0)))
6187 oldequiv = find_equiv_reg (old, insn, ALL_REGS, -1, NULL, 0, mode);
6189 if (oldequiv)
6191 unsigned int regno = true_regnum (oldequiv);
6193 /* Don't use OLDEQUIV if any other reload changes it at an
6194 earlier stage of this insn or at this stage. */
6195 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6196 rl->in, const0_rtx, j, 0))
6197 oldequiv = 0;
6199 /* If it is no cheaper to copy from OLDEQUIV into the
6200 reload register than it would be to move from memory,
6201 don't use it. Likewise, if we need a secondary register
6202 or memory. */
6204 if (oldequiv != 0
6205 && (((enum reg_class) REGNO_REG_CLASS (regno) != rl->class
6206 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6207 rl->class)
6208 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6209 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6210 || (SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6211 mode, oldequiv)
6212 != NO_REGS)
6213 #endif
6214 #ifdef SECONDARY_MEMORY_NEEDED
6215 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6216 rl->class,
6217 mode)
6218 #endif
6220 oldequiv = 0;
6223 /* delete_output_reload is only invoked properly if old contains
6224 the original pseudo register. Since this is replaced with a
6225 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6226 find the pseudo in RELOAD_IN_REG. */
6227 if (oldequiv == 0
6228 && reload_override_in[j]
6229 && REG_P (rl->in_reg))
6231 oldequiv = old;
6232 old = rl->in_reg;
6234 if (oldequiv == 0)
6235 oldequiv = old;
6236 else if (REG_P (oldequiv))
6237 oldequiv_reg = oldequiv;
6238 else if (GET_CODE (oldequiv) == SUBREG)
6239 oldequiv_reg = SUBREG_REG (oldequiv);
6241 /* If we are reloading from a register that was recently stored in
6242 with an output-reload, see if we can prove there was
6243 actually no need to store the old value in it. */
6245 if (optimize && REG_P (oldequiv)
6246 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6247 && spill_reg_store[REGNO (oldequiv)]
6248 && REG_P (old)
6249 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6250 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6251 rl->out_reg)))
6252 delete_output_reload (insn, j, REGNO (oldequiv));
6254 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6255 then load RELOADREG from OLDEQUIV. Note that we cannot use
6256 gen_lowpart_common since it can do the wrong thing when
6257 RELOADREG has a multi-word mode. Note that RELOADREG
6258 must always be a REG here. */
6260 if (GET_MODE (reloadreg) != mode)
6261 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6262 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6263 oldequiv = SUBREG_REG (oldequiv);
6264 if (GET_MODE (oldequiv) != VOIDmode
6265 && mode != GET_MODE (oldequiv))
6266 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6268 /* Switch to the right place to emit the reload insns. */
6269 switch (rl->when_needed)
6271 case RELOAD_OTHER:
6272 where = &other_input_reload_insns;
6273 break;
6274 case RELOAD_FOR_INPUT:
6275 where = &input_reload_insns[rl->opnum];
6276 break;
6277 case RELOAD_FOR_INPUT_ADDRESS:
6278 where = &input_address_reload_insns[rl->opnum];
6279 break;
6280 case RELOAD_FOR_INPADDR_ADDRESS:
6281 where = &inpaddr_address_reload_insns[rl->opnum];
6282 break;
6283 case RELOAD_FOR_OUTPUT_ADDRESS:
6284 where = &output_address_reload_insns[rl->opnum];
6285 break;
6286 case RELOAD_FOR_OUTADDR_ADDRESS:
6287 where = &outaddr_address_reload_insns[rl->opnum];
6288 break;
6289 case RELOAD_FOR_OPERAND_ADDRESS:
6290 where = &operand_reload_insns;
6291 break;
6292 case RELOAD_FOR_OPADDR_ADDR:
6293 where = &other_operand_reload_insns;
6294 break;
6295 case RELOAD_FOR_OTHER_ADDRESS:
6296 where = &other_input_address_reload_insns;
6297 break;
6298 default:
6299 abort ();
6302 push_to_sequence (*where);
6304 /* Auto-increment addresses must be reloaded in a special way. */
6305 if (rl->out && ! rl->out_reg)
6307 /* We are not going to bother supporting the case where a
6308 incremented register can't be copied directly from
6309 OLDEQUIV since this seems highly unlikely. */
6310 if (rl->secondary_in_reload >= 0)
6311 abort ();
6313 if (reload_inherited[j])
6314 oldequiv = reloadreg;
6316 old = XEXP (rl->in_reg, 0);
6318 if (optimize && REG_P (oldequiv)
6319 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6320 && spill_reg_store[REGNO (oldequiv)]
6321 && REG_P (old)
6322 && (dead_or_set_p (insn,
6323 spill_reg_stored_to[REGNO (oldequiv)])
6324 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6325 old)))
6326 delete_output_reload (insn, j, REGNO (oldequiv));
6328 /* Prevent normal processing of this reload. */
6329 special = 1;
6330 /* Output a special code sequence for this case. */
6331 new_spill_reg_store[REGNO (reloadreg)]
6332 = inc_for_reload (reloadreg, oldequiv, rl->out,
6333 rl->inc);
6336 /* If we are reloading a pseudo-register that was set by the previous
6337 insn, see if we can get rid of that pseudo-register entirely
6338 by redirecting the previous insn into our reload register. */
6340 else if (optimize && REG_P (old)
6341 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6342 && dead_or_set_p (insn, old)
6343 /* This is unsafe if some other reload
6344 uses the same reg first. */
6345 && ! conflicts_with_override (reloadreg)
6346 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6347 rl->when_needed, old, rl->out, j, 0))
6349 rtx temp = PREV_INSN (insn);
6350 while (temp && NOTE_P (temp))
6351 temp = PREV_INSN (temp);
6352 if (temp
6353 && NONJUMP_INSN_P (temp)
6354 && GET_CODE (PATTERN (temp)) == SET
6355 && SET_DEST (PATTERN (temp)) == old
6356 /* Make sure we can access insn_operand_constraint. */
6357 && asm_noperands (PATTERN (temp)) < 0
6358 /* This is unsafe if operand occurs more than once in current
6359 insn. Perhaps some occurrences aren't reloaded. */
6360 && count_occurrences (PATTERN (insn), old, 0) == 1)
6362 rtx old = SET_DEST (PATTERN (temp));
6363 /* Store into the reload register instead of the pseudo. */
6364 SET_DEST (PATTERN (temp)) = reloadreg;
6366 /* Verify that resulting insn is valid. */
6367 extract_insn (temp);
6368 if (constrain_operands (1))
6370 /* If the previous insn is an output reload, the source is
6371 a reload register, and its spill_reg_store entry will
6372 contain the previous destination. This is now
6373 invalid. */
6374 if (REG_P (SET_SRC (PATTERN (temp)))
6375 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6377 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6378 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6381 /* If these are the only uses of the pseudo reg,
6382 pretend for GDB it lives in the reload reg we used. */
6383 if (REG_N_DEATHS (REGNO (old)) == 1
6384 && REG_N_SETS (REGNO (old)) == 1)
6386 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6387 alter_reg (REGNO (old), -1);
6389 special = 1;
6391 else
6393 SET_DEST (PATTERN (temp)) = old;
6398 /* We can't do that, so output an insn to load RELOADREG. */
6400 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6401 /* If we have a secondary reload, pick up the secondary register
6402 and icode, if any. If OLDEQUIV and OLD are different or
6403 if this is an in-out reload, recompute whether or not we
6404 still need a secondary register and what the icode should
6405 be. If we still need a secondary register and the class or
6406 icode is different, go back to reloading from OLD if using
6407 OLDEQUIV means that we got the wrong type of register. We
6408 cannot have different class or icode due to an in-out reload
6409 because we don't make such reloads when both the input and
6410 output need secondary reload registers. */
6412 if (! special && rl->secondary_in_reload >= 0)
6414 rtx second_reload_reg = 0;
6415 int secondary_reload = rl->secondary_in_reload;
6416 rtx real_oldequiv = oldequiv;
6417 rtx real_old = old;
6418 rtx tmp;
6419 enum insn_code icode;
6421 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6422 and similarly for OLD.
6423 See comments in get_secondary_reload in reload.c. */
6424 /* If it is a pseudo that cannot be replaced with its
6425 equivalent MEM, we must fall back to reload_in, which
6426 will have all the necessary substitutions registered.
6427 Likewise for a pseudo that can't be replaced with its
6428 equivalent constant.
6430 Take extra care for subregs of such pseudos. Note that
6431 we cannot use reg_equiv_mem in this case because it is
6432 not in the right mode. */
6434 tmp = oldequiv;
6435 if (GET_CODE (tmp) == SUBREG)
6436 tmp = SUBREG_REG (tmp);
6437 if (REG_P (tmp)
6438 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6439 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6440 || reg_equiv_constant[REGNO (tmp)] != 0))
6442 if (! reg_equiv_mem[REGNO (tmp)]
6443 || num_not_at_initial_offset
6444 || GET_CODE (oldequiv) == SUBREG)
6445 real_oldequiv = rl->in;
6446 else
6447 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6450 tmp = old;
6451 if (GET_CODE (tmp) == SUBREG)
6452 tmp = SUBREG_REG (tmp);
6453 if (REG_P (tmp)
6454 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6455 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6456 || reg_equiv_constant[REGNO (tmp)] != 0))
6458 if (! reg_equiv_mem[REGNO (tmp)]
6459 || num_not_at_initial_offset
6460 || GET_CODE (old) == SUBREG)
6461 real_old = rl->in;
6462 else
6463 real_old = reg_equiv_mem[REGNO (tmp)];
6466 second_reload_reg = rld[secondary_reload].reg_rtx;
6467 icode = rl->secondary_in_icode;
6469 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6470 || (rl->in != 0 && rl->out != 0))
6472 enum reg_class new_class
6473 = SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6474 mode, real_oldequiv);
6476 if (new_class == NO_REGS)
6477 second_reload_reg = 0;
6478 else
6480 enum insn_code new_icode;
6481 enum machine_mode new_mode;
6483 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6484 REGNO (second_reload_reg)))
6485 oldequiv = old, real_oldequiv = real_old;
6486 else
6488 new_icode = reload_in_optab[(int) mode];
6489 if (new_icode != CODE_FOR_nothing
6490 && ((insn_data[(int) new_icode].operand[0].predicate
6491 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6492 (reloadreg, mode)))
6493 || (insn_data[(int) new_icode].operand[1].predicate
6494 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6495 (real_oldequiv, mode)))))
6496 new_icode = CODE_FOR_nothing;
6498 if (new_icode == CODE_FOR_nothing)
6499 new_mode = mode;
6500 else
6501 new_mode = insn_data[(int) new_icode].operand[2].mode;
6503 if (GET_MODE (second_reload_reg) != new_mode)
6505 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6506 new_mode))
6507 oldequiv = old, real_oldequiv = real_old;
6508 else
6509 second_reload_reg
6510 = reload_adjust_reg_for_mode (second_reload_reg,
6511 new_mode);
6517 /* If we still need a secondary reload register, check
6518 to see if it is being used as a scratch or intermediate
6519 register and generate code appropriately. If we need
6520 a scratch register, use REAL_OLDEQUIV since the form of
6521 the insn may depend on the actual address if it is
6522 a MEM. */
6524 if (second_reload_reg)
6526 if (icode != CODE_FOR_nothing)
6528 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6529 second_reload_reg));
6530 special = 1;
6532 else
6534 /* See if we need a scratch register to load the
6535 intermediate register (a tertiary reload). */
6536 enum insn_code tertiary_icode
6537 = rld[secondary_reload].secondary_in_icode;
6539 if (tertiary_icode != CODE_FOR_nothing)
6541 rtx third_reload_reg
6542 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
6544 emit_insn ((GEN_FCN (tertiary_icode)
6545 (second_reload_reg, real_oldequiv,
6546 third_reload_reg)));
6548 else
6549 gen_reload (second_reload_reg, real_oldequiv,
6550 rl->opnum,
6551 rl->when_needed);
6553 oldequiv = second_reload_reg;
6557 #endif
6559 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6561 rtx real_oldequiv = oldequiv;
6563 if ((REG_P (oldequiv)
6564 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6565 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6566 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6567 || (GET_CODE (oldequiv) == SUBREG
6568 && REG_P (SUBREG_REG (oldequiv))
6569 && (REGNO (SUBREG_REG (oldequiv))
6570 >= FIRST_PSEUDO_REGISTER)
6571 && ((reg_equiv_memory_loc
6572 [REGNO (SUBREG_REG (oldequiv))] != 0)
6573 || (reg_equiv_constant
6574 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6575 || (CONSTANT_P (oldequiv)
6576 && (PREFERRED_RELOAD_CLASS (oldequiv,
6577 REGNO_REG_CLASS (REGNO (reloadreg)))
6578 == NO_REGS)))
6579 real_oldequiv = rl->in;
6580 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6581 rl->when_needed);
6584 if (flag_non_call_exceptions)
6585 copy_eh_notes (insn, get_insns ());
6587 /* End this sequence. */
6588 *where = get_insns ();
6589 end_sequence ();
6591 /* Update reload_override_in so that delete_address_reloads_1
6592 can see the actual register usage. */
6593 if (oldequiv_reg)
6594 reload_override_in[j] = oldequiv;
6597 /* Generate insns to for the output reload RL, which is for the insn described
6598 by CHAIN and has the number J. */
6599 static void
6600 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
6601 int j)
6603 rtx reloadreg = rl->reg_rtx;
6604 rtx insn = chain->insn;
6605 int special = 0;
6606 rtx old = rl->out;
6607 enum machine_mode mode = GET_MODE (old);
6608 rtx p;
6610 if (rl->when_needed == RELOAD_OTHER)
6611 start_sequence ();
6612 else
6613 push_to_sequence (output_reload_insns[rl->opnum]);
6615 /* Determine the mode to reload in.
6616 See comments above (for input reloading). */
6618 if (mode == VOIDmode)
6620 /* VOIDmode should never happen for an output. */
6621 if (asm_noperands (PATTERN (insn)) < 0)
6622 /* It's the compiler's fault. */
6623 fatal_insn ("VOIDmode on an output", insn);
6624 error_for_asm (insn, "output operand is constant in `asm'");
6625 /* Prevent crash--use something we know is valid. */
6626 mode = word_mode;
6627 old = gen_rtx_REG (mode, REGNO (reloadreg));
6630 if (GET_MODE (reloadreg) != mode)
6631 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6633 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6635 /* If we need two reload regs, set RELOADREG to the intermediate
6636 one, since it will be stored into OLD. We might need a secondary
6637 register only for an input reload, so check again here. */
6639 if (rl->secondary_out_reload >= 0)
6641 rtx real_old = old;
6643 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
6644 && reg_equiv_mem[REGNO (old)] != 0)
6645 real_old = reg_equiv_mem[REGNO (old)];
6647 if ((SECONDARY_OUTPUT_RELOAD_CLASS (rl->class,
6648 mode, real_old)
6649 != NO_REGS))
6651 rtx second_reloadreg = reloadreg;
6652 reloadreg = rld[rl->secondary_out_reload].reg_rtx;
6654 /* See if RELOADREG is to be used as a scratch register
6655 or as an intermediate register. */
6656 if (rl->secondary_out_icode != CODE_FOR_nothing)
6658 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6659 (real_old, second_reloadreg, reloadreg)));
6660 special = 1;
6662 else
6664 /* See if we need both a scratch and intermediate reload
6665 register. */
6667 int secondary_reload = rl->secondary_out_reload;
6668 enum insn_code tertiary_icode
6669 = rld[secondary_reload].secondary_out_icode;
6671 if (GET_MODE (reloadreg) != mode)
6672 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6674 if (tertiary_icode != CODE_FOR_nothing)
6676 rtx third_reloadreg
6677 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
6678 rtx tem;
6680 /* Copy primary reload reg to secondary reload reg.
6681 (Note that these have been swapped above, then
6682 secondary reload reg to OLD using our insn.) */
6684 /* If REAL_OLD is a paradoxical SUBREG, remove it
6685 and try to put the opposite SUBREG on
6686 RELOADREG. */
6687 if (GET_CODE (real_old) == SUBREG
6688 && (GET_MODE_SIZE (GET_MODE (real_old))
6689 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6690 && 0 != (tem = gen_lowpart_common
6691 (GET_MODE (SUBREG_REG (real_old)),
6692 reloadreg)))
6693 real_old = SUBREG_REG (real_old), reloadreg = tem;
6695 gen_reload (reloadreg, second_reloadreg,
6696 rl->opnum, rl->when_needed);
6697 emit_insn ((GEN_FCN (tertiary_icode)
6698 (real_old, reloadreg, third_reloadreg)));
6699 special = 1;
6702 else
6703 /* Copy between the reload regs here and then to
6704 OUT later. */
6706 gen_reload (reloadreg, second_reloadreg,
6707 rl->opnum, rl->when_needed);
6711 #endif
6713 /* Output the last reload insn. */
6714 if (! special)
6716 rtx set;
6718 /* Don't output the last reload if OLD is not the dest of
6719 INSN and is in the src and is clobbered by INSN. */
6720 if (! flag_expensive_optimizations
6721 || !REG_P (old)
6722 || !(set = single_set (insn))
6723 || rtx_equal_p (old, SET_DEST (set))
6724 || !reg_mentioned_p (old, SET_SRC (set))
6725 || !regno_clobbered_p (REGNO (old), insn, rl->mode, 0))
6726 gen_reload (old, reloadreg, rl->opnum,
6727 rl->when_needed);
6730 /* Look at all insns we emitted, just to be safe. */
6731 for (p = get_insns (); p; p = NEXT_INSN (p))
6732 if (INSN_P (p))
6734 rtx pat = PATTERN (p);
6736 /* If this output reload doesn't come from a spill reg,
6737 clear any memory of reloaded copies of the pseudo reg.
6738 If this output reload comes from a spill reg,
6739 reg_has_output_reload will make this do nothing. */
6740 note_stores (pat, forget_old_reloads_1, NULL);
6742 if (reg_mentioned_p (rl->reg_rtx, pat))
6744 rtx set = single_set (insn);
6745 if (reload_spill_index[j] < 0
6746 && set
6747 && SET_SRC (set) == rl->reg_rtx)
6749 int src = REGNO (SET_SRC (set));
6751 reload_spill_index[j] = src;
6752 SET_HARD_REG_BIT (reg_is_output_reload, src);
6753 if (find_regno_note (insn, REG_DEAD, src))
6754 SET_HARD_REG_BIT (reg_reloaded_died, src);
6756 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6758 int s = rl->secondary_out_reload;
6759 set = single_set (p);
6760 /* If this reload copies only to the secondary reload
6761 register, the secondary reload does the actual
6762 store. */
6763 if (s >= 0 && set == NULL_RTX)
6764 /* We can't tell what function the secondary reload
6765 has and where the actual store to the pseudo is
6766 made; leave new_spill_reg_store alone. */
6768 else if (s >= 0
6769 && SET_SRC (set) == rl->reg_rtx
6770 && SET_DEST (set) == rld[s].reg_rtx)
6772 /* Usually the next instruction will be the
6773 secondary reload insn; if we can confirm
6774 that it is, setting new_spill_reg_store to
6775 that insn will allow an extra optimization. */
6776 rtx s_reg = rld[s].reg_rtx;
6777 rtx next = NEXT_INSN (p);
6778 rld[s].out = rl->out;
6779 rld[s].out_reg = rl->out_reg;
6780 set = single_set (next);
6781 if (set && SET_SRC (set) == s_reg
6782 && ! new_spill_reg_store[REGNO (s_reg)])
6784 SET_HARD_REG_BIT (reg_is_output_reload,
6785 REGNO (s_reg));
6786 new_spill_reg_store[REGNO (s_reg)] = next;
6789 else
6790 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6795 if (rl->when_needed == RELOAD_OTHER)
6797 emit_insn (other_output_reload_insns[rl->opnum]);
6798 other_output_reload_insns[rl->opnum] = get_insns ();
6800 else
6801 output_reload_insns[rl->opnum] = get_insns ();
6803 if (flag_non_call_exceptions)
6804 copy_eh_notes (insn, get_insns ());
6806 end_sequence ();
6809 /* Do input reloading for reload RL, which is for the insn described by CHAIN
6810 and has the number J. */
6811 static void
6812 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
6814 rtx insn = chain->insn;
6815 rtx old = (rl->in && MEM_P (rl->in)
6816 ? rl->in_reg : rl->in);
6818 if (old != 0
6819 /* AUTO_INC reloads need to be handled even if inherited. We got an
6820 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6821 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
6822 && ! rtx_equal_p (rl->reg_rtx, old)
6823 && rl->reg_rtx != 0)
6824 emit_input_reload_insns (chain, rld + j, old, j);
6826 /* When inheriting a wider reload, we have a MEM in rl->in,
6827 e.g. inheriting a SImode output reload for
6828 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6829 if (optimize && reload_inherited[j] && rl->in
6830 && MEM_P (rl->in)
6831 && MEM_P (rl->in_reg)
6832 && reload_spill_index[j] >= 0
6833 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6834 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
6836 /* If we are reloading a register that was recently stored in with an
6837 output-reload, see if we can prove there was
6838 actually no need to store the old value in it. */
6840 if (optimize
6841 && (reload_inherited[j] || reload_override_in[j])
6842 && rl->reg_rtx
6843 && REG_P (rl->reg_rtx)
6844 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
6845 #if 0
6846 /* There doesn't seem to be any reason to restrict this to pseudos
6847 and doing so loses in the case where we are copying from a
6848 register of the wrong class. */
6849 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
6850 >= FIRST_PSEUDO_REGISTER)
6851 #endif
6852 /* The insn might have already some references to stackslots
6853 replaced by MEMs, while reload_out_reg still names the
6854 original pseudo. */
6855 && (dead_or_set_p (insn,
6856 spill_reg_stored_to[REGNO (rl->reg_rtx)])
6857 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
6858 rl->out_reg)))
6859 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
6862 /* Do output reloading for reload RL, which is for the insn described by
6863 CHAIN and has the number J.
6864 ??? At some point we need to support handling output reloads of
6865 JUMP_INSNs or insns that set cc0. */
6866 static void
6867 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
6869 rtx note, old;
6870 rtx insn = chain->insn;
6871 /* If this is an output reload that stores something that is
6872 not loaded in this same reload, see if we can eliminate a previous
6873 store. */
6874 rtx pseudo = rl->out_reg;
6876 if (pseudo
6877 && optimize
6878 && REG_P (pseudo)
6879 && ! rtx_equal_p (rl->in_reg, pseudo)
6880 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
6881 && reg_last_reload_reg[REGNO (pseudo)])
6883 int pseudo_no = REGNO (pseudo);
6884 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
6886 /* We don't need to test full validity of last_regno for
6887 inherit here; we only want to know if the store actually
6888 matches the pseudo. */
6889 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
6890 && reg_reloaded_contents[last_regno] == pseudo_no
6891 && spill_reg_store[last_regno]
6892 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
6893 delete_output_reload (insn, j, last_regno);
6896 old = rl->out_reg;
6897 if (old == 0
6898 || rl->reg_rtx == old
6899 || rl->reg_rtx == 0)
6900 return;
6902 /* An output operand that dies right away does need a reload,
6903 but need not be copied from it. Show the new location in the
6904 REG_UNUSED note. */
6905 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
6906 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6908 XEXP (note, 0) = rl->reg_rtx;
6909 return;
6911 /* Likewise for a SUBREG of an operand that dies. */
6912 else if (GET_CODE (old) == SUBREG
6913 && REG_P (SUBREG_REG (old))
6914 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6915 SUBREG_REG (old))))
6917 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6918 rl->reg_rtx);
6919 return;
6921 else if (GET_CODE (old) == SCRATCH)
6922 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6923 but we don't want to make an output reload. */
6924 return;
6926 /* If is a JUMP_INSN, we can't support output reloads yet. */
6927 if (JUMP_P (insn))
6928 abort ();
6930 emit_output_reload_insns (chain, rld + j, j);
6933 /* Reload number R reloads from or to a group of hard registers starting at
6934 register REGNO. Return true if it can be treated for inheritance purposes
6935 like a group of reloads, each one reloading a single hard register.
6936 The caller has already checked that the spill register and REGNO use
6937 the same number of registers to store the reload value. */
6939 static bool
6940 inherit_piecemeal_p (int r ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED)
6942 #ifdef CANNOT_CHANGE_MODE_CLASS
6943 return (!REG_CANNOT_CHANGE_MODE_P (reload_spill_index[r],
6944 GET_MODE (rld[r].reg_rtx),
6945 reg_raw_mode[reload_spill_index[r]])
6946 && !REG_CANNOT_CHANGE_MODE_P (regno,
6947 GET_MODE (rld[r].reg_rtx),
6948 reg_raw_mode[regno]));
6949 #else
6950 return true;
6951 #endif
6954 /* Output insns to reload values in and out of the chosen reload regs. */
6956 static void
6957 emit_reload_insns (struct insn_chain *chain)
6959 rtx insn = chain->insn;
6961 int j;
6963 CLEAR_HARD_REG_SET (reg_reloaded_died);
6965 for (j = 0; j < reload_n_operands; j++)
6966 input_reload_insns[j] = input_address_reload_insns[j]
6967 = inpaddr_address_reload_insns[j]
6968 = output_reload_insns[j] = output_address_reload_insns[j]
6969 = outaddr_address_reload_insns[j]
6970 = other_output_reload_insns[j] = 0;
6971 other_input_address_reload_insns = 0;
6972 other_input_reload_insns = 0;
6973 operand_reload_insns = 0;
6974 other_operand_reload_insns = 0;
6976 /* Dump reloads into the dump file. */
6977 if (dump_file)
6979 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
6980 debug_reload_to_stream (dump_file);
6983 /* Now output the instructions to copy the data into and out of the
6984 reload registers. Do these in the order that the reloads were reported,
6985 since reloads of base and index registers precede reloads of operands
6986 and the operands may need the base and index registers reloaded. */
6988 for (j = 0; j < n_reloads; j++)
6990 if (rld[j].reg_rtx
6991 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
6992 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
6994 do_input_reload (chain, rld + j, j);
6995 do_output_reload (chain, rld + j, j);
6998 /* Now write all the insns we made for reloads in the order expected by
6999 the allocation functions. Prior to the insn being reloaded, we write
7000 the following reloads:
7002 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7004 RELOAD_OTHER reloads.
7006 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7007 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7008 RELOAD_FOR_INPUT reload for the operand.
7010 RELOAD_FOR_OPADDR_ADDRS reloads.
7012 RELOAD_FOR_OPERAND_ADDRESS reloads.
7014 After the insn being reloaded, we write the following:
7016 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7017 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7018 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7019 reloads for the operand. The RELOAD_OTHER output reloads are
7020 output in descending order by reload number. */
7022 emit_insn_before_sameloc (other_input_address_reload_insns, insn);
7023 emit_insn_before_sameloc (other_input_reload_insns, insn);
7025 for (j = 0; j < reload_n_operands; j++)
7027 emit_insn_before_sameloc (inpaddr_address_reload_insns[j], insn);
7028 emit_insn_before_sameloc (input_address_reload_insns[j], insn);
7029 emit_insn_before_sameloc (input_reload_insns[j], insn);
7032 emit_insn_before_sameloc (other_operand_reload_insns, insn);
7033 emit_insn_before_sameloc (operand_reload_insns, insn);
7035 for (j = 0; j < reload_n_operands; j++)
7037 rtx x = emit_insn_after_sameloc (outaddr_address_reload_insns[j], insn);
7038 x = emit_insn_after_sameloc (output_address_reload_insns[j], x);
7039 x = emit_insn_after_sameloc (output_reload_insns[j], x);
7040 emit_insn_after_sameloc (other_output_reload_insns[j], x);
7043 /* For all the spill regs newly reloaded in this instruction,
7044 record what they were reloaded from, so subsequent instructions
7045 can inherit the reloads.
7047 Update spill_reg_store for the reloads of this insn.
7048 Copy the elements that were updated in the loop above. */
7050 for (j = 0; j < n_reloads; j++)
7052 int r = reload_order[j];
7053 int i = reload_spill_index[r];
7055 /* If this is a non-inherited input reload from a pseudo, we must
7056 clear any memory of a previous store to the same pseudo. Only do
7057 something if there will not be an output reload for the pseudo
7058 being reloaded. */
7059 if (rld[r].in_reg != 0
7060 && ! (reload_inherited[r] || reload_override_in[r]))
7062 rtx reg = rld[r].in_reg;
7064 if (GET_CODE (reg) == SUBREG)
7065 reg = SUBREG_REG (reg);
7067 if (REG_P (reg)
7068 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7069 && ! reg_has_output_reload[REGNO (reg)])
7071 int nregno = REGNO (reg);
7073 if (reg_last_reload_reg[nregno])
7075 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7077 if (reg_reloaded_contents[last_regno] == nregno)
7078 spill_reg_store[last_regno] = 0;
7083 /* I is nonneg if this reload used a register.
7084 If rld[r].reg_rtx is 0, this is an optional reload
7085 that we opted to ignore. */
7087 if (i >= 0 && rld[r].reg_rtx != 0)
7089 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7090 int k;
7091 int part_reaches_end = 0;
7092 int all_reaches_end = 1;
7094 /* For a multi register reload, we need to check if all or part
7095 of the value lives to the end. */
7096 for (k = 0; k < nr; k++)
7098 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7099 rld[r].when_needed))
7100 part_reaches_end = 1;
7101 else
7102 all_reaches_end = 0;
7105 /* Ignore reloads that don't reach the end of the insn in
7106 entirety. */
7107 if (all_reaches_end)
7109 /* First, clear out memory of what used to be in this spill reg.
7110 If consecutive registers are used, clear them all. */
7112 for (k = 0; k < nr; k++)
7114 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7115 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7118 /* Maybe the spill reg contains a copy of reload_out. */
7119 if (rld[r].out != 0
7120 && (REG_P (rld[r].out)
7121 #ifdef AUTO_INC_DEC
7122 || ! rld[r].out_reg
7123 #endif
7124 || REG_P (rld[r].out_reg)))
7126 rtx out = (REG_P (rld[r].out)
7127 ? rld[r].out
7128 : rld[r].out_reg
7129 ? rld[r].out_reg
7130 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7131 int nregno = REGNO (out);
7132 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7133 : hard_regno_nregs[nregno]
7134 [GET_MODE (rld[r].reg_rtx)]);
7135 bool piecemeal;
7137 spill_reg_store[i] = new_spill_reg_store[i];
7138 spill_reg_stored_to[i] = out;
7139 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7141 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7142 && nr == nnr
7143 && inherit_piecemeal_p (r, nregno));
7145 /* If NREGNO is a hard register, it may occupy more than
7146 one register. If it does, say what is in the
7147 rest of the registers assuming that both registers
7148 agree on how many words the object takes. If not,
7149 invalidate the subsequent registers. */
7151 if (nregno < FIRST_PSEUDO_REGISTER)
7152 for (k = 1; k < nnr; k++)
7153 reg_last_reload_reg[nregno + k]
7154 = (piecemeal
7155 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7156 : 0);
7158 /* Now do the inverse operation. */
7159 for (k = 0; k < nr; k++)
7161 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7162 reg_reloaded_contents[i + k]
7163 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7164 ? nregno
7165 : nregno + k);
7166 reg_reloaded_insn[i + k] = insn;
7167 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7168 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (out)))
7169 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7173 /* Maybe the spill reg contains a copy of reload_in. Only do
7174 something if there will not be an output reload for
7175 the register being reloaded. */
7176 else if (rld[r].out_reg == 0
7177 && rld[r].in != 0
7178 && ((REG_P (rld[r].in)
7179 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7180 && ! reg_has_output_reload[REGNO (rld[r].in)])
7181 || (REG_P (rld[r].in_reg)
7182 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7183 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7185 int nregno;
7186 int nnr;
7187 rtx in;
7188 bool piecemeal;
7190 if (REG_P (rld[r].in)
7191 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7192 in = rld[r].in;
7193 else if (REG_P (rld[r].in_reg))
7194 in = rld[r].in_reg;
7195 else
7196 in = XEXP (rld[r].in_reg, 0);
7197 nregno = REGNO (in);
7199 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7200 : hard_regno_nregs[nregno]
7201 [GET_MODE (rld[r].reg_rtx)]);
7203 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7205 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7206 && nr == nnr
7207 && inherit_piecemeal_p (r, nregno));
7209 if (nregno < FIRST_PSEUDO_REGISTER)
7210 for (k = 1; k < nnr; k++)
7211 reg_last_reload_reg[nregno + k]
7212 = (piecemeal
7213 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7214 : 0);
7216 /* Unless we inherited this reload, show we haven't
7217 recently done a store.
7218 Previous stores of inherited auto_inc expressions
7219 also have to be discarded. */
7220 if (! reload_inherited[r]
7221 || (rld[r].out && ! rld[r].out_reg))
7222 spill_reg_store[i] = 0;
7224 for (k = 0; k < nr; k++)
7226 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7227 reg_reloaded_contents[i + k]
7228 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7229 ? nregno
7230 : nregno + k);
7231 reg_reloaded_insn[i + k] = insn;
7232 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7233 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (in)))
7234 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7239 /* However, if part of the reload reaches the end, then we must
7240 invalidate the old info for the part that survives to the end. */
7241 else if (part_reaches_end)
7243 for (k = 0; k < nr; k++)
7244 if (reload_reg_reaches_end_p (i + k,
7245 rld[r].opnum,
7246 rld[r].when_needed))
7247 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7251 /* The following if-statement was #if 0'd in 1.34 (or before...).
7252 It's reenabled in 1.35 because supposedly nothing else
7253 deals with this problem. */
7255 /* If a register gets output-reloaded from a non-spill register,
7256 that invalidates any previous reloaded copy of it.
7257 But forget_old_reloads_1 won't get to see it, because
7258 it thinks only about the original insn. So invalidate it here. */
7259 if (i < 0 && rld[r].out != 0
7260 && (REG_P (rld[r].out)
7261 || (MEM_P (rld[r].out)
7262 && REG_P (rld[r].out_reg))))
7264 rtx out = (REG_P (rld[r].out)
7265 ? rld[r].out : rld[r].out_reg);
7266 int nregno = REGNO (out);
7267 if (nregno >= FIRST_PSEUDO_REGISTER)
7269 rtx src_reg, store_insn = NULL_RTX;
7271 reg_last_reload_reg[nregno] = 0;
7273 /* If we can find a hard register that is stored, record
7274 the storing insn so that we may delete this insn with
7275 delete_output_reload. */
7276 src_reg = rld[r].reg_rtx;
7278 /* If this is an optional reload, try to find the source reg
7279 from an input reload. */
7280 if (! src_reg)
7282 rtx set = single_set (insn);
7283 if (set && SET_DEST (set) == rld[r].out)
7285 int k;
7287 src_reg = SET_SRC (set);
7288 store_insn = insn;
7289 for (k = 0; k < n_reloads; k++)
7291 if (rld[k].in == src_reg)
7293 src_reg = rld[k].reg_rtx;
7294 break;
7299 else
7300 store_insn = new_spill_reg_store[REGNO (src_reg)];
7301 if (src_reg && REG_P (src_reg)
7302 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7304 int src_regno = REGNO (src_reg);
7305 int nr = hard_regno_nregs[src_regno][rld[r].mode];
7306 /* The place where to find a death note varies with
7307 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7308 necessarily checked exactly in the code that moves
7309 notes, so just check both locations. */
7310 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7311 if (! note && store_insn)
7312 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7313 while (nr-- > 0)
7315 spill_reg_store[src_regno + nr] = store_insn;
7316 spill_reg_stored_to[src_regno + nr] = out;
7317 reg_reloaded_contents[src_regno + nr] = nregno;
7318 reg_reloaded_insn[src_regno + nr] = store_insn;
7319 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7320 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7321 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + nr,
7322 GET_MODE (src_reg)))
7323 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7324 src_regno + nr);
7325 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7326 if (note)
7327 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7328 else
7329 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7331 reg_last_reload_reg[nregno] = src_reg;
7332 /* We have to set reg_has_output_reload here, or else
7333 forget_old_reloads_1 will clear reg_last_reload_reg
7334 right away. */
7335 reg_has_output_reload[nregno] = 1;
7338 else
7340 int num_regs = hard_regno_nregs[nregno][GET_MODE (rld[r].out)];
7342 while (num_regs-- > 0)
7343 reg_last_reload_reg[nregno + num_regs] = 0;
7347 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7350 /* Emit code to perform a reload from IN (which may be a reload register) to
7351 OUT (which may also be a reload register). IN or OUT is from operand
7352 OPNUM with reload type TYPE.
7354 Returns first insn emitted. */
7357 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7359 rtx last = get_last_insn ();
7360 rtx tem;
7362 /* If IN is a paradoxical SUBREG, remove it and try to put the
7363 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7364 if (GET_CODE (in) == SUBREG
7365 && (GET_MODE_SIZE (GET_MODE (in))
7366 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7367 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7368 in = SUBREG_REG (in), out = tem;
7369 else if (GET_CODE (out) == SUBREG
7370 && (GET_MODE_SIZE (GET_MODE (out))
7371 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7372 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7373 out = SUBREG_REG (out), in = tem;
7375 /* How to do this reload can get quite tricky. Normally, we are being
7376 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7377 register that didn't get a hard register. In that case we can just
7378 call emit_move_insn.
7380 We can also be asked to reload a PLUS that adds a register or a MEM to
7381 another register, constant or MEM. This can occur during frame pointer
7382 elimination and while reloading addresses. This case is handled by
7383 trying to emit a single insn to perform the add. If it is not valid,
7384 we use a two insn sequence.
7386 Finally, we could be called to handle an 'o' constraint by putting
7387 an address into a register. In that case, we first try to do this
7388 with a named pattern of "reload_load_address". If no such pattern
7389 exists, we just emit a SET insn and hope for the best (it will normally
7390 be valid on machines that use 'o').
7392 This entire process is made complex because reload will never
7393 process the insns we generate here and so we must ensure that
7394 they will fit their constraints and also by the fact that parts of
7395 IN might be being reloaded separately and replaced with spill registers.
7396 Because of this, we are, in some sense, just guessing the right approach
7397 here. The one listed above seems to work.
7399 ??? At some point, this whole thing needs to be rethought. */
7401 if (GET_CODE (in) == PLUS
7402 && (REG_P (XEXP (in, 0))
7403 || GET_CODE (XEXP (in, 0)) == SUBREG
7404 || MEM_P (XEXP (in, 0)))
7405 && (REG_P (XEXP (in, 1))
7406 || GET_CODE (XEXP (in, 1)) == SUBREG
7407 || CONSTANT_P (XEXP (in, 1))
7408 || MEM_P (XEXP (in, 1))))
7410 /* We need to compute the sum of a register or a MEM and another
7411 register, constant, or MEM, and put it into the reload
7412 register. The best possible way of doing this is if the machine
7413 has a three-operand ADD insn that accepts the required operands.
7415 The simplest approach is to try to generate such an insn and see if it
7416 is recognized and matches its constraints. If so, it can be used.
7418 It might be better not to actually emit the insn unless it is valid,
7419 but we need to pass the insn as an operand to `recog' and
7420 `extract_insn' and it is simpler to emit and then delete the insn if
7421 not valid than to dummy things up. */
7423 rtx op0, op1, tem, insn;
7424 int code;
7426 op0 = find_replacement (&XEXP (in, 0));
7427 op1 = find_replacement (&XEXP (in, 1));
7429 /* Since constraint checking is strict, commutativity won't be
7430 checked, so we need to do that here to avoid spurious failure
7431 if the add instruction is two-address and the second operand
7432 of the add is the same as the reload reg, which is frequently
7433 the case. If the insn would be A = B + A, rearrange it so
7434 it will be A = A + B as constrain_operands expects. */
7436 if (REG_P (XEXP (in, 1))
7437 && REGNO (out) == REGNO (XEXP (in, 1)))
7438 tem = op0, op0 = op1, op1 = tem;
7440 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7441 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7443 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7444 code = recog_memoized (insn);
7446 if (code >= 0)
7448 extract_insn (insn);
7449 /* We want constrain operands to treat this insn strictly in
7450 its validity determination, i.e., the way it would after reload
7451 has completed. */
7452 if (constrain_operands (1))
7453 return insn;
7456 delete_insns_since (last);
7458 /* If that failed, we must use a conservative two-insn sequence.
7460 Use a move to copy one operand into the reload register. Prefer
7461 to reload a constant, MEM or pseudo since the move patterns can
7462 handle an arbitrary operand. If OP1 is not a constant, MEM or
7463 pseudo and OP1 is not a valid operand for an add instruction, then
7464 reload OP1.
7466 After reloading one of the operands into the reload register, add
7467 the reload register to the output register.
7469 If there is another way to do this for a specific machine, a
7470 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7471 we emit below. */
7473 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7475 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
7476 || (REG_P (op1)
7477 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7478 || (code != CODE_FOR_nothing
7479 && ! ((*insn_data[code].operand[2].predicate)
7480 (op1, insn_data[code].operand[2].mode))))
7481 tem = op0, op0 = op1, op1 = tem;
7483 gen_reload (out, op0, opnum, type);
7485 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7486 This fixes a problem on the 32K where the stack pointer cannot
7487 be used as an operand of an add insn. */
7489 if (rtx_equal_p (op0, op1))
7490 op1 = out;
7492 insn = emit_insn (gen_add2_insn (out, op1));
7494 /* If that failed, copy the address register to the reload register.
7495 Then add the constant to the reload register. */
7497 code = recog_memoized (insn);
7499 if (code >= 0)
7501 extract_insn (insn);
7502 /* We want constrain operands to treat this insn strictly in
7503 its validity determination, i.e., the way it would after reload
7504 has completed. */
7505 if (constrain_operands (1))
7507 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7508 REG_NOTES (insn)
7509 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7510 return insn;
7514 delete_insns_since (last);
7516 gen_reload (out, op1, opnum, type);
7517 insn = emit_insn (gen_add2_insn (out, op0));
7518 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7521 #ifdef SECONDARY_MEMORY_NEEDED
7522 /* If we need a memory location to do the move, do it that way. */
7523 else if ((REG_P (in) || GET_CODE (in) == SUBREG)
7524 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7525 && (REG_P (out) || GET_CODE (out) == SUBREG)
7526 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7527 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7528 REGNO_REG_CLASS (reg_or_subregno (out)),
7529 GET_MODE (out)))
7531 /* Get the memory to use and rewrite both registers to its mode. */
7532 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7534 if (GET_MODE (loc) != GET_MODE (out))
7535 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7537 if (GET_MODE (loc) != GET_MODE (in))
7538 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7540 gen_reload (loc, in, opnum, type);
7541 gen_reload (out, loc, opnum, type);
7543 #endif
7545 /* If IN is a simple operand, use gen_move_insn. */
7546 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
7547 emit_insn (gen_move_insn (out, in));
7549 #ifdef HAVE_reload_load_address
7550 else if (HAVE_reload_load_address)
7551 emit_insn (gen_reload_load_address (out, in));
7552 #endif
7554 /* Otherwise, just write (set OUT IN) and hope for the best. */
7555 else
7556 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7558 /* Return the first insn emitted.
7559 We can not just return get_last_insn, because there may have
7560 been multiple instructions emitted. Also note that gen_move_insn may
7561 emit more than one insn itself, so we can not assume that there is one
7562 insn emitted per emit_insn_before call. */
7564 return last ? NEXT_INSN (last) : get_insns ();
7567 /* Delete a previously made output-reload whose result we now believe
7568 is not needed. First we double-check.
7570 INSN is the insn now being processed.
7571 LAST_RELOAD_REG is the hard register number for which we want to delete
7572 the last output reload.
7573 J is the reload-number that originally used REG. The caller has made
7574 certain that reload J doesn't use REG any longer for input. */
7576 static void
7577 delete_output_reload (rtx insn, int j, int last_reload_reg)
7579 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7580 rtx reg = spill_reg_stored_to[last_reload_reg];
7581 int k;
7582 int n_occurrences;
7583 int n_inherited = 0;
7584 rtx i1;
7585 rtx substed;
7587 /* It is possible that this reload has been only used to set another reload
7588 we eliminated earlier and thus deleted this instruction too. */
7589 if (INSN_DELETED_P (output_reload_insn))
7590 return;
7592 /* Get the raw pseudo-register referred to. */
7594 while (GET_CODE (reg) == SUBREG)
7595 reg = SUBREG_REG (reg);
7596 substed = reg_equiv_memory_loc[REGNO (reg)];
7598 /* This is unsafe if the operand occurs more often in the current
7599 insn than it is inherited. */
7600 for (k = n_reloads - 1; k >= 0; k--)
7602 rtx reg2 = rld[k].in;
7603 if (! reg2)
7604 continue;
7605 if (MEM_P (reg2) || reload_override_in[k])
7606 reg2 = rld[k].in_reg;
7607 #ifdef AUTO_INC_DEC
7608 if (rld[k].out && ! rld[k].out_reg)
7609 reg2 = XEXP (rld[k].in_reg, 0);
7610 #endif
7611 while (GET_CODE (reg2) == SUBREG)
7612 reg2 = SUBREG_REG (reg2);
7613 if (rtx_equal_p (reg2, reg))
7615 if (reload_inherited[k] || reload_override_in[k] || k == j)
7617 n_inherited++;
7618 reg2 = rld[k].out_reg;
7619 if (! reg2)
7620 continue;
7621 while (GET_CODE (reg2) == SUBREG)
7622 reg2 = XEXP (reg2, 0);
7623 if (rtx_equal_p (reg2, reg))
7624 n_inherited++;
7626 else
7627 return;
7630 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7631 if (substed)
7632 n_occurrences += count_occurrences (PATTERN (insn),
7633 eliminate_regs (substed, 0,
7634 NULL_RTX), 0);
7635 if (n_occurrences > n_inherited)
7636 return;
7638 /* If the pseudo-reg we are reloading is no longer referenced
7639 anywhere between the store into it and here,
7640 and no jumps or labels intervene, then the value can get
7641 here through the reload reg alone.
7642 Otherwise, give up--return. */
7643 for (i1 = NEXT_INSN (output_reload_insn);
7644 i1 != insn; i1 = NEXT_INSN (i1))
7646 if (LABEL_P (i1) || JUMP_P (i1))
7647 return;
7648 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
7649 && reg_mentioned_p (reg, PATTERN (i1)))
7651 /* If this is USE in front of INSN, we only have to check that
7652 there are no more references than accounted for by inheritance. */
7653 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
7655 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7656 i1 = NEXT_INSN (i1);
7658 if (n_occurrences <= n_inherited && i1 == insn)
7659 break;
7660 return;
7664 /* We will be deleting the insn. Remove the spill reg information. */
7665 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
7667 spill_reg_store[last_reload_reg + k] = 0;
7668 spill_reg_stored_to[last_reload_reg + k] = 0;
7671 /* The caller has already checked that REG dies or is set in INSN.
7672 It has also checked that we are optimizing, and thus some
7673 inaccuracies in the debugging information are acceptable.
7674 So we could just delete output_reload_insn. But in some cases
7675 we can improve the debugging information without sacrificing
7676 optimization - maybe even improving the code: See if the pseudo
7677 reg has been completely replaced with reload regs. If so, delete
7678 the store insn and forget we had a stack slot for the pseudo. */
7679 if (rld[j].out != rld[j].in
7680 && REG_N_DEATHS (REGNO (reg)) == 1
7681 && REG_N_SETS (REGNO (reg)) == 1
7682 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7683 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7685 rtx i2;
7687 /* We know that it was used only between here and the beginning of
7688 the current basic block. (We also know that the last use before
7689 INSN was the output reload we are thinking of deleting, but never
7690 mind that.) Search that range; see if any ref remains. */
7691 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7693 rtx set = single_set (i2);
7695 /* Uses which just store in the pseudo don't count,
7696 since if they are the only uses, they are dead. */
7697 if (set != 0 && SET_DEST (set) == reg)
7698 continue;
7699 if (LABEL_P (i2)
7700 || JUMP_P (i2))
7701 break;
7702 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
7703 && reg_mentioned_p (reg, PATTERN (i2)))
7705 /* Some other ref remains; just delete the output reload we
7706 know to be dead. */
7707 delete_address_reloads (output_reload_insn, insn);
7708 delete_insn (output_reload_insn);
7709 return;
7713 /* Delete the now-dead stores into this pseudo. Note that this
7714 loop also takes care of deleting output_reload_insn. */
7715 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7717 rtx set = single_set (i2);
7719 if (set != 0 && SET_DEST (set) == reg)
7721 delete_address_reloads (i2, insn);
7722 delete_insn (i2);
7724 if (LABEL_P (i2)
7725 || JUMP_P (i2))
7726 break;
7729 /* For the debugging info, say the pseudo lives in this reload reg. */
7730 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
7731 alter_reg (REGNO (reg), -1);
7733 else
7735 delete_address_reloads (output_reload_insn, insn);
7736 delete_insn (output_reload_insn);
7740 /* We are going to delete DEAD_INSN. Recursively delete loads of
7741 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7742 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7743 static void
7744 delete_address_reloads (rtx dead_insn, rtx current_insn)
7746 rtx set = single_set (dead_insn);
7747 rtx set2, dst, prev, next;
7748 if (set)
7750 rtx dst = SET_DEST (set);
7751 if (MEM_P (dst))
7752 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
7754 /* If we deleted the store from a reloaded post_{in,de}c expression,
7755 we can delete the matching adds. */
7756 prev = PREV_INSN (dead_insn);
7757 next = NEXT_INSN (dead_insn);
7758 if (! prev || ! next)
7759 return;
7760 set = single_set (next);
7761 set2 = single_set (prev);
7762 if (! set || ! set2
7763 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
7764 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
7765 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
7766 return;
7767 dst = SET_DEST (set);
7768 if (! rtx_equal_p (dst, SET_DEST (set2))
7769 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
7770 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
7771 || (INTVAL (XEXP (SET_SRC (set), 1))
7772 != -INTVAL (XEXP (SET_SRC (set2), 1))))
7773 return;
7774 delete_related_insns (prev);
7775 delete_related_insns (next);
7778 /* Subfunction of delete_address_reloads: process registers found in X. */
7779 static void
7780 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
7782 rtx prev, set, dst, i2;
7783 int i, j;
7784 enum rtx_code code = GET_CODE (x);
7786 if (code != REG)
7788 const char *fmt = GET_RTX_FORMAT (code);
7789 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7791 if (fmt[i] == 'e')
7792 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
7793 else if (fmt[i] == 'E')
7795 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7796 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
7797 current_insn);
7800 return;
7803 if (spill_reg_order[REGNO (x)] < 0)
7804 return;
7806 /* Scan backwards for the insn that sets x. This might be a way back due
7807 to inheritance. */
7808 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
7810 code = GET_CODE (prev);
7811 if (code == CODE_LABEL || code == JUMP_INSN)
7812 return;
7813 if (!INSN_P (prev))
7814 continue;
7815 if (reg_set_p (x, PATTERN (prev)))
7816 break;
7817 if (reg_referenced_p (x, PATTERN (prev)))
7818 return;
7820 if (! prev || INSN_UID (prev) < reload_first_uid)
7821 return;
7822 /* Check that PREV only sets the reload register. */
7823 set = single_set (prev);
7824 if (! set)
7825 return;
7826 dst = SET_DEST (set);
7827 if (!REG_P (dst)
7828 || ! rtx_equal_p (dst, x))
7829 return;
7830 if (! reg_set_p (dst, PATTERN (dead_insn)))
7832 /* Check if DST was used in a later insn -
7833 it might have been inherited. */
7834 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
7836 if (LABEL_P (i2))
7837 break;
7838 if (! INSN_P (i2))
7839 continue;
7840 if (reg_referenced_p (dst, PATTERN (i2)))
7842 /* If there is a reference to the register in the current insn,
7843 it might be loaded in a non-inherited reload. If no other
7844 reload uses it, that means the register is set before
7845 referenced. */
7846 if (i2 == current_insn)
7848 for (j = n_reloads - 1; j >= 0; j--)
7849 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7850 || reload_override_in[j] == dst)
7851 return;
7852 for (j = n_reloads - 1; j >= 0; j--)
7853 if (rld[j].in && rld[j].reg_rtx == dst)
7854 break;
7855 if (j >= 0)
7856 break;
7858 return;
7860 if (JUMP_P (i2))
7861 break;
7862 /* If DST is still live at CURRENT_INSN, check if it is used for
7863 any reload. Note that even if CURRENT_INSN sets DST, we still
7864 have to check the reloads. */
7865 if (i2 == current_insn)
7867 for (j = n_reloads - 1; j >= 0; j--)
7868 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7869 || reload_override_in[j] == dst)
7870 return;
7871 /* ??? We can't finish the loop here, because dst might be
7872 allocated to a pseudo in this block if no reload in this
7873 block needs any of the classes containing DST - see
7874 spill_hard_reg. There is no easy way to tell this, so we
7875 have to scan till the end of the basic block. */
7877 if (reg_set_p (dst, PATTERN (i2)))
7878 break;
7881 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
7882 reg_reloaded_contents[REGNO (dst)] = -1;
7883 delete_insn (prev);
7886 /* Output reload-insns to reload VALUE into RELOADREG.
7887 VALUE is an autoincrement or autodecrement RTX whose operand
7888 is a register or memory location;
7889 so reloading involves incrementing that location.
7890 IN is either identical to VALUE, or some cheaper place to reload from.
7892 INC_AMOUNT is the number to increment or decrement by (always positive).
7893 This cannot be deduced from VALUE.
7895 Return the instruction that stores into RELOADREG. */
7897 static rtx
7898 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
7900 /* REG or MEM to be copied and incremented. */
7901 rtx incloc = XEXP (value, 0);
7902 /* Nonzero if increment after copying. */
7903 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7904 rtx last;
7905 rtx inc;
7906 rtx add_insn;
7907 int code;
7908 rtx store;
7909 rtx real_in = in == value ? XEXP (in, 0) : in;
7911 /* No hard register is equivalent to this register after
7912 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
7913 we could inc/dec that register as well (maybe even using it for
7914 the source), but I'm not sure it's worth worrying about. */
7915 if (REG_P (incloc))
7916 reg_last_reload_reg[REGNO (incloc)] = 0;
7918 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7919 inc_amount = -inc_amount;
7921 inc = GEN_INT (inc_amount);
7923 /* If this is post-increment, first copy the location to the reload reg. */
7924 if (post && real_in != reloadreg)
7925 emit_insn (gen_move_insn (reloadreg, real_in));
7927 if (in == value)
7929 /* See if we can directly increment INCLOC. Use a method similar to
7930 that in gen_reload. */
7932 last = get_last_insn ();
7933 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7934 gen_rtx_PLUS (GET_MODE (incloc),
7935 incloc, inc)));
7937 code = recog_memoized (add_insn);
7938 if (code >= 0)
7940 extract_insn (add_insn);
7941 if (constrain_operands (1))
7943 /* If this is a pre-increment and we have incremented the value
7944 where it lives, copy the incremented value to RELOADREG to
7945 be used as an address. */
7947 if (! post)
7948 emit_insn (gen_move_insn (reloadreg, incloc));
7950 return add_insn;
7953 delete_insns_since (last);
7956 /* If couldn't do the increment directly, must increment in RELOADREG.
7957 The way we do this depends on whether this is pre- or post-increment.
7958 For pre-increment, copy INCLOC to the reload register, increment it
7959 there, then save back. */
7961 if (! post)
7963 if (in != reloadreg)
7964 emit_insn (gen_move_insn (reloadreg, real_in));
7965 emit_insn (gen_add2_insn (reloadreg, inc));
7966 store = emit_insn (gen_move_insn (incloc, reloadreg));
7968 else
7970 /* Postincrement.
7971 Because this might be a jump insn or a compare, and because RELOADREG
7972 may not be available after the insn in an input reload, we must do
7973 the incrementation before the insn being reloaded for.
7975 We have already copied IN to RELOADREG. Increment the copy in
7976 RELOADREG, save that back, then decrement RELOADREG so it has
7977 the original value. */
7979 emit_insn (gen_add2_insn (reloadreg, inc));
7980 store = emit_insn (gen_move_insn (incloc, reloadreg));
7981 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
7984 return store;
7987 #ifdef AUTO_INC_DEC
7988 static void
7989 add_auto_inc_notes (rtx insn, rtx x)
7991 enum rtx_code code = GET_CODE (x);
7992 const char *fmt;
7993 int i, j;
7995 if (code == MEM && auto_inc_p (XEXP (x, 0)))
7997 REG_NOTES (insn)
7998 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
7999 return;
8002 /* Scan all the operand sub-expressions. */
8003 fmt = GET_RTX_FORMAT (code);
8004 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8006 if (fmt[i] == 'e')
8007 add_auto_inc_notes (insn, XEXP (x, i));
8008 else if (fmt[i] == 'E')
8009 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8010 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8013 #endif
8015 /* Copy EH notes from an insn to its reloads. */
8016 static void
8017 copy_eh_notes (rtx insn, rtx x)
8019 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8020 if (eh_note)
8022 for (; x != 0; x = NEXT_INSN (x))
8024 if (may_trap_p (PATTERN (x)))
8025 REG_NOTES (x)
8026 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8027 REG_NOTES (x));
8032 /* This is used by reload pass, that does emit some instructions after
8033 abnormal calls moving basic block end, but in fact it wants to emit
8034 them on the edge. Looks for abnormal call edges, find backward the
8035 proper call and fix the damage.
8037 Similar handle instructions throwing exceptions internally. */
8038 void
8039 fixup_abnormal_edges (void)
8041 bool inserted = false;
8042 basic_block bb;
8044 FOR_EACH_BB (bb)
8046 edge e;
8048 /* Look for cases we are interested in - calls or instructions causing
8049 exceptions. */
8050 for (e = bb->succ; e; e = e->succ_next)
8052 if (e->flags & EDGE_ABNORMAL_CALL)
8053 break;
8054 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8055 == (EDGE_ABNORMAL | EDGE_EH))
8056 break;
8058 if (e && !CALL_P (BB_END (bb))
8059 && !can_throw_internal (BB_END (bb)))
8061 rtx insn = BB_END (bb), stop = NEXT_INSN (BB_END (bb));
8062 rtx next;
8063 for (e = bb->succ; e; e = e->succ_next)
8064 if (e->flags & EDGE_FALLTHRU)
8065 break;
8066 /* Get past the new insns generated. Allow notes, as the insns may
8067 be already deleted. */
8068 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8069 && !can_throw_internal (insn)
8070 && insn != BB_HEAD (bb))
8071 insn = PREV_INSN (insn);
8072 if (!CALL_P (insn) && !can_throw_internal (insn))
8073 abort ();
8074 BB_END (bb) = insn;
8075 inserted = true;
8076 insn = NEXT_INSN (insn);
8077 while (insn && insn != stop)
8079 next = NEXT_INSN (insn);
8080 if (INSN_P (insn))
8082 delete_insn (insn);
8084 /* Sometimes there's still the return value USE.
8085 If it's placed after a trapping call (i.e. that
8086 call is the last insn anyway), we have no fallthru
8087 edge. Simply delete this use and don't try to insert
8088 on the non-existent edge. */
8089 if (GET_CODE (PATTERN (insn)) != USE)
8091 /* We're not deleting it, we're moving it. */
8092 INSN_DELETED_P (insn) = 0;
8093 PREV_INSN (insn) = NULL_RTX;
8094 NEXT_INSN (insn) = NULL_RTX;
8096 insert_insn_on_edge (insn, e);
8099 insn = next;
8103 /* We've possibly turned single trapping insn into multiple ones. */
8104 if (flag_non_call_exceptions)
8106 sbitmap blocks;
8107 blocks = sbitmap_alloc (last_basic_block);
8108 sbitmap_ones (blocks);
8109 find_many_sub_basic_blocks (blocks);
8111 if (inserted)
8112 commit_edge_insertions ();