* config.sub: Add cases for the Renesas m32c. (This patch has been
[official-gcc.git] / gcc / reload1.c
blob78484aa75361bafdbc91b0ef4bd13cf1fdb7b5d8
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 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 static struct obstack reload_obstack;
273 /* Points to the beginning of the reload_obstack. All insn_chain structures
274 are allocated first. */
275 static 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 static 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 bool 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 scan_paradoxical_subregs (rtx);
394 static void count_pseudo (int);
395 static void order_regs_for_reload (struct insn_chain *);
396 static void reload_as_needed (int);
397 static void forget_old_reloads_1 (rtx, rtx, void *);
398 static int reload_reg_class_lower (const void *, const void *);
399 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
400 enum machine_mode);
401 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
402 enum machine_mode);
403 static int reload_reg_free_p (unsigned int, int, enum reload_type);
404 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
405 rtx, rtx, int, int);
406 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
407 rtx, rtx, int, int);
408 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
409 static int allocate_reload_reg (struct insn_chain *, int, int);
410 static int conflicts_with_override (rtx);
411 static void failed_reload (rtx, int);
412 static int set_reload_reg (int, int);
413 static void choose_reload_regs_init (struct insn_chain *, rtx *);
414 static void choose_reload_regs (struct insn_chain *);
415 static void merge_assigned_reloads (rtx);
416 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
417 rtx, int);
418 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
419 int);
420 static void do_input_reload (struct insn_chain *, struct reload *, int);
421 static void do_output_reload (struct insn_chain *, struct reload *, int);
422 static bool inherit_piecemeal_p (int, int);
423 static void emit_reload_insns (struct insn_chain *);
424 static void delete_output_reload (rtx, int, int);
425 static void delete_address_reloads (rtx, rtx);
426 static void delete_address_reloads_1 (rtx, rtx, rtx);
427 static rtx inc_for_reload (rtx, rtx, rtx, int);
428 #ifdef AUTO_INC_DEC
429 static void add_auto_inc_notes (rtx, rtx);
430 #endif
431 static void copy_eh_notes (rtx, rtx);
432 static int reloads_conflict (int, int);
433 static rtx gen_reload (rtx, rtx, int, enum reload_type);
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;
526 reg_set_iterator rsi;
528 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
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 gcc_assert (reload_completed);
541 else
543 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
544 while (nregs-- > 0)
545 SET_HARD_REG_BIT (*to, r + nregs);
550 /* Replace all pseudos found in LOC with their corresponding
551 equivalences. */
553 static void
554 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
556 rtx x = *loc;
557 enum rtx_code code;
558 const char *fmt;
559 int i, j;
561 if (! x)
562 return;
564 code = GET_CODE (x);
565 if (code == REG)
567 unsigned int regno = REGNO (x);
569 if (regno < FIRST_PSEUDO_REGISTER)
570 return;
572 x = eliminate_regs (x, mem_mode, usage);
573 if (x != *loc)
575 *loc = x;
576 replace_pseudos_in (loc, mem_mode, usage);
577 return;
580 if (reg_equiv_constant[regno])
581 *loc = reg_equiv_constant[regno];
582 else if (reg_equiv_mem[regno])
583 *loc = reg_equiv_mem[regno];
584 else if (reg_equiv_address[regno])
585 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
586 else
588 gcc_assert (!REG_P (regno_reg_rtx[regno])
589 || REGNO (regno_reg_rtx[regno]) != regno);
590 *loc = regno_reg_rtx[regno];
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 static 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 /* Find all the pseudo registers that didn't get hard regs
685 but do have known equivalent constants or memory slots.
686 These include parameters (known equivalent to parameter slots)
687 and cse'd or loop-moved constant memory addresses.
689 Record constant equivalents in reg_equiv_constant
690 so they will be substituted by find_reloads.
691 Record memory equivalents in reg_mem_equiv so they can
692 be substituted eventually by altering the REG-rtx's. */
694 reg_equiv_constant = xcalloc (max_regno, sizeof (rtx));
695 reg_equiv_mem = xcalloc (max_regno, sizeof (rtx));
696 reg_equiv_init = xcalloc (max_regno, sizeof (rtx));
697 reg_equiv_address = xcalloc (max_regno, sizeof (rtx));
698 reg_max_ref_width = xcalloc (max_regno, sizeof (int));
699 reg_old_renumber = xcalloc (max_regno, sizeof (short));
700 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
701 pseudo_forbidden_regs = xmalloc (max_regno * sizeof (HARD_REG_SET));
702 pseudo_previous_regs = xcalloc (max_regno, sizeof (HARD_REG_SET));
704 CLEAR_HARD_REG_SET (bad_spill_regs_global);
706 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
707 to. Also find all paradoxical subregs and find largest such for
708 each pseudo. */
710 num_eliminable_invariants = 0;
711 for (insn = first; insn; insn = NEXT_INSN (insn))
713 rtx set = single_set (insn);
715 /* We may introduce USEs that we want to remove at the end, so
716 we'll mark them with QImode. Make sure there are no
717 previously-marked insns left by say regmove. */
718 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
719 && GET_MODE (insn) != VOIDmode)
720 PUT_MODE (insn, VOIDmode);
722 if (set != 0 && REG_P (SET_DEST (set)))
724 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
725 if (note
726 && (! function_invariant_p (XEXP (note, 0))
727 || ! flag_pic
728 /* A function invariant is often CONSTANT_P but may
729 include a register. We promise to only pass
730 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
731 || (CONSTANT_P (XEXP (note, 0))
732 && LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))))
734 rtx x = XEXP (note, 0);
735 i = REGNO (SET_DEST (set));
736 if (i > LAST_VIRTUAL_REGISTER)
738 /* It can happen that a REG_EQUIV note contains a MEM
739 that is not a legitimate memory operand. As later
740 stages of reload assume that all addresses found
741 in the reg_equiv_* arrays were originally legitimate,
743 It can also happen that a REG_EQUIV note contains a
744 readonly memory location. If the destination pseudo
745 is set from some other value (typically a different
746 pseudo), and the destination pseudo does not get a
747 hard reg, then reload will replace the destination
748 pseudo with its equivalent memory location. This
749 is horribly bad as it creates a store to a readonly
750 memory location and a runtime segfault. To avoid
751 this problem we reject readonly memory locations
752 for equivalences. This is overly conservative as
753 we could find all sets of the destination pseudo
754 and remove them as they should be redundant. */
755 if (memory_operand (x, VOIDmode))
757 /* Always unshare the equivalence, so we can
758 substitute into this insn without touching the
759 equivalence. */
760 reg_equiv_memory_loc[i] = copy_rtx (x);
762 else if (function_invariant_p (x))
764 if (GET_CODE (x) == PLUS)
766 /* This is PLUS of frame pointer and a constant,
767 and might be shared. Unshare it. */
768 reg_equiv_constant[i] = copy_rtx (x);
769 num_eliminable_invariants++;
771 else if (x == frame_pointer_rtx
772 || x == arg_pointer_rtx)
774 reg_equiv_constant[i] = x;
775 num_eliminable_invariants++;
777 else if (LEGITIMATE_CONSTANT_P (x))
778 reg_equiv_constant[i] = x;
779 else
781 reg_equiv_memory_loc[i]
782 = force_const_mem (GET_MODE (SET_DEST (set)), x);
783 if (!reg_equiv_memory_loc[i])
784 continue;
787 else
788 continue;
790 reg_equiv_init[i]
791 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[i]);
796 /* If this insn is setting a MEM from a register equivalent to it,
797 this is the equivalencing insn. */
798 else if (set && MEM_P (SET_DEST (set))
799 && REG_P (SET_SRC (set))
800 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
801 && rtx_equal_p (SET_DEST (set),
802 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
803 /* Equivalences made this way only have one initializing insn.
804 Previously, we may have set reg_equiv_init when encountering a
805 SET of this pseudo; discard that insn since it does not set up
806 an equivalence. */
807 reg_equiv_init[REGNO (SET_SRC (set))]
808 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
810 if (INSN_P (insn))
811 scan_paradoxical_subregs (PATTERN (insn));
814 init_elim_table ();
816 first_label_num = get_first_label_num ();
817 num_labels = max_label_num () - first_label_num;
819 /* Allocate the tables used to store offset information at labels. */
820 /* We used to use alloca here, but the size of what it would try to
821 allocate would occasionally cause it to exceed the stack limit and
822 cause a core dump. */
823 offsets_known_at = xmalloc (num_labels);
824 offsets_at = xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
826 /* Alter each pseudo-reg rtx to contain its hard reg number.
827 Assign stack slots to the pseudos that lack hard regs or equivalents.
828 Do not touch virtual registers. */
830 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
831 alter_reg (i, -1);
833 /* If we have some registers we think can be eliminated, scan all insns to
834 see if there is an insn that sets one of these registers to something
835 other than itself plus a constant. If so, the register cannot be
836 eliminated. Doing this scan here eliminates an extra pass through the
837 main reload loop in the most common case where register elimination
838 cannot be done. */
839 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
840 if (INSN_P (insn))
841 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
843 maybe_fix_stack_asms ();
845 insns_need_reload = 0;
846 something_needs_elimination = 0;
848 /* Initialize to -1, which means take the first spill register. */
849 last_spill_reg = -1;
851 /* Spill any hard regs that we know we can't eliminate. */
852 CLEAR_HARD_REG_SET (used_spill_regs);
853 /* There can be multiple ways to eliminate a register;
854 they should be listed adjacently.
855 Elimination for any register fails only if all possible ways fail. */
856 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
858 int from = ep->from;
859 int can_eliminate = 0;
862 can_eliminate |= ep->can_eliminate;
863 ep++;
865 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
866 if (! can_eliminate)
867 spill_hard_reg (from, 1);
870 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
871 if (frame_pointer_needed)
872 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
873 #endif
874 finish_spills (global);
876 /* From now on, we may need to generate moves differently. We may also
877 allow modifications of insns which cause them to not be recognized.
878 Any such modifications will be cleaned up during reload itself. */
879 reload_in_progress = 1;
881 /* This loop scans the entire function each go-round
882 and repeats until one repetition spills no additional hard regs. */
883 for (;;)
885 int something_changed;
886 int did_spill;
888 HOST_WIDE_INT starting_frame_size;
890 /* Round size of stack frame to stack_alignment_needed. This must be done
891 here because the stack size may be a part of the offset computation
892 for register elimination, and there might have been new stack slots
893 created in the last iteration of this loop. */
894 if (cfun->stack_alignment_needed)
895 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
897 starting_frame_size = get_frame_size ();
899 set_initial_elim_offsets ();
900 set_initial_label_offsets ();
902 /* For each pseudo register that has an equivalent location defined,
903 try to eliminate any eliminable registers (such as the frame pointer)
904 assuming initial offsets for the replacement register, which
905 is the normal case.
907 If the resulting location is directly addressable, substitute
908 the MEM we just got directly for the old REG.
910 If it is not addressable but is a constant or the sum of a hard reg
911 and constant, it is probably not addressable because the constant is
912 out of range, in that case record the address; we will generate
913 hairy code to compute the address in a register each time it is
914 needed. Similarly if it is a hard register, but one that is not
915 valid as an address register.
917 If the location is not addressable, but does not have one of the
918 above forms, assign a stack slot. We have to do this to avoid the
919 potential of producing lots of reloads if, e.g., a location involves
920 a pseudo that didn't get a hard register and has an equivalent memory
921 location that also involves a pseudo that didn't get a hard register.
923 Perhaps at some point we will improve reload_when_needed handling
924 so this problem goes away. But that's very hairy. */
926 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
927 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
929 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
931 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
932 XEXP (x, 0)))
933 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
934 else if (CONSTANT_P (XEXP (x, 0))
935 || (REG_P (XEXP (x, 0))
936 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
937 || (GET_CODE (XEXP (x, 0)) == PLUS
938 && REG_P (XEXP (XEXP (x, 0), 0))
939 && (REGNO (XEXP (XEXP (x, 0), 0))
940 < FIRST_PSEUDO_REGISTER)
941 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
942 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
943 else
945 /* Make a new stack slot. Then indicate that something
946 changed so we go back and recompute offsets for
947 eliminable registers because the allocation of memory
948 below might change some offset. reg_equiv_{mem,address}
949 will be set up for this pseudo on the next pass around
950 the loop. */
951 reg_equiv_memory_loc[i] = 0;
952 reg_equiv_init[i] = 0;
953 alter_reg (i, -1);
957 if (caller_save_needed)
958 setup_save_areas ();
960 /* If we allocated another stack slot, redo elimination bookkeeping. */
961 if (starting_frame_size != get_frame_size ())
962 continue;
964 if (caller_save_needed)
966 save_call_clobbered_regs ();
967 /* That might have allocated new insn_chain structures. */
968 reload_firstobj = obstack_alloc (&reload_obstack, 0);
971 calculate_needs_all_insns (global);
973 CLEAR_REG_SET (&spilled_pseudos);
974 did_spill = 0;
976 something_changed = 0;
978 /* If we allocated any new memory locations, make another pass
979 since it might have changed elimination offsets. */
980 if (starting_frame_size != get_frame_size ())
981 something_changed = 1;
983 /* Even if the frame size remained the same, we might still have
984 changed elimination offsets, e.g. if find_reloads called
985 force_const_mem requiring the back end to allocate a constant
986 pool base register that needs to be saved on the stack. */
987 else if (!verify_initial_elim_offsets ())
988 something_changed = 1;
991 HARD_REG_SET to_spill;
992 CLEAR_HARD_REG_SET (to_spill);
993 update_eliminables (&to_spill);
994 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
995 if (TEST_HARD_REG_BIT (to_spill, i))
997 spill_hard_reg (i, 1);
998 did_spill = 1;
1000 /* Regardless of the state of spills, if we previously had
1001 a register that we thought we could eliminate, but now can
1002 not eliminate, we must run another pass.
1004 Consider pseudos which have an entry in reg_equiv_* which
1005 reference an eliminable register. We must make another pass
1006 to update reg_equiv_* so that we do not substitute in the
1007 old value from when we thought the elimination could be
1008 performed. */
1009 something_changed = 1;
1013 select_reload_regs ();
1014 if (failure)
1015 goto failed;
1017 if (insns_need_reload != 0 || did_spill)
1018 something_changed |= finish_spills (global);
1020 if (! something_changed)
1021 break;
1023 if (caller_save_needed)
1024 delete_caller_save_insns ();
1026 obstack_free (&reload_obstack, reload_firstobj);
1029 /* If global-alloc was run, notify it of any register eliminations we have
1030 done. */
1031 if (global)
1032 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1033 if (ep->can_eliminate)
1034 mark_elimination (ep->from, ep->to);
1036 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1037 If that insn didn't set the register (i.e., it copied the register to
1038 memory), just delete that insn instead of the equivalencing insn plus
1039 anything now dead. If we call delete_dead_insn on that insn, we may
1040 delete the insn that actually sets the register if the register dies
1041 there and that is incorrect. */
1043 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1045 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1047 rtx list;
1048 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1050 rtx equiv_insn = XEXP (list, 0);
1052 /* If we already deleted the insn or if it may trap, we can't
1053 delete it. The latter case shouldn't happen, but can
1054 if an insn has a variable address, gets a REG_EH_REGION
1055 note added to it, and then gets converted into an load
1056 from a constant address. */
1057 if (NOTE_P (equiv_insn)
1058 || can_throw_internal (equiv_insn))
1060 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1061 delete_dead_insn (equiv_insn);
1062 else
1063 SET_INSN_DELETED (equiv_insn);
1068 /* Use the reload registers where necessary
1069 by generating move instructions to move the must-be-register
1070 values into or out of the reload registers. */
1072 if (insns_need_reload != 0 || something_needs_elimination
1073 || something_needs_operands_changed)
1075 HOST_WIDE_INT old_frame_size = get_frame_size ();
1077 reload_as_needed (global);
1079 gcc_assert (old_frame_size == get_frame_size ());
1081 gcc_assert (verify_initial_elim_offsets ());
1084 /* If we were able to eliminate the frame pointer, show that it is no
1085 longer live at the start of any basic block. If it ls live by
1086 virtue of being in a pseudo, that pseudo will be marked live
1087 and hence the frame pointer will be known to be live via that
1088 pseudo. */
1090 if (! frame_pointer_needed)
1091 FOR_EACH_BB (bb)
1092 CLEAR_REGNO_REG_SET (bb->global_live_at_start,
1093 HARD_FRAME_POINTER_REGNUM);
1095 /* Come here (with failure set nonzero) if we can't get enough spill
1096 regs. */
1097 failed:
1099 CLEAR_REG_SET (&spilled_pseudos);
1100 reload_in_progress = 0;
1102 /* Now eliminate all pseudo regs by modifying them into
1103 their equivalent memory references.
1104 The REG-rtx's for the pseudos are modified in place,
1105 so all insns that used to refer to them now refer to memory.
1107 For a reg that has a reg_equiv_address, all those insns
1108 were changed by reloading so that no insns refer to it any longer;
1109 but the DECL_RTL of a variable decl may refer to it,
1110 and if so this causes the debugging info to mention the variable. */
1112 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1114 rtx addr = 0;
1116 if (reg_equiv_mem[i])
1117 addr = XEXP (reg_equiv_mem[i], 0);
1119 if (reg_equiv_address[i])
1120 addr = reg_equiv_address[i];
1122 if (addr)
1124 if (reg_renumber[i] < 0)
1126 rtx reg = regno_reg_rtx[i];
1128 REG_USERVAR_P (reg) = 0;
1129 PUT_CODE (reg, MEM);
1130 XEXP (reg, 0) = addr;
1131 if (reg_equiv_memory_loc[i])
1132 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1133 else
1135 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1136 MEM_ATTRS (reg) = 0;
1139 else if (reg_equiv_mem[i])
1140 XEXP (reg_equiv_mem[i], 0) = addr;
1144 /* We must set reload_completed now since the cleanup_subreg_operands call
1145 below will re-recognize each insn and reload may have generated insns
1146 which are only valid during and after reload. */
1147 reload_completed = 1;
1149 /* Make a pass over all the insns and delete all USEs which we inserted
1150 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1151 notes. Delete all CLOBBER insns, except those that refer to the return
1152 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1153 from misarranging variable-array code, and simplify (subreg (reg))
1154 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1155 are no longer useful or accurate. Strip and regenerate REG_INC notes
1156 that may have been moved around. */
1158 for (insn = first; insn; insn = NEXT_INSN (insn))
1159 if (INSN_P (insn))
1161 rtx *pnote;
1163 if (CALL_P (insn))
1164 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1165 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1167 if ((GET_CODE (PATTERN (insn)) == USE
1168 /* We mark with QImode USEs introduced by reload itself. */
1169 && (GET_MODE (insn) == QImode
1170 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1171 || (GET_CODE (PATTERN (insn)) == CLOBBER
1172 && (!MEM_P (XEXP (PATTERN (insn), 0))
1173 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1174 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1175 && XEXP (XEXP (PATTERN (insn), 0), 0)
1176 != stack_pointer_rtx))
1177 && (!REG_P (XEXP (PATTERN (insn), 0))
1178 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1180 delete_insn (insn);
1181 continue;
1184 /* Some CLOBBERs may survive until here and still reference unassigned
1185 pseudos with const equivalent, which may in turn cause ICE in later
1186 passes if the reference remains in place. */
1187 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1188 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1189 VOIDmode, PATTERN (insn));
1191 /* Discard obvious no-ops, even without -O. This optimization
1192 is fast and doesn't interfere with debugging. */
1193 if (NONJUMP_INSN_P (insn)
1194 && GET_CODE (PATTERN (insn)) == SET
1195 && REG_P (SET_SRC (PATTERN (insn)))
1196 && REG_P (SET_DEST (PATTERN (insn)))
1197 && (REGNO (SET_SRC (PATTERN (insn)))
1198 == REGNO (SET_DEST (PATTERN (insn)))))
1200 delete_insn (insn);
1201 continue;
1204 pnote = &REG_NOTES (insn);
1205 while (*pnote != 0)
1207 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1208 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1209 || REG_NOTE_KIND (*pnote) == REG_INC
1210 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1211 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1212 *pnote = XEXP (*pnote, 1);
1213 else
1214 pnote = &XEXP (*pnote, 1);
1217 #ifdef AUTO_INC_DEC
1218 add_auto_inc_notes (insn, PATTERN (insn));
1219 #endif
1221 /* And simplify (subreg (reg)) if it appears as an operand. */
1222 cleanup_subreg_operands (insn);
1225 /* If we are doing stack checking, give a warning if this function's
1226 frame size is larger than we expect. */
1227 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1229 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1230 static int verbose_warned = 0;
1232 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1233 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1234 size += UNITS_PER_WORD;
1236 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1238 warning (0, "frame size too large for reliable stack checking");
1239 if (! verbose_warned)
1241 warning (0, "try reducing the number of local variables");
1242 verbose_warned = 1;
1247 /* Indicate that we no longer have known memory locations or constants. */
1248 if (reg_equiv_constant)
1249 free (reg_equiv_constant);
1250 reg_equiv_constant = 0;
1251 VARRAY_GROW (reg_equiv_memory_loc_varray, 0);
1252 reg_equiv_memory_loc = 0;
1254 if (offsets_known_at)
1255 free (offsets_known_at);
1256 if (offsets_at)
1257 free (offsets_at);
1259 free (reg_equiv_mem);
1260 free (reg_equiv_init);
1261 free (reg_equiv_address);
1262 free (reg_max_ref_width);
1263 free (reg_old_renumber);
1264 free (pseudo_previous_regs);
1265 free (pseudo_forbidden_regs);
1267 CLEAR_HARD_REG_SET (used_spill_regs);
1268 for (i = 0; i < n_spills; i++)
1269 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1271 /* Free all the insn_chain structures at once. */
1272 obstack_free (&reload_obstack, reload_startobj);
1273 unused_insn_chains = 0;
1274 fixup_abnormal_edges ();
1276 /* Replacing pseudos with their memory equivalents might have
1277 created shared rtx. Subsequent passes would get confused
1278 by this, so unshare everything here. */
1279 unshare_all_rtl_again (first);
1281 #ifdef STACK_BOUNDARY
1282 /* init_emit has set the alignment of the hard frame pointer
1283 to STACK_BOUNDARY. It is very likely no longer valid if
1284 the hard frame pointer was used for register allocation. */
1285 if (!frame_pointer_needed)
1286 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1287 #endif
1289 return failure;
1292 /* Yet another special case. Unfortunately, reg-stack forces people to
1293 write incorrect clobbers in asm statements. These clobbers must not
1294 cause the register to appear in bad_spill_regs, otherwise we'll call
1295 fatal_insn later. We clear the corresponding regnos in the live
1296 register sets to avoid this.
1297 The whole thing is rather sick, I'm afraid. */
1299 static void
1300 maybe_fix_stack_asms (void)
1302 #ifdef STACK_REGS
1303 const char *constraints[MAX_RECOG_OPERANDS];
1304 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1305 struct insn_chain *chain;
1307 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1309 int i, noperands;
1310 HARD_REG_SET clobbered, allowed;
1311 rtx pat;
1313 if (! INSN_P (chain->insn)
1314 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1315 continue;
1316 pat = PATTERN (chain->insn);
1317 if (GET_CODE (pat) != PARALLEL)
1318 continue;
1320 CLEAR_HARD_REG_SET (clobbered);
1321 CLEAR_HARD_REG_SET (allowed);
1323 /* First, make a mask of all stack regs that are clobbered. */
1324 for (i = 0; i < XVECLEN (pat, 0); i++)
1326 rtx t = XVECEXP (pat, 0, i);
1327 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1328 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1331 /* Get the operand values and constraints out of the insn. */
1332 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1333 constraints, operand_mode);
1335 /* For every operand, see what registers are allowed. */
1336 for (i = 0; i < noperands; i++)
1338 const char *p = constraints[i];
1339 /* For every alternative, we compute the class of registers allowed
1340 for reloading in CLS, and merge its contents into the reg set
1341 ALLOWED. */
1342 int cls = (int) NO_REGS;
1344 for (;;)
1346 char c = *p;
1348 if (c == '\0' || c == ',' || c == '#')
1350 /* End of one alternative - mark the regs in the current
1351 class, and reset the class. */
1352 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1353 cls = NO_REGS;
1354 p++;
1355 if (c == '#')
1356 do {
1357 c = *p++;
1358 } while (c != '\0' && c != ',');
1359 if (c == '\0')
1360 break;
1361 continue;
1364 switch (c)
1366 case '=': case '+': case '*': case '%': case '?': case '!':
1367 case '0': case '1': case '2': case '3': case '4': case 'm':
1368 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1369 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1370 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1371 case 'P':
1372 break;
1374 case 'p':
1375 cls = (int) reg_class_subunion[cls]
1376 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1377 break;
1379 case 'g':
1380 case 'r':
1381 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1382 break;
1384 default:
1385 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1386 cls = (int) reg_class_subunion[cls]
1387 [(int) MODE_BASE_REG_CLASS (VOIDmode)];
1388 else
1389 cls = (int) reg_class_subunion[cls]
1390 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1392 p += CONSTRAINT_LEN (c, p);
1395 /* Those of the registers which are clobbered, but allowed by the
1396 constraints, must be usable as reload registers. So clear them
1397 out of the life information. */
1398 AND_HARD_REG_SET (allowed, clobbered);
1399 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1400 if (TEST_HARD_REG_BIT (allowed, i))
1402 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1403 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1407 #endif
1410 /* Copy the global variables n_reloads and rld into the corresponding elts
1411 of CHAIN. */
1412 static void
1413 copy_reloads (struct insn_chain *chain)
1415 chain->n_reloads = n_reloads;
1416 chain->rld = obstack_alloc (&reload_obstack,
1417 n_reloads * sizeof (struct reload));
1418 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1419 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1422 /* Walk the chain of insns, and determine for each whether it needs reloads
1423 and/or eliminations. Build the corresponding insns_need_reload list, and
1424 set something_needs_elimination as appropriate. */
1425 static void
1426 calculate_needs_all_insns (int global)
1428 struct insn_chain **pprev_reload = &insns_need_reload;
1429 struct insn_chain *chain, *next = 0;
1431 something_needs_elimination = 0;
1433 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1434 for (chain = reload_insn_chain; chain != 0; chain = next)
1436 rtx insn = chain->insn;
1438 next = chain->next;
1440 /* Clear out the shortcuts. */
1441 chain->n_reloads = 0;
1442 chain->need_elim = 0;
1443 chain->need_reload = 0;
1444 chain->need_operand_change = 0;
1446 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1447 include REG_LABEL), we need to see what effects this has on the
1448 known offsets at labels. */
1450 if (LABEL_P (insn) || JUMP_P (insn)
1451 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1452 set_label_offsets (insn, insn, 0);
1454 if (INSN_P (insn))
1456 rtx old_body = PATTERN (insn);
1457 int old_code = INSN_CODE (insn);
1458 rtx old_notes = REG_NOTES (insn);
1459 int did_elimination = 0;
1460 int operands_changed = 0;
1461 rtx set = single_set (insn);
1463 /* Skip insns that only set an equivalence. */
1464 if (set && REG_P (SET_DEST (set))
1465 && reg_renumber[REGNO (SET_DEST (set))] < 0
1466 && reg_equiv_constant[REGNO (SET_DEST (set))])
1467 continue;
1469 /* If needed, eliminate any eliminable registers. */
1470 if (num_eliminable || num_eliminable_invariants)
1471 did_elimination = eliminate_regs_in_insn (insn, 0);
1473 /* Analyze the instruction. */
1474 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1475 global, spill_reg_order);
1477 /* If a no-op set needs more than one reload, this is likely
1478 to be something that needs input address reloads. We
1479 can't get rid of this cleanly later, and it is of no use
1480 anyway, so discard it now.
1481 We only do this when expensive_optimizations is enabled,
1482 since this complements reload inheritance / output
1483 reload deletion, and it can make debugging harder. */
1484 if (flag_expensive_optimizations && n_reloads > 1)
1486 rtx set = single_set (insn);
1487 if (set
1488 && SET_SRC (set) == SET_DEST (set)
1489 && REG_P (SET_SRC (set))
1490 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1492 delete_insn (insn);
1493 /* Delete it from the reload chain. */
1494 if (chain->prev)
1495 chain->prev->next = next;
1496 else
1497 reload_insn_chain = next;
1498 if (next)
1499 next->prev = chain->prev;
1500 chain->next = unused_insn_chains;
1501 unused_insn_chains = chain;
1502 continue;
1505 if (num_eliminable)
1506 update_eliminable_offsets ();
1508 /* Remember for later shortcuts which insns had any reloads or
1509 register eliminations. */
1510 chain->need_elim = did_elimination;
1511 chain->need_reload = n_reloads > 0;
1512 chain->need_operand_change = operands_changed;
1514 /* Discard any register replacements done. */
1515 if (did_elimination)
1517 obstack_free (&reload_obstack, reload_insn_firstobj);
1518 PATTERN (insn) = old_body;
1519 INSN_CODE (insn) = old_code;
1520 REG_NOTES (insn) = old_notes;
1521 something_needs_elimination = 1;
1524 something_needs_operands_changed |= operands_changed;
1526 if (n_reloads != 0)
1528 copy_reloads (chain);
1529 *pprev_reload = chain;
1530 pprev_reload = &chain->next_need_reload;
1534 *pprev_reload = 0;
1537 /* Comparison function for qsort to decide which of two reloads
1538 should be handled first. *P1 and *P2 are the reload numbers. */
1540 static int
1541 reload_reg_class_lower (const void *r1p, const void *r2p)
1543 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1544 int t;
1546 /* Consider required reloads before optional ones. */
1547 t = rld[r1].optional - rld[r2].optional;
1548 if (t != 0)
1549 return t;
1551 /* Count all solitary classes before non-solitary ones. */
1552 t = ((reg_class_size[(int) rld[r2].class] == 1)
1553 - (reg_class_size[(int) rld[r1].class] == 1));
1554 if (t != 0)
1555 return t;
1557 /* Aside from solitaires, consider all multi-reg groups first. */
1558 t = rld[r2].nregs - rld[r1].nregs;
1559 if (t != 0)
1560 return t;
1562 /* Consider reloads in order of increasing reg-class number. */
1563 t = (int) rld[r1].class - (int) rld[r2].class;
1564 if (t != 0)
1565 return t;
1567 /* If reloads are equally urgent, sort by reload number,
1568 so that the results of qsort leave nothing to chance. */
1569 return r1 - r2;
1572 /* The cost of spilling each hard reg. */
1573 static int spill_cost[FIRST_PSEUDO_REGISTER];
1575 /* When spilling multiple hard registers, we use SPILL_COST for the first
1576 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1577 only the first hard reg for a multi-reg pseudo. */
1578 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1580 /* Update the spill cost arrays, considering that pseudo REG is live. */
1582 static void
1583 count_pseudo (int reg)
1585 int freq = REG_FREQ (reg);
1586 int r = reg_renumber[reg];
1587 int nregs;
1589 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1590 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1591 return;
1593 SET_REGNO_REG_SET (&pseudos_counted, reg);
1595 gcc_assert (r >= 0);
1597 spill_add_cost[r] += freq;
1599 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1600 while (nregs-- > 0)
1601 spill_cost[r + nregs] += freq;
1604 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1605 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1607 static void
1608 order_regs_for_reload (struct insn_chain *chain)
1610 unsigned i;
1611 HARD_REG_SET used_by_pseudos;
1612 HARD_REG_SET used_by_pseudos2;
1613 reg_set_iterator rsi;
1615 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1617 memset (spill_cost, 0, sizeof spill_cost);
1618 memset (spill_add_cost, 0, sizeof spill_add_cost);
1620 /* Count number of uses of each hard reg by pseudo regs allocated to it
1621 and then order them by decreasing use. First exclude hard registers
1622 that are live in or across this insn. */
1624 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1625 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1626 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1627 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1629 /* Now find out which pseudos are allocated to it, and update
1630 hard_reg_n_uses. */
1631 CLEAR_REG_SET (&pseudos_counted);
1633 EXECUTE_IF_SET_IN_REG_SET
1634 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1636 count_pseudo (i);
1638 EXECUTE_IF_SET_IN_REG_SET
1639 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1641 count_pseudo (i);
1643 CLEAR_REG_SET (&pseudos_counted);
1646 /* Vector of reload-numbers showing the order in which the reloads should
1647 be processed. */
1648 static short reload_order[MAX_RELOADS];
1650 /* This is used to keep track of the spill regs used in one insn. */
1651 static HARD_REG_SET used_spill_regs_local;
1653 /* We decided to spill hard register SPILLED, which has a size of
1654 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1655 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1656 update SPILL_COST/SPILL_ADD_COST. */
1658 static void
1659 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1661 int r = reg_renumber[reg];
1662 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1664 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1665 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1666 return;
1668 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1670 spill_add_cost[r] -= REG_FREQ (reg);
1671 while (nregs-- > 0)
1672 spill_cost[r + nregs] -= REG_FREQ (reg);
1675 /* Find reload register to use for reload number ORDER. */
1677 static int
1678 find_reg (struct insn_chain *chain, int order)
1680 int rnum = reload_order[order];
1681 struct reload *rl = rld + rnum;
1682 int best_cost = INT_MAX;
1683 int best_reg = -1;
1684 unsigned int i, j;
1685 int k;
1686 HARD_REG_SET not_usable;
1687 HARD_REG_SET used_by_other_reload;
1688 reg_set_iterator rsi;
1690 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1691 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1692 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1694 CLEAR_HARD_REG_SET (used_by_other_reload);
1695 for (k = 0; k < order; k++)
1697 int other = reload_order[k];
1699 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1700 for (j = 0; j < rld[other].nregs; j++)
1701 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1704 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1706 unsigned int regno = i;
1708 if (! TEST_HARD_REG_BIT (not_usable, regno)
1709 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1710 && HARD_REGNO_MODE_OK (regno, rl->mode))
1712 int this_cost = spill_cost[regno];
1713 int ok = 1;
1714 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1716 for (j = 1; j < this_nregs; j++)
1718 this_cost += spill_add_cost[regno + j];
1719 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1720 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1721 ok = 0;
1723 if (! ok)
1724 continue;
1725 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1726 this_cost--;
1727 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1728 this_cost--;
1729 if (this_cost < best_cost
1730 /* Among registers with equal cost, prefer caller-saved ones, or
1731 use REG_ALLOC_ORDER if it is defined. */
1732 || (this_cost == best_cost
1733 #ifdef REG_ALLOC_ORDER
1734 && (inv_reg_alloc_order[regno]
1735 < inv_reg_alloc_order[best_reg])
1736 #else
1737 && call_used_regs[regno]
1738 && ! call_used_regs[best_reg]
1739 #endif
1742 best_reg = regno;
1743 best_cost = this_cost;
1747 if (best_reg == -1)
1748 return 0;
1750 if (dump_file)
1751 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1753 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1754 rl->regno = best_reg;
1756 EXECUTE_IF_SET_IN_REG_SET
1757 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1759 count_spilled_pseudo (best_reg, rl->nregs, j);
1762 EXECUTE_IF_SET_IN_REG_SET
1763 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1765 count_spilled_pseudo (best_reg, rl->nregs, j);
1768 for (i = 0; i < rl->nregs; i++)
1770 gcc_assert (spill_cost[best_reg + i] == 0);
1771 gcc_assert (spill_add_cost[best_reg + i] == 0);
1772 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1774 return 1;
1777 /* Find more reload regs to satisfy the remaining need of an insn, which
1778 is given by CHAIN.
1779 Do it by ascending class number, since otherwise a reg
1780 might be spilled for a big class and might fail to count
1781 for a smaller class even though it belongs to that class. */
1783 static void
1784 find_reload_regs (struct insn_chain *chain)
1786 int i;
1788 /* In order to be certain of getting the registers we need,
1789 we must sort the reloads into order of increasing register class.
1790 Then our grabbing of reload registers will parallel the process
1791 that provided the reload registers. */
1792 for (i = 0; i < chain->n_reloads; i++)
1794 /* Show whether this reload already has a hard reg. */
1795 if (chain->rld[i].reg_rtx)
1797 int regno = REGNO (chain->rld[i].reg_rtx);
1798 chain->rld[i].regno = regno;
1799 chain->rld[i].nregs
1800 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1802 else
1803 chain->rld[i].regno = -1;
1804 reload_order[i] = i;
1807 n_reloads = chain->n_reloads;
1808 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1810 CLEAR_HARD_REG_SET (used_spill_regs_local);
1812 if (dump_file)
1813 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1815 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1817 /* Compute the order of preference for hard registers to spill. */
1819 order_regs_for_reload (chain);
1821 for (i = 0; i < n_reloads; i++)
1823 int r = reload_order[i];
1825 /* Ignore reloads that got marked inoperative. */
1826 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1827 && ! rld[r].optional
1828 && rld[r].regno == -1)
1829 if (! find_reg (chain, i))
1831 spill_failure (chain->insn, rld[r].class);
1832 failure = 1;
1833 return;
1837 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1838 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1840 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1843 static void
1844 select_reload_regs (void)
1846 struct insn_chain *chain;
1848 /* Try to satisfy the needs for each insn. */
1849 for (chain = insns_need_reload; chain != 0;
1850 chain = chain->next_need_reload)
1851 find_reload_regs (chain);
1854 /* Delete all insns that were inserted by emit_caller_save_insns during
1855 this iteration. */
1856 static void
1857 delete_caller_save_insns (void)
1859 struct insn_chain *c = reload_insn_chain;
1861 while (c != 0)
1863 while (c != 0 && c->is_caller_save_insn)
1865 struct insn_chain *next = c->next;
1866 rtx insn = c->insn;
1868 if (c == reload_insn_chain)
1869 reload_insn_chain = next;
1870 delete_insn (insn);
1872 if (next)
1873 next->prev = c->prev;
1874 if (c->prev)
1875 c->prev->next = next;
1876 c->next = unused_insn_chains;
1877 unused_insn_chains = c;
1878 c = next;
1880 if (c != 0)
1881 c = c->next;
1885 /* Handle the failure to find a register to spill.
1886 INSN should be one of the insns which needed this particular spill reg. */
1888 static void
1889 spill_failure (rtx insn, enum reg_class class)
1891 if (asm_noperands (PATTERN (insn)) >= 0)
1892 error_for_asm (insn, "can't find a register in class %qs while "
1893 "reloading %<asm%>",
1894 reg_class_names[class]);
1895 else
1897 error ("unable to find a register to spill in class %qs",
1898 reg_class_names[class]);
1899 fatal_insn ("this is the insn:", insn);
1903 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1904 data that is dead in INSN. */
1906 static void
1907 delete_dead_insn (rtx insn)
1909 rtx prev = prev_real_insn (insn);
1910 rtx prev_dest;
1912 /* If the previous insn sets a register that dies in our insn, delete it
1913 too. */
1914 if (prev && GET_CODE (PATTERN (prev)) == SET
1915 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
1916 && reg_mentioned_p (prev_dest, PATTERN (insn))
1917 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1918 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1919 delete_dead_insn (prev);
1921 SET_INSN_DELETED (insn);
1924 /* Modify the home of pseudo-reg I.
1925 The new home is present in reg_renumber[I].
1927 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1928 or it may be -1, meaning there is none or it is not relevant.
1929 This is used so that all pseudos spilled from a given hard reg
1930 can share one stack slot. */
1932 static void
1933 alter_reg (int i, int from_reg)
1935 /* When outputting an inline function, this can happen
1936 for a reg that isn't actually used. */
1937 if (regno_reg_rtx[i] == 0)
1938 return;
1940 /* If the reg got changed to a MEM at rtl-generation time,
1941 ignore it. */
1942 if (!REG_P (regno_reg_rtx[i]))
1943 return;
1945 /* Modify the reg-rtx to contain the new hard reg
1946 number or else to contain its pseudo reg number. */
1947 REGNO (regno_reg_rtx[i])
1948 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1950 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1951 allocate a stack slot for it. */
1953 if (reg_renumber[i] < 0
1954 && REG_N_REFS (i) > 0
1955 && reg_equiv_constant[i] == 0
1956 && reg_equiv_memory_loc[i] == 0)
1958 rtx x;
1959 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
1960 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
1961 int adjust = 0;
1963 /* Each pseudo reg has an inherent size which comes from its own mode,
1964 and a total size which provides room for paradoxical subregs
1965 which refer to the pseudo reg in wider modes.
1967 We can use a slot already allocated if it provides both
1968 enough inherent space and enough total space.
1969 Otherwise, we allocate a new slot, making sure that it has no less
1970 inherent space, and no less total space, then the previous slot. */
1971 if (from_reg == -1)
1973 /* No known place to spill from => no slot to reuse. */
1974 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size,
1975 inherent_size == total_size ? 0 : -1);
1976 if (BYTES_BIG_ENDIAN)
1977 /* Cancel the big-endian correction done in assign_stack_local.
1978 Get the address of the beginning of the slot.
1979 This is so we can do a big-endian correction unconditionally
1980 below. */
1981 adjust = inherent_size - total_size;
1983 /* Nothing can alias this slot except this pseudo. */
1984 set_mem_alias_set (x, new_alias_set ());
1987 /* Reuse a stack slot if possible. */
1988 else if (spill_stack_slot[from_reg] != 0
1989 && spill_stack_slot_width[from_reg] >= total_size
1990 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
1991 >= inherent_size))
1992 x = spill_stack_slot[from_reg];
1994 /* Allocate a bigger slot. */
1995 else
1997 /* Compute maximum size needed, both for inherent size
1998 and for total size. */
1999 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2000 rtx stack_slot;
2002 if (spill_stack_slot[from_reg])
2004 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2005 > inherent_size)
2006 mode = GET_MODE (spill_stack_slot[from_reg]);
2007 if (spill_stack_slot_width[from_reg] > total_size)
2008 total_size = spill_stack_slot_width[from_reg];
2011 /* Make a slot with that size. */
2012 x = assign_stack_local (mode, total_size,
2013 inherent_size == total_size ? 0 : -1);
2014 stack_slot = x;
2016 /* All pseudos mapped to this slot can alias each other. */
2017 if (spill_stack_slot[from_reg])
2018 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
2019 else
2020 set_mem_alias_set (x, new_alias_set ());
2022 if (BYTES_BIG_ENDIAN)
2024 /* Cancel the big-endian correction done in assign_stack_local.
2025 Get the address of the beginning of the slot.
2026 This is so we can do a big-endian correction unconditionally
2027 below. */
2028 adjust = GET_MODE_SIZE (mode) - total_size;
2029 if (adjust)
2030 stack_slot
2031 = adjust_address_nv (x, mode_for_size (total_size
2032 * BITS_PER_UNIT,
2033 MODE_INT, 1),
2034 adjust);
2037 spill_stack_slot[from_reg] = stack_slot;
2038 spill_stack_slot_width[from_reg] = total_size;
2041 /* On a big endian machine, the "address" of the slot
2042 is the address of the low part that fits its inherent mode. */
2043 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2044 adjust += (total_size - inherent_size);
2046 /* If we have any adjustment to make, or if the stack slot is the
2047 wrong mode, make a new stack slot. */
2048 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2050 /* If we have a decl for the original register, set it for the
2051 memory. If this is a shared MEM, make a copy. */
2052 if (REG_EXPR (regno_reg_rtx[i])
2053 && DECL_P (REG_EXPR (regno_reg_rtx[i])))
2055 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2057 /* We can do this only for the DECLs home pseudo, not for
2058 any copies of it, since otherwise when the stack slot
2059 is reused, nonoverlapping_memrefs_p might think they
2060 cannot overlap. */
2061 if (decl && REG_P (decl) && REGNO (decl) == (unsigned) i)
2063 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2064 x = copy_rtx (x);
2066 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2070 /* Save the stack slot for later. */
2071 reg_equiv_memory_loc[i] = x;
2075 /* Mark the slots in regs_ever_live for the hard regs
2076 used by pseudo-reg number REGNO. */
2078 void
2079 mark_home_live (int regno)
2081 int i, lim;
2083 i = reg_renumber[regno];
2084 if (i < 0)
2085 return;
2086 lim = i + hard_regno_nregs[i][PSEUDO_REGNO_MODE (regno)];
2087 while (i < lim)
2088 regs_ever_live[i++] = 1;
2091 /* This function handles the tracking of elimination offsets around branches.
2093 X is a piece of RTL being scanned.
2095 INSN is the insn that it came from, if any.
2097 INITIAL_P is nonzero if we are to set the offset to be the initial
2098 offset and zero if we are setting the offset of the label to be the
2099 current offset. */
2101 static void
2102 set_label_offsets (rtx x, rtx insn, int initial_p)
2104 enum rtx_code code = GET_CODE (x);
2105 rtx tem;
2106 unsigned int i;
2107 struct elim_table *p;
2109 switch (code)
2111 case LABEL_REF:
2112 if (LABEL_REF_NONLOCAL_P (x))
2113 return;
2115 x = XEXP (x, 0);
2117 /* ... fall through ... */
2119 case CODE_LABEL:
2120 /* If we know nothing about this label, set the desired offsets. Note
2121 that this sets the offset at a label to be the offset before a label
2122 if we don't know anything about the label. This is not correct for
2123 the label after a BARRIER, but is the best guess we can make. If
2124 we guessed wrong, we will suppress an elimination that might have
2125 been possible had we been able to guess correctly. */
2127 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2129 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2130 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2131 = (initial_p ? reg_eliminate[i].initial_offset
2132 : reg_eliminate[i].offset);
2133 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2136 /* Otherwise, if this is the definition of a label and it is
2137 preceded by a BARRIER, set our offsets to the known offset of
2138 that label. */
2140 else if (x == insn
2141 && (tem = prev_nonnote_insn (insn)) != 0
2142 && BARRIER_P (tem))
2143 set_offsets_for_label (insn);
2144 else
2145 /* If neither of the above cases is true, compare each offset
2146 with those previously recorded and suppress any eliminations
2147 where the offsets disagree. */
2149 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2150 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2151 != (initial_p ? reg_eliminate[i].initial_offset
2152 : reg_eliminate[i].offset))
2153 reg_eliminate[i].can_eliminate = 0;
2155 return;
2157 case JUMP_INSN:
2158 set_label_offsets (PATTERN (insn), insn, initial_p);
2160 /* ... fall through ... */
2162 case INSN:
2163 case CALL_INSN:
2164 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2165 and hence must have all eliminations at their initial offsets. */
2166 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2167 if (REG_NOTE_KIND (tem) == REG_LABEL)
2168 set_label_offsets (XEXP (tem, 0), insn, 1);
2169 return;
2171 case PARALLEL:
2172 case ADDR_VEC:
2173 case ADDR_DIFF_VEC:
2174 /* Each of the labels in the parallel or address vector must be
2175 at their initial offsets. We want the first field for PARALLEL
2176 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2178 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2179 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2180 insn, initial_p);
2181 return;
2183 case SET:
2184 /* We only care about setting PC. If the source is not RETURN,
2185 IF_THEN_ELSE, or a label, disable any eliminations not at
2186 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2187 isn't one of those possibilities. For branches to a label,
2188 call ourselves recursively.
2190 Note that this can disable elimination unnecessarily when we have
2191 a non-local goto since it will look like a non-constant jump to
2192 someplace in the current function. This isn't a significant
2193 problem since such jumps will normally be when all elimination
2194 pairs are back to their initial offsets. */
2196 if (SET_DEST (x) != pc_rtx)
2197 return;
2199 switch (GET_CODE (SET_SRC (x)))
2201 case PC:
2202 case RETURN:
2203 return;
2205 case LABEL_REF:
2206 set_label_offsets (SET_SRC (x), insn, initial_p);
2207 return;
2209 case IF_THEN_ELSE:
2210 tem = XEXP (SET_SRC (x), 1);
2211 if (GET_CODE (tem) == LABEL_REF)
2212 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2213 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2214 break;
2216 tem = XEXP (SET_SRC (x), 2);
2217 if (GET_CODE (tem) == LABEL_REF)
2218 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2219 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2220 break;
2221 return;
2223 default:
2224 break;
2227 /* If we reach here, all eliminations must be at their initial
2228 offset because we are doing a jump to a variable address. */
2229 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2230 if (p->offset != p->initial_offset)
2231 p->can_eliminate = 0;
2232 break;
2234 default:
2235 break;
2239 /* Scan X and replace any eliminable registers (such as fp) with a
2240 replacement (such as sp), plus an offset.
2242 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2243 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2244 MEM, we are allowed to replace a sum of a register and the constant zero
2245 with the register, which we cannot do outside a MEM. In addition, we need
2246 to record the fact that a register is referenced outside a MEM.
2248 If INSN is an insn, it is the insn containing X. If we replace a REG
2249 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2250 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2251 the REG is being modified.
2253 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2254 That's used when we eliminate in expressions stored in notes.
2255 This means, do not set ref_outside_mem even if the reference
2256 is outside of MEMs.
2258 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2259 replacements done assuming all offsets are at their initial values. If
2260 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2261 encounter, return the actual location so that find_reloads will do
2262 the proper thing. */
2265 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2267 enum rtx_code code = GET_CODE (x);
2268 struct elim_table *ep;
2269 int regno;
2270 rtx new;
2271 int i, j;
2272 const char *fmt;
2273 int copied = 0;
2275 if (! current_function_decl)
2276 return x;
2278 switch (code)
2280 case CONST_INT:
2281 case CONST_DOUBLE:
2282 case CONST_VECTOR:
2283 case CONST:
2284 case SYMBOL_REF:
2285 case CODE_LABEL:
2286 case PC:
2287 case CC0:
2288 case ASM_INPUT:
2289 case ADDR_VEC:
2290 case ADDR_DIFF_VEC:
2291 case RETURN:
2292 return x;
2294 case REG:
2295 regno = REGNO (x);
2297 /* First handle the case where we encounter a bare register that
2298 is eliminable. Replace it with a PLUS. */
2299 if (regno < FIRST_PSEUDO_REGISTER)
2301 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2302 ep++)
2303 if (ep->from_rtx == x && ep->can_eliminate)
2304 return plus_constant (ep->to_rtx, ep->previous_offset);
2307 else if (reg_renumber && reg_renumber[regno] < 0
2308 && reg_equiv_constant && reg_equiv_constant[regno]
2309 && ! CONSTANT_P (reg_equiv_constant[regno]))
2310 return eliminate_regs (copy_rtx (reg_equiv_constant[regno]),
2311 mem_mode, insn);
2312 return x;
2314 /* You might think handling MINUS in a manner similar to PLUS is a
2315 good idea. It is not. It has been tried multiple times and every
2316 time the change has had to have been reverted.
2318 Other parts of reload know a PLUS is special (gen_reload for example)
2319 and require special code to handle code a reloaded PLUS operand.
2321 Also consider backends where the flags register is clobbered by a
2322 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2323 lea instruction comes to mind). If we try to reload a MINUS, we
2324 may kill the flags register that was holding a useful value.
2326 So, please before trying to handle MINUS, consider reload as a
2327 whole instead of this little section as well as the backend issues. */
2328 case PLUS:
2329 /* If this is the sum of an eliminable register and a constant, rework
2330 the sum. */
2331 if (REG_P (XEXP (x, 0))
2332 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2333 && CONSTANT_P (XEXP (x, 1)))
2335 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2336 ep++)
2337 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2339 /* The only time we want to replace a PLUS with a REG (this
2340 occurs when the constant operand of the PLUS is the negative
2341 of the offset) is when we are inside a MEM. We won't want
2342 to do so at other times because that would change the
2343 structure of the insn in a way that reload can't handle.
2344 We special-case the commonest situation in
2345 eliminate_regs_in_insn, so just replace a PLUS with a
2346 PLUS here, unless inside a MEM. */
2347 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2348 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2349 return ep->to_rtx;
2350 else
2351 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2352 plus_constant (XEXP (x, 1),
2353 ep->previous_offset));
2356 /* If the register is not eliminable, we are done since the other
2357 operand is a constant. */
2358 return x;
2361 /* If this is part of an address, we want to bring any constant to the
2362 outermost PLUS. We will do this by doing register replacement in
2363 our operands and seeing if a constant shows up in one of them.
2365 Note that there is no risk of modifying the structure of the insn,
2366 since we only get called for its operands, thus we are either
2367 modifying the address inside a MEM, or something like an address
2368 operand of a load-address insn. */
2371 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2372 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2374 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2376 /* If one side is a PLUS and the other side is a pseudo that
2377 didn't get a hard register but has a reg_equiv_constant,
2378 we must replace the constant here since it may no longer
2379 be in the position of any operand. */
2380 if (GET_CODE (new0) == PLUS && REG_P (new1)
2381 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2382 && reg_renumber[REGNO (new1)] < 0
2383 && reg_equiv_constant != 0
2384 && reg_equiv_constant[REGNO (new1)] != 0)
2385 new1 = reg_equiv_constant[REGNO (new1)];
2386 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2387 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2388 && reg_renumber[REGNO (new0)] < 0
2389 && reg_equiv_constant[REGNO (new0)] != 0)
2390 new0 = reg_equiv_constant[REGNO (new0)];
2392 new = form_sum (new0, new1);
2394 /* As above, if we are not inside a MEM we do not want to
2395 turn a PLUS into something else. We might try to do so here
2396 for an addition of 0 if we aren't optimizing. */
2397 if (! mem_mode && GET_CODE (new) != PLUS)
2398 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2399 else
2400 return new;
2403 return x;
2405 case MULT:
2406 /* If this is the product of an eliminable register and a
2407 constant, apply the distribute law and move the constant out
2408 so that we have (plus (mult ..) ..). This is needed in order
2409 to keep load-address insns valid. This case is pathological.
2410 We ignore the possibility of overflow here. */
2411 if (REG_P (XEXP (x, 0))
2412 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2413 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2414 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2415 ep++)
2416 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2418 if (! mem_mode
2419 /* Refs inside notes don't count for this purpose. */
2420 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2421 || GET_CODE (insn) == INSN_LIST)))
2422 ep->ref_outside_mem = 1;
2424 return
2425 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2426 ep->previous_offset * INTVAL (XEXP (x, 1)));
2429 /* ... fall through ... */
2431 case CALL:
2432 case COMPARE:
2433 /* See comments before PLUS about handling MINUS. */
2434 case MINUS:
2435 case DIV: case UDIV:
2436 case MOD: case UMOD:
2437 case AND: case IOR: case XOR:
2438 case ROTATERT: case ROTATE:
2439 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2440 case NE: case EQ:
2441 case GE: case GT: case GEU: case GTU:
2442 case LE: case LT: case LEU: case LTU:
2444 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2445 rtx new1
2446 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, insn) : 0;
2448 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2449 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2451 return x;
2453 case EXPR_LIST:
2454 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2455 if (XEXP (x, 0))
2457 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2458 if (new != XEXP (x, 0))
2460 /* If this is a REG_DEAD note, it is not valid anymore.
2461 Using the eliminated version could result in creating a
2462 REG_DEAD note for the stack or frame pointer. */
2463 if (GET_MODE (x) == REG_DEAD)
2464 return (XEXP (x, 1)
2465 ? eliminate_regs (XEXP (x, 1), mem_mode, insn)
2466 : NULL_RTX);
2468 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2472 /* ... fall through ... */
2474 case INSN_LIST:
2475 /* Now do eliminations in the rest of the chain. If this was
2476 an EXPR_LIST, this might result in allocating more memory than is
2477 strictly needed, but it simplifies the code. */
2478 if (XEXP (x, 1))
2480 new = eliminate_regs (XEXP (x, 1), mem_mode, insn);
2481 if (new != XEXP (x, 1))
2482 return
2483 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2485 return x;
2487 case PRE_INC:
2488 case POST_INC:
2489 case PRE_DEC:
2490 case POST_DEC:
2491 case STRICT_LOW_PART:
2492 case NEG: case NOT:
2493 case SIGN_EXTEND: case ZERO_EXTEND:
2494 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2495 case FLOAT: case FIX:
2496 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2497 case ABS:
2498 case SQRT:
2499 case FFS:
2500 case CLZ:
2501 case CTZ:
2502 case POPCOUNT:
2503 case PARITY:
2504 new = eliminate_regs (XEXP (x, 0), mem_mode, insn);
2505 if (new != XEXP (x, 0))
2506 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2507 return x;
2509 case SUBREG:
2510 /* Similar to above processing, but preserve SUBREG_BYTE.
2511 Convert (subreg (mem)) to (mem) if not paradoxical.
2512 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2513 pseudo didn't get a hard reg, we must replace this with the
2514 eliminated version of the memory location because push_reload
2515 may do the replacement in certain circumstances. */
2516 if (REG_P (SUBREG_REG (x))
2517 && (GET_MODE_SIZE (GET_MODE (x))
2518 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2519 && reg_equiv_memory_loc != 0
2520 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2522 new = SUBREG_REG (x);
2524 else
2525 new = eliminate_regs (SUBREG_REG (x), mem_mode, insn);
2527 if (new != SUBREG_REG (x))
2529 int x_size = GET_MODE_SIZE (GET_MODE (x));
2530 int new_size = GET_MODE_SIZE (GET_MODE (new));
2532 if (MEM_P (new)
2533 && ((x_size < new_size
2534 #ifdef WORD_REGISTER_OPERATIONS
2535 /* On these machines, combine can create rtl of the form
2536 (set (subreg:m1 (reg:m2 R) 0) ...)
2537 where m1 < m2, and expects something interesting to
2538 happen to the entire word. Moreover, it will use the
2539 (reg:m2 R) later, expecting all bits to be preserved.
2540 So if the number of words is the same, preserve the
2541 subreg so that push_reload can see it. */
2542 && ! ((x_size - 1) / UNITS_PER_WORD
2543 == (new_size -1 ) / UNITS_PER_WORD)
2544 #endif
2546 || x_size == new_size)
2548 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2549 else
2550 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2553 return x;
2555 case MEM:
2556 /* Our only special processing is to pass the mode of the MEM to our
2557 recursive call and copy the flags. While we are here, handle this
2558 case more efficiently. */
2559 return
2560 replace_equiv_address_nv (x,
2561 eliminate_regs (XEXP (x, 0),
2562 GET_MODE (x), insn));
2564 case USE:
2565 /* Handle insn_list USE that a call to a pure function may generate. */
2566 new = eliminate_regs (XEXP (x, 0), 0, insn);
2567 if (new != XEXP (x, 0))
2568 return gen_rtx_USE (GET_MODE (x), new);
2569 return x;
2571 case CLOBBER:
2572 case ASM_OPERANDS:
2573 case SET:
2574 gcc_unreachable ();
2576 default:
2577 break;
2580 /* Process each of our operands recursively. If any have changed, make a
2581 copy of the rtx. */
2582 fmt = GET_RTX_FORMAT (code);
2583 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2585 if (*fmt == 'e')
2587 new = eliminate_regs (XEXP (x, i), mem_mode, insn);
2588 if (new != XEXP (x, i) && ! copied)
2590 rtx new_x = rtx_alloc (code);
2591 memcpy (new_x, x, RTX_SIZE (code));
2592 x = new_x;
2593 copied = 1;
2595 XEXP (x, i) = new;
2597 else if (*fmt == 'E')
2599 int copied_vec = 0;
2600 for (j = 0; j < XVECLEN (x, i); j++)
2602 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2603 if (new != XVECEXP (x, i, j) && ! copied_vec)
2605 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2606 XVEC (x, i)->elem);
2607 if (! copied)
2609 rtx new_x = rtx_alloc (code);
2610 memcpy (new_x, x, RTX_SIZE (code));
2611 x = new_x;
2612 copied = 1;
2614 XVEC (x, i) = new_v;
2615 copied_vec = 1;
2617 XVECEXP (x, i, j) = new;
2622 return x;
2625 /* Scan rtx X for modifications of elimination target registers. Update
2626 the table of eliminables to reflect the changed state. MEM_MODE is
2627 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2629 static void
2630 elimination_effects (rtx x, enum machine_mode mem_mode)
2632 enum rtx_code code = GET_CODE (x);
2633 struct elim_table *ep;
2634 int regno;
2635 int i, j;
2636 const char *fmt;
2638 switch (code)
2640 case CONST_INT:
2641 case CONST_DOUBLE:
2642 case CONST_VECTOR:
2643 case CONST:
2644 case SYMBOL_REF:
2645 case CODE_LABEL:
2646 case PC:
2647 case CC0:
2648 case ASM_INPUT:
2649 case ADDR_VEC:
2650 case ADDR_DIFF_VEC:
2651 case RETURN:
2652 return;
2654 case REG:
2655 regno = REGNO (x);
2657 /* First handle the case where we encounter a bare register that
2658 is eliminable. Replace it with a PLUS. */
2659 if (regno < FIRST_PSEUDO_REGISTER)
2661 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2662 ep++)
2663 if (ep->from_rtx == x && ep->can_eliminate)
2665 if (! mem_mode)
2666 ep->ref_outside_mem = 1;
2667 return;
2671 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2672 && reg_equiv_constant[regno]
2673 && ! function_invariant_p (reg_equiv_constant[regno]))
2674 elimination_effects (reg_equiv_constant[regno], mem_mode);
2675 return;
2677 case PRE_INC:
2678 case POST_INC:
2679 case PRE_DEC:
2680 case POST_DEC:
2681 case POST_MODIFY:
2682 case PRE_MODIFY:
2683 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2684 if (ep->to_rtx == XEXP (x, 0))
2686 int size = GET_MODE_SIZE (mem_mode);
2688 /* If more bytes than MEM_MODE are pushed, account for them. */
2689 #ifdef PUSH_ROUNDING
2690 if (ep->to_rtx == stack_pointer_rtx)
2691 size = PUSH_ROUNDING (size);
2692 #endif
2693 if (code == PRE_DEC || code == POST_DEC)
2694 ep->offset += size;
2695 else if (code == PRE_INC || code == POST_INC)
2696 ep->offset -= size;
2697 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2698 && GET_CODE (XEXP (x, 1)) == PLUS
2699 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2700 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2701 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2704 /* These two aren't unary operators. */
2705 if (code == POST_MODIFY || code == PRE_MODIFY)
2706 break;
2708 /* Fall through to generic unary operation case. */
2709 case STRICT_LOW_PART:
2710 case NEG: case NOT:
2711 case SIGN_EXTEND: case ZERO_EXTEND:
2712 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2713 case FLOAT: case FIX:
2714 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2715 case ABS:
2716 case SQRT:
2717 case FFS:
2718 case CLZ:
2719 case CTZ:
2720 case POPCOUNT:
2721 case PARITY:
2722 elimination_effects (XEXP (x, 0), mem_mode);
2723 return;
2725 case SUBREG:
2726 if (REG_P (SUBREG_REG (x))
2727 && (GET_MODE_SIZE (GET_MODE (x))
2728 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2729 && reg_equiv_memory_loc != 0
2730 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2731 return;
2733 elimination_effects (SUBREG_REG (x), mem_mode);
2734 return;
2736 case USE:
2737 /* If using a register that is the source of an eliminate we still
2738 think can be performed, note it cannot be performed since we don't
2739 know how this register is used. */
2740 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2741 if (ep->from_rtx == XEXP (x, 0))
2742 ep->can_eliminate = 0;
2744 elimination_effects (XEXP (x, 0), mem_mode);
2745 return;
2747 case CLOBBER:
2748 /* If clobbering a register that is the replacement register for an
2749 elimination we still think can be performed, note that it cannot
2750 be performed. Otherwise, we need not be concerned about it. */
2751 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2752 if (ep->to_rtx == XEXP (x, 0))
2753 ep->can_eliminate = 0;
2755 elimination_effects (XEXP (x, 0), mem_mode);
2756 return;
2758 case SET:
2759 /* Check for setting a register that we know about. */
2760 if (REG_P (SET_DEST (x)))
2762 /* See if this is setting the replacement register for an
2763 elimination.
2765 If DEST is the hard frame pointer, we do nothing because we
2766 assume that all assignments to the frame pointer are for
2767 non-local gotos and are being done at a time when they are valid
2768 and do not disturb anything else. Some machines want to
2769 eliminate a fake argument pointer (or even a fake frame pointer)
2770 with either the real frame or the stack pointer. Assignments to
2771 the hard frame pointer must not prevent this elimination. */
2773 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2774 ep++)
2775 if (ep->to_rtx == SET_DEST (x)
2776 && SET_DEST (x) != hard_frame_pointer_rtx)
2778 /* If it is being incremented, adjust the offset. Otherwise,
2779 this elimination can't be done. */
2780 rtx src = SET_SRC (x);
2782 if (GET_CODE (src) == PLUS
2783 && XEXP (src, 0) == SET_DEST (x)
2784 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2785 ep->offset -= INTVAL (XEXP (src, 1));
2786 else
2787 ep->can_eliminate = 0;
2791 elimination_effects (SET_DEST (x), 0);
2792 elimination_effects (SET_SRC (x), 0);
2793 return;
2795 case MEM:
2796 /* Our only special processing is to pass the mode of the MEM to our
2797 recursive call. */
2798 elimination_effects (XEXP (x, 0), GET_MODE (x));
2799 return;
2801 default:
2802 break;
2805 fmt = GET_RTX_FORMAT (code);
2806 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2808 if (*fmt == 'e')
2809 elimination_effects (XEXP (x, i), mem_mode);
2810 else if (*fmt == 'E')
2811 for (j = 0; j < XVECLEN (x, i); j++)
2812 elimination_effects (XVECEXP (x, i, j), mem_mode);
2816 /* Descend through rtx X and verify that no references to eliminable registers
2817 remain. If any do remain, mark the involved register as not
2818 eliminable. */
2820 static void
2821 check_eliminable_occurrences (rtx x)
2823 const char *fmt;
2824 int i;
2825 enum rtx_code code;
2827 if (x == 0)
2828 return;
2830 code = GET_CODE (x);
2832 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2834 struct elim_table *ep;
2836 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2837 if (ep->from_rtx == x)
2838 ep->can_eliminate = 0;
2839 return;
2842 fmt = GET_RTX_FORMAT (code);
2843 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2845 if (*fmt == 'e')
2846 check_eliminable_occurrences (XEXP (x, i));
2847 else if (*fmt == 'E')
2849 int j;
2850 for (j = 0; j < XVECLEN (x, i); j++)
2851 check_eliminable_occurrences (XVECEXP (x, i, j));
2856 /* Scan INSN and eliminate all eliminable registers in it.
2858 If REPLACE is nonzero, do the replacement destructively. Also
2859 delete the insn as dead it if it is setting an eliminable register.
2861 If REPLACE is zero, do all our allocations in reload_obstack.
2863 If no eliminations were done and this insn doesn't require any elimination
2864 processing (these are not identical conditions: it might be updating sp,
2865 but not referencing fp; this needs to be seen during reload_as_needed so
2866 that the offset between fp and sp can be taken into consideration), zero
2867 is returned. Otherwise, 1 is returned. */
2869 static int
2870 eliminate_regs_in_insn (rtx insn, int replace)
2872 int icode = recog_memoized (insn);
2873 rtx old_body = PATTERN (insn);
2874 int insn_is_asm = asm_noperands (old_body) >= 0;
2875 rtx old_set = single_set (insn);
2876 rtx new_body;
2877 int val = 0;
2878 int i;
2879 rtx substed_operand[MAX_RECOG_OPERANDS];
2880 rtx orig_operand[MAX_RECOG_OPERANDS];
2881 struct elim_table *ep;
2882 rtx plus_src;
2884 if (! insn_is_asm && icode < 0)
2886 gcc_assert (GET_CODE (PATTERN (insn)) == USE
2887 || GET_CODE (PATTERN (insn)) == CLOBBER
2888 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2889 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2890 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
2891 return 0;
2894 if (old_set != 0 && REG_P (SET_DEST (old_set))
2895 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2897 /* Check for setting an eliminable register. */
2898 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2899 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2901 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2902 /* If this is setting the frame pointer register to the
2903 hardware frame pointer register and this is an elimination
2904 that will be done (tested above), this insn is really
2905 adjusting the frame pointer downward to compensate for
2906 the adjustment done before a nonlocal goto. */
2907 if (ep->from == FRAME_POINTER_REGNUM
2908 && ep->to == HARD_FRAME_POINTER_REGNUM)
2910 rtx base = SET_SRC (old_set);
2911 rtx base_insn = insn;
2912 HOST_WIDE_INT offset = 0;
2914 while (base != ep->to_rtx)
2916 rtx prev_insn, prev_set;
2918 if (GET_CODE (base) == PLUS
2919 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2921 offset += INTVAL (XEXP (base, 1));
2922 base = XEXP (base, 0);
2924 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2925 && (prev_set = single_set (prev_insn)) != 0
2926 && rtx_equal_p (SET_DEST (prev_set), base))
2928 base = SET_SRC (prev_set);
2929 base_insn = prev_insn;
2931 else
2932 break;
2935 if (base == ep->to_rtx)
2937 rtx src
2938 = plus_constant (ep->to_rtx, offset - ep->offset);
2940 new_body = old_body;
2941 if (! replace)
2943 new_body = copy_insn (old_body);
2944 if (REG_NOTES (insn))
2945 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
2947 PATTERN (insn) = new_body;
2948 old_set = single_set (insn);
2950 /* First see if this insn remains valid when we
2951 make the change. If not, keep the INSN_CODE
2952 the same and let reload fit it up. */
2953 validate_change (insn, &SET_SRC (old_set), src, 1);
2954 validate_change (insn, &SET_DEST (old_set),
2955 ep->to_rtx, 1);
2956 if (! apply_change_group ())
2958 SET_SRC (old_set) = src;
2959 SET_DEST (old_set) = ep->to_rtx;
2962 val = 1;
2963 goto done;
2966 #endif
2968 /* In this case this insn isn't serving a useful purpose. We
2969 will delete it in reload_as_needed once we know that this
2970 elimination is, in fact, being done.
2972 If REPLACE isn't set, we can't delete this insn, but needn't
2973 process it since it won't be used unless something changes. */
2974 if (replace)
2976 delete_dead_insn (insn);
2977 return 1;
2979 val = 1;
2980 goto done;
2984 /* We allow one special case which happens to work on all machines we
2985 currently support: a single set with the source or a REG_EQUAL
2986 note being a PLUS of an eliminable register and a constant. */
2987 plus_src = 0;
2988 if (old_set && REG_P (SET_DEST (old_set)))
2990 /* First see if the source is of the form (plus (reg) CST). */
2991 if (GET_CODE (SET_SRC (old_set)) == PLUS
2992 && REG_P (XEXP (SET_SRC (old_set), 0))
2993 && GET_CODE (XEXP (SET_SRC (old_set), 1)) == CONST_INT
2994 && REGNO (XEXP (SET_SRC (old_set), 0)) < FIRST_PSEUDO_REGISTER)
2995 plus_src = SET_SRC (old_set);
2996 else if (REG_P (SET_SRC (old_set)))
2998 /* Otherwise, see if we have a REG_EQUAL note of the form
2999 (plus (reg) CST). */
3000 rtx links;
3001 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3003 if (REG_NOTE_KIND (links) == REG_EQUAL
3004 && GET_CODE (XEXP (links, 0)) == PLUS
3005 && REG_P (XEXP (XEXP (links, 0), 0))
3006 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT
3007 && REGNO (XEXP (XEXP (links, 0), 0)) < FIRST_PSEUDO_REGISTER)
3009 plus_src = XEXP (links, 0);
3010 break;
3015 if (plus_src)
3017 rtx reg = XEXP (plus_src, 0);
3018 HOST_WIDE_INT offset = INTVAL (XEXP (plus_src, 1));
3020 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3021 if (ep->from_rtx == reg && ep->can_eliminate)
3023 offset += ep->offset;
3025 if (offset == 0)
3027 int num_clobbers;
3028 /* We assume here that if we need a PARALLEL with
3029 CLOBBERs for this assignment, we can do with the
3030 MATCH_SCRATCHes that add_clobbers allocates.
3031 There's not much we can do if that doesn't work. */
3032 PATTERN (insn) = gen_rtx_SET (VOIDmode,
3033 SET_DEST (old_set),
3034 ep->to_rtx);
3035 num_clobbers = 0;
3036 INSN_CODE (insn) = recog (PATTERN (insn), insn, &num_clobbers);
3037 if (num_clobbers)
3039 rtvec vec = rtvec_alloc (num_clobbers + 1);
3041 vec->elem[0] = PATTERN (insn);
3042 PATTERN (insn) = gen_rtx_PARALLEL (VOIDmode, vec);
3043 add_clobbers (PATTERN (insn), INSN_CODE (insn));
3045 gcc_assert (INSN_CODE (insn) >= 0);
3047 /* If we have a nonzero offset, and the source is already
3048 a simple REG, the following transformation would
3049 increase the cost of the insn by replacing a simple REG
3050 with (plus (reg sp) CST). So try only when plus_src
3051 comes from old_set proper, not REG_NOTES. */
3052 else if (SET_SRC (old_set) == plus_src)
3054 new_body = old_body;
3055 if (! replace)
3057 new_body = copy_insn (old_body);
3058 if (REG_NOTES (insn))
3059 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3061 PATTERN (insn) = new_body;
3062 old_set = single_set (insn);
3064 XEXP (SET_SRC (old_set), 0) = ep->to_rtx;
3065 XEXP (SET_SRC (old_set), 1) = GEN_INT (offset);
3067 else
3068 break;
3070 val = 1;
3071 /* This can't have an effect on elimination offsets, so skip right
3072 to the end. */
3073 goto done;
3077 /* Determine the effects of this insn on elimination offsets. */
3078 elimination_effects (old_body, 0);
3080 /* Eliminate all eliminable registers occurring in operands that
3081 can be handled by reload. */
3082 extract_insn (insn);
3083 for (i = 0; i < recog_data.n_operands; i++)
3085 orig_operand[i] = recog_data.operand[i];
3086 substed_operand[i] = recog_data.operand[i];
3088 /* For an asm statement, every operand is eliminable. */
3089 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3091 /* Check for setting a register that we know about. */
3092 if (recog_data.operand_type[i] != OP_IN
3093 && REG_P (orig_operand[i]))
3095 /* If we are assigning to a register that can be eliminated, it
3096 must be as part of a PARALLEL, since the code above handles
3097 single SETs. We must indicate that we can no longer
3098 eliminate this reg. */
3099 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3100 ep++)
3101 if (ep->from_rtx == orig_operand[i])
3102 ep->can_eliminate = 0;
3105 substed_operand[i] = eliminate_regs (recog_data.operand[i], 0,
3106 replace ? insn : NULL_RTX);
3107 if (substed_operand[i] != orig_operand[i])
3108 val = 1;
3109 /* Terminate the search in check_eliminable_occurrences at
3110 this point. */
3111 *recog_data.operand_loc[i] = 0;
3113 /* If an output operand changed from a REG to a MEM and INSN is an
3114 insn, write a CLOBBER insn. */
3115 if (recog_data.operand_type[i] != OP_IN
3116 && REG_P (orig_operand[i])
3117 && MEM_P (substed_operand[i])
3118 && replace)
3119 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3120 insn);
3124 for (i = 0; i < recog_data.n_dups; i++)
3125 *recog_data.dup_loc[i]
3126 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3128 /* If any eliminable remain, they aren't eliminable anymore. */
3129 check_eliminable_occurrences (old_body);
3131 /* Substitute the operands; the new values are in the substed_operand
3132 array. */
3133 for (i = 0; i < recog_data.n_operands; i++)
3134 *recog_data.operand_loc[i] = substed_operand[i];
3135 for (i = 0; i < recog_data.n_dups; i++)
3136 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3138 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3139 re-recognize the insn. We do this in case we had a simple addition
3140 but now can do this as a load-address. This saves an insn in this
3141 common case.
3142 If re-recognition fails, the old insn code number will still be used,
3143 and some register operands may have changed into PLUS expressions.
3144 These will be handled by find_reloads by loading them into a register
3145 again. */
3147 if (val)
3149 /* If we aren't replacing things permanently and we changed something,
3150 make another copy to ensure that all the RTL is new. Otherwise
3151 things can go wrong if find_reload swaps commutative operands
3152 and one is inside RTL that has been copied while the other is not. */
3153 new_body = old_body;
3154 if (! replace)
3156 new_body = copy_insn (old_body);
3157 if (REG_NOTES (insn))
3158 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3160 PATTERN (insn) = new_body;
3162 /* If we had a move insn but now we don't, rerecognize it. This will
3163 cause spurious re-recognition if the old move had a PARALLEL since
3164 the new one still will, but we can't call single_set without
3165 having put NEW_BODY into the insn and the re-recognition won't
3166 hurt in this rare case. */
3167 /* ??? Why this huge if statement - why don't we just rerecognize the
3168 thing always? */
3169 if (! insn_is_asm
3170 && old_set != 0
3171 && ((REG_P (SET_SRC (old_set))
3172 && (GET_CODE (new_body) != SET
3173 || !REG_P (SET_SRC (new_body))))
3174 /* If this was a load from or store to memory, compare
3175 the MEM in recog_data.operand to the one in the insn.
3176 If they are not equal, then rerecognize the insn. */
3177 || (old_set != 0
3178 && ((MEM_P (SET_SRC (old_set))
3179 && SET_SRC (old_set) != recog_data.operand[1])
3180 || (MEM_P (SET_DEST (old_set))
3181 && SET_DEST (old_set) != recog_data.operand[0])))
3182 /* If this was an add insn before, rerecognize. */
3183 || GET_CODE (SET_SRC (old_set)) == PLUS))
3185 int new_icode = recog (PATTERN (insn), insn, 0);
3186 if (new_icode < 0)
3187 INSN_CODE (insn) = icode;
3191 /* Restore the old body. If there were any changes to it, we made a copy
3192 of it while the changes were still in place, so we'll correctly return
3193 a modified insn below. */
3194 if (! replace)
3196 /* Restore the old body. */
3197 for (i = 0; i < recog_data.n_operands; i++)
3198 *recog_data.operand_loc[i] = orig_operand[i];
3199 for (i = 0; i < recog_data.n_dups; i++)
3200 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3203 /* Update all elimination pairs to reflect the status after the current
3204 insn. The changes we make were determined by the earlier call to
3205 elimination_effects.
3207 We also detect cases where register elimination cannot be done,
3208 namely, if a register would be both changed and referenced outside a MEM
3209 in the resulting insn since such an insn is often undefined and, even if
3210 not, we cannot know what meaning will be given to it. Note that it is
3211 valid to have a register used in an address in an insn that changes it
3212 (presumably with a pre- or post-increment or decrement).
3214 If anything changes, return nonzero. */
3216 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3218 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3219 ep->can_eliminate = 0;
3221 ep->ref_outside_mem = 0;
3223 if (ep->previous_offset != ep->offset)
3224 val = 1;
3227 done:
3228 /* If we changed something, perform elimination in REG_NOTES. This is
3229 needed even when REPLACE is zero because a REG_DEAD note might refer
3230 to a register that we eliminate and could cause a different number
3231 of spill registers to be needed in the final reload pass than in
3232 the pre-passes. */
3233 if (val && REG_NOTES (insn) != 0)
3234 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, REG_NOTES (insn));
3236 return val;
3239 /* Loop through all elimination pairs.
3240 Recalculate the number not at initial offset.
3242 Compute the maximum offset (minimum offset if the stack does not
3243 grow downward) for each elimination pair. */
3245 static void
3246 update_eliminable_offsets (void)
3248 struct elim_table *ep;
3250 num_not_at_initial_offset = 0;
3251 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3253 ep->previous_offset = ep->offset;
3254 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3255 num_not_at_initial_offset++;
3259 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3260 replacement we currently believe is valid, mark it as not eliminable if X
3261 modifies DEST in any way other than by adding a constant integer to it.
3263 If DEST is the frame pointer, we do nothing because we assume that
3264 all assignments to the hard frame pointer are nonlocal gotos and are being
3265 done at a time when they are valid and do not disturb anything else.
3266 Some machines want to eliminate a fake argument pointer with either the
3267 frame or stack pointer. Assignments to the hard frame pointer must not
3268 prevent this elimination.
3270 Called via note_stores from reload before starting its passes to scan
3271 the insns of the function. */
3273 static void
3274 mark_not_eliminable (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
3276 unsigned int i;
3278 /* A SUBREG of a hard register here is just changing its mode. We should
3279 not see a SUBREG of an eliminable hard register, but check just in
3280 case. */
3281 if (GET_CODE (dest) == SUBREG)
3282 dest = SUBREG_REG (dest);
3284 if (dest == hard_frame_pointer_rtx)
3285 return;
3287 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3288 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3289 && (GET_CODE (x) != SET
3290 || GET_CODE (SET_SRC (x)) != PLUS
3291 || XEXP (SET_SRC (x), 0) != dest
3292 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3294 reg_eliminate[i].can_eliminate_previous
3295 = reg_eliminate[i].can_eliminate = 0;
3296 num_eliminable--;
3300 /* Verify that the initial elimination offsets did not change since the
3301 last call to set_initial_elim_offsets. This is used to catch cases
3302 where something illegal happened during reload_as_needed that could
3303 cause incorrect code to be generated if we did not check for it. */
3305 static bool
3306 verify_initial_elim_offsets (void)
3308 HOST_WIDE_INT t;
3310 if (!num_eliminable)
3311 return true;
3313 #ifdef ELIMINABLE_REGS
3315 struct elim_table *ep;
3317 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3319 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3320 if (t != ep->initial_offset)
3321 return false;
3324 #else
3325 INITIAL_FRAME_POINTER_OFFSET (t);
3326 if (t != reg_eliminate[0].initial_offset)
3327 return false;
3328 #endif
3330 return true;
3333 /* Reset all offsets on eliminable registers to their initial values. */
3335 static void
3336 set_initial_elim_offsets (void)
3338 struct elim_table *ep = reg_eliminate;
3340 #ifdef ELIMINABLE_REGS
3341 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3343 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3344 ep->previous_offset = ep->offset = ep->initial_offset;
3346 #else
3347 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3348 ep->previous_offset = ep->offset = ep->initial_offset;
3349 #endif
3351 num_not_at_initial_offset = 0;
3354 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3356 static void
3357 set_initial_eh_label_offset (rtx label)
3359 set_label_offsets (label, NULL_RTX, 1);
3362 /* Initialize the known label offsets.
3363 Set a known offset for each forced label to be at the initial offset
3364 of each elimination. We do this because we assume that all
3365 computed jumps occur from a location where each elimination is
3366 at its initial offset.
3367 For all other labels, show that we don't know the offsets. */
3369 static void
3370 set_initial_label_offsets (void)
3372 rtx x;
3373 memset (offsets_known_at, 0, num_labels);
3375 for (x = forced_labels; x; x = XEXP (x, 1))
3376 if (XEXP (x, 0))
3377 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3379 for_each_eh_label (set_initial_eh_label_offset);
3382 /* Set all elimination offsets to the known values for the code label given
3383 by INSN. */
3385 static void
3386 set_offsets_for_label (rtx insn)
3388 unsigned int i;
3389 int label_nr = CODE_LABEL_NUMBER (insn);
3390 struct elim_table *ep;
3392 num_not_at_initial_offset = 0;
3393 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3395 ep->offset = ep->previous_offset
3396 = offsets_at[label_nr - first_label_num][i];
3397 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3398 num_not_at_initial_offset++;
3402 /* See if anything that happened changes which eliminations are valid.
3403 For example, on the SPARC, whether or not the frame pointer can
3404 be eliminated can depend on what registers have been used. We need
3405 not check some conditions again (such as flag_omit_frame_pointer)
3406 since they can't have changed. */
3408 static void
3409 update_eliminables (HARD_REG_SET *pset)
3411 int previous_frame_pointer_needed = frame_pointer_needed;
3412 struct elim_table *ep;
3414 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3415 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3416 #ifdef ELIMINABLE_REGS
3417 || ! CAN_ELIMINATE (ep->from, ep->to)
3418 #endif
3420 ep->can_eliminate = 0;
3422 /* Look for the case where we have discovered that we can't replace
3423 register A with register B and that means that we will now be
3424 trying to replace register A with register C. This means we can
3425 no longer replace register C with register B and we need to disable
3426 such an elimination, if it exists. This occurs often with A == ap,
3427 B == sp, and C == fp. */
3429 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3431 struct elim_table *op;
3432 int new_to = -1;
3434 if (! ep->can_eliminate && ep->can_eliminate_previous)
3436 /* Find the current elimination for ep->from, if there is a
3437 new one. */
3438 for (op = reg_eliminate;
3439 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3440 if (op->from == ep->from && op->can_eliminate)
3442 new_to = op->to;
3443 break;
3446 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3447 disable it. */
3448 for (op = reg_eliminate;
3449 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3450 if (op->from == new_to && op->to == ep->to)
3451 op->can_eliminate = 0;
3455 /* See if any registers that we thought we could eliminate the previous
3456 time are no longer eliminable. If so, something has changed and we
3457 must spill the register. Also, recompute the number of eliminable
3458 registers and see if the frame pointer is needed; it is if there is
3459 no elimination of the frame pointer that we can perform. */
3461 frame_pointer_needed = 1;
3462 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3464 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3465 && ep->to != HARD_FRAME_POINTER_REGNUM)
3466 frame_pointer_needed = 0;
3468 if (! ep->can_eliminate && ep->can_eliminate_previous)
3470 ep->can_eliminate_previous = 0;
3471 SET_HARD_REG_BIT (*pset, ep->from);
3472 num_eliminable--;
3476 /* If we didn't need a frame pointer last time, but we do now, spill
3477 the hard frame pointer. */
3478 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3479 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3482 /* Initialize the table of registers to eliminate. */
3484 static void
3485 init_elim_table (void)
3487 struct elim_table *ep;
3488 #ifdef ELIMINABLE_REGS
3489 const struct elim_table_1 *ep1;
3490 #endif
3492 if (!reg_eliminate)
3493 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3495 /* Does this function require a frame pointer? */
3497 frame_pointer_needed = (! flag_omit_frame_pointer
3498 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3499 and restore sp for alloca. So we can't eliminate
3500 the frame pointer in that case. At some point,
3501 we should improve this by emitting the
3502 sp-adjusting insns for this case. */
3503 || (current_function_calls_alloca
3504 && EXIT_IGNORE_STACK)
3505 || FRAME_POINTER_REQUIRED);
3507 num_eliminable = 0;
3509 #ifdef ELIMINABLE_REGS
3510 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3511 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3513 ep->from = ep1->from;
3514 ep->to = ep1->to;
3515 ep->can_eliminate = ep->can_eliminate_previous
3516 = (CAN_ELIMINATE (ep->from, ep->to)
3517 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3519 #else
3520 reg_eliminate[0].from = reg_eliminate_1[0].from;
3521 reg_eliminate[0].to = reg_eliminate_1[0].to;
3522 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3523 = ! frame_pointer_needed;
3524 #endif
3526 /* Count the number of eliminable registers and build the FROM and TO
3527 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3528 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3529 We depend on this. */
3530 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3532 num_eliminable += ep->can_eliminate;
3533 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3534 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3538 /* Kick all pseudos out of hard register REGNO.
3540 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3541 because we found we can't eliminate some register. In the case, no pseudos
3542 are allowed to be in the register, even if they are only in a block that
3543 doesn't require spill registers, unlike the case when we are spilling this
3544 hard reg to produce another spill register.
3546 Return nonzero if any pseudos needed to be kicked out. */
3548 static void
3549 spill_hard_reg (unsigned int regno, int cant_eliminate)
3551 int i;
3553 if (cant_eliminate)
3555 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3556 regs_ever_live[regno] = 1;
3559 /* Spill every pseudo reg that was allocated to this reg
3560 or to something that overlaps this reg. */
3562 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3563 if (reg_renumber[i] >= 0
3564 && (unsigned int) reg_renumber[i] <= regno
3565 && ((unsigned int) reg_renumber[i]
3566 + hard_regno_nregs[(unsigned int) reg_renumber[i]]
3567 [PSEUDO_REGNO_MODE (i)]
3568 > regno))
3569 SET_REGNO_REG_SET (&spilled_pseudos, i);
3572 /* After find_reload_regs has been run for all insn that need reloads,
3573 and/or spill_hard_regs was called, this function is used to actually
3574 spill pseudo registers and try to reallocate them. It also sets up the
3575 spill_regs array for use by choose_reload_regs. */
3577 static int
3578 finish_spills (int global)
3580 struct insn_chain *chain;
3581 int something_changed = 0;
3582 unsigned i;
3583 reg_set_iterator rsi;
3585 /* Build the spill_regs array for the function. */
3586 /* If there are some registers still to eliminate and one of the spill regs
3587 wasn't ever used before, additional stack space may have to be
3588 allocated to store this register. Thus, we may have changed the offset
3589 between the stack and frame pointers, so mark that something has changed.
3591 One might think that we need only set VAL to 1 if this is a call-used
3592 register. However, the set of registers that must be saved by the
3593 prologue is not identical to the call-used set. For example, the
3594 register used by the call insn for the return PC is a call-used register,
3595 but must be saved by the prologue. */
3597 n_spills = 0;
3598 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3599 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3601 spill_reg_order[i] = n_spills;
3602 spill_regs[n_spills++] = i;
3603 if (num_eliminable && ! regs_ever_live[i])
3604 something_changed = 1;
3605 regs_ever_live[i] = 1;
3607 else
3608 spill_reg_order[i] = -1;
3610 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
3612 /* Record the current hard register the pseudo is allocated to in
3613 pseudo_previous_regs so we avoid reallocating it to the same
3614 hard reg in a later pass. */
3615 gcc_assert (reg_renumber[i] >= 0);
3617 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3618 /* Mark it as no longer having a hard register home. */
3619 reg_renumber[i] = -1;
3620 /* We will need to scan everything again. */
3621 something_changed = 1;
3624 /* Retry global register allocation if possible. */
3625 if (global)
3627 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3628 /* For every insn that needs reloads, set the registers used as spill
3629 regs in pseudo_forbidden_regs for every pseudo live across the
3630 insn. */
3631 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3633 EXECUTE_IF_SET_IN_REG_SET
3634 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
3636 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3637 chain->used_spill_regs);
3639 EXECUTE_IF_SET_IN_REG_SET
3640 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
3642 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3643 chain->used_spill_regs);
3647 /* Retry allocating the spilled pseudos. For each reg, merge the
3648 various reg sets that indicate which hard regs can't be used,
3649 and call retry_global_alloc.
3650 We change spill_pseudos here to only contain pseudos that did not
3651 get a new hard register. */
3652 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3653 if (reg_old_renumber[i] != reg_renumber[i])
3655 HARD_REG_SET forbidden;
3656 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3657 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3658 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3659 retry_global_alloc (i, forbidden);
3660 if (reg_renumber[i] >= 0)
3661 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3665 /* Fix up the register information in the insn chain.
3666 This involves deleting those of the spilled pseudos which did not get
3667 a new hard register home from the live_{before,after} sets. */
3668 for (chain = reload_insn_chain; chain; chain = chain->next)
3670 HARD_REG_SET used_by_pseudos;
3671 HARD_REG_SET used_by_pseudos2;
3673 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3674 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3676 /* Mark any unallocated hard regs as available for spills. That
3677 makes inheritance work somewhat better. */
3678 if (chain->need_reload)
3680 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3681 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3682 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3684 /* Save the old value for the sanity test below. */
3685 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3687 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3688 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3689 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3690 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3692 /* Make sure we only enlarge the set. */
3693 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3694 gcc_unreachable ();
3695 ok:;
3699 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3700 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3702 int regno = reg_renumber[i];
3703 if (reg_old_renumber[i] == regno)
3704 continue;
3706 alter_reg (i, reg_old_renumber[i]);
3707 reg_old_renumber[i] = regno;
3708 if (dump_file)
3710 if (regno == -1)
3711 fprintf (dump_file, " Register %d now on stack.\n\n", i);
3712 else
3713 fprintf (dump_file, " Register %d now in %d.\n\n",
3714 i, reg_renumber[i]);
3718 return something_changed;
3721 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
3723 static void
3724 scan_paradoxical_subregs (rtx x)
3726 int i;
3727 const char *fmt;
3728 enum rtx_code code = GET_CODE (x);
3730 switch (code)
3732 case REG:
3733 case CONST_INT:
3734 case CONST:
3735 case SYMBOL_REF:
3736 case LABEL_REF:
3737 case CONST_DOUBLE:
3738 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3739 case CC0:
3740 case PC:
3741 case USE:
3742 case CLOBBER:
3743 return;
3745 case SUBREG:
3746 if (REG_P (SUBREG_REG (x))
3747 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3748 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3749 = GET_MODE_SIZE (GET_MODE (x));
3750 return;
3752 default:
3753 break;
3756 fmt = GET_RTX_FORMAT (code);
3757 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3759 if (fmt[i] == 'e')
3760 scan_paradoxical_subregs (XEXP (x, i));
3761 else if (fmt[i] == 'E')
3763 int j;
3764 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3765 scan_paradoxical_subregs (XVECEXP (x, i, j));
3770 /* Reload pseudo-registers into hard regs around each insn as needed.
3771 Additional register load insns are output before the insn that needs it
3772 and perhaps store insns after insns that modify the reloaded pseudo reg.
3774 reg_last_reload_reg and reg_reloaded_contents keep track of
3775 which registers are already available in reload registers.
3776 We update these for the reloads that we perform,
3777 as the insns are scanned. */
3779 static void
3780 reload_as_needed (int live_known)
3782 struct insn_chain *chain;
3783 #if defined (AUTO_INC_DEC)
3784 int i;
3785 #endif
3786 rtx x;
3788 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
3789 memset (spill_reg_store, 0, sizeof spill_reg_store);
3790 reg_last_reload_reg = xcalloc (max_regno, sizeof (rtx));
3791 reg_has_output_reload = xmalloc (max_regno);
3792 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3793 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
3795 set_initial_elim_offsets ();
3797 for (chain = reload_insn_chain; chain; chain = chain->next)
3799 rtx prev = 0;
3800 rtx insn = chain->insn;
3801 rtx old_next = NEXT_INSN (insn);
3803 /* If we pass a label, copy the offsets from the label information
3804 into the current offsets of each elimination. */
3805 if (LABEL_P (insn))
3806 set_offsets_for_label (insn);
3808 else if (INSN_P (insn))
3810 rtx oldpat = copy_rtx (PATTERN (insn));
3812 /* If this is a USE and CLOBBER of a MEM, ensure that any
3813 references to eliminable registers have been removed. */
3815 if ((GET_CODE (PATTERN (insn)) == USE
3816 || GET_CODE (PATTERN (insn)) == CLOBBER)
3817 && MEM_P (XEXP (PATTERN (insn), 0)))
3818 XEXP (XEXP (PATTERN (insn), 0), 0)
3819 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3820 GET_MODE (XEXP (PATTERN (insn), 0)),
3821 NULL_RTX);
3823 /* If we need to do register elimination processing, do so.
3824 This might delete the insn, in which case we are done. */
3825 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3827 eliminate_regs_in_insn (insn, 1);
3828 if (NOTE_P (insn))
3830 update_eliminable_offsets ();
3831 continue;
3835 /* If need_elim is nonzero but need_reload is zero, one might think
3836 that we could simply set n_reloads to 0. However, find_reloads
3837 could have done some manipulation of the insn (such as swapping
3838 commutative operands), and these manipulations are lost during
3839 the first pass for every insn that needs register elimination.
3840 So the actions of find_reloads must be redone here. */
3842 if (! chain->need_elim && ! chain->need_reload
3843 && ! chain->need_operand_change)
3844 n_reloads = 0;
3845 /* First find the pseudo regs that must be reloaded for this insn.
3846 This info is returned in the tables reload_... (see reload.h).
3847 Also modify the body of INSN by substituting RELOAD
3848 rtx's for those pseudo regs. */
3849 else
3851 memset (reg_has_output_reload, 0, max_regno);
3852 CLEAR_HARD_REG_SET (reg_is_output_reload);
3854 find_reloads (insn, 1, spill_indirect_levels, live_known,
3855 spill_reg_order);
3858 if (n_reloads > 0)
3860 rtx next = NEXT_INSN (insn);
3861 rtx p;
3863 prev = PREV_INSN (insn);
3865 /* Now compute which reload regs to reload them into. Perhaps
3866 reusing reload regs from previous insns, or else output
3867 load insns to reload them. Maybe output store insns too.
3868 Record the choices of reload reg in reload_reg_rtx. */
3869 choose_reload_regs (chain);
3871 /* Merge any reloads that we didn't combine for fear of
3872 increasing the number of spill registers needed but now
3873 discover can be safely merged. */
3874 if (SMALL_REGISTER_CLASSES)
3875 merge_assigned_reloads (insn);
3877 /* Generate the insns to reload operands into or out of
3878 their reload regs. */
3879 emit_reload_insns (chain);
3881 /* Substitute the chosen reload regs from reload_reg_rtx
3882 into the insn's body (or perhaps into the bodies of other
3883 load and store insn that we just made for reloading
3884 and that we moved the structure into). */
3885 subst_reloads (insn);
3887 /* If this was an ASM, make sure that all the reload insns
3888 we have generated are valid. If not, give an error
3889 and delete them. */
3891 if (asm_noperands (PATTERN (insn)) >= 0)
3892 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3893 if (p != insn && INSN_P (p)
3894 && GET_CODE (PATTERN (p)) != USE
3895 && (recog_memoized (p) < 0
3896 || (extract_insn (p), ! constrain_operands (1))))
3898 error_for_asm (insn,
3899 "%<asm%> operand requires "
3900 "impossible reload");
3901 delete_insn (p);
3905 if (num_eliminable && chain->need_elim)
3906 update_eliminable_offsets ();
3908 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3909 is no longer validly lying around to save a future reload.
3910 Note that this does not detect pseudos that were reloaded
3911 for this insn in order to be stored in
3912 (obeying register constraints). That is correct; such reload
3913 registers ARE still valid. */
3914 note_stores (oldpat, forget_old_reloads_1, NULL);
3916 /* There may have been CLOBBER insns placed after INSN. So scan
3917 between INSN and NEXT and use them to forget old reloads. */
3918 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
3919 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
3920 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
3922 #ifdef AUTO_INC_DEC
3923 /* Likewise for regs altered by auto-increment in this insn.
3924 REG_INC notes have been changed by reloading:
3925 find_reloads_address_1 records substitutions for them,
3926 which have been performed by subst_reloads above. */
3927 for (i = n_reloads - 1; i >= 0; i--)
3929 rtx in_reg = rld[i].in_reg;
3930 if (in_reg)
3932 enum rtx_code code = GET_CODE (in_reg);
3933 /* PRE_INC / PRE_DEC will have the reload register ending up
3934 with the same value as the stack slot, but that doesn't
3935 hold true for POST_INC / POST_DEC. Either we have to
3936 convert the memory access to a true POST_INC / POST_DEC,
3937 or we can't use the reload register for inheritance. */
3938 if ((code == POST_INC || code == POST_DEC)
3939 && TEST_HARD_REG_BIT (reg_reloaded_valid,
3940 REGNO (rld[i].reg_rtx))
3941 /* Make sure it is the inc/dec pseudo, and not
3942 some other (e.g. output operand) pseudo. */
3943 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
3944 == REGNO (XEXP (in_reg, 0))))
3947 rtx reload_reg = rld[i].reg_rtx;
3948 enum machine_mode mode = GET_MODE (reload_reg);
3949 int n = 0;
3950 rtx p;
3952 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
3954 /* We really want to ignore REG_INC notes here, so
3955 use PATTERN (p) as argument to reg_set_p . */
3956 if (reg_set_p (reload_reg, PATTERN (p)))
3957 break;
3958 n = count_occurrences (PATTERN (p), reload_reg, 0);
3959 if (! n)
3960 continue;
3961 if (n == 1)
3963 n = validate_replace_rtx (reload_reg,
3964 gen_rtx_fmt_e (code,
3965 mode,
3966 reload_reg),
3969 /* We must also verify that the constraints
3970 are met after the replacement. */
3971 extract_insn (p);
3972 if (n)
3973 n = constrain_operands (1);
3974 else
3975 break;
3977 /* If the constraints were not met, then
3978 undo the replacement. */
3979 if (!n)
3981 validate_replace_rtx (gen_rtx_fmt_e (code,
3982 mode,
3983 reload_reg),
3984 reload_reg, p);
3985 break;
3989 break;
3991 if (n == 1)
3993 REG_NOTES (p)
3994 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
3995 REG_NOTES (p));
3996 /* Mark this as having an output reload so that the
3997 REG_INC processing code below won't invalidate
3998 the reload for inheritance. */
3999 SET_HARD_REG_BIT (reg_is_output_reload,
4000 REGNO (reload_reg));
4001 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4003 else
4004 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4005 NULL);
4007 else if ((code == PRE_INC || code == PRE_DEC)
4008 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4009 REGNO (rld[i].reg_rtx))
4010 /* Make sure it is the inc/dec pseudo, and not
4011 some other (e.g. output operand) pseudo. */
4012 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4013 == REGNO (XEXP (in_reg, 0))))
4015 SET_HARD_REG_BIT (reg_is_output_reload,
4016 REGNO (rld[i].reg_rtx));
4017 reg_has_output_reload[REGNO (XEXP (in_reg, 0))] = 1;
4021 /* If a pseudo that got a hard register is auto-incremented,
4022 we must purge records of copying it into pseudos without
4023 hard registers. */
4024 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4025 if (REG_NOTE_KIND (x) == REG_INC)
4027 /* See if this pseudo reg was reloaded in this insn.
4028 If so, its last-reload info is still valid
4029 because it is based on this insn's reload. */
4030 for (i = 0; i < n_reloads; i++)
4031 if (rld[i].out == XEXP (x, 0))
4032 break;
4034 if (i == n_reloads)
4035 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4037 #endif
4039 /* A reload reg's contents are unknown after a label. */
4040 if (LABEL_P (insn))
4041 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4043 /* Don't assume a reload reg is still good after a call insn
4044 if it is a call-used reg, or if it contains a value that will
4045 be partially clobbered by the call. */
4046 else if (CALL_P (insn))
4048 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4049 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4053 /* Clean up. */
4054 free (reg_last_reload_reg);
4055 free (reg_has_output_reload);
4058 /* Discard all record of any value reloaded from X,
4059 or reloaded in X from someplace else;
4060 unless X is an output reload reg of the current insn.
4062 X may be a hard reg (the reload reg)
4063 or it may be a pseudo reg that was reloaded from. */
4065 static void
4066 forget_old_reloads_1 (rtx x, rtx ignored ATTRIBUTE_UNUSED,
4067 void *data ATTRIBUTE_UNUSED)
4069 unsigned int regno;
4070 unsigned int nr;
4072 /* note_stores does give us subregs of hard regs,
4073 subreg_regno_offset requires a hard reg. */
4074 while (GET_CODE (x) == SUBREG)
4076 /* We ignore the subreg offset when calculating the regno,
4077 because we are using the entire underlying hard register
4078 below. */
4079 x = SUBREG_REG (x);
4082 if (!REG_P (x))
4083 return;
4085 regno = REGNO (x);
4087 if (regno >= FIRST_PSEUDO_REGISTER)
4088 nr = 1;
4089 else
4091 unsigned int i;
4093 nr = hard_regno_nregs[regno][GET_MODE (x)];
4094 /* Storing into a spilled-reg invalidates its contents.
4095 This can happen if a block-local pseudo is allocated to that reg
4096 and it wasn't spilled because this block's total need is 0.
4097 Then some insn might have an optional reload and use this reg. */
4098 for (i = 0; i < nr; i++)
4099 /* But don't do this if the reg actually serves as an output
4100 reload reg in the current instruction. */
4101 if (n_reloads == 0
4102 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4104 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4105 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, regno + i);
4106 spill_reg_store[regno + i] = 0;
4110 /* Since value of X has changed,
4111 forget any value previously copied from it. */
4113 while (nr-- > 0)
4114 /* But don't forget a copy if this is the output reload
4115 that establishes the copy's validity. */
4116 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
4117 reg_last_reload_reg[regno + nr] = 0;
4120 /* The following HARD_REG_SETs indicate when each hard register is
4121 used for a reload of various parts of the current insn. */
4123 /* If reg is unavailable for all reloads. */
4124 static HARD_REG_SET reload_reg_unavailable;
4125 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4126 static HARD_REG_SET reload_reg_used;
4127 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4128 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4129 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4130 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4131 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4132 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4133 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4134 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4135 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4136 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4137 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4138 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4139 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4140 static HARD_REG_SET reload_reg_used_in_op_addr;
4141 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4142 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4143 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4144 static HARD_REG_SET reload_reg_used_in_insn;
4145 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4146 static HARD_REG_SET reload_reg_used_in_other_addr;
4148 /* If reg is in use as a reload reg for any sort of reload. */
4149 static HARD_REG_SET reload_reg_used_at_all;
4151 /* If reg is use as an inherited reload. We just mark the first register
4152 in the group. */
4153 static HARD_REG_SET reload_reg_used_for_inherit;
4155 /* Records which hard regs are used in any way, either as explicit use or
4156 by being allocated to a pseudo during any point of the current insn. */
4157 static HARD_REG_SET reg_used_in_insn;
4159 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4160 TYPE. MODE is used to indicate how many consecutive regs are
4161 actually used. */
4163 static void
4164 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4165 enum machine_mode mode)
4167 unsigned int nregs = hard_regno_nregs[regno][mode];
4168 unsigned int i;
4170 for (i = regno; i < nregs + regno; i++)
4172 switch (type)
4174 case RELOAD_OTHER:
4175 SET_HARD_REG_BIT (reload_reg_used, i);
4176 break;
4178 case RELOAD_FOR_INPUT_ADDRESS:
4179 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4180 break;
4182 case RELOAD_FOR_INPADDR_ADDRESS:
4183 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4184 break;
4186 case RELOAD_FOR_OUTPUT_ADDRESS:
4187 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4188 break;
4190 case RELOAD_FOR_OUTADDR_ADDRESS:
4191 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4192 break;
4194 case RELOAD_FOR_OPERAND_ADDRESS:
4195 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4196 break;
4198 case RELOAD_FOR_OPADDR_ADDR:
4199 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4200 break;
4202 case RELOAD_FOR_OTHER_ADDRESS:
4203 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4204 break;
4206 case RELOAD_FOR_INPUT:
4207 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4208 break;
4210 case RELOAD_FOR_OUTPUT:
4211 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4212 break;
4214 case RELOAD_FOR_INSN:
4215 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4216 break;
4219 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4223 /* Similarly, but show REGNO is no longer in use for a reload. */
4225 static void
4226 clear_reload_reg_in_use (unsigned int regno, int opnum,
4227 enum reload_type type, enum machine_mode mode)
4229 unsigned int nregs = hard_regno_nregs[regno][mode];
4230 unsigned int start_regno, end_regno, r;
4231 int i;
4232 /* A complication is that for some reload types, inheritance might
4233 allow multiple reloads of the same types to share a reload register.
4234 We set check_opnum if we have to check only reloads with the same
4235 operand number, and check_any if we have to check all reloads. */
4236 int check_opnum = 0;
4237 int check_any = 0;
4238 HARD_REG_SET *used_in_set;
4240 switch (type)
4242 case RELOAD_OTHER:
4243 used_in_set = &reload_reg_used;
4244 break;
4246 case RELOAD_FOR_INPUT_ADDRESS:
4247 used_in_set = &reload_reg_used_in_input_addr[opnum];
4248 break;
4250 case RELOAD_FOR_INPADDR_ADDRESS:
4251 check_opnum = 1;
4252 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4253 break;
4255 case RELOAD_FOR_OUTPUT_ADDRESS:
4256 used_in_set = &reload_reg_used_in_output_addr[opnum];
4257 break;
4259 case RELOAD_FOR_OUTADDR_ADDRESS:
4260 check_opnum = 1;
4261 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4262 break;
4264 case RELOAD_FOR_OPERAND_ADDRESS:
4265 used_in_set = &reload_reg_used_in_op_addr;
4266 break;
4268 case RELOAD_FOR_OPADDR_ADDR:
4269 check_any = 1;
4270 used_in_set = &reload_reg_used_in_op_addr_reload;
4271 break;
4273 case RELOAD_FOR_OTHER_ADDRESS:
4274 used_in_set = &reload_reg_used_in_other_addr;
4275 check_any = 1;
4276 break;
4278 case RELOAD_FOR_INPUT:
4279 used_in_set = &reload_reg_used_in_input[opnum];
4280 break;
4282 case RELOAD_FOR_OUTPUT:
4283 used_in_set = &reload_reg_used_in_output[opnum];
4284 break;
4286 case RELOAD_FOR_INSN:
4287 used_in_set = &reload_reg_used_in_insn;
4288 break;
4289 default:
4290 gcc_unreachable ();
4292 /* We resolve conflicts with remaining reloads of the same type by
4293 excluding the intervals of reload registers by them from the
4294 interval of freed reload registers. Since we only keep track of
4295 one set of interval bounds, we might have to exclude somewhat
4296 more than what would be necessary if we used a HARD_REG_SET here.
4297 But this should only happen very infrequently, so there should
4298 be no reason to worry about it. */
4300 start_regno = regno;
4301 end_regno = regno + nregs;
4302 if (check_opnum || check_any)
4304 for (i = n_reloads - 1; i >= 0; i--)
4306 if (rld[i].when_needed == type
4307 && (check_any || rld[i].opnum == opnum)
4308 && rld[i].reg_rtx)
4310 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4311 unsigned int conflict_end
4312 = (conflict_start
4313 + hard_regno_nregs[conflict_start][rld[i].mode]);
4315 /* If there is an overlap with the first to-be-freed register,
4316 adjust the interval start. */
4317 if (conflict_start <= start_regno && conflict_end > start_regno)
4318 start_regno = conflict_end;
4319 /* Otherwise, if there is a conflict with one of the other
4320 to-be-freed registers, adjust the interval end. */
4321 if (conflict_start > start_regno && conflict_start < end_regno)
4322 end_regno = conflict_start;
4327 for (r = start_regno; r < end_regno; r++)
4328 CLEAR_HARD_REG_BIT (*used_in_set, r);
4331 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4332 specified by OPNUM and TYPE. */
4334 static int
4335 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4337 int i;
4339 /* In use for a RELOAD_OTHER means it's not available for anything. */
4340 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4341 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4342 return 0;
4344 switch (type)
4346 case RELOAD_OTHER:
4347 /* In use for anything means we can't use it for RELOAD_OTHER. */
4348 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4349 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4350 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4351 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4352 return 0;
4354 for (i = 0; i < reload_n_operands; i++)
4355 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4356 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4357 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4358 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4359 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4360 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4361 return 0;
4363 return 1;
4365 case RELOAD_FOR_INPUT:
4366 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4367 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4368 return 0;
4370 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4371 return 0;
4373 /* If it is used for some other input, can't use it. */
4374 for (i = 0; i < reload_n_operands; i++)
4375 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4376 return 0;
4378 /* If it is used in a later operand's address, can't use it. */
4379 for (i = opnum + 1; i < reload_n_operands; i++)
4380 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4381 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4382 return 0;
4384 return 1;
4386 case RELOAD_FOR_INPUT_ADDRESS:
4387 /* Can't use a register if it is used for an input address for this
4388 operand or used as an input in an earlier one. */
4389 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4390 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4391 return 0;
4393 for (i = 0; i < opnum; i++)
4394 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4395 return 0;
4397 return 1;
4399 case RELOAD_FOR_INPADDR_ADDRESS:
4400 /* Can't use a register if it is used for an input address
4401 for this operand or used as an input in an earlier
4402 one. */
4403 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4404 return 0;
4406 for (i = 0; i < opnum; i++)
4407 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4408 return 0;
4410 return 1;
4412 case RELOAD_FOR_OUTPUT_ADDRESS:
4413 /* Can't use a register if it is used for an output address for this
4414 operand or used as an output in this or a later operand. Note
4415 that multiple output operands are emitted in reverse order, so
4416 the conflicting ones are those with lower indices. */
4417 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4418 return 0;
4420 for (i = 0; i <= opnum; i++)
4421 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4422 return 0;
4424 return 1;
4426 case RELOAD_FOR_OUTADDR_ADDRESS:
4427 /* Can't use a register if it is used for an output address
4428 for this operand or used as an output in this or a
4429 later operand. Note that multiple output operands are
4430 emitted in reverse order, so the conflicting ones are
4431 those with lower indices. */
4432 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4433 return 0;
4435 for (i = 0; i <= opnum; i++)
4436 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4437 return 0;
4439 return 1;
4441 case RELOAD_FOR_OPERAND_ADDRESS:
4442 for (i = 0; i < reload_n_operands; i++)
4443 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4444 return 0;
4446 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4447 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4449 case RELOAD_FOR_OPADDR_ADDR:
4450 for (i = 0; i < reload_n_operands; i++)
4451 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4452 return 0;
4454 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4456 case RELOAD_FOR_OUTPUT:
4457 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4458 outputs, or an operand address for this or an earlier output.
4459 Note that multiple output operands are emitted in reverse order,
4460 so the conflicting ones are those with higher indices. */
4461 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4462 return 0;
4464 for (i = 0; i < reload_n_operands; i++)
4465 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4466 return 0;
4468 for (i = opnum; i < reload_n_operands; i++)
4469 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4470 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4471 return 0;
4473 return 1;
4475 case RELOAD_FOR_INSN:
4476 for (i = 0; i < reload_n_operands; i++)
4477 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4478 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4479 return 0;
4481 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4482 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4484 case RELOAD_FOR_OTHER_ADDRESS:
4485 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4487 default:
4488 gcc_unreachable ();
4492 /* Return 1 if the value in reload reg REGNO, as used by a reload
4493 needed for the part of the insn specified by OPNUM and TYPE,
4494 is still available in REGNO at the end of the insn.
4496 We can assume that the reload reg was already tested for availability
4497 at the time it is needed, and we should not check this again,
4498 in case the reg has already been marked in use. */
4500 static int
4501 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4503 int i;
4505 switch (type)
4507 case RELOAD_OTHER:
4508 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4509 its value must reach the end. */
4510 return 1;
4512 /* If this use is for part of the insn,
4513 its value reaches if no subsequent part uses the same register.
4514 Just like the above function, don't try to do this with lots
4515 of fallthroughs. */
4517 case RELOAD_FOR_OTHER_ADDRESS:
4518 /* Here we check for everything else, since these don't conflict
4519 with anything else and everything comes later. */
4521 for (i = 0; i < reload_n_operands; i++)
4522 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4523 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4524 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4525 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4526 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4527 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4528 return 0;
4530 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4531 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4532 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4533 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4535 case RELOAD_FOR_INPUT_ADDRESS:
4536 case RELOAD_FOR_INPADDR_ADDRESS:
4537 /* Similar, except that we check only for this and subsequent inputs
4538 and the address of only subsequent inputs and we do not need
4539 to check for RELOAD_OTHER objects since they are known not to
4540 conflict. */
4542 for (i = opnum; i < reload_n_operands; i++)
4543 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4544 return 0;
4546 for (i = opnum + 1; i < reload_n_operands; i++)
4547 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4548 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4549 return 0;
4551 for (i = 0; i < reload_n_operands; i++)
4552 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4553 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4554 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4555 return 0;
4557 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4558 return 0;
4560 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4561 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4562 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4564 case RELOAD_FOR_INPUT:
4565 /* Similar to input address, except we start at the next operand for
4566 both input and input address and we do not check for
4567 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4568 would conflict. */
4570 for (i = opnum + 1; i < reload_n_operands; i++)
4571 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4572 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4573 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4574 return 0;
4576 /* ... fall through ... */
4578 case RELOAD_FOR_OPERAND_ADDRESS:
4579 /* Check outputs and their addresses. */
4581 for (i = 0; i < reload_n_operands; i++)
4582 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4583 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4584 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4585 return 0;
4587 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4589 case RELOAD_FOR_OPADDR_ADDR:
4590 for (i = 0; i < reload_n_operands; i++)
4591 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4592 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4593 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4594 return 0;
4596 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4597 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4598 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4600 case RELOAD_FOR_INSN:
4601 /* These conflict with other outputs with RELOAD_OTHER. So
4602 we need only check for output addresses. */
4604 opnum = reload_n_operands;
4606 /* ... fall through ... */
4608 case RELOAD_FOR_OUTPUT:
4609 case RELOAD_FOR_OUTPUT_ADDRESS:
4610 case RELOAD_FOR_OUTADDR_ADDRESS:
4611 /* We already know these can't conflict with a later output. So the
4612 only thing to check are later output addresses.
4613 Note that multiple output operands are emitted in reverse order,
4614 so the conflicting ones are those with lower indices. */
4615 for (i = 0; i < opnum; i++)
4616 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4617 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4618 return 0;
4620 return 1;
4622 default:
4623 gcc_unreachable ();
4627 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4628 Return 0 otherwise.
4630 This function uses the same algorithm as reload_reg_free_p above. */
4632 static int
4633 reloads_conflict (int r1, int r2)
4635 enum reload_type r1_type = rld[r1].when_needed;
4636 enum reload_type r2_type = rld[r2].when_needed;
4637 int r1_opnum = rld[r1].opnum;
4638 int r2_opnum = rld[r2].opnum;
4640 /* RELOAD_OTHER conflicts with everything. */
4641 if (r2_type == RELOAD_OTHER)
4642 return 1;
4644 /* Otherwise, check conflicts differently for each type. */
4646 switch (r1_type)
4648 case RELOAD_FOR_INPUT:
4649 return (r2_type == RELOAD_FOR_INSN
4650 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4651 || r2_type == RELOAD_FOR_OPADDR_ADDR
4652 || r2_type == RELOAD_FOR_INPUT
4653 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4654 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4655 && r2_opnum > r1_opnum));
4657 case RELOAD_FOR_INPUT_ADDRESS:
4658 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4659 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4661 case RELOAD_FOR_INPADDR_ADDRESS:
4662 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4663 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4665 case RELOAD_FOR_OUTPUT_ADDRESS:
4666 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4667 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4669 case RELOAD_FOR_OUTADDR_ADDRESS:
4670 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4671 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4673 case RELOAD_FOR_OPERAND_ADDRESS:
4674 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4675 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4677 case RELOAD_FOR_OPADDR_ADDR:
4678 return (r2_type == RELOAD_FOR_INPUT
4679 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4681 case RELOAD_FOR_OUTPUT:
4682 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4683 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4684 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4685 && r2_opnum >= r1_opnum));
4687 case RELOAD_FOR_INSN:
4688 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4689 || r2_type == RELOAD_FOR_INSN
4690 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4692 case RELOAD_FOR_OTHER_ADDRESS:
4693 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4695 case RELOAD_OTHER:
4696 return 1;
4698 default:
4699 gcc_unreachable ();
4703 /* Indexed by reload number, 1 if incoming value
4704 inherited from previous insns. */
4705 static char reload_inherited[MAX_RELOADS];
4707 /* For an inherited reload, this is the insn the reload was inherited from,
4708 if we know it. Otherwise, this is 0. */
4709 static rtx reload_inheritance_insn[MAX_RELOADS];
4711 /* If nonzero, this is a place to get the value of the reload,
4712 rather than using reload_in. */
4713 static rtx reload_override_in[MAX_RELOADS];
4715 /* For each reload, the hard register number of the register used,
4716 or -1 if we did not need a register for this reload. */
4717 static int reload_spill_index[MAX_RELOADS];
4719 /* Subroutine of free_for_value_p, used to check a single register.
4720 START_REGNO is the starting regno of the full reload register
4721 (possibly comprising multiple hard registers) that we are considering. */
4723 static int
4724 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
4725 enum reload_type type, rtx value, rtx out,
4726 int reloadnum, int ignore_address_reloads)
4728 int time1;
4729 /* Set if we see an input reload that must not share its reload register
4730 with any new earlyclobber, but might otherwise share the reload
4731 register with an output or input-output reload. */
4732 int check_earlyclobber = 0;
4733 int i;
4734 int copy = 0;
4736 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4737 return 0;
4739 if (out == const0_rtx)
4741 copy = 1;
4742 out = NULL_RTX;
4745 /* We use some pseudo 'time' value to check if the lifetimes of the
4746 new register use would overlap with the one of a previous reload
4747 that is not read-only or uses a different value.
4748 The 'time' used doesn't have to be linear in any shape or form, just
4749 monotonic.
4750 Some reload types use different 'buckets' for each operand.
4751 So there are MAX_RECOG_OPERANDS different time values for each
4752 such reload type.
4753 We compute TIME1 as the time when the register for the prospective
4754 new reload ceases to be live, and TIME2 for each existing
4755 reload as the time when that the reload register of that reload
4756 becomes live.
4757 Where there is little to be gained by exact lifetime calculations,
4758 we just make conservative assumptions, i.e. a longer lifetime;
4759 this is done in the 'default:' cases. */
4760 switch (type)
4762 case RELOAD_FOR_OTHER_ADDRESS:
4763 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4764 time1 = copy ? 0 : 1;
4765 break;
4766 case RELOAD_OTHER:
4767 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4768 break;
4769 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4770 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4771 respectively, to the time values for these, we get distinct time
4772 values. To get distinct time values for each operand, we have to
4773 multiply opnum by at least three. We round that up to four because
4774 multiply by four is often cheaper. */
4775 case RELOAD_FOR_INPADDR_ADDRESS:
4776 time1 = opnum * 4 + 2;
4777 break;
4778 case RELOAD_FOR_INPUT_ADDRESS:
4779 time1 = opnum * 4 + 3;
4780 break;
4781 case RELOAD_FOR_INPUT:
4782 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4783 executes (inclusive). */
4784 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
4785 break;
4786 case RELOAD_FOR_OPADDR_ADDR:
4787 /* opnum * 4 + 4
4788 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
4789 time1 = MAX_RECOG_OPERANDS * 4 + 1;
4790 break;
4791 case RELOAD_FOR_OPERAND_ADDRESS:
4792 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
4793 is executed. */
4794 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
4795 break;
4796 case RELOAD_FOR_OUTADDR_ADDRESS:
4797 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
4798 break;
4799 case RELOAD_FOR_OUTPUT_ADDRESS:
4800 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
4801 break;
4802 default:
4803 time1 = MAX_RECOG_OPERANDS * 5 + 5;
4806 for (i = 0; i < n_reloads; i++)
4808 rtx reg = rld[i].reg_rtx;
4809 if (reg && REG_P (reg)
4810 && ((unsigned) regno - true_regnum (reg)
4811 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
4812 && i != reloadnum)
4814 rtx other_input = rld[i].in;
4816 /* If the other reload loads the same input value, that
4817 will not cause a conflict only if it's loading it into
4818 the same register. */
4819 if (true_regnum (reg) != start_regno)
4820 other_input = NULL_RTX;
4821 if (! other_input || ! rtx_equal_p (other_input, value)
4822 || rld[i].out || out)
4824 int time2;
4825 switch (rld[i].when_needed)
4827 case RELOAD_FOR_OTHER_ADDRESS:
4828 time2 = 0;
4829 break;
4830 case RELOAD_FOR_INPADDR_ADDRESS:
4831 /* find_reloads makes sure that a
4832 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
4833 by at most one - the first -
4834 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
4835 address reload is inherited, the address address reload
4836 goes away, so we can ignore this conflict. */
4837 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
4838 && ignore_address_reloads
4839 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
4840 Then the address address is still needed to store
4841 back the new address. */
4842 && ! rld[reloadnum].out)
4843 continue;
4844 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
4845 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
4846 reloads go away. */
4847 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4848 && ignore_address_reloads
4849 /* Unless we are reloading an auto_inc expression. */
4850 && ! rld[reloadnum].out)
4851 continue;
4852 time2 = rld[i].opnum * 4 + 2;
4853 break;
4854 case RELOAD_FOR_INPUT_ADDRESS:
4855 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
4856 && ignore_address_reloads
4857 && ! rld[reloadnum].out)
4858 continue;
4859 time2 = rld[i].opnum * 4 + 3;
4860 break;
4861 case RELOAD_FOR_INPUT:
4862 time2 = rld[i].opnum * 4 + 4;
4863 check_earlyclobber = 1;
4864 break;
4865 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
4866 == MAX_RECOG_OPERAND * 4 */
4867 case RELOAD_FOR_OPADDR_ADDR:
4868 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
4869 && ignore_address_reloads
4870 && ! rld[reloadnum].out)
4871 continue;
4872 time2 = MAX_RECOG_OPERANDS * 4 + 1;
4873 break;
4874 case RELOAD_FOR_OPERAND_ADDRESS:
4875 time2 = MAX_RECOG_OPERANDS * 4 + 2;
4876 check_earlyclobber = 1;
4877 break;
4878 case RELOAD_FOR_INSN:
4879 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4880 break;
4881 case RELOAD_FOR_OUTPUT:
4882 /* All RELOAD_FOR_OUTPUT reloads become live just after the
4883 instruction is executed. */
4884 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4885 break;
4886 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
4887 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
4888 value. */
4889 case RELOAD_FOR_OUTADDR_ADDRESS:
4890 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
4891 && ignore_address_reloads
4892 && ! rld[reloadnum].out)
4893 continue;
4894 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
4895 break;
4896 case RELOAD_FOR_OUTPUT_ADDRESS:
4897 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
4898 break;
4899 case RELOAD_OTHER:
4900 /* If there is no conflict in the input part, handle this
4901 like an output reload. */
4902 if (! rld[i].in || rtx_equal_p (other_input, value))
4904 time2 = MAX_RECOG_OPERANDS * 4 + 4;
4905 /* Earlyclobbered outputs must conflict with inputs. */
4906 if (earlyclobber_operand_p (rld[i].out))
4907 time2 = MAX_RECOG_OPERANDS * 4 + 3;
4909 break;
4911 time2 = 1;
4912 /* RELOAD_OTHER might be live beyond instruction execution,
4913 but this is not obvious when we set time2 = 1. So check
4914 here if there might be a problem with the new reload
4915 clobbering the register used by the RELOAD_OTHER. */
4916 if (out)
4917 return 0;
4918 break;
4919 default:
4920 return 0;
4922 if ((time1 >= time2
4923 && (! rld[i].in || rld[i].out
4924 || ! rtx_equal_p (other_input, value)))
4925 || (out && rld[reloadnum].out_reg
4926 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
4927 return 0;
4932 /* Earlyclobbered outputs must conflict with inputs. */
4933 if (check_earlyclobber && out && earlyclobber_operand_p (out))
4934 return 0;
4936 return 1;
4939 /* Return 1 if the value in reload reg REGNO, as used by a reload
4940 needed for the part of the insn specified by OPNUM and TYPE,
4941 may be used to load VALUE into it.
4943 MODE is the mode in which the register is used, this is needed to
4944 determine how many hard regs to test.
4946 Other read-only reloads with the same value do not conflict
4947 unless OUT is nonzero and these other reloads have to live while
4948 output reloads live.
4949 If OUT is CONST0_RTX, this is a special case: it means that the
4950 test should not be for using register REGNO as reload register, but
4951 for copying from register REGNO into the reload register.
4953 RELOADNUM is the number of the reload we want to load this value for;
4954 a reload does not conflict with itself.
4956 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
4957 reloads that load an address for the very reload we are considering.
4959 The caller has to make sure that there is no conflict with the return
4960 register. */
4962 static int
4963 free_for_value_p (int regno, enum machine_mode mode, int opnum,
4964 enum reload_type type, rtx value, rtx out, int reloadnum,
4965 int ignore_address_reloads)
4967 int nregs = hard_regno_nregs[regno][mode];
4968 while (nregs-- > 0)
4969 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
4970 value, out, reloadnum,
4971 ignore_address_reloads))
4972 return 0;
4973 return 1;
4976 /* Return nonzero if the rtx X is invariant over the current function. */
4977 /* ??? Actually, the places where we use this expect exactly what is
4978 tested here, and not everything that is function invariant. In
4979 particular, the frame pointer and arg pointer are special cased;
4980 pic_offset_table_rtx is not, and we must not spill these things to
4981 memory. */
4984 function_invariant_p (rtx x)
4986 if (CONSTANT_P (x))
4987 return 1;
4988 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
4989 return 1;
4990 if (GET_CODE (x) == PLUS
4991 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
4992 && CONSTANT_P (XEXP (x, 1)))
4993 return 1;
4994 return 0;
4997 /* Determine whether the reload reg X overlaps any rtx'es used for
4998 overriding inheritance. Return nonzero if so. */
5000 static int
5001 conflicts_with_override (rtx x)
5003 int i;
5004 for (i = 0; i < n_reloads; i++)
5005 if (reload_override_in[i]
5006 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5007 return 1;
5008 return 0;
5011 /* Give an error message saying we failed to find a reload for INSN,
5012 and clear out reload R. */
5013 static void
5014 failed_reload (rtx insn, int r)
5016 if (asm_noperands (PATTERN (insn)) < 0)
5017 /* It's the compiler's fault. */
5018 fatal_insn ("could not find a spill register", insn);
5020 /* It's the user's fault; the operand's mode and constraint
5021 don't match. Disable this reload so we don't crash in final. */
5022 error_for_asm (insn,
5023 "%<asm%> operand constraint incompatible with operand size");
5024 rld[r].in = 0;
5025 rld[r].out = 0;
5026 rld[r].reg_rtx = 0;
5027 rld[r].optional = 1;
5028 rld[r].secondary_p = 1;
5031 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5032 for reload R. If it's valid, get an rtx for it. Return nonzero if
5033 successful. */
5034 static int
5035 set_reload_reg (int i, int r)
5037 int regno;
5038 rtx reg = spill_reg_rtx[i];
5040 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5041 spill_reg_rtx[i] = reg
5042 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5044 regno = true_regnum (reg);
5046 /* Detect when the reload reg can't hold the reload mode.
5047 This used to be one `if', but Sequent compiler can't handle that. */
5048 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5050 enum machine_mode test_mode = VOIDmode;
5051 if (rld[r].in)
5052 test_mode = GET_MODE (rld[r].in);
5053 /* If rld[r].in has VOIDmode, it means we will load it
5054 in whatever mode the reload reg has: to wit, rld[r].mode.
5055 We have already tested that for validity. */
5056 /* Aside from that, we need to test that the expressions
5057 to reload from or into have modes which are valid for this
5058 reload register. Otherwise the reload insns would be invalid. */
5059 if (! (rld[r].in != 0 && test_mode != VOIDmode
5060 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5061 if (! (rld[r].out != 0
5062 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5064 /* The reg is OK. */
5065 last_spill_reg = i;
5067 /* Mark as in use for this insn the reload regs we use
5068 for this. */
5069 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5070 rld[r].when_needed, rld[r].mode);
5072 rld[r].reg_rtx = reg;
5073 reload_spill_index[r] = spill_regs[i];
5074 return 1;
5077 return 0;
5080 /* Find a spill register to use as a reload register for reload R.
5081 LAST_RELOAD is nonzero if this is the last reload for the insn being
5082 processed.
5084 Set rld[R].reg_rtx to the register allocated.
5086 We return 1 if successful, or 0 if we couldn't find a spill reg and
5087 we didn't change anything. */
5089 static int
5090 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5091 int last_reload)
5093 int i, pass, count;
5095 /* If we put this reload ahead, thinking it is a group,
5096 then insist on finding a group. Otherwise we can grab a
5097 reg that some other reload needs.
5098 (That can happen when we have a 68000 DATA_OR_FP_REG
5099 which is a group of data regs or one fp reg.)
5100 We need not be so restrictive if there are no more reloads
5101 for this insn.
5103 ??? Really it would be nicer to have smarter handling
5104 for that kind of reg class, where a problem like this is normal.
5105 Perhaps those classes should be avoided for reloading
5106 by use of more alternatives. */
5108 int force_group = rld[r].nregs > 1 && ! last_reload;
5110 /* If we want a single register and haven't yet found one,
5111 take any reg in the right class and not in use.
5112 If we want a consecutive group, here is where we look for it.
5114 We use two passes so we can first look for reload regs to
5115 reuse, which are already in use for other reloads in this insn,
5116 and only then use additional registers.
5117 I think that maximizing reuse is needed to make sure we don't
5118 run out of reload regs. Suppose we have three reloads, and
5119 reloads A and B can share regs. These need two regs.
5120 Suppose A and B are given different regs.
5121 That leaves none for C. */
5122 for (pass = 0; pass < 2; pass++)
5124 /* I is the index in spill_regs.
5125 We advance it round-robin between insns to use all spill regs
5126 equally, so that inherited reloads have a chance
5127 of leapfrogging each other. */
5129 i = last_spill_reg;
5131 for (count = 0; count < n_spills; count++)
5133 int class = (int) rld[r].class;
5134 int regnum;
5136 i++;
5137 if (i >= n_spills)
5138 i -= n_spills;
5139 regnum = spill_regs[i];
5141 if ((reload_reg_free_p (regnum, rld[r].opnum,
5142 rld[r].when_needed)
5143 || (rld[r].in
5144 /* We check reload_reg_used to make sure we
5145 don't clobber the return register. */
5146 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5147 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5148 rld[r].when_needed, rld[r].in,
5149 rld[r].out, r, 1)))
5150 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5151 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5152 /* Look first for regs to share, then for unshared. But
5153 don't share regs used for inherited reloads; they are
5154 the ones we want to preserve. */
5155 && (pass
5156 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5157 regnum)
5158 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5159 regnum))))
5161 int nr = hard_regno_nregs[regnum][rld[r].mode];
5162 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5163 (on 68000) got us two FP regs. If NR is 1,
5164 we would reject both of them. */
5165 if (force_group)
5166 nr = rld[r].nregs;
5167 /* If we need only one reg, we have already won. */
5168 if (nr == 1)
5170 /* But reject a single reg if we demand a group. */
5171 if (force_group)
5172 continue;
5173 break;
5175 /* Otherwise check that as many consecutive regs as we need
5176 are available here. */
5177 while (nr > 1)
5179 int regno = regnum + nr - 1;
5180 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5181 && spill_reg_order[regno] >= 0
5182 && reload_reg_free_p (regno, rld[r].opnum,
5183 rld[r].when_needed)))
5184 break;
5185 nr--;
5187 if (nr == 1)
5188 break;
5192 /* If we found something on pass 1, omit pass 2. */
5193 if (count < n_spills)
5194 break;
5197 /* We should have found a spill register by now. */
5198 if (count >= n_spills)
5199 return 0;
5201 /* I is the index in SPILL_REG_RTX of the reload register we are to
5202 allocate. Get an rtx for it and find its register number. */
5204 return set_reload_reg (i, r);
5207 /* Initialize all the tables needed to allocate reload registers.
5208 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5209 is the array we use to restore the reg_rtx field for every reload. */
5211 static void
5212 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5214 int i;
5216 for (i = 0; i < n_reloads; i++)
5217 rld[i].reg_rtx = save_reload_reg_rtx[i];
5219 memset (reload_inherited, 0, MAX_RELOADS);
5220 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5221 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5223 CLEAR_HARD_REG_SET (reload_reg_used);
5224 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5225 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5226 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5227 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5228 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5230 CLEAR_HARD_REG_SET (reg_used_in_insn);
5232 HARD_REG_SET tmp;
5233 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5234 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5235 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5236 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5237 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5238 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5241 for (i = 0; i < reload_n_operands; i++)
5243 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5244 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5245 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5246 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5247 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5248 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5251 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5253 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5255 for (i = 0; i < n_reloads; i++)
5256 /* If we have already decided to use a certain register,
5257 don't use it in another way. */
5258 if (rld[i].reg_rtx)
5259 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5260 rld[i].when_needed, rld[i].mode);
5263 /* Assign hard reg targets for the pseudo-registers we must reload
5264 into hard regs for this insn.
5265 Also output the instructions to copy them in and out of the hard regs.
5267 For machines with register classes, we are responsible for
5268 finding a reload reg in the proper class. */
5270 static void
5271 choose_reload_regs (struct insn_chain *chain)
5273 rtx insn = chain->insn;
5274 int i, j;
5275 unsigned int max_group_size = 1;
5276 enum reg_class group_class = NO_REGS;
5277 int pass, win, inheritance;
5279 rtx save_reload_reg_rtx[MAX_RELOADS];
5281 /* In order to be certain of getting the registers we need,
5282 we must sort the reloads into order of increasing register class.
5283 Then our grabbing of reload registers will parallel the process
5284 that provided the reload registers.
5286 Also note whether any of the reloads wants a consecutive group of regs.
5287 If so, record the maximum size of the group desired and what
5288 register class contains all the groups needed by this insn. */
5290 for (j = 0; j < n_reloads; j++)
5292 reload_order[j] = j;
5293 reload_spill_index[j] = -1;
5295 if (rld[j].nregs > 1)
5297 max_group_size = MAX (rld[j].nregs, max_group_size);
5298 group_class
5299 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5302 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5305 if (n_reloads > 1)
5306 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5308 /* If -O, try first with inheritance, then turning it off.
5309 If not -O, don't do inheritance.
5310 Using inheritance when not optimizing leads to paradoxes
5311 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5312 because one side of the comparison might be inherited. */
5313 win = 0;
5314 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5316 choose_reload_regs_init (chain, save_reload_reg_rtx);
5318 /* Process the reloads in order of preference just found.
5319 Beyond this point, subregs can be found in reload_reg_rtx.
5321 This used to look for an existing reloaded home for all of the
5322 reloads, and only then perform any new reloads. But that could lose
5323 if the reloads were done out of reg-class order because a later
5324 reload with a looser constraint might have an old home in a register
5325 needed by an earlier reload with a tighter constraint.
5327 To solve this, we make two passes over the reloads, in the order
5328 described above. In the first pass we try to inherit a reload
5329 from a previous insn. If there is a later reload that needs a
5330 class that is a proper subset of the class being processed, we must
5331 also allocate a spill register during the first pass.
5333 Then make a second pass over the reloads to allocate any reloads
5334 that haven't been given registers yet. */
5336 for (j = 0; j < n_reloads; j++)
5338 int r = reload_order[j];
5339 rtx search_equiv = NULL_RTX;
5341 /* Ignore reloads that got marked inoperative. */
5342 if (rld[r].out == 0 && rld[r].in == 0
5343 && ! rld[r].secondary_p)
5344 continue;
5346 /* If find_reloads chose to use reload_in or reload_out as a reload
5347 register, we don't need to chose one. Otherwise, try even if it
5348 found one since we might save an insn if we find the value lying
5349 around.
5350 Try also when reload_in is a pseudo without a hard reg. */
5351 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5352 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5353 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5354 && !MEM_P (rld[r].in)
5355 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5356 continue;
5358 #if 0 /* No longer needed for correct operation.
5359 It might give better code, or might not; worth an experiment? */
5360 /* If this is an optional reload, we can't inherit from earlier insns
5361 until we are sure that any non-optional reloads have been allocated.
5362 The following code takes advantage of the fact that optional reloads
5363 are at the end of reload_order. */
5364 if (rld[r].optional != 0)
5365 for (i = 0; i < j; i++)
5366 if ((rld[reload_order[i]].out != 0
5367 || rld[reload_order[i]].in != 0
5368 || rld[reload_order[i]].secondary_p)
5369 && ! rld[reload_order[i]].optional
5370 && rld[reload_order[i]].reg_rtx == 0)
5371 allocate_reload_reg (chain, reload_order[i], 0);
5372 #endif
5374 /* First see if this pseudo is already available as reloaded
5375 for a previous insn. We cannot try to inherit for reloads
5376 that are smaller than the maximum number of registers needed
5377 for groups unless the register we would allocate cannot be used
5378 for the groups.
5380 We could check here to see if this is a secondary reload for
5381 an object that is already in a register of the desired class.
5382 This would avoid the need for the secondary reload register.
5383 But this is complex because we can't easily determine what
5384 objects might want to be loaded via this reload. So let a
5385 register be allocated here. In `emit_reload_insns' we suppress
5386 one of the loads in the case described above. */
5388 if (inheritance)
5390 int byte = 0;
5391 int regno = -1;
5392 enum machine_mode mode = VOIDmode;
5394 if (rld[r].in == 0)
5396 else if (REG_P (rld[r].in))
5398 regno = REGNO (rld[r].in);
5399 mode = GET_MODE (rld[r].in);
5401 else if (REG_P (rld[r].in_reg))
5403 regno = REGNO (rld[r].in_reg);
5404 mode = GET_MODE (rld[r].in_reg);
5406 else if (GET_CODE (rld[r].in_reg) == SUBREG
5407 && REG_P (SUBREG_REG (rld[r].in_reg)))
5409 byte = SUBREG_BYTE (rld[r].in_reg);
5410 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5411 if (regno < FIRST_PSEUDO_REGISTER)
5412 regno = subreg_regno (rld[r].in_reg);
5413 mode = GET_MODE (rld[r].in_reg);
5415 #ifdef AUTO_INC_DEC
5416 else if ((GET_CODE (rld[r].in_reg) == PRE_INC
5417 || GET_CODE (rld[r].in_reg) == PRE_DEC
5418 || GET_CODE (rld[r].in_reg) == POST_INC
5419 || GET_CODE (rld[r].in_reg) == POST_DEC)
5420 && REG_P (XEXP (rld[r].in_reg, 0)))
5422 regno = REGNO (XEXP (rld[r].in_reg, 0));
5423 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5424 rld[r].out = rld[r].in;
5426 #endif
5427 #if 0
5428 /* This won't work, since REGNO can be a pseudo reg number.
5429 Also, it takes much more hair to keep track of all the things
5430 that can invalidate an inherited reload of part of a pseudoreg. */
5431 else if (GET_CODE (rld[r].in) == SUBREG
5432 && REG_P (SUBREG_REG (rld[r].in)))
5433 regno = subreg_regno (rld[r].in);
5434 #endif
5436 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
5438 enum reg_class class = rld[r].class, last_class;
5439 rtx last_reg = reg_last_reload_reg[regno];
5440 enum machine_mode need_mode;
5442 i = REGNO (last_reg);
5443 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5444 last_class = REGNO_REG_CLASS (i);
5446 if (byte == 0)
5447 need_mode = mode;
5448 else
5449 need_mode
5450 = smallest_mode_for_size (GET_MODE_BITSIZE (mode)
5451 + byte * BITS_PER_UNIT,
5452 GET_MODE_CLASS (mode));
5454 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5455 >= GET_MODE_SIZE (need_mode))
5456 #ifdef CANNOT_CHANGE_MODE_CLASS
5457 /* Verify that the register in "i" can be obtained
5458 from LAST_REG. */
5459 && !REG_CANNOT_CHANGE_MODE_P (REGNO (last_reg),
5460 GET_MODE (last_reg),
5461 mode)
5462 #endif
5463 && reg_reloaded_contents[i] == regno
5464 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5465 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5466 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5467 /* Even if we can't use this register as a reload
5468 register, we might use it for reload_override_in,
5469 if copying it to the desired class is cheap
5470 enough. */
5471 || ((REGISTER_MOVE_COST (mode, last_class, class)
5472 < MEMORY_MOVE_COST (mode, class, 1))
5473 #ifdef SECONDARY_INPUT_RELOAD_CLASS
5474 && (SECONDARY_INPUT_RELOAD_CLASS (class, mode,
5475 last_reg)
5476 == NO_REGS)
5477 #endif
5478 #ifdef SECONDARY_MEMORY_NEEDED
5479 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5480 mode)
5481 #endif
5484 && (rld[r].nregs == max_group_size
5485 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5487 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5488 rld[r].when_needed, rld[r].in,
5489 const0_rtx, r, 1))
5491 /* If a group is needed, verify that all the subsequent
5492 registers still have their values intact. */
5493 int nr = hard_regno_nregs[i][rld[r].mode];
5494 int k;
5496 for (k = 1; k < nr; k++)
5497 if (reg_reloaded_contents[i + k] != regno
5498 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5499 break;
5501 if (k == nr)
5503 int i1;
5504 int bad_for_class;
5506 last_reg = (GET_MODE (last_reg) == mode
5507 ? last_reg : gen_rtx_REG (mode, i));
5509 bad_for_class = 0;
5510 for (k = 0; k < nr; k++)
5511 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5512 i+k);
5514 /* We found a register that contains the
5515 value we need. If this register is the
5516 same as an `earlyclobber' operand of the
5517 current insn, just mark it as a place to
5518 reload from since we can't use it as the
5519 reload register itself. */
5521 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5522 if (reg_overlap_mentioned_for_reload_p
5523 (reg_last_reload_reg[regno],
5524 reload_earlyclobbers[i1]))
5525 break;
5527 if (i1 != n_earlyclobbers
5528 || ! (free_for_value_p (i, rld[r].mode,
5529 rld[r].opnum,
5530 rld[r].when_needed, rld[r].in,
5531 rld[r].out, r, 1))
5532 /* Don't use it if we'd clobber a pseudo reg. */
5533 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5534 && rld[r].out
5535 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5536 /* Don't clobber the frame pointer. */
5537 || (i == HARD_FRAME_POINTER_REGNUM
5538 && frame_pointer_needed
5539 && rld[r].out)
5540 /* Don't really use the inherited spill reg
5541 if we need it wider than we've got it. */
5542 || (GET_MODE_SIZE (rld[r].mode)
5543 > GET_MODE_SIZE (mode))
5544 || bad_for_class
5546 /* If find_reloads chose reload_out as reload
5547 register, stay with it - that leaves the
5548 inherited register for subsequent reloads. */
5549 || (rld[r].out && rld[r].reg_rtx
5550 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5552 if (! rld[r].optional)
5554 reload_override_in[r] = last_reg;
5555 reload_inheritance_insn[r]
5556 = reg_reloaded_insn[i];
5559 else
5561 int k;
5562 /* We can use this as a reload reg. */
5563 /* Mark the register as in use for this part of
5564 the insn. */
5565 mark_reload_reg_in_use (i,
5566 rld[r].opnum,
5567 rld[r].when_needed,
5568 rld[r].mode);
5569 rld[r].reg_rtx = last_reg;
5570 reload_inherited[r] = 1;
5571 reload_inheritance_insn[r]
5572 = reg_reloaded_insn[i];
5573 reload_spill_index[r] = i;
5574 for (k = 0; k < nr; k++)
5575 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5576 i + k);
5583 /* Here's another way to see if the value is already lying around. */
5584 if (inheritance
5585 && rld[r].in != 0
5586 && ! reload_inherited[r]
5587 && rld[r].out == 0
5588 && (CONSTANT_P (rld[r].in)
5589 || GET_CODE (rld[r].in) == PLUS
5590 || REG_P (rld[r].in)
5591 || MEM_P (rld[r].in))
5592 && (rld[r].nregs == max_group_size
5593 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5594 search_equiv = rld[r].in;
5595 /* If this is an output reload from a simple move insn, look
5596 if an equivalence for the input is available. */
5597 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5599 rtx set = single_set (insn);
5601 if (set
5602 && rtx_equal_p (rld[r].out, SET_DEST (set))
5603 && CONSTANT_P (SET_SRC (set)))
5604 search_equiv = SET_SRC (set);
5607 if (search_equiv)
5609 rtx equiv
5610 = find_equiv_reg (search_equiv, insn, rld[r].class,
5611 -1, NULL, 0, rld[r].mode);
5612 int regno = 0;
5614 if (equiv != 0)
5616 if (REG_P (equiv))
5617 regno = REGNO (equiv);
5618 else
5620 /* This must be a SUBREG of a hard register.
5621 Make a new REG since this might be used in an
5622 address and not all machines support SUBREGs
5623 there. */
5624 gcc_assert (GET_CODE (equiv) == SUBREG);
5625 regno = subreg_regno (equiv);
5626 equiv = gen_rtx_REG (rld[r].mode, regno);
5627 /* If we choose EQUIV as the reload register, but the
5628 loop below decides to cancel the inheritance, we'll
5629 end up reloading EQUIV in rld[r].mode, not the mode
5630 it had originally. That isn't safe when EQUIV isn't
5631 available as a spill register since its value might
5632 still be live at this point. */
5633 for (i = regno; i < regno + (int) rld[r].nregs; i++)
5634 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
5635 equiv = 0;
5639 /* If we found a spill reg, reject it unless it is free
5640 and of the desired class. */
5641 if (equiv != 0)
5643 int regs_used = 0;
5644 int bad_for_class = 0;
5645 int max_regno = regno + rld[r].nregs;
5647 for (i = regno; i < max_regno; i++)
5649 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
5651 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5655 if ((regs_used
5656 && ! free_for_value_p (regno, rld[r].mode,
5657 rld[r].opnum, rld[r].when_needed,
5658 rld[r].in, rld[r].out, r, 1))
5659 || bad_for_class)
5660 equiv = 0;
5663 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5664 equiv = 0;
5666 /* We found a register that contains the value we need.
5667 If this register is the same as an `earlyclobber' operand
5668 of the current insn, just mark it as a place to reload from
5669 since we can't use it as the reload register itself. */
5671 if (equiv != 0)
5672 for (i = 0; i < n_earlyclobbers; i++)
5673 if (reg_overlap_mentioned_for_reload_p (equiv,
5674 reload_earlyclobbers[i]))
5676 if (! rld[r].optional)
5677 reload_override_in[r] = equiv;
5678 equiv = 0;
5679 break;
5682 /* If the equiv register we have found is explicitly clobbered
5683 in the current insn, it depends on the reload type if we
5684 can use it, use it for reload_override_in, or not at all.
5685 In particular, we then can't use EQUIV for a
5686 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5688 if (equiv != 0)
5690 if (regno_clobbered_p (regno, insn, rld[r].mode, 0))
5691 switch (rld[r].when_needed)
5693 case RELOAD_FOR_OTHER_ADDRESS:
5694 case RELOAD_FOR_INPADDR_ADDRESS:
5695 case RELOAD_FOR_INPUT_ADDRESS:
5696 case RELOAD_FOR_OPADDR_ADDR:
5697 break;
5698 case RELOAD_OTHER:
5699 case RELOAD_FOR_INPUT:
5700 case RELOAD_FOR_OPERAND_ADDRESS:
5701 if (! rld[r].optional)
5702 reload_override_in[r] = equiv;
5703 /* Fall through. */
5704 default:
5705 equiv = 0;
5706 break;
5708 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5709 switch (rld[r].when_needed)
5711 case RELOAD_FOR_OTHER_ADDRESS:
5712 case RELOAD_FOR_INPADDR_ADDRESS:
5713 case RELOAD_FOR_INPUT_ADDRESS:
5714 case RELOAD_FOR_OPADDR_ADDR:
5715 case RELOAD_FOR_OPERAND_ADDRESS:
5716 case RELOAD_FOR_INPUT:
5717 break;
5718 case RELOAD_OTHER:
5719 if (! rld[r].optional)
5720 reload_override_in[r] = equiv;
5721 /* Fall through. */
5722 default:
5723 equiv = 0;
5724 break;
5728 /* If we found an equivalent reg, say no code need be generated
5729 to load it, and use it as our reload reg. */
5730 if (equiv != 0
5731 && (regno != HARD_FRAME_POINTER_REGNUM
5732 || !frame_pointer_needed))
5734 int nr = hard_regno_nregs[regno][rld[r].mode];
5735 int k;
5736 rld[r].reg_rtx = equiv;
5737 reload_inherited[r] = 1;
5739 /* If reg_reloaded_valid is not set for this register,
5740 there might be a stale spill_reg_store lying around.
5741 We must clear it, since otherwise emit_reload_insns
5742 might delete the store. */
5743 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5744 spill_reg_store[regno] = NULL_RTX;
5745 /* If any of the hard registers in EQUIV are spill
5746 registers, mark them as in use for this insn. */
5747 for (k = 0; k < nr; k++)
5749 i = spill_reg_order[regno + k];
5750 if (i >= 0)
5752 mark_reload_reg_in_use (regno, rld[r].opnum,
5753 rld[r].when_needed,
5754 rld[r].mode);
5755 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5756 regno + k);
5762 /* If we found a register to use already, or if this is an optional
5763 reload, we are done. */
5764 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5765 continue;
5767 #if 0
5768 /* No longer needed for correct operation. Might or might
5769 not give better code on the average. Want to experiment? */
5771 /* See if there is a later reload that has a class different from our
5772 class that intersects our class or that requires less register
5773 than our reload. If so, we must allocate a register to this
5774 reload now, since that reload might inherit a previous reload
5775 and take the only available register in our class. Don't do this
5776 for optional reloads since they will force all previous reloads
5777 to be allocated. Also don't do this for reloads that have been
5778 turned off. */
5780 for (i = j + 1; i < n_reloads; i++)
5782 int s = reload_order[i];
5784 if ((rld[s].in == 0 && rld[s].out == 0
5785 && ! rld[s].secondary_p)
5786 || rld[s].optional)
5787 continue;
5789 if ((rld[s].class != rld[r].class
5790 && reg_classes_intersect_p (rld[r].class,
5791 rld[s].class))
5792 || rld[s].nregs < rld[r].nregs)
5793 break;
5796 if (i == n_reloads)
5797 continue;
5799 allocate_reload_reg (chain, r, j == n_reloads - 1);
5800 #endif
5803 /* Now allocate reload registers for anything non-optional that
5804 didn't get one yet. */
5805 for (j = 0; j < n_reloads; j++)
5807 int r = reload_order[j];
5809 /* Ignore reloads that got marked inoperative. */
5810 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
5811 continue;
5813 /* Skip reloads that already have a register allocated or are
5814 optional. */
5815 if (rld[r].reg_rtx != 0 || rld[r].optional)
5816 continue;
5818 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
5819 break;
5822 /* If that loop got all the way, we have won. */
5823 if (j == n_reloads)
5825 win = 1;
5826 break;
5829 /* Loop around and try without any inheritance. */
5832 if (! win)
5834 /* First undo everything done by the failed attempt
5835 to allocate with inheritance. */
5836 choose_reload_regs_init (chain, save_reload_reg_rtx);
5838 /* Some sanity tests to verify that the reloads found in the first
5839 pass are identical to the ones we have now. */
5840 gcc_assert (chain->n_reloads == n_reloads);
5842 for (i = 0; i < n_reloads; i++)
5844 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
5845 continue;
5846 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
5847 for (j = 0; j < n_spills; j++)
5848 if (spill_regs[j] == chain->rld[i].regno)
5849 if (! set_reload_reg (j, i))
5850 failed_reload (chain->insn, i);
5854 /* If we thought we could inherit a reload, because it seemed that
5855 nothing else wanted the same reload register earlier in the insn,
5856 verify that assumption, now that all reloads have been assigned.
5857 Likewise for reloads where reload_override_in has been set. */
5859 /* If doing expensive optimizations, do one preliminary pass that doesn't
5860 cancel any inheritance, but removes reloads that have been needed only
5861 for reloads that we know can be inherited. */
5862 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
5864 for (j = 0; j < n_reloads; j++)
5866 int r = reload_order[j];
5867 rtx check_reg;
5868 if (reload_inherited[r] && rld[r].reg_rtx)
5869 check_reg = rld[r].reg_rtx;
5870 else if (reload_override_in[r]
5871 && (REG_P (reload_override_in[r])
5872 || GET_CODE (reload_override_in[r]) == SUBREG))
5873 check_reg = reload_override_in[r];
5874 else
5875 continue;
5876 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
5877 rld[r].opnum, rld[r].when_needed, rld[r].in,
5878 (reload_inherited[r]
5879 ? rld[r].out : const0_rtx),
5880 r, 1))
5882 if (pass)
5883 continue;
5884 reload_inherited[r] = 0;
5885 reload_override_in[r] = 0;
5887 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
5888 reload_override_in, then we do not need its related
5889 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
5890 likewise for other reload types.
5891 We handle this by removing a reload when its only replacement
5892 is mentioned in reload_in of the reload we are going to inherit.
5893 A special case are auto_inc expressions; even if the input is
5894 inherited, we still need the address for the output. We can
5895 recognize them because they have RELOAD_OUT set to RELOAD_IN.
5896 If we succeeded removing some reload and we are doing a preliminary
5897 pass just to remove such reloads, make another pass, since the
5898 removal of one reload might allow us to inherit another one. */
5899 else if (rld[r].in
5900 && rld[r].out != rld[r].in
5901 && remove_address_replacements (rld[r].in) && pass)
5902 pass = 2;
5906 /* Now that reload_override_in is known valid,
5907 actually override reload_in. */
5908 for (j = 0; j < n_reloads; j++)
5909 if (reload_override_in[j])
5910 rld[j].in = reload_override_in[j];
5912 /* If this reload won't be done because it has been canceled or is
5913 optional and not inherited, clear reload_reg_rtx so other
5914 routines (such as subst_reloads) don't get confused. */
5915 for (j = 0; j < n_reloads; j++)
5916 if (rld[j].reg_rtx != 0
5917 && ((rld[j].optional && ! reload_inherited[j])
5918 || (rld[j].in == 0 && rld[j].out == 0
5919 && ! rld[j].secondary_p)))
5921 int regno = true_regnum (rld[j].reg_rtx);
5923 if (spill_reg_order[regno] >= 0)
5924 clear_reload_reg_in_use (regno, rld[j].opnum,
5925 rld[j].when_needed, rld[j].mode);
5926 rld[j].reg_rtx = 0;
5927 reload_spill_index[j] = -1;
5930 /* Record which pseudos and which spill regs have output reloads. */
5931 for (j = 0; j < n_reloads; j++)
5933 int r = reload_order[j];
5935 i = reload_spill_index[r];
5937 /* I is nonneg if this reload uses a register.
5938 If rld[r].reg_rtx is 0, this is an optional reload
5939 that we opted to ignore. */
5940 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
5941 && rld[r].reg_rtx != 0)
5943 int nregno = REGNO (rld[r].out_reg);
5944 int nr = 1;
5946 if (nregno < FIRST_PSEUDO_REGISTER)
5947 nr = hard_regno_nregs[nregno][rld[r].mode];
5949 while (--nr >= 0)
5950 reg_has_output_reload[nregno + nr] = 1;
5952 if (i >= 0)
5954 nr = hard_regno_nregs[i][rld[r].mode];
5955 while (--nr >= 0)
5956 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
5959 gcc_assert (rld[r].when_needed == RELOAD_OTHER
5960 || rld[r].when_needed == RELOAD_FOR_OUTPUT
5961 || rld[r].when_needed == RELOAD_FOR_INSN);
5966 /* Deallocate the reload register for reload R. This is called from
5967 remove_address_replacements. */
5969 void
5970 deallocate_reload_reg (int r)
5972 int regno;
5974 if (! rld[r].reg_rtx)
5975 return;
5976 regno = true_regnum (rld[r].reg_rtx);
5977 rld[r].reg_rtx = 0;
5978 if (spill_reg_order[regno] >= 0)
5979 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
5980 rld[r].mode);
5981 reload_spill_index[r] = -1;
5984 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
5985 reloads of the same item for fear that we might not have enough reload
5986 registers. However, normally they will get the same reload register
5987 and hence actually need not be loaded twice.
5989 Here we check for the most common case of this phenomenon: when we have
5990 a number of reloads for the same object, each of which were allocated
5991 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
5992 reload, and is not modified in the insn itself. If we find such,
5993 merge all the reloads and set the resulting reload to RELOAD_OTHER.
5994 This will not increase the number of spill registers needed and will
5995 prevent redundant code. */
5997 static void
5998 merge_assigned_reloads (rtx insn)
6000 int i, j;
6002 /* Scan all the reloads looking for ones that only load values and
6003 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6004 assigned and not modified by INSN. */
6006 for (i = 0; i < n_reloads; i++)
6008 int conflicting_input = 0;
6009 int max_input_address_opnum = -1;
6010 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6012 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6013 || rld[i].out != 0 || rld[i].reg_rtx == 0
6014 || reg_set_p (rld[i].reg_rtx, insn))
6015 continue;
6017 /* Look at all other reloads. Ensure that the only use of this
6018 reload_reg_rtx is in a reload that just loads the same value
6019 as we do. Note that any secondary reloads must be of the identical
6020 class since the values, modes, and result registers are the
6021 same, so we need not do anything with any secondary reloads. */
6023 for (j = 0; j < n_reloads; j++)
6025 if (i == j || rld[j].reg_rtx == 0
6026 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6027 rld[i].reg_rtx))
6028 continue;
6030 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6031 && rld[j].opnum > max_input_address_opnum)
6032 max_input_address_opnum = rld[j].opnum;
6034 /* If the reload regs aren't exactly the same (e.g, different modes)
6035 or if the values are different, we can't merge this reload.
6036 But if it is an input reload, we might still merge
6037 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6039 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6040 || rld[j].out != 0 || rld[j].in == 0
6041 || ! rtx_equal_p (rld[i].in, rld[j].in))
6043 if (rld[j].when_needed != RELOAD_FOR_INPUT
6044 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6045 || rld[i].opnum > rld[j].opnum)
6046 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6047 break;
6048 conflicting_input = 1;
6049 if (min_conflicting_input_opnum > rld[j].opnum)
6050 min_conflicting_input_opnum = rld[j].opnum;
6054 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6055 we, in fact, found any matching reloads. */
6057 if (j == n_reloads
6058 && max_input_address_opnum <= min_conflicting_input_opnum)
6060 for (j = 0; j < n_reloads; j++)
6061 if (i != j && rld[j].reg_rtx != 0
6062 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6063 && (! conflicting_input
6064 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6065 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6067 rld[i].when_needed = RELOAD_OTHER;
6068 rld[j].in = 0;
6069 reload_spill_index[j] = -1;
6070 transfer_replacements (i, j);
6073 /* If this is now RELOAD_OTHER, look for any reloads that load
6074 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6075 if they were for inputs, RELOAD_OTHER for outputs. Note that
6076 this test is equivalent to looking for reloads for this operand
6077 number. */
6078 /* We must take special care when there are two or more reloads to
6079 be merged and a RELOAD_FOR_OUTPUT_ADDRESS reload that loads the
6080 same value or a part of it; we must not change its type if there
6081 is a conflicting input. */
6083 if (rld[i].when_needed == RELOAD_OTHER)
6084 for (j = 0; j < n_reloads; j++)
6085 if (rld[j].in != 0
6086 && rld[j].when_needed != RELOAD_OTHER
6087 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6088 && (! conflicting_input
6089 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6090 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6091 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6092 rld[i].in))
6094 int k;
6096 rld[j].when_needed
6097 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6098 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6099 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6101 /* Check to see if we accidentally converted two
6102 reloads that use the same reload register with
6103 different inputs to the same type. If so, the
6104 resulting code won't work. */
6105 if (rld[j].reg_rtx)
6106 for (k = 0; k < j; k++)
6107 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6108 || rld[k].when_needed != rld[j].when_needed
6109 || !rtx_equal_p (rld[k].reg_rtx,
6110 rld[j].reg_rtx)
6111 || rtx_equal_p (rld[k].in,
6112 rld[j].in));
6118 /* These arrays are filled by emit_reload_insns and its subroutines. */
6119 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6120 static rtx other_input_address_reload_insns = 0;
6121 static rtx other_input_reload_insns = 0;
6122 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6123 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6124 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6125 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6126 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6127 static rtx operand_reload_insns = 0;
6128 static rtx other_operand_reload_insns = 0;
6129 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6131 /* Values to be put in spill_reg_store are put here first. */
6132 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6133 static HARD_REG_SET reg_reloaded_died;
6135 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6136 has the number J. OLD contains the value to be used as input. */
6138 static void
6139 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6140 rtx old, int j)
6142 rtx insn = chain->insn;
6143 rtx reloadreg = rl->reg_rtx;
6144 rtx oldequiv_reg = 0;
6145 rtx oldequiv = 0;
6146 int special = 0;
6147 enum machine_mode mode;
6148 rtx *where;
6150 /* Determine the mode to reload in.
6151 This is very tricky because we have three to choose from.
6152 There is the mode the insn operand wants (rl->inmode).
6153 There is the mode of the reload register RELOADREG.
6154 There is the intrinsic mode of the operand, which we could find
6155 by stripping some SUBREGs.
6156 It turns out that RELOADREG's mode is irrelevant:
6157 we can change that arbitrarily.
6159 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6160 then the reload reg may not support QImode moves, so use SImode.
6161 If foo is in memory due to spilling a pseudo reg, this is safe,
6162 because the QImode value is in the least significant part of a
6163 slot big enough for a SImode. If foo is some other sort of
6164 memory reference, then it is impossible to reload this case,
6165 so previous passes had better make sure this never happens.
6167 Then consider a one-word union which has SImode and one of its
6168 members is a float, being fetched as (SUBREG:SF union:SI).
6169 We must fetch that as SFmode because we could be loading into
6170 a float-only register. In this case OLD's mode is correct.
6172 Consider an immediate integer: it has VOIDmode. Here we need
6173 to get a mode from something else.
6175 In some cases, there is a fourth mode, the operand's
6176 containing mode. If the insn specifies a containing mode for
6177 this operand, it overrides all others.
6179 I am not sure whether the algorithm here is always right,
6180 but it does the right things in those cases. */
6182 mode = GET_MODE (old);
6183 if (mode == VOIDmode)
6184 mode = rl->inmode;
6186 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6187 /* If we need a secondary register for this operation, see if
6188 the value is already in a register in that class. Don't
6189 do this if the secondary register will be used as a scratch
6190 register. */
6192 if (rl->secondary_in_reload >= 0
6193 && rl->secondary_in_icode == CODE_FOR_nothing
6194 && optimize)
6195 oldequiv
6196 = find_equiv_reg (old, insn,
6197 rld[rl->secondary_in_reload].class,
6198 -1, NULL, 0, mode);
6199 #endif
6201 /* If reloading from memory, see if there is a register
6202 that already holds the same value. If so, reload from there.
6203 We can pass 0 as the reload_reg_p argument because
6204 any other reload has either already been emitted,
6205 in which case find_equiv_reg will see the reload-insn,
6206 or has yet to be emitted, in which case it doesn't matter
6207 because we will use this equiv reg right away. */
6209 if (oldequiv == 0 && optimize
6210 && (MEM_P (old)
6211 || (REG_P (old)
6212 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6213 && reg_renumber[REGNO (old)] < 0)))
6214 oldequiv = find_equiv_reg (old, insn, ALL_REGS, -1, NULL, 0, mode);
6216 if (oldequiv)
6218 unsigned int regno = true_regnum (oldequiv);
6220 /* Don't use OLDEQUIV if any other reload changes it at an
6221 earlier stage of this insn or at this stage. */
6222 if (! free_for_value_p (regno, rl->mode, rl->opnum, rl->when_needed,
6223 rl->in, const0_rtx, j, 0))
6224 oldequiv = 0;
6226 /* If it is no cheaper to copy from OLDEQUIV into the
6227 reload register than it would be to move from memory,
6228 don't use it. Likewise, if we need a secondary register
6229 or memory. */
6231 if (oldequiv != 0
6232 && (((enum reg_class) REGNO_REG_CLASS (regno) != rl->class
6233 && (REGISTER_MOVE_COST (mode, REGNO_REG_CLASS (regno),
6234 rl->class)
6235 >= MEMORY_MOVE_COST (mode, rl->class, 1)))
6236 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6237 || (SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6238 mode, oldequiv)
6239 != NO_REGS)
6240 #endif
6241 #ifdef SECONDARY_MEMORY_NEEDED
6242 || SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (regno),
6243 rl->class,
6244 mode)
6245 #endif
6247 oldequiv = 0;
6250 /* delete_output_reload is only invoked properly if old contains
6251 the original pseudo register. Since this is replaced with a
6252 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6253 find the pseudo in RELOAD_IN_REG. */
6254 if (oldequiv == 0
6255 && reload_override_in[j]
6256 && REG_P (rl->in_reg))
6258 oldequiv = old;
6259 old = rl->in_reg;
6261 if (oldequiv == 0)
6262 oldequiv = old;
6263 else if (REG_P (oldequiv))
6264 oldequiv_reg = oldequiv;
6265 else if (GET_CODE (oldequiv) == SUBREG)
6266 oldequiv_reg = SUBREG_REG (oldequiv);
6268 /* If we are reloading from a register that was recently stored in
6269 with an output-reload, see if we can prove there was
6270 actually no need to store the old value in it. */
6272 if (optimize && REG_P (oldequiv)
6273 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6274 && spill_reg_store[REGNO (oldequiv)]
6275 && REG_P (old)
6276 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6277 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6278 rl->out_reg)))
6279 delete_output_reload (insn, j, REGNO (oldequiv));
6281 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6282 then load RELOADREG from OLDEQUIV. Note that we cannot use
6283 gen_lowpart_common since it can do the wrong thing when
6284 RELOADREG has a multi-word mode. Note that RELOADREG
6285 must always be a REG here. */
6287 if (GET_MODE (reloadreg) != mode)
6288 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6289 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6290 oldequiv = SUBREG_REG (oldequiv);
6291 if (GET_MODE (oldequiv) != VOIDmode
6292 && mode != GET_MODE (oldequiv))
6293 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6295 /* Switch to the right place to emit the reload insns. */
6296 switch (rl->when_needed)
6298 case RELOAD_OTHER:
6299 where = &other_input_reload_insns;
6300 break;
6301 case RELOAD_FOR_INPUT:
6302 where = &input_reload_insns[rl->opnum];
6303 break;
6304 case RELOAD_FOR_INPUT_ADDRESS:
6305 where = &input_address_reload_insns[rl->opnum];
6306 break;
6307 case RELOAD_FOR_INPADDR_ADDRESS:
6308 where = &inpaddr_address_reload_insns[rl->opnum];
6309 break;
6310 case RELOAD_FOR_OUTPUT_ADDRESS:
6311 where = &output_address_reload_insns[rl->opnum];
6312 break;
6313 case RELOAD_FOR_OUTADDR_ADDRESS:
6314 where = &outaddr_address_reload_insns[rl->opnum];
6315 break;
6316 case RELOAD_FOR_OPERAND_ADDRESS:
6317 where = &operand_reload_insns;
6318 break;
6319 case RELOAD_FOR_OPADDR_ADDR:
6320 where = &other_operand_reload_insns;
6321 break;
6322 case RELOAD_FOR_OTHER_ADDRESS:
6323 where = &other_input_address_reload_insns;
6324 break;
6325 default:
6326 gcc_unreachable ();
6329 push_to_sequence (*where);
6331 /* Auto-increment addresses must be reloaded in a special way. */
6332 if (rl->out && ! rl->out_reg)
6334 /* We are not going to bother supporting the case where a
6335 incremented register can't be copied directly from
6336 OLDEQUIV since this seems highly unlikely. */
6337 gcc_assert (rl->secondary_in_reload < 0);
6339 if (reload_inherited[j])
6340 oldequiv = reloadreg;
6342 old = XEXP (rl->in_reg, 0);
6344 if (optimize && REG_P (oldequiv)
6345 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6346 && spill_reg_store[REGNO (oldequiv)]
6347 && REG_P (old)
6348 && (dead_or_set_p (insn,
6349 spill_reg_stored_to[REGNO (oldequiv)])
6350 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6351 old)))
6352 delete_output_reload (insn, j, REGNO (oldequiv));
6354 /* Prevent normal processing of this reload. */
6355 special = 1;
6356 /* Output a special code sequence for this case. */
6357 new_spill_reg_store[REGNO (reloadreg)]
6358 = inc_for_reload (reloadreg, oldequiv, rl->out,
6359 rl->inc);
6362 /* If we are reloading a pseudo-register that was set by the previous
6363 insn, see if we can get rid of that pseudo-register entirely
6364 by redirecting the previous insn into our reload register. */
6366 else if (optimize && REG_P (old)
6367 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6368 && dead_or_set_p (insn, old)
6369 /* This is unsafe if some other reload
6370 uses the same reg first. */
6371 && ! conflicts_with_override (reloadreg)
6372 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6373 rl->when_needed, old, rl->out, j, 0))
6375 rtx temp = PREV_INSN (insn);
6376 while (temp && NOTE_P (temp))
6377 temp = PREV_INSN (temp);
6378 if (temp
6379 && NONJUMP_INSN_P (temp)
6380 && GET_CODE (PATTERN (temp)) == SET
6381 && SET_DEST (PATTERN (temp)) == old
6382 /* Make sure we can access insn_operand_constraint. */
6383 && asm_noperands (PATTERN (temp)) < 0
6384 /* This is unsafe if operand occurs more than once in current
6385 insn. Perhaps some occurrences aren't reloaded. */
6386 && count_occurrences (PATTERN (insn), old, 0) == 1)
6388 rtx old = SET_DEST (PATTERN (temp));
6389 /* Store into the reload register instead of the pseudo. */
6390 SET_DEST (PATTERN (temp)) = reloadreg;
6392 /* Verify that resulting insn is valid. */
6393 extract_insn (temp);
6394 if (constrain_operands (1))
6396 /* If the previous insn is an output reload, the source is
6397 a reload register, and its spill_reg_store entry will
6398 contain the previous destination. This is now
6399 invalid. */
6400 if (REG_P (SET_SRC (PATTERN (temp)))
6401 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6403 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6404 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6407 /* If these are the only uses of the pseudo reg,
6408 pretend for GDB it lives in the reload reg we used. */
6409 if (REG_N_DEATHS (REGNO (old)) == 1
6410 && REG_N_SETS (REGNO (old)) == 1)
6412 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6413 alter_reg (REGNO (old), -1);
6415 special = 1;
6417 else
6419 SET_DEST (PATTERN (temp)) = old;
6424 /* We can't do that, so output an insn to load RELOADREG. */
6426 #ifdef SECONDARY_INPUT_RELOAD_CLASS
6427 /* If we have a secondary reload, pick up the secondary register
6428 and icode, if any. If OLDEQUIV and OLD are different or
6429 if this is an in-out reload, recompute whether or not we
6430 still need a secondary register and what the icode should
6431 be. If we still need a secondary register and the class or
6432 icode is different, go back to reloading from OLD if using
6433 OLDEQUIV means that we got the wrong type of register. We
6434 cannot have different class or icode due to an in-out reload
6435 because we don't make such reloads when both the input and
6436 output need secondary reload registers. */
6438 if (! special && rl->secondary_in_reload >= 0)
6440 rtx second_reload_reg = 0;
6441 int secondary_reload = rl->secondary_in_reload;
6442 rtx real_oldequiv = oldequiv;
6443 rtx real_old = old;
6444 rtx tmp;
6445 enum insn_code icode;
6447 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6448 and similarly for OLD.
6449 See comments in get_secondary_reload in reload.c. */
6450 /* If it is a pseudo that cannot be replaced with its
6451 equivalent MEM, we must fall back to reload_in, which
6452 will have all the necessary substitutions registered.
6453 Likewise for a pseudo that can't be replaced with its
6454 equivalent constant.
6456 Take extra care for subregs of such pseudos. Note that
6457 we cannot use reg_equiv_mem in this case because it is
6458 not in the right mode. */
6460 tmp = oldequiv;
6461 if (GET_CODE (tmp) == SUBREG)
6462 tmp = SUBREG_REG (tmp);
6463 if (REG_P (tmp)
6464 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6465 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6466 || reg_equiv_constant[REGNO (tmp)] != 0))
6468 if (! reg_equiv_mem[REGNO (tmp)]
6469 || num_not_at_initial_offset
6470 || GET_CODE (oldequiv) == SUBREG)
6471 real_oldequiv = rl->in;
6472 else
6473 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6476 tmp = old;
6477 if (GET_CODE (tmp) == SUBREG)
6478 tmp = SUBREG_REG (tmp);
6479 if (REG_P (tmp)
6480 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6481 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6482 || reg_equiv_constant[REGNO (tmp)] != 0))
6484 if (! reg_equiv_mem[REGNO (tmp)]
6485 || num_not_at_initial_offset
6486 || GET_CODE (old) == SUBREG)
6487 real_old = rl->in;
6488 else
6489 real_old = reg_equiv_mem[REGNO (tmp)];
6492 second_reload_reg = rld[secondary_reload].reg_rtx;
6493 icode = rl->secondary_in_icode;
6495 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6496 || (rl->in != 0 && rl->out != 0))
6498 enum reg_class new_class
6499 = SECONDARY_INPUT_RELOAD_CLASS (rl->class,
6500 mode, real_oldequiv);
6502 if (new_class == NO_REGS)
6503 second_reload_reg = 0;
6504 else
6506 enum insn_code new_icode;
6507 enum machine_mode new_mode;
6509 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
6510 REGNO (second_reload_reg)))
6511 oldequiv = old, real_oldequiv = real_old;
6512 else
6514 new_icode = reload_in_optab[(int) mode];
6515 if (new_icode != CODE_FOR_nothing
6516 && ((insn_data[(int) new_icode].operand[0].predicate
6517 && ! ((*insn_data[(int) new_icode].operand[0].predicate)
6518 (reloadreg, mode)))
6519 || (insn_data[(int) new_icode].operand[1].predicate
6520 && ! ((*insn_data[(int) new_icode].operand[1].predicate)
6521 (real_oldequiv, mode)))))
6522 new_icode = CODE_FOR_nothing;
6524 if (new_icode == CODE_FOR_nothing)
6525 new_mode = mode;
6526 else
6527 new_mode = insn_data[(int) new_icode].operand[2].mode;
6529 if (GET_MODE (second_reload_reg) != new_mode)
6531 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
6532 new_mode))
6533 oldequiv = old, real_oldequiv = real_old;
6534 else
6535 second_reload_reg
6536 = reload_adjust_reg_for_mode (second_reload_reg,
6537 new_mode);
6543 /* If we still need a secondary reload register, check
6544 to see if it is being used as a scratch or intermediate
6545 register and generate code appropriately. If we need
6546 a scratch register, use REAL_OLDEQUIV since the form of
6547 the insn may depend on the actual address if it is
6548 a MEM. */
6550 if (second_reload_reg)
6552 if (icode != CODE_FOR_nothing)
6554 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6555 second_reload_reg));
6556 special = 1;
6558 else
6560 /* See if we need a scratch register to load the
6561 intermediate register (a tertiary reload). */
6562 enum insn_code tertiary_icode
6563 = rld[secondary_reload].secondary_in_icode;
6565 if (tertiary_icode != CODE_FOR_nothing)
6567 rtx third_reload_reg
6568 = rld[rld[secondary_reload].secondary_in_reload].reg_rtx;
6570 emit_insn ((GEN_FCN (tertiary_icode)
6571 (second_reload_reg, real_oldequiv,
6572 third_reload_reg)));
6574 else
6575 gen_reload (second_reload_reg, real_oldequiv,
6576 rl->opnum,
6577 rl->when_needed);
6579 oldequiv = second_reload_reg;
6583 #endif
6585 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6587 rtx real_oldequiv = oldequiv;
6589 if ((REG_P (oldequiv)
6590 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6591 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6592 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6593 || (GET_CODE (oldequiv) == SUBREG
6594 && REG_P (SUBREG_REG (oldequiv))
6595 && (REGNO (SUBREG_REG (oldequiv))
6596 >= FIRST_PSEUDO_REGISTER)
6597 && ((reg_equiv_memory_loc
6598 [REGNO (SUBREG_REG (oldequiv))] != 0)
6599 || (reg_equiv_constant
6600 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6601 || (CONSTANT_P (oldequiv)
6602 && (PREFERRED_RELOAD_CLASS (oldequiv,
6603 REGNO_REG_CLASS (REGNO (reloadreg)))
6604 == NO_REGS)))
6605 real_oldequiv = rl->in;
6606 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6607 rl->when_needed);
6610 if (flag_non_call_exceptions)
6611 copy_eh_notes (insn, get_insns ());
6613 /* End this sequence. */
6614 *where = get_insns ();
6615 end_sequence ();
6617 /* Update reload_override_in so that delete_address_reloads_1
6618 can see the actual register usage. */
6619 if (oldequiv_reg)
6620 reload_override_in[j] = oldequiv;
6623 /* Generate insns to for the output reload RL, which is for the insn described
6624 by CHAIN and has the number J. */
6625 static void
6626 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
6627 int j)
6629 rtx reloadreg = rl->reg_rtx;
6630 rtx insn = chain->insn;
6631 int special = 0;
6632 rtx old = rl->out;
6633 enum machine_mode mode = GET_MODE (old);
6634 rtx p;
6636 if (rl->when_needed == RELOAD_OTHER)
6637 start_sequence ();
6638 else
6639 push_to_sequence (output_reload_insns[rl->opnum]);
6641 /* Determine the mode to reload in.
6642 See comments above (for input reloading). */
6644 if (mode == VOIDmode)
6646 /* VOIDmode should never happen for an output. */
6647 if (asm_noperands (PATTERN (insn)) < 0)
6648 /* It's the compiler's fault. */
6649 fatal_insn ("VOIDmode on an output", insn);
6650 error_for_asm (insn, "output operand is constant in %<asm%>");
6651 /* Prevent crash--use something we know is valid. */
6652 mode = word_mode;
6653 old = gen_rtx_REG (mode, REGNO (reloadreg));
6656 if (GET_MODE (reloadreg) != mode)
6657 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6659 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
6661 /* If we need two reload regs, set RELOADREG to the intermediate
6662 one, since it will be stored into OLD. We might need a secondary
6663 register only for an input reload, so check again here. */
6665 if (rl->secondary_out_reload >= 0)
6667 rtx real_old = old;
6669 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
6670 && reg_equiv_mem[REGNO (old)] != 0)
6671 real_old = reg_equiv_mem[REGNO (old)];
6673 if ((SECONDARY_OUTPUT_RELOAD_CLASS (rl->class,
6674 mode, real_old)
6675 != NO_REGS))
6677 rtx second_reloadreg = reloadreg;
6678 reloadreg = rld[rl->secondary_out_reload].reg_rtx;
6680 /* See if RELOADREG is to be used as a scratch register
6681 or as an intermediate register. */
6682 if (rl->secondary_out_icode != CODE_FOR_nothing)
6684 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6685 (real_old, second_reloadreg, reloadreg)));
6686 special = 1;
6688 else
6690 /* See if we need both a scratch and intermediate reload
6691 register. */
6693 int secondary_reload = rl->secondary_out_reload;
6694 enum insn_code tertiary_icode
6695 = rld[secondary_reload].secondary_out_icode;
6697 if (GET_MODE (reloadreg) != mode)
6698 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6700 if (tertiary_icode != CODE_FOR_nothing)
6702 rtx third_reloadreg
6703 = rld[rld[secondary_reload].secondary_out_reload].reg_rtx;
6704 rtx tem;
6706 /* Copy primary reload reg to secondary reload reg.
6707 (Note that these have been swapped above, then
6708 secondary reload reg to OLD using our insn.) */
6710 /* If REAL_OLD is a paradoxical SUBREG, remove it
6711 and try to put the opposite SUBREG on
6712 RELOADREG. */
6713 if (GET_CODE (real_old) == SUBREG
6714 && (GET_MODE_SIZE (GET_MODE (real_old))
6715 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6716 && 0 != (tem = gen_lowpart_common
6717 (GET_MODE (SUBREG_REG (real_old)),
6718 reloadreg)))
6719 real_old = SUBREG_REG (real_old), reloadreg = tem;
6721 gen_reload (reloadreg, second_reloadreg,
6722 rl->opnum, rl->when_needed);
6723 emit_insn ((GEN_FCN (tertiary_icode)
6724 (real_old, reloadreg, third_reloadreg)));
6725 special = 1;
6728 else
6729 /* Copy between the reload regs here and then to
6730 OUT later. */
6732 gen_reload (reloadreg, second_reloadreg,
6733 rl->opnum, rl->when_needed);
6737 #endif
6739 /* Output the last reload insn. */
6740 if (! special)
6742 rtx set;
6744 /* Don't output the last reload if OLD is not the dest of
6745 INSN and is in the src and is clobbered by INSN. */
6746 if (! flag_expensive_optimizations
6747 || !REG_P (old)
6748 || !(set = single_set (insn))
6749 || rtx_equal_p (old, SET_DEST (set))
6750 || !reg_mentioned_p (old, SET_SRC (set))
6751 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
6752 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
6753 gen_reload (old, reloadreg, rl->opnum,
6754 rl->when_needed);
6757 /* Look at all insns we emitted, just to be safe. */
6758 for (p = get_insns (); p; p = NEXT_INSN (p))
6759 if (INSN_P (p))
6761 rtx pat = PATTERN (p);
6763 /* If this output reload doesn't come from a spill reg,
6764 clear any memory of reloaded copies of the pseudo reg.
6765 If this output reload comes from a spill reg,
6766 reg_has_output_reload will make this do nothing. */
6767 note_stores (pat, forget_old_reloads_1, NULL);
6769 if (reg_mentioned_p (rl->reg_rtx, pat))
6771 rtx set = single_set (insn);
6772 if (reload_spill_index[j] < 0
6773 && set
6774 && SET_SRC (set) == rl->reg_rtx)
6776 int src = REGNO (SET_SRC (set));
6778 reload_spill_index[j] = src;
6779 SET_HARD_REG_BIT (reg_is_output_reload, src);
6780 if (find_regno_note (insn, REG_DEAD, src))
6781 SET_HARD_REG_BIT (reg_reloaded_died, src);
6783 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
6785 int s = rl->secondary_out_reload;
6786 set = single_set (p);
6787 /* If this reload copies only to the secondary reload
6788 register, the secondary reload does the actual
6789 store. */
6790 if (s >= 0 && set == NULL_RTX)
6791 /* We can't tell what function the secondary reload
6792 has and where the actual store to the pseudo is
6793 made; leave new_spill_reg_store alone. */
6795 else if (s >= 0
6796 && SET_SRC (set) == rl->reg_rtx
6797 && SET_DEST (set) == rld[s].reg_rtx)
6799 /* Usually the next instruction will be the
6800 secondary reload insn; if we can confirm
6801 that it is, setting new_spill_reg_store to
6802 that insn will allow an extra optimization. */
6803 rtx s_reg = rld[s].reg_rtx;
6804 rtx next = NEXT_INSN (p);
6805 rld[s].out = rl->out;
6806 rld[s].out_reg = rl->out_reg;
6807 set = single_set (next);
6808 if (set && SET_SRC (set) == s_reg
6809 && ! new_spill_reg_store[REGNO (s_reg)])
6811 SET_HARD_REG_BIT (reg_is_output_reload,
6812 REGNO (s_reg));
6813 new_spill_reg_store[REGNO (s_reg)] = next;
6816 else
6817 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
6822 if (rl->when_needed == RELOAD_OTHER)
6824 emit_insn (other_output_reload_insns[rl->opnum]);
6825 other_output_reload_insns[rl->opnum] = get_insns ();
6827 else
6828 output_reload_insns[rl->opnum] = get_insns ();
6830 if (flag_non_call_exceptions)
6831 copy_eh_notes (insn, get_insns ());
6833 end_sequence ();
6836 /* Do input reloading for reload RL, which is for the insn described by CHAIN
6837 and has the number J. */
6838 static void
6839 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
6841 rtx insn = chain->insn;
6842 rtx old = (rl->in && MEM_P (rl->in)
6843 ? rl->in_reg : rl->in);
6845 if (old != 0
6846 /* AUTO_INC reloads need to be handled even if inherited. We got an
6847 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
6848 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
6849 && ! rtx_equal_p (rl->reg_rtx, old)
6850 && rl->reg_rtx != 0)
6851 emit_input_reload_insns (chain, rld + j, old, j);
6853 /* When inheriting a wider reload, we have a MEM in rl->in,
6854 e.g. inheriting a SImode output reload for
6855 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
6856 if (optimize && reload_inherited[j] && rl->in
6857 && MEM_P (rl->in)
6858 && MEM_P (rl->in_reg)
6859 && reload_spill_index[j] >= 0
6860 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
6861 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
6863 /* If we are reloading a register that was recently stored in with an
6864 output-reload, see if we can prove there was
6865 actually no need to store the old value in it. */
6867 if (optimize
6868 /* Only attempt this for input reloads; for RELOAD_OTHER we miss
6869 that there may be multiple uses of the previous output reload.
6870 Restricting to RELOAD_FOR_INPUT is mostly paranoia. */
6871 && rl->when_needed == RELOAD_FOR_INPUT
6872 && (reload_inherited[j] || reload_override_in[j])
6873 && rl->reg_rtx
6874 && REG_P (rl->reg_rtx)
6875 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
6876 #if 0
6877 /* There doesn't seem to be any reason to restrict this to pseudos
6878 and doing so loses in the case where we are copying from a
6879 register of the wrong class. */
6880 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
6881 >= FIRST_PSEUDO_REGISTER)
6882 #endif
6883 /* The insn might have already some references to stackslots
6884 replaced by MEMs, while reload_out_reg still names the
6885 original pseudo. */
6886 && (dead_or_set_p (insn,
6887 spill_reg_stored_to[REGNO (rl->reg_rtx)])
6888 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
6889 rl->out_reg)))
6890 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
6893 /* Do output reloading for reload RL, which is for the insn described by
6894 CHAIN and has the number J.
6895 ??? At some point we need to support handling output reloads of
6896 JUMP_INSNs or insns that set cc0. */
6897 static void
6898 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
6900 rtx note, old;
6901 rtx insn = chain->insn;
6902 /* If this is an output reload that stores something that is
6903 not loaded in this same reload, see if we can eliminate a previous
6904 store. */
6905 rtx pseudo = rl->out_reg;
6907 if (pseudo
6908 && optimize
6909 && REG_P (pseudo)
6910 && ! rtx_equal_p (rl->in_reg, pseudo)
6911 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
6912 && reg_last_reload_reg[REGNO (pseudo)])
6914 int pseudo_no = REGNO (pseudo);
6915 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
6917 /* We don't need to test full validity of last_regno for
6918 inherit here; we only want to know if the store actually
6919 matches the pseudo. */
6920 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
6921 && reg_reloaded_contents[last_regno] == pseudo_no
6922 && spill_reg_store[last_regno]
6923 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
6924 delete_output_reload (insn, j, last_regno);
6927 old = rl->out_reg;
6928 if (old == 0
6929 || rl->reg_rtx == old
6930 || rl->reg_rtx == 0)
6931 return;
6933 /* An output operand that dies right away does need a reload,
6934 but need not be copied from it. Show the new location in the
6935 REG_UNUSED note. */
6936 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
6937 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
6939 XEXP (note, 0) = rl->reg_rtx;
6940 return;
6942 /* Likewise for a SUBREG of an operand that dies. */
6943 else if (GET_CODE (old) == SUBREG
6944 && REG_P (SUBREG_REG (old))
6945 && 0 != (note = find_reg_note (insn, REG_UNUSED,
6946 SUBREG_REG (old))))
6948 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
6949 rl->reg_rtx);
6950 return;
6952 else if (GET_CODE (old) == SCRATCH)
6953 /* If we aren't optimizing, there won't be a REG_UNUSED note,
6954 but we don't want to make an output reload. */
6955 return;
6957 /* If is a JUMP_INSN, we can't support output reloads yet. */
6958 gcc_assert (!JUMP_P (insn));
6960 emit_output_reload_insns (chain, rld + j, j);
6963 /* Reload number R reloads from or to a group of hard registers starting at
6964 register REGNO. Return true if it can be treated for inheritance purposes
6965 like a group of reloads, each one reloading a single hard register.
6966 The caller has already checked that the spill register and REGNO use
6967 the same number of registers to store the reload value. */
6969 static bool
6970 inherit_piecemeal_p (int r ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED)
6972 #ifdef CANNOT_CHANGE_MODE_CLASS
6973 return (!REG_CANNOT_CHANGE_MODE_P (reload_spill_index[r],
6974 GET_MODE (rld[r].reg_rtx),
6975 reg_raw_mode[reload_spill_index[r]])
6976 && !REG_CANNOT_CHANGE_MODE_P (regno,
6977 GET_MODE (rld[r].reg_rtx),
6978 reg_raw_mode[regno]));
6979 #else
6980 return true;
6981 #endif
6984 /* Output insns to reload values in and out of the chosen reload regs. */
6986 static void
6987 emit_reload_insns (struct insn_chain *chain)
6989 rtx insn = chain->insn;
6991 int j;
6993 CLEAR_HARD_REG_SET (reg_reloaded_died);
6995 for (j = 0; j < reload_n_operands; j++)
6996 input_reload_insns[j] = input_address_reload_insns[j]
6997 = inpaddr_address_reload_insns[j]
6998 = output_reload_insns[j] = output_address_reload_insns[j]
6999 = outaddr_address_reload_insns[j]
7000 = other_output_reload_insns[j] = 0;
7001 other_input_address_reload_insns = 0;
7002 other_input_reload_insns = 0;
7003 operand_reload_insns = 0;
7004 other_operand_reload_insns = 0;
7006 /* Dump reloads into the dump file. */
7007 if (dump_file)
7009 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7010 debug_reload_to_stream (dump_file);
7013 /* Now output the instructions to copy the data into and out of the
7014 reload registers. Do these in the order that the reloads were reported,
7015 since reloads of base and index registers precede reloads of operands
7016 and the operands may need the base and index registers reloaded. */
7018 for (j = 0; j < n_reloads; j++)
7020 if (rld[j].reg_rtx
7021 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7022 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
7024 do_input_reload (chain, rld + j, j);
7025 do_output_reload (chain, rld + j, j);
7028 /* Now write all the insns we made for reloads in the order expected by
7029 the allocation functions. Prior to the insn being reloaded, we write
7030 the following reloads:
7032 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7034 RELOAD_OTHER reloads.
7036 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7037 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7038 RELOAD_FOR_INPUT reload for the operand.
7040 RELOAD_FOR_OPADDR_ADDRS reloads.
7042 RELOAD_FOR_OPERAND_ADDRESS reloads.
7044 After the insn being reloaded, we write the following:
7046 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7047 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7048 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7049 reloads for the operand. The RELOAD_OTHER output reloads are
7050 output in descending order by reload number. */
7052 emit_insn_before (other_input_address_reload_insns, insn);
7053 emit_insn_before (other_input_reload_insns, insn);
7055 for (j = 0; j < reload_n_operands; j++)
7057 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7058 emit_insn_before (input_address_reload_insns[j], insn);
7059 emit_insn_before (input_reload_insns[j], insn);
7062 emit_insn_before (other_operand_reload_insns, insn);
7063 emit_insn_before (operand_reload_insns, insn);
7065 for (j = 0; j < reload_n_operands; j++)
7067 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7068 x = emit_insn_after (output_address_reload_insns[j], x);
7069 x = emit_insn_after (output_reload_insns[j], x);
7070 emit_insn_after (other_output_reload_insns[j], x);
7073 /* For all the spill regs newly reloaded in this instruction,
7074 record what they were reloaded from, so subsequent instructions
7075 can inherit the reloads.
7077 Update spill_reg_store for the reloads of this insn.
7078 Copy the elements that were updated in the loop above. */
7080 for (j = 0; j < n_reloads; j++)
7082 int r = reload_order[j];
7083 int i = reload_spill_index[r];
7085 /* If this is a non-inherited input reload from a pseudo, we must
7086 clear any memory of a previous store to the same pseudo. Only do
7087 something if there will not be an output reload for the pseudo
7088 being reloaded. */
7089 if (rld[r].in_reg != 0
7090 && ! (reload_inherited[r] || reload_override_in[r]))
7092 rtx reg = rld[r].in_reg;
7094 if (GET_CODE (reg) == SUBREG)
7095 reg = SUBREG_REG (reg);
7097 if (REG_P (reg)
7098 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7099 && ! reg_has_output_reload[REGNO (reg)])
7101 int nregno = REGNO (reg);
7103 if (reg_last_reload_reg[nregno])
7105 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7107 if (reg_reloaded_contents[last_regno] == nregno)
7108 spill_reg_store[last_regno] = 0;
7113 /* I is nonneg if this reload used a register.
7114 If rld[r].reg_rtx is 0, this is an optional reload
7115 that we opted to ignore. */
7117 if (i >= 0 && rld[r].reg_rtx != 0)
7119 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7120 int k;
7121 int part_reaches_end = 0;
7122 int all_reaches_end = 1;
7124 /* For a multi register reload, we need to check if all or part
7125 of the value lives to the end. */
7126 for (k = 0; k < nr; k++)
7128 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7129 rld[r].when_needed))
7130 part_reaches_end = 1;
7131 else
7132 all_reaches_end = 0;
7135 /* Ignore reloads that don't reach the end of the insn in
7136 entirety. */
7137 if (all_reaches_end)
7139 /* First, clear out memory of what used to be in this spill reg.
7140 If consecutive registers are used, clear them all. */
7142 for (k = 0; k < nr; k++)
7144 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7145 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7148 /* Maybe the spill reg contains a copy of reload_out. */
7149 if (rld[r].out != 0
7150 && (REG_P (rld[r].out)
7151 #ifdef AUTO_INC_DEC
7152 || ! rld[r].out_reg
7153 #endif
7154 || REG_P (rld[r].out_reg)))
7156 rtx out = (REG_P (rld[r].out)
7157 ? rld[r].out
7158 : rld[r].out_reg
7159 ? rld[r].out_reg
7160 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7161 int nregno = REGNO (out);
7162 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7163 : hard_regno_nregs[nregno]
7164 [GET_MODE (rld[r].reg_rtx)]);
7165 bool piecemeal;
7167 spill_reg_store[i] = new_spill_reg_store[i];
7168 spill_reg_stored_to[i] = out;
7169 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7171 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7172 && nr == nnr
7173 && inherit_piecemeal_p (r, nregno));
7175 /* If NREGNO is a hard register, it may occupy more than
7176 one register. If it does, say what is in the
7177 rest of the registers assuming that both registers
7178 agree on how many words the object takes. If not,
7179 invalidate the subsequent registers. */
7181 if (nregno < FIRST_PSEUDO_REGISTER)
7182 for (k = 1; k < nnr; k++)
7183 reg_last_reload_reg[nregno + k]
7184 = (piecemeal
7185 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7186 : 0);
7188 /* Now do the inverse operation. */
7189 for (k = 0; k < nr; k++)
7191 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7192 reg_reloaded_contents[i + k]
7193 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7194 ? nregno
7195 : nregno + k);
7196 reg_reloaded_insn[i + k] = insn;
7197 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7198 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (out)))
7199 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7203 /* Maybe the spill reg contains a copy of reload_in. Only do
7204 something if there will not be an output reload for
7205 the register being reloaded. */
7206 else if (rld[r].out_reg == 0
7207 && rld[r].in != 0
7208 && ((REG_P (rld[r].in)
7209 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7210 && ! reg_has_output_reload[REGNO (rld[r].in)])
7211 || (REG_P (rld[r].in_reg)
7212 && ! reg_has_output_reload[REGNO (rld[r].in_reg)]))
7213 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7215 int nregno;
7216 int nnr;
7217 rtx in;
7218 bool piecemeal;
7220 if (REG_P (rld[r].in)
7221 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7222 in = rld[r].in;
7223 else if (REG_P (rld[r].in_reg))
7224 in = rld[r].in_reg;
7225 else
7226 in = XEXP (rld[r].in_reg, 0);
7227 nregno = REGNO (in);
7229 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7230 : hard_regno_nregs[nregno]
7231 [GET_MODE (rld[r].reg_rtx)]);
7233 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7235 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7236 && nr == nnr
7237 && inherit_piecemeal_p (r, nregno));
7239 if (nregno < FIRST_PSEUDO_REGISTER)
7240 for (k = 1; k < nnr; k++)
7241 reg_last_reload_reg[nregno + k]
7242 = (piecemeal
7243 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7244 : 0);
7246 /* Unless we inherited this reload, show we haven't
7247 recently done a store.
7248 Previous stores of inherited auto_inc expressions
7249 also have to be discarded. */
7250 if (! reload_inherited[r]
7251 || (rld[r].out && ! rld[r].out_reg))
7252 spill_reg_store[i] = 0;
7254 for (k = 0; k < nr; k++)
7256 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7257 reg_reloaded_contents[i + k]
7258 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7259 ? nregno
7260 : nregno + k);
7261 reg_reloaded_insn[i + k] = insn;
7262 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7263 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (in)))
7264 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7269 /* However, if part of the reload reaches the end, then we must
7270 invalidate the old info for the part that survives to the end. */
7271 else if (part_reaches_end)
7273 for (k = 0; k < nr; k++)
7274 if (reload_reg_reaches_end_p (i + k,
7275 rld[r].opnum,
7276 rld[r].when_needed))
7277 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7281 /* The following if-statement was #if 0'd in 1.34 (or before...).
7282 It's reenabled in 1.35 because supposedly nothing else
7283 deals with this problem. */
7285 /* If a register gets output-reloaded from a non-spill register,
7286 that invalidates any previous reloaded copy of it.
7287 But forget_old_reloads_1 won't get to see it, because
7288 it thinks only about the original insn. So invalidate it here. */
7289 if (i < 0 && rld[r].out != 0
7290 && (REG_P (rld[r].out)
7291 || (MEM_P (rld[r].out)
7292 && REG_P (rld[r].out_reg))))
7294 rtx out = (REG_P (rld[r].out)
7295 ? rld[r].out : rld[r].out_reg);
7296 int nregno = REGNO (out);
7297 if (nregno >= FIRST_PSEUDO_REGISTER)
7299 rtx src_reg, store_insn = NULL_RTX;
7301 reg_last_reload_reg[nregno] = 0;
7303 /* If we can find a hard register that is stored, record
7304 the storing insn so that we may delete this insn with
7305 delete_output_reload. */
7306 src_reg = rld[r].reg_rtx;
7308 /* If this is an optional reload, try to find the source reg
7309 from an input reload. */
7310 if (! src_reg)
7312 rtx set = single_set (insn);
7313 if (set && SET_DEST (set) == rld[r].out)
7315 int k;
7317 src_reg = SET_SRC (set);
7318 store_insn = insn;
7319 for (k = 0; k < n_reloads; k++)
7321 if (rld[k].in == src_reg)
7323 src_reg = rld[k].reg_rtx;
7324 break;
7329 else
7330 store_insn = new_spill_reg_store[REGNO (src_reg)];
7331 if (src_reg && REG_P (src_reg)
7332 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7334 int src_regno = REGNO (src_reg);
7335 int nr = hard_regno_nregs[src_regno][rld[r].mode];
7336 /* The place where to find a death note varies with
7337 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7338 necessarily checked exactly in the code that moves
7339 notes, so just check both locations. */
7340 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7341 if (! note && store_insn)
7342 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7343 while (nr-- > 0)
7345 spill_reg_store[src_regno + nr] = store_insn;
7346 spill_reg_stored_to[src_regno + nr] = out;
7347 reg_reloaded_contents[src_regno + nr] = nregno;
7348 reg_reloaded_insn[src_regno + nr] = store_insn;
7349 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7350 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7351 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + nr,
7352 GET_MODE (src_reg)))
7353 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7354 src_regno + nr);
7355 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7356 if (note)
7357 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7358 else
7359 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7361 reg_last_reload_reg[nregno] = src_reg;
7362 /* We have to set reg_has_output_reload here, or else
7363 forget_old_reloads_1 will clear reg_last_reload_reg
7364 right away. */
7365 reg_has_output_reload[nregno] = 1;
7368 else
7370 int num_regs = hard_regno_nregs[nregno][GET_MODE (rld[r].out)];
7372 while (num_regs-- > 0)
7373 reg_last_reload_reg[nregno + num_regs] = 0;
7377 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7380 /* Emit code to perform a reload from IN (which may be a reload register) to
7381 OUT (which may also be a reload register). IN or OUT is from operand
7382 OPNUM with reload type TYPE.
7384 Returns first insn emitted. */
7386 static rtx
7387 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7389 rtx last = get_last_insn ();
7390 rtx tem;
7392 /* If IN is a paradoxical SUBREG, remove it and try to put the
7393 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7394 if (GET_CODE (in) == SUBREG
7395 && (GET_MODE_SIZE (GET_MODE (in))
7396 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7397 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7398 in = SUBREG_REG (in), out = tem;
7399 else if (GET_CODE (out) == SUBREG
7400 && (GET_MODE_SIZE (GET_MODE (out))
7401 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7402 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7403 out = SUBREG_REG (out), in = tem;
7405 /* How to do this reload can get quite tricky. Normally, we are being
7406 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7407 register that didn't get a hard register. In that case we can just
7408 call emit_move_insn.
7410 We can also be asked to reload a PLUS that adds a register or a MEM to
7411 another register, constant or MEM. This can occur during frame pointer
7412 elimination and while reloading addresses. This case is handled by
7413 trying to emit a single insn to perform the add. If it is not valid,
7414 we use a two insn sequence.
7416 Finally, we could be called to handle an 'o' constraint by putting
7417 an address into a register. In that case, we first try to do this
7418 with a named pattern of "reload_load_address". If no such pattern
7419 exists, we just emit a SET insn and hope for the best (it will normally
7420 be valid on machines that use 'o').
7422 This entire process is made complex because reload will never
7423 process the insns we generate here and so we must ensure that
7424 they will fit their constraints and also by the fact that parts of
7425 IN might be being reloaded separately and replaced with spill registers.
7426 Because of this, we are, in some sense, just guessing the right approach
7427 here. The one listed above seems to work.
7429 ??? At some point, this whole thing needs to be rethought. */
7431 if (GET_CODE (in) == PLUS
7432 && (REG_P (XEXP (in, 0))
7433 || GET_CODE (XEXP (in, 0)) == SUBREG
7434 || MEM_P (XEXP (in, 0)))
7435 && (REG_P (XEXP (in, 1))
7436 || GET_CODE (XEXP (in, 1)) == SUBREG
7437 || CONSTANT_P (XEXP (in, 1))
7438 || MEM_P (XEXP (in, 1))))
7440 /* We need to compute the sum of a register or a MEM and another
7441 register, constant, or MEM, and put it into the reload
7442 register. The best possible way of doing this is if the machine
7443 has a three-operand ADD insn that accepts the required operands.
7445 The simplest approach is to try to generate such an insn and see if it
7446 is recognized and matches its constraints. If so, it can be used.
7448 It might be better not to actually emit the insn unless it is valid,
7449 but we need to pass the insn as an operand to `recog' and
7450 `extract_insn' and it is simpler to emit and then delete the insn if
7451 not valid than to dummy things up. */
7453 rtx op0, op1, tem, insn;
7454 int code;
7456 op0 = find_replacement (&XEXP (in, 0));
7457 op1 = find_replacement (&XEXP (in, 1));
7459 /* Since constraint checking is strict, commutativity won't be
7460 checked, so we need to do that here to avoid spurious failure
7461 if the add instruction is two-address and the second operand
7462 of the add is the same as the reload reg, which is frequently
7463 the case. If the insn would be A = B + A, rearrange it so
7464 it will be A = A + B as constrain_operands expects. */
7466 if (REG_P (XEXP (in, 1))
7467 && REGNO (out) == REGNO (XEXP (in, 1)))
7468 tem = op0, op0 = op1, op1 = tem;
7470 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7471 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7473 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
7474 code = recog_memoized (insn);
7476 if (code >= 0)
7478 extract_insn (insn);
7479 /* We want constrain operands to treat this insn strictly in
7480 its validity determination, i.e., the way it would after reload
7481 has completed. */
7482 if (constrain_operands (1))
7483 return insn;
7486 delete_insns_since (last);
7488 /* If that failed, we must use a conservative two-insn sequence.
7490 Use a move to copy one operand into the reload register. Prefer
7491 to reload a constant, MEM or pseudo since the move patterns can
7492 handle an arbitrary operand. If OP1 is not a constant, MEM or
7493 pseudo and OP1 is not a valid operand for an add instruction, then
7494 reload OP1.
7496 After reloading one of the operands into the reload register, add
7497 the reload register to the output register.
7499 If there is another way to do this for a specific machine, a
7500 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7501 we emit below. */
7503 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7505 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
7506 || (REG_P (op1)
7507 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7508 || (code != CODE_FOR_nothing
7509 && ! ((*insn_data[code].operand[2].predicate)
7510 (op1, insn_data[code].operand[2].mode))))
7511 tem = op0, op0 = op1, op1 = tem;
7513 gen_reload (out, op0, opnum, type);
7515 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7516 This fixes a problem on the 32K where the stack pointer cannot
7517 be used as an operand of an add insn. */
7519 if (rtx_equal_p (op0, op1))
7520 op1 = out;
7522 insn = emit_insn (gen_add2_insn (out, op1));
7524 /* If that failed, copy the address register to the reload register.
7525 Then add the constant to the reload register. */
7527 code = recog_memoized (insn);
7529 if (code >= 0)
7531 extract_insn (insn);
7532 /* We want constrain operands to treat this insn strictly in
7533 its validity determination, i.e., the way it would after reload
7534 has completed. */
7535 if (constrain_operands (1))
7537 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7538 REG_NOTES (insn)
7539 = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7540 return insn;
7544 delete_insns_since (last);
7546 gen_reload (out, op1, opnum, type);
7547 insn = emit_insn (gen_add2_insn (out, op0));
7548 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUIV, in, REG_NOTES (insn));
7551 #ifdef SECONDARY_MEMORY_NEEDED
7552 /* If we need a memory location to do the move, do it that way. */
7553 else if ((REG_P (in) || GET_CODE (in) == SUBREG)
7554 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7555 && (REG_P (out) || GET_CODE (out) == SUBREG)
7556 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7557 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7558 REGNO_REG_CLASS (reg_or_subregno (out)),
7559 GET_MODE (out)))
7561 /* Get the memory to use and rewrite both registers to its mode. */
7562 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7564 if (GET_MODE (loc) != GET_MODE (out))
7565 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7567 if (GET_MODE (loc) != GET_MODE (in))
7568 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7570 gen_reload (loc, in, opnum, type);
7571 gen_reload (out, loc, opnum, type);
7573 #endif
7575 /* If IN is a simple operand, use gen_move_insn. */
7576 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
7577 emit_insn (gen_move_insn (out, in));
7579 #ifdef HAVE_reload_load_address
7580 else if (HAVE_reload_load_address)
7581 emit_insn (gen_reload_load_address (out, in));
7582 #endif
7584 /* Otherwise, just write (set OUT IN) and hope for the best. */
7585 else
7586 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7588 /* Return the first insn emitted.
7589 We can not just return get_last_insn, because there may have
7590 been multiple instructions emitted. Also note that gen_move_insn may
7591 emit more than one insn itself, so we can not assume that there is one
7592 insn emitted per emit_insn_before call. */
7594 return last ? NEXT_INSN (last) : get_insns ();
7597 /* Delete a previously made output-reload whose result we now believe
7598 is not needed. First we double-check.
7600 INSN is the insn now being processed.
7601 LAST_RELOAD_REG is the hard register number for which we want to delete
7602 the last output reload.
7603 J is the reload-number that originally used REG. The caller has made
7604 certain that reload J doesn't use REG any longer for input. */
7606 static void
7607 delete_output_reload (rtx insn, int j, int last_reload_reg)
7609 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7610 rtx reg = spill_reg_stored_to[last_reload_reg];
7611 int k;
7612 int n_occurrences;
7613 int n_inherited = 0;
7614 rtx i1;
7615 rtx substed;
7617 /* It is possible that this reload has been only used to set another reload
7618 we eliminated earlier and thus deleted this instruction too. */
7619 if (INSN_DELETED_P (output_reload_insn))
7620 return;
7622 /* Get the raw pseudo-register referred to. */
7624 while (GET_CODE (reg) == SUBREG)
7625 reg = SUBREG_REG (reg);
7626 substed = reg_equiv_memory_loc[REGNO (reg)];
7628 /* This is unsafe if the operand occurs more often in the current
7629 insn than it is inherited. */
7630 for (k = n_reloads - 1; k >= 0; k--)
7632 rtx reg2 = rld[k].in;
7633 if (! reg2)
7634 continue;
7635 if (MEM_P (reg2) || reload_override_in[k])
7636 reg2 = rld[k].in_reg;
7637 #ifdef AUTO_INC_DEC
7638 if (rld[k].out && ! rld[k].out_reg)
7639 reg2 = XEXP (rld[k].in_reg, 0);
7640 #endif
7641 while (GET_CODE (reg2) == SUBREG)
7642 reg2 = SUBREG_REG (reg2);
7643 if (rtx_equal_p (reg2, reg))
7645 if (reload_inherited[k] || reload_override_in[k] || k == j)
7647 n_inherited++;
7648 reg2 = rld[k].out_reg;
7649 if (! reg2)
7650 continue;
7651 while (GET_CODE (reg2) == SUBREG)
7652 reg2 = XEXP (reg2, 0);
7653 if (rtx_equal_p (reg2, reg))
7654 n_inherited++;
7656 else
7657 return;
7660 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7661 if (substed)
7662 n_occurrences += count_occurrences (PATTERN (insn),
7663 eliminate_regs (substed, 0,
7664 NULL_RTX), 0);
7665 if (n_occurrences > n_inherited)
7666 return;
7668 /* If the pseudo-reg we are reloading is no longer referenced
7669 anywhere between the store into it and here,
7670 and we're within the same basic block, then the value can only
7671 pass through the reload reg and end up here.
7672 Otherwise, give up--return. */
7673 for (i1 = NEXT_INSN (output_reload_insn);
7674 i1 != insn; i1 = NEXT_INSN (i1))
7676 if (NOTE_INSN_BASIC_BLOCK_P (i1))
7677 return;
7678 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
7679 && reg_mentioned_p (reg, PATTERN (i1)))
7681 /* If this is USE in front of INSN, we only have to check that
7682 there are no more references than accounted for by inheritance. */
7683 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
7685 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
7686 i1 = NEXT_INSN (i1);
7688 if (n_occurrences <= n_inherited && i1 == insn)
7689 break;
7690 return;
7694 /* We will be deleting the insn. Remove the spill reg information. */
7695 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
7697 spill_reg_store[last_reload_reg + k] = 0;
7698 spill_reg_stored_to[last_reload_reg + k] = 0;
7701 /* The caller has already checked that REG dies or is set in INSN.
7702 It has also checked that we are optimizing, and thus some
7703 inaccuracies in the debugging information are acceptable.
7704 So we could just delete output_reload_insn. But in some cases
7705 we can improve the debugging information without sacrificing
7706 optimization - maybe even improving the code: See if the pseudo
7707 reg has been completely replaced with reload regs. If so, delete
7708 the store insn and forget we had a stack slot for the pseudo. */
7709 if (rld[j].out != rld[j].in
7710 && REG_N_DEATHS (REGNO (reg)) == 1
7711 && REG_N_SETS (REGNO (reg)) == 1
7712 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
7713 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
7715 rtx i2;
7717 /* We know that it was used only between here and the beginning of
7718 the current basic block. (We also know that the last use before
7719 INSN was the output reload we are thinking of deleting, but never
7720 mind that.) Search that range; see if any ref remains. */
7721 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7723 rtx set = single_set (i2);
7725 /* Uses which just store in the pseudo don't count,
7726 since if they are the only uses, they are dead. */
7727 if (set != 0 && SET_DEST (set) == reg)
7728 continue;
7729 if (LABEL_P (i2)
7730 || JUMP_P (i2))
7731 break;
7732 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
7733 && reg_mentioned_p (reg, PATTERN (i2)))
7735 /* Some other ref remains; just delete the output reload we
7736 know to be dead. */
7737 delete_address_reloads (output_reload_insn, insn);
7738 delete_insn (output_reload_insn);
7739 return;
7743 /* Delete the now-dead stores into this pseudo. Note that this
7744 loop also takes care of deleting output_reload_insn. */
7745 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
7747 rtx set = single_set (i2);
7749 if (set != 0 && SET_DEST (set) == reg)
7751 delete_address_reloads (i2, insn);
7752 delete_insn (i2);
7754 if (LABEL_P (i2)
7755 || JUMP_P (i2))
7756 break;
7759 /* For the debugging info, say the pseudo lives in this reload reg. */
7760 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
7761 alter_reg (REGNO (reg), -1);
7763 else
7765 delete_address_reloads (output_reload_insn, insn);
7766 delete_insn (output_reload_insn);
7770 /* We are going to delete DEAD_INSN. Recursively delete loads of
7771 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
7772 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
7773 static void
7774 delete_address_reloads (rtx dead_insn, rtx current_insn)
7776 rtx set = single_set (dead_insn);
7777 rtx set2, dst, prev, next;
7778 if (set)
7780 rtx dst = SET_DEST (set);
7781 if (MEM_P (dst))
7782 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
7784 /* If we deleted the store from a reloaded post_{in,de}c expression,
7785 we can delete the matching adds. */
7786 prev = PREV_INSN (dead_insn);
7787 next = NEXT_INSN (dead_insn);
7788 if (! prev || ! next)
7789 return;
7790 set = single_set (next);
7791 set2 = single_set (prev);
7792 if (! set || ! set2
7793 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
7794 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
7795 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
7796 return;
7797 dst = SET_DEST (set);
7798 if (! rtx_equal_p (dst, SET_DEST (set2))
7799 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
7800 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
7801 || (INTVAL (XEXP (SET_SRC (set), 1))
7802 != -INTVAL (XEXP (SET_SRC (set2), 1))))
7803 return;
7804 delete_related_insns (prev);
7805 delete_related_insns (next);
7808 /* Subfunction of delete_address_reloads: process registers found in X. */
7809 static void
7810 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
7812 rtx prev, set, dst, i2;
7813 int i, j;
7814 enum rtx_code code = GET_CODE (x);
7816 if (code != REG)
7818 const char *fmt = GET_RTX_FORMAT (code);
7819 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7821 if (fmt[i] == 'e')
7822 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
7823 else if (fmt[i] == 'E')
7825 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7826 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
7827 current_insn);
7830 return;
7833 if (spill_reg_order[REGNO (x)] < 0)
7834 return;
7836 /* Scan backwards for the insn that sets x. This might be a way back due
7837 to inheritance. */
7838 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
7840 code = GET_CODE (prev);
7841 if (code == CODE_LABEL || code == JUMP_INSN)
7842 return;
7843 if (!INSN_P (prev))
7844 continue;
7845 if (reg_set_p (x, PATTERN (prev)))
7846 break;
7847 if (reg_referenced_p (x, PATTERN (prev)))
7848 return;
7850 if (! prev || INSN_UID (prev) < reload_first_uid)
7851 return;
7852 /* Check that PREV only sets the reload register. */
7853 set = single_set (prev);
7854 if (! set)
7855 return;
7856 dst = SET_DEST (set);
7857 if (!REG_P (dst)
7858 || ! rtx_equal_p (dst, x))
7859 return;
7860 if (! reg_set_p (dst, PATTERN (dead_insn)))
7862 /* Check if DST was used in a later insn -
7863 it might have been inherited. */
7864 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
7866 if (LABEL_P (i2))
7867 break;
7868 if (! INSN_P (i2))
7869 continue;
7870 if (reg_referenced_p (dst, PATTERN (i2)))
7872 /* If there is a reference to the register in the current insn,
7873 it might be loaded in a non-inherited reload. If no other
7874 reload uses it, that means the register is set before
7875 referenced. */
7876 if (i2 == current_insn)
7878 for (j = n_reloads - 1; j >= 0; j--)
7879 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7880 || reload_override_in[j] == dst)
7881 return;
7882 for (j = n_reloads - 1; j >= 0; j--)
7883 if (rld[j].in && rld[j].reg_rtx == dst)
7884 break;
7885 if (j >= 0)
7886 break;
7888 return;
7890 if (JUMP_P (i2))
7891 break;
7892 /* If DST is still live at CURRENT_INSN, check if it is used for
7893 any reload. Note that even if CURRENT_INSN sets DST, we still
7894 have to check the reloads. */
7895 if (i2 == current_insn)
7897 for (j = n_reloads - 1; j >= 0; j--)
7898 if ((rld[j].reg_rtx == dst && reload_inherited[j])
7899 || reload_override_in[j] == dst)
7900 return;
7901 /* ??? We can't finish the loop here, because dst might be
7902 allocated to a pseudo in this block if no reload in this
7903 block needs any of the classes containing DST - see
7904 spill_hard_reg. There is no easy way to tell this, so we
7905 have to scan till the end of the basic block. */
7907 if (reg_set_p (dst, PATTERN (i2)))
7908 break;
7911 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
7912 reg_reloaded_contents[REGNO (dst)] = -1;
7913 delete_insn (prev);
7916 /* Output reload-insns to reload VALUE into RELOADREG.
7917 VALUE is an autoincrement or autodecrement RTX whose operand
7918 is a register or memory location;
7919 so reloading involves incrementing that location.
7920 IN is either identical to VALUE, or some cheaper place to reload from.
7922 INC_AMOUNT is the number to increment or decrement by (always positive).
7923 This cannot be deduced from VALUE.
7925 Return the instruction that stores into RELOADREG. */
7927 static rtx
7928 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
7930 /* REG or MEM to be copied and incremented. */
7931 rtx incloc = XEXP (value, 0);
7932 /* Nonzero if increment after copying. */
7933 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
7934 rtx last;
7935 rtx inc;
7936 rtx add_insn;
7937 int code;
7938 rtx store;
7939 rtx real_in = in == value ? XEXP (in, 0) : in;
7941 /* No hard register is equivalent to this register after
7942 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
7943 we could inc/dec that register as well (maybe even using it for
7944 the source), but I'm not sure it's worth worrying about. */
7945 if (REG_P (incloc))
7946 reg_last_reload_reg[REGNO (incloc)] = 0;
7948 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
7949 inc_amount = -inc_amount;
7951 inc = GEN_INT (inc_amount);
7953 /* If this is post-increment, first copy the location to the reload reg. */
7954 if (post && real_in != reloadreg)
7955 emit_insn (gen_move_insn (reloadreg, real_in));
7957 if (in == value)
7959 /* See if we can directly increment INCLOC. Use a method similar to
7960 that in gen_reload. */
7962 last = get_last_insn ();
7963 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
7964 gen_rtx_PLUS (GET_MODE (incloc),
7965 incloc, inc)));
7967 code = recog_memoized (add_insn);
7968 if (code >= 0)
7970 extract_insn (add_insn);
7971 if (constrain_operands (1))
7973 /* If this is a pre-increment and we have incremented the value
7974 where it lives, copy the incremented value to RELOADREG to
7975 be used as an address. */
7977 if (! post)
7978 emit_insn (gen_move_insn (reloadreg, incloc));
7980 return add_insn;
7983 delete_insns_since (last);
7986 /* If couldn't do the increment directly, must increment in RELOADREG.
7987 The way we do this depends on whether this is pre- or post-increment.
7988 For pre-increment, copy INCLOC to the reload register, increment it
7989 there, then save back. */
7991 if (! post)
7993 if (in != reloadreg)
7994 emit_insn (gen_move_insn (reloadreg, real_in));
7995 emit_insn (gen_add2_insn (reloadreg, inc));
7996 store = emit_insn (gen_move_insn (incloc, reloadreg));
7998 else
8000 /* Postincrement.
8001 Because this might be a jump insn or a compare, and because RELOADREG
8002 may not be available after the insn in an input reload, we must do
8003 the incrementation before the insn being reloaded for.
8005 We have already copied IN to RELOADREG. Increment the copy in
8006 RELOADREG, save that back, then decrement RELOADREG so it has
8007 the original value. */
8009 emit_insn (gen_add2_insn (reloadreg, inc));
8010 store = emit_insn (gen_move_insn (incloc, reloadreg));
8011 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)));
8014 return store;
8017 #ifdef AUTO_INC_DEC
8018 static void
8019 add_auto_inc_notes (rtx insn, rtx x)
8021 enum rtx_code code = GET_CODE (x);
8022 const char *fmt;
8023 int i, j;
8025 if (code == MEM && auto_inc_p (XEXP (x, 0)))
8027 REG_NOTES (insn)
8028 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
8029 return;
8032 /* Scan all the operand sub-expressions. */
8033 fmt = GET_RTX_FORMAT (code);
8034 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8036 if (fmt[i] == 'e')
8037 add_auto_inc_notes (insn, XEXP (x, i));
8038 else if (fmt[i] == 'E')
8039 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8040 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8043 #endif
8045 /* Copy EH notes from an insn to its reloads. */
8046 static void
8047 copy_eh_notes (rtx insn, rtx x)
8049 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8050 if (eh_note)
8052 for (; x != 0; x = NEXT_INSN (x))
8054 if (may_trap_p (PATTERN (x)))
8055 REG_NOTES (x)
8056 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8057 REG_NOTES (x));
8062 /* This is used by reload pass, that does emit some instructions after
8063 abnormal calls moving basic block end, but in fact it wants to emit
8064 them on the edge. Looks for abnormal call edges, find backward the
8065 proper call and fix the damage.
8067 Similar handle instructions throwing exceptions internally. */
8068 void
8069 fixup_abnormal_edges (void)
8071 bool inserted = false;
8072 basic_block bb;
8074 FOR_EACH_BB (bb)
8076 edge e;
8077 edge_iterator ei;
8079 /* Look for cases we are interested in - calls or instructions causing
8080 exceptions. */
8081 FOR_EACH_EDGE (e, ei, bb->succs)
8083 if (e->flags & EDGE_ABNORMAL_CALL)
8084 break;
8085 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8086 == (EDGE_ABNORMAL | EDGE_EH))
8087 break;
8089 if (e && !CALL_P (BB_END (bb))
8090 && !can_throw_internal (BB_END (bb)))
8092 rtx insn = BB_END (bb), stop = NEXT_INSN (BB_END (bb));
8093 rtx next;
8094 FOR_EACH_EDGE (e, ei, bb->succs)
8095 if (e->flags & EDGE_FALLTHRU)
8096 break;
8097 /* Get past the new insns generated. Allow notes, as the insns may
8098 be already deleted. */
8099 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8100 && !can_throw_internal (insn)
8101 && insn != BB_HEAD (bb))
8102 insn = PREV_INSN (insn);
8103 gcc_assert (CALL_P (insn) || can_throw_internal (insn));
8104 BB_END (bb) = insn;
8105 inserted = true;
8106 insn = NEXT_INSN (insn);
8107 while (insn && insn != stop)
8109 next = NEXT_INSN (insn);
8110 if (INSN_P (insn))
8112 delete_insn (insn);
8114 /* Sometimes there's still the return value USE.
8115 If it's placed after a trapping call (i.e. that
8116 call is the last insn anyway), we have no fallthru
8117 edge. Simply delete this use and don't try to insert
8118 on the non-existent edge. */
8119 if (GET_CODE (PATTERN (insn)) != USE)
8121 /* We're not deleting it, we're moving it. */
8122 INSN_DELETED_P (insn) = 0;
8123 PREV_INSN (insn) = NULL_RTX;
8124 NEXT_INSN (insn) = NULL_RTX;
8126 insert_insn_on_edge (insn, e);
8129 insn = next;
8133 /* We've possibly turned single trapping insn into multiple ones. */
8134 if (flag_non_call_exceptions)
8136 sbitmap blocks;
8137 blocks = sbitmap_alloc (last_basic_block);
8138 sbitmap_ones (blocks);
8139 find_many_sub_basic_blocks (blocks);
8141 if (inserted)
8142 commit_edge_insertions ();