PR middle-end/30761
[official-gcc.git] / gcc / reload1.c
blobc86b2bd429f57e363bd320729a58e901246a77db
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, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
28 #include "machmode.h"
29 #include "hard-reg-set.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "obstack.h"
33 #include "insn-config.h"
34 #include "flags.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "addresses.h"
40 #include "basic-block.h"
41 #include "reload.h"
42 #include "recog.h"
43 #include "output.h"
44 #include "real.h"
45 #include "toplev.h"
46 #include "except.h"
47 #include "tree.h"
48 #include "target.h"
50 /* This file contains the reload pass of the compiler, which is
51 run after register allocation has been done. It checks that
52 each insn is valid (operands required to be in registers really
53 are in registers of the proper class) and fixes up invalid ones
54 by copying values temporarily into registers for the insns
55 that need them.
57 The results of register allocation are described by the vector
58 reg_renumber; the insns still contain pseudo regs, but reg_renumber
59 can be used to find which hard reg, if any, a pseudo reg is in.
61 The technique we always use is to free up a few hard regs that are
62 called ``reload regs'', and for each place where a pseudo reg
63 must be in a hard reg, copy it temporarily into one of the reload regs.
65 Reload regs are allocated locally for every instruction that needs
66 reloads. When there are pseudos which are allocated to a register that
67 has been chosen as a reload reg, such pseudos must be ``spilled''.
68 This means that they go to other hard regs, or to stack slots if no other
69 available hard regs can be found. Spilling can invalidate more
70 insns, requiring additional need for reloads, so we must keep checking
71 until the process stabilizes.
73 For machines with different classes of registers, we must keep track
74 of the register class needed for each reload, and make sure that
75 we allocate enough reload registers of each class.
77 The file reload.c contains the code that checks one insn for
78 validity and reports the reloads that it needs. This file
79 is in charge of scanning the entire rtl code, accumulating the
80 reload needs, spilling, assigning reload registers to use for
81 fixing up each insn, and generating the new insns to copy values
82 into the reload registers. */
84 /* During reload_as_needed, element N contains a REG rtx for the hard reg
85 into which reg N has been reloaded (perhaps for a previous insn). */
86 static rtx *reg_last_reload_reg;
88 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
89 for an output reload that stores into reg N. */
90 static regset_head reg_has_output_reload;
92 /* Indicates which hard regs are reload-registers for an output reload
93 in the current insn. */
94 static HARD_REG_SET reg_is_output_reload;
96 /* Element N is the constant value to which pseudo reg N is equivalent,
97 or zero if pseudo reg N is not equivalent to a constant.
98 find_reloads looks at this in order to replace pseudo reg N
99 with the constant it stands for. */
100 rtx *reg_equiv_constant;
102 /* Element N is an invariant value to which pseudo reg N is equivalent.
103 eliminate_regs_in_insn uses this to replace pseudos in particular
104 contexts. */
105 rtx *reg_equiv_invariant;
107 /* Element N is a memory location to which pseudo reg N is equivalent,
108 prior to any register elimination (such as frame pointer to stack
109 pointer). Depending on whether or not it is a valid address, this value
110 is transferred to either reg_equiv_address or reg_equiv_mem. */
111 rtx *reg_equiv_memory_loc;
113 /* We allocate reg_equiv_memory_loc inside a varray so that the garbage
114 collector can keep track of what is inside. */
115 VEC(rtx,gc) *reg_equiv_memory_loc_vec;
117 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
118 This is used when the address is not valid as a memory address
119 (because its displacement is too big for the machine.) */
120 rtx *reg_equiv_address;
122 /* Element N is the memory slot to which pseudo reg N is equivalent,
123 or zero if pseudo reg N is not equivalent to a memory slot. */
124 rtx *reg_equiv_mem;
126 /* Element N is an EXPR_LIST of REG_EQUIVs containing MEMs with
127 alternate representations of the location of pseudo reg N. */
128 rtx *reg_equiv_alt_mem_list;
130 /* Widest width in which each pseudo reg is referred to (via subreg). */
131 static unsigned int *reg_max_ref_width;
133 /* Element N is the list of insns that initialized reg N from its equivalent
134 constant or memory slot. */
135 rtx *reg_equiv_init;
136 int reg_equiv_init_size;
138 /* Vector to remember old contents of reg_renumber before spilling. */
139 static short *reg_old_renumber;
141 /* During reload_as_needed, element N contains the last pseudo regno reloaded
142 into hard register N. If that pseudo reg occupied more than one register,
143 reg_reloaded_contents points to that pseudo for each spill register in
144 use; all of these must remain set for an inheritance to occur. */
145 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
147 /* During reload_as_needed, element N contains the insn for which
148 hard register N was last used. Its contents are significant only
149 when reg_reloaded_valid is set for this register. */
150 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
152 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
153 static HARD_REG_SET reg_reloaded_valid;
154 /* Indicate if the register was dead at the end of the reload.
155 This is only valid if reg_reloaded_contents is set and valid. */
156 static HARD_REG_SET reg_reloaded_dead;
158 /* Indicate whether the register's current value is one that is not
159 safe to retain across a call, even for registers that are normally
160 call-saved. */
161 static HARD_REG_SET reg_reloaded_call_part_clobbered;
163 /* Number of spill-regs so far; number of valid elements of spill_regs. */
164 static int n_spills;
166 /* In parallel with spill_regs, contains REG rtx's for those regs.
167 Holds the last rtx used for any given reg, or 0 if it has never
168 been used for spilling yet. This rtx is reused, provided it has
169 the proper mode. */
170 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
172 /* In parallel with spill_regs, contains nonzero for a spill reg
173 that was stored after the last time it was used.
174 The precise value is the insn generated to do the store. */
175 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
177 /* This is the register that was stored with spill_reg_store. This is a
178 copy of reload_out / reload_out_reg when the value was stored; if
179 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
180 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
182 /* This table is the inverse mapping of spill_regs:
183 indexed by hard reg number,
184 it contains the position of that reg in spill_regs,
185 or -1 for something that is not in spill_regs.
187 ?!? This is no longer accurate. */
188 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
190 /* This reg set indicates registers that can't be used as spill registers for
191 the currently processed insn. These are the hard registers which are live
192 during the insn, but not allocated to pseudos, as well as fixed
193 registers. */
194 static HARD_REG_SET bad_spill_regs;
196 /* These are the hard registers that can't be used as spill register for any
197 insn. This includes registers used for user variables and registers that
198 we can't eliminate. A register that appears in this set also can't be used
199 to retry register allocation. */
200 static HARD_REG_SET bad_spill_regs_global;
202 /* Describes order of use of registers for reloading
203 of spilled pseudo-registers. `n_spills' is the number of
204 elements that are actually valid; new ones are added at the end.
206 Both spill_regs and spill_reg_order are used on two occasions:
207 once during find_reload_regs, where they keep track of the spill registers
208 for a single insn, but also during reload_as_needed where they show all
209 the registers ever used by reload. For the latter case, the information
210 is calculated during finish_spills. */
211 static short spill_regs[FIRST_PSEUDO_REGISTER];
213 /* This vector of reg sets indicates, for each pseudo, which hard registers
214 may not be used for retrying global allocation because the register was
215 formerly spilled from one of them. If we allowed reallocating a pseudo to
216 a register that it was already allocated to, reload might not
217 terminate. */
218 static HARD_REG_SET *pseudo_previous_regs;
220 /* This vector of reg sets indicates, for each pseudo, which hard
221 registers may not be used for retrying global allocation because they
222 are used as spill registers during one of the insns in which the
223 pseudo is live. */
224 static HARD_REG_SET *pseudo_forbidden_regs;
226 /* All hard regs that have been used as spill registers for any insn are
227 marked in this set. */
228 static HARD_REG_SET used_spill_regs;
230 /* Index of last register assigned as a spill register. We allocate in
231 a round-robin fashion. */
232 static int last_spill_reg;
234 /* Nonzero if indirect addressing is supported on the machine; this means
235 that spilling (REG n) does not require reloading it into a register in
236 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
237 value indicates the level of indirect addressing supported, e.g., two
238 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
239 a hard register. */
240 static char spill_indirect_levels;
242 /* Nonzero if indirect addressing is supported when the innermost MEM is
243 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
244 which these are valid is the same as spill_indirect_levels, above. */
245 char indirect_symref_ok;
247 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
248 char double_reg_address_ok;
250 /* Record the stack slot for each spilled hard register. */
251 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
253 /* Width allocated so far for that stack slot. */
254 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
256 /* Record which pseudos needed to be spilled. */
257 static regset_head spilled_pseudos;
259 /* Used for communication between order_regs_for_reload and count_pseudo.
260 Used to avoid counting one pseudo twice. */
261 static regset_head pseudos_counted;
263 /* First uid used by insns created by reload in this function.
264 Used in find_equiv_reg. */
265 int reload_first_uid;
267 /* Flag set by local-alloc or global-alloc if anything is live in
268 a call-clobbered reg across calls. */
269 int caller_save_needed;
271 /* Set to 1 while reload_as_needed is operating.
272 Required by some machines to handle any generated moves differently. */
273 int reload_in_progress = 0;
275 /* These arrays record the insn_code of insns that may be needed to
276 perform input and output reloads of special objects. They provide a
277 place to pass a scratch register. */
278 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
279 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
281 /* This obstack is used for allocation of rtl during register elimination.
282 The allocated storage can be freed once find_reloads has processed the
283 insn. */
284 static struct obstack reload_obstack;
286 /* Points to the beginning of the reload_obstack. All insn_chain structures
287 are allocated first. */
288 static char *reload_startobj;
290 /* The point after all insn_chain structures. Used to quickly deallocate
291 memory allocated in copy_reloads during calculate_needs_all_insns. */
292 static char *reload_firstobj;
294 /* This points before all local rtl generated by register elimination.
295 Used to quickly free all memory after processing one insn. */
296 static char *reload_insn_firstobj;
298 /* List of insn_chain instructions, one for every insn that reload needs to
299 examine. */
300 struct insn_chain *reload_insn_chain;
302 /* List of all insns needing reloads. */
303 static struct insn_chain *insns_need_reload;
305 /* This structure is used to record information about register eliminations.
306 Each array entry describes one possible way of eliminating a register
307 in favor of another. If there is more than one way of eliminating a
308 particular register, the most preferred should be specified first. */
310 struct elim_table
312 int from; /* Register number to be eliminated. */
313 int to; /* Register number used as replacement. */
314 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
315 int can_eliminate; /* Nonzero if this elimination can be done. */
316 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
317 insns made by reload. */
318 HOST_WIDE_INT offset; /* Current offset between the two regs. */
319 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
320 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
321 rtx from_rtx; /* REG rtx for the register to be eliminated.
322 We cannot simply compare the number since
323 we might then spuriously replace a hard
324 register corresponding to a pseudo
325 assigned to the reg to be eliminated. */
326 rtx to_rtx; /* REG rtx for the replacement. */
329 static struct elim_table *reg_eliminate = 0;
331 /* This is an intermediate structure to initialize the table. It has
332 exactly the members provided by ELIMINABLE_REGS. */
333 static const struct elim_table_1
335 const int from;
336 const int to;
337 } reg_eliminate_1[] =
339 /* If a set of eliminable registers was specified, define the table from it.
340 Otherwise, default to the normal case of the frame pointer being
341 replaced by the stack pointer. */
343 #ifdef ELIMINABLE_REGS
344 ELIMINABLE_REGS;
345 #else
346 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
347 #endif
349 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
351 /* Record the number of pending eliminations that have an offset not equal
352 to their initial offset. If nonzero, we use a new copy of each
353 replacement result in any insns encountered. */
354 int num_not_at_initial_offset;
356 /* Count the number of registers that we may be able to eliminate. */
357 static int num_eliminable;
358 /* And the number of registers that are equivalent to a constant that
359 can be eliminated to frame_pointer / arg_pointer + constant. */
360 static int num_eliminable_invariants;
362 /* For each label, we record the offset of each elimination. If we reach
363 a label by more than one path and an offset differs, we cannot do the
364 elimination. This information is indexed by the difference of the
365 number of the label and the first label number. We can't offset the
366 pointer itself as this can cause problems on machines with segmented
367 memory. The first table is an array of flags that records whether we
368 have yet encountered a label and the second table is an array of arrays,
369 one entry in the latter array for each elimination. */
371 static int first_label_num;
372 static char *offsets_known_at;
373 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
375 /* Number of labels in the current function. */
377 static int num_labels;
379 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
380 static void maybe_fix_stack_asms (void);
381 static void copy_reloads (struct insn_chain *);
382 static void calculate_needs_all_insns (int);
383 static int find_reg (struct insn_chain *, int);
384 static void find_reload_regs (struct insn_chain *);
385 static void select_reload_regs (void);
386 static void delete_caller_save_insns (void);
388 static void spill_failure (rtx, enum reg_class);
389 static void count_spilled_pseudo (int, int, int);
390 static void delete_dead_insn (rtx);
391 static void alter_reg (int, int);
392 static void set_label_offsets (rtx, rtx, int);
393 static void check_eliminable_occurrences (rtx);
394 static void elimination_effects (rtx, enum machine_mode);
395 static int eliminate_regs_in_insn (rtx, int);
396 static void update_eliminable_offsets (void);
397 static void mark_not_eliminable (rtx, rtx, void *);
398 static void set_initial_elim_offsets (void);
399 static bool verify_initial_elim_offsets (void);
400 static void set_initial_label_offsets (void);
401 static void set_offsets_for_label (rtx);
402 static void init_elim_table (void);
403 static void update_eliminables (HARD_REG_SET *);
404 static void spill_hard_reg (unsigned int, int);
405 static int finish_spills (int);
406 static void scan_paradoxical_subregs (rtx);
407 static void count_pseudo (int);
408 static void order_regs_for_reload (struct insn_chain *);
409 static void reload_as_needed (int);
410 static void forget_old_reloads_1 (rtx, rtx, void *);
411 static void forget_marked_reloads (regset);
412 static int reload_reg_class_lower (const void *, const void *);
413 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
414 enum machine_mode);
415 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
416 enum machine_mode);
417 static int reload_reg_free_p (unsigned int, int, enum reload_type);
418 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
419 rtx, rtx, int, int);
420 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
421 rtx, rtx, int, int);
422 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
423 static int allocate_reload_reg (struct insn_chain *, int, int);
424 static int conflicts_with_override (rtx);
425 static void failed_reload (rtx, int);
426 static int set_reload_reg (int, int);
427 static void choose_reload_regs_init (struct insn_chain *, rtx *);
428 static void choose_reload_regs (struct insn_chain *);
429 static void merge_assigned_reloads (rtx);
430 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
431 rtx, int);
432 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
433 int);
434 static void do_input_reload (struct insn_chain *, struct reload *, int);
435 static void do_output_reload (struct insn_chain *, struct reload *, int);
436 static bool inherit_piecemeal_p (int, int);
437 static void emit_reload_insns (struct insn_chain *);
438 static void delete_output_reload (rtx, int, int);
439 static void delete_address_reloads (rtx, rtx);
440 static void delete_address_reloads_1 (rtx, rtx, rtx);
441 static rtx inc_for_reload (rtx, rtx, rtx, int);
442 #ifdef AUTO_INC_DEC
443 static void add_auto_inc_notes (rtx, rtx);
444 #endif
445 static void copy_eh_notes (rtx, rtx);
446 static int reloads_conflict (int, int);
447 static rtx gen_reload (rtx, rtx, int, enum reload_type);
448 static rtx emit_insn_if_valid_for_reload (rtx);
450 /* Initialize the reload pass once per compilation. */
452 void
453 init_reload (void)
455 int i;
457 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
458 Set spill_indirect_levels to the number of levels such addressing is
459 permitted, zero if it is not permitted at all. */
461 rtx tem
462 = gen_rtx_MEM (Pmode,
463 gen_rtx_PLUS (Pmode,
464 gen_rtx_REG (Pmode,
465 LAST_VIRTUAL_REGISTER + 1),
466 GEN_INT (4)));
467 spill_indirect_levels = 0;
469 while (memory_address_p (QImode, tem))
471 spill_indirect_levels++;
472 tem = gen_rtx_MEM (Pmode, tem);
475 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
477 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
478 indirect_symref_ok = memory_address_p (QImode, tem);
480 /* See if reg+reg is a valid (and offsettable) address. */
482 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
484 tem = gen_rtx_PLUS (Pmode,
485 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
486 gen_rtx_REG (Pmode, i));
488 /* This way, we make sure that reg+reg is an offsettable address. */
489 tem = plus_constant (tem, 4);
491 if (memory_address_p (QImode, tem))
493 double_reg_address_ok = 1;
494 break;
498 /* Initialize obstack for our rtl allocation. */
499 gcc_obstack_init (&reload_obstack);
500 reload_startobj = obstack_alloc (&reload_obstack, 0);
502 INIT_REG_SET (&spilled_pseudos);
503 INIT_REG_SET (&pseudos_counted);
506 /* List of insn chains that are currently unused. */
507 static struct insn_chain *unused_insn_chains = 0;
509 /* Allocate an empty insn_chain structure. */
510 struct insn_chain *
511 new_insn_chain (void)
513 struct insn_chain *c;
515 if (unused_insn_chains == 0)
517 c = obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
518 INIT_REG_SET (&c->live_throughout);
519 INIT_REG_SET (&c->dead_or_set);
521 else
523 c = unused_insn_chains;
524 unused_insn_chains = c->next;
526 c->is_caller_save_insn = 0;
527 c->need_operand_change = 0;
528 c->need_reload = 0;
529 c->need_elim = 0;
530 return c;
533 /* Small utility function to set all regs in hard reg set TO which are
534 allocated to pseudos in regset FROM. */
536 void
537 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
539 unsigned int regno;
540 reg_set_iterator rsi;
542 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
544 int r = reg_renumber[regno];
545 int nregs;
547 if (r < 0)
549 /* reload_combine uses the information from
550 BASIC_BLOCK->global_live_at_start, which might still
551 contain registers that have not actually been allocated
552 since they have an equivalence. */
553 gcc_assert (reload_completed);
555 else
557 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (regno)];
558 while (nregs-- > 0)
559 SET_HARD_REG_BIT (*to, r + nregs);
564 /* Replace all pseudos found in LOC with their corresponding
565 equivalences. */
567 static void
568 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
570 rtx x = *loc;
571 enum rtx_code code;
572 const char *fmt;
573 int i, j;
575 if (! x)
576 return;
578 code = GET_CODE (x);
579 if (code == REG)
581 unsigned int regno = REGNO (x);
583 if (regno < FIRST_PSEUDO_REGISTER)
584 return;
586 x = eliminate_regs (x, mem_mode, usage);
587 if (x != *loc)
589 *loc = x;
590 replace_pseudos_in (loc, mem_mode, usage);
591 return;
594 if (reg_equiv_constant[regno])
595 *loc = reg_equiv_constant[regno];
596 else if (reg_equiv_mem[regno])
597 *loc = reg_equiv_mem[regno];
598 else if (reg_equiv_address[regno])
599 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
600 else
602 gcc_assert (!REG_P (regno_reg_rtx[regno])
603 || REGNO (regno_reg_rtx[regno]) != regno);
604 *loc = regno_reg_rtx[regno];
607 return;
609 else if (code == MEM)
611 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
612 return;
615 /* Process each of our operands recursively. */
616 fmt = GET_RTX_FORMAT (code);
617 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
618 if (*fmt == 'e')
619 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
620 else if (*fmt == 'E')
621 for (j = 0; j < XVECLEN (x, i); j++)
622 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
626 /* Global variables used by reload and its subroutines. */
628 /* Set during calculate_needs if an insn needs register elimination. */
629 static int something_needs_elimination;
630 /* Set during calculate_needs if an insn needs an operand changed. */
631 static int something_needs_operands_changed;
633 /* Nonzero means we couldn't get enough spill regs. */
634 static int failure;
636 /* Main entry point for the reload pass.
638 FIRST is the first insn of the function being compiled.
640 GLOBAL nonzero means we were called from global_alloc
641 and should attempt to reallocate any pseudoregs that we
642 displace from hard regs we will use for reloads.
643 If GLOBAL is zero, we do not have enough information to do that,
644 so any pseudo reg that is spilled must go to the stack.
646 Return value is nonzero if reload failed
647 and we must not do any more for this function. */
650 reload (rtx first, int global)
652 int i;
653 rtx insn;
654 struct elim_table *ep;
655 basic_block bb;
657 /* Make sure even insns with volatile mem refs are recognizable. */
658 init_recog ();
660 failure = 0;
662 reload_firstobj = obstack_alloc (&reload_obstack, 0);
664 /* Make sure that the last insn in the chain
665 is not something that needs reloading. */
666 emit_note (NOTE_INSN_DELETED);
668 /* Enable find_equiv_reg to distinguish insns made by reload. */
669 reload_first_uid = get_max_uid ();
671 #ifdef SECONDARY_MEMORY_NEEDED
672 /* Initialize the secondary memory table. */
673 clear_secondary_mem ();
674 #endif
676 /* We don't have a stack slot for any spill reg yet. */
677 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
678 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
680 /* Initialize the save area information for caller-save, in case some
681 are needed. */
682 init_save_areas ();
684 /* Compute which hard registers are now in use
685 as homes for pseudo registers.
686 This is done here rather than (eg) in global_alloc
687 because this point is reached even if not optimizing. */
688 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
689 mark_home_live (i);
691 /* A function that receives a nonlocal goto must save all call-saved
692 registers. */
693 if (current_function_has_nonlocal_label)
694 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
695 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
696 regs_ever_live[i] = 1;
698 /* Find all the pseudo registers that didn't get hard regs
699 but do have known equivalent constants or memory slots.
700 These include parameters (known equivalent to parameter slots)
701 and cse'd or loop-moved constant memory addresses.
703 Record constant equivalents in reg_equiv_constant
704 so they will be substituted by find_reloads.
705 Record memory equivalents in reg_mem_equiv so they can
706 be substituted eventually by altering the REG-rtx's. */
708 reg_equiv_constant = XCNEWVEC (rtx, max_regno);
709 reg_equiv_invariant = XCNEWVEC (rtx, max_regno);
710 reg_equiv_mem = XCNEWVEC (rtx, max_regno);
711 reg_equiv_alt_mem_list = XCNEWVEC (rtx, max_regno);
712 reg_equiv_address = XCNEWVEC (rtx, max_regno);
713 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
714 reg_old_renumber = XCNEWVEC (short, max_regno);
715 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
716 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
717 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
719 CLEAR_HARD_REG_SET (bad_spill_regs_global);
721 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
722 to. Also find all paradoxical subregs and find largest such for
723 each pseudo. */
725 num_eliminable_invariants = 0;
726 for (insn = first; insn; insn = NEXT_INSN (insn))
728 rtx set = single_set (insn);
730 /* We may introduce USEs that we want to remove at the end, so
731 we'll mark them with QImode. Make sure there are no
732 previously-marked insns left by say regmove. */
733 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
734 && GET_MODE (insn) != VOIDmode)
735 PUT_MODE (insn, VOIDmode);
737 if (INSN_P (insn))
738 scan_paradoxical_subregs (PATTERN (insn));
740 if (set != 0 && REG_P (SET_DEST (set)))
742 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
743 rtx x;
745 if (! note)
746 continue;
748 i = REGNO (SET_DEST (set));
749 x = XEXP (note, 0);
751 if (i <= LAST_VIRTUAL_REGISTER)
752 continue;
754 if (! function_invariant_p (x)
755 || ! flag_pic
756 /* A function invariant is often CONSTANT_P but may
757 include a register. We promise to only pass
758 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
759 || (CONSTANT_P (x)
760 && LEGITIMATE_PIC_OPERAND_P (x)))
762 /* It can happen that a REG_EQUIV note contains a MEM
763 that is not a legitimate memory operand. As later
764 stages of reload assume that all addresses found
765 in the reg_equiv_* arrays were originally legitimate,
766 we ignore such REG_EQUIV notes. */
767 if (memory_operand (x, VOIDmode))
769 /* Always unshare the equivalence, so we can
770 substitute into this insn without touching the
771 equivalence. */
772 reg_equiv_memory_loc[i] = copy_rtx (x);
774 else if (function_invariant_p (x))
776 if (GET_CODE (x) == PLUS)
778 /* This is PLUS of frame pointer and a constant,
779 and might be shared. Unshare it. */
780 reg_equiv_invariant[i] = copy_rtx (x);
781 num_eliminable_invariants++;
783 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
785 reg_equiv_invariant[i] = x;
786 num_eliminable_invariants++;
788 else if (LEGITIMATE_CONSTANT_P (x))
789 reg_equiv_constant[i] = x;
790 else
792 reg_equiv_memory_loc[i]
793 = force_const_mem (GET_MODE (SET_DEST (set)), x);
794 if (! reg_equiv_memory_loc[i])
795 reg_equiv_init[i] = NULL_RTX;
798 else
800 reg_equiv_init[i] = NULL_RTX;
801 continue;
804 else
805 reg_equiv_init[i] = NULL_RTX;
809 if (dump_file)
810 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
811 if (reg_equiv_init[i])
813 fprintf (dump_file, "init_insns for %u: ", i);
814 print_inline_rtx (dump_file, reg_equiv_init[i], 20);
815 fprintf (dump_file, "\n");
818 init_elim_table ();
820 first_label_num = get_first_label_num ();
821 num_labels = max_label_num () - first_label_num;
823 /* Allocate the tables used to store offset information at labels. */
824 /* We used to use alloca here, but the size of what it would try to
825 allocate would occasionally cause it to exceed the stack limit and
826 cause a core dump. */
827 offsets_known_at = XNEWVEC (char, num_labels);
828 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
830 /* Alter each pseudo-reg rtx to contain its hard reg number.
831 Assign stack slots to the pseudos that lack hard regs or equivalents.
832 Do not touch virtual registers. */
834 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
835 alter_reg (i, -1);
837 /* If we have some registers we think can be eliminated, scan all insns to
838 see if there is an insn that sets one of these registers to something
839 other than itself plus a constant. If so, the register cannot be
840 eliminated. Doing this scan here eliminates an extra pass through the
841 main reload loop in the most common case where register elimination
842 cannot be done. */
843 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
844 if (INSN_P (insn))
845 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
847 maybe_fix_stack_asms ();
849 insns_need_reload = 0;
850 something_needs_elimination = 0;
852 /* Initialize to -1, which means take the first spill register. */
853 last_spill_reg = -1;
855 /* Spill any hard regs that we know we can't eliminate. */
856 CLEAR_HARD_REG_SET (used_spill_regs);
857 /* There can be multiple ways to eliminate a register;
858 they should be listed adjacently.
859 Elimination for any register fails only if all possible ways fail. */
860 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
862 int from = ep->from;
863 int can_eliminate = 0;
866 can_eliminate |= ep->can_eliminate;
867 ep++;
869 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
870 if (! can_eliminate)
871 spill_hard_reg (from, 1);
874 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
875 if (frame_pointer_needed)
876 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
877 #endif
878 finish_spills (global);
880 /* From now on, we may need to generate moves differently. We may also
881 allow modifications of insns which cause them to not be recognized.
882 Any such modifications will be cleaned up during reload itself. */
883 reload_in_progress = 1;
885 /* This loop scans the entire function each go-round
886 and repeats until one repetition spills no additional hard regs. */
887 for (;;)
889 int something_changed;
890 int did_spill;
891 HOST_WIDE_INT starting_frame_size;
893 starting_frame_size = get_frame_size ();
895 set_initial_elim_offsets ();
896 set_initial_label_offsets ();
898 /* For each pseudo register that has an equivalent location defined,
899 try to eliminate any eliminable registers (such as the frame pointer)
900 assuming initial offsets for the replacement register, which
901 is the normal case.
903 If the resulting location is directly addressable, substitute
904 the MEM we just got directly for the old REG.
906 If it is not addressable but is a constant or the sum of a hard reg
907 and constant, it is probably not addressable because the constant is
908 out of range, in that case record the address; we will generate
909 hairy code to compute the address in a register each time it is
910 needed. Similarly if it is a hard register, but one that is not
911 valid as an address register.
913 If the location is not addressable, but does not have one of the
914 above forms, assign a stack slot. We have to do this to avoid the
915 potential of producing lots of reloads if, e.g., a location involves
916 a pseudo that didn't get a hard register and has an equivalent memory
917 location that also involves a pseudo that didn't get a hard register.
919 Perhaps at some point we will improve reload_when_needed handling
920 so this problem goes away. But that's very hairy. */
922 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
923 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
925 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
927 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
928 XEXP (x, 0)))
929 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
930 else if (CONSTANT_P (XEXP (x, 0))
931 || (REG_P (XEXP (x, 0))
932 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
933 || (GET_CODE (XEXP (x, 0)) == PLUS
934 && REG_P (XEXP (XEXP (x, 0), 0))
935 && (REGNO (XEXP (XEXP (x, 0), 0))
936 < FIRST_PSEUDO_REGISTER)
937 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
938 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
939 else
941 /* Make a new stack slot. Then indicate that something
942 changed so we go back and recompute offsets for
943 eliminable registers because the allocation of memory
944 below might change some offset. reg_equiv_{mem,address}
945 will be set up for this pseudo on the next pass around
946 the loop. */
947 reg_equiv_memory_loc[i] = 0;
948 reg_equiv_init[i] = 0;
949 alter_reg (i, -1);
953 if (caller_save_needed)
954 setup_save_areas ();
956 /* If we allocated another stack slot, redo elimination bookkeeping. */
957 if (starting_frame_size != get_frame_size ())
958 continue;
959 if (starting_frame_size && cfun->stack_alignment_needed)
961 /* If we have a stack frame, we must align it now. The
962 stack size may be a part of the offset computation for
963 register elimination. So if this changes the stack size,
964 then repeat the elimination bookkeeping. We don't
965 realign when there is no stack, as that will cause a
966 stack frame when none is needed should
967 STARTING_FRAME_OFFSET not be already aligned to
968 STACK_BOUNDARY. */
969 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
970 if (starting_frame_size != get_frame_size ())
971 continue;
974 if (caller_save_needed)
976 save_call_clobbered_regs ();
977 /* That might have allocated new insn_chain structures. */
978 reload_firstobj = obstack_alloc (&reload_obstack, 0);
981 calculate_needs_all_insns (global);
983 CLEAR_REG_SET (&spilled_pseudos);
984 did_spill = 0;
986 something_changed = 0;
988 /* If we allocated any new memory locations, make another pass
989 since it might have changed elimination offsets. */
990 if (starting_frame_size != get_frame_size ())
991 something_changed = 1;
993 /* Even if the frame size remained the same, we might still have
994 changed elimination offsets, e.g. if find_reloads called
995 force_const_mem requiring the back end to allocate a constant
996 pool base register that needs to be saved on the stack. */
997 else if (!verify_initial_elim_offsets ())
998 something_changed = 1;
1001 HARD_REG_SET to_spill;
1002 CLEAR_HARD_REG_SET (to_spill);
1003 update_eliminables (&to_spill);
1004 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
1006 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1007 if (TEST_HARD_REG_BIT (to_spill, i))
1009 spill_hard_reg (i, 1);
1010 did_spill = 1;
1012 /* Regardless of the state of spills, if we previously had
1013 a register that we thought we could eliminate, but now can
1014 not eliminate, we must run another pass.
1016 Consider pseudos which have an entry in reg_equiv_* which
1017 reference an eliminable register. We must make another pass
1018 to update reg_equiv_* so that we do not substitute in the
1019 old value from when we thought the elimination could be
1020 performed. */
1021 something_changed = 1;
1025 select_reload_regs ();
1026 if (failure)
1027 goto failed;
1029 if (insns_need_reload != 0 || did_spill)
1030 something_changed |= finish_spills (global);
1032 if (! something_changed)
1033 break;
1035 if (caller_save_needed)
1036 delete_caller_save_insns ();
1038 obstack_free (&reload_obstack, reload_firstobj);
1041 /* If global-alloc was run, notify it of any register eliminations we have
1042 done. */
1043 if (global)
1044 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1045 if (ep->can_eliminate)
1046 mark_elimination (ep->from, ep->to);
1048 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1049 If that insn didn't set the register (i.e., it copied the register to
1050 memory), just delete that insn instead of the equivalencing insn plus
1051 anything now dead. If we call delete_dead_insn on that insn, we may
1052 delete the insn that actually sets the register if the register dies
1053 there and that is incorrect. */
1055 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1057 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1059 rtx list;
1060 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1062 rtx equiv_insn = XEXP (list, 0);
1064 /* If we already deleted the insn or if it may trap, we can't
1065 delete it. The latter case shouldn't happen, but can
1066 if an insn has a variable address, gets a REG_EH_REGION
1067 note added to it, and then gets converted into a load
1068 from a constant address. */
1069 if (NOTE_P (equiv_insn)
1070 || can_throw_internal (equiv_insn))
1072 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1073 delete_dead_insn (equiv_insn);
1074 else
1075 SET_INSN_DELETED (equiv_insn);
1080 /* Use the reload registers where necessary
1081 by generating move instructions to move the must-be-register
1082 values into or out of the reload registers. */
1084 if (insns_need_reload != 0 || something_needs_elimination
1085 || something_needs_operands_changed)
1087 HOST_WIDE_INT old_frame_size = get_frame_size ();
1089 reload_as_needed (global);
1091 gcc_assert (old_frame_size == get_frame_size ());
1093 gcc_assert (verify_initial_elim_offsets ());
1096 /* If we were able to eliminate the frame pointer, show that it is no
1097 longer live at the start of any basic block. If it ls live by
1098 virtue of being in a pseudo, that pseudo will be marked live
1099 and hence the frame pointer will be known to be live via that
1100 pseudo. */
1102 if (! frame_pointer_needed)
1103 FOR_EACH_BB (bb)
1104 CLEAR_REGNO_REG_SET (bb->il.rtl->global_live_at_start,
1105 HARD_FRAME_POINTER_REGNUM);
1107 /* Come here (with failure set nonzero) if we can't get enough spill
1108 regs. */
1109 failed:
1111 CLEAR_REG_SET (&spilled_pseudos);
1112 reload_in_progress = 0;
1114 /* Now eliminate all pseudo regs by modifying them into
1115 their equivalent memory references.
1116 The REG-rtx's for the pseudos are modified in place,
1117 so all insns that used to refer to them now refer to memory.
1119 For a reg that has a reg_equiv_address, all those insns
1120 were changed by reloading so that no insns refer to it any longer;
1121 but the DECL_RTL of a variable decl may refer to it,
1122 and if so this causes the debugging info to mention the variable. */
1124 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1126 rtx addr = 0;
1128 if (reg_equiv_mem[i])
1129 addr = XEXP (reg_equiv_mem[i], 0);
1131 if (reg_equiv_address[i])
1132 addr = reg_equiv_address[i];
1134 if (addr)
1136 if (reg_renumber[i] < 0)
1138 rtx reg = regno_reg_rtx[i];
1140 REG_USERVAR_P (reg) = 0;
1141 PUT_CODE (reg, MEM);
1142 XEXP (reg, 0) = addr;
1143 if (reg_equiv_memory_loc[i])
1144 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1145 else
1147 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1148 MEM_ATTRS (reg) = 0;
1150 MEM_NOTRAP_P (reg) = 1;
1152 else if (reg_equiv_mem[i])
1153 XEXP (reg_equiv_mem[i], 0) = addr;
1157 /* We must set reload_completed now since the cleanup_subreg_operands call
1158 below will re-recognize each insn and reload may have generated insns
1159 which are only valid during and after reload. */
1160 reload_completed = 1;
1162 /* Make a pass over all the insns and delete all USEs which we inserted
1163 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1164 notes. Delete all CLOBBER insns, except those that refer to the return
1165 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1166 from misarranging variable-array code, and simplify (subreg (reg))
1167 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1168 are no longer useful or accurate. Strip and regenerate REG_INC notes
1169 that may have been moved around. */
1171 for (insn = first; insn; insn = NEXT_INSN (insn))
1172 if (INSN_P (insn))
1174 rtx *pnote;
1176 /* Clean up invalid ASMs so that they don't confuse later passes.
1177 See PR 21299. */
1178 if (asm_noperands (PATTERN (insn)) >= 0)
1180 extract_insn (insn);
1181 if (!constrain_operands (1))
1183 error_for_asm (insn,
1184 "%<asm%> operand has impossible constraints");
1185 delete_insn (insn);
1186 continue;
1190 if (CALL_P (insn))
1191 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1192 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1194 if ((GET_CODE (PATTERN (insn)) == USE
1195 /* We mark with QImode USEs introduced by reload itself. */
1196 && (GET_MODE (insn) == QImode
1197 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1198 || (GET_CODE (PATTERN (insn)) == CLOBBER
1199 && (!MEM_P (XEXP (PATTERN (insn), 0))
1200 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1201 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1202 && XEXP (XEXP (PATTERN (insn), 0), 0)
1203 != stack_pointer_rtx))
1204 && (!REG_P (XEXP (PATTERN (insn), 0))
1205 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1207 delete_insn (insn);
1208 continue;
1211 /* Some CLOBBERs may survive until here and still reference unassigned
1212 pseudos with const equivalent, which may in turn cause ICE in later
1213 passes if the reference remains in place. */
1214 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1215 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1216 VOIDmode, PATTERN (insn));
1218 /* Discard obvious no-ops, even without -O. This optimization
1219 is fast and doesn't interfere with debugging. */
1220 if (NONJUMP_INSN_P (insn)
1221 && GET_CODE (PATTERN (insn)) == SET
1222 && REG_P (SET_SRC (PATTERN (insn)))
1223 && REG_P (SET_DEST (PATTERN (insn)))
1224 && (REGNO (SET_SRC (PATTERN (insn)))
1225 == REGNO (SET_DEST (PATTERN (insn)))))
1227 delete_insn (insn);
1228 continue;
1231 pnote = &REG_NOTES (insn);
1232 while (*pnote != 0)
1234 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1235 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1236 || REG_NOTE_KIND (*pnote) == REG_INC
1237 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1238 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1239 *pnote = XEXP (*pnote, 1);
1240 else
1241 pnote = &XEXP (*pnote, 1);
1244 #ifdef AUTO_INC_DEC
1245 add_auto_inc_notes (insn, PATTERN (insn));
1246 #endif
1248 /* And simplify (subreg (reg)) if it appears as an operand. */
1249 cleanup_subreg_operands (insn);
1252 /* If we are doing stack checking, give a warning if this function's
1253 frame size is larger than we expect. */
1254 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1256 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1257 static int verbose_warned = 0;
1259 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1260 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1261 size += UNITS_PER_WORD;
1263 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1265 warning (0, "frame size too large for reliable stack checking");
1266 if (! verbose_warned)
1268 warning (0, "try reducing the number of local variables");
1269 verbose_warned = 1;
1274 /* Indicate that we no longer have known memory locations or constants. */
1275 if (reg_equiv_constant)
1276 free (reg_equiv_constant);
1277 if (reg_equiv_invariant)
1278 free (reg_equiv_invariant);
1279 reg_equiv_constant = 0;
1280 reg_equiv_invariant = 0;
1281 VEC_free (rtx, gc, reg_equiv_memory_loc_vec);
1282 reg_equiv_memory_loc = 0;
1284 if (offsets_known_at)
1285 free (offsets_known_at);
1286 if (offsets_at)
1287 free (offsets_at);
1289 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1290 if (reg_equiv_alt_mem_list[i])
1291 free_EXPR_LIST_list (&reg_equiv_alt_mem_list[i]);
1292 free (reg_equiv_alt_mem_list);
1294 free (reg_equiv_mem);
1295 reg_equiv_init = 0;
1296 free (reg_equiv_address);
1297 free (reg_max_ref_width);
1298 free (reg_old_renumber);
1299 free (pseudo_previous_regs);
1300 free (pseudo_forbidden_regs);
1302 CLEAR_HARD_REG_SET (used_spill_regs);
1303 for (i = 0; i < n_spills; i++)
1304 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1306 /* Free all the insn_chain structures at once. */
1307 obstack_free (&reload_obstack, reload_startobj);
1308 unused_insn_chains = 0;
1309 fixup_abnormal_edges ();
1311 /* Replacing pseudos with their memory equivalents might have
1312 created shared rtx. Subsequent passes would get confused
1313 by this, so unshare everything here. */
1314 unshare_all_rtl_again (first);
1316 #ifdef STACK_BOUNDARY
1317 /* init_emit has set the alignment of the hard frame pointer
1318 to STACK_BOUNDARY. It is very likely no longer valid if
1319 the hard frame pointer was used for register allocation. */
1320 if (!frame_pointer_needed)
1321 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1322 #endif
1324 return failure;
1327 /* Yet another special case. Unfortunately, reg-stack forces people to
1328 write incorrect clobbers in asm statements. These clobbers must not
1329 cause the register to appear in bad_spill_regs, otherwise we'll call
1330 fatal_insn later. We clear the corresponding regnos in the live
1331 register sets to avoid this.
1332 The whole thing is rather sick, I'm afraid. */
1334 static void
1335 maybe_fix_stack_asms (void)
1337 #ifdef STACK_REGS
1338 const char *constraints[MAX_RECOG_OPERANDS];
1339 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1340 struct insn_chain *chain;
1342 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1344 int i, noperands;
1345 HARD_REG_SET clobbered, allowed;
1346 rtx pat;
1348 if (! INSN_P (chain->insn)
1349 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1350 continue;
1351 pat = PATTERN (chain->insn);
1352 if (GET_CODE (pat) != PARALLEL)
1353 continue;
1355 CLEAR_HARD_REG_SET (clobbered);
1356 CLEAR_HARD_REG_SET (allowed);
1358 /* First, make a mask of all stack regs that are clobbered. */
1359 for (i = 0; i < XVECLEN (pat, 0); i++)
1361 rtx t = XVECEXP (pat, 0, i);
1362 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1363 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1366 /* Get the operand values and constraints out of the insn. */
1367 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1368 constraints, operand_mode);
1370 /* For every operand, see what registers are allowed. */
1371 for (i = 0; i < noperands; i++)
1373 const char *p = constraints[i];
1374 /* For every alternative, we compute the class of registers allowed
1375 for reloading in CLS, and merge its contents into the reg set
1376 ALLOWED. */
1377 int cls = (int) NO_REGS;
1379 for (;;)
1381 char c = *p;
1383 if (c == '\0' || c == ',' || c == '#')
1385 /* End of one alternative - mark the regs in the current
1386 class, and reset the class. */
1387 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1388 cls = NO_REGS;
1389 p++;
1390 if (c == '#')
1391 do {
1392 c = *p++;
1393 } while (c != '\0' && c != ',');
1394 if (c == '\0')
1395 break;
1396 continue;
1399 switch (c)
1401 case '=': case '+': case '*': case '%': case '?': case '!':
1402 case '0': case '1': case '2': case '3': case '4': case 'm':
1403 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1404 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1405 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1406 case 'P':
1407 break;
1409 case 'p':
1410 cls = (int) reg_class_subunion[cls]
1411 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1412 break;
1414 case 'g':
1415 case 'r':
1416 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1417 break;
1419 default:
1420 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1421 cls = (int) reg_class_subunion[cls]
1422 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1423 else
1424 cls = (int) reg_class_subunion[cls]
1425 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1427 p += CONSTRAINT_LEN (c, p);
1430 /* Those of the registers which are clobbered, but allowed by the
1431 constraints, must be usable as reload registers. So clear them
1432 out of the life information. */
1433 AND_HARD_REG_SET (allowed, clobbered);
1434 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1435 if (TEST_HARD_REG_BIT (allowed, i))
1437 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1438 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1442 #endif
1445 /* Copy the global variables n_reloads and rld into the corresponding elts
1446 of CHAIN. */
1447 static void
1448 copy_reloads (struct insn_chain *chain)
1450 chain->n_reloads = n_reloads;
1451 chain->rld = obstack_alloc (&reload_obstack,
1452 n_reloads * sizeof (struct reload));
1453 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1454 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1457 /* Walk the chain of insns, and determine for each whether it needs reloads
1458 and/or eliminations. Build the corresponding insns_need_reload list, and
1459 set something_needs_elimination as appropriate. */
1460 static void
1461 calculate_needs_all_insns (int global)
1463 struct insn_chain **pprev_reload = &insns_need_reload;
1464 struct insn_chain *chain, *next = 0;
1466 something_needs_elimination = 0;
1468 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1469 for (chain = reload_insn_chain; chain != 0; chain = next)
1471 rtx insn = chain->insn;
1473 next = chain->next;
1475 /* Clear out the shortcuts. */
1476 chain->n_reloads = 0;
1477 chain->need_elim = 0;
1478 chain->need_reload = 0;
1479 chain->need_operand_change = 0;
1481 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1482 include REG_LABEL), we need to see what effects this has on the
1483 known offsets at labels. */
1485 if (LABEL_P (insn) || JUMP_P (insn)
1486 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1487 set_label_offsets (insn, insn, 0);
1489 if (INSN_P (insn))
1491 rtx old_body = PATTERN (insn);
1492 int old_code = INSN_CODE (insn);
1493 rtx old_notes = REG_NOTES (insn);
1494 int did_elimination = 0;
1495 int operands_changed = 0;
1496 rtx set = single_set (insn);
1498 /* Skip insns that only set an equivalence. */
1499 if (set && REG_P (SET_DEST (set))
1500 && reg_renumber[REGNO (SET_DEST (set))] < 0
1501 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1502 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1503 && reg_equiv_init[REGNO (SET_DEST (set))])
1504 continue;
1506 /* If needed, eliminate any eliminable registers. */
1507 if (num_eliminable || num_eliminable_invariants)
1508 did_elimination = eliminate_regs_in_insn (insn, 0);
1510 /* Analyze the instruction. */
1511 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1512 global, spill_reg_order);
1514 /* If a no-op set needs more than one reload, this is likely
1515 to be something that needs input address reloads. We
1516 can't get rid of this cleanly later, and it is of no use
1517 anyway, so discard it now.
1518 We only do this when expensive_optimizations is enabled,
1519 since this complements reload inheritance / output
1520 reload deletion, and it can make debugging harder. */
1521 if (flag_expensive_optimizations && n_reloads > 1)
1523 rtx set = single_set (insn);
1524 if (set
1525 && SET_SRC (set) == SET_DEST (set)
1526 && REG_P (SET_SRC (set))
1527 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1529 delete_insn (insn);
1530 /* Delete it from the reload chain. */
1531 if (chain->prev)
1532 chain->prev->next = next;
1533 else
1534 reload_insn_chain = next;
1535 if (next)
1536 next->prev = chain->prev;
1537 chain->next = unused_insn_chains;
1538 unused_insn_chains = chain;
1539 continue;
1542 if (num_eliminable)
1543 update_eliminable_offsets ();
1545 /* Remember for later shortcuts which insns had any reloads or
1546 register eliminations. */
1547 chain->need_elim = did_elimination;
1548 chain->need_reload = n_reloads > 0;
1549 chain->need_operand_change = operands_changed;
1551 /* Discard any register replacements done. */
1552 if (did_elimination)
1554 obstack_free (&reload_obstack, reload_insn_firstobj);
1555 PATTERN (insn) = old_body;
1556 INSN_CODE (insn) = old_code;
1557 REG_NOTES (insn) = old_notes;
1558 something_needs_elimination = 1;
1561 something_needs_operands_changed |= operands_changed;
1563 if (n_reloads != 0)
1565 copy_reloads (chain);
1566 *pprev_reload = chain;
1567 pprev_reload = &chain->next_need_reload;
1571 *pprev_reload = 0;
1574 /* Comparison function for qsort to decide which of two reloads
1575 should be handled first. *P1 and *P2 are the reload numbers. */
1577 static int
1578 reload_reg_class_lower (const void *r1p, const void *r2p)
1580 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1581 int t;
1583 /* Consider required reloads before optional ones. */
1584 t = rld[r1].optional - rld[r2].optional;
1585 if (t != 0)
1586 return t;
1588 /* Count all solitary classes before non-solitary ones. */
1589 t = ((reg_class_size[(int) rld[r2].class] == 1)
1590 - (reg_class_size[(int) rld[r1].class] == 1));
1591 if (t != 0)
1592 return t;
1594 /* Aside from solitaires, consider all multi-reg groups first. */
1595 t = rld[r2].nregs - rld[r1].nregs;
1596 if (t != 0)
1597 return t;
1599 /* Consider reloads in order of increasing reg-class number. */
1600 t = (int) rld[r1].class - (int) rld[r2].class;
1601 if (t != 0)
1602 return t;
1604 /* If reloads are equally urgent, sort by reload number,
1605 so that the results of qsort leave nothing to chance. */
1606 return r1 - r2;
1609 /* The cost of spilling each hard reg. */
1610 static int spill_cost[FIRST_PSEUDO_REGISTER];
1612 /* When spilling multiple hard registers, we use SPILL_COST for the first
1613 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1614 only the first hard reg for a multi-reg pseudo. */
1615 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1617 /* Update the spill cost arrays, considering that pseudo REG is live. */
1619 static void
1620 count_pseudo (int reg)
1622 int freq = REG_FREQ (reg);
1623 int r = reg_renumber[reg];
1624 int nregs;
1626 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1627 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1628 return;
1630 SET_REGNO_REG_SET (&pseudos_counted, reg);
1632 gcc_assert (r >= 0);
1634 spill_add_cost[r] += freq;
1636 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1637 while (nregs-- > 0)
1638 spill_cost[r + nregs] += freq;
1641 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1642 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1644 static void
1645 order_regs_for_reload (struct insn_chain *chain)
1647 unsigned i;
1648 HARD_REG_SET used_by_pseudos;
1649 HARD_REG_SET used_by_pseudos2;
1650 reg_set_iterator rsi;
1652 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1654 memset (spill_cost, 0, sizeof spill_cost);
1655 memset (spill_add_cost, 0, sizeof spill_add_cost);
1657 /* Count number of uses of each hard reg by pseudo regs allocated to it
1658 and then order them by decreasing use. First exclude hard registers
1659 that are live in or across this insn. */
1661 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1662 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1663 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1664 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1666 /* Now find out which pseudos are allocated to it, and update
1667 hard_reg_n_uses. */
1668 CLEAR_REG_SET (&pseudos_counted);
1670 EXECUTE_IF_SET_IN_REG_SET
1671 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1673 count_pseudo (i);
1675 EXECUTE_IF_SET_IN_REG_SET
1676 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1678 count_pseudo (i);
1680 CLEAR_REG_SET (&pseudos_counted);
1683 /* Vector of reload-numbers showing the order in which the reloads should
1684 be processed. */
1685 static short reload_order[MAX_RELOADS];
1687 /* This is used to keep track of the spill regs used in one insn. */
1688 static HARD_REG_SET used_spill_regs_local;
1690 /* We decided to spill hard register SPILLED, which has a size of
1691 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1692 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1693 update SPILL_COST/SPILL_ADD_COST. */
1695 static void
1696 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1698 int r = reg_renumber[reg];
1699 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1701 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1702 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1703 return;
1705 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1707 spill_add_cost[r] -= REG_FREQ (reg);
1708 while (nregs-- > 0)
1709 spill_cost[r + nregs] -= REG_FREQ (reg);
1712 /* Find reload register to use for reload number ORDER. */
1714 static int
1715 find_reg (struct insn_chain *chain, int order)
1717 int rnum = reload_order[order];
1718 struct reload *rl = rld + rnum;
1719 int best_cost = INT_MAX;
1720 int best_reg = -1;
1721 unsigned int i, j;
1722 int k;
1723 HARD_REG_SET not_usable;
1724 HARD_REG_SET used_by_other_reload;
1725 reg_set_iterator rsi;
1727 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1728 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1729 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1731 CLEAR_HARD_REG_SET (used_by_other_reload);
1732 for (k = 0; k < order; k++)
1734 int other = reload_order[k];
1736 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1737 for (j = 0; j < rld[other].nregs; j++)
1738 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1741 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1743 unsigned int regno = i;
1745 if (! TEST_HARD_REG_BIT (not_usable, regno)
1746 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1747 && HARD_REGNO_MODE_OK (regno, rl->mode))
1749 int this_cost = spill_cost[regno];
1750 int ok = 1;
1751 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1753 for (j = 1; j < this_nregs; j++)
1755 this_cost += spill_add_cost[regno + j];
1756 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1757 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1758 ok = 0;
1760 if (! ok)
1761 continue;
1762 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1763 this_cost--;
1764 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1765 this_cost--;
1766 if (this_cost < best_cost
1767 /* Among registers with equal cost, prefer caller-saved ones, or
1768 use REG_ALLOC_ORDER if it is defined. */
1769 || (this_cost == best_cost
1770 #ifdef REG_ALLOC_ORDER
1771 && (inv_reg_alloc_order[regno]
1772 < inv_reg_alloc_order[best_reg])
1773 #else
1774 && call_used_regs[regno]
1775 && ! call_used_regs[best_reg]
1776 #endif
1779 best_reg = regno;
1780 best_cost = this_cost;
1784 if (best_reg == -1)
1785 return 0;
1787 if (dump_file)
1788 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1790 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1791 rl->regno = best_reg;
1793 EXECUTE_IF_SET_IN_REG_SET
1794 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1796 count_spilled_pseudo (best_reg, rl->nregs, j);
1799 EXECUTE_IF_SET_IN_REG_SET
1800 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1802 count_spilled_pseudo (best_reg, rl->nregs, j);
1805 for (i = 0; i < rl->nregs; i++)
1807 gcc_assert (spill_cost[best_reg + i] == 0);
1808 gcc_assert (spill_add_cost[best_reg + i] == 0);
1809 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1811 return 1;
1814 /* Find more reload regs to satisfy the remaining need of an insn, which
1815 is given by CHAIN.
1816 Do it by ascending class number, since otherwise a reg
1817 might be spilled for a big class and might fail to count
1818 for a smaller class even though it belongs to that class. */
1820 static void
1821 find_reload_regs (struct insn_chain *chain)
1823 int i;
1825 /* In order to be certain of getting the registers we need,
1826 we must sort the reloads into order of increasing register class.
1827 Then our grabbing of reload registers will parallel the process
1828 that provided the reload registers. */
1829 for (i = 0; i < chain->n_reloads; i++)
1831 /* Show whether this reload already has a hard reg. */
1832 if (chain->rld[i].reg_rtx)
1834 int regno = REGNO (chain->rld[i].reg_rtx);
1835 chain->rld[i].regno = regno;
1836 chain->rld[i].nregs
1837 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1839 else
1840 chain->rld[i].regno = -1;
1841 reload_order[i] = i;
1844 n_reloads = chain->n_reloads;
1845 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1847 CLEAR_HARD_REG_SET (used_spill_regs_local);
1849 if (dump_file)
1850 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1852 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1854 /* Compute the order of preference for hard registers to spill. */
1856 order_regs_for_reload (chain);
1858 for (i = 0; i < n_reloads; i++)
1860 int r = reload_order[i];
1862 /* Ignore reloads that got marked inoperative. */
1863 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1864 && ! rld[r].optional
1865 && rld[r].regno == -1)
1866 if (! find_reg (chain, i))
1868 if (dump_file)
1869 fprintf (dump_file, "reload failure for reload %d\n", r);
1870 spill_failure (chain->insn, rld[r].class);
1871 failure = 1;
1872 return;
1876 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1877 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1879 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1882 static void
1883 select_reload_regs (void)
1885 struct insn_chain *chain;
1887 /* Try to satisfy the needs for each insn. */
1888 for (chain = insns_need_reload; chain != 0;
1889 chain = chain->next_need_reload)
1890 find_reload_regs (chain);
1893 /* Delete all insns that were inserted by emit_caller_save_insns during
1894 this iteration. */
1895 static void
1896 delete_caller_save_insns (void)
1898 struct insn_chain *c = reload_insn_chain;
1900 while (c != 0)
1902 while (c != 0 && c->is_caller_save_insn)
1904 struct insn_chain *next = c->next;
1905 rtx insn = c->insn;
1907 if (c == reload_insn_chain)
1908 reload_insn_chain = next;
1909 delete_insn (insn);
1911 if (next)
1912 next->prev = c->prev;
1913 if (c->prev)
1914 c->prev->next = next;
1915 c->next = unused_insn_chains;
1916 unused_insn_chains = c;
1917 c = next;
1919 if (c != 0)
1920 c = c->next;
1924 /* Handle the failure to find a register to spill.
1925 INSN should be one of the insns which needed this particular spill reg. */
1927 static void
1928 spill_failure (rtx insn, enum reg_class class)
1930 if (asm_noperands (PATTERN (insn)) >= 0)
1931 error_for_asm (insn, "can't find a register in class %qs while "
1932 "reloading %<asm%>",
1933 reg_class_names[class]);
1934 else
1936 error ("unable to find a register to spill in class %qs",
1937 reg_class_names[class]);
1939 if (dump_file)
1941 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
1942 debug_reload_to_stream (dump_file);
1944 fatal_insn ("this is the insn:", insn);
1948 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
1949 data that is dead in INSN. */
1951 static void
1952 delete_dead_insn (rtx insn)
1954 rtx prev = prev_real_insn (insn);
1955 rtx prev_dest;
1957 /* If the previous insn sets a register that dies in our insn, delete it
1958 too. */
1959 if (prev && GET_CODE (PATTERN (prev)) == SET
1960 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
1961 && reg_mentioned_p (prev_dest, PATTERN (insn))
1962 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
1963 && ! side_effects_p (SET_SRC (PATTERN (prev))))
1964 delete_dead_insn (prev);
1966 SET_INSN_DELETED (insn);
1969 /* Modify the home of pseudo-reg I.
1970 The new home is present in reg_renumber[I].
1972 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
1973 or it may be -1, meaning there is none or it is not relevant.
1974 This is used so that all pseudos spilled from a given hard reg
1975 can share one stack slot. */
1977 static void
1978 alter_reg (int i, int from_reg)
1980 /* When outputting an inline function, this can happen
1981 for a reg that isn't actually used. */
1982 if (regno_reg_rtx[i] == 0)
1983 return;
1985 /* If the reg got changed to a MEM at rtl-generation time,
1986 ignore it. */
1987 if (!REG_P (regno_reg_rtx[i]))
1988 return;
1990 /* Modify the reg-rtx to contain the new hard reg
1991 number or else to contain its pseudo reg number. */
1992 REGNO (regno_reg_rtx[i])
1993 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
1995 /* If we have a pseudo that is needed but has no hard reg or equivalent,
1996 allocate a stack slot for it. */
1998 if (reg_renumber[i] < 0
1999 && REG_N_REFS (i) > 0
2000 && reg_equiv_constant[i] == 0
2001 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
2002 && reg_equiv_memory_loc[i] == 0)
2004 rtx x;
2005 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2006 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2007 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2008 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2009 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2010 int adjust = 0;
2012 /* Each pseudo reg has an inherent size which comes from its own mode,
2013 and a total size which provides room for paradoxical subregs
2014 which refer to the pseudo reg in wider modes.
2016 We can use a slot already allocated if it provides both
2017 enough inherent space and enough total space.
2018 Otherwise, we allocate a new slot, making sure that it has no less
2019 inherent space, and no less total space, then the previous slot. */
2020 if (from_reg == -1)
2022 /* No known place to spill from => no slot to reuse. */
2023 x = assign_stack_local (mode, total_size,
2024 min_align > inherent_align
2025 || total_size > inherent_size ? -1 : 0);
2026 if (BYTES_BIG_ENDIAN)
2027 /* Cancel the big-endian correction done in assign_stack_local.
2028 Get the address of the beginning of the slot.
2029 This is so we can do a big-endian correction unconditionally
2030 below. */
2031 adjust = inherent_size - total_size;
2033 /* Nothing can alias this slot except this pseudo. */
2034 set_mem_alias_set (x, new_alias_set ());
2037 /* Reuse a stack slot if possible. */
2038 else if (spill_stack_slot[from_reg] != 0
2039 && spill_stack_slot_width[from_reg] >= total_size
2040 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2041 >= inherent_size)
2042 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2043 x = spill_stack_slot[from_reg];
2045 /* Allocate a bigger slot. */
2046 else
2048 /* Compute maximum size needed, both for inherent size
2049 and for total size. */
2050 rtx stack_slot;
2052 if (spill_stack_slot[from_reg])
2054 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2055 > inherent_size)
2056 mode = GET_MODE (spill_stack_slot[from_reg]);
2057 if (spill_stack_slot_width[from_reg] > total_size)
2058 total_size = spill_stack_slot_width[from_reg];
2059 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2060 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2063 /* Make a slot with that size. */
2064 x = assign_stack_local (mode, total_size,
2065 min_align > inherent_align
2066 || total_size > inherent_size ? -1 : 0);
2067 stack_slot = x;
2069 /* All pseudos mapped to this slot can alias each other. */
2070 if (spill_stack_slot[from_reg])
2071 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
2072 else
2073 set_mem_alias_set (x, new_alias_set ());
2075 if (BYTES_BIG_ENDIAN)
2077 /* Cancel the big-endian correction done in assign_stack_local.
2078 Get the address of the beginning of the slot.
2079 This is so we can do a big-endian correction unconditionally
2080 below. */
2081 adjust = GET_MODE_SIZE (mode) - total_size;
2082 if (adjust)
2083 stack_slot
2084 = adjust_address_nv (x, mode_for_size (total_size
2085 * BITS_PER_UNIT,
2086 MODE_INT, 1),
2087 adjust);
2090 spill_stack_slot[from_reg] = stack_slot;
2091 spill_stack_slot_width[from_reg] = total_size;
2094 /* On a big endian machine, the "address" of the slot
2095 is the address of the low part that fits its inherent mode. */
2096 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2097 adjust += (total_size - inherent_size);
2099 /* If we have any adjustment to make, or if the stack slot is the
2100 wrong mode, make a new stack slot. */
2101 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2103 /* If we have a decl for the original register, set it for the
2104 memory. If this is a shared MEM, make a copy. */
2105 if (REG_EXPR (regno_reg_rtx[i])
2106 && DECL_P (REG_EXPR (regno_reg_rtx[i])))
2108 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2110 /* We can do this only for the DECLs home pseudo, not for
2111 any copies of it, since otherwise when the stack slot
2112 is reused, nonoverlapping_memrefs_p might think they
2113 cannot overlap. */
2114 if (decl && REG_P (decl) && REGNO (decl) == (unsigned) i)
2116 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2117 x = copy_rtx (x);
2119 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2123 /* Save the stack slot for later. */
2124 reg_equiv_memory_loc[i] = x;
2128 /* Mark the slots in regs_ever_live for the hard regs
2129 used by pseudo-reg number REGNO. */
2131 void
2132 mark_home_live (int regno)
2134 int i, lim;
2136 i = reg_renumber[regno];
2137 if (i < 0)
2138 return;
2139 lim = i + hard_regno_nregs[i][PSEUDO_REGNO_MODE (regno)];
2140 while (i < lim)
2141 regs_ever_live[i++] = 1;
2144 /* This function handles the tracking of elimination offsets around branches.
2146 X is a piece of RTL being scanned.
2148 INSN is the insn that it came from, if any.
2150 INITIAL_P is nonzero if we are to set the offset to be the initial
2151 offset and zero if we are setting the offset of the label to be the
2152 current offset. */
2154 static void
2155 set_label_offsets (rtx x, rtx insn, int initial_p)
2157 enum rtx_code code = GET_CODE (x);
2158 rtx tem;
2159 unsigned int i;
2160 struct elim_table *p;
2162 switch (code)
2164 case LABEL_REF:
2165 if (LABEL_REF_NONLOCAL_P (x))
2166 return;
2168 x = XEXP (x, 0);
2170 /* ... fall through ... */
2172 case CODE_LABEL:
2173 /* If we know nothing about this label, set the desired offsets. Note
2174 that this sets the offset at a label to be the offset before a label
2175 if we don't know anything about the label. This is not correct for
2176 the label after a BARRIER, but is the best guess we can make. If
2177 we guessed wrong, we will suppress an elimination that might have
2178 been possible had we been able to guess correctly. */
2180 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2182 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2183 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2184 = (initial_p ? reg_eliminate[i].initial_offset
2185 : reg_eliminate[i].offset);
2186 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2189 /* Otherwise, if this is the definition of a label and it is
2190 preceded by a BARRIER, set our offsets to the known offset of
2191 that label. */
2193 else if (x == insn
2194 && (tem = prev_nonnote_insn (insn)) != 0
2195 && BARRIER_P (tem))
2196 set_offsets_for_label (insn);
2197 else
2198 /* If neither of the above cases is true, compare each offset
2199 with those previously recorded and suppress any eliminations
2200 where the offsets disagree. */
2202 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2203 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2204 != (initial_p ? reg_eliminate[i].initial_offset
2205 : reg_eliminate[i].offset))
2206 reg_eliminate[i].can_eliminate = 0;
2208 return;
2210 case JUMP_INSN:
2211 set_label_offsets (PATTERN (insn), insn, initial_p);
2213 /* ... fall through ... */
2215 case INSN:
2216 case CALL_INSN:
2217 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2218 and hence must have all eliminations at their initial offsets. */
2219 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2220 if (REG_NOTE_KIND (tem) == REG_LABEL)
2221 set_label_offsets (XEXP (tem, 0), insn, 1);
2222 return;
2224 case PARALLEL:
2225 case ADDR_VEC:
2226 case ADDR_DIFF_VEC:
2227 /* Each of the labels in the parallel or address vector must be
2228 at their initial offsets. We want the first field for PARALLEL
2229 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2231 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2232 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2233 insn, initial_p);
2234 return;
2236 case SET:
2237 /* We only care about setting PC. If the source is not RETURN,
2238 IF_THEN_ELSE, or a label, disable any eliminations not at
2239 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2240 isn't one of those possibilities. For branches to a label,
2241 call ourselves recursively.
2243 Note that this can disable elimination unnecessarily when we have
2244 a non-local goto since it will look like a non-constant jump to
2245 someplace in the current function. This isn't a significant
2246 problem since such jumps will normally be when all elimination
2247 pairs are back to their initial offsets. */
2249 if (SET_DEST (x) != pc_rtx)
2250 return;
2252 switch (GET_CODE (SET_SRC (x)))
2254 case PC:
2255 case RETURN:
2256 return;
2258 case LABEL_REF:
2259 set_label_offsets (SET_SRC (x), insn, initial_p);
2260 return;
2262 case IF_THEN_ELSE:
2263 tem = XEXP (SET_SRC (x), 1);
2264 if (GET_CODE (tem) == LABEL_REF)
2265 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2266 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2267 break;
2269 tem = XEXP (SET_SRC (x), 2);
2270 if (GET_CODE (tem) == LABEL_REF)
2271 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2272 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2273 break;
2274 return;
2276 default:
2277 break;
2280 /* If we reach here, all eliminations must be at their initial
2281 offset because we are doing a jump to a variable address. */
2282 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2283 if (p->offset != p->initial_offset)
2284 p->can_eliminate = 0;
2285 break;
2287 default:
2288 break;
2292 /* Scan X and replace any eliminable registers (such as fp) with a
2293 replacement (such as sp), plus an offset.
2295 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2296 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2297 MEM, we are allowed to replace a sum of a register and the constant zero
2298 with the register, which we cannot do outside a MEM. In addition, we need
2299 to record the fact that a register is referenced outside a MEM.
2301 If INSN is an insn, it is the insn containing X. If we replace a REG
2302 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2303 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2304 the REG is being modified.
2306 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2307 That's used when we eliminate in expressions stored in notes.
2308 This means, do not set ref_outside_mem even if the reference
2309 is outside of MEMs.
2311 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2312 replacements done assuming all offsets are at their initial values. If
2313 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2314 encounter, return the actual location so that find_reloads will do
2315 the proper thing. */
2317 static rtx
2318 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2319 bool may_use_invariant)
2321 enum rtx_code code = GET_CODE (x);
2322 struct elim_table *ep;
2323 int regno;
2324 rtx new;
2325 int i, j;
2326 const char *fmt;
2327 int copied = 0;
2329 if (! current_function_decl)
2330 return x;
2332 switch (code)
2334 case CONST_INT:
2335 case CONST_DOUBLE:
2336 case CONST_VECTOR:
2337 case CONST:
2338 case SYMBOL_REF:
2339 case CODE_LABEL:
2340 case PC:
2341 case CC0:
2342 case ASM_INPUT:
2343 case ADDR_VEC:
2344 case ADDR_DIFF_VEC:
2345 case RETURN:
2346 return x;
2348 case REG:
2349 regno = REGNO (x);
2351 /* First handle the case where we encounter a bare register that
2352 is eliminable. Replace it with a PLUS. */
2353 if (regno < FIRST_PSEUDO_REGISTER)
2355 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2356 ep++)
2357 if (ep->from_rtx == x && ep->can_eliminate)
2358 return plus_constant (ep->to_rtx, ep->previous_offset);
2361 else if (reg_renumber && reg_renumber[regno] < 0
2362 && reg_equiv_invariant && reg_equiv_invariant[regno])
2364 if (may_use_invariant)
2365 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2366 mem_mode, insn, true);
2367 /* There exists at least one use of REGNO that cannot be
2368 eliminated. Prevent the defining insn from being deleted. */
2369 reg_equiv_init[regno] = NULL_RTX;
2370 alter_reg (regno, -1);
2372 return x;
2374 /* You might think handling MINUS in a manner similar to PLUS is a
2375 good idea. It is not. It has been tried multiple times and every
2376 time the change has had to have been reverted.
2378 Other parts of reload know a PLUS is special (gen_reload for example)
2379 and require special code to handle code a reloaded PLUS operand.
2381 Also consider backends where the flags register is clobbered by a
2382 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2383 lea instruction comes to mind). If we try to reload a MINUS, we
2384 may kill the flags register that was holding a useful value.
2386 So, please before trying to handle MINUS, consider reload as a
2387 whole instead of this little section as well as the backend issues. */
2388 case PLUS:
2389 /* If this is the sum of an eliminable register and a constant, rework
2390 the sum. */
2391 if (REG_P (XEXP (x, 0))
2392 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2393 && CONSTANT_P (XEXP (x, 1)))
2395 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2396 ep++)
2397 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2399 /* The only time we want to replace a PLUS with a REG (this
2400 occurs when the constant operand of the PLUS is the negative
2401 of the offset) is when we are inside a MEM. We won't want
2402 to do so at other times because that would change the
2403 structure of the insn in a way that reload can't handle.
2404 We special-case the commonest situation in
2405 eliminate_regs_in_insn, so just replace a PLUS with a
2406 PLUS here, unless inside a MEM. */
2407 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2408 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2409 return ep->to_rtx;
2410 else
2411 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2412 plus_constant (XEXP (x, 1),
2413 ep->previous_offset));
2416 /* If the register is not eliminable, we are done since the other
2417 operand is a constant. */
2418 return x;
2421 /* If this is part of an address, we want to bring any constant to the
2422 outermost PLUS. We will do this by doing register replacement in
2423 our operands and seeing if a constant shows up in one of them.
2425 Note that there is no risk of modifying the structure of the insn,
2426 since we only get called for its operands, thus we are either
2427 modifying the address inside a MEM, or something like an address
2428 operand of a load-address insn. */
2431 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2432 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2434 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2436 /* If one side is a PLUS and the other side is a pseudo that
2437 didn't get a hard register but has a reg_equiv_constant,
2438 we must replace the constant here since it may no longer
2439 be in the position of any operand. */
2440 if (GET_CODE (new0) == PLUS && REG_P (new1)
2441 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2442 && reg_renumber[REGNO (new1)] < 0
2443 && reg_equiv_constant != 0
2444 && reg_equiv_constant[REGNO (new1)] != 0)
2445 new1 = reg_equiv_constant[REGNO (new1)];
2446 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2447 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2448 && reg_renumber[REGNO (new0)] < 0
2449 && reg_equiv_constant[REGNO (new0)] != 0)
2450 new0 = reg_equiv_constant[REGNO (new0)];
2452 new = form_sum (new0, new1);
2454 /* As above, if we are not inside a MEM we do not want to
2455 turn a PLUS into something else. We might try to do so here
2456 for an addition of 0 if we aren't optimizing. */
2457 if (! mem_mode && GET_CODE (new) != PLUS)
2458 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2459 else
2460 return new;
2463 return x;
2465 case MULT:
2466 /* If this is the product of an eliminable register and a
2467 constant, apply the distribute law and move the constant out
2468 so that we have (plus (mult ..) ..). This is needed in order
2469 to keep load-address insns valid. This case is pathological.
2470 We ignore the possibility of overflow here. */
2471 if (REG_P (XEXP (x, 0))
2472 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2473 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2474 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2475 ep++)
2476 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2478 if (! mem_mode
2479 /* Refs inside notes don't count for this purpose. */
2480 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2481 || GET_CODE (insn) == INSN_LIST)))
2482 ep->ref_outside_mem = 1;
2484 return
2485 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2486 ep->previous_offset * INTVAL (XEXP (x, 1)));
2489 /* ... fall through ... */
2491 case CALL:
2492 case COMPARE:
2493 /* See comments before PLUS about handling MINUS. */
2494 case MINUS:
2495 case DIV: case UDIV:
2496 case MOD: case UMOD:
2497 case AND: case IOR: case XOR:
2498 case ROTATERT: case ROTATE:
2499 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2500 case NE: case EQ:
2501 case GE: case GT: case GEU: case GTU:
2502 case LE: case LT: case LEU: case LTU:
2504 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2505 rtx new1 = XEXP (x, 1)
2506 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false) : 0;
2508 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2509 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2511 return x;
2513 case EXPR_LIST:
2514 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2515 if (XEXP (x, 0))
2517 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2518 if (new != XEXP (x, 0))
2520 /* If this is a REG_DEAD note, it is not valid anymore.
2521 Using the eliminated version could result in creating a
2522 REG_DEAD note for the stack or frame pointer. */
2523 if (GET_MODE (x) == REG_DEAD)
2524 return (XEXP (x, 1)
2525 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true)
2526 : NULL_RTX);
2528 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2532 /* ... fall through ... */
2534 case INSN_LIST:
2535 /* Now do eliminations in the rest of the chain. If this was
2536 an EXPR_LIST, this might result in allocating more memory than is
2537 strictly needed, but it simplifies the code. */
2538 if (XEXP (x, 1))
2540 new = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2541 if (new != XEXP (x, 1))
2542 return
2543 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2545 return x;
2547 case PRE_INC:
2548 case POST_INC:
2549 case PRE_DEC:
2550 case POST_DEC:
2551 case STRICT_LOW_PART:
2552 case NEG: case NOT:
2553 case SIGN_EXTEND: case ZERO_EXTEND:
2554 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2555 case FLOAT: case FIX:
2556 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2557 case ABS:
2558 case SQRT:
2559 case FFS:
2560 case CLZ:
2561 case CTZ:
2562 case POPCOUNT:
2563 case PARITY:
2564 case BSWAP:
2565 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2566 if (new != XEXP (x, 0))
2567 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2568 return x;
2570 case SUBREG:
2571 /* Similar to above processing, but preserve SUBREG_BYTE.
2572 Convert (subreg (mem)) to (mem) if not paradoxical.
2573 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2574 pseudo didn't get a hard reg, we must replace this with the
2575 eliminated version of the memory location because push_reload
2576 may do the replacement in certain circumstances. */
2577 if (REG_P (SUBREG_REG (x))
2578 && (GET_MODE_SIZE (GET_MODE (x))
2579 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2580 && reg_equiv_memory_loc != 0
2581 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2583 new = SUBREG_REG (x);
2585 else
2586 new = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false);
2588 if (new != SUBREG_REG (x))
2590 int x_size = GET_MODE_SIZE (GET_MODE (x));
2591 int new_size = GET_MODE_SIZE (GET_MODE (new));
2593 if (MEM_P (new)
2594 && ((x_size < new_size
2595 #ifdef WORD_REGISTER_OPERATIONS
2596 /* On these machines, combine can create rtl of the form
2597 (set (subreg:m1 (reg:m2 R) 0) ...)
2598 where m1 < m2, and expects something interesting to
2599 happen to the entire word. Moreover, it will use the
2600 (reg:m2 R) later, expecting all bits to be preserved.
2601 So if the number of words is the same, preserve the
2602 subreg so that push_reload can see it. */
2603 && ! ((x_size - 1) / UNITS_PER_WORD
2604 == (new_size -1 ) / UNITS_PER_WORD)
2605 #endif
2607 || x_size == new_size)
2609 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2610 else
2611 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2614 return x;
2616 case MEM:
2617 /* Our only special processing is to pass the mode of the MEM to our
2618 recursive call and copy the flags. While we are here, handle this
2619 case more efficiently. */
2620 return
2621 replace_equiv_address_nv (x,
2622 eliminate_regs_1 (XEXP (x, 0), GET_MODE (x),
2623 insn, true));
2625 case USE:
2626 /* Handle insn_list USE that a call to a pure function may generate. */
2627 new = eliminate_regs_1 (XEXP (x, 0), 0, insn, false);
2628 if (new != XEXP (x, 0))
2629 return gen_rtx_USE (GET_MODE (x), new);
2630 return x;
2632 case CLOBBER:
2633 case ASM_OPERANDS:
2634 case SET:
2635 gcc_unreachable ();
2637 default:
2638 break;
2641 /* Process each of our operands recursively. If any have changed, make a
2642 copy of the rtx. */
2643 fmt = GET_RTX_FORMAT (code);
2644 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2646 if (*fmt == 'e')
2648 new = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false);
2649 if (new != XEXP (x, i) && ! copied)
2651 x = shallow_copy_rtx (x);
2652 copied = 1;
2654 XEXP (x, i) = new;
2656 else if (*fmt == 'E')
2658 int copied_vec = 0;
2659 for (j = 0; j < XVECLEN (x, i); j++)
2661 new = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false);
2662 if (new != XVECEXP (x, i, j) && ! copied_vec)
2664 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2665 XVEC (x, i)->elem);
2666 if (! copied)
2668 x = shallow_copy_rtx (x);
2669 copied = 1;
2671 XVEC (x, i) = new_v;
2672 copied_vec = 1;
2674 XVECEXP (x, i, j) = new;
2679 return x;
2683 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2685 return eliminate_regs_1 (x, mem_mode, insn, false);
2688 /* Scan rtx X for modifications of elimination target registers. Update
2689 the table of eliminables to reflect the changed state. MEM_MODE is
2690 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2692 static void
2693 elimination_effects (rtx x, enum machine_mode mem_mode)
2695 enum rtx_code code = GET_CODE (x);
2696 struct elim_table *ep;
2697 int regno;
2698 int i, j;
2699 const char *fmt;
2701 switch (code)
2703 case CONST_INT:
2704 case CONST_DOUBLE:
2705 case CONST_VECTOR:
2706 case CONST:
2707 case SYMBOL_REF:
2708 case CODE_LABEL:
2709 case PC:
2710 case CC0:
2711 case ASM_INPUT:
2712 case ADDR_VEC:
2713 case ADDR_DIFF_VEC:
2714 case RETURN:
2715 return;
2717 case REG:
2718 regno = REGNO (x);
2720 /* First handle the case where we encounter a bare register that
2721 is eliminable. Replace it with a PLUS. */
2722 if (regno < FIRST_PSEUDO_REGISTER)
2724 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2725 ep++)
2726 if (ep->from_rtx == x && ep->can_eliminate)
2728 if (! mem_mode)
2729 ep->ref_outside_mem = 1;
2730 return;
2734 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2735 && reg_equiv_constant[regno]
2736 && ! function_invariant_p (reg_equiv_constant[regno]))
2737 elimination_effects (reg_equiv_constant[regno], mem_mode);
2738 return;
2740 case PRE_INC:
2741 case POST_INC:
2742 case PRE_DEC:
2743 case POST_DEC:
2744 case POST_MODIFY:
2745 case PRE_MODIFY:
2746 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2747 if (ep->to_rtx == XEXP (x, 0))
2749 int size = GET_MODE_SIZE (mem_mode);
2751 /* If more bytes than MEM_MODE are pushed, account for them. */
2752 #ifdef PUSH_ROUNDING
2753 if (ep->to_rtx == stack_pointer_rtx)
2754 size = PUSH_ROUNDING (size);
2755 #endif
2756 if (code == PRE_DEC || code == POST_DEC)
2757 ep->offset += size;
2758 else if (code == PRE_INC || code == POST_INC)
2759 ep->offset -= size;
2760 else if ((code == PRE_MODIFY || code == POST_MODIFY)
2761 && GET_CODE (XEXP (x, 1)) == PLUS
2762 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2763 && CONSTANT_P (XEXP (XEXP (x, 1), 1)))
2764 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2767 /* These two aren't unary operators. */
2768 if (code == POST_MODIFY || code == PRE_MODIFY)
2769 break;
2771 /* Fall through to generic unary operation case. */
2772 case STRICT_LOW_PART:
2773 case NEG: case NOT:
2774 case SIGN_EXTEND: case ZERO_EXTEND:
2775 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2776 case FLOAT: case FIX:
2777 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2778 case ABS:
2779 case SQRT:
2780 case FFS:
2781 case CLZ:
2782 case CTZ:
2783 case POPCOUNT:
2784 case PARITY:
2785 case BSWAP:
2786 elimination_effects (XEXP (x, 0), mem_mode);
2787 return;
2789 case SUBREG:
2790 if (REG_P (SUBREG_REG (x))
2791 && (GET_MODE_SIZE (GET_MODE (x))
2792 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2793 && reg_equiv_memory_loc != 0
2794 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2795 return;
2797 elimination_effects (SUBREG_REG (x), mem_mode);
2798 return;
2800 case USE:
2801 /* If using a register that is the source of an eliminate we still
2802 think can be performed, note it cannot be performed since we don't
2803 know how this register is used. */
2804 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2805 if (ep->from_rtx == XEXP (x, 0))
2806 ep->can_eliminate = 0;
2808 elimination_effects (XEXP (x, 0), mem_mode);
2809 return;
2811 case CLOBBER:
2812 /* If clobbering a register that is the replacement register for an
2813 elimination we still think can be performed, note that it cannot
2814 be performed. Otherwise, we need not be concerned about it. */
2815 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2816 if (ep->to_rtx == XEXP (x, 0))
2817 ep->can_eliminate = 0;
2819 elimination_effects (XEXP (x, 0), mem_mode);
2820 return;
2822 case SET:
2823 /* Check for setting a register that we know about. */
2824 if (REG_P (SET_DEST (x)))
2826 /* See if this is setting the replacement register for an
2827 elimination.
2829 If DEST is the hard frame pointer, we do nothing because we
2830 assume that all assignments to the frame pointer are for
2831 non-local gotos and are being done at a time when they are valid
2832 and do not disturb anything else. Some machines want to
2833 eliminate a fake argument pointer (or even a fake frame pointer)
2834 with either the real frame or the stack pointer. Assignments to
2835 the hard frame pointer must not prevent this elimination. */
2837 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2838 ep++)
2839 if (ep->to_rtx == SET_DEST (x)
2840 && SET_DEST (x) != hard_frame_pointer_rtx)
2842 /* If it is being incremented, adjust the offset. Otherwise,
2843 this elimination can't be done. */
2844 rtx src = SET_SRC (x);
2846 if (GET_CODE (src) == PLUS
2847 && XEXP (src, 0) == SET_DEST (x)
2848 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2849 ep->offset -= INTVAL (XEXP (src, 1));
2850 else
2851 ep->can_eliminate = 0;
2855 elimination_effects (SET_DEST (x), 0);
2856 elimination_effects (SET_SRC (x), 0);
2857 return;
2859 case MEM:
2860 /* Our only special processing is to pass the mode of the MEM to our
2861 recursive call. */
2862 elimination_effects (XEXP (x, 0), GET_MODE (x));
2863 return;
2865 default:
2866 break;
2869 fmt = GET_RTX_FORMAT (code);
2870 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2872 if (*fmt == 'e')
2873 elimination_effects (XEXP (x, i), mem_mode);
2874 else if (*fmt == 'E')
2875 for (j = 0; j < XVECLEN (x, i); j++)
2876 elimination_effects (XVECEXP (x, i, j), mem_mode);
2880 /* Descend through rtx X and verify that no references to eliminable registers
2881 remain. If any do remain, mark the involved register as not
2882 eliminable. */
2884 static void
2885 check_eliminable_occurrences (rtx x)
2887 const char *fmt;
2888 int i;
2889 enum rtx_code code;
2891 if (x == 0)
2892 return;
2894 code = GET_CODE (x);
2896 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2898 struct elim_table *ep;
2900 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2901 if (ep->from_rtx == x)
2902 ep->can_eliminate = 0;
2903 return;
2906 fmt = GET_RTX_FORMAT (code);
2907 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2909 if (*fmt == 'e')
2910 check_eliminable_occurrences (XEXP (x, i));
2911 else if (*fmt == 'E')
2913 int j;
2914 for (j = 0; j < XVECLEN (x, i); j++)
2915 check_eliminable_occurrences (XVECEXP (x, i, j));
2920 /* Scan INSN and eliminate all eliminable registers in it.
2922 If REPLACE is nonzero, do the replacement destructively. Also
2923 delete the insn as dead it if it is setting an eliminable register.
2925 If REPLACE is zero, do all our allocations in reload_obstack.
2927 If no eliminations were done and this insn doesn't require any elimination
2928 processing (these are not identical conditions: it might be updating sp,
2929 but not referencing fp; this needs to be seen during reload_as_needed so
2930 that the offset between fp and sp can be taken into consideration), zero
2931 is returned. Otherwise, 1 is returned. */
2933 static int
2934 eliminate_regs_in_insn (rtx insn, int replace)
2936 int icode = recog_memoized (insn);
2937 rtx old_body = PATTERN (insn);
2938 int insn_is_asm = asm_noperands (old_body) >= 0;
2939 rtx old_set = single_set (insn);
2940 rtx new_body;
2941 int val = 0;
2942 int i;
2943 rtx substed_operand[MAX_RECOG_OPERANDS];
2944 rtx orig_operand[MAX_RECOG_OPERANDS];
2945 struct elim_table *ep;
2946 rtx plus_src, plus_cst_src;
2948 if (! insn_is_asm && icode < 0)
2950 gcc_assert (GET_CODE (PATTERN (insn)) == USE
2951 || GET_CODE (PATTERN (insn)) == CLOBBER
2952 || GET_CODE (PATTERN (insn)) == ADDR_VEC
2953 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
2954 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
2955 return 0;
2958 if (old_set != 0 && REG_P (SET_DEST (old_set))
2959 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
2961 /* Check for setting an eliminable register. */
2962 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2963 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
2965 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2966 /* If this is setting the frame pointer register to the
2967 hardware frame pointer register and this is an elimination
2968 that will be done (tested above), this insn is really
2969 adjusting the frame pointer downward to compensate for
2970 the adjustment done before a nonlocal goto. */
2971 if (ep->from == FRAME_POINTER_REGNUM
2972 && ep->to == HARD_FRAME_POINTER_REGNUM)
2974 rtx base = SET_SRC (old_set);
2975 rtx base_insn = insn;
2976 HOST_WIDE_INT offset = 0;
2978 while (base != ep->to_rtx)
2980 rtx prev_insn, prev_set;
2982 if (GET_CODE (base) == PLUS
2983 && GET_CODE (XEXP (base, 1)) == CONST_INT)
2985 offset += INTVAL (XEXP (base, 1));
2986 base = XEXP (base, 0);
2988 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
2989 && (prev_set = single_set (prev_insn)) != 0
2990 && rtx_equal_p (SET_DEST (prev_set), base))
2992 base = SET_SRC (prev_set);
2993 base_insn = prev_insn;
2995 else
2996 break;
2999 if (base == ep->to_rtx)
3001 rtx src
3002 = plus_constant (ep->to_rtx, offset - ep->offset);
3004 new_body = old_body;
3005 if (! replace)
3007 new_body = copy_insn (old_body);
3008 if (REG_NOTES (insn))
3009 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3011 PATTERN (insn) = new_body;
3012 old_set = single_set (insn);
3014 /* First see if this insn remains valid when we
3015 make the change. If not, keep the INSN_CODE
3016 the same and let reload fit it up. */
3017 validate_change (insn, &SET_SRC (old_set), src, 1);
3018 validate_change (insn, &SET_DEST (old_set),
3019 ep->to_rtx, 1);
3020 if (! apply_change_group ())
3022 SET_SRC (old_set) = src;
3023 SET_DEST (old_set) = ep->to_rtx;
3026 val = 1;
3027 goto done;
3030 #endif
3032 /* In this case this insn isn't serving a useful purpose. We
3033 will delete it in reload_as_needed once we know that this
3034 elimination is, in fact, being done.
3036 If REPLACE isn't set, we can't delete this insn, but needn't
3037 process it since it won't be used unless something changes. */
3038 if (replace)
3040 delete_dead_insn (insn);
3041 return 1;
3043 val = 1;
3044 goto done;
3048 /* We allow one special case which happens to work on all machines we
3049 currently support: a single set with the source or a REG_EQUAL
3050 note being a PLUS of an eliminable register and a constant. */
3051 plus_src = plus_cst_src = 0;
3052 if (old_set && REG_P (SET_DEST (old_set)))
3054 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3055 plus_src = SET_SRC (old_set);
3056 /* First see if the source is of the form (plus (...) CST). */
3057 if (plus_src
3058 && GET_CODE (XEXP (plus_src, 1)) == CONST_INT)
3059 plus_cst_src = plus_src;
3060 else if (REG_P (SET_SRC (old_set))
3061 || plus_src)
3063 /* Otherwise, see if we have a REG_EQUAL note of the form
3064 (plus (...) CST). */
3065 rtx links;
3066 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3068 if (REG_NOTE_KIND (links) == REG_EQUAL
3069 && GET_CODE (XEXP (links, 0)) == PLUS
3070 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT)
3072 plus_cst_src = XEXP (links, 0);
3073 break;
3078 /* Check that the first operand of the PLUS is a hard reg or
3079 the lowpart subreg of one. */
3080 if (plus_cst_src)
3082 rtx reg = XEXP (plus_cst_src, 0);
3083 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3084 reg = SUBREG_REG (reg);
3086 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3087 plus_cst_src = 0;
3090 if (plus_cst_src)
3092 rtx reg = XEXP (plus_cst_src, 0);
3093 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3095 if (GET_CODE (reg) == SUBREG)
3096 reg = SUBREG_REG (reg);
3098 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3099 if (ep->from_rtx == reg && ep->can_eliminate)
3101 rtx to_rtx = ep->to_rtx;
3102 offset += ep->offset;
3103 offset = trunc_int_for_mode (offset, GET_MODE (reg));
3105 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3106 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3107 to_rtx);
3108 /* If we have a nonzero offset, and the source is already
3109 a simple REG, the following transformation would
3110 increase the cost of the insn by replacing a simple REG
3111 with (plus (reg sp) CST). So try only when we already
3112 had a PLUS before. */
3113 if (offset == 0 || plus_src)
3115 rtx new_src = plus_constant (to_rtx, offset);
3117 new_body = old_body;
3118 if (! replace)
3120 new_body = copy_insn (old_body);
3121 if (REG_NOTES (insn))
3122 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3124 PATTERN (insn) = new_body;
3125 old_set = single_set (insn);
3127 /* First see if this insn remains valid when we make the
3128 change. If not, try to replace the whole pattern with
3129 a simple set (this may help if the original insn was a
3130 PARALLEL that was only recognized as single_set due to
3131 REG_UNUSED notes). If this isn't valid either, keep
3132 the INSN_CODE the same and let reload fix it up. */
3133 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3135 rtx new_pat = gen_rtx_SET (VOIDmode,
3136 SET_DEST (old_set), new_src);
3138 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3139 SET_SRC (old_set) = new_src;
3142 else
3143 break;
3145 val = 1;
3146 /* This can't have an effect on elimination offsets, so skip right
3147 to the end. */
3148 goto done;
3152 /* Determine the effects of this insn on elimination offsets. */
3153 elimination_effects (old_body, 0);
3155 /* Eliminate all eliminable registers occurring in operands that
3156 can be handled by reload. */
3157 extract_insn (insn);
3158 for (i = 0; i < recog_data.n_operands; i++)
3160 orig_operand[i] = recog_data.operand[i];
3161 substed_operand[i] = recog_data.operand[i];
3163 /* For an asm statement, every operand is eliminable. */
3164 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3166 bool is_set_src, in_plus;
3168 /* Check for setting a register that we know about. */
3169 if (recog_data.operand_type[i] != OP_IN
3170 && REG_P (orig_operand[i]))
3172 /* If we are assigning to a register that can be eliminated, it
3173 must be as part of a PARALLEL, since the code above handles
3174 single SETs. We must indicate that we can no longer
3175 eliminate this reg. */
3176 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3177 ep++)
3178 if (ep->from_rtx == orig_operand[i])
3179 ep->can_eliminate = 0;
3182 /* Companion to the above plus substitution, we can allow
3183 invariants as the source of a plain move. */
3184 is_set_src = false;
3185 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3186 is_set_src = true;
3187 in_plus = false;
3188 if (plus_src
3189 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3190 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3191 in_plus = true;
3193 substed_operand[i]
3194 = eliminate_regs_1 (recog_data.operand[i], 0,
3195 replace ? insn : NULL_RTX,
3196 is_set_src || in_plus);
3197 if (substed_operand[i] != orig_operand[i])
3198 val = 1;
3199 /* Terminate the search in check_eliminable_occurrences at
3200 this point. */
3201 *recog_data.operand_loc[i] = 0;
3203 /* If an output operand changed from a REG to a MEM and INSN is an
3204 insn, write a CLOBBER insn. */
3205 if (recog_data.operand_type[i] != OP_IN
3206 && REG_P (orig_operand[i])
3207 && MEM_P (substed_operand[i])
3208 && replace)
3209 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3210 insn);
3214 for (i = 0; i < recog_data.n_dups; i++)
3215 *recog_data.dup_loc[i]
3216 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3218 /* If any eliminable remain, they aren't eliminable anymore. */
3219 check_eliminable_occurrences (old_body);
3221 /* Substitute the operands; the new values are in the substed_operand
3222 array. */
3223 for (i = 0; i < recog_data.n_operands; i++)
3224 *recog_data.operand_loc[i] = substed_operand[i];
3225 for (i = 0; i < recog_data.n_dups; i++)
3226 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3228 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3229 re-recognize the insn. We do this in case we had a simple addition
3230 but now can do this as a load-address. This saves an insn in this
3231 common case.
3232 If re-recognition fails, the old insn code number will still be used,
3233 and some register operands may have changed into PLUS expressions.
3234 These will be handled by find_reloads by loading them into a register
3235 again. */
3237 if (val)
3239 /* If we aren't replacing things permanently and we changed something,
3240 make another copy to ensure that all the RTL is new. Otherwise
3241 things can go wrong if find_reload swaps commutative operands
3242 and one is inside RTL that has been copied while the other is not. */
3243 new_body = old_body;
3244 if (! replace)
3246 new_body = copy_insn (old_body);
3247 if (REG_NOTES (insn))
3248 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3250 PATTERN (insn) = new_body;
3252 /* If we had a move insn but now we don't, rerecognize it. This will
3253 cause spurious re-recognition if the old move had a PARALLEL since
3254 the new one still will, but we can't call single_set without
3255 having put NEW_BODY into the insn and the re-recognition won't
3256 hurt in this rare case. */
3257 /* ??? Why this huge if statement - why don't we just rerecognize the
3258 thing always? */
3259 if (! insn_is_asm
3260 && old_set != 0
3261 && ((REG_P (SET_SRC (old_set))
3262 && (GET_CODE (new_body) != SET
3263 || !REG_P (SET_SRC (new_body))))
3264 /* If this was a load from or store to memory, compare
3265 the MEM in recog_data.operand to the one in the insn.
3266 If they are not equal, then rerecognize the insn. */
3267 || (old_set != 0
3268 && ((MEM_P (SET_SRC (old_set))
3269 && SET_SRC (old_set) != recog_data.operand[1])
3270 || (MEM_P (SET_DEST (old_set))
3271 && SET_DEST (old_set) != recog_data.operand[0])))
3272 /* If this was an add insn before, rerecognize. */
3273 || GET_CODE (SET_SRC (old_set)) == PLUS))
3275 int new_icode = recog (PATTERN (insn), insn, 0);
3276 if (new_icode >= 0)
3277 INSN_CODE (insn) = new_icode;
3281 /* Restore the old body. If there were any changes to it, we made a copy
3282 of it while the changes were still in place, so we'll correctly return
3283 a modified insn below. */
3284 if (! replace)
3286 /* Restore the old body. */
3287 for (i = 0; i < recog_data.n_operands; i++)
3288 *recog_data.operand_loc[i] = orig_operand[i];
3289 for (i = 0; i < recog_data.n_dups; i++)
3290 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3293 /* Update all elimination pairs to reflect the status after the current
3294 insn. The changes we make were determined by the earlier call to
3295 elimination_effects.
3297 We also detect cases where register elimination cannot be done,
3298 namely, if a register would be both changed and referenced outside a MEM
3299 in the resulting insn since such an insn is often undefined and, even if
3300 not, we cannot know what meaning will be given to it. Note that it is
3301 valid to have a register used in an address in an insn that changes it
3302 (presumably with a pre- or post-increment or decrement).
3304 If anything changes, return nonzero. */
3306 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3308 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3309 ep->can_eliminate = 0;
3311 ep->ref_outside_mem = 0;
3313 if (ep->previous_offset != ep->offset)
3314 val = 1;
3317 done:
3318 /* If we changed something, perform elimination in REG_NOTES. This is
3319 needed even when REPLACE is zero because a REG_DEAD note might refer
3320 to a register that we eliminate and could cause a different number
3321 of spill registers to be needed in the final reload pass than in
3322 the pre-passes. */
3323 if (val && REG_NOTES (insn) != 0)
3324 REG_NOTES (insn)
3325 = eliminate_regs_1 (REG_NOTES (insn), 0, REG_NOTES (insn), true);
3327 return val;
3330 /* Loop through all elimination pairs.
3331 Recalculate the number not at initial offset.
3333 Compute the maximum offset (minimum offset if the stack does not
3334 grow downward) for each elimination pair. */
3336 static void
3337 update_eliminable_offsets (void)
3339 struct elim_table *ep;
3341 num_not_at_initial_offset = 0;
3342 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3344 ep->previous_offset = ep->offset;
3345 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3346 num_not_at_initial_offset++;
3350 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3351 replacement we currently believe is valid, mark it as not eliminable if X
3352 modifies DEST in any way other than by adding a constant integer to it.
3354 If DEST is the frame pointer, we do nothing because we assume that
3355 all assignments to the hard frame pointer are nonlocal gotos and are being
3356 done at a time when they are valid and do not disturb anything else.
3357 Some machines want to eliminate a fake argument pointer with either the
3358 frame or stack pointer. Assignments to the hard frame pointer must not
3359 prevent this elimination.
3361 Called via note_stores from reload before starting its passes to scan
3362 the insns of the function. */
3364 static void
3365 mark_not_eliminable (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
3367 unsigned int i;
3369 /* A SUBREG of a hard register here is just changing its mode. We should
3370 not see a SUBREG of an eliminable hard register, but check just in
3371 case. */
3372 if (GET_CODE (dest) == SUBREG)
3373 dest = SUBREG_REG (dest);
3375 if (dest == hard_frame_pointer_rtx)
3376 return;
3378 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3379 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3380 && (GET_CODE (x) != SET
3381 || GET_CODE (SET_SRC (x)) != PLUS
3382 || XEXP (SET_SRC (x), 0) != dest
3383 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3385 reg_eliminate[i].can_eliminate_previous
3386 = reg_eliminate[i].can_eliminate = 0;
3387 num_eliminable--;
3391 /* Verify that the initial elimination offsets did not change since the
3392 last call to set_initial_elim_offsets. This is used to catch cases
3393 where something illegal happened during reload_as_needed that could
3394 cause incorrect code to be generated if we did not check for it. */
3396 static bool
3397 verify_initial_elim_offsets (void)
3399 HOST_WIDE_INT t;
3401 if (!num_eliminable)
3402 return true;
3404 #ifdef ELIMINABLE_REGS
3406 struct elim_table *ep;
3408 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3410 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3411 if (t != ep->initial_offset)
3412 return false;
3415 #else
3416 INITIAL_FRAME_POINTER_OFFSET (t);
3417 if (t != reg_eliminate[0].initial_offset)
3418 return false;
3419 #endif
3421 return true;
3424 /* Reset all offsets on eliminable registers to their initial values. */
3426 static void
3427 set_initial_elim_offsets (void)
3429 struct elim_table *ep = reg_eliminate;
3431 #ifdef ELIMINABLE_REGS
3432 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3434 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3435 ep->previous_offset = ep->offset = ep->initial_offset;
3437 #else
3438 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3439 ep->previous_offset = ep->offset = ep->initial_offset;
3440 #endif
3442 num_not_at_initial_offset = 0;
3445 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3447 static void
3448 set_initial_eh_label_offset (rtx label)
3450 set_label_offsets (label, NULL_RTX, 1);
3453 /* Initialize the known label offsets.
3454 Set a known offset for each forced label to be at the initial offset
3455 of each elimination. We do this because we assume that all
3456 computed jumps occur from a location where each elimination is
3457 at its initial offset.
3458 For all other labels, show that we don't know the offsets. */
3460 static void
3461 set_initial_label_offsets (void)
3463 rtx x;
3464 memset (offsets_known_at, 0, num_labels);
3466 for (x = forced_labels; x; x = XEXP (x, 1))
3467 if (XEXP (x, 0))
3468 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3470 for_each_eh_label (set_initial_eh_label_offset);
3473 /* Set all elimination offsets to the known values for the code label given
3474 by INSN. */
3476 static void
3477 set_offsets_for_label (rtx insn)
3479 unsigned int i;
3480 int label_nr = CODE_LABEL_NUMBER (insn);
3481 struct elim_table *ep;
3483 num_not_at_initial_offset = 0;
3484 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3486 ep->offset = ep->previous_offset
3487 = offsets_at[label_nr - first_label_num][i];
3488 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3489 num_not_at_initial_offset++;
3493 /* See if anything that happened changes which eliminations are valid.
3494 For example, on the SPARC, whether or not the frame pointer can
3495 be eliminated can depend on what registers have been used. We need
3496 not check some conditions again (such as flag_omit_frame_pointer)
3497 since they can't have changed. */
3499 static void
3500 update_eliminables (HARD_REG_SET *pset)
3502 int previous_frame_pointer_needed = frame_pointer_needed;
3503 struct elim_table *ep;
3505 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3506 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3507 #ifdef ELIMINABLE_REGS
3508 || ! CAN_ELIMINATE (ep->from, ep->to)
3509 #endif
3511 ep->can_eliminate = 0;
3513 /* Look for the case where we have discovered that we can't replace
3514 register A with register B and that means that we will now be
3515 trying to replace register A with register C. This means we can
3516 no longer replace register C with register B and we need to disable
3517 such an elimination, if it exists. This occurs often with A == ap,
3518 B == sp, and C == fp. */
3520 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3522 struct elim_table *op;
3523 int new_to = -1;
3525 if (! ep->can_eliminate && ep->can_eliminate_previous)
3527 /* Find the current elimination for ep->from, if there is a
3528 new one. */
3529 for (op = reg_eliminate;
3530 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3531 if (op->from == ep->from && op->can_eliminate)
3533 new_to = op->to;
3534 break;
3537 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3538 disable it. */
3539 for (op = reg_eliminate;
3540 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3541 if (op->from == new_to && op->to == ep->to)
3542 op->can_eliminate = 0;
3546 /* See if any registers that we thought we could eliminate the previous
3547 time are no longer eliminable. If so, something has changed and we
3548 must spill the register. Also, recompute the number of eliminable
3549 registers and see if the frame pointer is needed; it is if there is
3550 no elimination of the frame pointer that we can perform. */
3552 frame_pointer_needed = 1;
3553 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3555 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3556 && ep->to != HARD_FRAME_POINTER_REGNUM)
3557 frame_pointer_needed = 0;
3559 if (! ep->can_eliminate && ep->can_eliminate_previous)
3561 ep->can_eliminate_previous = 0;
3562 SET_HARD_REG_BIT (*pset, ep->from);
3563 num_eliminable--;
3567 /* If we didn't need a frame pointer last time, but we do now, spill
3568 the hard frame pointer. */
3569 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3570 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3573 /* Initialize the table of registers to eliminate. */
3575 static void
3576 init_elim_table (void)
3578 struct elim_table *ep;
3579 #ifdef ELIMINABLE_REGS
3580 const struct elim_table_1 *ep1;
3581 #endif
3583 if (!reg_eliminate)
3584 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3586 /* Does this function require a frame pointer? */
3588 frame_pointer_needed = (! flag_omit_frame_pointer
3589 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3590 and restore sp for alloca. So we can't eliminate
3591 the frame pointer in that case. At some point,
3592 we should improve this by emitting the
3593 sp-adjusting insns for this case. */
3594 || (current_function_calls_alloca
3595 && EXIT_IGNORE_STACK)
3596 || current_function_accesses_prior_frames
3597 || FRAME_POINTER_REQUIRED);
3599 num_eliminable = 0;
3601 #ifdef ELIMINABLE_REGS
3602 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3603 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3605 ep->from = ep1->from;
3606 ep->to = ep1->to;
3607 ep->can_eliminate = ep->can_eliminate_previous
3608 = (CAN_ELIMINATE (ep->from, ep->to)
3609 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3611 #else
3612 reg_eliminate[0].from = reg_eliminate_1[0].from;
3613 reg_eliminate[0].to = reg_eliminate_1[0].to;
3614 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3615 = ! frame_pointer_needed;
3616 #endif
3618 /* Count the number of eliminable registers and build the FROM and TO
3619 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3620 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3621 We depend on this. */
3622 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3624 num_eliminable += ep->can_eliminate;
3625 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3626 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3630 /* Kick all pseudos out of hard register REGNO.
3632 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3633 because we found we can't eliminate some register. In the case, no pseudos
3634 are allowed to be in the register, even if they are only in a block that
3635 doesn't require spill registers, unlike the case when we are spilling this
3636 hard reg to produce another spill register.
3638 Return nonzero if any pseudos needed to be kicked out. */
3640 static void
3641 spill_hard_reg (unsigned int regno, int cant_eliminate)
3643 int i;
3645 if (cant_eliminate)
3647 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3648 regs_ever_live[regno] = 1;
3651 /* Spill every pseudo reg that was allocated to this reg
3652 or to something that overlaps this reg. */
3654 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3655 if (reg_renumber[i] >= 0
3656 && (unsigned int) reg_renumber[i] <= regno
3657 && ((unsigned int) reg_renumber[i]
3658 + hard_regno_nregs[(unsigned int) reg_renumber[i]]
3659 [PSEUDO_REGNO_MODE (i)]
3660 > regno))
3661 SET_REGNO_REG_SET (&spilled_pseudos, i);
3664 /* After find_reload_regs has been run for all insn that need reloads,
3665 and/or spill_hard_regs was called, this function is used to actually
3666 spill pseudo registers and try to reallocate them. It also sets up the
3667 spill_regs array for use by choose_reload_regs. */
3669 static int
3670 finish_spills (int global)
3672 struct insn_chain *chain;
3673 int something_changed = 0;
3674 unsigned i;
3675 reg_set_iterator rsi;
3677 /* Build the spill_regs array for the function. */
3678 /* If there are some registers still to eliminate and one of the spill regs
3679 wasn't ever used before, additional stack space may have to be
3680 allocated to store this register. Thus, we may have changed the offset
3681 between the stack and frame pointers, so mark that something has changed.
3683 One might think that we need only set VAL to 1 if this is a call-used
3684 register. However, the set of registers that must be saved by the
3685 prologue is not identical to the call-used set. For example, the
3686 register used by the call insn for the return PC is a call-used register,
3687 but must be saved by the prologue. */
3689 n_spills = 0;
3690 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3691 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3693 spill_reg_order[i] = n_spills;
3694 spill_regs[n_spills++] = i;
3695 if (num_eliminable && ! regs_ever_live[i])
3696 something_changed = 1;
3697 regs_ever_live[i] = 1;
3699 else
3700 spill_reg_order[i] = -1;
3702 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
3704 /* Record the current hard register the pseudo is allocated to in
3705 pseudo_previous_regs so we avoid reallocating it to the same
3706 hard reg in a later pass. */
3707 gcc_assert (reg_renumber[i] >= 0);
3709 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3710 /* Mark it as no longer having a hard register home. */
3711 reg_renumber[i] = -1;
3712 /* We will need to scan everything again. */
3713 something_changed = 1;
3716 /* Retry global register allocation if possible. */
3717 if (global)
3719 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3720 /* For every insn that needs reloads, set the registers used as spill
3721 regs in pseudo_forbidden_regs for every pseudo live across the
3722 insn. */
3723 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3725 EXECUTE_IF_SET_IN_REG_SET
3726 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
3728 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3729 chain->used_spill_regs);
3731 EXECUTE_IF_SET_IN_REG_SET
3732 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
3734 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3735 chain->used_spill_regs);
3739 /* Retry allocating the spilled pseudos. For each reg, merge the
3740 various reg sets that indicate which hard regs can't be used,
3741 and call retry_global_alloc.
3742 We change spill_pseudos here to only contain pseudos that did not
3743 get a new hard register. */
3744 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3745 if (reg_old_renumber[i] != reg_renumber[i])
3747 HARD_REG_SET forbidden;
3748 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3749 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3750 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3751 retry_global_alloc (i, forbidden);
3752 if (reg_renumber[i] >= 0)
3753 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3757 /* Fix up the register information in the insn chain.
3758 This involves deleting those of the spilled pseudos which did not get
3759 a new hard register home from the live_{before,after} sets. */
3760 for (chain = reload_insn_chain; chain; chain = chain->next)
3762 HARD_REG_SET used_by_pseudos;
3763 HARD_REG_SET used_by_pseudos2;
3765 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3766 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3768 /* Mark any unallocated hard regs as available for spills. That
3769 makes inheritance work somewhat better. */
3770 if (chain->need_reload)
3772 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3773 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3774 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3776 /* Save the old value for the sanity test below. */
3777 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3779 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3780 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3781 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3782 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3784 /* Make sure we only enlarge the set. */
3785 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3786 gcc_unreachable ();
3787 ok:;
3791 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3792 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3794 int regno = reg_renumber[i];
3795 if (reg_old_renumber[i] == regno)
3796 continue;
3798 alter_reg (i, reg_old_renumber[i]);
3799 reg_old_renumber[i] = regno;
3800 if (dump_file)
3802 if (regno == -1)
3803 fprintf (dump_file, " Register %d now on stack.\n\n", i);
3804 else
3805 fprintf (dump_file, " Register %d now in %d.\n\n",
3806 i, reg_renumber[i]);
3810 return something_changed;
3813 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
3815 static void
3816 scan_paradoxical_subregs (rtx x)
3818 int i;
3819 const char *fmt;
3820 enum rtx_code code = GET_CODE (x);
3822 switch (code)
3824 case REG:
3825 case CONST_INT:
3826 case CONST:
3827 case SYMBOL_REF:
3828 case LABEL_REF:
3829 case CONST_DOUBLE:
3830 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3831 case CC0:
3832 case PC:
3833 case USE:
3834 case CLOBBER:
3835 return;
3837 case SUBREG:
3838 if (REG_P (SUBREG_REG (x))
3839 && (GET_MODE_SIZE (GET_MODE (x))
3840 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
3841 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3842 = GET_MODE_SIZE (GET_MODE (x));
3843 return;
3845 default:
3846 break;
3849 fmt = GET_RTX_FORMAT (code);
3850 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3852 if (fmt[i] == 'e')
3853 scan_paradoxical_subregs (XEXP (x, i));
3854 else if (fmt[i] == 'E')
3856 int j;
3857 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3858 scan_paradoxical_subregs (XVECEXP (x, i, j));
3863 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
3864 examine all of the reload insns between PREV and NEXT exclusive, and
3865 annotate all that may trap. */
3867 static void
3868 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
3870 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3871 unsigned int trap_count;
3872 rtx i;
3874 if (note == NULL)
3875 return;
3877 if (may_trap_p (PATTERN (insn)))
3878 trap_count = 1;
3879 else
3881 remove_note (insn, note);
3882 trap_count = 0;
3885 for (i = NEXT_INSN (prev); i != next; i = NEXT_INSN (i))
3886 if (INSN_P (i) && i != insn && may_trap_p (PATTERN (i)))
3888 trap_count++;
3889 REG_NOTES (i)
3890 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (note, 0), REG_NOTES (i));
3894 /* Reload pseudo-registers into hard regs around each insn as needed.
3895 Additional register load insns are output before the insn that needs it
3896 and perhaps store insns after insns that modify the reloaded pseudo reg.
3898 reg_last_reload_reg and reg_reloaded_contents keep track of
3899 which registers are already available in reload registers.
3900 We update these for the reloads that we perform,
3901 as the insns are scanned. */
3903 static void
3904 reload_as_needed (int live_known)
3906 struct insn_chain *chain;
3907 #if defined (AUTO_INC_DEC)
3908 int i;
3909 #endif
3910 rtx x;
3912 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
3913 memset (spill_reg_store, 0, sizeof spill_reg_store);
3914 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
3915 INIT_REG_SET (&reg_has_output_reload);
3916 CLEAR_HARD_REG_SET (reg_reloaded_valid);
3917 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
3919 set_initial_elim_offsets ();
3921 for (chain = reload_insn_chain; chain; chain = chain->next)
3923 rtx prev = 0;
3924 rtx insn = chain->insn;
3925 rtx old_next = NEXT_INSN (insn);
3927 /* If we pass a label, copy the offsets from the label information
3928 into the current offsets of each elimination. */
3929 if (LABEL_P (insn))
3930 set_offsets_for_label (insn);
3932 else if (INSN_P (insn))
3934 regset_head regs_to_forget;
3935 INIT_REG_SET (&regs_to_forget);
3936 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
3938 /* If this is a USE and CLOBBER of a MEM, ensure that any
3939 references to eliminable registers have been removed. */
3941 if ((GET_CODE (PATTERN (insn)) == USE
3942 || GET_CODE (PATTERN (insn)) == CLOBBER)
3943 && MEM_P (XEXP (PATTERN (insn), 0)))
3944 XEXP (XEXP (PATTERN (insn), 0), 0)
3945 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3946 GET_MODE (XEXP (PATTERN (insn), 0)),
3947 NULL_RTX);
3949 /* If we need to do register elimination processing, do so.
3950 This might delete the insn, in which case we are done. */
3951 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
3953 eliminate_regs_in_insn (insn, 1);
3954 if (NOTE_P (insn))
3956 update_eliminable_offsets ();
3957 CLEAR_REG_SET (&regs_to_forget);
3958 continue;
3962 /* If need_elim is nonzero but need_reload is zero, one might think
3963 that we could simply set n_reloads to 0. However, find_reloads
3964 could have done some manipulation of the insn (such as swapping
3965 commutative operands), and these manipulations are lost during
3966 the first pass for every insn that needs register elimination.
3967 So the actions of find_reloads must be redone here. */
3969 if (! chain->need_elim && ! chain->need_reload
3970 && ! chain->need_operand_change)
3971 n_reloads = 0;
3972 /* First find the pseudo regs that must be reloaded for this insn.
3973 This info is returned in the tables reload_... (see reload.h).
3974 Also modify the body of INSN by substituting RELOAD
3975 rtx's for those pseudo regs. */
3976 else
3978 CLEAR_REG_SET (&reg_has_output_reload);
3979 CLEAR_HARD_REG_SET (reg_is_output_reload);
3981 find_reloads (insn, 1, spill_indirect_levels, live_known,
3982 spill_reg_order);
3985 if (n_reloads > 0)
3987 rtx next = NEXT_INSN (insn);
3988 rtx p;
3990 prev = PREV_INSN (insn);
3992 /* Now compute which reload regs to reload them into. Perhaps
3993 reusing reload regs from previous insns, or else output
3994 load insns to reload them. Maybe output store insns too.
3995 Record the choices of reload reg in reload_reg_rtx. */
3996 choose_reload_regs (chain);
3998 /* Merge any reloads that we didn't combine for fear of
3999 increasing the number of spill registers needed but now
4000 discover can be safely merged. */
4001 if (SMALL_REGISTER_CLASSES)
4002 merge_assigned_reloads (insn);
4004 /* Generate the insns to reload operands into or out of
4005 their reload regs. */
4006 emit_reload_insns (chain);
4008 /* Substitute the chosen reload regs from reload_reg_rtx
4009 into the insn's body (or perhaps into the bodies of other
4010 load and store insn that we just made for reloading
4011 and that we moved the structure into). */
4012 subst_reloads (insn);
4014 /* Adjust the exception region notes for loads and stores. */
4015 if (flag_non_call_exceptions && !CALL_P (insn))
4016 fixup_eh_region_note (insn, prev, next);
4018 /* If this was an ASM, make sure that all the reload insns
4019 we have generated are valid. If not, give an error
4020 and delete them. */
4021 if (asm_noperands (PATTERN (insn)) >= 0)
4022 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4023 if (p != insn && INSN_P (p)
4024 && GET_CODE (PATTERN (p)) != USE
4025 && (recog_memoized (p) < 0
4026 || (extract_insn (p), ! constrain_operands (1))))
4028 error_for_asm (insn,
4029 "%<asm%> operand requires "
4030 "impossible reload");
4031 delete_insn (p);
4035 if (num_eliminable && chain->need_elim)
4036 update_eliminable_offsets ();
4038 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4039 is no longer validly lying around to save a future reload.
4040 Note that this does not detect pseudos that were reloaded
4041 for this insn in order to be stored in
4042 (obeying register constraints). That is correct; such reload
4043 registers ARE still valid. */
4044 forget_marked_reloads (&regs_to_forget);
4045 CLEAR_REG_SET (&regs_to_forget);
4047 /* There may have been CLOBBER insns placed after INSN. So scan
4048 between INSN and NEXT and use them to forget old reloads. */
4049 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4050 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4051 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4053 #ifdef AUTO_INC_DEC
4054 /* Likewise for regs altered by auto-increment in this insn.
4055 REG_INC notes have been changed by reloading:
4056 find_reloads_address_1 records substitutions for them,
4057 which have been performed by subst_reloads above. */
4058 for (i = n_reloads - 1; i >= 0; i--)
4060 rtx in_reg = rld[i].in_reg;
4061 if (in_reg)
4063 enum rtx_code code = GET_CODE (in_reg);
4064 /* PRE_INC / PRE_DEC will have the reload register ending up
4065 with the same value as the stack slot, but that doesn't
4066 hold true for POST_INC / POST_DEC. Either we have to
4067 convert the memory access to a true POST_INC / POST_DEC,
4068 or we can't use the reload register for inheritance. */
4069 if ((code == POST_INC || code == POST_DEC)
4070 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4071 REGNO (rld[i].reg_rtx))
4072 /* Make sure it is the inc/dec pseudo, and not
4073 some other (e.g. output operand) pseudo. */
4074 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4075 == REGNO (XEXP (in_reg, 0))))
4078 rtx reload_reg = rld[i].reg_rtx;
4079 enum machine_mode mode = GET_MODE (reload_reg);
4080 int n = 0;
4081 rtx p;
4083 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4085 /* We really want to ignore REG_INC notes here, so
4086 use PATTERN (p) as argument to reg_set_p . */
4087 if (reg_set_p (reload_reg, PATTERN (p)))
4088 break;
4089 n = count_occurrences (PATTERN (p), reload_reg, 0);
4090 if (! n)
4091 continue;
4092 if (n == 1)
4094 n = validate_replace_rtx (reload_reg,
4095 gen_rtx_fmt_e (code,
4096 mode,
4097 reload_reg),
4100 /* We must also verify that the constraints
4101 are met after the replacement. */
4102 extract_insn (p);
4103 if (n)
4104 n = constrain_operands (1);
4105 else
4106 break;
4108 /* If the constraints were not met, then
4109 undo the replacement. */
4110 if (!n)
4112 validate_replace_rtx (gen_rtx_fmt_e (code,
4113 mode,
4114 reload_reg),
4115 reload_reg, p);
4116 break;
4120 break;
4122 if (n == 1)
4124 REG_NOTES (p)
4125 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4126 REG_NOTES (p));
4127 /* Mark this as having an output reload so that the
4128 REG_INC processing code below won't invalidate
4129 the reload for inheritance. */
4130 SET_HARD_REG_BIT (reg_is_output_reload,
4131 REGNO (reload_reg));
4132 SET_REGNO_REG_SET (&reg_has_output_reload,
4133 REGNO (XEXP (in_reg, 0)));
4135 else
4136 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4137 NULL);
4139 else if ((code == PRE_INC || code == PRE_DEC)
4140 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4141 REGNO (rld[i].reg_rtx))
4142 /* Make sure it is the inc/dec pseudo, and not
4143 some other (e.g. output operand) pseudo. */
4144 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4145 == REGNO (XEXP (in_reg, 0))))
4147 SET_HARD_REG_BIT (reg_is_output_reload,
4148 REGNO (rld[i].reg_rtx));
4149 SET_REGNO_REG_SET (&reg_has_output_reload,
4150 REGNO (XEXP (in_reg, 0)));
4154 /* If a pseudo that got a hard register is auto-incremented,
4155 we must purge records of copying it into pseudos without
4156 hard registers. */
4157 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4158 if (REG_NOTE_KIND (x) == REG_INC)
4160 /* See if this pseudo reg was reloaded in this insn.
4161 If so, its last-reload info is still valid
4162 because it is based on this insn's reload. */
4163 for (i = 0; i < n_reloads; i++)
4164 if (rld[i].out == XEXP (x, 0))
4165 break;
4167 if (i == n_reloads)
4168 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4170 #endif
4172 /* A reload reg's contents are unknown after a label. */
4173 if (LABEL_P (insn))
4174 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4176 /* Don't assume a reload reg is still good after a call insn
4177 if it is a call-used reg, or if it contains a value that will
4178 be partially clobbered by the call. */
4179 else if (CALL_P (insn))
4181 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4182 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4186 /* Clean up. */
4187 free (reg_last_reload_reg);
4188 CLEAR_REG_SET (&reg_has_output_reload);
4191 /* Discard all record of any value reloaded from X,
4192 or reloaded in X from someplace else;
4193 unless X is an output reload reg of the current insn.
4195 X may be a hard reg (the reload reg)
4196 or it may be a pseudo reg that was reloaded from.
4198 When DATA is non-NULL just mark the registers in regset
4199 to be forgotten later. */
4201 static void
4202 forget_old_reloads_1 (rtx x, rtx ignored ATTRIBUTE_UNUSED,
4203 void *data)
4205 unsigned int regno;
4206 unsigned int nr;
4207 regset regs = (regset) data;
4209 /* note_stores does give us subregs of hard regs,
4210 subreg_regno_offset requires a hard reg. */
4211 while (GET_CODE (x) == SUBREG)
4213 /* We ignore the subreg offset when calculating the regno,
4214 because we are using the entire underlying hard register
4215 below. */
4216 x = SUBREG_REG (x);
4219 if (!REG_P (x))
4220 return;
4222 regno = REGNO (x);
4224 if (regno >= FIRST_PSEUDO_REGISTER)
4225 nr = 1;
4226 else
4228 unsigned int i;
4230 nr = hard_regno_nregs[regno][GET_MODE (x)];
4231 /* Storing into a spilled-reg invalidates its contents.
4232 This can happen if a block-local pseudo is allocated to that reg
4233 and it wasn't spilled because this block's total need is 0.
4234 Then some insn might have an optional reload and use this reg. */
4235 if (!regs)
4236 for (i = 0; i < nr; i++)
4237 /* But don't do this if the reg actually serves as an output
4238 reload reg in the current instruction. */
4239 if (n_reloads == 0
4240 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4242 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4243 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, regno + i);
4244 spill_reg_store[regno + i] = 0;
4248 if (regs)
4249 while (nr-- > 0)
4250 SET_REGNO_REG_SET (regs, regno + nr);
4251 else
4253 /* Since value of X has changed,
4254 forget any value previously copied from it. */
4256 while (nr-- > 0)
4257 /* But don't forget a copy if this is the output reload
4258 that establishes the copy's validity. */
4259 if (n_reloads == 0
4260 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4261 reg_last_reload_reg[regno + nr] = 0;
4265 /* Forget the reloads marked in regset by previous function. */
4266 static void
4267 forget_marked_reloads (regset regs)
4269 unsigned int reg;
4270 reg_set_iterator rsi;
4271 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4273 if (reg < FIRST_PSEUDO_REGISTER
4274 /* But don't do this if the reg actually serves as an output
4275 reload reg in the current instruction. */
4276 && (n_reloads == 0
4277 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4279 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4280 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, reg);
4281 spill_reg_store[reg] = 0;
4283 if (n_reloads == 0
4284 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4285 reg_last_reload_reg[reg] = 0;
4289 /* The following HARD_REG_SETs indicate when each hard register is
4290 used for a reload of various parts of the current insn. */
4292 /* If reg is unavailable for all reloads. */
4293 static HARD_REG_SET reload_reg_unavailable;
4294 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4295 static HARD_REG_SET reload_reg_used;
4296 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4297 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4298 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4299 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4300 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4301 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4302 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4303 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4304 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4305 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4306 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4307 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4308 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4309 static HARD_REG_SET reload_reg_used_in_op_addr;
4310 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4311 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4312 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4313 static HARD_REG_SET reload_reg_used_in_insn;
4314 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4315 static HARD_REG_SET reload_reg_used_in_other_addr;
4317 /* If reg is in use as a reload reg for any sort of reload. */
4318 static HARD_REG_SET reload_reg_used_at_all;
4320 /* If reg is use as an inherited reload. We just mark the first register
4321 in the group. */
4322 static HARD_REG_SET reload_reg_used_for_inherit;
4324 /* Records which hard regs are used in any way, either as explicit use or
4325 by being allocated to a pseudo during any point of the current insn. */
4326 static HARD_REG_SET reg_used_in_insn;
4328 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4329 TYPE. MODE is used to indicate how many consecutive regs are
4330 actually used. */
4332 static void
4333 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4334 enum machine_mode mode)
4336 unsigned int nregs = hard_regno_nregs[regno][mode];
4337 unsigned int i;
4339 for (i = regno; i < nregs + regno; i++)
4341 switch (type)
4343 case RELOAD_OTHER:
4344 SET_HARD_REG_BIT (reload_reg_used, i);
4345 break;
4347 case RELOAD_FOR_INPUT_ADDRESS:
4348 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4349 break;
4351 case RELOAD_FOR_INPADDR_ADDRESS:
4352 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4353 break;
4355 case RELOAD_FOR_OUTPUT_ADDRESS:
4356 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4357 break;
4359 case RELOAD_FOR_OUTADDR_ADDRESS:
4360 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4361 break;
4363 case RELOAD_FOR_OPERAND_ADDRESS:
4364 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4365 break;
4367 case RELOAD_FOR_OPADDR_ADDR:
4368 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4369 break;
4371 case RELOAD_FOR_OTHER_ADDRESS:
4372 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4373 break;
4375 case RELOAD_FOR_INPUT:
4376 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4377 break;
4379 case RELOAD_FOR_OUTPUT:
4380 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4381 break;
4383 case RELOAD_FOR_INSN:
4384 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4385 break;
4388 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4392 /* Similarly, but show REGNO is no longer in use for a reload. */
4394 static void
4395 clear_reload_reg_in_use (unsigned int regno, int opnum,
4396 enum reload_type type, enum machine_mode mode)
4398 unsigned int nregs = hard_regno_nregs[regno][mode];
4399 unsigned int start_regno, end_regno, r;
4400 int i;
4401 /* A complication is that for some reload types, inheritance might
4402 allow multiple reloads of the same types to share a reload register.
4403 We set check_opnum if we have to check only reloads with the same
4404 operand number, and check_any if we have to check all reloads. */
4405 int check_opnum = 0;
4406 int check_any = 0;
4407 HARD_REG_SET *used_in_set;
4409 switch (type)
4411 case RELOAD_OTHER:
4412 used_in_set = &reload_reg_used;
4413 break;
4415 case RELOAD_FOR_INPUT_ADDRESS:
4416 used_in_set = &reload_reg_used_in_input_addr[opnum];
4417 break;
4419 case RELOAD_FOR_INPADDR_ADDRESS:
4420 check_opnum = 1;
4421 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4422 break;
4424 case RELOAD_FOR_OUTPUT_ADDRESS:
4425 used_in_set = &reload_reg_used_in_output_addr[opnum];
4426 break;
4428 case RELOAD_FOR_OUTADDR_ADDRESS:
4429 check_opnum = 1;
4430 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4431 break;
4433 case RELOAD_FOR_OPERAND_ADDRESS:
4434 used_in_set = &reload_reg_used_in_op_addr;
4435 break;
4437 case RELOAD_FOR_OPADDR_ADDR:
4438 check_any = 1;
4439 used_in_set = &reload_reg_used_in_op_addr_reload;
4440 break;
4442 case RELOAD_FOR_OTHER_ADDRESS:
4443 used_in_set = &reload_reg_used_in_other_addr;
4444 check_any = 1;
4445 break;
4447 case RELOAD_FOR_INPUT:
4448 used_in_set = &reload_reg_used_in_input[opnum];
4449 break;
4451 case RELOAD_FOR_OUTPUT:
4452 used_in_set = &reload_reg_used_in_output[opnum];
4453 break;
4455 case RELOAD_FOR_INSN:
4456 used_in_set = &reload_reg_used_in_insn;
4457 break;
4458 default:
4459 gcc_unreachable ();
4461 /* We resolve conflicts with remaining reloads of the same type by
4462 excluding the intervals of reload registers by them from the
4463 interval of freed reload registers. Since we only keep track of
4464 one set of interval bounds, we might have to exclude somewhat
4465 more than what would be necessary if we used a HARD_REG_SET here.
4466 But this should only happen very infrequently, so there should
4467 be no reason to worry about it. */
4469 start_regno = regno;
4470 end_regno = regno + nregs;
4471 if (check_opnum || check_any)
4473 for (i = n_reloads - 1; i >= 0; i--)
4475 if (rld[i].when_needed == type
4476 && (check_any || rld[i].opnum == opnum)
4477 && rld[i].reg_rtx)
4479 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4480 unsigned int conflict_end
4481 = (conflict_start
4482 + hard_regno_nregs[conflict_start][rld[i].mode]);
4484 /* If there is an overlap with the first to-be-freed register,
4485 adjust the interval start. */
4486 if (conflict_start <= start_regno && conflict_end > start_regno)
4487 start_regno = conflict_end;
4488 /* Otherwise, if there is a conflict with one of the other
4489 to-be-freed registers, adjust the interval end. */
4490 if (conflict_start > start_regno && conflict_start < end_regno)
4491 end_regno = conflict_start;
4496 for (r = start_regno; r < end_regno; r++)
4497 CLEAR_HARD_REG_BIT (*used_in_set, r);
4500 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4501 specified by OPNUM and TYPE. */
4503 static int
4504 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4506 int i;
4508 /* In use for a RELOAD_OTHER means it's not available for anything. */
4509 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4510 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4511 return 0;
4513 switch (type)
4515 case RELOAD_OTHER:
4516 /* In use for anything means we can't use it for RELOAD_OTHER. */
4517 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4518 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4519 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4520 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4521 return 0;
4523 for (i = 0; i < reload_n_operands; i++)
4524 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4525 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4526 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4527 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4528 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4529 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4530 return 0;
4532 return 1;
4534 case RELOAD_FOR_INPUT:
4535 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4536 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4537 return 0;
4539 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4540 return 0;
4542 /* If it is used for some other input, can't use it. */
4543 for (i = 0; i < reload_n_operands; i++)
4544 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4545 return 0;
4547 /* If it is used in a later operand's address, can't use it. */
4548 for (i = opnum + 1; i < reload_n_operands; i++)
4549 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4550 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4551 return 0;
4553 return 1;
4555 case RELOAD_FOR_INPUT_ADDRESS:
4556 /* Can't use a register if it is used for an input address for this
4557 operand or used as an input in an earlier one. */
4558 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4559 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4560 return 0;
4562 for (i = 0; i < opnum; i++)
4563 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4564 return 0;
4566 return 1;
4568 case RELOAD_FOR_INPADDR_ADDRESS:
4569 /* Can't use a register if it is used for an input address
4570 for this operand or used as an input in an earlier
4571 one. */
4572 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4573 return 0;
4575 for (i = 0; i < opnum; i++)
4576 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4577 return 0;
4579 return 1;
4581 case RELOAD_FOR_OUTPUT_ADDRESS:
4582 /* Can't use a register if it is used for an output address for this
4583 operand or used as an output in this or a later operand. Note
4584 that multiple output operands are emitted in reverse order, so
4585 the conflicting ones are those with lower indices. */
4586 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4587 return 0;
4589 for (i = 0; i <= opnum; i++)
4590 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4591 return 0;
4593 return 1;
4595 case RELOAD_FOR_OUTADDR_ADDRESS:
4596 /* Can't use a register if it is used for an output address
4597 for this operand or used as an output in this or a
4598 later operand. Note that multiple output operands are
4599 emitted in reverse order, so the conflicting ones are
4600 those with lower indices. */
4601 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4602 return 0;
4604 for (i = 0; i <= opnum; i++)
4605 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4606 return 0;
4608 return 1;
4610 case RELOAD_FOR_OPERAND_ADDRESS:
4611 for (i = 0; i < reload_n_operands; i++)
4612 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4613 return 0;
4615 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4616 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4618 case RELOAD_FOR_OPADDR_ADDR:
4619 for (i = 0; i < reload_n_operands; i++)
4620 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4621 return 0;
4623 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4625 case RELOAD_FOR_OUTPUT:
4626 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4627 outputs, or an operand address for this or an earlier output.
4628 Note that multiple output operands are emitted in reverse order,
4629 so the conflicting ones are those with higher indices. */
4630 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4631 return 0;
4633 for (i = 0; i < reload_n_operands; i++)
4634 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4635 return 0;
4637 for (i = opnum; i < reload_n_operands; i++)
4638 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4639 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4640 return 0;
4642 return 1;
4644 case RELOAD_FOR_INSN:
4645 for (i = 0; i < reload_n_operands; i++)
4646 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4647 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4648 return 0;
4650 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4651 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4653 case RELOAD_FOR_OTHER_ADDRESS:
4654 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4656 default:
4657 gcc_unreachable ();
4661 /* Return 1 if the value in reload reg REGNO, as used by a reload
4662 needed for the part of the insn specified by OPNUM and TYPE,
4663 is still available in REGNO at the end of the insn.
4665 We can assume that the reload reg was already tested for availability
4666 at the time it is needed, and we should not check this again,
4667 in case the reg has already been marked in use. */
4669 static int
4670 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4672 int i;
4674 switch (type)
4676 case RELOAD_OTHER:
4677 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4678 its value must reach the end. */
4679 return 1;
4681 /* If this use is for part of the insn,
4682 its value reaches if no subsequent part uses the same register.
4683 Just like the above function, don't try to do this with lots
4684 of fallthroughs. */
4686 case RELOAD_FOR_OTHER_ADDRESS:
4687 /* Here we check for everything else, since these don't conflict
4688 with anything else and everything comes later. */
4690 for (i = 0; i < reload_n_operands; i++)
4691 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4692 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4693 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4694 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4695 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4696 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4697 return 0;
4699 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4700 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4701 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4702 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4704 case RELOAD_FOR_INPUT_ADDRESS:
4705 case RELOAD_FOR_INPADDR_ADDRESS:
4706 /* Similar, except that we check only for this and subsequent inputs
4707 and the address of only subsequent inputs and we do not need
4708 to check for RELOAD_OTHER objects since they are known not to
4709 conflict. */
4711 for (i = opnum; i < reload_n_operands; i++)
4712 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4713 return 0;
4715 for (i = opnum + 1; i < reload_n_operands; i++)
4716 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4717 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4718 return 0;
4720 for (i = 0; i < reload_n_operands; i++)
4721 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4722 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4723 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4724 return 0;
4726 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4727 return 0;
4729 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4730 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4731 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4733 case RELOAD_FOR_INPUT:
4734 /* Similar to input address, except we start at the next operand for
4735 both input and input address and we do not check for
4736 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4737 would conflict. */
4739 for (i = opnum + 1; i < reload_n_operands; i++)
4740 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4741 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4742 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4743 return 0;
4745 /* ... fall through ... */
4747 case RELOAD_FOR_OPERAND_ADDRESS:
4748 /* Check outputs and their addresses. */
4750 for (i = 0; i < reload_n_operands; i++)
4751 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4752 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4753 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4754 return 0;
4756 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4758 case RELOAD_FOR_OPADDR_ADDR:
4759 for (i = 0; i < reload_n_operands; i++)
4760 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4761 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4762 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4763 return 0;
4765 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4766 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4767 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4769 case RELOAD_FOR_INSN:
4770 /* These conflict with other outputs with RELOAD_OTHER. So
4771 we need only check for output addresses. */
4773 opnum = reload_n_operands;
4775 /* ... fall through ... */
4777 case RELOAD_FOR_OUTPUT:
4778 case RELOAD_FOR_OUTPUT_ADDRESS:
4779 case RELOAD_FOR_OUTADDR_ADDRESS:
4780 /* We already know these can't conflict with a later output. So the
4781 only thing to check are later output addresses.
4782 Note that multiple output operands are emitted in reverse order,
4783 so the conflicting ones are those with lower indices. */
4784 for (i = 0; i < opnum; i++)
4785 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4786 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4787 return 0;
4789 return 1;
4791 default:
4792 gcc_unreachable ();
4797 /* Returns whether R1 and R2 are uniquely chained: the value of one
4798 is used by the other, and that value is not used by any other
4799 reload for this insn. This is used to partially undo the decision
4800 made in find_reloads when in the case of multiple
4801 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
4802 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
4803 reloads. This code tries to avoid the conflict created by that
4804 change. It might be cleaner to explicitly keep track of which
4805 RELOAD_FOR_OPADDR_ADDR reload is associated with which
4806 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
4807 this after the fact. */
4808 static bool
4809 reloads_unique_chain_p (int r1, int r2)
4811 int i;
4813 /* We only check input reloads. */
4814 if (! rld[r1].in || ! rld[r2].in)
4815 return false;
4817 /* Avoid anything with output reloads. */
4818 if (rld[r1].out || rld[r2].out)
4819 return false;
4821 /* "chained" means one reload is a component of the other reload,
4822 not the same as the other reload. */
4823 if (rld[r1].opnum != rld[r2].opnum
4824 || rtx_equal_p (rld[r1].in, rld[r2].in)
4825 || rld[r1].optional || rld[r2].optional
4826 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
4827 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
4828 return false;
4830 for (i = 0; i < n_reloads; i ++)
4831 /* Look for input reloads that aren't our two */
4832 if (i != r1 && i != r2 && rld[i].in)
4834 /* If our reload is mentioned at all, it isn't a simple chain. */
4835 if (reg_mentioned_p (rld[r1].in, rld[i].in))
4836 return false;
4838 return true;
4841 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4842 Return 0 otherwise.
4844 This function uses the same algorithm as reload_reg_free_p above. */
4846 static int
4847 reloads_conflict (int r1, int r2)
4849 enum reload_type r1_type = rld[r1].when_needed;
4850 enum reload_type r2_type = rld[r2].when_needed;
4851 int r1_opnum = rld[r1].opnum;
4852 int r2_opnum = rld[r2].opnum;
4854 /* RELOAD_OTHER conflicts with everything. */
4855 if (r2_type == RELOAD_OTHER)
4856 return 1;
4858 /* Otherwise, check conflicts differently for each type. */
4860 switch (r1_type)
4862 case RELOAD_FOR_INPUT:
4863 return (r2_type == RELOAD_FOR_INSN
4864 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4865 || r2_type == RELOAD_FOR_OPADDR_ADDR
4866 || r2_type == RELOAD_FOR_INPUT
4867 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4868 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4869 && r2_opnum > r1_opnum));
4871 case RELOAD_FOR_INPUT_ADDRESS:
4872 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4873 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4875 case RELOAD_FOR_INPADDR_ADDRESS:
4876 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4877 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4879 case RELOAD_FOR_OUTPUT_ADDRESS:
4880 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4881 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4883 case RELOAD_FOR_OUTADDR_ADDRESS:
4884 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4885 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4887 case RELOAD_FOR_OPERAND_ADDRESS:
4888 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4889 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
4890 && !reloads_unique_chain_p (r1, r2)));
4892 case RELOAD_FOR_OPADDR_ADDR:
4893 return (r2_type == RELOAD_FOR_INPUT
4894 || r2_type == RELOAD_FOR_OPADDR_ADDR);
4896 case RELOAD_FOR_OUTPUT:
4897 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
4898 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
4899 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
4900 && r2_opnum >= r1_opnum));
4902 case RELOAD_FOR_INSN:
4903 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
4904 || r2_type == RELOAD_FOR_INSN
4905 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
4907 case RELOAD_FOR_OTHER_ADDRESS:
4908 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
4910 case RELOAD_OTHER:
4911 return 1;
4913 default:
4914 gcc_unreachable ();
4918 /* Indexed by reload number, 1 if incoming value
4919 inherited from previous insns. */
4920 static char reload_inherited[MAX_RELOADS];
4922 /* For an inherited reload, this is the insn the reload was inherited from,
4923 if we know it. Otherwise, this is 0. */
4924 static rtx reload_inheritance_insn[MAX_RELOADS];
4926 /* If nonzero, this is a place to get the value of the reload,
4927 rather than using reload_in. */
4928 static rtx reload_override_in[MAX_RELOADS];
4930 /* For each reload, the hard register number of the register used,
4931 or -1 if we did not need a register for this reload. */
4932 static int reload_spill_index[MAX_RELOADS];
4934 /* Subroutine of free_for_value_p, used to check a single register.
4935 START_REGNO is the starting regno of the full reload register
4936 (possibly comprising multiple hard registers) that we are considering. */
4938 static int
4939 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
4940 enum reload_type type, rtx value, rtx out,
4941 int reloadnum, int ignore_address_reloads)
4943 int time1;
4944 /* Set if we see an input reload that must not share its reload register
4945 with any new earlyclobber, but might otherwise share the reload
4946 register with an output or input-output reload. */
4947 int check_earlyclobber = 0;
4948 int i;
4949 int copy = 0;
4951 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4952 return 0;
4954 if (out == const0_rtx)
4956 copy = 1;
4957 out = NULL_RTX;
4960 /* We use some pseudo 'time' value to check if the lifetimes of the
4961 new register use would overlap with the one of a previous reload
4962 that is not read-only or uses a different value.
4963 The 'time' used doesn't have to be linear in any shape or form, just
4964 monotonic.
4965 Some reload types use different 'buckets' for each operand.
4966 So there are MAX_RECOG_OPERANDS different time values for each
4967 such reload type.
4968 We compute TIME1 as the time when the register for the prospective
4969 new reload ceases to be live, and TIME2 for each existing
4970 reload as the time when that the reload register of that reload
4971 becomes live.
4972 Where there is little to be gained by exact lifetime calculations,
4973 we just make conservative assumptions, i.e. a longer lifetime;
4974 this is done in the 'default:' cases. */
4975 switch (type)
4977 case RELOAD_FOR_OTHER_ADDRESS:
4978 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
4979 time1 = copy ? 0 : 1;
4980 break;
4981 case RELOAD_OTHER:
4982 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
4983 break;
4984 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
4985 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
4986 respectively, to the time values for these, we get distinct time
4987 values. To get distinct time values for each operand, we have to
4988 multiply opnum by at least three. We round that up to four because
4989 multiply by four is often cheaper. */
4990 case RELOAD_FOR_INPADDR_ADDRESS:
4991 time1 = opnum * 4 + 2;
4992 break;
4993 case RELOAD_FOR_INPUT_ADDRESS:
4994 time1 = opnum * 4 + 3;
4995 break;
4996 case RELOAD_FOR_INPUT:
4997 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
4998 executes (inclusive). */
4999 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5000 break;
5001 case RELOAD_FOR_OPADDR_ADDR:
5002 /* opnum * 4 + 4
5003 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5004 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5005 break;
5006 case RELOAD_FOR_OPERAND_ADDRESS:
5007 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5008 is executed. */
5009 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5010 break;
5011 case RELOAD_FOR_OUTADDR_ADDRESS:
5012 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5013 break;
5014 case RELOAD_FOR_OUTPUT_ADDRESS:
5015 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5016 break;
5017 default:
5018 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5021 for (i = 0; i < n_reloads; i++)
5023 rtx reg = rld[i].reg_rtx;
5024 if (reg && REG_P (reg)
5025 && ((unsigned) regno - true_regnum (reg)
5026 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5027 && i != reloadnum)
5029 rtx other_input = rld[i].in;
5031 /* If the other reload loads the same input value, that
5032 will not cause a conflict only if it's loading it into
5033 the same register. */
5034 if (true_regnum (reg) != start_regno)
5035 other_input = NULL_RTX;
5036 if (! other_input || ! rtx_equal_p (other_input, value)
5037 || rld[i].out || out)
5039 int time2;
5040 switch (rld[i].when_needed)
5042 case RELOAD_FOR_OTHER_ADDRESS:
5043 time2 = 0;
5044 break;
5045 case RELOAD_FOR_INPADDR_ADDRESS:
5046 /* find_reloads makes sure that a
5047 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5048 by at most one - the first -
5049 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5050 address reload is inherited, the address address reload
5051 goes away, so we can ignore this conflict. */
5052 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5053 && ignore_address_reloads
5054 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5055 Then the address address is still needed to store
5056 back the new address. */
5057 && ! rld[reloadnum].out)
5058 continue;
5059 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5060 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5061 reloads go away. */
5062 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5063 && ignore_address_reloads
5064 /* Unless we are reloading an auto_inc expression. */
5065 && ! rld[reloadnum].out)
5066 continue;
5067 time2 = rld[i].opnum * 4 + 2;
5068 break;
5069 case RELOAD_FOR_INPUT_ADDRESS:
5070 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5071 && ignore_address_reloads
5072 && ! rld[reloadnum].out)
5073 continue;
5074 time2 = rld[i].opnum * 4 + 3;
5075 break;
5076 case RELOAD_FOR_INPUT:
5077 time2 = rld[i].opnum * 4 + 4;
5078 check_earlyclobber = 1;
5079 break;
5080 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5081 == MAX_RECOG_OPERAND * 4 */
5082 case RELOAD_FOR_OPADDR_ADDR:
5083 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5084 && ignore_address_reloads
5085 && ! rld[reloadnum].out)
5086 continue;
5087 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5088 break;
5089 case RELOAD_FOR_OPERAND_ADDRESS:
5090 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5091 check_earlyclobber = 1;
5092 break;
5093 case RELOAD_FOR_INSN:
5094 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5095 break;
5096 case RELOAD_FOR_OUTPUT:
5097 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5098 instruction is executed. */
5099 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5100 break;
5101 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5102 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5103 value. */
5104 case RELOAD_FOR_OUTADDR_ADDRESS:
5105 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5106 && ignore_address_reloads
5107 && ! rld[reloadnum].out)
5108 continue;
5109 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5110 break;
5111 case RELOAD_FOR_OUTPUT_ADDRESS:
5112 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5113 break;
5114 case RELOAD_OTHER:
5115 /* If there is no conflict in the input part, handle this
5116 like an output reload. */
5117 if (! rld[i].in || rtx_equal_p (other_input, value))
5119 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5120 /* Earlyclobbered outputs must conflict with inputs. */
5121 if (earlyclobber_operand_p (rld[i].out))
5122 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5124 break;
5126 time2 = 1;
5127 /* RELOAD_OTHER might be live beyond instruction execution,
5128 but this is not obvious when we set time2 = 1. So check
5129 here if there might be a problem with the new reload
5130 clobbering the register used by the RELOAD_OTHER. */
5131 if (out)
5132 return 0;
5133 break;
5134 default:
5135 return 0;
5137 if ((time1 >= time2
5138 && (! rld[i].in || rld[i].out
5139 || ! rtx_equal_p (other_input, value)))
5140 || (out && rld[reloadnum].out_reg
5141 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5142 return 0;
5147 /* Earlyclobbered outputs must conflict with inputs. */
5148 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5149 return 0;
5151 return 1;
5154 /* Return 1 if the value in reload reg REGNO, as used by a reload
5155 needed for the part of the insn specified by OPNUM and TYPE,
5156 may be used to load VALUE into it.
5158 MODE is the mode in which the register is used, this is needed to
5159 determine how many hard regs to test.
5161 Other read-only reloads with the same value do not conflict
5162 unless OUT is nonzero and these other reloads have to live while
5163 output reloads live.
5164 If OUT is CONST0_RTX, this is a special case: it means that the
5165 test should not be for using register REGNO as reload register, but
5166 for copying from register REGNO into the reload register.
5168 RELOADNUM is the number of the reload we want to load this value for;
5169 a reload does not conflict with itself.
5171 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5172 reloads that load an address for the very reload we are considering.
5174 The caller has to make sure that there is no conflict with the return
5175 register. */
5177 static int
5178 free_for_value_p (int regno, enum machine_mode mode, int opnum,
5179 enum reload_type type, rtx value, rtx out, int reloadnum,
5180 int ignore_address_reloads)
5182 int nregs = hard_regno_nregs[regno][mode];
5183 while (nregs-- > 0)
5184 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5185 value, out, reloadnum,
5186 ignore_address_reloads))
5187 return 0;
5188 return 1;
5191 /* Return nonzero if the rtx X is invariant over the current function. */
5192 /* ??? Actually, the places where we use this expect exactly what is
5193 tested here, and not everything that is function invariant. In
5194 particular, the frame pointer and arg pointer are special cased;
5195 pic_offset_table_rtx is not, and we must not spill these things to
5196 memory. */
5199 function_invariant_p (rtx x)
5201 if (CONSTANT_P (x))
5202 return 1;
5203 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
5204 return 1;
5205 if (GET_CODE (x) == PLUS
5206 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
5207 && CONSTANT_P (XEXP (x, 1)))
5208 return 1;
5209 return 0;
5212 /* Determine whether the reload reg X overlaps any rtx'es used for
5213 overriding inheritance. Return nonzero if so. */
5215 static int
5216 conflicts_with_override (rtx x)
5218 int i;
5219 for (i = 0; i < n_reloads; i++)
5220 if (reload_override_in[i]
5221 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5222 return 1;
5223 return 0;
5226 /* Give an error message saying we failed to find a reload for INSN,
5227 and clear out reload R. */
5228 static void
5229 failed_reload (rtx insn, int r)
5231 if (asm_noperands (PATTERN (insn)) < 0)
5232 /* It's the compiler's fault. */
5233 fatal_insn ("could not find a spill register", insn);
5235 /* It's the user's fault; the operand's mode and constraint
5236 don't match. Disable this reload so we don't crash in final. */
5237 error_for_asm (insn,
5238 "%<asm%> operand constraint incompatible with operand size");
5239 rld[r].in = 0;
5240 rld[r].out = 0;
5241 rld[r].reg_rtx = 0;
5242 rld[r].optional = 1;
5243 rld[r].secondary_p = 1;
5246 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5247 for reload R. If it's valid, get an rtx for it. Return nonzero if
5248 successful. */
5249 static int
5250 set_reload_reg (int i, int r)
5252 int regno;
5253 rtx reg = spill_reg_rtx[i];
5255 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5256 spill_reg_rtx[i] = reg
5257 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5259 regno = true_regnum (reg);
5261 /* Detect when the reload reg can't hold the reload mode.
5262 This used to be one `if', but Sequent compiler can't handle that. */
5263 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5265 enum machine_mode test_mode = VOIDmode;
5266 if (rld[r].in)
5267 test_mode = GET_MODE (rld[r].in);
5268 /* If rld[r].in has VOIDmode, it means we will load it
5269 in whatever mode the reload reg has: to wit, rld[r].mode.
5270 We have already tested that for validity. */
5271 /* Aside from that, we need to test that the expressions
5272 to reload from or into have modes which are valid for this
5273 reload register. Otherwise the reload insns would be invalid. */
5274 if (! (rld[r].in != 0 && test_mode != VOIDmode
5275 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5276 if (! (rld[r].out != 0
5277 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5279 /* The reg is OK. */
5280 last_spill_reg = i;
5282 /* Mark as in use for this insn the reload regs we use
5283 for this. */
5284 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5285 rld[r].when_needed, rld[r].mode);
5287 rld[r].reg_rtx = reg;
5288 reload_spill_index[r] = spill_regs[i];
5289 return 1;
5292 return 0;
5295 /* Find a spill register to use as a reload register for reload R.
5296 LAST_RELOAD is nonzero if this is the last reload for the insn being
5297 processed.
5299 Set rld[R].reg_rtx to the register allocated.
5301 We return 1 if successful, or 0 if we couldn't find a spill reg and
5302 we didn't change anything. */
5304 static int
5305 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5306 int last_reload)
5308 int i, pass, count;
5310 /* If we put this reload ahead, thinking it is a group,
5311 then insist on finding a group. Otherwise we can grab a
5312 reg that some other reload needs.
5313 (That can happen when we have a 68000 DATA_OR_FP_REG
5314 which is a group of data regs or one fp reg.)
5315 We need not be so restrictive if there are no more reloads
5316 for this insn.
5318 ??? Really it would be nicer to have smarter handling
5319 for that kind of reg class, where a problem like this is normal.
5320 Perhaps those classes should be avoided for reloading
5321 by use of more alternatives. */
5323 int force_group = rld[r].nregs > 1 && ! last_reload;
5325 /* If we want a single register and haven't yet found one,
5326 take any reg in the right class and not in use.
5327 If we want a consecutive group, here is where we look for it.
5329 We use two passes so we can first look for reload regs to
5330 reuse, which are already in use for other reloads in this insn,
5331 and only then use additional registers.
5332 I think that maximizing reuse is needed to make sure we don't
5333 run out of reload regs. Suppose we have three reloads, and
5334 reloads A and B can share regs. These need two regs.
5335 Suppose A and B are given different regs.
5336 That leaves none for C. */
5337 for (pass = 0; pass < 2; pass++)
5339 /* I is the index in spill_regs.
5340 We advance it round-robin between insns to use all spill regs
5341 equally, so that inherited reloads have a chance
5342 of leapfrogging each other. */
5344 i = last_spill_reg;
5346 for (count = 0; count < n_spills; count++)
5348 int class = (int) rld[r].class;
5349 int regnum;
5351 i++;
5352 if (i >= n_spills)
5353 i -= n_spills;
5354 regnum = spill_regs[i];
5356 if ((reload_reg_free_p (regnum, rld[r].opnum,
5357 rld[r].when_needed)
5358 || (rld[r].in
5359 /* We check reload_reg_used to make sure we
5360 don't clobber the return register. */
5361 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5362 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5363 rld[r].when_needed, rld[r].in,
5364 rld[r].out, r, 1)))
5365 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5366 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5367 /* Look first for regs to share, then for unshared. But
5368 don't share regs used for inherited reloads; they are
5369 the ones we want to preserve. */
5370 && (pass
5371 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5372 regnum)
5373 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5374 regnum))))
5376 int nr = hard_regno_nregs[regnum][rld[r].mode];
5377 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5378 (on 68000) got us two FP regs. If NR is 1,
5379 we would reject both of them. */
5380 if (force_group)
5381 nr = rld[r].nregs;
5382 /* If we need only one reg, we have already won. */
5383 if (nr == 1)
5385 /* But reject a single reg if we demand a group. */
5386 if (force_group)
5387 continue;
5388 break;
5390 /* Otherwise check that as many consecutive regs as we need
5391 are available here. */
5392 while (nr > 1)
5394 int regno = regnum + nr - 1;
5395 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5396 && spill_reg_order[regno] >= 0
5397 && reload_reg_free_p (regno, rld[r].opnum,
5398 rld[r].when_needed)))
5399 break;
5400 nr--;
5402 if (nr == 1)
5403 break;
5407 /* If we found something on pass 1, omit pass 2. */
5408 if (count < n_spills)
5409 break;
5412 /* We should have found a spill register by now. */
5413 if (count >= n_spills)
5414 return 0;
5416 /* I is the index in SPILL_REG_RTX of the reload register we are to
5417 allocate. Get an rtx for it and find its register number. */
5419 return set_reload_reg (i, r);
5422 /* Initialize all the tables needed to allocate reload registers.
5423 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5424 is the array we use to restore the reg_rtx field for every reload. */
5426 static void
5427 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5429 int i;
5431 for (i = 0; i < n_reloads; i++)
5432 rld[i].reg_rtx = save_reload_reg_rtx[i];
5434 memset (reload_inherited, 0, MAX_RELOADS);
5435 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5436 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5438 CLEAR_HARD_REG_SET (reload_reg_used);
5439 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5440 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5441 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5442 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5443 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5445 CLEAR_HARD_REG_SET (reg_used_in_insn);
5447 HARD_REG_SET tmp;
5448 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5449 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5450 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5451 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5452 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5453 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5456 for (i = 0; i < reload_n_operands; i++)
5458 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5459 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5460 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5461 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5462 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5463 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5466 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5468 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5470 for (i = 0; i < n_reloads; i++)
5471 /* If we have already decided to use a certain register,
5472 don't use it in another way. */
5473 if (rld[i].reg_rtx)
5474 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5475 rld[i].when_needed, rld[i].mode);
5478 /* Assign hard reg targets for the pseudo-registers we must reload
5479 into hard regs for this insn.
5480 Also output the instructions to copy them in and out of the hard regs.
5482 For machines with register classes, we are responsible for
5483 finding a reload reg in the proper class. */
5485 static void
5486 choose_reload_regs (struct insn_chain *chain)
5488 rtx insn = chain->insn;
5489 int i, j;
5490 unsigned int max_group_size = 1;
5491 enum reg_class group_class = NO_REGS;
5492 int pass, win, inheritance;
5494 rtx save_reload_reg_rtx[MAX_RELOADS];
5496 /* In order to be certain of getting the registers we need,
5497 we must sort the reloads into order of increasing register class.
5498 Then our grabbing of reload registers will parallel the process
5499 that provided the reload registers.
5501 Also note whether any of the reloads wants a consecutive group of regs.
5502 If so, record the maximum size of the group desired and what
5503 register class contains all the groups needed by this insn. */
5505 for (j = 0; j < n_reloads; j++)
5507 reload_order[j] = j;
5508 reload_spill_index[j] = -1;
5510 if (rld[j].nregs > 1)
5512 max_group_size = MAX (rld[j].nregs, max_group_size);
5513 group_class
5514 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5517 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5520 if (n_reloads > 1)
5521 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5523 /* If -O, try first with inheritance, then turning it off.
5524 If not -O, don't do inheritance.
5525 Using inheritance when not optimizing leads to paradoxes
5526 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5527 because one side of the comparison might be inherited. */
5528 win = 0;
5529 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5531 choose_reload_regs_init (chain, save_reload_reg_rtx);
5533 /* Process the reloads in order of preference just found.
5534 Beyond this point, subregs can be found in reload_reg_rtx.
5536 This used to look for an existing reloaded home for all of the
5537 reloads, and only then perform any new reloads. But that could lose
5538 if the reloads were done out of reg-class order because a later
5539 reload with a looser constraint might have an old home in a register
5540 needed by an earlier reload with a tighter constraint.
5542 To solve this, we make two passes over the reloads, in the order
5543 described above. In the first pass we try to inherit a reload
5544 from a previous insn. If there is a later reload that needs a
5545 class that is a proper subset of the class being processed, we must
5546 also allocate a spill register during the first pass.
5548 Then make a second pass over the reloads to allocate any reloads
5549 that haven't been given registers yet. */
5551 for (j = 0; j < n_reloads; j++)
5553 int r = reload_order[j];
5554 rtx search_equiv = NULL_RTX;
5556 /* Ignore reloads that got marked inoperative. */
5557 if (rld[r].out == 0 && rld[r].in == 0
5558 && ! rld[r].secondary_p)
5559 continue;
5561 /* If find_reloads chose to use reload_in or reload_out as a reload
5562 register, we don't need to chose one. Otherwise, try even if it
5563 found one since we might save an insn if we find the value lying
5564 around.
5565 Try also when reload_in is a pseudo without a hard reg. */
5566 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5567 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5568 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5569 && !MEM_P (rld[r].in)
5570 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5571 continue;
5573 #if 0 /* No longer needed for correct operation.
5574 It might give better code, or might not; worth an experiment? */
5575 /* If this is an optional reload, we can't inherit from earlier insns
5576 until we are sure that any non-optional reloads have been allocated.
5577 The following code takes advantage of the fact that optional reloads
5578 are at the end of reload_order. */
5579 if (rld[r].optional != 0)
5580 for (i = 0; i < j; i++)
5581 if ((rld[reload_order[i]].out != 0
5582 || rld[reload_order[i]].in != 0
5583 || rld[reload_order[i]].secondary_p)
5584 && ! rld[reload_order[i]].optional
5585 && rld[reload_order[i]].reg_rtx == 0)
5586 allocate_reload_reg (chain, reload_order[i], 0);
5587 #endif
5589 /* First see if this pseudo is already available as reloaded
5590 for a previous insn. We cannot try to inherit for reloads
5591 that are smaller than the maximum number of registers needed
5592 for groups unless the register we would allocate cannot be used
5593 for the groups.
5595 We could check here to see if this is a secondary reload for
5596 an object that is already in a register of the desired class.
5597 This would avoid the need for the secondary reload register.
5598 But this is complex because we can't easily determine what
5599 objects might want to be loaded via this reload. So let a
5600 register be allocated here. In `emit_reload_insns' we suppress
5601 one of the loads in the case described above. */
5603 if (inheritance)
5605 int byte = 0;
5606 int regno = -1;
5607 enum machine_mode mode = VOIDmode;
5609 if (rld[r].in == 0)
5611 else if (REG_P (rld[r].in))
5613 regno = REGNO (rld[r].in);
5614 mode = GET_MODE (rld[r].in);
5616 else if (REG_P (rld[r].in_reg))
5618 regno = REGNO (rld[r].in_reg);
5619 mode = GET_MODE (rld[r].in_reg);
5621 else if (GET_CODE (rld[r].in_reg) == SUBREG
5622 && REG_P (SUBREG_REG (rld[r].in_reg)))
5624 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5625 if (regno < FIRST_PSEUDO_REGISTER)
5626 regno = subreg_regno (rld[r].in_reg);
5627 else
5628 byte = SUBREG_BYTE (rld[r].in_reg);
5629 mode = GET_MODE (rld[r].in_reg);
5631 #ifdef AUTO_INC_DEC
5632 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
5633 && REG_P (XEXP (rld[r].in_reg, 0)))
5635 regno = REGNO (XEXP (rld[r].in_reg, 0));
5636 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5637 rld[r].out = rld[r].in;
5639 #endif
5640 #if 0
5641 /* This won't work, since REGNO can be a pseudo reg number.
5642 Also, it takes much more hair to keep track of all the things
5643 that can invalidate an inherited reload of part of a pseudoreg. */
5644 else if (GET_CODE (rld[r].in) == SUBREG
5645 && REG_P (SUBREG_REG (rld[r].in)))
5646 regno = subreg_regno (rld[r].in);
5647 #endif
5649 if (regno >= 0
5650 && reg_last_reload_reg[regno] != 0
5651 #ifdef CANNOT_CHANGE_MODE_CLASS
5652 /* Verify that the register it's in can be used in
5653 mode MODE. */
5654 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
5655 GET_MODE (reg_last_reload_reg[regno]),
5656 mode)
5657 #endif
5660 enum reg_class class = rld[r].class, last_class;
5661 rtx last_reg = reg_last_reload_reg[regno];
5662 enum machine_mode need_mode;
5664 i = REGNO (last_reg);
5665 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5666 last_class = REGNO_REG_CLASS (i);
5668 if (byte == 0)
5669 need_mode = mode;
5670 else
5671 need_mode
5672 = smallest_mode_for_size (GET_MODE_BITSIZE (mode)
5673 + byte * BITS_PER_UNIT,
5674 GET_MODE_CLASS (mode));
5676 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5677 >= GET_MODE_SIZE (need_mode))
5678 && reg_reloaded_contents[i] == regno
5679 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5680 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5681 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5682 /* Even if we can't use this register as a reload
5683 register, we might use it for reload_override_in,
5684 if copying it to the desired class is cheap
5685 enough. */
5686 || ((REGISTER_MOVE_COST (mode, last_class, class)
5687 < MEMORY_MOVE_COST (mode, class, 1))
5688 && (secondary_reload_class (1, class, mode,
5689 last_reg)
5690 == NO_REGS)
5691 #ifdef SECONDARY_MEMORY_NEEDED
5692 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5693 mode)
5694 #endif
5697 && (rld[r].nregs == max_group_size
5698 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5700 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5701 rld[r].when_needed, rld[r].in,
5702 const0_rtx, r, 1))
5704 /* If a group is needed, verify that all the subsequent
5705 registers still have their values intact. */
5706 int nr = hard_regno_nregs[i][rld[r].mode];
5707 int k;
5709 for (k = 1; k < nr; k++)
5710 if (reg_reloaded_contents[i + k] != regno
5711 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5712 break;
5714 if (k == nr)
5716 int i1;
5717 int bad_for_class;
5719 last_reg = (GET_MODE (last_reg) == mode
5720 ? last_reg : gen_rtx_REG (mode, i));
5722 bad_for_class = 0;
5723 for (k = 0; k < nr; k++)
5724 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5725 i+k);
5727 /* We found a register that contains the
5728 value we need. If this register is the
5729 same as an `earlyclobber' operand of the
5730 current insn, just mark it as a place to
5731 reload from since we can't use it as the
5732 reload register itself. */
5734 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5735 if (reg_overlap_mentioned_for_reload_p
5736 (reg_last_reload_reg[regno],
5737 reload_earlyclobbers[i1]))
5738 break;
5740 if (i1 != n_earlyclobbers
5741 || ! (free_for_value_p (i, rld[r].mode,
5742 rld[r].opnum,
5743 rld[r].when_needed, rld[r].in,
5744 rld[r].out, r, 1))
5745 /* Don't use it if we'd clobber a pseudo reg. */
5746 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5747 && rld[r].out
5748 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5749 /* Don't clobber the frame pointer. */
5750 || (i == HARD_FRAME_POINTER_REGNUM
5751 && frame_pointer_needed
5752 && rld[r].out)
5753 /* Don't really use the inherited spill reg
5754 if we need it wider than we've got it. */
5755 || (GET_MODE_SIZE (rld[r].mode)
5756 > GET_MODE_SIZE (mode))
5757 || bad_for_class
5759 /* If find_reloads chose reload_out as reload
5760 register, stay with it - that leaves the
5761 inherited register for subsequent reloads. */
5762 || (rld[r].out && rld[r].reg_rtx
5763 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5765 if (! rld[r].optional)
5767 reload_override_in[r] = last_reg;
5768 reload_inheritance_insn[r]
5769 = reg_reloaded_insn[i];
5772 else
5774 int k;
5775 /* We can use this as a reload reg. */
5776 /* Mark the register as in use for this part of
5777 the insn. */
5778 mark_reload_reg_in_use (i,
5779 rld[r].opnum,
5780 rld[r].when_needed,
5781 rld[r].mode);
5782 rld[r].reg_rtx = last_reg;
5783 reload_inherited[r] = 1;
5784 reload_inheritance_insn[r]
5785 = reg_reloaded_insn[i];
5786 reload_spill_index[r] = i;
5787 for (k = 0; k < nr; k++)
5788 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5789 i + k);
5796 /* Here's another way to see if the value is already lying around. */
5797 if (inheritance
5798 && rld[r].in != 0
5799 && ! reload_inherited[r]
5800 && rld[r].out == 0
5801 && (CONSTANT_P (rld[r].in)
5802 || GET_CODE (rld[r].in) == PLUS
5803 || REG_P (rld[r].in)
5804 || MEM_P (rld[r].in))
5805 && (rld[r].nregs == max_group_size
5806 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5807 search_equiv = rld[r].in;
5808 /* If this is an output reload from a simple move insn, look
5809 if an equivalence for the input is available. */
5810 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5812 rtx set = single_set (insn);
5814 if (set
5815 && rtx_equal_p (rld[r].out, SET_DEST (set))
5816 && CONSTANT_P (SET_SRC (set)))
5817 search_equiv = SET_SRC (set);
5820 if (search_equiv)
5822 rtx equiv
5823 = find_equiv_reg (search_equiv, insn, rld[r].class,
5824 -1, NULL, 0, rld[r].mode);
5825 int regno = 0;
5827 if (equiv != 0)
5829 if (REG_P (equiv))
5830 regno = REGNO (equiv);
5831 else
5833 /* This must be a SUBREG of a hard register.
5834 Make a new REG since this might be used in an
5835 address and not all machines support SUBREGs
5836 there. */
5837 gcc_assert (GET_CODE (equiv) == SUBREG);
5838 regno = subreg_regno (equiv);
5839 equiv = gen_rtx_REG (rld[r].mode, regno);
5840 /* If we choose EQUIV as the reload register, but the
5841 loop below decides to cancel the inheritance, we'll
5842 end up reloading EQUIV in rld[r].mode, not the mode
5843 it had originally. That isn't safe when EQUIV isn't
5844 available as a spill register since its value might
5845 still be live at this point. */
5846 for (i = regno; i < regno + (int) rld[r].nregs; i++)
5847 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
5848 equiv = 0;
5852 /* If we found a spill reg, reject it unless it is free
5853 and of the desired class. */
5854 if (equiv != 0)
5856 int regs_used = 0;
5857 int bad_for_class = 0;
5858 int max_regno = regno + rld[r].nregs;
5860 for (i = regno; i < max_regno; i++)
5862 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
5864 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5868 if ((regs_used
5869 && ! free_for_value_p (regno, rld[r].mode,
5870 rld[r].opnum, rld[r].when_needed,
5871 rld[r].in, rld[r].out, r, 1))
5872 || bad_for_class)
5873 equiv = 0;
5876 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5877 equiv = 0;
5879 /* We found a register that contains the value we need.
5880 If this register is the same as an `earlyclobber' operand
5881 of the current insn, just mark it as a place to reload from
5882 since we can't use it as the reload register itself. */
5884 if (equiv != 0)
5885 for (i = 0; i < n_earlyclobbers; i++)
5886 if (reg_overlap_mentioned_for_reload_p (equiv,
5887 reload_earlyclobbers[i]))
5889 if (! rld[r].optional)
5890 reload_override_in[r] = equiv;
5891 equiv = 0;
5892 break;
5895 /* If the equiv register we have found is explicitly clobbered
5896 in the current insn, it depends on the reload type if we
5897 can use it, use it for reload_override_in, or not at all.
5898 In particular, we then can't use EQUIV for a
5899 RELOAD_FOR_OUTPUT_ADDRESS reload. */
5901 if (equiv != 0)
5903 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
5904 switch (rld[r].when_needed)
5906 case RELOAD_FOR_OTHER_ADDRESS:
5907 case RELOAD_FOR_INPADDR_ADDRESS:
5908 case RELOAD_FOR_INPUT_ADDRESS:
5909 case RELOAD_FOR_OPADDR_ADDR:
5910 break;
5911 case RELOAD_OTHER:
5912 case RELOAD_FOR_INPUT:
5913 case RELOAD_FOR_OPERAND_ADDRESS:
5914 if (! rld[r].optional)
5915 reload_override_in[r] = equiv;
5916 /* Fall through. */
5917 default:
5918 equiv = 0;
5919 break;
5921 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
5922 switch (rld[r].when_needed)
5924 case RELOAD_FOR_OTHER_ADDRESS:
5925 case RELOAD_FOR_INPADDR_ADDRESS:
5926 case RELOAD_FOR_INPUT_ADDRESS:
5927 case RELOAD_FOR_OPADDR_ADDR:
5928 case RELOAD_FOR_OPERAND_ADDRESS:
5929 case RELOAD_FOR_INPUT:
5930 break;
5931 case RELOAD_OTHER:
5932 if (! rld[r].optional)
5933 reload_override_in[r] = equiv;
5934 /* Fall through. */
5935 default:
5936 equiv = 0;
5937 break;
5941 /* If we found an equivalent reg, say no code need be generated
5942 to load it, and use it as our reload reg. */
5943 if (equiv != 0
5944 && (regno != HARD_FRAME_POINTER_REGNUM
5945 || !frame_pointer_needed))
5947 int nr = hard_regno_nregs[regno][rld[r].mode];
5948 int k;
5949 rld[r].reg_rtx = equiv;
5950 reload_inherited[r] = 1;
5952 /* If reg_reloaded_valid is not set for this register,
5953 there might be a stale spill_reg_store lying around.
5954 We must clear it, since otherwise emit_reload_insns
5955 might delete the store. */
5956 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
5957 spill_reg_store[regno] = NULL_RTX;
5958 /* If any of the hard registers in EQUIV are spill
5959 registers, mark them as in use for this insn. */
5960 for (k = 0; k < nr; k++)
5962 i = spill_reg_order[regno + k];
5963 if (i >= 0)
5965 mark_reload_reg_in_use (regno, rld[r].opnum,
5966 rld[r].when_needed,
5967 rld[r].mode);
5968 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5969 regno + k);
5975 /* If we found a register to use already, or if this is an optional
5976 reload, we are done. */
5977 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
5978 continue;
5980 #if 0
5981 /* No longer needed for correct operation. Might or might
5982 not give better code on the average. Want to experiment? */
5984 /* See if there is a later reload that has a class different from our
5985 class that intersects our class or that requires less register
5986 than our reload. If so, we must allocate a register to this
5987 reload now, since that reload might inherit a previous reload
5988 and take the only available register in our class. Don't do this
5989 for optional reloads since they will force all previous reloads
5990 to be allocated. Also don't do this for reloads that have been
5991 turned off. */
5993 for (i = j + 1; i < n_reloads; i++)
5995 int s = reload_order[i];
5997 if ((rld[s].in == 0 && rld[s].out == 0
5998 && ! rld[s].secondary_p)
5999 || rld[s].optional)
6000 continue;
6002 if ((rld[s].class != rld[r].class
6003 && reg_classes_intersect_p (rld[r].class,
6004 rld[s].class))
6005 || rld[s].nregs < rld[r].nregs)
6006 break;
6009 if (i == n_reloads)
6010 continue;
6012 allocate_reload_reg (chain, r, j == n_reloads - 1);
6013 #endif
6016 /* Now allocate reload registers for anything non-optional that
6017 didn't get one yet. */
6018 for (j = 0; j < n_reloads; j++)
6020 int r = reload_order[j];
6022 /* Ignore reloads that got marked inoperative. */
6023 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6024 continue;
6026 /* Skip reloads that already have a register allocated or are
6027 optional. */
6028 if (rld[r].reg_rtx != 0 || rld[r].optional)
6029 continue;
6031 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6032 break;
6035 /* If that loop got all the way, we have won. */
6036 if (j == n_reloads)
6038 win = 1;
6039 break;
6042 /* Loop around and try without any inheritance. */
6045 if (! win)
6047 /* First undo everything done by the failed attempt
6048 to allocate with inheritance. */
6049 choose_reload_regs_init (chain, save_reload_reg_rtx);
6051 /* Some sanity tests to verify that the reloads found in the first
6052 pass are identical to the ones we have now. */
6053 gcc_assert (chain->n_reloads == n_reloads);
6055 for (i = 0; i < n_reloads; i++)
6057 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6058 continue;
6059 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6060 for (j = 0; j < n_spills; j++)
6061 if (spill_regs[j] == chain->rld[i].regno)
6062 if (! set_reload_reg (j, i))
6063 failed_reload (chain->insn, i);
6067 /* If we thought we could inherit a reload, because it seemed that
6068 nothing else wanted the same reload register earlier in the insn,
6069 verify that assumption, now that all reloads have been assigned.
6070 Likewise for reloads where reload_override_in has been set. */
6072 /* If doing expensive optimizations, do one preliminary pass that doesn't
6073 cancel any inheritance, but removes reloads that have been needed only
6074 for reloads that we know can be inherited. */
6075 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6077 for (j = 0; j < n_reloads; j++)
6079 int r = reload_order[j];
6080 rtx check_reg;
6081 if (reload_inherited[r] && rld[r].reg_rtx)
6082 check_reg = rld[r].reg_rtx;
6083 else if (reload_override_in[r]
6084 && (REG_P (reload_override_in[r])
6085 || GET_CODE (reload_override_in[r]) == SUBREG))
6086 check_reg = reload_override_in[r];
6087 else
6088 continue;
6089 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6090 rld[r].opnum, rld[r].when_needed, rld[r].in,
6091 (reload_inherited[r]
6092 ? rld[r].out : const0_rtx),
6093 r, 1))
6095 if (pass)
6096 continue;
6097 reload_inherited[r] = 0;
6098 reload_override_in[r] = 0;
6100 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6101 reload_override_in, then we do not need its related
6102 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6103 likewise for other reload types.
6104 We handle this by removing a reload when its only replacement
6105 is mentioned in reload_in of the reload we are going to inherit.
6106 A special case are auto_inc expressions; even if the input is
6107 inherited, we still need the address for the output. We can
6108 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6109 If we succeeded removing some reload and we are doing a preliminary
6110 pass just to remove such reloads, make another pass, since the
6111 removal of one reload might allow us to inherit another one. */
6112 else if (rld[r].in
6113 && rld[r].out != rld[r].in
6114 && remove_address_replacements (rld[r].in) && pass)
6115 pass = 2;
6119 /* Now that reload_override_in is known valid,
6120 actually override reload_in. */
6121 for (j = 0; j < n_reloads; j++)
6122 if (reload_override_in[j])
6123 rld[j].in = reload_override_in[j];
6125 /* If this reload won't be done because it has been canceled or is
6126 optional and not inherited, clear reload_reg_rtx so other
6127 routines (such as subst_reloads) don't get confused. */
6128 for (j = 0; j < n_reloads; j++)
6129 if (rld[j].reg_rtx != 0
6130 && ((rld[j].optional && ! reload_inherited[j])
6131 || (rld[j].in == 0 && rld[j].out == 0
6132 && ! rld[j].secondary_p)))
6134 int regno = true_regnum (rld[j].reg_rtx);
6136 if (spill_reg_order[regno] >= 0)
6137 clear_reload_reg_in_use (regno, rld[j].opnum,
6138 rld[j].when_needed, rld[j].mode);
6139 rld[j].reg_rtx = 0;
6140 reload_spill_index[j] = -1;
6143 /* Record which pseudos and which spill regs have output reloads. */
6144 for (j = 0; j < n_reloads; j++)
6146 int r = reload_order[j];
6148 i = reload_spill_index[r];
6150 /* I is nonneg if this reload uses a register.
6151 If rld[r].reg_rtx is 0, this is an optional reload
6152 that we opted to ignore. */
6153 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
6154 && rld[r].reg_rtx != 0)
6156 int nregno = REGNO (rld[r].out_reg);
6157 int nr = 1;
6159 if (nregno < FIRST_PSEUDO_REGISTER)
6160 nr = hard_regno_nregs[nregno][rld[r].mode];
6162 while (--nr >= 0)
6163 SET_REGNO_REG_SET (&reg_has_output_reload,
6164 nregno + nr);
6166 if (i >= 0)
6168 nr = hard_regno_nregs[i][rld[r].mode];
6169 while (--nr >= 0)
6170 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6173 gcc_assert (rld[r].when_needed == RELOAD_OTHER
6174 || rld[r].when_needed == RELOAD_FOR_OUTPUT
6175 || rld[r].when_needed == RELOAD_FOR_INSN);
6180 /* Deallocate the reload register for reload R. This is called from
6181 remove_address_replacements. */
6183 void
6184 deallocate_reload_reg (int r)
6186 int regno;
6188 if (! rld[r].reg_rtx)
6189 return;
6190 regno = true_regnum (rld[r].reg_rtx);
6191 rld[r].reg_rtx = 0;
6192 if (spill_reg_order[regno] >= 0)
6193 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6194 rld[r].mode);
6195 reload_spill_index[r] = -1;
6198 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
6199 reloads of the same item for fear that we might not have enough reload
6200 registers. However, normally they will get the same reload register
6201 and hence actually need not be loaded twice.
6203 Here we check for the most common case of this phenomenon: when we have
6204 a number of reloads for the same object, each of which were allocated
6205 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6206 reload, and is not modified in the insn itself. If we find such,
6207 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6208 This will not increase the number of spill registers needed and will
6209 prevent redundant code. */
6211 static void
6212 merge_assigned_reloads (rtx insn)
6214 int i, j;
6216 /* Scan all the reloads looking for ones that only load values and
6217 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6218 assigned and not modified by INSN. */
6220 for (i = 0; i < n_reloads; i++)
6222 int conflicting_input = 0;
6223 int max_input_address_opnum = -1;
6224 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6226 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6227 || rld[i].out != 0 || rld[i].reg_rtx == 0
6228 || reg_set_p (rld[i].reg_rtx, insn))
6229 continue;
6231 /* Look at all other reloads. Ensure that the only use of this
6232 reload_reg_rtx is in a reload that just loads the same value
6233 as we do. Note that any secondary reloads must be of the identical
6234 class since the values, modes, and result registers are the
6235 same, so we need not do anything with any secondary reloads. */
6237 for (j = 0; j < n_reloads; j++)
6239 if (i == j || rld[j].reg_rtx == 0
6240 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6241 rld[i].reg_rtx))
6242 continue;
6244 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6245 && rld[j].opnum > max_input_address_opnum)
6246 max_input_address_opnum = rld[j].opnum;
6248 /* If the reload regs aren't exactly the same (e.g, different modes)
6249 or if the values are different, we can't merge this reload.
6250 But if it is an input reload, we might still merge
6251 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6253 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6254 || rld[j].out != 0 || rld[j].in == 0
6255 || ! rtx_equal_p (rld[i].in, rld[j].in))
6257 if (rld[j].when_needed != RELOAD_FOR_INPUT
6258 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6259 || rld[i].opnum > rld[j].opnum)
6260 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6261 break;
6262 conflicting_input = 1;
6263 if (min_conflicting_input_opnum > rld[j].opnum)
6264 min_conflicting_input_opnum = rld[j].opnum;
6268 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6269 we, in fact, found any matching reloads. */
6271 if (j == n_reloads
6272 && max_input_address_opnum <= min_conflicting_input_opnum)
6274 gcc_assert (rld[i].when_needed != RELOAD_FOR_OUTPUT);
6276 for (j = 0; j < n_reloads; j++)
6277 if (i != j && rld[j].reg_rtx != 0
6278 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6279 && (! conflicting_input
6280 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6281 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6283 rld[i].when_needed = RELOAD_OTHER;
6284 rld[j].in = 0;
6285 reload_spill_index[j] = -1;
6286 transfer_replacements (i, j);
6289 /* If this is now RELOAD_OTHER, look for any reloads that load
6290 parts of this operand and set them to RELOAD_FOR_OTHER_ADDRESS
6291 if they were for inputs, RELOAD_OTHER for outputs. Note that
6292 this test is equivalent to looking for reloads for this operand
6293 number. */
6294 /* We must take special care with RELOAD_FOR_OUTPUT_ADDRESS; it may
6295 share registers with a RELOAD_FOR_INPUT, so we can not change it
6296 to RELOAD_FOR_OTHER_ADDRESS. We should never need to, since we
6297 do not modify RELOAD_FOR_OUTPUT. */
6299 if (rld[i].when_needed == RELOAD_OTHER)
6300 for (j = 0; j < n_reloads; j++)
6301 if (rld[j].in != 0
6302 && rld[j].when_needed != RELOAD_OTHER
6303 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6304 && rld[j].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
6305 && (! conflicting_input
6306 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6307 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6308 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6309 rld[i].in))
6311 int k;
6313 rld[j].when_needed
6314 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6315 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6316 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6318 /* Check to see if we accidentally converted two
6319 reloads that use the same reload register with
6320 different inputs to the same type. If so, the
6321 resulting code won't work. */
6322 if (rld[j].reg_rtx)
6323 for (k = 0; k < j; k++)
6324 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6325 || rld[k].when_needed != rld[j].when_needed
6326 || !rtx_equal_p (rld[k].reg_rtx,
6327 rld[j].reg_rtx)
6328 || rtx_equal_p (rld[k].in,
6329 rld[j].in));
6335 /* These arrays are filled by emit_reload_insns and its subroutines. */
6336 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6337 static rtx other_input_address_reload_insns = 0;
6338 static rtx other_input_reload_insns = 0;
6339 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6340 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6341 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6342 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6343 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6344 static rtx operand_reload_insns = 0;
6345 static rtx other_operand_reload_insns = 0;
6346 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6348 /* Values to be put in spill_reg_store are put here first. */
6349 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6350 static HARD_REG_SET reg_reloaded_died;
6352 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
6353 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
6354 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
6355 adjusted register, and return true. Otherwise, return false. */
6356 static bool
6357 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
6358 enum reg_class new_class,
6359 enum machine_mode new_mode)
6362 rtx reg;
6364 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
6366 unsigned regno = REGNO (reg);
6368 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
6369 continue;
6370 if (GET_MODE (reg) != new_mode)
6372 if (!HARD_REGNO_MODE_OK (regno, new_mode))
6373 continue;
6374 if (hard_regno_nregs[regno][new_mode]
6375 > hard_regno_nregs[regno][GET_MODE (reg)])
6376 continue;
6377 reg = reload_adjust_reg_for_mode (reg, new_mode);
6379 *reload_reg = reg;
6380 return true;
6382 return false;
6385 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
6386 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
6387 nonzero, if that is suitable. On success, change *RELOAD_REG to the
6388 adjusted register, and return true. Otherwise, return false. */
6389 static bool
6390 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
6391 enum insn_code icode)
6394 enum reg_class new_class = scratch_reload_class (icode);
6395 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
6397 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
6398 new_class, new_mode);
6401 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6402 has the number J. OLD contains the value to be used as input. */
6404 static void
6405 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6406 rtx old, int j)
6408 rtx insn = chain->insn;
6409 rtx reloadreg = rl->reg_rtx;
6410 rtx oldequiv_reg = 0;
6411 rtx oldequiv = 0;
6412 int special = 0;
6413 enum machine_mode mode;
6414 rtx *where;
6416 /* Determine the mode to reload in.
6417 This is very tricky because we have three to choose from.
6418 There is the mode the insn operand wants (rl->inmode).
6419 There is the mode of the reload register RELOADREG.
6420 There is the intrinsic mode of the operand, which we could find
6421 by stripping some SUBREGs.
6422 It turns out that RELOADREG's mode is irrelevant:
6423 we can change that arbitrarily.
6425 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6426 then the reload reg may not support QImode moves, so use SImode.
6427 If foo is in memory due to spilling a pseudo reg, this is safe,
6428 because the QImode value is in the least significant part of a
6429 slot big enough for a SImode. If foo is some other sort of
6430 memory reference, then it is impossible to reload this case,
6431 so previous passes had better make sure this never happens.
6433 Then consider a one-word union which has SImode and one of its
6434 members is a float, being fetched as (SUBREG:SF union:SI).
6435 We must fetch that as SFmode because we could be loading into
6436 a float-only register. In this case OLD's mode is correct.
6438 Consider an immediate integer: it has VOIDmode. Here we need
6439 to get a mode from something else.
6441 In some cases, there is a fourth mode, the operand's
6442 containing mode. If the insn specifies a containing mode for
6443 this operand, it overrides all others.
6445 I am not sure whether the algorithm here is always right,
6446 but it does the right things in those cases. */
6448 mode = GET_MODE (old);
6449 if (mode == VOIDmode)
6450 mode = rl->inmode;
6452 /* delete_output_reload is only invoked properly if old contains
6453 the original pseudo register. Since this is replaced with a
6454 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6455 find the pseudo in RELOAD_IN_REG. */
6456 if (reload_override_in[j]
6457 && REG_P (rl->in_reg))
6459 oldequiv = old;
6460 old = rl->in_reg;
6462 if (oldequiv == 0)
6463 oldequiv = old;
6464 else if (REG_P (oldequiv))
6465 oldequiv_reg = oldequiv;
6466 else if (GET_CODE (oldequiv) == SUBREG)
6467 oldequiv_reg = SUBREG_REG (oldequiv);
6469 /* If we are reloading from a register that was recently stored in
6470 with an output-reload, see if we can prove there was
6471 actually no need to store the old value in it. */
6473 if (optimize && REG_P (oldequiv)
6474 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6475 && spill_reg_store[REGNO (oldequiv)]
6476 && REG_P (old)
6477 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6478 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6479 rl->out_reg)))
6480 delete_output_reload (insn, j, REGNO (oldequiv));
6482 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6483 then load RELOADREG from OLDEQUIV. Note that we cannot use
6484 gen_lowpart_common since it can do the wrong thing when
6485 RELOADREG has a multi-word mode. Note that RELOADREG
6486 must always be a REG here. */
6488 if (GET_MODE (reloadreg) != mode)
6489 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6490 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6491 oldequiv = SUBREG_REG (oldequiv);
6492 if (GET_MODE (oldequiv) != VOIDmode
6493 && mode != GET_MODE (oldequiv))
6494 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6496 /* Switch to the right place to emit the reload insns. */
6497 switch (rl->when_needed)
6499 case RELOAD_OTHER:
6500 where = &other_input_reload_insns;
6501 break;
6502 case RELOAD_FOR_INPUT:
6503 where = &input_reload_insns[rl->opnum];
6504 break;
6505 case RELOAD_FOR_INPUT_ADDRESS:
6506 where = &input_address_reload_insns[rl->opnum];
6507 break;
6508 case RELOAD_FOR_INPADDR_ADDRESS:
6509 where = &inpaddr_address_reload_insns[rl->opnum];
6510 break;
6511 case RELOAD_FOR_OUTPUT_ADDRESS:
6512 where = &output_address_reload_insns[rl->opnum];
6513 break;
6514 case RELOAD_FOR_OUTADDR_ADDRESS:
6515 where = &outaddr_address_reload_insns[rl->opnum];
6516 break;
6517 case RELOAD_FOR_OPERAND_ADDRESS:
6518 where = &operand_reload_insns;
6519 break;
6520 case RELOAD_FOR_OPADDR_ADDR:
6521 where = &other_operand_reload_insns;
6522 break;
6523 case RELOAD_FOR_OTHER_ADDRESS:
6524 where = &other_input_address_reload_insns;
6525 break;
6526 default:
6527 gcc_unreachable ();
6530 push_to_sequence (*where);
6532 /* Auto-increment addresses must be reloaded in a special way. */
6533 if (rl->out && ! rl->out_reg)
6535 /* We are not going to bother supporting the case where a
6536 incremented register can't be copied directly from
6537 OLDEQUIV since this seems highly unlikely. */
6538 gcc_assert (rl->secondary_in_reload < 0);
6540 if (reload_inherited[j])
6541 oldequiv = reloadreg;
6543 old = XEXP (rl->in_reg, 0);
6545 if (optimize && REG_P (oldequiv)
6546 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6547 && spill_reg_store[REGNO (oldequiv)]
6548 && REG_P (old)
6549 && (dead_or_set_p (insn,
6550 spill_reg_stored_to[REGNO (oldequiv)])
6551 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6552 old)))
6553 delete_output_reload (insn, j, REGNO (oldequiv));
6555 /* Prevent normal processing of this reload. */
6556 special = 1;
6557 /* Output a special code sequence for this case. */
6558 new_spill_reg_store[REGNO (reloadreg)]
6559 = inc_for_reload (reloadreg, oldequiv, rl->out,
6560 rl->inc);
6563 /* If we are reloading a pseudo-register that was set by the previous
6564 insn, see if we can get rid of that pseudo-register entirely
6565 by redirecting the previous insn into our reload register. */
6567 else if (optimize && REG_P (old)
6568 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6569 && dead_or_set_p (insn, old)
6570 /* This is unsafe if some other reload
6571 uses the same reg first. */
6572 && ! conflicts_with_override (reloadreg)
6573 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6574 rl->when_needed, old, rl->out, j, 0))
6576 rtx temp = PREV_INSN (insn);
6577 while (temp && NOTE_P (temp))
6578 temp = PREV_INSN (temp);
6579 if (temp
6580 && NONJUMP_INSN_P (temp)
6581 && GET_CODE (PATTERN (temp)) == SET
6582 && SET_DEST (PATTERN (temp)) == old
6583 /* Make sure we can access insn_operand_constraint. */
6584 && asm_noperands (PATTERN (temp)) < 0
6585 /* This is unsafe if operand occurs more than once in current
6586 insn. Perhaps some occurrences aren't reloaded. */
6587 && count_occurrences (PATTERN (insn), old, 0) == 1)
6589 rtx old = SET_DEST (PATTERN (temp));
6590 /* Store into the reload register instead of the pseudo. */
6591 SET_DEST (PATTERN (temp)) = reloadreg;
6593 /* Verify that resulting insn is valid. */
6594 extract_insn (temp);
6595 if (constrain_operands (1))
6597 /* If the previous insn is an output reload, the source is
6598 a reload register, and its spill_reg_store entry will
6599 contain the previous destination. This is now
6600 invalid. */
6601 if (REG_P (SET_SRC (PATTERN (temp)))
6602 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6604 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6605 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6608 /* If these are the only uses of the pseudo reg,
6609 pretend for GDB it lives in the reload reg we used. */
6610 if (REG_N_DEATHS (REGNO (old)) == 1
6611 && REG_N_SETS (REGNO (old)) == 1)
6613 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6614 alter_reg (REGNO (old), -1);
6616 special = 1;
6618 else
6620 SET_DEST (PATTERN (temp)) = old;
6625 /* We can't do that, so output an insn to load RELOADREG. */
6627 /* If we have a secondary reload, pick up the secondary register
6628 and icode, if any. If OLDEQUIV and OLD are different or
6629 if this is an in-out reload, recompute whether or not we
6630 still need a secondary register and what the icode should
6631 be. If we still need a secondary register and the class or
6632 icode is different, go back to reloading from OLD if using
6633 OLDEQUIV means that we got the wrong type of register. We
6634 cannot have different class or icode due to an in-out reload
6635 because we don't make such reloads when both the input and
6636 output need secondary reload registers. */
6638 if (! special && rl->secondary_in_reload >= 0)
6640 rtx second_reload_reg = 0;
6641 rtx third_reload_reg = 0;
6642 int secondary_reload = rl->secondary_in_reload;
6643 rtx real_oldequiv = oldequiv;
6644 rtx real_old = old;
6645 rtx tmp;
6646 enum insn_code icode;
6647 enum insn_code tertiary_icode = CODE_FOR_nothing;
6649 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6650 and similarly for OLD.
6651 See comments in get_secondary_reload in reload.c. */
6652 /* If it is a pseudo that cannot be replaced with its
6653 equivalent MEM, we must fall back to reload_in, which
6654 will have all the necessary substitutions registered.
6655 Likewise for a pseudo that can't be replaced with its
6656 equivalent constant.
6658 Take extra care for subregs of such pseudos. Note that
6659 we cannot use reg_equiv_mem in this case because it is
6660 not in the right mode. */
6662 tmp = oldequiv;
6663 if (GET_CODE (tmp) == SUBREG)
6664 tmp = SUBREG_REG (tmp);
6665 if (REG_P (tmp)
6666 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6667 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6668 || reg_equiv_constant[REGNO (tmp)] != 0))
6670 if (! reg_equiv_mem[REGNO (tmp)]
6671 || num_not_at_initial_offset
6672 || GET_CODE (oldequiv) == SUBREG)
6673 real_oldequiv = rl->in;
6674 else
6675 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6678 tmp = old;
6679 if (GET_CODE (tmp) == SUBREG)
6680 tmp = SUBREG_REG (tmp);
6681 if (REG_P (tmp)
6682 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6683 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6684 || reg_equiv_constant[REGNO (tmp)] != 0))
6686 if (! reg_equiv_mem[REGNO (tmp)]
6687 || num_not_at_initial_offset
6688 || GET_CODE (old) == SUBREG)
6689 real_old = rl->in;
6690 else
6691 real_old = reg_equiv_mem[REGNO (tmp)];
6694 second_reload_reg = rld[secondary_reload].reg_rtx;
6695 if (rld[secondary_reload].secondary_in_reload >= 0)
6697 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
6699 third_reload_reg = rld[tertiary_reload].reg_rtx;
6700 tertiary_icode = rld[secondary_reload].secondary_in_icode;
6701 /* We'd have to add more code for quartary reloads. */
6702 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
6704 icode = rl->secondary_in_icode;
6706 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6707 || (rl->in != 0 && rl->out != 0))
6709 secondary_reload_info sri, sri2;
6710 enum reg_class new_class, new_t_class;
6712 sri.icode = CODE_FOR_nothing;
6713 sri.prev_sri = NULL;
6714 new_class = targetm.secondary_reload (1, real_oldequiv, rl->class,
6715 mode, &sri);
6717 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
6718 second_reload_reg = 0;
6719 else if (new_class == NO_REGS)
6721 if (reload_adjust_reg_for_icode (&second_reload_reg,
6722 third_reload_reg, sri.icode))
6723 icode = sri.icode, third_reload_reg = 0;
6724 else
6725 oldequiv = old, real_oldequiv = real_old;
6727 else if (sri.icode != CODE_FOR_nothing)
6728 /* We currently lack a way to express this in reloads. */
6729 gcc_unreachable ();
6730 else
6732 sri2.icode = CODE_FOR_nothing;
6733 sri2.prev_sri = &sri;
6734 new_t_class = targetm.secondary_reload (1, real_oldequiv,
6735 new_class, mode, &sri);
6736 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
6738 if (reload_adjust_reg_for_temp (&second_reload_reg,
6739 third_reload_reg,
6740 new_class, mode))
6741 third_reload_reg = 0, tertiary_icode = sri2.icode;
6742 else
6743 oldequiv = old, real_oldequiv = real_old;
6745 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
6747 rtx intermediate = second_reload_reg;
6749 if (reload_adjust_reg_for_temp (&intermediate, NULL,
6750 new_class, mode)
6751 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
6752 sri2.icode))
6754 second_reload_reg = intermediate;
6755 tertiary_icode = sri2.icode;
6757 else
6758 oldequiv = old, real_oldequiv = real_old;
6760 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
6762 rtx intermediate = second_reload_reg;
6764 if (reload_adjust_reg_for_temp (&intermediate, NULL,
6765 new_class, mode)
6766 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
6767 new_t_class, mode))
6769 second_reload_reg = intermediate;
6770 tertiary_icode = sri2.icode;
6772 else
6773 oldequiv = old, real_oldequiv = real_old;
6775 else
6776 /* This could be handled more intelligently too. */
6777 oldequiv = old, real_oldequiv = real_old;
6781 /* If we still need a secondary reload register, check
6782 to see if it is being used as a scratch or intermediate
6783 register and generate code appropriately. If we need
6784 a scratch register, use REAL_OLDEQUIV since the form of
6785 the insn may depend on the actual address if it is
6786 a MEM. */
6788 if (second_reload_reg)
6790 if (icode != CODE_FOR_nothing)
6792 /* We'd have to add extra code to handle this case. */
6793 gcc_assert (!third_reload_reg);
6795 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6796 second_reload_reg));
6797 special = 1;
6799 else
6801 /* See if we need a scratch register to load the
6802 intermediate register (a tertiary reload). */
6803 if (tertiary_icode != CODE_FOR_nothing)
6805 emit_insn ((GEN_FCN (tertiary_icode)
6806 (second_reload_reg, real_oldequiv,
6807 third_reload_reg)));
6809 else if (third_reload_reg)
6811 gen_reload (third_reload_reg, real_oldequiv,
6812 rl->opnum,
6813 rl->when_needed);
6814 gen_reload (second_reload_reg, third_reload_reg,
6815 rl->opnum,
6816 rl->when_needed);
6818 else
6819 gen_reload (second_reload_reg, real_oldequiv,
6820 rl->opnum,
6821 rl->when_needed);
6823 oldequiv = second_reload_reg;
6828 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6830 rtx real_oldequiv = oldequiv;
6832 if ((REG_P (oldequiv)
6833 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6834 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6835 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6836 || (GET_CODE (oldequiv) == SUBREG
6837 && REG_P (SUBREG_REG (oldequiv))
6838 && (REGNO (SUBREG_REG (oldequiv))
6839 >= FIRST_PSEUDO_REGISTER)
6840 && ((reg_equiv_memory_loc
6841 [REGNO (SUBREG_REG (oldequiv))] != 0)
6842 || (reg_equiv_constant
6843 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6844 || (CONSTANT_P (oldequiv)
6845 && (PREFERRED_RELOAD_CLASS (oldequiv,
6846 REGNO_REG_CLASS (REGNO (reloadreg)))
6847 == NO_REGS)))
6848 real_oldequiv = rl->in;
6849 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6850 rl->when_needed);
6853 if (flag_non_call_exceptions)
6854 copy_eh_notes (insn, get_insns ());
6856 /* End this sequence. */
6857 *where = get_insns ();
6858 end_sequence ();
6860 /* Update reload_override_in so that delete_address_reloads_1
6861 can see the actual register usage. */
6862 if (oldequiv_reg)
6863 reload_override_in[j] = oldequiv;
6866 /* Generate insns to for the output reload RL, which is for the insn described
6867 by CHAIN and has the number J. */
6868 static void
6869 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
6870 int j)
6872 rtx reloadreg = rl->reg_rtx;
6873 rtx insn = chain->insn;
6874 int special = 0;
6875 rtx old = rl->out;
6876 enum machine_mode mode = GET_MODE (old);
6877 rtx p;
6879 if (rl->when_needed == RELOAD_OTHER)
6880 start_sequence ();
6881 else
6882 push_to_sequence (output_reload_insns[rl->opnum]);
6884 /* Determine the mode to reload in.
6885 See comments above (for input reloading). */
6887 if (mode == VOIDmode)
6889 /* VOIDmode should never happen for an output. */
6890 if (asm_noperands (PATTERN (insn)) < 0)
6891 /* It's the compiler's fault. */
6892 fatal_insn ("VOIDmode on an output", insn);
6893 error_for_asm (insn, "output operand is constant in %<asm%>");
6894 /* Prevent crash--use something we know is valid. */
6895 mode = word_mode;
6896 old = gen_rtx_REG (mode, REGNO (reloadreg));
6899 if (GET_MODE (reloadreg) != mode)
6900 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6902 /* If we need two reload regs, set RELOADREG to the intermediate
6903 one, since it will be stored into OLD. We might need a secondary
6904 register only for an input reload, so check again here. */
6906 if (rl->secondary_out_reload >= 0)
6908 rtx real_old = old;
6909 int secondary_reload = rl->secondary_out_reload;
6910 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
6912 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
6913 && reg_equiv_mem[REGNO (old)] != 0)
6914 real_old = reg_equiv_mem[REGNO (old)];
6916 if (secondary_reload_class (0, rl->class, mode, real_old) != NO_REGS)
6918 rtx second_reloadreg = reloadreg;
6919 reloadreg = rld[secondary_reload].reg_rtx;
6921 /* See if RELOADREG is to be used as a scratch register
6922 or as an intermediate register. */
6923 if (rl->secondary_out_icode != CODE_FOR_nothing)
6925 /* We'd have to add extra code to handle this case. */
6926 gcc_assert (tertiary_reload < 0);
6928 emit_insn ((GEN_FCN (rl->secondary_out_icode)
6929 (real_old, second_reloadreg, reloadreg)));
6930 special = 1;
6932 else
6934 /* See if we need both a scratch and intermediate reload
6935 register. */
6937 enum insn_code tertiary_icode
6938 = rld[secondary_reload].secondary_out_icode;
6940 /* We'd have to add more code for quartary reloads. */
6941 gcc_assert (tertiary_reload < 0
6942 || rld[tertiary_reload].secondary_out_reload < 0);
6944 if (GET_MODE (reloadreg) != mode)
6945 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6947 if (tertiary_icode != CODE_FOR_nothing)
6949 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
6950 rtx tem;
6952 /* Copy primary reload reg to secondary reload reg.
6953 (Note that these have been swapped above, then
6954 secondary reload reg to OLD using our insn.) */
6956 /* If REAL_OLD is a paradoxical SUBREG, remove it
6957 and try to put the opposite SUBREG on
6958 RELOADREG. */
6959 if (GET_CODE (real_old) == SUBREG
6960 && (GET_MODE_SIZE (GET_MODE (real_old))
6961 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
6962 && 0 != (tem = gen_lowpart_common
6963 (GET_MODE (SUBREG_REG (real_old)),
6964 reloadreg)))
6965 real_old = SUBREG_REG (real_old), reloadreg = tem;
6967 gen_reload (reloadreg, second_reloadreg,
6968 rl->opnum, rl->when_needed);
6969 emit_insn ((GEN_FCN (tertiary_icode)
6970 (real_old, reloadreg, third_reloadreg)));
6971 special = 1;
6974 else
6976 /* Copy between the reload regs here and then to
6977 OUT later. */
6979 gen_reload (reloadreg, second_reloadreg,
6980 rl->opnum, rl->when_needed);
6981 if (tertiary_reload >= 0)
6983 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
6985 gen_reload (third_reloadreg, reloadreg,
6986 rl->opnum, rl->when_needed);
6987 reloadreg = third_reloadreg;
6994 /* Output the last reload insn. */
6995 if (! special)
6997 rtx set;
6999 /* Don't output the last reload if OLD is not the dest of
7000 INSN and is in the src and is clobbered by INSN. */
7001 if (! flag_expensive_optimizations
7002 || !REG_P (old)
7003 || !(set = single_set (insn))
7004 || rtx_equal_p (old, SET_DEST (set))
7005 || !reg_mentioned_p (old, SET_SRC (set))
7006 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7007 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7008 gen_reload (old, reloadreg, rl->opnum,
7009 rl->when_needed);
7012 /* Look at all insns we emitted, just to be safe. */
7013 for (p = get_insns (); p; p = NEXT_INSN (p))
7014 if (INSN_P (p))
7016 rtx pat = PATTERN (p);
7018 /* If this output reload doesn't come from a spill reg,
7019 clear any memory of reloaded copies of the pseudo reg.
7020 If this output reload comes from a spill reg,
7021 reg_has_output_reload will make this do nothing. */
7022 note_stores (pat, forget_old_reloads_1, NULL);
7024 if (reg_mentioned_p (rl->reg_rtx, pat))
7026 rtx set = single_set (insn);
7027 if (reload_spill_index[j] < 0
7028 && set
7029 && SET_SRC (set) == rl->reg_rtx)
7031 int src = REGNO (SET_SRC (set));
7033 reload_spill_index[j] = src;
7034 SET_HARD_REG_BIT (reg_is_output_reload, src);
7035 if (find_regno_note (insn, REG_DEAD, src))
7036 SET_HARD_REG_BIT (reg_reloaded_died, src);
7038 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
7040 int s = rl->secondary_out_reload;
7041 set = single_set (p);
7042 /* If this reload copies only to the secondary reload
7043 register, the secondary reload does the actual
7044 store. */
7045 if (s >= 0 && set == NULL_RTX)
7046 /* We can't tell what function the secondary reload
7047 has and where the actual store to the pseudo is
7048 made; leave new_spill_reg_store alone. */
7050 else if (s >= 0
7051 && SET_SRC (set) == rl->reg_rtx
7052 && SET_DEST (set) == rld[s].reg_rtx)
7054 /* Usually the next instruction will be the
7055 secondary reload insn; if we can confirm
7056 that it is, setting new_spill_reg_store to
7057 that insn will allow an extra optimization. */
7058 rtx s_reg = rld[s].reg_rtx;
7059 rtx next = NEXT_INSN (p);
7060 rld[s].out = rl->out;
7061 rld[s].out_reg = rl->out_reg;
7062 set = single_set (next);
7063 if (set && SET_SRC (set) == s_reg
7064 && ! new_spill_reg_store[REGNO (s_reg)])
7066 SET_HARD_REG_BIT (reg_is_output_reload,
7067 REGNO (s_reg));
7068 new_spill_reg_store[REGNO (s_reg)] = next;
7071 else
7072 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
7077 if (rl->when_needed == RELOAD_OTHER)
7079 emit_insn (other_output_reload_insns[rl->opnum]);
7080 other_output_reload_insns[rl->opnum] = get_insns ();
7082 else
7083 output_reload_insns[rl->opnum] = get_insns ();
7085 if (flag_non_call_exceptions)
7086 copy_eh_notes (insn, get_insns ());
7088 end_sequence ();
7091 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7092 and has the number J. */
7093 static void
7094 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7096 rtx insn = chain->insn;
7097 rtx old = (rl->in && MEM_P (rl->in)
7098 ? rl->in_reg : rl->in);
7100 if (old != 0
7101 /* AUTO_INC reloads need to be handled even if inherited. We got an
7102 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7103 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7104 && ! rtx_equal_p (rl->reg_rtx, old)
7105 && rl->reg_rtx != 0)
7106 emit_input_reload_insns (chain, rld + j, old, j);
7108 /* When inheriting a wider reload, we have a MEM in rl->in,
7109 e.g. inheriting a SImode output reload for
7110 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7111 if (optimize && reload_inherited[j] && rl->in
7112 && MEM_P (rl->in)
7113 && MEM_P (rl->in_reg)
7114 && reload_spill_index[j] >= 0
7115 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7116 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7118 /* If we are reloading a register that was recently stored in with an
7119 output-reload, see if we can prove there was
7120 actually no need to store the old value in it. */
7122 if (optimize
7123 /* Only attempt this for input reloads; for RELOAD_OTHER we miss
7124 that there may be multiple uses of the previous output reload.
7125 Restricting to RELOAD_FOR_INPUT is mostly paranoia. */
7126 && rl->when_needed == RELOAD_FOR_INPUT
7127 && (reload_inherited[j] || reload_override_in[j])
7128 && rl->reg_rtx
7129 && REG_P (rl->reg_rtx)
7130 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
7131 #if 0
7132 /* There doesn't seem to be any reason to restrict this to pseudos
7133 and doing so loses in the case where we are copying from a
7134 register of the wrong class. */
7135 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
7136 >= FIRST_PSEUDO_REGISTER)
7137 #endif
7138 /* The insn might have already some references to stackslots
7139 replaced by MEMs, while reload_out_reg still names the
7140 original pseudo. */
7141 && (dead_or_set_p (insn,
7142 spill_reg_stored_to[REGNO (rl->reg_rtx)])
7143 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
7144 rl->out_reg)))
7145 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
7148 /* Do output reloading for reload RL, which is for the insn described by
7149 CHAIN and has the number J.
7150 ??? At some point we need to support handling output reloads of
7151 JUMP_INSNs or insns that set cc0. */
7152 static void
7153 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7155 rtx note, old;
7156 rtx insn = chain->insn;
7157 /* If this is an output reload that stores something that is
7158 not loaded in this same reload, see if we can eliminate a previous
7159 store. */
7160 rtx pseudo = rl->out_reg;
7162 if (pseudo
7163 && optimize
7164 && REG_P (pseudo)
7165 && ! rtx_equal_p (rl->in_reg, pseudo)
7166 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7167 && reg_last_reload_reg[REGNO (pseudo)])
7169 int pseudo_no = REGNO (pseudo);
7170 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7172 /* We don't need to test full validity of last_regno for
7173 inherit here; we only want to know if the store actually
7174 matches the pseudo. */
7175 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7176 && reg_reloaded_contents[last_regno] == pseudo_no
7177 && spill_reg_store[last_regno]
7178 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7179 delete_output_reload (insn, j, last_regno);
7182 old = rl->out_reg;
7183 if (old == 0
7184 || rl->reg_rtx == old
7185 || rl->reg_rtx == 0)
7186 return;
7188 /* An output operand that dies right away does need a reload,
7189 but need not be copied from it. Show the new location in the
7190 REG_UNUSED note. */
7191 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7192 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7194 XEXP (note, 0) = rl->reg_rtx;
7195 return;
7197 /* Likewise for a SUBREG of an operand that dies. */
7198 else if (GET_CODE (old) == SUBREG
7199 && REG_P (SUBREG_REG (old))
7200 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7201 SUBREG_REG (old))))
7203 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7204 rl->reg_rtx);
7205 return;
7207 else if (GET_CODE (old) == SCRATCH)
7208 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7209 but we don't want to make an output reload. */
7210 return;
7212 /* If is a JUMP_INSN, we can't support output reloads yet. */
7213 gcc_assert (NONJUMP_INSN_P (insn));
7215 emit_output_reload_insns (chain, rld + j, j);
7218 /* Reload number R reloads from or to a group of hard registers starting at
7219 register REGNO. Return true if it can be treated for inheritance purposes
7220 like a group of reloads, each one reloading a single hard register.
7221 The caller has already checked that the spill register and REGNO use
7222 the same number of registers to store the reload value. */
7224 static bool
7225 inherit_piecemeal_p (int r ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED)
7227 #ifdef CANNOT_CHANGE_MODE_CLASS
7228 return (!REG_CANNOT_CHANGE_MODE_P (reload_spill_index[r],
7229 GET_MODE (rld[r].reg_rtx),
7230 reg_raw_mode[reload_spill_index[r]])
7231 && !REG_CANNOT_CHANGE_MODE_P (regno,
7232 GET_MODE (rld[r].reg_rtx),
7233 reg_raw_mode[regno]));
7234 #else
7235 return true;
7236 #endif
7239 /* Output insns to reload values in and out of the chosen reload regs. */
7241 static void
7242 emit_reload_insns (struct insn_chain *chain)
7244 rtx insn = chain->insn;
7246 int j;
7248 CLEAR_HARD_REG_SET (reg_reloaded_died);
7250 for (j = 0; j < reload_n_operands; j++)
7251 input_reload_insns[j] = input_address_reload_insns[j]
7252 = inpaddr_address_reload_insns[j]
7253 = output_reload_insns[j] = output_address_reload_insns[j]
7254 = outaddr_address_reload_insns[j]
7255 = other_output_reload_insns[j] = 0;
7256 other_input_address_reload_insns = 0;
7257 other_input_reload_insns = 0;
7258 operand_reload_insns = 0;
7259 other_operand_reload_insns = 0;
7261 /* Dump reloads into the dump file. */
7262 if (dump_file)
7264 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7265 debug_reload_to_stream (dump_file);
7268 /* Now output the instructions to copy the data into and out of the
7269 reload registers. Do these in the order that the reloads were reported,
7270 since reloads of base and index registers precede reloads of operands
7271 and the operands may need the base and index registers reloaded. */
7273 for (j = 0; j < n_reloads; j++)
7275 if (rld[j].reg_rtx
7276 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7277 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
7279 do_input_reload (chain, rld + j, j);
7280 do_output_reload (chain, rld + j, j);
7283 /* Now write all the insns we made for reloads in the order expected by
7284 the allocation functions. Prior to the insn being reloaded, we write
7285 the following reloads:
7287 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7289 RELOAD_OTHER reloads.
7291 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7292 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7293 RELOAD_FOR_INPUT reload for the operand.
7295 RELOAD_FOR_OPADDR_ADDRS reloads.
7297 RELOAD_FOR_OPERAND_ADDRESS reloads.
7299 After the insn being reloaded, we write the following:
7301 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7302 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7303 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7304 reloads for the operand. The RELOAD_OTHER output reloads are
7305 output in descending order by reload number. */
7307 emit_insn_before (other_input_address_reload_insns, insn);
7308 emit_insn_before (other_input_reload_insns, insn);
7310 for (j = 0; j < reload_n_operands; j++)
7312 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7313 emit_insn_before (input_address_reload_insns[j], insn);
7314 emit_insn_before (input_reload_insns[j], insn);
7317 emit_insn_before (other_operand_reload_insns, insn);
7318 emit_insn_before (operand_reload_insns, insn);
7320 for (j = 0; j < reload_n_operands; j++)
7322 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7323 x = emit_insn_after (output_address_reload_insns[j], x);
7324 x = emit_insn_after (output_reload_insns[j], x);
7325 emit_insn_after (other_output_reload_insns[j], x);
7328 /* For all the spill regs newly reloaded in this instruction,
7329 record what they were reloaded from, so subsequent instructions
7330 can inherit the reloads.
7332 Update spill_reg_store for the reloads of this insn.
7333 Copy the elements that were updated in the loop above. */
7335 for (j = 0; j < n_reloads; j++)
7337 int r = reload_order[j];
7338 int i = reload_spill_index[r];
7340 /* If this is a non-inherited input reload from a pseudo, we must
7341 clear any memory of a previous store to the same pseudo. Only do
7342 something if there will not be an output reload for the pseudo
7343 being reloaded. */
7344 if (rld[r].in_reg != 0
7345 && ! (reload_inherited[r] || reload_override_in[r]))
7347 rtx reg = rld[r].in_reg;
7349 if (GET_CODE (reg) == SUBREG)
7350 reg = SUBREG_REG (reg);
7352 if (REG_P (reg)
7353 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7354 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
7356 int nregno = REGNO (reg);
7358 if (reg_last_reload_reg[nregno])
7360 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7362 if (reg_reloaded_contents[last_regno] == nregno)
7363 spill_reg_store[last_regno] = 0;
7368 /* I is nonneg if this reload used a register.
7369 If rld[r].reg_rtx is 0, this is an optional reload
7370 that we opted to ignore. */
7372 if (i >= 0 && rld[r].reg_rtx != 0)
7374 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7375 int k;
7376 int part_reaches_end = 0;
7377 int all_reaches_end = 1;
7379 /* For a multi register reload, we need to check if all or part
7380 of the value lives to the end. */
7381 for (k = 0; k < nr; k++)
7383 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7384 rld[r].when_needed))
7385 part_reaches_end = 1;
7386 else
7387 all_reaches_end = 0;
7390 /* Ignore reloads that don't reach the end of the insn in
7391 entirety. */
7392 if (all_reaches_end)
7394 /* First, clear out memory of what used to be in this spill reg.
7395 If consecutive registers are used, clear them all. */
7397 for (k = 0; k < nr; k++)
7399 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7400 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7403 /* Maybe the spill reg contains a copy of reload_out. */
7404 if (rld[r].out != 0
7405 && (REG_P (rld[r].out)
7406 #ifdef AUTO_INC_DEC
7407 || ! rld[r].out_reg
7408 #endif
7409 || REG_P (rld[r].out_reg)))
7411 rtx out = (REG_P (rld[r].out)
7412 ? rld[r].out
7413 : rld[r].out_reg
7414 ? rld[r].out_reg
7415 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7416 int nregno = REGNO (out);
7417 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7418 : hard_regno_nregs[nregno]
7419 [GET_MODE (rld[r].reg_rtx)]);
7420 bool piecemeal;
7422 spill_reg_store[i] = new_spill_reg_store[i];
7423 spill_reg_stored_to[i] = out;
7424 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7426 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7427 && nr == nnr
7428 && inherit_piecemeal_p (r, nregno));
7430 /* If NREGNO is a hard register, it may occupy more than
7431 one register. If it does, say what is in the
7432 rest of the registers assuming that both registers
7433 agree on how many words the object takes. If not,
7434 invalidate the subsequent registers. */
7436 if (nregno < FIRST_PSEUDO_REGISTER)
7437 for (k = 1; k < nnr; k++)
7438 reg_last_reload_reg[nregno + k]
7439 = (piecemeal
7440 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7441 : 0);
7443 /* Now do the inverse operation. */
7444 for (k = 0; k < nr; k++)
7446 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7447 reg_reloaded_contents[i + k]
7448 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7449 ? nregno
7450 : nregno + k);
7451 reg_reloaded_insn[i + k] = insn;
7452 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7453 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (out)))
7454 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7458 /* Maybe the spill reg contains a copy of reload_in. Only do
7459 something if there will not be an output reload for
7460 the register being reloaded. */
7461 else if (rld[r].out_reg == 0
7462 && rld[r].in != 0
7463 && ((REG_P (rld[r].in)
7464 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7465 && !REGNO_REG_SET_P (&reg_has_output_reload,
7466 REGNO (rld[r].in)))
7467 || (REG_P (rld[r].in_reg)
7468 && !REGNO_REG_SET_P (&reg_has_output_reload,
7469 REGNO (rld[r].in_reg))))
7470 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7472 int nregno;
7473 int nnr;
7474 rtx in;
7475 bool piecemeal;
7477 if (REG_P (rld[r].in)
7478 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7479 in = rld[r].in;
7480 else if (REG_P (rld[r].in_reg))
7481 in = rld[r].in_reg;
7482 else
7483 in = XEXP (rld[r].in_reg, 0);
7484 nregno = REGNO (in);
7486 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7487 : hard_regno_nregs[nregno]
7488 [GET_MODE (rld[r].reg_rtx)]);
7490 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7492 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7493 && nr == nnr
7494 && inherit_piecemeal_p (r, nregno));
7496 if (nregno < FIRST_PSEUDO_REGISTER)
7497 for (k = 1; k < nnr; k++)
7498 reg_last_reload_reg[nregno + k]
7499 = (piecemeal
7500 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7501 : 0);
7503 /* Unless we inherited this reload, show we haven't
7504 recently done a store.
7505 Previous stores of inherited auto_inc expressions
7506 also have to be discarded. */
7507 if (! reload_inherited[r]
7508 || (rld[r].out && ! rld[r].out_reg))
7509 spill_reg_store[i] = 0;
7511 for (k = 0; k < nr; k++)
7513 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7514 reg_reloaded_contents[i + k]
7515 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7516 ? nregno
7517 : nregno + k);
7518 reg_reloaded_insn[i + k] = insn;
7519 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7520 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (in)))
7521 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7526 /* However, if part of the reload reaches the end, then we must
7527 invalidate the old info for the part that survives to the end. */
7528 else if (part_reaches_end)
7530 for (k = 0; k < nr; k++)
7531 if (reload_reg_reaches_end_p (i + k,
7532 rld[r].opnum,
7533 rld[r].when_needed))
7534 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7538 /* The following if-statement was #if 0'd in 1.34 (or before...).
7539 It's reenabled in 1.35 because supposedly nothing else
7540 deals with this problem. */
7542 /* If a register gets output-reloaded from a non-spill register,
7543 that invalidates any previous reloaded copy of it.
7544 But forget_old_reloads_1 won't get to see it, because
7545 it thinks only about the original insn. So invalidate it here.
7546 Also do the same thing for RELOAD_OTHER constraints where the
7547 output is discarded. */
7548 if (i < 0
7549 && ((rld[r].out != 0
7550 && (REG_P (rld[r].out)
7551 || (MEM_P (rld[r].out)
7552 && REG_P (rld[r].out_reg))))
7553 || (rld[r].out == 0 && rld[r].out_reg
7554 && REG_P (rld[r].out_reg))))
7556 rtx out = ((rld[r].out && REG_P (rld[r].out))
7557 ? rld[r].out : rld[r].out_reg);
7558 int nregno = REGNO (out);
7560 /* REG_RTX is now set or clobbered by the main instruction.
7561 As the comment above explains, forget_old_reloads_1 only
7562 sees the original instruction, and there is no guarantee
7563 that the original instruction also clobbered REG_RTX.
7564 For example, if find_reloads sees that the input side of
7565 a matched operand pair dies in this instruction, it may
7566 use the input register as the reload register.
7568 Calling forget_old_reloads_1 is a waste of effort if
7569 REG_RTX is also the output register.
7571 If we know that REG_RTX holds the value of a pseudo
7572 register, the code after the call will record that fact. */
7573 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
7574 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
7576 if (nregno >= FIRST_PSEUDO_REGISTER)
7578 rtx src_reg, store_insn = NULL_RTX;
7580 reg_last_reload_reg[nregno] = 0;
7582 /* If we can find a hard register that is stored, record
7583 the storing insn so that we may delete this insn with
7584 delete_output_reload. */
7585 src_reg = rld[r].reg_rtx;
7587 /* If this is an optional reload, try to find the source reg
7588 from an input reload. */
7589 if (! src_reg)
7591 rtx set = single_set (insn);
7592 if (set && SET_DEST (set) == rld[r].out)
7594 int k;
7596 src_reg = SET_SRC (set);
7597 store_insn = insn;
7598 for (k = 0; k < n_reloads; k++)
7600 if (rld[k].in == src_reg)
7602 src_reg = rld[k].reg_rtx;
7603 break;
7608 else
7609 store_insn = new_spill_reg_store[REGNO (src_reg)];
7610 if (src_reg && REG_P (src_reg)
7611 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7613 int src_regno = REGNO (src_reg);
7614 int nr = hard_regno_nregs[src_regno][rld[r].mode];
7615 /* The place where to find a death note varies with
7616 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7617 necessarily checked exactly in the code that moves
7618 notes, so just check both locations. */
7619 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7620 if (! note && store_insn)
7621 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7622 while (nr-- > 0)
7624 spill_reg_store[src_regno + nr] = store_insn;
7625 spill_reg_stored_to[src_regno + nr] = out;
7626 reg_reloaded_contents[src_regno + nr] = nregno;
7627 reg_reloaded_insn[src_regno + nr] = store_insn;
7628 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7629 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7630 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + nr,
7631 GET_MODE (src_reg)))
7632 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7633 src_regno + nr);
7634 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7635 if (note)
7636 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7637 else
7638 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7640 reg_last_reload_reg[nregno] = src_reg;
7641 /* We have to set reg_has_output_reload here, or else
7642 forget_old_reloads_1 will clear reg_last_reload_reg
7643 right away. */
7644 SET_REGNO_REG_SET (&reg_has_output_reload,
7645 nregno);
7648 else
7650 int num_regs = hard_regno_nregs[nregno][GET_MODE (out)];
7652 while (num_regs-- > 0)
7653 reg_last_reload_reg[nregno + num_regs] = 0;
7657 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7660 /* Go through the motions to emit INSN and test if it is strictly valid.
7661 Return the emitted insn if valid, else return NULL. */
7663 static rtx
7664 emit_insn_if_valid_for_reload (rtx insn)
7666 rtx last = get_last_insn ();
7667 int code;
7669 insn = emit_insn (insn);
7670 code = recog_memoized (insn);
7672 if (code >= 0)
7674 extract_insn (insn);
7675 /* We want constrain operands to treat this insn strictly in its
7676 validity determination, i.e., the way it would after reload has
7677 completed. */
7678 if (constrain_operands (1))
7679 return insn;
7682 delete_insns_since (last);
7683 return NULL;
7686 /* Emit code to perform a reload from IN (which may be a reload register) to
7687 OUT (which may also be a reload register). IN or OUT is from operand
7688 OPNUM with reload type TYPE.
7690 Returns first insn emitted. */
7692 static rtx
7693 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7695 rtx last = get_last_insn ();
7696 rtx tem;
7698 /* If IN is a paradoxical SUBREG, remove it and try to put the
7699 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7700 if (GET_CODE (in) == SUBREG
7701 && (GET_MODE_SIZE (GET_MODE (in))
7702 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7703 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7704 in = SUBREG_REG (in), out = tem;
7705 else if (GET_CODE (out) == SUBREG
7706 && (GET_MODE_SIZE (GET_MODE (out))
7707 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7708 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7709 out = SUBREG_REG (out), in = tem;
7711 /* How to do this reload can get quite tricky. Normally, we are being
7712 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7713 register that didn't get a hard register. In that case we can just
7714 call emit_move_insn.
7716 We can also be asked to reload a PLUS that adds a register or a MEM to
7717 another register, constant or MEM. This can occur during frame pointer
7718 elimination and while reloading addresses. This case is handled by
7719 trying to emit a single insn to perform the add. If it is not valid,
7720 we use a two insn sequence.
7722 Or we can be asked to reload an unary operand that was a fragment of
7723 an addressing mode, into a register. If it isn't recognized as-is,
7724 we try making the unop operand and the reload-register the same:
7725 (set reg:X (unop:X expr:Y))
7726 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
7728 Finally, we could be called to handle an 'o' constraint by putting
7729 an address into a register. In that case, we first try to do this
7730 with a named pattern of "reload_load_address". If no such pattern
7731 exists, we just emit a SET insn and hope for the best (it will normally
7732 be valid on machines that use 'o').
7734 This entire process is made complex because reload will never
7735 process the insns we generate here and so we must ensure that
7736 they will fit their constraints and also by the fact that parts of
7737 IN might be being reloaded separately and replaced with spill registers.
7738 Because of this, we are, in some sense, just guessing the right approach
7739 here. The one listed above seems to work.
7741 ??? At some point, this whole thing needs to be rethought. */
7743 if (GET_CODE (in) == PLUS
7744 && (REG_P (XEXP (in, 0))
7745 || GET_CODE (XEXP (in, 0)) == SUBREG
7746 || MEM_P (XEXP (in, 0)))
7747 && (REG_P (XEXP (in, 1))
7748 || GET_CODE (XEXP (in, 1)) == SUBREG
7749 || CONSTANT_P (XEXP (in, 1))
7750 || MEM_P (XEXP (in, 1))))
7752 /* We need to compute the sum of a register or a MEM and another
7753 register, constant, or MEM, and put it into the reload
7754 register. The best possible way of doing this is if the machine
7755 has a three-operand ADD insn that accepts the required operands.
7757 The simplest approach is to try to generate such an insn and see if it
7758 is recognized and matches its constraints. If so, it can be used.
7760 It might be better not to actually emit the insn unless it is valid,
7761 but we need to pass the insn as an operand to `recog' and
7762 `extract_insn' and it is simpler to emit and then delete the insn if
7763 not valid than to dummy things up. */
7765 rtx op0, op1, tem, insn;
7766 int code;
7768 op0 = find_replacement (&XEXP (in, 0));
7769 op1 = find_replacement (&XEXP (in, 1));
7771 /* Since constraint checking is strict, commutativity won't be
7772 checked, so we need to do that here to avoid spurious failure
7773 if the add instruction is two-address and the second operand
7774 of the add is the same as the reload reg, which is frequently
7775 the case. If the insn would be A = B + A, rearrange it so
7776 it will be A = A + B as constrain_operands expects. */
7778 if (REG_P (XEXP (in, 1))
7779 && REGNO (out) == REGNO (XEXP (in, 1)))
7780 tem = op0, op0 = op1, op1 = tem;
7782 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7783 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7785 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
7786 if (insn)
7787 return insn;
7789 /* If that failed, we must use a conservative two-insn sequence.
7791 Use a move to copy one operand into the reload register. Prefer
7792 to reload a constant, MEM or pseudo since the move patterns can
7793 handle an arbitrary operand. If OP1 is not a constant, MEM or
7794 pseudo and OP1 is not a valid operand for an add instruction, then
7795 reload OP1.
7797 After reloading one of the operands into the reload register, add
7798 the reload register to the output register.
7800 If there is another way to do this for a specific machine, a
7801 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7802 we emit below. */
7804 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7806 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
7807 || (REG_P (op1)
7808 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7809 || (code != CODE_FOR_nothing
7810 && ! ((*insn_data[code].operand[2].predicate)
7811 (op1, insn_data[code].operand[2].mode))))
7812 tem = op0, op0 = op1, op1 = tem;
7814 gen_reload (out, op0, opnum, type);
7816 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7817 This fixes a problem on the 32K where the stack pointer cannot
7818 be used as an operand of an add insn. */
7820 if (rtx_equal_p (op0, op1))
7821 op1 = out;
7823 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
7824 if (insn)
7826 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7827 set_unique_reg_note (insn, REG_EQUIV, in);
7828 return insn;
7831 /* If that failed, copy the address register to the reload register.
7832 Then add the constant to the reload register. */
7834 gen_reload (out, op1, opnum, type);
7835 insn = emit_insn (gen_add2_insn (out, op0));
7836 set_unique_reg_note (insn, REG_EQUIV, in);
7839 #ifdef SECONDARY_MEMORY_NEEDED
7840 /* If we need a memory location to do the move, do it that way. */
7841 else if ((REG_P (in) || GET_CODE (in) == SUBREG)
7842 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7843 && (REG_P (out) || GET_CODE (out) == SUBREG)
7844 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7845 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7846 REGNO_REG_CLASS (reg_or_subregno (out)),
7847 GET_MODE (out)))
7849 /* Get the memory to use and rewrite both registers to its mode. */
7850 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7852 if (GET_MODE (loc) != GET_MODE (out))
7853 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7855 if (GET_MODE (loc) != GET_MODE (in))
7856 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7858 gen_reload (loc, in, opnum, type);
7859 gen_reload (out, loc, opnum, type);
7861 #endif
7862 else if (REG_P (out) && UNARY_P (in))
7864 rtx insn;
7865 rtx op1;
7866 rtx out_moded;
7867 rtx set;
7869 op1 = find_replacement (&XEXP (in, 0));
7870 if (op1 != XEXP (in, 0))
7871 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
7873 /* First, try a plain SET. */
7874 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
7875 if (set)
7876 return set;
7878 /* If that failed, move the inner operand to the reload
7879 register, and try the same unop with the inner expression
7880 replaced with the reload register. */
7882 if (GET_MODE (op1) != GET_MODE (out))
7883 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
7884 else
7885 out_moded = out;
7887 gen_reload (out_moded, op1, opnum, type);
7889 insn
7890 = gen_rtx_SET (VOIDmode, out,
7891 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
7892 out_moded));
7893 insn = emit_insn_if_valid_for_reload (insn);
7894 if (insn)
7896 set_unique_reg_note (insn, REG_EQUIV, in);
7897 return insn;
7900 fatal_insn ("Failure trying to reload:", set);
7902 /* If IN is a simple operand, use gen_move_insn. */
7903 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
7905 tem = emit_insn (gen_move_insn (out, in));
7906 /* IN may contain a LABEL_REF, if so add a REG_LABEL note. */
7907 mark_jump_label (in, tem, 0);
7910 #ifdef HAVE_reload_load_address
7911 else if (HAVE_reload_load_address)
7912 emit_insn (gen_reload_load_address (out, in));
7913 #endif
7915 /* Otherwise, just write (set OUT IN) and hope for the best. */
7916 else
7917 emit_insn (gen_rtx_SET (VOIDmode, out, in));
7919 /* Return the first insn emitted.
7920 We can not just return get_last_insn, because there may have
7921 been multiple instructions emitted. Also note that gen_move_insn may
7922 emit more than one insn itself, so we can not assume that there is one
7923 insn emitted per emit_insn_before call. */
7925 return last ? NEXT_INSN (last) : get_insns ();
7928 /* Delete a previously made output-reload whose result we now believe
7929 is not needed. First we double-check.
7931 INSN is the insn now being processed.
7932 LAST_RELOAD_REG is the hard register number for which we want to delete
7933 the last output reload.
7934 J is the reload-number that originally used REG. The caller has made
7935 certain that reload J doesn't use REG any longer for input. */
7937 static void
7938 delete_output_reload (rtx insn, int j, int last_reload_reg)
7940 rtx output_reload_insn = spill_reg_store[last_reload_reg];
7941 rtx reg = spill_reg_stored_to[last_reload_reg];
7942 int k;
7943 int n_occurrences;
7944 int n_inherited = 0;
7945 rtx i1;
7946 rtx substed;
7948 /* It is possible that this reload has been only used to set another reload
7949 we eliminated earlier and thus deleted this instruction too. */
7950 if (INSN_DELETED_P (output_reload_insn))
7951 return;
7953 /* Get the raw pseudo-register referred to. */
7955 while (GET_CODE (reg) == SUBREG)
7956 reg = SUBREG_REG (reg);
7957 substed = reg_equiv_memory_loc[REGNO (reg)];
7959 /* This is unsafe if the operand occurs more often in the current
7960 insn than it is inherited. */
7961 for (k = n_reloads - 1; k >= 0; k--)
7963 rtx reg2 = rld[k].in;
7964 if (! reg2)
7965 continue;
7966 if (MEM_P (reg2) || reload_override_in[k])
7967 reg2 = rld[k].in_reg;
7968 #ifdef AUTO_INC_DEC
7969 if (rld[k].out && ! rld[k].out_reg)
7970 reg2 = XEXP (rld[k].in_reg, 0);
7971 #endif
7972 while (GET_CODE (reg2) == SUBREG)
7973 reg2 = SUBREG_REG (reg2);
7974 if (rtx_equal_p (reg2, reg))
7976 if (reload_inherited[k] || reload_override_in[k] || k == j)
7978 n_inherited++;
7979 reg2 = rld[k].out_reg;
7980 if (! reg2)
7981 continue;
7982 while (GET_CODE (reg2) == SUBREG)
7983 reg2 = XEXP (reg2, 0);
7984 if (rtx_equal_p (reg2, reg))
7985 n_inherited++;
7987 else
7988 return;
7991 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
7992 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
7993 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
7994 reg, 0);
7995 if (substed)
7996 n_occurrences += count_occurrences (PATTERN (insn),
7997 eliminate_regs (substed, 0,
7998 NULL_RTX), 0);
7999 for (i1 = reg_equiv_alt_mem_list [REGNO (reg)]; i1; i1 = XEXP (i1, 1))
8001 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8002 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8004 if (n_occurrences > n_inherited)
8005 return;
8007 /* If the pseudo-reg we are reloading is no longer referenced
8008 anywhere between the store into it and here,
8009 and we're within the same basic block, then the value can only
8010 pass through the reload reg and end up here.
8011 Otherwise, give up--return. */
8012 for (i1 = NEXT_INSN (output_reload_insn);
8013 i1 != insn; i1 = NEXT_INSN (i1))
8015 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8016 return;
8017 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8018 && reg_mentioned_p (reg, PATTERN (i1)))
8020 /* If this is USE in front of INSN, we only have to check that
8021 there are no more references than accounted for by inheritance. */
8022 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8024 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8025 i1 = NEXT_INSN (i1);
8027 if (n_occurrences <= n_inherited && i1 == insn)
8028 break;
8029 return;
8033 /* We will be deleting the insn. Remove the spill reg information. */
8034 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8036 spill_reg_store[last_reload_reg + k] = 0;
8037 spill_reg_stored_to[last_reload_reg + k] = 0;
8040 /* The caller has already checked that REG dies or is set in INSN.
8041 It has also checked that we are optimizing, and thus some
8042 inaccuracies in the debugging information are acceptable.
8043 So we could just delete output_reload_insn. But in some cases
8044 we can improve the debugging information without sacrificing
8045 optimization - maybe even improving the code: See if the pseudo
8046 reg has been completely replaced with reload regs. If so, delete
8047 the store insn and forget we had a stack slot for the pseudo. */
8048 if (rld[j].out != rld[j].in
8049 && REG_N_DEATHS (REGNO (reg)) == 1
8050 && REG_N_SETS (REGNO (reg)) == 1
8051 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
8052 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8054 rtx i2;
8056 /* We know that it was used only between here and the beginning of
8057 the current basic block. (We also know that the last use before
8058 INSN was the output reload we are thinking of deleting, but never
8059 mind that.) Search that range; see if any ref remains. */
8060 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8062 rtx set = single_set (i2);
8064 /* Uses which just store in the pseudo don't count,
8065 since if they are the only uses, they are dead. */
8066 if (set != 0 && SET_DEST (set) == reg)
8067 continue;
8068 if (LABEL_P (i2)
8069 || JUMP_P (i2))
8070 break;
8071 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8072 && reg_mentioned_p (reg, PATTERN (i2)))
8074 /* Some other ref remains; just delete the output reload we
8075 know to be dead. */
8076 delete_address_reloads (output_reload_insn, insn);
8077 delete_insn (output_reload_insn);
8078 return;
8082 /* Delete the now-dead stores into this pseudo. Note that this
8083 loop also takes care of deleting output_reload_insn. */
8084 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8086 rtx set = single_set (i2);
8088 if (set != 0 && SET_DEST (set) == reg)
8090 delete_address_reloads (i2, insn);
8091 delete_insn (i2);
8093 if (LABEL_P (i2)
8094 || JUMP_P (i2))
8095 break;
8098 /* For the debugging info, say the pseudo lives in this reload reg. */
8099 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8100 alter_reg (REGNO (reg), -1);
8102 else
8104 delete_address_reloads (output_reload_insn, insn);
8105 delete_insn (output_reload_insn);
8109 /* We are going to delete DEAD_INSN. Recursively delete loads of
8110 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8111 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8112 static void
8113 delete_address_reloads (rtx dead_insn, rtx current_insn)
8115 rtx set = single_set (dead_insn);
8116 rtx set2, dst, prev, next;
8117 if (set)
8119 rtx dst = SET_DEST (set);
8120 if (MEM_P (dst))
8121 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8123 /* If we deleted the store from a reloaded post_{in,de}c expression,
8124 we can delete the matching adds. */
8125 prev = PREV_INSN (dead_insn);
8126 next = NEXT_INSN (dead_insn);
8127 if (! prev || ! next)
8128 return;
8129 set = single_set (next);
8130 set2 = single_set (prev);
8131 if (! set || ! set2
8132 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8133 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8134 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8135 return;
8136 dst = SET_DEST (set);
8137 if (! rtx_equal_p (dst, SET_DEST (set2))
8138 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8139 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8140 || (INTVAL (XEXP (SET_SRC (set), 1))
8141 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8142 return;
8143 delete_related_insns (prev);
8144 delete_related_insns (next);
8147 /* Subfunction of delete_address_reloads: process registers found in X. */
8148 static void
8149 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8151 rtx prev, set, dst, i2;
8152 int i, j;
8153 enum rtx_code code = GET_CODE (x);
8155 if (code != REG)
8157 const char *fmt = GET_RTX_FORMAT (code);
8158 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8160 if (fmt[i] == 'e')
8161 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8162 else if (fmt[i] == 'E')
8164 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8165 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8166 current_insn);
8169 return;
8172 if (spill_reg_order[REGNO (x)] < 0)
8173 return;
8175 /* Scan backwards for the insn that sets x. This might be a way back due
8176 to inheritance. */
8177 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8179 code = GET_CODE (prev);
8180 if (code == CODE_LABEL || code == JUMP_INSN)
8181 return;
8182 if (!INSN_P (prev))
8183 continue;
8184 if (reg_set_p (x, PATTERN (prev)))
8185 break;
8186 if (reg_referenced_p (x, PATTERN (prev)))
8187 return;
8189 if (! prev || INSN_UID (prev) < reload_first_uid)
8190 return;
8191 /* Check that PREV only sets the reload register. */
8192 set = single_set (prev);
8193 if (! set)
8194 return;
8195 dst = SET_DEST (set);
8196 if (!REG_P (dst)
8197 || ! rtx_equal_p (dst, x))
8198 return;
8199 if (! reg_set_p (dst, PATTERN (dead_insn)))
8201 /* Check if DST was used in a later insn -
8202 it might have been inherited. */
8203 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8205 if (LABEL_P (i2))
8206 break;
8207 if (! INSN_P (i2))
8208 continue;
8209 if (reg_referenced_p (dst, PATTERN (i2)))
8211 /* If there is a reference to the register in the current insn,
8212 it might be loaded in a non-inherited reload. If no other
8213 reload uses it, that means the register is set before
8214 referenced. */
8215 if (i2 == current_insn)
8217 for (j = n_reloads - 1; j >= 0; j--)
8218 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8219 || reload_override_in[j] == dst)
8220 return;
8221 for (j = n_reloads - 1; j >= 0; j--)
8222 if (rld[j].in && rld[j].reg_rtx == dst)
8223 break;
8224 if (j >= 0)
8225 break;
8227 return;
8229 if (JUMP_P (i2))
8230 break;
8231 /* If DST is still live at CURRENT_INSN, check if it is used for
8232 any reload. Note that even if CURRENT_INSN sets DST, we still
8233 have to check the reloads. */
8234 if (i2 == current_insn)
8236 for (j = n_reloads - 1; j >= 0; j--)
8237 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8238 || reload_override_in[j] == dst)
8239 return;
8240 /* ??? We can't finish the loop here, because dst might be
8241 allocated to a pseudo in this block if no reload in this
8242 block needs any of the classes containing DST - see
8243 spill_hard_reg. There is no easy way to tell this, so we
8244 have to scan till the end of the basic block. */
8246 if (reg_set_p (dst, PATTERN (i2)))
8247 break;
8250 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8251 reg_reloaded_contents[REGNO (dst)] = -1;
8252 delete_insn (prev);
8255 /* Output reload-insns to reload VALUE into RELOADREG.
8256 VALUE is an autoincrement or autodecrement RTX whose operand
8257 is a register or memory location;
8258 so reloading involves incrementing that location.
8259 IN is either identical to VALUE, or some cheaper place to reload from.
8261 INC_AMOUNT is the number to increment or decrement by (always positive).
8262 This cannot be deduced from VALUE.
8264 Return the instruction that stores into RELOADREG. */
8266 static rtx
8267 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
8269 /* REG or MEM to be copied and incremented. */
8270 rtx incloc = find_replacement (&XEXP (value, 0));
8271 /* Nonzero if increment after copying. */
8272 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
8273 || GET_CODE (value) == POST_MODIFY);
8274 rtx last;
8275 rtx inc;
8276 rtx add_insn;
8277 int code;
8278 rtx store;
8279 rtx real_in = in == value ? incloc : in;
8281 /* No hard register is equivalent to this register after
8282 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
8283 we could inc/dec that register as well (maybe even using it for
8284 the source), but I'm not sure it's worth worrying about. */
8285 if (REG_P (incloc))
8286 reg_last_reload_reg[REGNO (incloc)] = 0;
8288 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
8290 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
8291 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
8293 else
8295 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8296 inc_amount = -inc_amount;
8298 inc = GEN_INT (inc_amount);
8301 /* If this is post-increment, first copy the location to the reload reg. */
8302 if (post && real_in != reloadreg)
8303 emit_insn (gen_move_insn (reloadreg, real_in));
8305 if (in == value)
8307 /* See if we can directly increment INCLOC. Use a method similar to
8308 that in gen_reload. */
8310 last = get_last_insn ();
8311 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8312 gen_rtx_PLUS (GET_MODE (incloc),
8313 incloc, inc)));
8315 code = recog_memoized (add_insn);
8316 if (code >= 0)
8318 extract_insn (add_insn);
8319 if (constrain_operands (1))
8321 /* If this is a pre-increment and we have incremented the value
8322 where it lives, copy the incremented value to RELOADREG to
8323 be used as an address. */
8325 if (! post)
8326 emit_insn (gen_move_insn (reloadreg, incloc));
8328 return add_insn;
8331 delete_insns_since (last);
8334 /* If couldn't do the increment directly, must increment in RELOADREG.
8335 The way we do this depends on whether this is pre- or post-increment.
8336 For pre-increment, copy INCLOC to the reload register, increment it
8337 there, then save back. */
8339 if (! post)
8341 if (in != reloadreg)
8342 emit_insn (gen_move_insn (reloadreg, real_in));
8343 emit_insn (gen_add2_insn (reloadreg, inc));
8344 store = emit_insn (gen_move_insn (incloc, reloadreg));
8346 else
8348 /* Postincrement.
8349 Because this might be a jump insn or a compare, and because RELOADREG
8350 may not be available after the insn in an input reload, we must do
8351 the incrementation before the insn being reloaded for.
8353 We have already copied IN to RELOADREG. Increment the copy in
8354 RELOADREG, save that back, then decrement RELOADREG so it has
8355 the original value. */
8357 emit_insn (gen_add2_insn (reloadreg, inc));
8358 store = emit_insn (gen_move_insn (incloc, reloadreg));
8359 if (GET_CODE (inc) == CONST_INT)
8360 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
8361 else
8362 emit_insn (gen_sub2_insn (reloadreg, inc));
8365 return store;
8368 #ifdef AUTO_INC_DEC
8369 static void
8370 add_auto_inc_notes (rtx insn, rtx x)
8372 enum rtx_code code = GET_CODE (x);
8373 const char *fmt;
8374 int i, j;
8376 if (code == MEM && auto_inc_p (XEXP (x, 0)))
8378 REG_NOTES (insn)
8379 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
8380 return;
8383 /* Scan all the operand sub-expressions. */
8384 fmt = GET_RTX_FORMAT (code);
8385 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8387 if (fmt[i] == 'e')
8388 add_auto_inc_notes (insn, XEXP (x, i));
8389 else if (fmt[i] == 'E')
8390 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8391 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8394 #endif
8396 /* Copy EH notes from an insn to its reloads. */
8397 static void
8398 copy_eh_notes (rtx insn, rtx x)
8400 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8401 if (eh_note)
8403 for (; x != 0; x = NEXT_INSN (x))
8405 if (may_trap_p (PATTERN (x)))
8406 REG_NOTES (x)
8407 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8408 REG_NOTES (x));
8413 /* This is used by reload pass, that does emit some instructions after
8414 abnormal calls moving basic block end, but in fact it wants to emit
8415 them on the edge. Looks for abnormal call edges, find backward the
8416 proper call and fix the damage.
8418 Similar handle instructions throwing exceptions internally. */
8419 void
8420 fixup_abnormal_edges (void)
8422 bool inserted = false;
8423 basic_block bb;
8425 FOR_EACH_BB (bb)
8427 edge e;
8428 edge_iterator ei;
8430 /* Look for cases we are interested in - calls or instructions causing
8431 exceptions. */
8432 FOR_EACH_EDGE (e, ei, bb->succs)
8434 if (e->flags & EDGE_ABNORMAL_CALL)
8435 break;
8436 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8437 == (EDGE_ABNORMAL | EDGE_EH))
8438 break;
8440 if (e && !CALL_P (BB_END (bb))
8441 && !can_throw_internal (BB_END (bb)))
8443 rtx insn;
8445 /* Get past the new insns generated. Allow notes, as the insns
8446 may be already deleted. */
8447 insn = BB_END (bb);
8448 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8449 && !can_throw_internal (insn)
8450 && insn != BB_HEAD (bb))
8451 insn = PREV_INSN (insn);
8453 if (CALL_P (insn) || can_throw_internal (insn))
8455 rtx stop, next;
8457 stop = NEXT_INSN (BB_END (bb));
8458 BB_END (bb) = insn;
8459 insn = NEXT_INSN (insn);
8461 FOR_EACH_EDGE (e, ei, bb->succs)
8462 if (e->flags & EDGE_FALLTHRU)
8463 break;
8465 while (insn && insn != stop)
8467 next = NEXT_INSN (insn);
8468 if (INSN_P (insn))
8470 delete_insn (insn);
8472 /* Sometimes there's still the return value USE.
8473 If it's placed after a trapping call (i.e. that
8474 call is the last insn anyway), we have no fallthru
8475 edge. Simply delete this use and don't try to insert
8476 on the non-existent edge. */
8477 if (GET_CODE (PATTERN (insn)) != USE)
8479 /* We're not deleting it, we're moving it. */
8480 INSN_DELETED_P (insn) = 0;
8481 PREV_INSN (insn) = NULL_RTX;
8482 NEXT_INSN (insn) = NULL_RTX;
8484 insert_insn_on_edge (insn, e);
8485 inserted = true;
8488 insn = next;
8492 /* It may be that we don't find any such trapping insn. In this
8493 case we discovered quite late that the insn that had been
8494 marked as can_throw_internal in fact couldn't trap at all.
8495 So we should in fact delete the EH edges out of the block. */
8496 else
8497 purge_dead_edges (bb);
8501 /* We've possibly turned single trapping insn into multiple ones. */
8502 if (flag_non_call_exceptions)
8504 sbitmap blocks;
8505 blocks = sbitmap_alloc (last_basic_block);
8506 sbitmap_ones (blocks);
8507 find_many_sub_basic_blocks (blocks);
8510 if (inserted)
8511 commit_edge_insertions ();
8513 #ifdef ENABLE_CHECKING
8514 /* Verify that we didn't turn one trapping insn into many, and that
8515 we found and corrected all of the problems wrt fixups on the
8516 fallthru edge. */
8517 verify_flow_info ();
8518 #endif