* builtins.c (expand_builtin_setjmp_setup): Update comment.
[official-gcc.git] / gcc / reload1.c
blobdd79b0bcbf248fc856ab106ecd9c84f3266e628d
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);
625 /* Determine if the current function has an exception receiver block
626 that reaches the exit block via non-exceptional edges */
628 static bool
629 has_nonexceptional_receiver (void)
631 edge e;
632 edge_iterator ei;
633 basic_block *tos, *worklist, bb;
635 /* If we're not optimizing, then just err on the safe side. */
636 if (!optimize)
637 return true;
639 /* First determine which blocks can reach exit via normal paths. */
640 tos = worklist = xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
642 FOR_EACH_BB (bb)
643 bb->flags &= ~BB_REACHABLE;
645 /* Place the exit block on our worklist. */
646 EXIT_BLOCK_PTR->flags |= BB_REACHABLE;
647 *tos++ = EXIT_BLOCK_PTR;
649 /* Iterate: find everything reachable from what we've already seen. */
650 while (tos != worklist)
652 bb = *--tos;
654 FOR_EACH_EDGE (e, ei, bb->preds)
655 if (!(e->flags & EDGE_ABNORMAL))
657 basic_block src = e->src;
659 if (!(src->flags & BB_REACHABLE))
661 src->flags |= BB_REACHABLE;
662 *tos++ = src;
666 free (worklist);
668 /* Now see if there's a reachable block with an exceptional incoming
669 edge. */
670 FOR_EACH_BB (bb)
671 if (bb->flags & BB_REACHABLE)
672 FOR_EACH_EDGE (e, ei, bb->preds)
673 if (e->flags & EDGE_ABNORMAL)
674 return true;
676 /* No exceptional block reached exit unexceptionally. */
677 return false;
681 /* Global variables used by reload and its subroutines. */
683 /* Set during calculate_needs if an insn needs register elimination. */
684 static int something_needs_elimination;
685 /* Set during calculate_needs if an insn needs an operand changed. */
686 static int something_needs_operands_changed;
688 /* Nonzero means we couldn't get enough spill regs. */
689 static int failure;
691 /* Main entry point for the reload pass.
693 FIRST is the first insn of the function being compiled.
695 GLOBAL nonzero means we were called from global_alloc
696 and should attempt to reallocate any pseudoregs that we
697 displace from hard regs we will use for reloads.
698 If GLOBAL is zero, we do not have enough information to do that,
699 so any pseudo reg that is spilled must go to the stack.
701 Return value is nonzero if reload failed
702 and we must not do any more for this function. */
705 reload (rtx first, int global)
707 int i;
708 rtx insn;
709 struct elim_table *ep;
710 basic_block bb;
712 /* Make sure even insns with volatile mem refs are recognizable. */
713 init_recog ();
715 failure = 0;
717 reload_firstobj = obstack_alloc (&reload_obstack, 0);
719 /* Make sure that the last insn in the chain
720 is not something that needs reloading. */
721 emit_note (NOTE_INSN_DELETED);
723 /* Enable find_equiv_reg to distinguish insns made by reload. */
724 reload_first_uid = get_max_uid ();
726 #ifdef SECONDARY_MEMORY_NEEDED
727 /* Initialize the secondary memory table. */
728 clear_secondary_mem ();
729 #endif
731 /* We don't have a stack slot for any spill reg yet. */
732 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
733 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
735 /* Initialize the save area information for caller-save, in case some
736 are needed. */
737 init_save_areas ();
739 /* Compute which hard registers are now in use
740 as homes for pseudo registers.
741 This is done here rather than (eg) in global_alloc
742 because this point is reached even if not optimizing. */
743 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
744 mark_home_live (i);
746 /* A function that has a nonlocal label that can reach the exit
747 block via non-exceptional paths must save all call-saved
748 registers. */
749 if (current_function_calls_unwind_init
750 || (current_function_has_nonlocal_label
751 && has_nonexceptional_receiver ()))
752 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
753 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
754 regs_ever_live[i] = 1;
756 /* Find all the pseudo registers that didn't get hard regs
757 but do have known equivalent constants or memory slots.
758 These include parameters (known equivalent to parameter slots)
759 and cse'd or loop-moved constant memory addresses.
761 Record constant equivalents in reg_equiv_constant
762 so they will be substituted by find_reloads.
763 Record memory equivalents in reg_mem_equiv so they can
764 be substituted eventually by altering the REG-rtx's. */
766 reg_equiv_constant = XCNEWVEC (rtx, max_regno);
767 reg_equiv_invariant = XCNEWVEC (rtx, max_regno);
768 reg_equiv_mem = XCNEWVEC (rtx, max_regno);
769 reg_equiv_alt_mem_list = XCNEWVEC (rtx, max_regno);
770 reg_equiv_address = XCNEWVEC (rtx, max_regno);
771 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
772 reg_old_renumber = XCNEWVEC (short, max_regno);
773 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
774 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
775 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
777 CLEAR_HARD_REG_SET (bad_spill_regs_global);
779 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
780 to. Also find all paradoxical subregs and find largest such for
781 each pseudo. */
783 num_eliminable_invariants = 0;
784 for (insn = first; insn; insn = NEXT_INSN (insn))
786 rtx set = single_set (insn);
788 /* We may introduce USEs that we want to remove at the end, so
789 we'll mark them with QImode. Make sure there are no
790 previously-marked insns left by say regmove. */
791 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
792 && GET_MODE (insn) != VOIDmode)
793 PUT_MODE (insn, VOIDmode);
795 if (INSN_P (insn))
796 scan_paradoxical_subregs (PATTERN (insn));
798 if (set != 0 && REG_P (SET_DEST (set)))
800 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
801 rtx x;
803 if (! note)
804 continue;
806 i = REGNO (SET_DEST (set));
807 x = XEXP (note, 0);
809 if (i <= LAST_VIRTUAL_REGISTER)
810 continue;
812 if (! function_invariant_p (x)
813 || ! flag_pic
814 /* A function invariant is often CONSTANT_P but may
815 include a register. We promise to only pass
816 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
817 || (CONSTANT_P (x)
818 && LEGITIMATE_PIC_OPERAND_P (x)))
820 /* It can happen that a REG_EQUIV note contains a MEM
821 that is not a legitimate memory operand. As later
822 stages of reload assume that all addresses found
823 in the reg_equiv_* arrays were originally legitimate,
824 we ignore such REG_EQUIV notes. */
825 if (memory_operand (x, VOIDmode))
827 /* Always unshare the equivalence, so we can
828 substitute into this insn without touching the
829 equivalence. */
830 reg_equiv_memory_loc[i] = copy_rtx (x);
832 else if (function_invariant_p (x))
834 if (GET_CODE (x) == PLUS)
836 /* This is PLUS of frame pointer and a constant,
837 and might be shared. Unshare it. */
838 reg_equiv_invariant[i] = copy_rtx (x);
839 num_eliminable_invariants++;
841 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
843 reg_equiv_invariant[i] = x;
844 num_eliminable_invariants++;
846 else if (LEGITIMATE_CONSTANT_P (x))
847 reg_equiv_constant[i] = x;
848 else
850 reg_equiv_memory_loc[i]
851 = force_const_mem (GET_MODE (SET_DEST (set)), x);
852 if (! reg_equiv_memory_loc[i])
853 reg_equiv_init[i] = NULL_RTX;
856 else
858 reg_equiv_init[i] = NULL_RTX;
859 continue;
862 else
863 reg_equiv_init[i] = NULL_RTX;
867 if (dump_file)
868 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
869 if (reg_equiv_init[i])
871 fprintf (dump_file, "init_insns for %u: ", i);
872 print_inline_rtx (dump_file, reg_equiv_init[i], 20);
873 fprintf (dump_file, "\n");
876 init_elim_table ();
878 first_label_num = get_first_label_num ();
879 num_labels = max_label_num () - first_label_num;
881 /* Allocate the tables used to store offset information at labels. */
882 /* We used to use alloca here, but the size of what it would try to
883 allocate would occasionally cause it to exceed the stack limit and
884 cause a core dump. */
885 offsets_known_at = XNEWVEC (char, num_labels);
886 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
888 /* Alter each pseudo-reg rtx to contain its hard reg number.
889 Assign stack slots to the pseudos that lack hard regs or equivalents.
890 Do not touch virtual registers. */
892 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
893 alter_reg (i, -1);
895 /* If we have some registers we think can be eliminated, scan all insns to
896 see if there is an insn that sets one of these registers to something
897 other than itself plus a constant. If so, the register cannot be
898 eliminated. Doing this scan here eliminates an extra pass through the
899 main reload loop in the most common case where register elimination
900 cannot be done. */
901 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
902 if (INSN_P (insn))
903 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
905 maybe_fix_stack_asms ();
907 insns_need_reload = 0;
908 something_needs_elimination = 0;
910 /* Initialize to -1, which means take the first spill register. */
911 last_spill_reg = -1;
913 /* Spill any hard regs that we know we can't eliminate. */
914 CLEAR_HARD_REG_SET (used_spill_regs);
915 /* There can be multiple ways to eliminate a register;
916 they should be listed adjacently.
917 Elimination for any register fails only if all possible ways fail. */
918 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
920 int from = ep->from;
921 int can_eliminate = 0;
924 can_eliminate |= ep->can_eliminate;
925 ep++;
927 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
928 if (! can_eliminate)
929 spill_hard_reg (from, 1);
932 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
933 if (frame_pointer_needed)
934 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
935 #endif
936 finish_spills (global);
938 /* From now on, we may need to generate moves differently. We may also
939 allow modifications of insns which cause them to not be recognized.
940 Any such modifications will be cleaned up during reload itself. */
941 reload_in_progress = 1;
943 /* This loop scans the entire function each go-round
944 and repeats until one repetition spills no additional hard regs. */
945 for (;;)
947 int something_changed;
948 int did_spill;
949 HOST_WIDE_INT starting_frame_size;
951 starting_frame_size = get_frame_size ();
953 set_initial_elim_offsets ();
954 set_initial_label_offsets ();
956 /* For each pseudo register that has an equivalent location defined,
957 try to eliminate any eliminable registers (such as the frame pointer)
958 assuming initial offsets for the replacement register, which
959 is the normal case.
961 If the resulting location is directly addressable, substitute
962 the MEM we just got directly for the old REG.
964 If it is not addressable but is a constant or the sum of a hard reg
965 and constant, it is probably not addressable because the constant is
966 out of range, in that case record the address; we will generate
967 hairy code to compute the address in a register each time it is
968 needed. Similarly if it is a hard register, but one that is not
969 valid as an address register.
971 If the location is not addressable, but does not have one of the
972 above forms, assign a stack slot. We have to do this to avoid the
973 potential of producing lots of reloads if, e.g., a location involves
974 a pseudo that didn't get a hard register and has an equivalent memory
975 location that also involves a pseudo that didn't get a hard register.
977 Perhaps at some point we will improve reload_when_needed handling
978 so this problem goes away. But that's very hairy. */
980 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
981 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
983 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
985 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
986 XEXP (x, 0)))
987 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
988 else if (CONSTANT_P (XEXP (x, 0))
989 || (REG_P (XEXP (x, 0))
990 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
991 || (GET_CODE (XEXP (x, 0)) == PLUS
992 && REG_P (XEXP (XEXP (x, 0), 0))
993 && (REGNO (XEXP (XEXP (x, 0), 0))
994 < FIRST_PSEUDO_REGISTER)
995 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
996 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
997 else
999 /* Make a new stack slot. Then indicate that something
1000 changed so we go back and recompute offsets for
1001 eliminable registers because the allocation of memory
1002 below might change some offset. reg_equiv_{mem,address}
1003 will be set up for this pseudo on the next pass around
1004 the loop. */
1005 reg_equiv_memory_loc[i] = 0;
1006 reg_equiv_init[i] = 0;
1007 alter_reg (i, -1);
1011 if (caller_save_needed)
1012 setup_save_areas ();
1014 /* If we allocated another stack slot, redo elimination bookkeeping. */
1015 if (starting_frame_size != get_frame_size ())
1016 continue;
1017 if (starting_frame_size && cfun->stack_alignment_needed)
1019 /* If we have a stack frame, we must align it now. The
1020 stack size may be a part of the offset computation for
1021 register elimination. So if this changes the stack size,
1022 then repeat the elimination bookkeeping. We don't
1023 realign when there is no stack, as that will cause a
1024 stack frame when none is needed should
1025 STARTING_FRAME_OFFSET not be already aligned to
1026 STACK_BOUNDARY. */
1027 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
1028 if (starting_frame_size != get_frame_size ())
1029 continue;
1032 if (caller_save_needed)
1034 save_call_clobbered_regs ();
1035 /* That might have allocated new insn_chain structures. */
1036 reload_firstobj = obstack_alloc (&reload_obstack, 0);
1039 calculate_needs_all_insns (global);
1041 CLEAR_REG_SET (&spilled_pseudos);
1042 did_spill = 0;
1044 something_changed = 0;
1046 /* If we allocated any new memory locations, make another pass
1047 since it might have changed elimination offsets. */
1048 if (starting_frame_size != get_frame_size ())
1049 something_changed = 1;
1051 /* Even if the frame size remained the same, we might still have
1052 changed elimination offsets, e.g. if find_reloads called
1053 force_const_mem requiring the back end to allocate a constant
1054 pool base register that needs to be saved on the stack. */
1055 else if (!verify_initial_elim_offsets ())
1056 something_changed = 1;
1059 HARD_REG_SET to_spill;
1060 CLEAR_HARD_REG_SET (to_spill);
1061 update_eliminables (&to_spill);
1062 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
1064 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1065 if (TEST_HARD_REG_BIT (to_spill, i))
1067 spill_hard_reg (i, 1);
1068 did_spill = 1;
1070 /* Regardless of the state of spills, if we previously had
1071 a register that we thought we could eliminate, but now can
1072 not eliminate, we must run another pass.
1074 Consider pseudos which have an entry in reg_equiv_* which
1075 reference an eliminable register. We must make another pass
1076 to update reg_equiv_* so that we do not substitute in the
1077 old value from when we thought the elimination could be
1078 performed. */
1079 something_changed = 1;
1083 select_reload_regs ();
1084 if (failure)
1085 goto failed;
1087 if (insns_need_reload != 0 || did_spill)
1088 something_changed |= finish_spills (global);
1090 if (! something_changed)
1091 break;
1093 if (caller_save_needed)
1094 delete_caller_save_insns ();
1096 obstack_free (&reload_obstack, reload_firstobj);
1099 /* If global-alloc was run, notify it of any register eliminations we have
1100 done. */
1101 if (global)
1102 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1103 if (ep->can_eliminate)
1104 mark_elimination (ep->from, ep->to);
1106 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1107 If that insn didn't set the register (i.e., it copied the register to
1108 memory), just delete that insn instead of the equivalencing insn plus
1109 anything now dead. If we call delete_dead_insn on that insn, we may
1110 delete the insn that actually sets the register if the register dies
1111 there and that is incorrect. */
1113 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1115 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1117 rtx list;
1118 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1120 rtx equiv_insn = XEXP (list, 0);
1122 /* If we already deleted the insn or if it may trap, we can't
1123 delete it. The latter case shouldn't happen, but can
1124 if an insn has a variable address, gets a REG_EH_REGION
1125 note added to it, and then gets converted into a load
1126 from a constant address. */
1127 if (NOTE_P (equiv_insn)
1128 || can_throw_internal (equiv_insn))
1130 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1131 delete_dead_insn (equiv_insn);
1132 else
1133 SET_INSN_DELETED (equiv_insn);
1138 /* Use the reload registers where necessary
1139 by generating move instructions to move the must-be-register
1140 values into or out of the reload registers. */
1142 if (insns_need_reload != 0 || something_needs_elimination
1143 || something_needs_operands_changed)
1145 HOST_WIDE_INT old_frame_size = get_frame_size ();
1147 reload_as_needed (global);
1149 gcc_assert (old_frame_size == get_frame_size ());
1151 gcc_assert (verify_initial_elim_offsets ());
1154 /* If we were able to eliminate the frame pointer, show that it is no
1155 longer live at the start of any basic block. If it ls live by
1156 virtue of being in a pseudo, that pseudo will be marked live
1157 and hence the frame pointer will be known to be live via that
1158 pseudo. */
1160 if (! frame_pointer_needed)
1161 FOR_EACH_BB (bb)
1162 CLEAR_REGNO_REG_SET (bb->il.rtl->global_live_at_start,
1163 HARD_FRAME_POINTER_REGNUM);
1165 /* Come here (with failure set nonzero) if we can't get enough spill
1166 regs. */
1167 failed:
1169 CLEAR_REG_SET (&spilled_pseudos);
1170 reload_in_progress = 0;
1172 /* Now eliminate all pseudo regs by modifying them into
1173 their equivalent memory references.
1174 The REG-rtx's for the pseudos are modified in place,
1175 so all insns that used to refer to them now refer to memory.
1177 For a reg that has a reg_equiv_address, all those insns
1178 were changed by reloading so that no insns refer to it any longer;
1179 but the DECL_RTL of a variable decl may refer to it,
1180 and if so this causes the debugging info to mention the variable. */
1182 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1184 rtx addr = 0;
1186 if (reg_equiv_mem[i])
1187 addr = XEXP (reg_equiv_mem[i], 0);
1189 if (reg_equiv_address[i])
1190 addr = reg_equiv_address[i];
1192 if (addr)
1194 if (reg_renumber[i] < 0)
1196 rtx reg = regno_reg_rtx[i];
1198 REG_USERVAR_P (reg) = 0;
1199 PUT_CODE (reg, MEM);
1200 XEXP (reg, 0) = addr;
1201 if (reg_equiv_memory_loc[i])
1202 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1203 else
1205 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1206 MEM_ATTRS (reg) = 0;
1208 MEM_NOTRAP_P (reg) = 1;
1210 else if (reg_equiv_mem[i])
1211 XEXP (reg_equiv_mem[i], 0) = addr;
1215 /* We must set reload_completed now since the cleanup_subreg_operands call
1216 below will re-recognize each insn and reload may have generated insns
1217 which are only valid during and after reload. */
1218 reload_completed = 1;
1220 /* Make a pass over all the insns and delete all USEs which we inserted
1221 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1222 notes. Delete all CLOBBER insns, except those that refer to the return
1223 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1224 from misarranging variable-array code, and simplify (subreg (reg))
1225 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1226 are no longer useful or accurate. Strip and regenerate REG_INC notes
1227 that may have been moved around. */
1229 for (insn = first; insn; insn = NEXT_INSN (insn))
1230 if (INSN_P (insn))
1232 rtx *pnote;
1234 if (CALL_P (insn))
1235 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1236 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1238 if ((GET_CODE (PATTERN (insn)) == USE
1239 /* We mark with QImode USEs introduced by reload itself. */
1240 && (GET_MODE (insn) == QImode
1241 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1242 || (GET_CODE (PATTERN (insn)) == CLOBBER
1243 && (!MEM_P (XEXP (PATTERN (insn), 0))
1244 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1245 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1246 && XEXP (XEXP (PATTERN (insn), 0), 0)
1247 != stack_pointer_rtx))
1248 && (!REG_P (XEXP (PATTERN (insn), 0))
1249 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1251 delete_insn (insn);
1252 continue;
1255 /* Some CLOBBERs may survive until here and still reference unassigned
1256 pseudos with const equivalent, which may in turn cause ICE in later
1257 passes if the reference remains in place. */
1258 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1259 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1260 VOIDmode, PATTERN (insn));
1262 /* Discard obvious no-ops, even without -O. This optimization
1263 is fast and doesn't interfere with debugging. */
1264 if (NONJUMP_INSN_P (insn)
1265 && GET_CODE (PATTERN (insn)) == SET
1266 && REG_P (SET_SRC (PATTERN (insn)))
1267 && REG_P (SET_DEST (PATTERN (insn)))
1268 && (REGNO (SET_SRC (PATTERN (insn)))
1269 == REGNO (SET_DEST (PATTERN (insn)))))
1271 delete_insn (insn);
1272 continue;
1275 pnote = &REG_NOTES (insn);
1276 while (*pnote != 0)
1278 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1279 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1280 || REG_NOTE_KIND (*pnote) == REG_INC
1281 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1282 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1283 *pnote = XEXP (*pnote, 1);
1284 else
1285 pnote = &XEXP (*pnote, 1);
1288 #ifdef AUTO_INC_DEC
1289 add_auto_inc_notes (insn, PATTERN (insn));
1290 #endif
1292 /* Simplify (subreg (reg)) if it appears as an operand. */
1293 cleanup_subreg_operands (insn);
1295 /* Clean up invalid ASMs so that they don't confuse later passes.
1296 See PR 21299. */
1297 if (asm_noperands (PATTERN (insn)) >= 0)
1299 extract_insn (insn);
1300 if (!constrain_operands (1))
1302 error_for_asm (insn,
1303 "%<asm%> operand has impossible constraints");
1304 delete_insn (insn);
1305 continue;
1310 /* If we are doing stack checking, give a warning if this function's
1311 frame size is larger than we expect. */
1312 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1314 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1315 static int verbose_warned = 0;
1317 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1318 if (regs_ever_live[i] && ! fixed_regs[i] && call_used_regs[i])
1319 size += UNITS_PER_WORD;
1321 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1323 warning (0, "frame size too large for reliable stack checking");
1324 if (! verbose_warned)
1326 warning (0, "try reducing the number of local variables");
1327 verbose_warned = 1;
1332 /* Indicate that we no longer have known memory locations or constants. */
1333 if (reg_equiv_constant)
1334 free (reg_equiv_constant);
1335 if (reg_equiv_invariant)
1336 free (reg_equiv_invariant);
1337 reg_equiv_constant = 0;
1338 reg_equiv_invariant = 0;
1339 VEC_free (rtx, gc, reg_equiv_memory_loc_vec);
1340 reg_equiv_memory_loc = 0;
1342 if (offsets_known_at)
1343 free (offsets_known_at);
1344 if (offsets_at)
1345 free (offsets_at);
1347 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1348 if (reg_equiv_alt_mem_list[i])
1349 free_EXPR_LIST_list (&reg_equiv_alt_mem_list[i]);
1350 free (reg_equiv_alt_mem_list);
1352 free (reg_equiv_mem);
1353 reg_equiv_init = 0;
1354 free (reg_equiv_address);
1355 free (reg_max_ref_width);
1356 free (reg_old_renumber);
1357 free (pseudo_previous_regs);
1358 free (pseudo_forbidden_regs);
1360 CLEAR_HARD_REG_SET (used_spill_regs);
1361 for (i = 0; i < n_spills; i++)
1362 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1364 /* Free all the insn_chain structures at once. */
1365 obstack_free (&reload_obstack, reload_startobj);
1366 unused_insn_chains = 0;
1367 fixup_abnormal_edges ();
1369 /* Replacing pseudos with their memory equivalents might have
1370 created shared rtx. Subsequent passes would get confused
1371 by this, so unshare everything here. */
1372 unshare_all_rtl_again (first);
1374 #ifdef STACK_BOUNDARY
1375 /* init_emit has set the alignment of the hard frame pointer
1376 to STACK_BOUNDARY. It is very likely no longer valid if
1377 the hard frame pointer was used for register allocation. */
1378 if (!frame_pointer_needed)
1379 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1380 #endif
1382 return failure;
1385 /* Yet another special case. Unfortunately, reg-stack forces people to
1386 write incorrect clobbers in asm statements. These clobbers must not
1387 cause the register to appear in bad_spill_regs, otherwise we'll call
1388 fatal_insn later. We clear the corresponding regnos in the live
1389 register sets to avoid this.
1390 The whole thing is rather sick, I'm afraid. */
1392 static void
1393 maybe_fix_stack_asms (void)
1395 #ifdef STACK_REGS
1396 const char *constraints[MAX_RECOG_OPERANDS];
1397 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1398 struct insn_chain *chain;
1400 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1402 int i, noperands;
1403 HARD_REG_SET clobbered, allowed;
1404 rtx pat;
1406 if (! INSN_P (chain->insn)
1407 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1408 continue;
1409 pat = PATTERN (chain->insn);
1410 if (GET_CODE (pat) != PARALLEL)
1411 continue;
1413 CLEAR_HARD_REG_SET (clobbered);
1414 CLEAR_HARD_REG_SET (allowed);
1416 /* First, make a mask of all stack regs that are clobbered. */
1417 for (i = 0; i < XVECLEN (pat, 0); i++)
1419 rtx t = XVECEXP (pat, 0, i);
1420 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1421 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1424 /* Get the operand values and constraints out of the insn. */
1425 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1426 constraints, operand_mode, NULL);
1428 /* For every operand, see what registers are allowed. */
1429 for (i = 0; i < noperands; i++)
1431 const char *p = constraints[i];
1432 /* For every alternative, we compute the class of registers allowed
1433 for reloading in CLS, and merge its contents into the reg set
1434 ALLOWED. */
1435 int cls = (int) NO_REGS;
1437 for (;;)
1439 char c = *p;
1441 if (c == '\0' || c == ',' || c == '#')
1443 /* End of one alternative - mark the regs in the current
1444 class, and reset the class. */
1445 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1446 cls = NO_REGS;
1447 p++;
1448 if (c == '#')
1449 do {
1450 c = *p++;
1451 } while (c != '\0' && c != ',');
1452 if (c == '\0')
1453 break;
1454 continue;
1457 switch (c)
1459 case '=': case '+': case '*': case '%': case '?': case '!':
1460 case '0': case '1': case '2': case '3': case '4': case 'm':
1461 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1462 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1463 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1464 case 'P':
1465 break;
1467 case 'p':
1468 cls = (int) reg_class_subunion[cls]
1469 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1470 break;
1472 case 'g':
1473 case 'r':
1474 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1475 break;
1477 default:
1478 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1479 cls = (int) reg_class_subunion[cls]
1480 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1481 else
1482 cls = (int) reg_class_subunion[cls]
1483 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1485 p += CONSTRAINT_LEN (c, p);
1488 /* Those of the registers which are clobbered, but allowed by the
1489 constraints, must be usable as reload registers. So clear them
1490 out of the life information. */
1491 AND_HARD_REG_SET (allowed, clobbered);
1492 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1493 if (TEST_HARD_REG_BIT (allowed, i))
1495 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1496 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1500 #endif
1503 /* Copy the global variables n_reloads and rld into the corresponding elts
1504 of CHAIN. */
1505 static void
1506 copy_reloads (struct insn_chain *chain)
1508 chain->n_reloads = n_reloads;
1509 chain->rld = obstack_alloc (&reload_obstack,
1510 n_reloads * sizeof (struct reload));
1511 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1512 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1515 /* Walk the chain of insns, and determine for each whether it needs reloads
1516 and/or eliminations. Build the corresponding insns_need_reload list, and
1517 set something_needs_elimination as appropriate. */
1518 static void
1519 calculate_needs_all_insns (int global)
1521 struct insn_chain **pprev_reload = &insns_need_reload;
1522 struct insn_chain *chain, *next = 0;
1524 something_needs_elimination = 0;
1526 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1527 for (chain = reload_insn_chain; chain != 0; chain = next)
1529 rtx insn = chain->insn;
1531 next = chain->next;
1533 /* Clear out the shortcuts. */
1534 chain->n_reloads = 0;
1535 chain->need_elim = 0;
1536 chain->need_reload = 0;
1537 chain->need_operand_change = 0;
1539 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1540 include REG_LABEL), we need to see what effects this has on the
1541 known offsets at labels. */
1543 if (LABEL_P (insn) || JUMP_P (insn)
1544 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1545 set_label_offsets (insn, insn, 0);
1547 if (INSN_P (insn))
1549 rtx old_body = PATTERN (insn);
1550 int old_code = INSN_CODE (insn);
1551 rtx old_notes = REG_NOTES (insn);
1552 int did_elimination = 0;
1553 int operands_changed = 0;
1554 rtx set = single_set (insn);
1556 /* Skip insns that only set an equivalence. */
1557 if (set && REG_P (SET_DEST (set))
1558 && reg_renumber[REGNO (SET_DEST (set))] < 0
1559 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1560 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1561 && reg_equiv_init[REGNO (SET_DEST (set))])
1562 continue;
1564 /* If needed, eliminate any eliminable registers. */
1565 if (num_eliminable || num_eliminable_invariants)
1566 did_elimination = eliminate_regs_in_insn (insn, 0);
1568 /* Analyze the instruction. */
1569 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1570 global, spill_reg_order);
1572 /* If a no-op set needs more than one reload, this is likely
1573 to be something that needs input address reloads. We
1574 can't get rid of this cleanly later, and it is of no use
1575 anyway, so discard it now.
1576 We only do this when expensive_optimizations is enabled,
1577 since this complements reload inheritance / output
1578 reload deletion, and it can make debugging harder. */
1579 if (flag_expensive_optimizations && n_reloads > 1)
1581 rtx set = single_set (insn);
1582 if (set
1583 && SET_SRC (set) == SET_DEST (set)
1584 && REG_P (SET_SRC (set))
1585 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1587 delete_insn (insn);
1588 /* Delete it from the reload chain. */
1589 if (chain->prev)
1590 chain->prev->next = next;
1591 else
1592 reload_insn_chain = next;
1593 if (next)
1594 next->prev = chain->prev;
1595 chain->next = unused_insn_chains;
1596 unused_insn_chains = chain;
1597 continue;
1600 if (num_eliminable)
1601 update_eliminable_offsets ();
1603 /* Remember for later shortcuts which insns had any reloads or
1604 register eliminations. */
1605 chain->need_elim = did_elimination;
1606 chain->need_reload = n_reloads > 0;
1607 chain->need_operand_change = operands_changed;
1609 /* Discard any register replacements done. */
1610 if (did_elimination)
1612 obstack_free (&reload_obstack, reload_insn_firstobj);
1613 PATTERN (insn) = old_body;
1614 INSN_CODE (insn) = old_code;
1615 REG_NOTES (insn) = old_notes;
1616 something_needs_elimination = 1;
1619 something_needs_operands_changed |= operands_changed;
1621 if (n_reloads != 0)
1623 copy_reloads (chain);
1624 *pprev_reload = chain;
1625 pprev_reload = &chain->next_need_reload;
1629 *pprev_reload = 0;
1632 /* Comparison function for qsort to decide which of two reloads
1633 should be handled first. *P1 and *P2 are the reload numbers. */
1635 static int
1636 reload_reg_class_lower (const void *r1p, const void *r2p)
1638 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1639 int t;
1641 /* Consider required reloads before optional ones. */
1642 t = rld[r1].optional - rld[r2].optional;
1643 if (t != 0)
1644 return t;
1646 /* Count all solitary classes before non-solitary ones. */
1647 t = ((reg_class_size[(int) rld[r2].class] == 1)
1648 - (reg_class_size[(int) rld[r1].class] == 1));
1649 if (t != 0)
1650 return t;
1652 /* Aside from solitaires, consider all multi-reg groups first. */
1653 t = rld[r2].nregs - rld[r1].nregs;
1654 if (t != 0)
1655 return t;
1657 /* Consider reloads in order of increasing reg-class number. */
1658 t = (int) rld[r1].class - (int) rld[r2].class;
1659 if (t != 0)
1660 return t;
1662 /* If reloads are equally urgent, sort by reload number,
1663 so that the results of qsort leave nothing to chance. */
1664 return r1 - r2;
1667 /* The cost of spilling each hard reg. */
1668 static int spill_cost[FIRST_PSEUDO_REGISTER];
1670 /* When spilling multiple hard registers, we use SPILL_COST for the first
1671 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1672 only the first hard reg for a multi-reg pseudo. */
1673 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1675 /* Update the spill cost arrays, considering that pseudo REG is live. */
1677 static void
1678 count_pseudo (int reg)
1680 int freq = REG_FREQ (reg);
1681 int r = reg_renumber[reg];
1682 int nregs;
1684 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1685 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1686 return;
1688 SET_REGNO_REG_SET (&pseudos_counted, reg);
1690 gcc_assert (r >= 0);
1692 spill_add_cost[r] += freq;
1694 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1695 while (nregs-- > 0)
1696 spill_cost[r + nregs] += freq;
1699 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1700 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1702 static void
1703 order_regs_for_reload (struct insn_chain *chain)
1705 unsigned i;
1706 HARD_REG_SET used_by_pseudos;
1707 HARD_REG_SET used_by_pseudos2;
1708 reg_set_iterator rsi;
1710 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1712 memset (spill_cost, 0, sizeof spill_cost);
1713 memset (spill_add_cost, 0, sizeof spill_add_cost);
1715 /* Count number of uses of each hard reg by pseudo regs allocated to it
1716 and then order them by decreasing use. First exclude hard registers
1717 that are live in or across this insn. */
1719 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1720 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1721 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1722 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1724 /* Now find out which pseudos are allocated to it, and update
1725 hard_reg_n_uses. */
1726 CLEAR_REG_SET (&pseudos_counted);
1728 EXECUTE_IF_SET_IN_REG_SET
1729 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1731 count_pseudo (i);
1733 EXECUTE_IF_SET_IN_REG_SET
1734 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1736 count_pseudo (i);
1738 CLEAR_REG_SET (&pseudos_counted);
1741 /* Vector of reload-numbers showing the order in which the reloads should
1742 be processed. */
1743 static short reload_order[MAX_RELOADS];
1745 /* This is used to keep track of the spill regs used in one insn. */
1746 static HARD_REG_SET used_spill_regs_local;
1748 /* We decided to spill hard register SPILLED, which has a size of
1749 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1750 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1751 update SPILL_COST/SPILL_ADD_COST. */
1753 static void
1754 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1756 int r = reg_renumber[reg];
1757 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1759 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1760 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1761 return;
1763 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1765 spill_add_cost[r] -= REG_FREQ (reg);
1766 while (nregs-- > 0)
1767 spill_cost[r + nregs] -= REG_FREQ (reg);
1770 /* Find reload register to use for reload number ORDER. */
1772 static int
1773 find_reg (struct insn_chain *chain, int order)
1775 int rnum = reload_order[order];
1776 struct reload *rl = rld + rnum;
1777 int best_cost = INT_MAX;
1778 int best_reg = -1;
1779 unsigned int i, j;
1780 int k;
1781 HARD_REG_SET not_usable;
1782 HARD_REG_SET used_by_other_reload;
1783 reg_set_iterator rsi;
1785 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1786 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1787 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1789 CLEAR_HARD_REG_SET (used_by_other_reload);
1790 for (k = 0; k < order; k++)
1792 int other = reload_order[k];
1794 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1795 for (j = 0; j < rld[other].nregs; j++)
1796 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1799 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1801 unsigned int regno = i;
1803 if (! TEST_HARD_REG_BIT (not_usable, regno)
1804 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1805 && HARD_REGNO_MODE_OK (regno, rl->mode))
1807 int this_cost = spill_cost[regno];
1808 int ok = 1;
1809 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1811 for (j = 1; j < this_nregs; j++)
1813 this_cost += spill_add_cost[regno + j];
1814 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1815 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1816 ok = 0;
1818 if (! ok)
1819 continue;
1820 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1821 this_cost--;
1822 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1823 this_cost--;
1824 if (this_cost < best_cost
1825 /* Among registers with equal cost, prefer caller-saved ones, or
1826 use REG_ALLOC_ORDER if it is defined. */
1827 || (this_cost == best_cost
1828 #ifdef REG_ALLOC_ORDER
1829 && (inv_reg_alloc_order[regno]
1830 < inv_reg_alloc_order[best_reg])
1831 #else
1832 && call_used_regs[regno]
1833 && ! call_used_regs[best_reg]
1834 #endif
1837 best_reg = regno;
1838 best_cost = this_cost;
1842 if (best_reg == -1)
1843 return 0;
1845 if (dump_file)
1846 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1848 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1849 rl->regno = best_reg;
1851 EXECUTE_IF_SET_IN_REG_SET
1852 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1854 count_spilled_pseudo (best_reg, rl->nregs, j);
1857 EXECUTE_IF_SET_IN_REG_SET
1858 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1860 count_spilled_pseudo (best_reg, rl->nregs, j);
1863 for (i = 0; i < rl->nregs; i++)
1865 gcc_assert (spill_cost[best_reg + i] == 0);
1866 gcc_assert (spill_add_cost[best_reg + i] == 0);
1867 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1869 return 1;
1872 /* Find more reload regs to satisfy the remaining need of an insn, which
1873 is given by CHAIN.
1874 Do it by ascending class number, since otherwise a reg
1875 might be spilled for a big class and might fail to count
1876 for a smaller class even though it belongs to that class. */
1878 static void
1879 find_reload_regs (struct insn_chain *chain)
1881 int i;
1883 /* In order to be certain of getting the registers we need,
1884 we must sort the reloads into order of increasing register class.
1885 Then our grabbing of reload registers will parallel the process
1886 that provided the reload registers. */
1887 for (i = 0; i < chain->n_reloads; i++)
1889 /* Show whether this reload already has a hard reg. */
1890 if (chain->rld[i].reg_rtx)
1892 int regno = REGNO (chain->rld[i].reg_rtx);
1893 chain->rld[i].regno = regno;
1894 chain->rld[i].nregs
1895 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1897 else
1898 chain->rld[i].regno = -1;
1899 reload_order[i] = i;
1902 n_reloads = chain->n_reloads;
1903 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1905 CLEAR_HARD_REG_SET (used_spill_regs_local);
1907 if (dump_file)
1908 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1910 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1912 /* Compute the order of preference for hard registers to spill. */
1914 order_regs_for_reload (chain);
1916 for (i = 0; i < n_reloads; i++)
1918 int r = reload_order[i];
1920 /* Ignore reloads that got marked inoperative. */
1921 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
1922 && ! rld[r].optional
1923 && rld[r].regno == -1)
1924 if (! find_reg (chain, i))
1926 if (dump_file)
1927 fprintf (dump_file, "reload failure for reload %d\n", r);
1928 spill_failure (chain->insn, rld[r].class);
1929 failure = 1;
1930 return;
1934 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
1935 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
1937 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1940 static void
1941 select_reload_regs (void)
1943 struct insn_chain *chain;
1945 /* Try to satisfy the needs for each insn. */
1946 for (chain = insns_need_reload; chain != 0;
1947 chain = chain->next_need_reload)
1948 find_reload_regs (chain);
1951 /* Delete all insns that were inserted by emit_caller_save_insns during
1952 this iteration. */
1953 static void
1954 delete_caller_save_insns (void)
1956 struct insn_chain *c = reload_insn_chain;
1958 while (c != 0)
1960 while (c != 0 && c->is_caller_save_insn)
1962 struct insn_chain *next = c->next;
1963 rtx insn = c->insn;
1965 if (c == reload_insn_chain)
1966 reload_insn_chain = next;
1967 delete_insn (insn);
1969 if (next)
1970 next->prev = c->prev;
1971 if (c->prev)
1972 c->prev->next = next;
1973 c->next = unused_insn_chains;
1974 unused_insn_chains = c;
1975 c = next;
1977 if (c != 0)
1978 c = c->next;
1982 /* Handle the failure to find a register to spill.
1983 INSN should be one of the insns which needed this particular spill reg. */
1985 static void
1986 spill_failure (rtx insn, enum reg_class class)
1988 if (asm_noperands (PATTERN (insn)) >= 0)
1989 error_for_asm (insn, "can't find a register in class %qs while "
1990 "reloading %<asm%>",
1991 reg_class_names[class]);
1992 else
1994 error ("unable to find a register to spill in class %qs",
1995 reg_class_names[class]);
1997 if (dump_file)
1999 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2000 debug_reload_to_stream (dump_file);
2002 fatal_insn ("this is the insn:", insn);
2006 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2007 data that is dead in INSN. */
2009 static void
2010 delete_dead_insn (rtx insn)
2012 rtx prev = prev_real_insn (insn);
2013 rtx prev_dest;
2015 /* If the previous insn sets a register that dies in our insn, delete it
2016 too. */
2017 if (prev && GET_CODE (PATTERN (prev)) == SET
2018 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2019 && reg_mentioned_p (prev_dest, PATTERN (insn))
2020 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2021 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2022 delete_dead_insn (prev);
2024 SET_INSN_DELETED (insn);
2027 /* Modify the home of pseudo-reg I.
2028 The new home is present in reg_renumber[I].
2030 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2031 or it may be -1, meaning there is none or it is not relevant.
2032 This is used so that all pseudos spilled from a given hard reg
2033 can share one stack slot. */
2035 static void
2036 alter_reg (int i, int from_reg)
2038 /* When outputting an inline function, this can happen
2039 for a reg that isn't actually used. */
2040 if (regno_reg_rtx[i] == 0)
2041 return;
2043 /* If the reg got changed to a MEM at rtl-generation time,
2044 ignore it. */
2045 if (!REG_P (regno_reg_rtx[i]))
2046 return;
2048 /* Modify the reg-rtx to contain the new hard reg
2049 number or else to contain its pseudo reg number. */
2050 REGNO (regno_reg_rtx[i])
2051 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2053 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2054 allocate a stack slot for it. */
2056 if (reg_renumber[i] < 0
2057 && REG_N_REFS (i) > 0
2058 && reg_equiv_constant[i] == 0
2059 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
2060 && reg_equiv_memory_loc[i] == 0)
2062 rtx x;
2063 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2064 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2065 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2066 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2067 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2068 int adjust = 0;
2070 /* Each pseudo reg has an inherent size which comes from its own mode,
2071 and a total size which provides room for paradoxical subregs
2072 which refer to the pseudo reg in wider modes.
2074 We can use a slot already allocated if it provides both
2075 enough inherent space and enough total space.
2076 Otherwise, we allocate a new slot, making sure that it has no less
2077 inherent space, and no less total space, then the previous slot. */
2078 if (from_reg == -1)
2080 /* No known place to spill from => no slot to reuse. */
2081 x = assign_stack_local (mode, total_size,
2082 min_align > inherent_align
2083 || total_size > inherent_size ? -1 : 0);
2084 if (BYTES_BIG_ENDIAN)
2085 /* Cancel the big-endian correction done in assign_stack_local.
2086 Get the address of the beginning of the slot.
2087 This is so we can do a big-endian correction unconditionally
2088 below. */
2089 adjust = inherent_size - total_size;
2091 /* Nothing can alias this slot except this pseudo. */
2092 set_mem_alias_set (x, new_alias_set ());
2095 /* Reuse a stack slot if possible. */
2096 else if (spill_stack_slot[from_reg] != 0
2097 && spill_stack_slot_width[from_reg] >= total_size
2098 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2099 >= inherent_size)
2100 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2101 x = spill_stack_slot[from_reg];
2103 /* Allocate a bigger slot. */
2104 else
2106 /* Compute maximum size needed, both for inherent size
2107 and for total size. */
2108 rtx stack_slot;
2110 if (spill_stack_slot[from_reg])
2112 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2113 > inherent_size)
2114 mode = GET_MODE (spill_stack_slot[from_reg]);
2115 if (spill_stack_slot_width[from_reg] > total_size)
2116 total_size = spill_stack_slot_width[from_reg];
2117 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2118 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2121 /* Make a slot with that size. */
2122 x = assign_stack_local (mode, total_size,
2123 min_align > inherent_align
2124 || total_size > inherent_size ? -1 : 0);
2125 stack_slot = x;
2127 /* All pseudos mapped to this slot can alias each other. */
2128 if (spill_stack_slot[from_reg])
2129 set_mem_alias_set (x, MEM_ALIAS_SET (spill_stack_slot[from_reg]));
2130 else
2131 set_mem_alias_set (x, new_alias_set ());
2133 if (BYTES_BIG_ENDIAN)
2135 /* Cancel the big-endian correction done in assign_stack_local.
2136 Get the address of the beginning of the slot.
2137 This is so we can do a big-endian correction unconditionally
2138 below. */
2139 adjust = GET_MODE_SIZE (mode) - total_size;
2140 if (adjust)
2141 stack_slot
2142 = adjust_address_nv (x, mode_for_size (total_size
2143 * BITS_PER_UNIT,
2144 MODE_INT, 1),
2145 adjust);
2148 spill_stack_slot[from_reg] = stack_slot;
2149 spill_stack_slot_width[from_reg] = total_size;
2152 /* On a big endian machine, the "address" of the slot
2153 is the address of the low part that fits its inherent mode. */
2154 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2155 adjust += (total_size - inherent_size);
2157 /* If we have any adjustment to make, or if the stack slot is the
2158 wrong mode, make a new stack slot. */
2159 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2161 /* If we have a decl for the original register, set it for the
2162 memory. If this is a shared MEM, make a copy. */
2163 if (REG_EXPR (regno_reg_rtx[i])
2164 && DECL_P (REG_EXPR (regno_reg_rtx[i])))
2166 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2168 /* We can do this only for the DECLs home pseudo, not for
2169 any copies of it, since otherwise when the stack slot
2170 is reused, nonoverlapping_memrefs_p might think they
2171 cannot overlap. */
2172 if (decl && REG_P (decl) && REGNO (decl) == (unsigned) i)
2174 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2175 x = copy_rtx (x);
2177 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2181 /* Save the stack slot for later. */
2182 reg_equiv_memory_loc[i] = x;
2186 /* Mark the slots in regs_ever_live for the hard regs
2187 used by pseudo-reg number REGNO. */
2189 void
2190 mark_home_live (int regno)
2192 int i, lim;
2194 i = reg_renumber[regno];
2195 if (i < 0)
2196 return;
2197 lim = i + hard_regno_nregs[i][PSEUDO_REGNO_MODE (regno)];
2198 while (i < lim)
2199 regs_ever_live[i++] = 1;
2202 /* This function handles the tracking of elimination offsets around branches.
2204 X is a piece of RTL being scanned.
2206 INSN is the insn that it came from, if any.
2208 INITIAL_P is nonzero if we are to set the offset to be the initial
2209 offset and zero if we are setting the offset of the label to be the
2210 current offset. */
2212 static void
2213 set_label_offsets (rtx x, rtx insn, int initial_p)
2215 enum rtx_code code = GET_CODE (x);
2216 rtx tem;
2217 unsigned int i;
2218 struct elim_table *p;
2220 switch (code)
2222 case LABEL_REF:
2223 if (LABEL_REF_NONLOCAL_P (x))
2224 return;
2226 x = XEXP (x, 0);
2228 /* ... fall through ... */
2230 case CODE_LABEL:
2231 /* If we know nothing about this label, set the desired offsets. Note
2232 that this sets the offset at a label to be the offset before a label
2233 if we don't know anything about the label. This is not correct for
2234 the label after a BARRIER, but is the best guess we can make. If
2235 we guessed wrong, we will suppress an elimination that might have
2236 been possible had we been able to guess correctly. */
2238 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2240 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2241 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2242 = (initial_p ? reg_eliminate[i].initial_offset
2243 : reg_eliminate[i].offset);
2244 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2247 /* Otherwise, if this is the definition of a label and it is
2248 preceded by a BARRIER, set our offsets to the known offset of
2249 that label. */
2251 else if (x == insn
2252 && (tem = prev_nonnote_insn (insn)) != 0
2253 && BARRIER_P (tem))
2254 set_offsets_for_label (insn);
2255 else
2256 /* If neither of the above cases is true, compare each offset
2257 with those previously recorded and suppress any eliminations
2258 where the offsets disagree. */
2260 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2261 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2262 != (initial_p ? reg_eliminate[i].initial_offset
2263 : reg_eliminate[i].offset))
2264 reg_eliminate[i].can_eliminate = 0;
2266 return;
2268 case JUMP_INSN:
2269 set_label_offsets (PATTERN (insn), insn, initial_p);
2271 /* ... fall through ... */
2273 case INSN:
2274 case CALL_INSN:
2275 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2276 and hence must have all eliminations at their initial offsets. */
2277 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2278 if (REG_NOTE_KIND (tem) == REG_LABEL)
2279 set_label_offsets (XEXP (tem, 0), insn, 1);
2280 return;
2282 case PARALLEL:
2283 case ADDR_VEC:
2284 case ADDR_DIFF_VEC:
2285 /* Each of the labels in the parallel or address vector must be
2286 at their initial offsets. We want the first field for PARALLEL
2287 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2289 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2290 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2291 insn, initial_p);
2292 return;
2294 case SET:
2295 /* We only care about setting PC. If the source is not RETURN,
2296 IF_THEN_ELSE, or a label, disable any eliminations not at
2297 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2298 isn't one of those possibilities. For branches to a label,
2299 call ourselves recursively.
2301 Note that this can disable elimination unnecessarily when we have
2302 a non-local goto since it will look like a non-constant jump to
2303 someplace in the current function. This isn't a significant
2304 problem since such jumps will normally be when all elimination
2305 pairs are back to their initial offsets. */
2307 if (SET_DEST (x) != pc_rtx)
2308 return;
2310 switch (GET_CODE (SET_SRC (x)))
2312 case PC:
2313 case RETURN:
2314 return;
2316 case LABEL_REF:
2317 set_label_offsets (SET_SRC (x), insn, initial_p);
2318 return;
2320 case IF_THEN_ELSE:
2321 tem = XEXP (SET_SRC (x), 1);
2322 if (GET_CODE (tem) == LABEL_REF)
2323 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2324 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2325 break;
2327 tem = XEXP (SET_SRC (x), 2);
2328 if (GET_CODE (tem) == LABEL_REF)
2329 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2330 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2331 break;
2332 return;
2334 default:
2335 break;
2338 /* If we reach here, all eliminations must be at their initial
2339 offset because we are doing a jump to a variable address. */
2340 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2341 if (p->offset != p->initial_offset)
2342 p->can_eliminate = 0;
2343 break;
2345 default:
2346 break;
2350 /* Scan X and replace any eliminable registers (such as fp) with a
2351 replacement (such as sp), plus an offset.
2353 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2354 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2355 MEM, we are allowed to replace a sum of a register and the constant zero
2356 with the register, which we cannot do outside a MEM. In addition, we need
2357 to record the fact that a register is referenced outside a MEM.
2359 If INSN is an insn, it is the insn containing X. If we replace a REG
2360 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2361 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2362 the REG is being modified.
2364 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2365 That's used when we eliminate in expressions stored in notes.
2366 This means, do not set ref_outside_mem even if the reference
2367 is outside of MEMs.
2369 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2370 replacements done assuming all offsets are at their initial values. If
2371 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2372 encounter, return the actual location so that find_reloads will do
2373 the proper thing. */
2375 static rtx
2376 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2377 bool may_use_invariant)
2379 enum rtx_code code = GET_CODE (x);
2380 struct elim_table *ep;
2381 int regno;
2382 rtx new;
2383 int i, j;
2384 const char *fmt;
2385 int copied = 0;
2387 if (! current_function_decl)
2388 return x;
2390 switch (code)
2392 case CONST_INT:
2393 case CONST_DOUBLE:
2394 case CONST_VECTOR:
2395 case CONST:
2396 case SYMBOL_REF:
2397 case CODE_LABEL:
2398 case PC:
2399 case CC0:
2400 case ASM_INPUT:
2401 case ADDR_VEC:
2402 case ADDR_DIFF_VEC:
2403 case RETURN:
2404 return x;
2406 case REG:
2407 regno = REGNO (x);
2409 /* First handle the case where we encounter a bare register that
2410 is eliminable. Replace it with a PLUS. */
2411 if (regno < FIRST_PSEUDO_REGISTER)
2413 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2414 ep++)
2415 if (ep->from_rtx == x && ep->can_eliminate)
2416 return plus_constant (ep->to_rtx, ep->previous_offset);
2419 else if (reg_renumber && reg_renumber[regno] < 0
2420 && reg_equiv_invariant && reg_equiv_invariant[regno])
2422 if (may_use_invariant)
2423 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2424 mem_mode, insn, true);
2425 /* There exists at least one use of REGNO that cannot be
2426 eliminated. Prevent the defining insn from being deleted. */
2427 reg_equiv_init[regno] = NULL_RTX;
2428 alter_reg (regno, -1);
2430 return x;
2432 /* You might think handling MINUS in a manner similar to PLUS is a
2433 good idea. It is not. It has been tried multiple times and every
2434 time the change has had to have been reverted.
2436 Other parts of reload know a PLUS is special (gen_reload for example)
2437 and require special code to handle code a reloaded PLUS operand.
2439 Also consider backends where the flags register is clobbered by a
2440 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2441 lea instruction comes to mind). If we try to reload a MINUS, we
2442 may kill the flags register that was holding a useful value.
2444 So, please before trying to handle MINUS, consider reload as a
2445 whole instead of this little section as well as the backend issues. */
2446 case PLUS:
2447 /* If this is the sum of an eliminable register and a constant, rework
2448 the sum. */
2449 if (REG_P (XEXP (x, 0))
2450 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2451 && CONSTANT_P (XEXP (x, 1)))
2453 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2454 ep++)
2455 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2457 /* The only time we want to replace a PLUS with a REG (this
2458 occurs when the constant operand of the PLUS is the negative
2459 of the offset) is when we are inside a MEM. We won't want
2460 to do so at other times because that would change the
2461 structure of the insn in a way that reload can't handle.
2462 We special-case the commonest situation in
2463 eliminate_regs_in_insn, so just replace a PLUS with a
2464 PLUS here, unless inside a MEM. */
2465 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2466 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2467 return ep->to_rtx;
2468 else
2469 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2470 plus_constant (XEXP (x, 1),
2471 ep->previous_offset));
2474 /* If the register is not eliminable, we are done since the other
2475 operand is a constant. */
2476 return x;
2479 /* If this is part of an address, we want to bring any constant to the
2480 outermost PLUS. We will do this by doing register replacement in
2481 our operands and seeing if a constant shows up in one of them.
2483 Note that there is no risk of modifying the structure of the insn,
2484 since we only get called for its operands, thus we are either
2485 modifying the address inside a MEM, or something like an address
2486 operand of a load-address insn. */
2489 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2490 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2492 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2494 /* If one side is a PLUS and the other side is a pseudo that
2495 didn't get a hard register but has a reg_equiv_constant,
2496 we must replace the constant here since it may no longer
2497 be in the position of any operand. */
2498 if (GET_CODE (new0) == PLUS && REG_P (new1)
2499 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2500 && reg_renumber[REGNO (new1)] < 0
2501 && reg_equiv_constant != 0
2502 && reg_equiv_constant[REGNO (new1)] != 0)
2503 new1 = reg_equiv_constant[REGNO (new1)];
2504 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2505 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2506 && reg_renumber[REGNO (new0)] < 0
2507 && reg_equiv_constant[REGNO (new0)] != 0)
2508 new0 = reg_equiv_constant[REGNO (new0)];
2510 new = form_sum (new0, new1);
2512 /* As above, if we are not inside a MEM we do not want to
2513 turn a PLUS into something else. We might try to do so here
2514 for an addition of 0 if we aren't optimizing. */
2515 if (! mem_mode && GET_CODE (new) != PLUS)
2516 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2517 else
2518 return new;
2521 return x;
2523 case MULT:
2524 /* If this is the product of an eliminable register and a
2525 constant, apply the distribute law and move the constant out
2526 so that we have (plus (mult ..) ..). This is needed in order
2527 to keep load-address insns valid. This case is pathological.
2528 We ignore the possibility of overflow here. */
2529 if (REG_P (XEXP (x, 0))
2530 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2531 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2532 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2533 ep++)
2534 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2536 if (! mem_mode
2537 /* Refs inside notes don't count for this purpose. */
2538 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2539 || GET_CODE (insn) == INSN_LIST)))
2540 ep->ref_outside_mem = 1;
2542 return
2543 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2544 ep->previous_offset * INTVAL (XEXP (x, 1)));
2547 /* ... fall through ... */
2549 case CALL:
2550 case COMPARE:
2551 /* See comments before PLUS about handling MINUS. */
2552 case MINUS:
2553 case DIV: case UDIV:
2554 case MOD: case UMOD:
2555 case AND: case IOR: case XOR:
2556 case ROTATERT: case ROTATE:
2557 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2558 case NE: case EQ:
2559 case GE: case GT: case GEU: case GTU:
2560 case LE: case LT: case LEU: case LTU:
2562 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2563 rtx new1 = XEXP (x, 1)
2564 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false) : 0;
2566 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2567 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2569 return x;
2571 case EXPR_LIST:
2572 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2573 if (XEXP (x, 0))
2575 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2576 if (new != XEXP (x, 0))
2578 /* If this is a REG_DEAD note, it is not valid anymore.
2579 Using the eliminated version could result in creating a
2580 REG_DEAD note for the stack or frame pointer. */
2581 if (GET_MODE (x) == REG_DEAD)
2582 return (XEXP (x, 1)
2583 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true)
2584 : NULL_RTX);
2586 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2590 /* ... fall through ... */
2592 case INSN_LIST:
2593 /* Now do eliminations in the rest of the chain. If this was
2594 an EXPR_LIST, this might result in allocating more memory than is
2595 strictly needed, but it simplifies the code. */
2596 if (XEXP (x, 1))
2598 new = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2599 if (new != XEXP (x, 1))
2600 return
2601 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2603 return x;
2605 case PRE_INC:
2606 case POST_INC:
2607 case PRE_DEC:
2608 case POST_DEC:
2609 /* We do not support elimination of a register that is modified.
2610 elimination_effects has already make sure that this does not
2611 happen. */
2612 return x;
2614 case PRE_MODIFY:
2615 case POST_MODIFY:
2616 /* We do not support elimination of a register that is modified.
2617 elimination_effects has already make sure that this does not
2618 happen. The only remaining case we need to consider here is
2619 that the increment value may be an eliminable register. */
2620 if (GET_CODE (XEXP (x, 1)) == PLUS
2621 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2623 rtx new = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2624 insn, true);
2626 if (new != XEXP (XEXP (x, 1), 1))
2627 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2628 gen_rtx_PLUS (GET_MODE (x),
2629 XEXP (x, 0), new));
2631 return x;
2633 case STRICT_LOW_PART:
2634 case NEG: case NOT:
2635 case SIGN_EXTEND: case ZERO_EXTEND:
2636 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2637 case FLOAT: case FIX:
2638 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2639 case ABS:
2640 case SQRT:
2641 case FFS:
2642 case CLZ:
2643 case CTZ:
2644 case POPCOUNT:
2645 case PARITY:
2646 case BSWAP:
2647 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2648 if (new != XEXP (x, 0))
2649 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2650 return x;
2652 case SUBREG:
2653 /* Similar to above processing, but preserve SUBREG_BYTE.
2654 Convert (subreg (mem)) to (mem) if not paradoxical.
2655 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2656 pseudo didn't get a hard reg, we must replace this with the
2657 eliminated version of the memory location because push_reload
2658 may do the replacement in certain circumstances. */
2659 if (REG_P (SUBREG_REG (x))
2660 && (GET_MODE_SIZE (GET_MODE (x))
2661 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2662 && reg_equiv_memory_loc != 0
2663 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2665 new = SUBREG_REG (x);
2667 else
2668 new = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false);
2670 if (new != SUBREG_REG (x))
2672 int x_size = GET_MODE_SIZE (GET_MODE (x));
2673 int new_size = GET_MODE_SIZE (GET_MODE (new));
2675 if (MEM_P (new)
2676 && ((x_size < new_size
2677 #ifdef WORD_REGISTER_OPERATIONS
2678 /* On these machines, combine can create rtl of the form
2679 (set (subreg:m1 (reg:m2 R) 0) ...)
2680 where m1 < m2, and expects something interesting to
2681 happen to the entire word. Moreover, it will use the
2682 (reg:m2 R) later, expecting all bits to be preserved.
2683 So if the number of words is the same, preserve the
2684 subreg so that push_reload can see it. */
2685 && ! ((x_size - 1) / UNITS_PER_WORD
2686 == (new_size -1 ) / UNITS_PER_WORD)
2687 #endif
2689 || x_size == new_size)
2691 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2692 else
2693 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2696 return x;
2698 case MEM:
2699 /* Our only special processing is to pass the mode of the MEM to our
2700 recursive call and copy the flags. While we are here, handle this
2701 case more efficiently. */
2702 return
2703 replace_equiv_address_nv (x,
2704 eliminate_regs_1 (XEXP (x, 0), GET_MODE (x),
2705 insn, true));
2707 case USE:
2708 /* Handle insn_list USE that a call to a pure function may generate. */
2709 new = eliminate_regs_1 (XEXP (x, 0), 0, insn, false);
2710 if (new != XEXP (x, 0))
2711 return gen_rtx_USE (GET_MODE (x), new);
2712 return x;
2714 case CLOBBER:
2715 case ASM_OPERANDS:
2716 case SET:
2717 gcc_unreachable ();
2719 default:
2720 break;
2723 /* Process each of our operands recursively. If any have changed, make a
2724 copy of the rtx. */
2725 fmt = GET_RTX_FORMAT (code);
2726 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2728 if (*fmt == 'e')
2730 new = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false);
2731 if (new != XEXP (x, i) && ! copied)
2733 x = shallow_copy_rtx (x);
2734 copied = 1;
2736 XEXP (x, i) = new;
2738 else if (*fmt == 'E')
2740 int copied_vec = 0;
2741 for (j = 0; j < XVECLEN (x, i); j++)
2743 new = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false);
2744 if (new != XVECEXP (x, i, j) && ! copied_vec)
2746 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2747 XVEC (x, i)->elem);
2748 if (! copied)
2750 x = shallow_copy_rtx (x);
2751 copied = 1;
2753 XVEC (x, i) = new_v;
2754 copied_vec = 1;
2756 XVECEXP (x, i, j) = new;
2761 return x;
2765 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2767 return eliminate_regs_1 (x, mem_mode, insn, false);
2770 /* Scan rtx X for modifications of elimination target registers. Update
2771 the table of eliminables to reflect the changed state. MEM_MODE is
2772 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2774 static void
2775 elimination_effects (rtx x, enum machine_mode mem_mode)
2777 enum rtx_code code = GET_CODE (x);
2778 struct elim_table *ep;
2779 int regno;
2780 int i, j;
2781 const char *fmt;
2783 switch (code)
2785 case CONST_INT:
2786 case CONST_DOUBLE:
2787 case CONST_VECTOR:
2788 case CONST:
2789 case SYMBOL_REF:
2790 case CODE_LABEL:
2791 case PC:
2792 case CC0:
2793 case ASM_INPUT:
2794 case ADDR_VEC:
2795 case ADDR_DIFF_VEC:
2796 case RETURN:
2797 return;
2799 case REG:
2800 regno = REGNO (x);
2802 /* First handle the case where we encounter a bare register that
2803 is eliminable. Replace it with a PLUS. */
2804 if (regno < FIRST_PSEUDO_REGISTER)
2806 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2807 ep++)
2808 if (ep->from_rtx == x && ep->can_eliminate)
2810 if (! mem_mode)
2811 ep->ref_outside_mem = 1;
2812 return;
2816 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2817 && reg_equiv_constant[regno]
2818 && ! function_invariant_p (reg_equiv_constant[regno]))
2819 elimination_effects (reg_equiv_constant[regno], mem_mode);
2820 return;
2822 case PRE_INC:
2823 case POST_INC:
2824 case PRE_DEC:
2825 case POST_DEC:
2826 case POST_MODIFY:
2827 case PRE_MODIFY:
2828 /* If we modify the source of an elimination rule, disable it. */
2829 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2830 if (ep->from_rtx == XEXP (x, 0))
2831 ep->can_eliminate = 0;
2833 /* If we modify the target of an elimination rule by adding a constant,
2834 update its offset. If we modify the target in any other way, we'll
2835 have to disable the rule as well. */
2836 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2837 if (ep->to_rtx == XEXP (x, 0))
2839 int size = GET_MODE_SIZE (mem_mode);
2841 /* If more bytes than MEM_MODE are pushed, account for them. */
2842 #ifdef PUSH_ROUNDING
2843 if (ep->to_rtx == stack_pointer_rtx)
2844 size = PUSH_ROUNDING (size);
2845 #endif
2846 if (code == PRE_DEC || code == POST_DEC)
2847 ep->offset += size;
2848 else if (code == PRE_INC || code == POST_INC)
2849 ep->offset -= size;
2850 else if (code == PRE_MODIFY || code == POST_MODIFY)
2852 if (GET_CODE (XEXP (x, 1)) == PLUS
2853 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2854 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
2855 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2856 else
2857 ep->can_eliminate = 0;
2861 /* These two aren't unary operators. */
2862 if (code == POST_MODIFY || code == PRE_MODIFY)
2863 break;
2865 /* Fall through to generic unary operation case. */
2866 case STRICT_LOW_PART:
2867 case NEG: case NOT:
2868 case SIGN_EXTEND: case ZERO_EXTEND:
2869 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2870 case FLOAT: case FIX:
2871 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2872 case ABS:
2873 case SQRT:
2874 case FFS:
2875 case CLZ:
2876 case CTZ:
2877 case POPCOUNT:
2878 case PARITY:
2879 case BSWAP:
2880 elimination_effects (XEXP (x, 0), mem_mode);
2881 return;
2883 case SUBREG:
2884 if (REG_P (SUBREG_REG (x))
2885 && (GET_MODE_SIZE (GET_MODE (x))
2886 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2887 && reg_equiv_memory_loc != 0
2888 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2889 return;
2891 elimination_effects (SUBREG_REG (x), mem_mode);
2892 return;
2894 case USE:
2895 /* If using a register that is the source of an eliminate we still
2896 think can be performed, note it cannot be performed since we don't
2897 know how this register is used. */
2898 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2899 if (ep->from_rtx == XEXP (x, 0))
2900 ep->can_eliminate = 0;
2902 elimination_effects (XEXP (x, 0), mem_mode);
2903 return;
2905 case CLOBBER:
2906 /* If clobbering a register that is the replacement register for an
2907 elimination we still think can be performed, note that it cannot
2908 be performed. Otherwise, we need not be concerned about it. */
2909 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2910 if (ep->to_rtx == XEXP (x, 0))
2911 ep->can_eliminate = 0;
2913 elimination_effects (XEXP (x, 0), mem_mode);
2914 return;
2916 case SET:
2917 /* Check for setting a register that we know about. */
2918 if (REG_P (SET_DEST (x)))
2920 /* See if this is setting the replacement register for an
2921 elimination.
2923 If DEST is the hard frame pointer, we do nothing because we
2924 assume that all assignments to the frame pointer are for
2925 non-local gotos and are being done at a time when they are valid
2926 and do not disturb anything else. Some machines want to
2927 eliminate a fake argument pointer (or even a fake frame pointer)
2928 with either the real frame or the stack pointer. Assignments to
2929 the hard frame pointer must not prevent this elimination. */
2931 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2932 ep++)
2933 if (ep->to_rtx == SET_DEST (x)
2934 && SET_DEST (x) != hard_frame_pointer_rtx)
2936 /* If it is being incremented, adjust the offset. Otherwise,
2937 this elimination can't be done. */
2938 rtx src = SET_SRC (x);
2940 if (GET_CODE (src) == PLUS
2941 && XEXP (src, 0) == SET_DEST (x)
2942 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2943 ep->offset -= INTVAL (XEXP (src, 1));
2944 else
2945 ep->can_eliminate = 0;
2949 elimination_effects (SET_DEST (x), 0);
2950 elimination_effects (SET_SRC (x), 0);
2951 return;
2953 case MEM:
2954 /* Our only special processing is to pass the mode of the MEM to our
2955 recursive call. */
2956 elimination_effects (XEXP (x, 0), GET_MODE (x));
2957 return;
2959 default:
2960 break;
2963 fmt = GET_RTX_FORMAT (code);
2964 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2966 if (*fmt == 'e')
2967 elimination_effects (XEXP (x, i), mem_mode);
2968 else if (*fmt == 'E')
2969 for (j = 0; j < XVECLEN (x, i); j++)
2970 elimination_effects (XVECEXP (x, i, j), mem_mode);
2974 /* Descend through rtx X and verify that no references to eliminable registers
2975 remain. If any do remain, mark the involved register as not
2976 eliminable. */
2978 static void
2979 check_eliminable_occurrences (rtx x)
2981 const char *fmt;
2982 int i;
2983 enum rtx_code code;
2985 if (x == 0)
2986 return;
2988 code = GET_CODE (x);
2990 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
2992 struct elim_table *ep;
2994 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2995 if (ep->from_rtx == x)
2996 ep->can_eliminate = 0;
2997 return;
3000 fmt = GET_RTX_FORMAT (code);
3001 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3003 if (*fmt == 'e')
3004 check_eliminable_occurrences (XEXP (x, i));
3005 else if (*fmt == 'E')
3007 int j;
3008 for (j = 0; j < XVECLEN (x, i); j++)
3009 check_eliminable_occurrences (XVECEXP (x, i, j));
3014 /* Scan INSN and eliminate all eliminable registers in it.
3016 If REPLACE is nonzero, do the replacement destructively. Also
3017 delete the insn as dead it if it is setting an eliminable register.
3019 If REPLACE is zero, do all our allocations in reload_obstack.
3021 If no eliminations were done and this insn doesn't require any elimination
3022 processing (these are not identical conditions: it might be updating sp,
3023 but not referencing fp; this needs to be seen during reload_as_needed so
3024 that the offset between fp and sp can be taken into consideration), zero
3025 is returned. Otherwise, 1 is returned. */
3027 static int
3028 eliminate_regs_in_insn (rtx insn, int replace)
3030 int icode = recog_memoized (insn);
3031 rtx old_body = PATTERN (insn);
3032 int insn_is_asm = asm_noperands (old_body) >= 0;
3033 rtx old_set = single_set (insn);
3034 rtx new_body;
3035 int val = 0;
3036 int i;
3037 rtx substed_operand[MAX_RECOG_OPERANDS];
3038 rtx orig_operand[MAX_RECOG_OPERANDS];
3039 struct elim_table *ep;
3040 rtx plus_src, plus_cst_src;
3042 if (! insn_is_asm && icode < 0)
3044 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3045 || GET_CODE (PATTERN (insn)) == CLOBBER
3046 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3047 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3048 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
3049 return 0;
3052 if (old_set != 0 && REG_P (SET_DEST (old_set))
3053 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3055 /* Check for setting an eliminable register. */
3056 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3057 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3059 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3060 /* If this is setting the frame pointer register to the
3061 hardware frame pointer register and this is an elimination
3062 that will be done (tested above), this insn is really
3063 adjusting the frame pointer downward to compensate for
3064 the adjustment done before a nonlocal goto. */
3065 if (ep->from == FRAME_POINTER_REGNUM
3066 && ep->to == HARD_FRAME_POINTER_REGNUM)
3068 rtx base = SET_SRC (old_set);
3069 rtx base_insn = insn;
3070 HOST_WIDE_INT offset = 0;
3072 while (base != ep->to_rtx)
3074 rtx prev_insn, prev_set;
3076 if (GET_CODE (base) == PLUS
3077 && GET_CODE (XEXP (base, 1)) == CONST_INT)
3079 offset += INTVAL (XEXP (base, 1));
3080 base = XEXP (base, 0);
3082 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3083 && (prev_set = single_set (prev_insn)) != 0
3084 && rtx_equal_p (SET_DEST (prev_set), base))
3086 base = SET_SRC (prev_set);
3087 base_insn = prev_insn;
3089 else
3090 break;
3093 if (base == ep->to_rtx)
3095 rtx src
3096 = plus_constant (ep->to_rtx, offset - ep->offset);
3098 new_body = old_body;
3099 if (! replace)
3101 new_body = copy_insn (old_body);
3102 if (REG_NOTES (insn))
3103 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3105 PATTERN (insn) = new_body;
3106 old_set = single_set (insn);
3108 /* First see if this insn remains valid when we
3109 make the change. If not, keep the INSN_CODE
3110 the same and let reload fit it up. */
3111 validate_change (insn, &SET_SRC (old_set), src, 1);
3112 validate_change (insn, &SET_DEST (old_set),
3113 ep->to_rtx, 1);
3114 if (! apply_change_group ())
3116 SET_SRC (old_set) = src;
3117 SET_DEST (old_set) = ep->to_rtx;
3120 val = 1;
3121 goto done;
3124 #endif
3126 /* In this case this insn isn't serving a useful purpose. We
3127 will delete it in reload_as_needed once we know that this
3128 elimination is, in fact, being done.
3130 If REPLACE isn't set, we can't delete this insn, but needn't
3131 process it since it won't be used unless something changes. */
3132 if (replace)
3134 delete_dead_insn (insn);
3135 return 1;
3137 val = 1;
3138 goto done;
3142 /* We allow one special case which happens to work on all machines we
3143 currently support: a single set with the source or a REG_EQUAL
3144 note being a PLUS of an eliminable register and a constant. */
3145 plus_src = plus_cst_src = 0;
3146 if (old_set && REG_P (SET_DEST (old_set)))
3148 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3149 plus_src = SET_SRC (old_set);
3150 /* First see if the source is of the form (plus (...) CST). */
3151 if (plus_src
3152 && GET_CODE (XEXP (plus_src, 1)) == CONST_INT)
3153 plus_cst_src = plus_src;
3154 else if (REG_P (SET_SRC (old_set))
3155 || plus_src)
3157 /* Otherwise, see if we have a REG_EQUAL note of the form
3158 (plus (...) CST). */
3159 rtx links;
3160 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3162 if ((REG_NOTE_KIND (links) == REG_EQUAL
3163 || REG_NOTE_KIND (links) == REG_EQUIV)
3164 && GET_CODE (XEXP (links, 0)) == PLUS
3165 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT)
3167 plus_cst_src = XEXP (links, 0);
3168 break;
3173 /* Check that the first operand of the PLUS is a hard reg or
3174 the lowpart subreg of one. */
3175 if (plus_cst_src)
3177 rtx reg = XEXP (plus_cst_src, 0);
3178 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3179 reg = SUBREG_REG (reg);
3181 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3182 plus_cst_src = 0;
3185 if (plus_cst_src)
3187 rtx reg = XEXP (plus_cst_src, 0);
3188 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3190 if (GET_CODE (reg) == SUBREG)
3191 reg = SUBREG_REG (reg);
3193 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3194 if (ep->from_rtx == reg && ep->can_eliminate)
3196 rtx to_rtx = ep->to_rtx;
3197 offset += ep->offset;
3198 offset = trunc_int_for_mode (offset, GET_MODE (reg));
3200 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3201 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3202 to_rtx);
3203 /* If we have a nonzero offset, and the source is already
3204 a simple REG, the following transformation would
3205 increase the cost of the insn by replacing a simple REG
3206 with (plus (reg sp) CST). So try only when we already
3207 had a PLUS before. */
3208 if (offset == 0 || plus_src)
3210 rtx new_src = plus_constant (to_rtx, offset);
3212 new_body = old_body;
3213 if (! replace)
3215 new_body = copy_insn (old_body);
3216 if (REG_NOTES (insn))
3217 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3219 PATTERN (insn) = new_body;
3220 old_set = single_set (insn);
3222 /* First see if this insn remains valid when we make the
3223 change. If not, try to replace the whole pattern with
3224 a simple set (this may help if the original insn was a
3225 PARALLEL that was only recognized as single_set due to
3226 REG_UNUSED notes). If this isn't valid either, keep
3227 the INSN_CODE the same and let reload fix it up. */
3228 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3230 rtx new_pat = gen_rtx_SET (VOIDmode,
3231 SET_DEST (old_set), new_src);
3233 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3234 SET_SRC (old_set) = new_src;
3237 else
3238 break;
3240 val = 1;
3241 /* This can't have an effect on elimination offsets, so skip right
3242 to the end. */
3243 goto done;
3247 /* Determine the effects of this insn on elimination offsets. */
3248 elimination_effects (old_body, 0);
3250 /* Eliminate all eliminable registers occurring in operands that
3251 can be handled by reload. */
3252 extract_insn (insn);
3253 for (i = 0; i < recog_data.n_operands; i++)
3255 orig_operand[i] = recog_data.operand[i];
3256 substed_operand[i] = recog_data.operand[i];
3258 /* For an asm statement, every operand is eliminable. */
3259 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3261 bool is_set_src, in_plus;
3263 /* Check for setting a register that we know about. */
3264 if (recog_data.operand_type[i] != OP_IN
3265 && REG_P (orig_operand[i]))
3267 /* If we are assigning to a register that can be eliminated, it
3268 must be as part of a PARALLEL, since the code above handles
3269 single SETs. We must indicate that we can no longer
3270 eliminate this reg. */
3271 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3272 ep++)
3273 if (ep->from_rtx == orig_operand[i])
3274 ep->can_eliminate = 0;
3277 /* Companion to the above plus substitution, we can allow
3278 invariants as the source of a plain move. */
3279 is_set_src = false;
3280 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3281 is_set_src = true;
3282 in_plus = false;
3283 if (plus_src
3284 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3285 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3286 in_plus = true;
3288 substed_operand[i]
3289 = eliminate_regs_1 (recog_data.operand[i], 0,
3290 replace ? insn : NULL_RTX,
3291 is_set_src || in_plus);
3292 if (substed_operand[i] != orig_operand[i])
3293 val = 1;
3294 /* Terminate the search in check_eliminable_occurrences at
3295 this point. */
3296 *recog_data.operand_loc[i] = 0;
3298 /* If an output operand changed from a REG to a MEM and INSN is an
3299 insn, write a CLOBBER insn. */
3300 if (recog_data.operand_type[i] != OP_IN
3301 && REG_P (orig_operand[i])
3302 && MEM_P (substed_operand[i])
3303 && replace)
3304 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3305 insn);
3309 for (i = 0; i < recog_data.n_dups; i++)
3310 *recog_data.dup_loc[i]
3311 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3313 /* If any eliminable remain, they aren't eliminable anymore. */
3314 check_eliminable_occurrences (old_body);
3316 /* Substitute the operands; the new values are in the substed_operand
3317 array. */
3318 for (i = 0; i < recog_data.n_operands; i++)
3319 *recog_data.operand_loc[i] = substed_operand[i];
3320 for (i = 0; i < recog_data.n_dups; i++)
3321 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3323 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3324 re-recognize the insn. We do this in case we had a simple addition
3325 but now can do this as a load-address. This saves an insn in this
3326 common case.
3327 If re-recognition fails, the old insn code number will still be used,
3328 and some register operands may have changed into PLUS expressions.
3329 These will be handled by find_reloads by loading them into a register
3330 again. */
3332 if (val)
3334 /* If we aren't replacing things permanently and we changed something,
3335 make another copy to ensure that all the RTL is new. Otherwise
3336 things can go wrong if find_reload swaps commutative operands
3337 and one is inside RTL that has been copied while the other is not. */
3338 new_body = old_body;
3339 if (! replace)
3341 new_body = copy_insn (old_body);
3342 if (REG_NOTES (insn))
3343 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3345 PATTERN (insn) = new_body;
3347 /* If we had a move insn but now we don't, rerecognize it. This will
3348 cause spurious re-recognition if the old move had a PARALLEL since
3349 the new one still will, but we can't call single_set without
3350 having put NEW_BODY into the insn and the re-recognition won't
3351 hurt in this rare case. */
3352 /* ??? Why this huge if statement - why don't we just rerecognize the
3353 thing always? */
3354 if (! insn_is_asm
3355 && old_set != 0
3356 && ((REG_P (SET_SRC (old_set))
3357 && (GET_CODE (new_body) != SET
3358 || !REG_P (SET_SRC (new_body))))
3359 /* If this was a load from or store to memory, compare
3360 the MEM in recog_data.operand to the one in the insn.
3361 If they are not equal, then rerecognize the insn. */
3362 || (old_set != 0
3363 && ((MEM_P (SET_SRC (old_set))
3364 && SET_SRC (old_set) != recog_data.operand[1])
3365 || (MEM_P (SET_DEST (old_set))
3366 && SET_DEST (old_set) != recog_data.operand[0])))
3367 /* If this was an add insn before, rerecognize. */
3368 || GET_CODE (SET_SRC (old_set)) == PLUS))
3370 int new_icode = recog (PATTERN (insn), insn, 0);
3371 if (new_icode >= 0)
3372 INSN_CODE (insn) = new_icode;
3376 /* Restore the old body. If there were any changes to it, we made a copy
3377 of it while the changes were still in place, so we'll correctly return
3378 a modified insn below. */
3379 if (! replace)
3381 /* Restore the old body. */
3382 for (i = 0; i < recog_data.n_operands; i++)
3383 *recog_data.operand_loc[i] = orig_operand[i];
3384 for (i = 0; i < recog_data.n_dups; i++)
3385 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3388 /* Update all elimination pairs to reflect the status after the current
3389 insn. The changes we make were determined by the earlier call to
3390 elimination_effects.
3392 We also detect cases where register elimination cannot be done,
3393 namely, if a register would be both changed and referenced outside a MEM
3394 in the resulting insn since such an insn is often undefined and, even if
3395 not, we cannot know what meaning will be given to it. Note that it is
3396 valid to have a register used in an address in an insn that changes it
3397 (presumably with a pre- or post-increment or decrement).
3399 If anything changes, return nonzero. */
3401 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3403 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3404 ep->can_eliminate = 0;
3406 ep->ref_outside_mem = 0;
3408 if (ep->previous_offset != ep->offset)
3409 val = 1;
3412 done:
3413 /* If we changed something, perform elimination in REG_NOTES. This is
3414 needed even when REPLACE is zero because a REG_DEAD note might refer
3415 to a register that we eliminate and could cause a different number
3416 of spill registers to be needed in the final reload pass than in
3417 the pre-passes. */
3418 if (val && REG_NOTES (insn) != 0)
3419 REG_NOTES (insn)
3420 = eliminate_regs_1 (REG_NOTES (insn), 0, REG_NOTES (insn), true);
3422 return val;
3425 /* Loop through all elimination pairs.
3426 Recalculate the number not at initial offset.
3428 Compute the maximum offset (minimum offset if the stack does not
3429 grow downward) for each elimination pair. */
3431 static void
3432 update_eliminable_offsets (void)
3434 struct elim_table *ep;
3436 num_not_at_initial_offset = 0;
3437 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3439 ep->previous_offset = ep->offset;
3440 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3441 num_not_at_initial_offset++;
3445 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3446 replacement we currently believe is valid, mark it as not eliminable if X
3447 modifies DEST in any way other than by adding a constant integer to it.
3449 If DEST is the frame pointer, we do nothing because we assume that
3450 all assignments to the hard frame pointer are nonlocal gotos and are being
3451 done at a time when they are valid and do not disturb anything else.
3452 Some machines want to eliminate a fake argument pointer with either the
3453 frame or stack pointer. Assignments to the hard frame pointer must not
3454 prevent this elimination.
3456 Called via note_stores from reload before starting its passes to scan
3457 the insns of the function. */
3459 static void
3460 mark_not_eliminable (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
3462 unsigned int i;
3464 /* A SUBREG of a hard register here is just changing its mode. We should
3465 not see a SUBREG of an eliminable hard register, but check just in
3466 case. */
3467 if (GET_CODE (dest) == SUBREG)
3468 dest = SUBREG_REG (dest);
3470 if (dest == hard_frame_pointer_rtx)
3471 return;
3473 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3474 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3475 && (GET_CODE (x) != SET
3476 || GET_CODE (SET_SRC (x)) != PLUS
3477 || XEXP (SET_SRC (x), 0) != dest
3478 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3480 reg_eliminate[i].can_eliminate_previous
3481 = reg_eliminate[i].can_eliminate = 0;
3482 num_eliminable--;
3486 /* Verify that the initial elimination offsets did not change since the
3487 last call to set_initial_elim_offsets. This is used to catch cases
3488 where something illegal happened during reload_as_needed that could
3489 cause incorrect code to be generated if we did not check for it. */
3491 static bool
3492 verify_initial_elim_offsets (void)
3494 HOST_WIDE_INT t;
3496 if (!num_eliminable)
3497 return true;
3499 #ifdef ELIMINABLE_REGS
3501 struct elim_table *ep;
3503 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3505 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3506 if (t != ep->initial_offset)
3507 return false;
3510 #else
3511 INITIAL_FRAME_POINTER_OFFSET (t);
3512 if (t != reg_eliminate[0].initial_offset)
3513 return false;
3514 #endif
3516 return true;
3519 /* Reset all offsets on eliminable registers to their initial values. */
3521 static void
3522 set_initial_elim_offsets (void)
3524 struct elim_table *ep = reg_eliminate;
3526 #ifdef ELIMINABLE_REGS
3527 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3529 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3530 ep->previous_offset = ep->offset = ep->initial_offset;
3532 #else
3533 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3534 ep->previous_offset = ep->offset = ep->initial_offset;
3535 #endif
3537 num_not_at_initial_offset = 0;
3540 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3542 static void
3543 set_initial_eh_label_offset (rtx label)
3545 set_label_offsets (label, NULL_RTX, 1);
3548 /* Initialize the known label offsets.
3549 Set a known offset for each forced label to be at the initial offset
3550 of each elimination. We do this because we assume that all
3551 computed jumps occur from a location where each elimination is
3552 at its initial offset.
3553 For all other labels, show that we don't know the offsets. */
3555 static void
3556 set_initial_label_offsets (void)
3558 rtx x;
3559 memset (offsets_known_at, 0, num_labels);
3561 for (x = forced_labels; x; x = XEXP (x, 1))
3562 if (XEXP (x, 0))
3563 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3565 for_each_eh_label (set_initial_eh_label_offset);
3568 /* Set all elimination offsets to the known values for the code label given
3569 by INSN. */
3571 static void
3572 set_offsets_for_label (rtx insn)
3574 unsigned int i;
3575 int label_nr = CODE_LABEL_NUMBER (insn);
3576 struct elim_table *ep;
3578 num_not_at_initial_offset = 0;
3579 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3581 ep->offset = ep->previous_offset
3582 = offsets_at[label_nr - first_label_num][i];
3583 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3584 num_not_at_initial_offset++;
3588 /* See if anything that happened changes which eliminations are valid.
3589 For example, on the SPARC, whether or not the frame pointer can
3590 be eliminated can depend on what registers have been used. We need
3591 not check some conditions again (such as flag_omit_frame_pointer)
3592 since they can't have changed. */
3594 static void
3595 update_eliminables (HARD_REG_SET *pset)
3597 int previous_frame_pointer_needed = frame_pointer_needed;
3598 struct elim_table *ep;
3600 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3601 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3602 #ifdef ELIMINABLE_REGS
3603 || ! CAN_ELIMINATE (ep->from, ep->to)
3604 #endif
3606 ep->can_eliminate = 0;
3608 /* Look for the case where we have discovered that we can't replace
3609 register A with register B and that means that we will now be
3610 trying to replace register A with register C. This means we can
3611 no longer replace register C with register B and we need to disable
3612 such an elimination, if it exists. This occurs often with A == ap,
3613 B == sp, and C == fp. */
3615 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3617 struct elim_table *op;
3618 int new_to = -1;
3620 if (! ep->can_eliminate && ep->can_eliminate_previous)
3622 /* Find the current elimination for ep->from, if there is a
3623 new one. */
3624 for (op = reg_eliminate;
3625 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3626 if (op->from == ep->from && op->can_eliminate)
3628 new_to = op->to;
3629 break;
3632 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3633 disable it. */
3634 for (op = reg_eliminate;
3635 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3636 if (op->from == new_to && op->to == ep->to)
3637 op->can_eliminate = 0;
3641 /* See if any registers that we thought we could eliminate the previous
3642 time are no longer eliminable. If so, something has changed and we
3643 must spill the register. Also, recompute the number of eliminable
3644 registers and see if the frame pointer is needed; it is if there is
3645 no elimination of the frame pointer that we can perform. */
3647 frame_pointer_needed = 1;
3648 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3650 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3651 && ep->to != HARD_FRAME_POINTER_REGNUM)
3652 frame_pointer_needed = 0;
3654 if (! ep->can_eliminate && ep->can_eliminate_previous)
3656 ep->can_eliminate_previous = 0;
3657 SET_HARD_REG_BIT (*pset, ep->from);
3658 num_eliminable--;
3662 /* If we didn't need a frame pointer last time, but we do now, spill
3663 the hard frame pointer. */
3664 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3665 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3668 /* Return true if X is used as the target register of an elimination. */
3670 bool
3671 elimination_target_reg_p (rtx x)
3673 struct elim_table *ep;
3675 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3676 if (ep->to_rtx == x && ep->can_eliminate)
3677 return true;
3679 return false;
3682 /* Initialize the table of registers to eliminate. */
3684 static void
3685 init_elim_table (void)
3687 struct elim_table *ep;
3688 #ifdef ELIMINABLE_REGS
3689 const struct elim_table_1 *ep1;
3690 #endif
3692 if (!reg_eliminate)
3693 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3695 /* Does this function require a frame pointer? */
3697 frame_pointer_needed = (! flag_omit_frame_pointer
3698 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3699 and restore sp for alloca. So we can't eliminate
3700 the frame pointer in that case. At some point,
3701 we should improve this by emitting the
3702 sp-adjusting insns for this case. */
3703 || (current_function_calls_alloca
3704 && EXIT_IGNORE_STACK)
3705 || current_function_accesses_prior_frames
3706 || FRAME_POINTER_REQUIRED);
3708 num_eliminable = 0;
3710 #ifdef ELIMINABLE_REGS
3711 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3712 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3714 ep->from = ep1->from;
3715 ep->to = ep1->to;
3716 ep->can_eliminate = ep->can_eliminate_previous
3717 = (CAN_ELIMINATE (ep->from, ep->to)
3718 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3720 #else
3721 reg_eliminate[0].from = reg_eliminate_1[0].from;
3722 reg_eliminate[0].to = reg_eliminate_1[0].to;
3723 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3724 = ! frame_pointer_needed;
3725 #endif
3727 /* Count the number of eliminable registers and build the FROM and TO
3728 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3729 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3730 We depend on this. */
3731 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3733 num_eliminable += ep->can_eliminate;
3734 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3735 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3739 /* Kick all pseudos out of hard register REGNO.
3741 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3742 because we found we can't eliminate some register. In the case, no pseudos
3743 are allowed to be in the register, even if they are only in a block that
3744 doesn't require spill registers, unlike the case when we are spilling this
3745 hard reg to produce another spill register.
3747 Return nonzero if any pseudos needed to be kicked out. */
3749 static void
3750 spill_hard_reg (unsigned int regno, int cant_eliminate)
3752 int i;
3754 if (cant_eliminate)
3756 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3757 regs_ever_live[regno] = 1;
3760 /* Spill every pseudo reg that was allocated to this reg
3761 or to something that overlaps this reg. */
3763 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3764 if (reg_renumber[i] >= 0
3765 && (unsigned int) reg_renumber[i] <= regno
3766 && ((unsigned int) reg_renumber[i]
3767 + hard_regno_nregs[(unsigned int) reg_renumber[i]]
3768 [PSEUDO_REGNO_MODE (i)]
3769 > regno))
3770 SET_REGNO_REG_SET (&spilled_pseudos, i);
3773 /* After find_reload_regs has been run for all insn that need reloads,
3774 and/or spill_hard_regs was called, this function is used to actually
3775 spill pseudo registers and try to reallocate them. It also sets up the
3776 spill_regs array for use by choose_reload_regs. */
3778 static int
3779 finish_spills (int global)
3781 struct insn_chain *chain;
3782 int something_changed = 0;
3783 unsigned i;
3784 reg_set_iterator rsi;
3786 /* Build the spill_regs array for the function. */
3787 /* If there are some registers still to eliminate and one of the spill regs
3788 wasn't ever used before, additional stack space may have to be
3789 allocated to store this register. Thus, we may have changed the offset
3790 between the stack and frame pointers, so mark that something has changed.
3792 One might think that we need only set VAL to 1 if this is a call-used
3793 register. However, the set of registers that must be saved by the
3794 prologue is not identical to the call-used set. For example, the
3795 register used by the call insn for the return PC is a call-used register,
3796 but must be saved by the prologue. */
3798 n_spills = 0;
3799 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3800 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3802 spill_reg_order[i] = n_spills;
3803 spill_regs[n_spills++] = i;
3804 if (num_eliminable && ! regs_ever_live[i])
3805 something_changed = 1;
3806 regs_ever_live[i] = 1;
3808 else
3809 spill_reg_order[i] = -1;
3811 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
3813 /* Record the current hard register the pseudo is allocated to in
3814 pseudo_previous_regs so we avoid reallocating it to the same
3815 hard reg in a later pass. */
3816 gcc_assert (reg_renumber[i] >= 0);
3818 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3819 /* Mark it as no longer having a hard register home. */
3820 reg_renumber[i] = -1;
3821 /* We will need to scan everything again. */
3822 something_changed = 1;
3825 /* Retry global register allocation if possible. */
3826 if (global)
3828 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3829 /* For every insn that needs reloads, set the registers used as spill
3830 regs in pseudo_forbidden_regs for every pseudo live across the
3831 insn. */
3832 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3834 EXECUTE_IF_SET_IN_REG_SET
3835 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
3837 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3838 chain->used_spill_regs);
3840 EXECUTE_IF_SET_IN_REG_SET
3841 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
3843 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3844 chain->used_spill_regs);
3848 /* Retry allocating the spilled pseudos. For each reg, merge the
3849 various reg sets that indicate which hard regs can't be used,
3850 and call retry_global_alloc.
3851 We change spill_pseudos here to only contain pseudos that did not
3852 get a new hard register. */
3853 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3854 if (reg_old_renumber[i] != reg_renumber[i])
3856 HARD_REG_SET forbidden;
3857 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3858 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3859 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3860 retry_global_alloc (i, forbidden);
3861 if (reg_renumber[i] >= 0)
3862 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3866 /* Fix up the register information in the insn chain.
3867 This involves deleting those of the spilled pseudos which did not get
3868 a new hard register home from the live_{before,after} sets. */
3869 for (chain = reload_insn_chain; chain; chain = chain->next)
3871 HARD_REG_SET used_by_pseudos;
3872 HARD_REG_SET used_by_pseudos2;
3874 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
3875 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
3877 /* Mark any unallocated hard regs as available for spills. That
3878 makes inheritance work somewhat better. */
3879 if (chain->need_reload)
3881 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
3882 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
3883 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
3885 /* Save the old value for the sanity test below. */
3886 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
3888 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
3889 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
3890 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
3891 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
3893 /* Make sure we only enlarge the set. */
3894 GO_IF_HARD_REG_SUBSET (used_by_pseudos2, chain->used_spill_regs, ok);
3895 gcc_unreachable ();
3896 ok:;
3900 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
3901 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3903 int regno = reg_renumber[i];
3904 if (reg_old_renumber[i] == regno)
3905 continue;
3907 alter_reg (i, reg_old_renumber[i]);
3908 reg_old_renumber[i] = regno;
3909 if (dump_file)
3911 if (regno == -1)
3912 fprintf (dump_file, " Register %d now on stack.\n\n", i);
3913 else
3914 fprintf (dump_file, " Register %d now in %d.\n\n",
3915 i, reg_renumber[i]);
3919 return something_changed;
3922 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
3924 static void
3925 scan_paradoxical_subregs (rtx x)
3927 int i;
3928 const char *fmt;
3929 enum rtx_code code = GET_CODE (x);
3931 switch (code)
3933 case REG:
3934 case CONST_INT:
3935 case CONST:
3936 case SYMBOL_REF:
3937 case LABEL_REF:
3938 case CONST_DOUBLE:
3939 case CONST_VECTOR: /* shouldn't happen, but just in case. */
3940 case CC0:
3941 case PC:
3942 case USE:
3943 case CLOBBER:
3944 return;
3946 case SUBREG:
3947 if (REG_P (SUBREG_REG (x))
3948 && (GET_MODE_SIZE (GET_MODE (x))
3949 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
3950 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3951 = GET_MODE_SIZE (GET_MODE (x));
3952 return;
3954 default:
3955 break;
3958 fmt = GET_RTX_FORMAT (code);
3959 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3961 if (fmt[i] == 'e')
3962 scan_paradoxical_subregs (XEXP (x, i));
3963 else if (fmt[i] == 'E')
3965 int j;
3966 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3967 scan_paradoxical_subregs (XVECEXP (x, i, j));
3972 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
3973 examine all of the reload insns between PREV and NEXT exclusive, and
3974 annotate all that may trap. */
3976 static void
3977 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
3979 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3980 unsigned int trap_count;
3981 rtx i;
3983 if (note == NULL)
3984 return;
3986 if (may_trap_p (PATTERN (insn)))
3987 trap_count = 1;
3988 else
3990 remove_note (insn, note);
3991 trap_count = 0;
3994 for (i = NEXT_INSN (prev); i != next; i = NEXT_INSN (i))
3995 if (INSN_P (i) && i != insn && may_trap_p (PATTERN (i)))
3997 trap_count++;
3998 REG_NOTES (i)
3999 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (note, 0), REG_NOTES (i));
4003 /* Reload pseudo-registers into hard regs around each insn as needed.
4004 Additional register load insns are output before the insn that needs it
4005 and perhaps store insns after insns that modify the reloaded pseudo reg.
4007 reg_last_reload_reg and reg_reloaded_contents keep track of
4008 which registers are already available in reload registers.
4009 We update these for the reloads that we perform,
4010 as the insns are scanned. */
4012 static void
4013 reload_as_needed (int live_known)
4015 struct insn_chain *chain;
4016 #if defined (AUTO_INC_DEC)
4017 int i;
4018 #endif
4019 rtx x;
4021 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
4022 memset (spill_reg_store, 0, sizeof spill_reg_store);
4023 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
4024 INIT_REG_SET (&reg_has_output_reload);
4025 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4026 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
4028 set_initial_elim_offsets ();
4030 for (chain = reload_insn_chain; chain; chain = chain->next)
4032 rtx prev = 0;
4033 rtx insn = chain->insn;
4034 rtx old_next = NEXT_INSN (insn);
4036 /* If we pass a label, copy the offsets from the label information
4037 into the current offsets of each elimination. */
4038 if (LABEL_P (insn))
4039 set_offsets_for_label (insn);
4041 else if (INSN_P (insn))
4043 regset_head regs_to_forget;
4044 INIT_REG_SET (&regs_to_forget);
4045 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
4047 /* If this is a USE and CLOBBER of a MEM, ensure that any
4048 references to eliminable registers have been removed. */
4050 if ((GET_CODE (PATTERN (insn)) == USE
4051 || GET_CODE (PATTERN (insn)) == CLOBBER)
4052 && MEM_P (XEXP (PATTERN (insn), 0)))
4053 XEXP (XEXP (PATTERN (insn), 0), 0)
4054 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4055 GET_MODE (XEXP (PATTERN (insn), 0)),
4056 NULL_RTX);
4058 /* If we need to do register elimination processing, do so.
4059 This might delete the insn, in which case we are done. */
4060 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4062 eliminate_regs_in_insn (insn, 1);
4063 if (NOTE_P (insn))
4065 update_eliminable_offsets ();
4066 CLEAR_REG_SET (&regs_to_forget);
4067 continue;
4071 /* If need_elim is nonzero but need_reload is zero, one might think
4072 that we could simply set n_reloads to 0. However, find_reloads
4073 could have done some manipulation of the insn (such as swapping
4074 commutative operands), and these manipulations are lost during
4075 the first pass for every insn that needs register elimination.
4076 So the actions of find_reloads must be redone here. */
4078 if (! chain->need_elim && ! chain->need_reload
4079 && ! chain->need_operand_change)
4080 n_reloads = 0;
4081 /* First find the pseudo regs that must be reloaded for this insn.
4082 This info is returned in the tables reload_... (see reload.h).
4083 Also modify the body of INSN by substituting RELOAD
4084 rtx's for those pseudo regs. */
4085 else
4087 CLEAR_REG_SET (&reg_has_output_reload);
4088 CLEAR_HARD_REG_SET (reg_is_output_reload);
4090 find_reloads (insn, 1, spill_indirect_levels, live_known,
4091 spill_reg_order);
4094 if (n_reloads > 0)
4096 rtx next = NEXT_INSN (insn);
4097 rtx p;
4099 prev = PREV_INSN (insn);
4101 /* Now compute which reload regs to reload them into. Perhaps
4102 reusing reload regs from previous insns, or else output
4103 load insns to reload them. Maybe output store insns too.
4104 Record the choices of reload reg in reload_reg_rtx. */
4105 choose_reload_regs (chain);
4107 /* Merge any reloads that we didn't combine for fear of
4108 increasing the number of spill registers needed but now
4109 discover can be safely merged. */
4110 if (SMALL_REGISTER_CLASSES)
4111 merge_assigned_reloads (insn);
4113 /* Generate the insns to reload operands into or out of
4114 their reload regs. */
4115 emit_reload_insns (chain);
4117 /* Substitute the chosen reload regs from reload_reg_rtx
4118 into the insn's body (or perhaps into the bodies of other
4119 load and store insn that we just made for reloading
4120 and that we moved the structure into). */
4121 subst_reloads (insn);
4123 /* Adjust the exception region notes for loads and stores. */
4124 if (flag_non_call_exceptions && !CALL_P (insn))
4125 fixup_eh_region_note (insn, prev, next);
4127 /* If this was an ASM, make sure that all the reload insns
4128 we have generated are valid. If not, give an error
4129 and delete them. */
4130 if (asm_noperands (PATTERN (insn)) >= 0)
4131 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4132 if (p != insn && INSN_P (p)
4133 && GET_CODE (PATTERN (p)) != USE
4134 && (recog_memoized (p) < 0
4135 || (extract_insn (p), ! constrain_operands (1))))
4137 error_for_asm (insn,
4138 "%<asm%> operand requires "
4139 "impossible reload");
4140 delete_insn (p);
4144 if (num_eliminable && chain->need_elim)
4145 update_eliminable_offsets ();
4147 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4148 is no longer validly lying around to save a future reload.
4149 Note that this does not detect pseudos that were reloaded
4150 for this insn in order to be stored in
4151 (obeying register constraints). That is correct; such reload
4152 registers ARE still valid. */
4153 forget_marked_reloads (&regs_to_forget);
4154 CLEAR_REG_SET (&regs_to_forget);
4156 /* There may have been CLOBBER insns placed after INSN. So scan
4157 between INSN and NEXT and use them to forget old reloads. */
4158 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4159 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4160 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4162 #ifdef AUTO_INC_DEC
4163 /* Likewise for regs altered by auto-increment in this insn.
4164 REG_INC notes have been changed by reloading:
4165 find_reloads_address_1 records substitutions for them,
4166 which have been performed by subst_reloads above. */
4167 for (i = n_reloads - 1; i >= 0; i--)
4169 rtx in_reg = rld[i].in_reg;
4170 if (in_reg)
4172 enum rtx_code code = GET_CODE (in_reg);
4173 /* PRE_INC / PRE_DEC will have the reload register ending up
4174 with the same value as the stack slot, but that doesn't
4175 hold true for POST_INC / POST_DEC. Either we have to
4176 convert the memory access to a true POST_INC / POST_DEC,
4177 or we can't use the reload register for inheritance. */
4178 if ((code == POST_INC || code == POST_DEC)
4179 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4180 REGNO (rld[i].reg_rtx))
4181 /* Make sure it is the inc/dec pseudo, and not
4182 some other (e.g. output operand) pseudo. */
4183 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4184 == REGNO (XEXP (in_reg, 0))))
4187 rtx reload_reg = rld[i].reg_rtx;
4188 enum machine_mode mode = GET_MODE (reload_reg);
4189 int n = 0;
4190 rtx p;
4192 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4194 /* We really want to ignore REG_INC notes here, so
4195 use PATTERN (p) as argument to reg_set_p . */
4196 if (reg_set_p (reload_reg, PATTERN (p)))
4197 break;
4198 n = count_occurrences (PATTERN (p), reload_reg, 0);
4199 if (! n)
4200 continue;
4201 if (n == 1)
4203 n = validate_replace_rtx (reload_reg,
4204 gen_rtx_fmt_e (code,
4205 mode,
4206 reload_reg),
4209 /* We must also verify that the constraints
4210 are met after the replacement. */
4211 extract_insn (p);
4212 if (n)
4213 n = constrain_operands (1);
4214 else
4215 break;
4217 /* If the constraints were not met, then
4218 undo the replacement. */
4219 if (!n)
4221 validate_replace_rtx (gen_rtx_fmt_e (code,
4222 mode,
4223 reload_reg),
4224 reload_reg, p);
4225 break;
4229 break;
4231 if (n == 1)
4233 REG_NOTES (p)
4234 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4235 REG_NOTES (p));
4236 /* Mark this as having an output reload so that the
4237 REG_INC processing code below won't invalidate
4238 the reload for inheritance. */
4239 SET_HARD_REG_BIT (reg_is_output_reload,
4240 REGNO (reload_reg));
4241 SET_REGNO_REG_SET (&reg_has_output_reload,
4242 REGNO (XEXP (in_reg, 0)));
4244 else
4245 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4246 NULL);
4248 else if ((code == PRE_INC || code == PRE_DEC)
4249 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4250 REGNO (rld[i].reg_rtx))
4251 /* Make sure it is the inc/dec pseudo, and not
4252 some other (e.g. output operand) pseudo. */
4253 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4254 == REGNO (XEXP (in_reg, 0))))
4256 SET_HARD_REG_BIT (reg_is_output_reload,
4257 REGNO (rld[i].reg_rtx));
4258 SET_REGNO_REG_SET (&reg_has_output_reload,
4259 REGNO (XEXP (in_reg, 0)));
4263 /* If a pseudo that got a hard register is auto-incremented,
4264 we must purge records of copying it into pseudos without
4265 hard registers. */
4266 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4267 if (REG_NOTE_KIND (x) == REG_INC)
4269 /* See if this pseudo reg was reloaded in this insn.
4270 If so, its last-reload info is still valid
4271 because it is based on this insn's reload. */
4272 for (i = 0; i < n_reloads; i++)
4273 if (rld[i].out == XEXP (x, 0))
4274 break;
4276 if (i == n_reloads)
4277 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4279 #endif
4281 /* A reload reg's contents are unknown after a label. */
4282 if (LABEL_P (insn))
4283 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4285 /* Don't assume a reload reg is still good after a call insn
4286 if it is a call-used reg, or if it contains a value that will
4287 be partially clobbered by the call. */
4288 else if (CALL_P (insn))
4290 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4291 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4295 /* Clean up. */
4296 free (reg_last_reload_reg);
4297 CLEAR_REG_SET (&reg_has_output_reload);
4300 /* Discard all record of any value reloaded from X,
4301 or reloaded in X from someplace else;
4302 unless X is an output reload reg of the current insn.
4304 X may be a hard reg (the reload reg)
4305 or it may be a pseudo reg that was reloaded from.
4307 When DATA is non-NULL just mark the registers in regset
4308 to be forgotten later. */
4310 static void
4311 forget_old_reloads_1 (rtx x, rtx ignored ATTRIBUTE_UNUSED,
4312 void *data)
4314 unsigned int regno;
4315 unsigned int nr;
4316 regset regs = (regset) data;
4318 /* note_stores does give us subregs of hard regs,
4319 subreg_regno_offset requires a hard reg. */
4320 while (GET_CODE (x) == SUBREG)
4322 /* We ignore the subreg offset when calculating the regno,
4323 because we are using the entire underlying hard register
4324 below. */
4325 x = SUBREG_REG (x);
4328 if (!REG_P (x))
4329 return;
4331 regno = REGNO (x);
4333 if (regno >= FIRST_PSEUDO_REGISTER)
4334 nr = 1;
4335 else
4337 unsigned int i;
4339 nr = hard_regno_nregs[regno][GET_MODE (x)];
4340 /* Storing into a spilled-reg invalidates its contents.
4341 This can happen if a block-local pseudo is allocated to that reg
4342 and it wasn't spilled because this block's total need is 0.
4343 Then some insn might have an optional reload and use this reg. */
4344 if (!regs)
4345 for (i = 0; i < nr; i++)
4346 /* But don't do this if the reg actually serves as an output
4347 reload reg in the current instruction. */
4348 if (n_reloads == 0
4349 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4351 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4352 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, regno + i);
4353 spill_reg_store[regno + i] = 0;
4357 if (regs)
4358 while (nr-- > 0)
4359 SET_REGNO_REG_SET (regs, regno + nr);
4360 else
4362 /* Since value of X has changed,
4363 forget any value previously copied from it. */
4365 while (nr-- > 0)
4366 /* But don't forget a copy if this is the output reload
4367 that establishes the copy's validity. */
4368 if (n_reloads == 0
4369 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4370 reg_last_reload_reg[regno + nr] = 0;
4374 /* Forget the reloads marked in regset by previous function. */
4375 static void
4376 forget_marked_reloads (regset regs)
4378 unsigned int reg;
4379 reg_set_iterator rsi;
4380 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4382 if (reg < FIRST_PSEUDO_REGISTER
4383 /* But don't do this if the reg actually serves as an output
4384 reload reg in the current instruction. */
4385 && (n_reloads == 0
4386 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4388 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4389 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, reg);
4390 spill_reg_store[reg] = 0;
4392 if (n_reloads == 0
4393 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4394 reg_last_reload_reg[reg] = 0;
4398 /* The following HARD_REG_SETs indicate when each hard register is
4399 used for a reload of various parts of the current insn. */
4401 /* If reg is unavailable for all reloads. */
4402 static HARD_REG_SET reload_reg_unavailable;
4403 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4404 static HARD_REG_SET reload_reg_used;
4405 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4406 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4407 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4408 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4409 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4410 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4411 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4412 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4413 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4414 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4415 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4416 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4417 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4418 static HARD_REG_SET reload_reg_used_in_op_addr;
4419 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4420 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4421 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4422 static HARD_REG_SET reload_reg_used_in_insn;
4423 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4424 static HARD_REG_SET reload_reg_used_in_other_addr;
4426 /* If reg is in use as a reload reg for any sort of reload. */
4427 static HARD_REG_SET reload_reg_used_at_all;
4429 /* If reg is use as an inherited reload. We just mark the first register
4430 in the group. */
4431 static HARD_REG_SET reload_reg_used_for_inherit;
4433 /* Records which hard regs are used in any way, either as explicit use or
4434 by being allocated to a pseudo during any point of the current insn. */
4435 static HARD_REG_SET reg_used_in_insn;
4437 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4438 TYPE. MODE is used to indicate how many consecutive regs are
4439 actually used. */
4441 static void
4442 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4443 enum machine_mode mode)
4445 unsigned int nregs = hard_regno_nregs[regno][mode];
4446 unsigned int i;
4448 for (i = regno; i < nregs + regno; i++)
4450 switch (type)
4452 case RELOAD_OTHER:
4453 SET_HARD_REG_BIT (reload_reg_used, i);
4454 break;
4456 case RELOAD_FOR_INPUT_ADDRESS:
4457 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4458 break;
4460 case RELOAD_FOR_INPADDR_ADDRESS:
4461 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4462 break;
4464 case RELOAD_FOR_OUTPUT_ADDRESS:
4465 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4466 break;
4468 case RELOAD_FOR_OUTADDR_ADDRESS:
4469 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4470 break;
4472 case RELOAD_FOR_OPERAND_ADDRESS:
4473 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4474 break;
4476 case RELOAD_FOR_OPADDR_ADDR:
4477 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4478 break;
4480 case RELOAD_FOR_OTHER_ADDRESS:
4481 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4482 break;
4484 case RELOAD_FOR_INPUT:
4485 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4486 break;
4488 case RELOAD_FOR_OUTPUT:
4489 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4490 break;
4492 case RELOAD_FOR_INSN:
4493 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4494 break;
4497 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4501 /* Similarly, but show REGNO is no longer in use for a reload. */
4503 static void
4504 clear_reload_reg_in_use (unsigned int regno, int opnum,
4505 enum reload_type type, enum machine_mode mode)
4507 unsigned int nregs = hard_regno_nregs[regno][mode];
4508 unsigned int start_regno, end_regno, r;
4509 int i;
4510 /* A complication is that for some reload types, inheritance might
4511 allow multiple reloads of the same types to share a reload register.
4512 We set check_opnum if we have to check only reloads with the same
4513 operand number, and check_any if we have to check all reloads. */
4514 int check_opnum = 0;
4515 int check_any = 0;
4516 HARD_REG_SET *used_in_set;
4518 switch (type)
4520 case RELOAD_OTHER:
4521 used_in_set = &reload_reg_used;
4522 break;
4524 case RELOAD_FOR_INPUT_ADDRESS:
4525 used_in_set = &reload_reg_used_in_input_addr[opnum];
4526 break;
4528 case RELOAD_FOR_INPADDR_ADDRESS:
4529 check_opnum = 1;
4530 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4531 break;
4533 case RELOAD_FOR_OUTPUT_ADDRESS:
4534 used_in_set = &reload_reg_used_in_output_addr[opnum];
4535 break;
4537 case RELOAD_FOR_OUTADDR_ADDRESS:
4538 check_opnum = 1;
4539 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4540 break;
4542 case RELOAD_FOR_OPERAND_ADDRESS:
4543 used_in_set = &reload_reg_used_in_op_addr;
4544 break;
4546 case RELOAD_FOR_OPADDR_ADDR:
4547 check_any = 1;
4548 used_in_set = &reload_reg_used_in_op_addr_reload;
4549 break;
4551 case RELOAD_FOR_OTHER_ADDRESS:
4552 used_in_set = &reload_reg_used_in_other_addr;
4553 check_any = 1;
4554 break;
4556 case RELOAD_FOR_INPUT:
4557 used_in_set = &reload_reg_used_in_input[opnum];
4558 break;
4560 case RELOAD_FOR_OUTPUT:
4561 used_in_set = &reload_reg_used_in_output[opnum];
4562 break;
4564 case RELOAD_FOR_INSN:
4565 used_in_set = &reload_reg_used_in_insn;
4566 break;
4567 default:
4568 gcc_unreachable ();
4570 /* We resolve conflicts with remaining reloads of the same type by
4571 excluding the intervals of reload registers by them from the
4572 interval of freed reload registers. Since we only keep track of
4573 one set of interval bounds, we might have to exclude somewhat
4574 more than what would be necessary if we used a HARD_REG_SET here.
4575 But this should only happen very infrequently, so there should
4576 be no reason to worry about it. */
4578 start_regno = regno;
4579 end_regno = regno + nregs;
4580 if (check_opnum || check_any)
4582 for (i = n_reloads - 1; i >= 0; i--)
4584 if (rld[i].when_needed == type
4585 && (check_any || rld[i].opnum == opnum)
4586 && rld[i].reg_rtx)
4588 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4589 unsigned int conflict_end
4590 = (conflict_start
4591 + hard_regno_nregs[conflict_start][rld[i].mode]);
4593 /* If there is an overlap with the first to-be-freed register,
4594 adjust the interval start. */
4595 if (conflict_start <= start_regno && conflict_end > start_regno)
4596 start_regno = conflict_end;
4597 /* Otherwise, if there is a conflict with one of the other
4598 to-be-freed registers, adjust the interval end. */
4599 if (conflict_start > start_regno && conflict_start < end_regno)
4600 end_regno = conflict_start;
4605 for (r = start_regno; r < end_regno; r++)
4606 CLEAR_HARD_REG_BIT (*used_in_set, r);
4609 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4610 specified by OPNUM and TYPE. */
4612 static int
4613 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4615 int i;
4617 /* In use for a RELOAD_OTHER means it's not available for anything. */
4618 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4619 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4620 return 0;
4622 switch (type)
4624 case RELOAD_OTHER:
4625 /* In use for anything means we can't use it for RELOAD_OTHER. */
4626 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4627 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4628 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4629 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4630 return 0;
4632 for (i = 0; i < reload_n_operands; i++)
4633 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4634 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4635 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4636 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4637 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4638 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4639 return 0;
4641 return 1;
4643 case RELOAD_FOR_INPUT:
4644 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4645 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4646 return 0;
4648 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4649 return 0;
4651 /* If it is used for some other input, can't use it. */
4652 for (i = 0; i < reload_n_operands; i++)
4653 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4654 return 0;
4656 /* If it is used in a later operand's address, can't use it. */
4657 for (i = opnum + 1; i < reload_n_operands; i++)
4658 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4659 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4660 return 0;
4662 return 1;
4664 case RELOAD_FOR_INPUT_ADDRESS:
4665 /* Can't use a register if it is used for an input address for this
4666 operand or used as an input in an earlier one. */
4667 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4668 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4669 return 0;
4671 for (i = 0; i < opnum; i++)
4672 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4673 return 0;
4675 return 1;
4677 case RELOAD_FOR_INPADDR_ADDRESS:
4678 /* Can't use a register if it is used for an input address
4679 for this operand or used as an input in an earlier
4680 one. */
4681 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4682 return 0;
4684 for (i = 0; i < opnum; i++)
4685 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4686 return 0;
4688 return 1;
4690 case RELOAD_FOR_OUTPUT_ADDRESS:
4691 /* Can't use a register if it is used for an output address for this
4692 operand or used as an output in this or a later operand. Note
4693 that multiple output operands are emitted in reverse order, so
4694 the conflicting ones are those with lower indices. */
4695 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4696 return 0;
4698 for (i = 0; i <= opnum; i++)
4699 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4700 return 0;
4702 return 1;
4704 case RELOAD_FOR_OUTADDR_ADDRESS:
4705 /* Can't use a register if it is used for an output address
4706 for this operand or used as an output in this or a
4707 later operand. Note that multiple output operands are
4708 emitted in reverse order, so the conflicting ones are
4709 those with lower indices. */
4710 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4711 return 0;
4713 for (i = 0; i <= opnum; i++)
4714 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4715 return 0;
4717 return 1;
4719 case RELOAD_FOR_OPERAND_ADDRESS:
4720 for (i = 0; i < reload_n_operands; i++)
4721 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4722 return 0;
4724 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4725 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4727 case RELOAD_FOR_OPADDR_ADDR:
4728 for (i = 0; i < reload_n_operands; i++)
4729 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4730 return 0;
4732 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4734 case RELOAD_FOR_OUTPUT:
4735 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4736 outputs, or an operand address for this or an earlier output.
4737 Note that multiple output operands are emitted in reverse order,
4738 so the conflicting ones are those with higher indices. */
4739 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4740 return 0;
4742 for (i = 0; i < reload_n_operands; i++)
4743 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4744 return 0;
4746 for (i = opnum; i < reload_n_operands; i++)
4747 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4748 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4749 return 0;
4751 return 1;
4753 case RELOAD_FOR_INSN:
4754 for (i = 0; i < reload_n_operands; i++)
4755 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4756 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4757 return 0;
4759 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4760 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4762 case RELOAD_FOR_OTHER_ADDRESS:
4763 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4765 default:
4766 gcc_unreachable ();
4770 /* Return 1 if the value in reload reg REGNO, as used by a reload
4771 needed for the part of the insn specified by OPNUM and TYPE,
4772 is still available in REGNO at the end of the insn.
4774 We can assume that the reload reg was already tested for availability
4775 at the time it is needed, and we should not check this again,
4776 in case the reg has already been marked in use. */
4778 static int
4779 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4781 int i;
4783 switch (type)
4785 case RELOAD_OTHER:
4786 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4787 its value must reach the end. */
4788 return 1;
4790 /* If this use is for part of the insn,
4791 its value reaches if no subsequent part uses the same register.
4792 Just like the above function, don't try to do this with lots
4793 of fallthroughs. */
4795 case RELOAD_FOR_OTHER_ADDRESS:
4796 /* Here we check for everything else, since these don't conflict
4797 with anything else and everything comes later. */
4799 for (i = 0; i < reload_n_operands; i++)
4800 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4801 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4802 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4803 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4804 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4805 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4806 return 0;
4808 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4809 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4810 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4811 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4813 case RELOAD_FOR_INPUT_ADDRESS:
4814 case RELOAD_FOR_INPADDR_ADDRESS:
4815 /* Similar, except that we check only for this and subsequent inputs
4816 and the address of only subsequent inputs and we do not need
4817 to check for RELOAD_OTHER objects since they are known not to
4818 conflict. */
4820 for (i = opnum; i < reload_n_operands; i++)
4821 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4822 return 0;
4824 for (i = opnum + 1; i < reload_n_operands; i++)
4825 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4826 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4827 return 0;
4829 for (i = 0; i < reload_n_operands; i++)
4830 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4831 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4832 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4833 return 0;
4835 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4836 return 0;
4838 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4839 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4840 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4842 case RELOAD_FOR_INPUT:
4843 /* Similar to input address, except we start at the next operand for
4844 both input and input address and we do not check for
4845 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4846 would conflict. */
4848 for (i = opnum + 1; i < reload_n_operands; i++)
4849 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4850 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4851 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4852 return 0;
4854 /* ... fall through ... */
4856 case RELOAD_FOR_OPERAND_ADDRESS:
4857 /* Check outputs and their addresses. */
4859 for (i = 0; i < reload_n_operands; i++)
4860 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4861 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4862 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4863 return 0;
4865 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
4867 case RELOAD_FOR_OPADDR_ADDR:
4868 for (i = 0; i < reload_n_operands; i++)
4869 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4870 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4871 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4872 return 0;
4874 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4875 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4876 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4878 case RELOAD_FOR_INSN:
4879 /* These conflict with other outputs with RELOAD_OTHER. So
4880 we need only check for output addresses. */
4882 opnum = reload_n_operands;
4884 /* ... fall through ... */
4886 case RELOAD_FOR_OUTPUT:
4887 case RELOAD_FOR_OUTPUT_ADDRESS:
4888 case RELOAD_FOR_OUTADDR_ADDRESS:
4889 /* We already know these can't conflict with a later output. So the
4890 only thing to check are later output addresses.
4891 Note that multiple output operands are emitted in reverse order,
4892 so the conflicting ones are those with lower indices. */
4893 for (i = 0; i < opnum; i++)
4894 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4895 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4896 return 0;
4898 return 1;
4900 default:
4901 gcc_unreachable ();
4906 /* Returns whether R1 and R2 are uniquely chained: the value of one
4907 is used by the other, and that value is not used by any other
4908 reload for this insn. This is used to partially undo the decision
4909 made in find_reloads when in the case of multiple
4910 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
4911 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
4912 reloads. This code tries to avoid the conflict created by that
4913 change. It might be cleaner to explicitly keep track of which
4914 RELOAD_FOR_OPADDR_ADDR reload is associated with which
4915 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
4916 this after the fact. */
4917 static bool
4918 reloads_unique_chain_p (int r1, int r2)
4920 int i;
4922 /* We only check input reloads. */
4923 if (! rld[r1].in || ! rld[r2].in)
4924 return false;
4926 /* Avoid anything with output reloads. */
4927 if (rld[r1].out || rld[r2].out)
4928 return false;
4930 /* "chained" means one reload is a component of the other reload,
4931 not the same as the other reload. */
4932 if (rld[r1].opnum != rld[r2].opnum
4933 || rtx_equal_p (rld[r1].in, rld[r2].in)
4934 || rld[r1].optional || rld[r2].optional
4935 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
4936 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
4937 return false;
4939 for (i = 0; i < n_reloads; i ++)
4940 /* Look for input reloads that aren't our two */
4941 if (i != r1 && i != r2 && rld[i].in)
4943 /* If our reload is mentioned at all, it isn't a simple chain. */
4944 if (reg_mentioned_p (rld[r1].in, rld[i].in))
4945 return false;
4947 return true;
4950 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
4951 Return 0 otherwise.
4953 This function uses the same algorithm as reload_reg_free_p above. */
4955 static int
4956 reloads_conflict (int r1, int r2)
4958 enum reload_type r1_type = rld[r1].when_needed;
4959 enum reload_type r2_type = rld[r2].when_needed;
4960 int r1_opnum = rld[r1].opnum;
4961 int r2_opnum = rld[r2].opnum;
4963 /* RELOAD_OTHER conflicts with everything. */
4964 if (r2_type == RELOAD_OTHER)
4965 return 1;
4967 /* Otherwise, check conflicts differently for each type. */
4969 switch (r1_type)
4971 case RELOAD_FOR_INPUT:
4972 return (r2_type == RELOAD_FOR_INSN
4973 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
4974 || r2_type == RELOAD_FOR_OPADDR_ADDR
4975 || r2_type == RELOAD_FOR_INPUT
4976 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
4977 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
4978 && r2_opnum > r1_opnum));
4980 case RELOAD_FOR_INPUT_ADDRESS:
4981 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
4982 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4984 case RELOAD_FOR_INPADDR_ADDRESS:
4985 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
4986 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
4988 case RELOAD_FOR_OUTPUT_ADDRESS:
4989 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
4990 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4992 case RELOAD_FOR_OUTADDR_ADDRESS:
4993 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
4994 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
4996 case RELOAD_FOR_OPERAND_ADDRESS:
4997 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
4998 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
4999 && !reloads_unique_chain_p (r1, r2)));
5001 case RELOAD_FOR_OPADDR_ADDR:
5002 return (r2_type == RELOAD_FOR_INPUT
5003 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5005 case RELOAD_FOR_OUTPUT:
5006 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5007 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5008 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5009 && r2_opnum >= r1_opnum));
5011 case RELOAD_FOR_INSN:
5012 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5013 || r2_type == RELOAD_FOR_INSN
5014 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5016 case RELOAD_FOR_OTHER_ADDRESS:
5017 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5019 case RELOAD_OTHER:
5020 return 1;
5022 default:
5023 gcc_unreachable ();
5027 /* Indexed by reload number, 1 if incoming value
5028 inherited from previous insns. */
5029 static char reload_inherited[MAX_RELOADS];
5031 /* For an inherited reload, this is the insn the reload was inherited from,
5032 if we know it. Otherwise, this is 0. */
5033 static rtx reload_inheritance_insn[MAX_RELOADS];
5035 /* If nonzero, this is a place to get the value of the reload,
5036 rather than using reload_in. */
5037 static rtx reload_override_in[MAX_RELOADS];
5039 /* For each reload, the hard register number of the register used,
5040 or -1 if we did not need a register for this reload. */
5041 static int reload_spill_index[MAX_RELOADS];
5043 /* Subroutine of free_for_value_p, used to check a single register.
5044 START_REGNO is the starting regno of the full reload register
5045 (possibly comprising multiple hard registers) that we are considering. */
5047 static int
5048 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
5049 enum reload_type type, rtx value, rtx out,
5050 int reloadnum, int ignore_address_reloads)
5052 int time1;
5053 /* Set if we see an input reload that must not share its reload register
5054 with any new earlyclobber, but might otherwise share the reload
5055 register with an output or input-output reload. */
5056 int check_earlyclobber = 0;
5057 int i;
5058 int copy = 0;
5060 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5061 return 0;
5063 if (out == const0_rtx)
5065 copy = 1;
5066 out = NULL_RTX;
5069 /* We use some pseudo 'time' value to check if the lifetimes of the
5070 new register use would overlap with the one of a previous reload
5071 that is not read-only or uses a different value.
5072 The 'time' used doesn't have to be linear in any shape or form, just
5073 monotonic.
5074 Some reload types use different 'buckets' for each operand.
5075 So there are MAX_RECOG_OPERANDS different time values for each
5076 such reload type.
5077 We compute TIME1 as the time when the register for the prospective
5078 new reload ceases to be live, and TIME2 for each existing
5079 reload as the time when that the reload register of that reload
5080 becomes live.
5081 Where there is little to be gained by exact lifetime calculations,
5082 we just make conservative assumptions, i.e. a longer lifetime;
5083 this is done in the 'default:' cases. */
5084 switch (type)
5086 case RELOAD_FOR_OTHER_ADDRESS:
5087 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
5088 time1 = copy ? 0 : 1;
5089 break;
5090 case RELOAD_OTHER:
5091 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5092 break;
5093 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5094 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5095 respectively, to the time values for these, we get distinct time
5096 values. To get distinct time values for each operand, we have to
5097 multiply opnum by at least three. We round that up to four because
5098 multiply by four is often cheaper. */
5099 case RELOAD_FOR_INPADDR_ADDRESS:
5100 time1 = opnum * 4 + 2;
5101 break;
5102 case RELOAD_FOR_INPUT_ADDRESS:
5103 time1 = opnum * 4 + 3;
5104 break;
5105 case RELOAD_FOR_INPUT:
5106 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5107 executes (inclusive). */
5108 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5109 break;
5110 case RELOAD_FOR_OPADDR_ADDR:
5111 /* opnum * 4 + 4
5112 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5113 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5114 break;
5115 case RELOAD_FOR_OPERAND_ADDRESS:
5116 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5117 is executed. */
5118 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5119 break;
5120 case RELOAD_FOR_OUTADDR_ADDRESS:
5121 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5122 break;
5123 case RELOAD_FOR_OUTPUT_ADDRESS:
5124 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5125 break;
5126 default:
5127 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5130 for (i = 0; i < n_reloads; i++)
5132 rtx reg = rld[i].reg_rtx;
5133 if (reg && REG_P (reg)
5134 && ((unsigned) regno - true_regnum (reg)
5135 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5136 && i != reloadnum)
5138 rtx other_input = rld[i].in;
5140 /* If the other reload loads the same input value, that
5141 will not cause a conflict only if it's loading it into
5142 the same register. */
5143 if (true_regnum (reg) != start_regno)
5144 other_input = NULL_RTX;
5145 if (! other_input || ! rtx_equal_p (other_input, value)
5146 || rld[i].out || out)
5148 int time2;
5149 switch (rld[i].when_needed)
5151 case RELOAD_FOR_OTHER_ADDRESS:
5152 time2 = 0;
5153 break;
5154 case RELOAD_FOR_INPADDR_ADDRESS:
5155 /* find_reloads makes sure that a
5156 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5157 by at most one - the first -
5158 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5159 address reload is inherited, the address address reload
5160 goes away, so we can ignore this conflict. */
5161 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5162 && ignore_address_reloads
5163 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5164 Then the address address is still needed to store
5165 back the new address. */
5166 && ! rld[reloadnum].out)
5167 continue;
5168 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5169 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5170 reloads go away. */
5171 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5172 && ignore_address_reloads
5173 /* Unless we are reloading an auto_inc expression. */
5174 && ! rld[reloadnum].out)
5175 continue;
5176 time2 = rld[i].opnum * 4 + 2;
5177 break;
5178 case RELOAD_FOR_INPUT_ADDRESS:
5179 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5180 && ignore_address_reloads
5181 && ! rld[reloadnum].out)
5182 continue;
5183 time2 = rld[i].opnum * 4 + 3;
5184 break;
5185 case RELOAD_FOR_INPUT:
5186 time2 = rld[i].opnum * 4 + 4;
5187 check_earlyclobber = 1;
5188 break;
5189 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5190 == MAX_RECOG_OPERAND * 4 */
5191 case RELOAD_FOR_OPADDR_ADDR:
5192 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5193 && ignore_address_reloads
5194 && ! rld[reloadnum].out)
5195 continue;
5196 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5197 break;
5198 case RELOAD_FOR_OPERAND_ADDRESS:
5199 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5200 check_earlyclobber = 1;
5201 break;
5202 case RELOAD_FOR_INSN:
5203 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5204 break;
5205 case RELOAD_FOR_OUTPUT:
5206 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5207 instruction is executed. */
5208 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5209 break;
5210 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5211 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5212 value. */
5213 case RELOAD_FOR_OUTADDR_ADDRESS:
5214 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5215 && ignore_address_reloads
5216 && ! rld[reloadnum].out)
5217 continue;
5218 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5219 break;
5220 case RELOAD_FOR_OUTPUT_ADDRESS:
5221 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5222 break;
5223 case RELOAD_OTHER:
5224 /* If there is no conflict in the input part, handle this
5225 like an output reload. */
5226 if (! rld[i].in || rtx_equal_p (other_input, value))
5228 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5229 /* Earlyclobbered outputs must conflict with inputs. */
5230 if (earlyclobber_operand_p (rld[i].out))
5231 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5233 break;
5235 time2 = 1;
5236 /* RELOAD_OTHER might be live beyond instruction execution,
5237 but this is not obvious when we set time2 = 1. So check
5238 here if there might be a problem with the new reload
5239 clobbering the register used by the RELOAD_OTHER. */
5240 if (out)
5241 return 0;
5242 break;
5243 default:
5244 return 0;
5246 if ((time1 >= time2
5247 && (! rld[i].in || rld[i].out
5248 || ! rtx_equal_p (other_input, value)))
5249 || (out && rld[reloadnum].out_reg
5250 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5251 return 0;
5256 /* Earlyclobbered outputs must conflict with inputs. */
5257 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5258 return 0;
5260 return 1;
5263 /* Return 1 if the value in reload reg REGNO, as used by a reload
5264 needed for the part of the insn specified by OPNUM and TYPE,
5265 may be used to load VALUE into it.
5267 MODE is the mode in which the register is used, this is needed to
5268 determine how many hard regs to test.
5270 Other read-only reloads with the same value do not conflict
5271 unless OUT is nonzero and these other reloads have to live while
5272 output reloads live.
5273 If OUT is CONST0_RTX, this is a special case: it means that the
5274 test should not be for using register REGNO as reload register, but
5275 for copying from register REGNO into the reload register.
5277 RELOADNUM is the number of the reload we want to load this value for;
5278 a reload does not conflict with itself.
5280 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5281 reloads that load an address for the very reload we are considering.
5283 The caller has to make sure that there is no conflict with the return
5284 register. */
5286 static int
5287 free_for_value_p (int regno, enum machine_mode mode, int opnum,
5288 enum reload_type type, rtx value, rtx out, int reloadnum,
5289 int ignore_address_reloads)
5291 int nregs = hard_regno_nregs[regno][mode];
5292 while (nregs-- > 0)
5293 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5294 value, out, reloadnum,
5295 ignore_address_reloads))
5296 return 0;
5297 return 1;
5300 /* Return nonzero if the rtx X is invariant over the current function. */
5301 /* ??? Actually, the places where we use this expect exactly what is
5302 tested here, and not everything that is function invariant. In
5303 particular, the frame pointer and arg pointer are special cased;
5304 pic_offset_table_rtx is not, and we must not spill these things to
5305 memory. */
5308 function_invariant_p (rtx x)
5310 if (CONSTANT_P (x))
5311 return 1;
5312 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
5313 return 1;
5314 if (GET_CODE (x) == PLUS
5315 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
5316 && CONSTANT_P (XEXP (x, 1)))
5317 return 1;
5318 return 0;
5321 /* Determine whether the reload reg X overlaps any rtx'es used for
5322 overriding inheritance. Return nonzero if so. */
5324 static int
5325 conflicts_with_override (rtx x)
5327 int i;
5328 for (i = 0; i < n_reloads; i++)
5329 if (reload_override_in[i]
5330 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5331 return 1;
5332 return 0;
5335 /* Give an error message saying we failed to find a reload for INSN,
5336 and clear out reload R. */
5337 static void
5338 failed_reload (rtx insn, int r)
5340 if (asm_noperands (PATTERN (insn)) < 0)
5341 /* It's the compiler's fault. */
5342 fatal_insn ("could not find a spill register", insn);
5344 /* It's the user's fault; the operand's mode and constraint
5345 don't match. Disable this reload so we don't crash in final. */
5346 error_for_asm (insn,
5347 "%<asm%> operand constraint incompatible with operand size");
5348 rld[r].in = 0;
5349 rld[r].out = 0;
5350 rld[r].reg_rtx = 0;
5351 rld[r].optional = 1;
5352 rld[r].secondary_p = 1;
5355 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5356 for reload R. If it's valid, get an rtx for it. Return nonzero if
5357 successful. */
5358 static int
5359 set_reload_reg (int i, int r)
5361 int regno;
5362 rtx reg = spill_reg_rtx[i];
5364 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5365 spill_reg_rtx[i] = reg
5366 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5368 regno = true_regnum (reg);
5370 /* Detect when the reload reg can't hold the reload mode.
5371 This used to be one `if', but Sequent compiler can't handle that. */
5372 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5374 enum machine_mode test_mode = VOIDmode;
5375 if (rld[r].in)
5376 test_mode = GET_MODE (rld[r].in);
5377 /* If rld[r].in has VOIDmode, it means we will load it
5378 in whatever mode the reload reg has: to wit, rld[r].mode.
5379 We have already tested that for validity. */
5380 /* Aside from that, we need to test that the expressions
5381 to reload from or into have modes which are valid for this
5382 reload register. Otherwise the reload insns would be invalid. */
5383 if (! (rld[r].in != 0 && test_mode != VOIDmode
5384 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5385 if (! (rld[r].out != 0
5386 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5388 /* The reg is OK. */
5389 last_spill_reg = i;
5391 /* Mark as in use for this insn the reload regs we use
5392 for this. */
5393 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5394 rld[r].when_needed, rld[r].mode);
5396 rld[r].reg_rtx = reg;
5397 reload_spill_index[r] = spill_regs[i];
5398 return 1;
5401 return 0;
5404 /* Find a spill register to use as a reload register for reload R.
5405 LAST_RELOAD is nonzero if this is the last reload for the insn being
5406 processed.
5408 Set rld[R].reg_rtx to the register allocated.
5410 We return 1 if successful, or 0 if we couldn't find a spill reg and
5411 we didn't change anything. */
5413 static int
5414 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5415 int last_reload)
5417 int i, pass, count;
5419 /* If we put this reload ahead, thinking it is a group,
5420 then insist on finding a group. Otherwise we can grab a
5421 reg that some other reload needs.
5422 (That can happen when we have a 68000 DATA_OR_FP_REG
5423 which is a group of data regs or one fp reg.)
5424 We need not be so restrictive if there are no more reloads
5425 for this insn.
5427 ??? Really it would be nicer to have smarter handling
5428 for that kind of reg class, where a problem like this is normal.
5429 Perhaps those classes should be avoided for reloading
5430 by use of more alternatives. */
5432 int force_group = rld[r].nregs > 1 && ! last_reload;
5434 /* If we want a single register and haven't yet found one,
5435 take any reg in the right class and not in use.
5436 If we want a consecutive group, here is where we look for it.
5438 We use two passes so we can first look for reload regs to
5439 reuse, which are already in use for other reloads in this insn,
5440 and only then use additional registers.
5441 I think that maximizing reuse is needed to make sure we don't
5442 run out of reload regs. Suppose we have three reloads, and
5443 reloads A and B can share regs. These need two regs.
5444 Suppose A and B are given different regs.
5445 That leaves none for C. */
5446 for (pass = 0; pass < 2; pass++)
5448 /* I is the index in spill_regs.
5449 We advance it round-robin between insns to use all spill regs
5450 equally, so that inherited reloads have a chance
5451 of leapfrogging each other. */
5453 i = last_spill_reg;
5455 for (count = 0; count < n_spills; count++)
5457 int class = (int) rld[r].class;
5458 int regnum;
5460 i++;
5461 if (i >= n_spills)
5462 i -= n_spills;
5463 regnum = spill_regs[i];
5465 if ((reload_reg_free_p (regnum, rld[r].opnum,
5466 rld[r].when_needed)
5467 || (rld[r].in
5468 /* We check reload_reg_used to make sure we
5469 don't clobber the return register. */
5470 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5471 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5472 rld[r].when_needed, rld[r].in,
5473 rld[r].out, r, 1)))
5474 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5475 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5476 /* Look first for regs to share, then for unshared. But
5477 don't share regs used for inherited reloads; they are
5478 the ones we want to preserve. */
5479 && (pass
5480 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5481 regnum)
5482 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5483 regnum))))
5485 int nr = hard_regno_nregs[regnum][rld[r].mode];
5486 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5487 (on 68000) got us two FP regs. If NR is 1,
5488 we would reject both of them. */
5489 if (force_group)
5490 nr = rld[r].nregs;
5491 /* If we need only one reg, we have already won. */
5492 if (nr == 1)
5494 /* But reject a single reg if we demand a group. */
5495 if (force_group)
5496 continue;
5497 break;
5499 /* Otherwise check that as many consecutive regs as we need
5500 are available here. */
5501 while (nr > 1)
5503 int regno = regnum + nr - 1;
5504 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5505 && spill_reg_order[regno] >= 0
5506 && reload_reg_free_p (regno, rld[r].opnum,
5507 rld[r].when_needed)))
5508 break;
5509 nr--;
5511 if (nr == 1)
5512 break;
5516 /* If we found something on pass 1, omit pass 2. */
5517 if (count < n_spills)
5518 break;
5521 /* We should have found a spill register by now. */
5522 if (count >= n_spills)
5523 return 0;
5525 /* I is the index in SPILL_REG_RTX of the reload register we are to
5526 allocate. Get an rtx for it and find its register number. */
5528 return set_reload_reg (i, r);
5531 /* Initialize all the tables needed to allocate reload registers.
5532 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5533 is the array we use to restore the reg_rtx field for every reload. */
5535 static void
5536 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5538 int i;
5540 for (i = 0; i < n_reloads; i++)
5541 rld[i].reg_rtx = save_reload_reg_rtx[i];
5543 memset (reload_inherited, 0, MAX_RELOADS);
5544 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5545 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5547 CLEAR_HARD_REG_SET (reload_reg_used);
5548 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5549 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5550 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5551 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5552 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5554 CLEAR_HARD_REG_SET (reg_used_in_insn);
5556 HARD_REG_SET tmp;
5557 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5558 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5559 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5560 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5561 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5562 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5565 for (i = 0; i < reload_n_operands; i++)
5567 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5568 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5569 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5570 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5571 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5572 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5575 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5577 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5579 for (i = 0; i < n_reloads; i++)
5580 /* If we have already decided to use a certain register,
5581 don't use it in another way. */
5582 if (rld[i].reg_rtx)
5583 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5584 rld[i].when_needed, rld[i].mode);
5587 /* Assign hard reg targets for the pseudo-registers we must reload
5588 into hard regs for this insn.
5589 Also output the instructions to copy them in and out of the hard regs.
5591 For machines with register classes, we are responsible for
5592 finding a reload reg in the proper class. */
5594 static void
5595 choose_reload_regs (struct insn_chain *chain)
5597 rtx insn = chain->insn;
5598 int i, j;
5599 unsigned int max_group_size = 1;
5600 enum reg_class group_class = NO_REGS;
5601 int pass, win, inheritance;
5603 rtx save_reload_reg_rtx[MAX_RELOADS];
5605 /* In order to be certain of getting the registers we need,
5606 we must sort the reloads into order of increasing register class.
5607 Then our grabbing of reload registers will parallel the process
5608 that provided the reload registers.
5610 Also note whether any of the reloads wants a consecutive group of regs.
5611 If so, record the maximum size of the group desired and what
5612 register class contains all the groups needed by this insn. */
5614 for (j = 0; j < n_reloads; j++)
5616 reload_order[j] = j;
5617 reload_spill_index[j] = -1;
5619 if (rld[j].nregs > 1)
5621 max_group_size = MAX (rld[j].nregs, max_group_size);
5622 group_class
5623 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5626 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5629 if (n_reloads > 1)
5630 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5632 /* If -O, try first with inheritance, then turning it off.
5633 If not -O, don't do inheritance.
5634 Using inheritance when not optimizing leads to paradoxes
5635 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5636 because one side of the comparison might be inherited. */
5637 win = 0;
5638 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5640 choose_reload_regs_init (chain, save_reload_reg_rtx);
5642 /* Process the reloads in order of preference just found.
5643 Beyond this point, subregs can be found in reload_reg_rtx.
5645 This used to look for an existing reloaded home for all of the
5646 reloads, and only then perform any new reloads. But that could lose
5647 if the reloads were done out of reg-class order because a later
5648 reload with a looser constraint might have an old home in a register
5649 needed by an earlier reload with a tighter constraint.
5651 To solve this, we make two passes over the reloads, in the order
5652 described above. In the first pass we try to inherit a reload
5653 from a previous insn. If there is a later reload that needs a
5654 class that is a proper subset of the class being processed, we must
5655 also allocate a spill register during the first pass.
5657 Then make a second pass over the reloads to allocate any reloads
5658 that haven't been given registers yet. */
5660 for (j = 0; j < n_reloads; j++)
5662 int r = reload_order[j];
5663 rtx search_equiv = NULL_RTX;
5665 /* Ignore reloads that got marked inoperative. */
5666 if (rld[r].out == 0 && rld[r].in == 0
5667 && ! rld[r].secondary_p)
5668 continue;
5670 /* If find_reloads chose to use reload_in or reload_out as a reload
5671 register, we don't need to chose one. Otherwise, try even if it
5672 found one since we might save an insn if we find the value lying
5673 around.
5674 Try also when reload_in is a pseudo without a hard reg. */
5675 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5676 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5677 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5678 && !MEM_P (rld[r].in)
5679 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5680 continue;
5682 #if 0 /* No longer needed for correct operation.
5683 It might give better code, or might not; worth an experiment? */
5684 /* If this is an optional reload, we can't inherit from earlier insns
5685 until we are sure that any non-optional reloads have been allocated.
5686 The following code takes advantage of the fact that optional reloads
5687 are at the end of reload_order. */
5688 if (rld[r].optional != 0)
5689 for (i = 0; i < j; i++)
5690 if ((rld[reload_order[i]].out != 0
5691 || rld[reload_order[i]].in != 0
5692 || rld[reload_order[i]].secondary_p)
5693 && ! rld[reload_order[i]].optional
5694 && rld[reload_order[i]].reg_rtx == 0)
5695 allocate_reload_reg (chain, reload_order[i], 0);
5696 #endif
5698 /* First see if this pseudo is already available as reloaded
5699 for a previous insn. We cannot try to inherit for reloads
5700 that are smaller than the maximum number of registers needed
5701 for groups unless the register we would allocate cannot be used
5702 for the groups.
5704 We could check here to see if this is a secondary reload for
5705 an object that is already in a register of the desired class.
5706 This would avoid the need for the secondary reload register.
5707 But this is complex because we can't easily determine what
5708 objects might want to be loaded via this reload. So let a
5709 register be allocated here. In `emit_reload_insns' we suppress
5710 one of the loads in the case described above. */
5712 if (inheritance)
5714 int byte = 0;
5715 int regno = -1;
5716 enum machine_mode mode = VOIDmode;
5718 if (rld[r].in == 0)
5720 else if (REG_P (rld[r].in))
5722 regno = REGNO (rld[r].in);
5723 mode = GET_MODE (rld[r].in);
5725 else if (REG_P (rld[r].in_reg))
5727 regno = REGNO (rld[r].in_reg);
5728 mode = GET_MODE (rld[r].in_reg);
5730 else if (GET_CODE (rld[r].in_reg) == SUBREG
5731 && REG_P (SUBREG_REG (rld[r].in_reg)))
5733 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5734 if (regno < FIRST_PSEUDO_REGISTER)
5735 regno = subreg_regno (rld[r].in_reg);
5736 else
5737 byte = SUBREG_BYTE (rld[r].in_reg);
5738 mode = GET_MODE (rld[r].in_reg);
5740 #ifdef AUTO_INC_DEC
5741 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
5742 && REG_P (XEXP (rld[r].in_reg, 0)))
5744 regno = REGNO (XEXP (rld[r].in_reg, 0));
5745 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5746 rld[r].out = rld[r].in;
5748 #endif
5749 #if 0
5750 /* This won't work, since REGNO can be a pseudo reg number.
5751 Also, it takes much more hair to keep track of all the things
5752 that can invalidate an inherited reload of part of a pseudoreg. */
5753 else if (GET_CODE (rld[r].in) == SUBREG
5754 && REG_P (SUBREG_REG (rld[r].in)))
5755 regno = subreg_regno (rld[r].in);
5756 #endif
5758 if (regno >= 0
5759 && reg_last_reload_reg[regno] != 0
5760 #ifdef CANNOT_CHANGE_MODE_CLASS
5761 /* Verify that the register it's in can be used in
5762 mode MODE. */
5763 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
5764 GET_MODE (reg_last_reload_reg[regno]),
5765 mode)
5766 #endif
5769 enum reg_class class = rld[r].class, last_class;
5770 rtx last_reg = reg_last_reload_reg[regno];
5771 enum machine_mode need_mode;
5773 i = REGNO (last_reg);
5774 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5775 last_class = REGNO_REG_CLASS (i);
5777 if (byte == 0)
5778 need_mode = mode;
5779 else
5780 need_mode
5781 = smallest_mode_for_size (GET_MODE_BITSIZE (mode)
5782 + byte * BITS_PER_UNIT,
5783 GET_MODE_CLASS (mode));
5785 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5786 >= GET_MODE_SIZE (need_mode))
5787 && reg_reloaded_contents[i] == regno
5788 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5789 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5790 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5791 /* Even if we can't use this register as a reload
5792 register, we might use it for reload_override_in,
5793 if copying it to the desired class is cheap
5794 enough. */
5795 || ((REGISTER_MOVE_COST (mode, last_class, class)
5796 < MEMORY_MOVE_COST (mode, class, 1))
5797 && (secondary_reload_class (1, class, mode,
5798 last_reg)
5799 == NO_REGS)
5800 #ifdef SECONDARY_MEMORY_NEEDED
5801 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5802 mode)
5803 #endif
5806 && (rld[r].nregs == max_group_size
5807 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5809 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5810 rld[r].when_needed, rld[r].in,
5811 const0_rtx, r, 1))
5813 /* If a group is needed, verify that all the subsequent
5814 registers still have their values intact. */
5815 int nr = hard_regno_nregs[i][rld[r].mode];
5816 int k;
5818 for (k = 1; k < nr; k++)
5819 if (reg_reloaded_contents[i + k] != regno
5820 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5821 break;
5823 if (k == nr)
5825 int i1;
5826 int bad_for_class;
5828 last_reg = (GET_MODE (last_reg) == mode
5829 ? last_reg : gen_rtx_REG (mode, i));
5831 bad_for_class = 0;
5832 for (k = 0; k < nr; k++)
5833 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5834 i+k);
5836 /* We found a register that contains the
5837 value we need. If this register is the
5838 same as an `earlyclobber' operand of the
5839 current insn, just mark it as a place to
5840 reload from since we can't use it as the
5841 reload register itself. */
5843 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5844 if (reg_overlap_mentioned_for_reload_p
5845 (reg_last_reload_reg[regno],
5846 reload_earlyclobbers[i1]))
5847 break;
5849 if (i1 != n_earlyclobbers
5850 || ! (free_for_value_p (i, rld[r].mode,
5851 rld[r].opnum,
5852 rld[r].when_needed, rld[r].in,
5853 rld[r].out, r, 1))
5854 /* Don't use it if we'd clobber a pseudo reg. */
5855 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
5856 && rld[r].out
5857 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
5858 /* Don't clobber the frame pointer. */
5859 || (i == HARD_FRAME_POINTER_REGNUM
5860 && frame_pointer_needed
5861 && rld[r].out)
5862 /* Don't really use the inherited spill reg
5863 if we need it wider than we've got it. */
5864 || (GET_MODE_SIZE (rld[r].mode)
5865 > GET_MODE_SIZE (mode))
5866 || bad_for_class
5868 /* If find_reloads chose reload_out as reload
5869 register, stay with it - that leaves the
5870 inherited register for subsequent reloads. */
5871 || (rld[r].out && rld[r].reg_rtx
5872 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
5874 if (! rld[r].optional)
5876 reload_override_in[r] = last_reg;
5877 reload_inheritance_insn[r]
5878 = reg_reloaded_insn[i];
5881 else
5883 int k;
5884 /* We can use this as a reload reg. */
5885 /* Mark the register as in use for this part of
5886 the insn. */
5887 mark_reload_reg_in_use (i,
5888 rld[r].opnum,
5889 rld[r].when_needed,
5890 rld[r].mode);
5891 rld[r].reg_rtx = last_reg;
5892 reload_inherited[r] = 1;
5893 reload_inheritance_insn[r]
5894 = reg_reloaded_insn[i];
5895 reload_spill_index[r] = i;
5896 for (k = 0; k < nr; k++)
5897 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
5898 i + k);
5905 /* Here's another way to see if the value is already lying around. */
5906 if (inheritance
5907 && rld[r].in != 0
5908 && ! reload_inherited[r]
5909 && rld[r].out == 0
5910 && (CONSTANT_P (rld[r].in)
5911 || GET_CODE (rld[r].in) == PLUS
5912 || REG_P (rld[r].in)
5913 || MEM_P (rld[r].in))
5914 && (rld[r].nregs == max_group_size
5915 || ! reg_classes_intersect_p (rld[r].class, group_class)))
5916 search_equiv = rld[r].in;
5917 /* If this is an output reload from a simple move insn, look
5918 if an equivalence for the input is available. */
5919 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
5921 rtx set = single_set (insn);
5923 if (set
5924 && rtx_equal_p (rld[r].out, SET_DEST (set))
5925 && CONSTANT_P (SET_SRC (set)))
5926 search_equiv = SET_SRC (set);
5929 if (search_equiv)
5931 rtx equiv
5932 = find_equiv_reg (search_equiv, insn, rld[r].class,
5933 -1, NULL, 0, rld[r].mode);
5934 int regno = 0;
5936 if (equiv != 0)
5938 if (REG_P (equiv))
5939 regno = REGNO (equiv);
5940 else
5942 /* This must be a SUBREG of a hard register.
5943 Make a new REG since this might be used in an
5944 address and not all machines support SUBREGs
5945 there. */
5946 gcc_assert (GET_CODE (equiv) == SUBREG);
5947 regno = subreg_regno (equiv);
5948 equiv = gen_rtx_REG (rld[r].mode, regno);
5949 /* If we choose EQUIV as the reload register, but the
5950 loop below decides to cancel the inheritance, we'll
5951 end up reloading EQUIV in rld[r].mode, not the mode
5952 it had originally. That isn't safe when EQUIV isn't
5953 available as a spill register since its value might
5954 still be live at this point. */
5955 for (i = regno; i < regno + (int) rld[r].nregs; i++)
5956 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
5957 equiv = 0;
5961 /* If we found a spill reg, reject it unless it is free
5962 and of the desired class. */
5963 if (equiv != 0)
5965 int regs_used = 0;
5966 int bad_for_class = 0;
5967 int max_regno = regno + rld[r].nregs;
5969 for (i = regno; i < max_regno; i++)
5971 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
5973 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5977 if ((regs_used
5978 && ! free_for_value_p (regno, rld[r].mode,
5979 rld[r].opnum, rld[r].when_needed,
5980 rld[r].in, rld[r].out, r, 1))
5981 || bad_for_class)
5982 equiv = 0;
5985 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
5986 equiv = 0;
5988 /* We found a register that contains the value we need.
5989 If this register is the same as an `earlyclobber' operand
5990 of the current insn, just mark it as a place to reload from
5991 since we can't use it as the reload register itself. */
5993 if (equiv != 0)
5994 for (i = 0; i < n_earlyclobbers; i++)
5995 if (reg_overlap_mentioned_for_reload_p (equiv,
5996 reload_earlyclobbers[i]))
5998 if (! rld[r].optional)
5999 reload_override_in[r] = equiv;
6000 equiv = 0;
6001 break;
6004 /* If the equiv register we have found is explicitly clobbered
6005 in the current insn, it depends on the reload type if we
6006 can use it, use it for reload_override_in, or not at all.
6007 In particular, we then can't use EQUIV for a
6008 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6010 if (equiv != 0)
6012 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
6013 switch (rld[r].when_needed)
6015 case RELOAD_FOR_OTHER_ADDRESS:
6016 case RELOAD_FOR_INPADDR_ADDRESS:
6017 case RELOAD_FOR_INPUT_ADDRESS:
6018 case RELOAD_FOR_OPADDR_ADDR:
6019 break;
6020 case RELOAD_OTHER:
6021 case RELOAD_FOR_INPUT:
6022 case RELOAD_FOR_OPERAND_ADDRESS:
6023 if (! rld[r].optional)
6024 reload_override_in[r] = equiv;
6025 /* Fall through. */
6026 default:
6027 equiv = 0;
6028 break;
6030 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
6031 switch (rld[r].when_needed)
6033 case RELOAD_FOR_OTHER_ADDRESS:
6034 case RELOAD_FOR_INPADDR_ADDRESS:
6035 case RELOAD_FOR_INPUT_ADDRESS:
6036 case RELOAD_FOR_OPADDR_ADDR:
6037 case RELOAD_FOR_OPERAND_ADDRESS:
6038 case RELOAD_FOR_INPUT:
6039 break;
6040 case RELOAD_OTHER:
6041 if (! rld[r].optional)
6042 reload_override_in[r] = equiv;
6043 /* Fall through. */
6044 default:
6045 equiv = 0;
6046 break;
6050 /* If we found an equivalent reg, say no code need be generated
6051 to load it, and use it as our reload reg. */
6052 if (equiv != 0
6053 && (regno != HARD_FRAME_POINTER_REGNUM
6054 || !frame_pointer_needed))
6056 int nr = hard_regno_nregs[regno][rld[r].mode];
6057 int k;
6058 rld[r].reg_rtx = equiv;
6059 reload_inherited[r] = 1;
6061 /* If reg_reloaded_valid is not set for this register,
6062 there might be a stale spill_reg_store lying around.
6063 We must clear it, since otherwise emit_reload_insns
6064 might delete the store. */
6065 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6066 spill_reg_store[regno] = NULL_RTX;
6067 /* If any of the hard registers in EQUIV are spill
6068 registers, mark them as in use for this insn. */
6069 for (k = 0; k < nr; k++)
6071 i = spill_reg_order[regno + k];
6072 if (i >= 0)
6074 mark_reload_reg_in_use (regno, rld[r].opnum,
6075 rld[r].when_needed,
6076 rld[r].mode);
6077 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6078 regno + k);
6084 /* If we found a register to use already, or if this is an optional
6085 reload, we are done. */
6086 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6087 continue;
6089 #if 0
6090 /* No longer needed for correct operation. Might or might
6091 not give better code on the average. Want to experiment? */
6093 /* See if there is a later reload that has a class different from our
6094 class that intersects our class or that requires less register
6095 than our reload. If so, we must allocate a register to this
6096 reload now, since that reload might inherit a previous reload
6097 and take the only available register in our class. Don't do this
6098 for optional reloads since they will force all previous reloads
6099 to be allocated. Also don't do this for reloads that have been
6100 turned off. */
6102 for (i = j + 1; i < n_reloads; i++)
6104 int s = reload_order[i];
6106 if ((rld[s].in == 0 && rld[s].out == 0
6107 && ! rld[s].secondary_p)
6108 || rld[s].optional)
6109 continue;
6111 if ((rld[s].class != rld[r].class
6112 && reg_classes_intersect_p (rld[r].class,
6113 rld[s].class))
6114 || rld[s].nregs < rld[r].nregs)
6115 break;
6118 if (i == n_reloads)
6119 continue;
6121 allocate_reload_reg (chain, r, j == n_reloads - 1);
6122 #endif
6125 /* Now allocate reload registers for anything non-optional that
6126 didn't get one yet. */
6127 for (j = 0; j < n_reloads; j++)
6129 int r = reload_order[j];
6131 /* Ignore reloads that got marked inoperative. */
6132 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6133 continue;
6135 /* Skip reloads that already have a register allocated or are
6136 optional. */
6137 if (rld[r].reg_rtx != 0 || rld[r].optional)
6138 continue;
6140 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6141 break;
6144 /* If that loop got all the way, we have won. */
6145 if (j == n_reloads)
6147 win = 1;
6148 break;
6151 /* Loop around and try without any inheritance. */
6154 if (! win)
6156 /* First undo everything done by the failed attempt
6157 to allocate with inheritance. */
6158 choose_reload_regs_init (chain, save_reload_reg_rtx);
6160 /* Some sanity tests to verify that the reloads found in the first
6161 pass are identical to the ones we have now. */
6162 gcc_assert (chain->n_reloads == n_reloads);
6164 for (i = 0; i < n_reloads; i++)
6166 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6167 continue;
6168 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6169 for (j = 0; j < n_spills; j++)
6170 if (spill_regs[j] == chain->rld[i].regno)
6171 if (! set_reload_reg (j, i))
6172 failed_reload (chain->insn, i);
6176 /* If we thought we could inherit a reload, because it seemed that
6177 nothing else wanted the same reload register earlier in the insn,
6178 verify that assumption, now that all reloads have been assigned.
6179 Likewise for reloads where reload_override_in has been set. */
6181 /* If doing expensive optimizations, do one preliminary pass that doesn't
6182 cancel any inheritance, but removes reloads that have been needed only
6183 for reloads that we know can be inherited. */
6184 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6186 for (j = 0; j < n_reloads; j++)
6188 int r = reload_order[j];
6189 rtx check_reg;
6190 if (reload_inherited[r] && rld[r].reg_rtx)
6191 check_reg = rld[r].reg_rtx;
6192 else if (reload_override_in[r]
6193 && (REG_P (reload_override_in[r])
6194 || GET_CODE (reload_override_in[r]) == SUBREG))
6195 check_reg = reload_override_in[r];
6196 else
6197 continue;
6198 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6199 rld[r].opnum, rld[r].when_needed, rld[r].in,
6200 (reload_inherited[r]
6201 ? rld[r].out : const0_rtx),
6202 r, 1))
6204 if (pass)
6205 continue;
6206 reload_inherited[r] = 0;
6207 reload_override_in[r] = 0;
6209 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6210 reload_override_in, then we do not need its related
6211 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6212 likewise for other reload types.
6213 We handle this by removing a reload when its only replacement
6214 is mentioned in reload_in of the reload we are going to inherit.
6215 A special case are auto_inc expressions; even if the input is
6216 inherited, we still need the address for the output. We can
6217 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6218 If we succeeded removing some reload and we are doing a preliminary
6219 pass just to remove such reloads, make another pass, since the
6220 removal of one reload might allow us to inherit another one. */
6221 else if (rld[r].in
6222 && rld[r].out != rld[r].in
6223 && remove_address_replacements (rld[r].in) && pass)
6224 pass = 2;
6228 /* Now that reload_override_in is known valid,
6229 actually override reload_in. */
6230 for (j = 0; j < n_reloads; j++)
6231 if (reload_override_in[j])
6232 rld[j].in = reload_override_in[j];
6234 /* If this reload won't be done because it has been canceled or is
6235 optional and not inherited, clear reload_reg_rtx so other
6236 routines (such as subst_reloads) don't get confused. */
6237 for (j = 0; j < n_reloads; j++)
6238 if (rld[j].reg_rtx != 0
6239 && ((rld[j].optional && ! reload_inherited[j])
6240 || (rld[j].in == 0 && rld[j].out == 0
6241 && ! rld[j].secondary_p)))
6243 int regno = true_regnum (rld[j].reg_rtx);
6245 if (spill_reg_order[regno] >= 0)
6246 clear_reload_reg_in_use (regno, rld[j].opnum,
6247 rld[j].when_needed, rld[j].mode);
6248 rld[j].reg_rtx = 0;
6249 reload_spill_index[j] = -1;
6252 /* Record which pseudos and which spill regs have output reloads. */
6253 for (j = 0; j < n_reloads; j++)
6255 int r = reload_order[j];
6257 i = reload_spill_index[r];
6259 /* I is nonneg if this reload uses a register.
6260 If rld[r].reg_rtx is 0, this is an optional reload
6261 that we opted to ignore. */
6262 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
6263 && rld[r].reg_rtx != 0)
6265 int nregno = REGNO (rld[r].out_reg);
6266 int nr = 1;
6268 if (nregno < FIRST_PSEUDO_REGISTER)
6269 nr = hard_regno_nregs[nregno][rld[r].mode];
6271 while (--nr >= 0)
6272 SET_REGNO_REG_SET (&reg_has_output_reload,
6273 nregno + nr);
6275 if (i >= 0)
6277 nr = hard_regno_nregs[i][rld[r].mode];
6278 while (--nr >= 0)
6279 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6282 gcc_assert (rld[r].when_needed == RELOAD_OTHER
6283 || rld[r].when_needed == RELOAD_FOR_OUTPUT
6284 || rld[r].when_needed == RELOAD_FOR_INSN);
6289 /* Deallocate the reload register for reload R. This is called from
6290 remove_address_replacements. */
6292 void
6293 deallocate_reload_reg (int r)
6295 int regno;
6297 if (! rld[r].reg_rtx)
6298 return;
6299 regno = true_regnum (rld[r].reg_rtx);
6300 rld[r].reg_rtx = 0;
6301 if (spill_reg_order[regno] >= 0)
6302 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6303 rld[r].mode);
6304 reload_spill_index[r] = -1;
6307 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
6308 reloads of the same item for fear that we might not have enough reload
6309 registers. However, normally they will get the same reload register
6310 and hence actually need not be loaded twice.
6312 Here we check for the most common case of this phenomenon: when we have
6313 a number of reloads for the same object, each of which were allocated
6314 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6315 reload, and is not modified in the insn itself. If we find such,
6316 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6317 This will not increase the number of spill registers needed and will
6318 prevent redundant code. */
6320 static void
6321 merge_assigned_reloads (rtx insn)
6323 int i, j;
6325 /* Scan all the reloads looking for ones that only load values and
6326 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6327 assigned and not modified by INSN. */
6329 for (i = 0; i < n_reloads; i++)
6331 int conflicting_input = 0;
6332 int max_input_address_opnum = -1;
6333 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6335 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6336 || rld[i].out != 0 || rld[i].reg_rtx == 0
6337 || reg_set_p (rld[i].reg_rtx, insn))
6338 continue;
6340 /* Look at all other reloads. Ensure that the only use of this
6341 reload_reg_rtx is in a reload that just loads the same value
6342 as we do. Note that any secondary reloads must be of the identical
6343 class since the values, modes, and result registers are the
6344 same, so we need not do anything with any secondary reloads. */
6346 for (j = 0; j < n_reloads; j++)
6348 if (i == j || rld[j].reg_rtx == 0
6349 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6350 rld[i].reg_rtx))
6351 continue;
6353 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6354 && rld[j].opnum > max_input_address_opnum)
6355 max_input_address_opnum = rld[j].opnum;
6357 /* If the reload regs aren't exactly the same (e.g, different modes)
6358 or if the values are different, we can't merge this reload.
6359 But if it is an input reload, we might still merge
6360 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6362 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6363 || rld[j].out != 0 || rld[j].in == 0
6364 || ! rtx_equal_p (rld[i].in, rld[j].in))
6366 if (rld[j].when_needed != RELOAD_FOR_INPUT
6367 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6368 || rld[i].opnum > rld[j].opnum)
6369 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6370 break;
6371 conflicting_input = 1;
6372 if (min_conflicting_input_opnum > rld[j].opnum)
6373 min_conflicting_input_opnum = rld[j].opnum;
6377 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6378 we, in fact, found any matching reloads. */
6380 if (j == n_reloads
6381 && max_input_address_opnum <= min_conflicting_input_opnum)
6383 gcc_assert (rld[i].when_needed != RELOAD_FOR_OUTPUT);
6385 for (j = 0; j < n_reloads; j++)
6386 if (i != j && rld[j].reg_rtx != 0
6387 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6388 && (! conflicting_input
6389 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6390 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6392 rld[i].when_needed = RELOAD_OTHER;
6393 rld[j].in = 0;
6394 reload_spill_index[j] = -1;
6395 transfer_replacements (i, j);
6398 /* If this is now RELOAD_OTHER, look for any reloads that
6399 load parts of this operand and set them to
6400 RELOAD_FOR_OTHER_ADDRESS if they were for inputs,
6401 RELOAD_OTHER for outputs. Note that this test is
6402 equivalent to looking for reloads for this operand
6403 number.
6405 We must take special care with RELOAD_FOR_OUTPUT_ADDRESS;
6406 it may share registers with a RELOAD_FOR_INPUT, so we can
6407 not change it to RELOAD_FOR_OTHER_ADDRESS. We should
6408 never need to, since we do not modify RELOAD_FOR_OUTPUT.
6410 It is possible that the RELOAD_FOR_OPERAND_ADDRESS
6411 instruction is assigned the same register as the earlier
6412 RELOAD_FOR_OTHER_ADDRESS instruction. Merging these two
6413 instructions will cause the RELOAD_FOR_OTHER_ADDRESS
6414 instruction to be deleted later on. */
6416 if (rld[i].when_needed == RELOAD_OTHER)
6417 for (j = 0; j < n_reloads; j++)
6418 if (rld[j].in != 0
6419 && rld[j].when_needed != RELOAD_OTHER
6420 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6421 && rld[j].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
6422 && rld[j].when_needed != RELOAD_FOR_OPERAND_ADDRESS
6423 && (! conflicting_input
6424 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6425 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6426 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6427 rld[i].in))
6429 int k;
6431 rld[j].when_needed
6432 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6433 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6434 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6436 /* Check to see if we accidentally converted two
6437 reloads that use the same reload register with
6438 different inputs to the same type. If so, the
6439 resulting code won't work. */
6440 if (rld[j].reg_rtx)
6441 for (k = 0; k < j; k++)
6442 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6443 || rld[k].when_needed != rld[j].when_needed
6444 || !rtx_equal_p (rld[k].reg_rtx,
6445 rld[j].reg_rtx)
6446 || rtx_equal_p (rld[k].in,
6447 rld[j].in));
6453 /* These arrays are filled by emit_reload_insns and its subroutines. */
6454 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6455 static rtx other_input_address_reload_insns = 0;
6456 static rtx other_input_reload_insns = 0;
6457 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6458 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6459 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6460 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6461 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6462 static rtx operand_reload_insns = 0;
6463 static rtx other_operand_reload_insns = 0;
6464 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6466 /* Values to be put in spill_reg_store are put here first. */
6467 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6468 static HARD_REG_SET reg_reloaded_died;
6470 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
6471 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
6472 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
6473 adjusted register, and return true. Otherwise, return false. */
6474 static bool
6475 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
6476 enum reg_class new_class,
6477 enum machine_mode new_mode)
6480 rtx reg;
6482 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
6484 unsigned regno = REGNO (reg);
6486 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
6487 continue;
6488 if (GET_MODE (reg) != new_mode)
6490 if (!HARD_REGNO_MODE_OK (regno, new_mode))
6491 continue;
6492 if (hard_regno_nregs[regno][new_mode]
6493 > hard_regno_nregs[regno][GET_MODE (reg)])
6494 continue;
6495 reg = reload_adjust_reg_for_mode (reg, new_mode);
6497 *reload_reg = reg;
6498 return true;
6500 return false;
6503 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
6504 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
6505 nonzero, if that is suitable. On success, change *RELOAD_REG to the
6506 adjusted register, and return true. Otherwise, return false. */
6507 static bool
6508 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
6509 enum insn_code icode)
6512 enum reg_class new_class = scratch_reload_class (icode);
6513 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
6515 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
6516 new_class, new_mode);
6519 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6520 has the number J. OLD contains the value to be used as input. */
6522 static void
6523 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6524 rtx old, int j)
6526 rtx insn = chain->insn;
6527 rtx reloadreg = rl->reg_rtx;
6528 rtx oldequiv_reg = 0;
6529 rtx oldequiv = 0;
6530 int special = 0;
6531 enum machine_mode mode;
6532 rtx *where;
6534 /* Determine the mode to reload in.
6535 This is very tricky because we have three to choose from.
6536 There is the mode the insn operand wants (rl->inmode).
6537 There is the mode of the reload register RELOADREG.
6538 There is the intrinsic mode of the operand, which we could find
6539 by stripping some SUBREGs.
6540 It turns out that RELOADREG's mode is irrelevant:
6541 we can change that arbitrarily.
6543 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6544 then the reload reg may not support QImode moves, so use SImode.
6545 If foo is in memory due to spilling a pseudo reg, this is safe,
6546 because the QImode value is in the least significant part of a
6547 slot big enough for a SImode. If foo is some other sort of
6548 memory reference, then it is impossible to reload this case,
6549 so previous passes had better make sure this never happens.
6551 Then consider a one-word union which has SImode and one of its
6552 members is a float, being fetched as (SUBREG:SF union:SI).
6553 We must fetch that as SFmode because we could be loading into
6554 a float-only register. In this case OLD's mode is correct.
6556 Consider an immediate integer: it has VOIDmode. Here we need
6557 to get a mode from something else.
6559 In some cases, there is a fourth mode, the operand's
6560 containing mode. If the insn specifies a containing mode for
6561 this operand, it overrides all others.
6563 I am not sure whether the algorithm here is always right,
6564 but it does the right things in those cases. */
6566 mode = GET_MODE (old);
6567 if (mode == VOIDmode)
6568 mode = rl->inmode;
6570 /* delete_output_reload is only invoked properly if old contains
6571 the original pseudo register. Since this is replaced with a
6572 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6573 find the pseudo in RELOAD_IN_REG. */
6574 if (reload_override_in[j]
6575 && REG_P (rl->in_reg))
6577 oldequiv = old;
6578 old = rl->in_reg;
6580 if (oldequiv == 0)
6581 oldequiv = old;
6582 else if (REG_P (oldequiv))
6583 oldequiv_reg = oldequiv;
6584 else if (GET_CODE (oldequiv) == SUBREG)
6585 oldequiv_reg = SUBREG_REG (oldequiv);
6587 /* If we are reloading from a register that was recently stored in
6588 with an output-reload, see if we can prove there was
6589 actually no need to store the old value in it. */
6591 if (optimize && REG_P (oldequiv)
6592 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6593 && spill_reg_store[REGNO (oldequiv)]
6594 && REG_P (old)
6595 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6596 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6597 rl->out_reg)))
6598 delete_output_reload (insn, j, REGNO (oldequiv));
6600 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6601 then load RELOADREG from OLDEQUIV. Note that we cannot use
6602 gen_lowpart_common since it can do the wrong thing when
6603 RELOADREG has a multi-word mode. Note that RELOADREG
6604 must always be a REG here. */
6606 if (GET_MODE (reloadreg) != mode)
6607 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6608 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6609 oldequiv = SUBREG_REG (oldequiv);
6610 if (GET_MODE (oldequiv) != VOIDmode
6611 && mode != GET_MODE (oldequiv))
6612 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6614 /* Switch to the right place to emit the reload insns. */
6615 switch (rl->when_needed)
6617 case RELOAD_OTHER:
6618 where = &other_input_reload_insns;
6619 break;
6620 case RELOAD_FOR_INPUT:
6621 where = &input_reload_insns[rl->opnum];
6622 break;
6623 case RELOAD_FOR_INPUT_ADDRESS:
6624 where = &input_address_reload_insns[rl->opnum];
6625 break;
6626 case RELOAD_FOR_INPADDR_ADDRESS:
6627 where = &inpaddr_address_reload_insns[rl->opnum];
6628 break;
6629 case RELOAD_FOR_OUTPUT_ADDRESS:
6630 where = &output_address_reload_insns[rl->opnum];
6631 break;
6632 case RELOAD_FOR_OUTADDR_ADDRESS:
6633 where = &outaddr_address_reload_insns[rl->opnum];
6634 break;
6635 case RELOAD_FOR_OPERAND_ADDRESS:
6636 where = &operand_reload_insns;
6637 break;
6638 case RELOAD_FOR_OPADDR_ADDR:
6639 where = &other_operand_reload_insns;
6640 break;
6641 case RELOAD_FOR_OTHER_ADDRESS:
6642 where = &other_input_address_reload_insns;
6643 break;
6644 default:
6645 gcc_unreachable ();
6648 push_to_sequence (*where);
6650 /* Auto-increment addresses must be reloaded in a special way. */
6651 if (rl->out && ! rl->out_reg)
6653 /* We are not going to bother supporting the case where a
6654 incremented register can't be copied directly from
6655 OLDEQUIV since this seems highly unlikely. */
6656 gcc_assert (rl->secondary_in_reload < 0);
6658 if (reload_inherited[j])
6659 oldequiv = reloadreg;
6661 old = XEXP (rl->in_reg, 0);
6663 if (optimize && REG_P (oldequiv)
6664 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6665 && spill_reg_store[REGNO (oldequiv)]
6666 && REG_P (old)
6667 && (dead_or_set_p (insn,
6668 spill_reg_stored_to[REGNO (oldequiv)])
6669 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6670 old)))
6671 delete_output_reload (insn, j, REGNO (oldequiv));
6673 /* Prevent normal processing of this reload. */
6674 special = 1;
6675 /* Output a special code sequence for this case. */
6676 new_spill_reg_store[REGNO (reloadreg)]
6677 = inc_for_reload (reloadreg, oldequiv, rl->out,
6678 rl->inc);
6681 /* If we are reloading a pseudo-register that was set by the previous
6682 insn, see if we can get rid of that pseudo-register entirely
6683 by redirecting the previous insn into our reload register. */
6685 else if (optimize && REG_P (old)
6686 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6687 && dead_or_set_p (insn, old)
6688 /* This is unsafe if some other reload
6689 uses the same reg first. */
6690 && ! conflicts_with_override (reloadreg)
6691 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6692 rl->when_needed, old, rl->out, j, 0))
6694 rtx temp = PREV_INSN (insn);
6695 while (temp && NOTE_P (temp))
6696 temp = PREV_INSN (temp);
6697 if (temp
6698 && NONJUMP_INSN_P (temp)
6699 && GET_CODE (PATTERN (temp)) == SET
6700 && SET_DEST (PATTERN (temp)) == old
6701 /* Make sure we can access insn_operand_constraint. */
6702 && asm_noperands (PATTERN (temp)) < 0
6703 /* This is unsafe if operand occurs more than once in current
6704 insn. Perhaps some occurrences aren't reloaded. */
6705 && count_occurrences (PATTERN (insn), old, 0) == 1)
6707 rtx old = SET_DEST (PATTERN (temp));
6708 /* Store into the reload register instead of the pseudo. */
6709 SET_DEST (PATTERN (temp)) = reloadreg;
6711 /* Verify that resulting insn is valid. */
6712 extract_insn (temp);
6713 if (constrain_operands (1))
6715 /* If the previous insn is an output reload, the source is
6716 a reload register, and its spill_reg_store entry will
6717 contain the previous destination. This is now
6718 invalid. */
6719 if (REG_P (SET_SRC (PATTERN (temp)))
6720 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6722 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6723 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6726 /* If these are the only uses of the pseudo reg,
6727 pretend for GDB it lives in the reload reg we used. */
6728 if (REG_N_DEATHS (REGNO (old)) == 1
6729 && REG_N_SETS (REGNO (old)) == 1)
6731 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6732 alter_reg (REGNO (old), -1);
6734 special = 1;
6736 else
6738 SET_DEST (PATTERN (temp)) = old;
6743 /* We can't do that, so output an insn to load RELOADREG. */
6745 /* If we have a secondary reload, pick up the secondary register
6746 and icode, if any. If OLDEQUIV and OLD are different or
6747 if this is an in-out reload, recompute whether or not we
6748 still need a secondary register and what the icode should
6749 be. If we still need a secondary register and the class or
6750 icode is different, go back to reloading from OLD if using
6751 OLDEQUIV means that we got the wrong type of register. We
6752 cannot have different class or icode due to an in-out reload
6753 because we don't make such reloads when both the input and
6754 output need secondary reload registers. */
6756 if (! special && rl->secondary_in_reload >= 0)
6758 rtx second_reload_reg = 0;
6759 rtx third_reload_reg = 0;
6760 int secondary_reload = rl->secondary_in_reload;
6761 rtx real_oldequiv = oldequiv;
6762 rtx real_old = old;
6763 rtx tmp;
6764 enum insn_code icode;
6765 enum insn_code tertiary_icode = CODE_FOR_nothing;
6767 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6768 and similarly for OLD.
6769 See comments in get_secondary_reload in reload.c. */
6770 /* If it is a pseudo that cannot be replaced with its
6771 equivalent MEM, we must fall back to reload_in, which
6772 will have all the necessary substitutions registered.
6773 Likewise for a pseudo that can't be replaced with its
6774 equivalent constant.
6776 Take extra care for subregs of such pseudos. Note that
6777 we cannot use reg_equiv_mem in this case because it is
6778 not in the right mode. */
6780 tmp = oldequiv;
6781 if (GET_CODE (tmp) == SUBREG)
6782 tmp = SUBREG_REG (tmp);
6783 if (REG_P (tmp)
6784 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6785 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6786 || reg_equiv_constant[REGNO (tmp)] != 0))
6788 if (! reg_equiv_mem[REGNO (tmp)]
6789 || num_not_at_initial_offset
6790 || GET_CODE (oldequiv) == SUBREG)
6791 real_oldequiv = rl->in;
6792 else
6793 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6796 tmp = old;
6797 if (GET_CODE (tmp) == SUBREG)
6798 tmp = SUBREG_REG (tmp);
6799 if (REG_P (tmp)
6800 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6801 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6802 || reg_equiv_constant[REGNO (tmp)] != 0))
6804 if (! reg_equiv_mem[REGNO (tmp)]
6805 || num_not_at_initial_offset
6806 || GET_CODE (old) == SUBREG)
6807 real_old = rl->in;
6808 else
6809 real_old = reg_equiv_mem[REGNO (tmp)];
6812 second_reload_reg = rld[secondary_reload].reg_rtx;
6813 if (rld[secondary_reload].secondary_in_reload >= 0)
6815 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
6817 third_reload_reg = rld[tertiary_reload].reg_rtx;
6818 tertiary_icode = rld[secondary_reload].secondary_in_icode;
6819 /* We'd have to add more code for quartary reloads. */
6820 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
6822 icode = rl->secondary_in_icode;
6824 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6825 || (rl->in != 0 && rl->out != 0))
6827 secondary_reload_info sri, sri2;
6828 enum reg_class new_class, new_t_class;
6830 sri.icode = CODE_FOR_nothing;
6831 sri.prev_sri = NULL;
6832 new_class = targetm.secondary_reload (1, real_oldequiv, rl->class,
6833 mode, &sri);
6835 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
6836 second_reload_reg = 0;
6837 else if (new_class == NO_REGS)
6839 if (reload_adjust_reg_for_icode (&second_reload_reg,
6840 third_reload_reg, sri.icode))
6841 icode = sri.icode, third_reload_reg = 0;
6842 else
6843 oldequiv = old, real_oldequiv = real_old;
6845 else if (sri.icode != CODE_FOR_nothing)
6846 /* We currently lack a way to express this in reloads. */
6847 gcc_unreachable ();
6848 else
6850 sri2.icode = CODE_FOR_nothing;
6851 sri2.prev_sri = &sri;
6852 new_t_class = targetm.secondary_reload (1, real_oldequiv,
6853 new_class, mode, &sri);
6854 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
6856 if (reload_adjust_reg_for_temp (&second_reload_reg,
6857 third_reload_reg,
6858 new_class, mode))
6859 third_reload_reg = 0, tertiary_icode = sri2.icode;
6860 else
6861 oldequiv = old, real_oldequiv = real_old;
6863 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
6865 rtx intermediate = second_reload_reg;
6867 if (reload_adjust_reg_for_temp (&intermediate, NULL,
6868 new_class, mode)
6869 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
6870 sri2.icode))
6872 second_reload_reg = intermediate;
6873 tertiary_icode = sri2.icode;
6875 else
6876 oldequiv = old, real_oldequiv = real_old;
6878 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
6880 rtx intermediate = second_reload_reg;
6882 if (reload_adjust_reg_for_temp (&intermediate, NULL,
6883 new_class, mode)
6884 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
6885 new_t_class, mode))
6887 second_reload_reg = intermediate;
6888 tertiary_icode = sri2.icode;
6890 else
6891 oldequiv = old, real_oldequiv = real_old;
6893 else
6894 /* This could be handled more intelligently too. */
6895 oldequiv = old, real_oldequiv = real_old;
6899 /* If we still need a secondary reload register, check
6900 to see if it is being used as a scratch or intermediate
6901 register and generate code appropriately. If we need
6902 a scratch register, use REAL_OLDEQUIV since the form of
6903 the insn may depend on the actual address if it is
6904 a MEM. */
6906 if (second_reload_reg)
6908 if (icode != CODE_FOR_nothing)
6910 /* We'd have to add extra code to handle this case. */
6911 gcc_assert (!third_reload_reg);
6913 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
6914 second_reload_reg));
6915 special = 1;
6917 else
6919 /* See if we need a scratch register to load the
6920 intermediate register (a tertiary reload). */
6921 if (tertiary_icode != CODE_FOR_nothing)
6923 emit_insn ((GEN_FCN (tertiary_icode)
6924 (second_reload_reg, real_oldequiv,
6925 third_reload_reg)));
6927 else if (third_reload_reg)
6929 gen_reload (third_reload_reg, real_oldequiv,
6930 rl->opnum,
6931 rl->when_needed);
6932 gen_reload (second_reload_reg, third_reload_reg,
6933 rl->opnum,
6934 rl->when_needed);
6936 else
6937 gen_reload (second_reload_reg, real_oldequiv,
6938 rl->opnum,
6939 rl->when_needed);
6941 oldequiv = second_reload_reg;
6946 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
6948 rtx real_oldequiv = oldequiv;
6950 if ((REG_P (oldequiv)
6951 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
6952 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
6953 || reg_equiv_constant[REGNO (oldequiv)] != 0))
6954 || (GET_CODE (oldequiv) == SUBREG
6955 && REG_P (SUBREG_REG (oldequiv))
6956 && (REGNO (SUBREG_REG (oldequiv))
6957 >= FIRST_PSEUDO_REGISTER)
6958 && ((reg_equiv_memory_loc
6959 [REGNO (SUBREG_REG (oldequiv))] != 0)
6960 || (reg_equiv_constant
6961 [REGNO (SUBREG_REG (oldequiv))] != 0)))
6962 || (CONSTANT_P (oldequiv)
6963 && (PREFERRED_RELOAD_CLASS (oldequiv,
6964 REGNO_REG_CLASS (REGNO (reloadreg)))
6965 == NO_REGS)))
6966 real_oldequiv = rl->in;
6967 gen_reload (reloadreg, real_oldequiv, rl->opnum,
6968 rl->when_needed);
6971 if (flag_non_call_exceptions)
6972 copy_eh_notes (insn, get_insns ());
6974 /* End this sequence. */
6975 *where = get_insns ();
6976 end_sequence ();
6978 /* Update reload_override_in so that delete_address_reloads_1
6979 can see the actual register usage. */
6980 if (oldequiv_reg)
6981 reload_override_in[j] = oldequiv;
6984 /* Generate insns to for the output reload RL, which is for the insn described
6985 by CHAIN and has the number J. */
6986 static void
6987 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
6988 int j)
6990 rtx reloadreg = rl->reg_rtx;
6991 rtx insn = chain->insn;
6992 int special = 0;
6993 rtx old = rl->out;
6994 enum machine_mode mode = GET_MODE (old);
6995 rtx p;
6997 if (rl->when_needed == RELOAD_OTHER)
6998 start_sequence ();
6999 else
7000 push_to_sequence (output_reload_insns[rl->opnum]);
7002 /* Determine the mode to reload in.
7003 See comments above (for input reloading). */
7005 if (mode == VOIDmode)
7007 /* VOIDmode should never happen for an output. */
7008 if (asm_noperands (PATTERN (insn)) < 0)
7009 /* It's the compiler's fault. */
7010 fatal_insn ("VOIDmode on an output", insn);
7011 error_for_asm (insn, "output operand is constant in %<asm%>");
7012 /* Prevent crash--use something we know is valid. */
7013 mode = word_mode;
7014 old = gen_rtx_REG (mode, REGNO (reloadreg));
7017 if (GET_MODE (reloadreg) != mode)
7018 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7020 /* If we need two reload regs, set RELOADREG to the intermediate
7021 one, since it will be stored into OLD. We might need a secondary
7022 register only for an input reload, so check again here. */
7024 if (rl->secondary_out_reload >= 0)
7026 rtx real_old = old;
7027 int secondary_reload = rl->secondary_out_reload;
7028 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
7030 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
7031 && reg_equiv_mem[REGNO (old)] != 0)
7032 real_old = reg_equiv_mem[REGNO (old)];
7034 if (secondary_reload_class (0, rl->class, mode, real_old) != NO_REGS)
7036 rtx second_reloadreg = reloadreg;
7037 reloadreg = rld[secondary_reload].reg_rtx;
7039 /* See if RELOADREG is to be used as a scratch register
7040 or as an intermediate register. */
7041 if (rl->secondary_out_icode != CODE_FOR_nothing)
7043 /* We'd have to add extra code to handle this case. */
7044 gcc_assert (tertiary_reload < 0);
7046 emit_insn ((GEN_FCN (rl->secondary_out_icode)
7047 (real_old, second_reloadreg, reloadreg)));
7048 special = 1;
7050 else
7052 /* See if we need both a scratch and intermediate reload
7053 register. */
7055 enum insn_code tertiary_icode
7056 = rld[secondary_reload].secondary_out_icode;
7058 /* We'd have to add more code for quartary reloads. */
7059 gcc_assert (tertiary_reload < 0
7060 || rld[tertiary_reload].secondary_out_reload < 0);
7062 if (GET_MODE (reloadreg) != mode)
7063 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7065 if (tertiary_icode != CODE_FOR_nothing)
7067 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7068 rtx tem;
7070 /* Copy primary reload reg to secondary reload reg.
7071 (Note that these have been swapped above, then
7072 secondary reload reg to OLD using our insn.) */
7074 /* If REAL_OLD is a paradoxical SUBREG, remove it
7075 and try to put the opposite SUBREG on
7076 RELOADREG. */
7077 if (GET_CODE (real_old) == SUBREG
7078 && (GET_MODE_SIZE (GET_MODE (real_old))
7079 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7080 && 0 != (tem = gen_lowpart_common
7081 (GET_MODE (SUBREG_REG (real_old)),
7082 reloadreg)))
7083 real_old = SUBREG_REG (real_old), reloadreg = tem;
7085 gen_reload (reloadreg, second_reloadreg,
7086 rl->opnum, rl->when_needed);
7087 emit_insn ((GEN_FCN (tertiary_icode)
7088 (real_old, reloadreg, third_reloadreg)));
7089 special = 1;
7092 else
7094 /* Copy between the reload regs here and then to
7095 OUT later. */
7097 gen_reload (reloadreg, second_reloadreg,
7098 rl->opnum, rl->when_needed);
7099 if (tertiary_reload >= 0)
7101 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7103 gen_reload (third_reloadreg, reloadreg,
7104 rl->opnum, rl->when_needed);
7105 reloadreg = third_reloadreg;
7112 /* Output the last reload insn. */
7113 if (! special)
7115 rtx set;
7117 /* Don't output the last reload if OLD is not the dest of
7118 INSN and is in the src and is clobbered by INSN. */
7119 if (! flag_expensive_optimizations
7120 || !REG_P (old)
7121 || !(set = single_set (insn))
7122 || rtx_equal_p (old, SET_DEST (set))
7123 || !reg_mentioned_p (old, SET_SRC (set))
7124 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7125 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7126 gen_reload (old, reloadreg, rl->opnum,
7127 rl->when_needed);
7130 /* Look at all insns we emitted, just to be safe. */
7131 for (p = get_insns (); p; p = NEXT_INSN (p))
7132 if (INSN_P (p))
7134 rtx pat = PATTERN (p);
7136 /* If this output reload doesn't come from a spill reg,
7137 clear any memory of reloaded copies of the pseudo reg.
7138 If this output reload comes from a spill reg,
7139 reg_has_output_reload will make this do nothing. */
7140 note_stores (pat, forget_old_reloads_1, NULL);
7142 if (reg_mentioned_p (rl->reg_rtx, pat))
7144 rtx set = single_set (insn);
7145 if (reload_spill_index[j] < 0
7146 && set
7147 && SET_SRC (set) == rl->reg_rtx)
7149 int src = REGNO (SET_SRC (set));
7151 reload_spill_index[j] = src;
7152 SET_HARD_REG_BIT (reg_is_output_reload, src);
7153 if (find_regno_note (insn, REG_DEAD, src))
7154 SET_HARD_REG_BIT (reg_reloaded_died, src);
7156 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
7158 int s = rl->secondary_out_reload;
7159 set = single_set (p);
7160 /* If this reload copies only to the secondary reload
7161 register, the secondary reload does the actual
7162 store. */
7163 if (s >= 0 && set == NULL_RTX)
7164 /* We can't tell what function the secondary reload
7165 has and where the actual store to the pseudo is
7166 made; leave new_spill_reg_store alone. */
7168 else if (s >= 0
7169 && SET_SRC (set) == rl->reg_rtx
7170 && SET_DEST (set) == rld[s].reg_rtx)
7172 /* Usually the next instruction will be the
7173 secondary reload insn; if we can confirm
7174 that it is, setting new_spill_reg_store to
7175 that insn will allow an extra optimization. */
7176 rtx s_reg = rld[s].reg_rtx;
7177 rtx next = NEXT_INSN (p);
7178 rld[s].out = rl->out;
7179 rld[s].out_reg = rl->out_reg;
7180 set = single_set (next);
7181 if (set && SET_SRC (set) == s_reg
7182 && ! new_spill_reg_store[REGNO (s_reg)])
7184 SET_HARD_REG_BIT (reg_is_output_reload,
7185 REGNO (s_reg));
7186 new_spill_reg_store[REGNO (s_reg)] = next;
7189 else
7190 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
7195 if (rl->when_needed == RELOAD_OTHER)
7197 emit_insn (other_output_reload_insns[rl->opnum]);
7198 other_output_reload_insns[rl->opnum] = get_insns ();
7200 else
7201 output_reload_insns[rl->opnum] = get_insns ();
7203 if (flag_non_call_exceptions)
7204 copy_eh_notes (insn, get_insns ());
7206 end_sequence ();
7209 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7210 and has the number J. */
7211 static void
7212 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7214 rtx insn = chain->insn;
7215 rtx old = (rl->in && MEM_P (rl->in)
7216 ? rl->in_reg : rl->in);
7218 if (old != 0
7219 /* AUTO_INC reloads need to be handled even if inherited. We got an
7220 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7221 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7222 && ! rtx_equal_p (rl->reg_rtx, old)
7223 && rl->reg_rtx != 0)
7224 emit_input_reload_insns (chain, rld + j, old, j);
7226 /* When inheriting a wider reload, we have a MEM in rl->in,
7227 e.g. inheriting a SImode output reload for
7228 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7229 if (optimize && reload_inherited[j] && rl->in
7230 && MEM_P (rl->in)
7231 && MEM_P (rl->in_reg)
7232 && reload_spill_index[j] >= 0
7233 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7234 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7236 /* If we are reloading a register that was recently stored in with an
7237 output-reload, see if we can prove there was
7238 actually no need to store the old value in it. */
7240 if (optimize
7241 && (reload_inherited[j] || reload_override_in[j])
7242 && rl->reg_rtx
7243 && REG_P (rl->reg_rtx)
7244 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
7245 #if 0
7246 /* There doesn't seem to be any reason to restrict this to pseudos
7247 and doing so loses in the case where we are copying from a
7248 register of the wrong class. */
7249 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
7250 >= FIRST_PSEUDO_REGISTER)
7251 #endif
7252 /* The insn might have already some references to stackslots
7253 replaced by MEMs, while reload_out_reg still names the
7254 original pseudo. */
7255 && (dead_or_set_p (insn,
7256 spill_reg_stored_to[REGNO (rl->reg_rtx)])
7257 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
7258 rl->out_reg)))
7259 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
7262 /* Do output reloading for reload RL, which is for the insn described by
7263 CHAIN and has the number J.
7264 ??? At some point we need to support handling output reloads of
7265 JUMP_INSNs or insns that set cc0. */
7266 static void
7267 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7269 rtx note, old;
7270 rtx insn = chain->insn;
7271 /* If this is an output reload that stores something that is
7272 not loaded in this same reload, see if we can eliminate a previous
7273 store. */
7274 rtx pseudo = rl->out_reg;
7276 if (pseudo
7277 && optimize
7278 && REG_P (pseudo)
7279 && ! rtx_equal_p (rl->in_reg, pseudo)
7280 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7281 && reg_last_reload_reg[REGNO (pseudo)])
7283 int pseudo_no = REGNO (pseudo);
7284 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7286 /* We don't need to test full validity of last_regno for
7287 inherit here; we only want to know if the store actually
7288 matches the pseudo. */
7289 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7290 && reg_reloaded_contents[last_regno] == pseudo_no
7291 && spill_reg_store[last_regno]
7292 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7293 delete_output_reload (insn, j, last_regno);
7296 old = rl->out_reg;
7297 if (old == 0
7298 || rl->reg_rtx == old
7299 || rl->reg_rtx == 0)
7300 return;
7302 /* An output operand that dies right away does need a reload,
7303 but need not be copied from it. Show the new location in the
7304 REG_UNUSED note. */
7305 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7306 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7308 XEXP (note, 0) = rl->reg_rtx;
7309 return;
7311 /* Likewise for a SUBREG of an operand that dies. */
7312 else if (GET_CODE (old) == SUBREG
7313 && REG_P (SUBREG_REG (old))
7314 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7315 SUBREG_REG (old))))
7317 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7318 rl->reg_rtx);
7319 return;
7321 else if (GET_CODE (old) == SCRATCH)
7322 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7323 but we don't want to make an output reload. */
7324 return;
7326 /* If is a JUMP_INSN, we can't support output reloads yet. */
7327 gcc_assert (NONJUMP_INSN_P (insn));
7329 emit_output_reload_insns (chain, rld + j, j);
7332 /* Reload number R reloads from or to a group of hard registers starting at
7333 register REGNO. Return true if it can be treated for inheritance purposes
7334 like a group of reloads, each one reloading a single hard register.
7335 The caller has already checked that the spill register and REGNO use
7336 the same number of registers to store the reload value. */
7338 static bool
7339 inherit_piecemeal_p (int r ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED)
7341 #ifdef CANNOT_CHANGE_MODE_CLASS
7342 return (!REG_CANNOT_CHANGE_MODE_P (reload_spill_index[r],
7343 GET_MODE (rld[r].reg_rtx),
7344 reg_raw_mode[reload_spill_index[r]])
7345 && !REG_CANNOT_CHANGE_MODE_P (regno,
7346 GET_MODE (rld[r].reg_rtx),
7347 reg_raw_mode[regno]));
7348 #else
7349 return true;
7350 #endif
7353 /* Output insns to reload values in and out of the chosen reload regs. */
7355 static void
7356 emit_reload_insns (struct insn_chain *chain)
7358 rtx insn = chain->insn;
7360 int j;
7362 CLEAR_HARD_REG_SET (reg_reloaded_died);
7364 for (j = 0; j < reload_n_operands; j++)
7365 input_reload_insns[j] = input_address_reload_insns[j]
7366 = inpaddr_address_reload_insns[j]
7367 = output_reload_insns[j] = output_address_reload_insns[j]
7368 = outaddr_address_reload_insns[j]
7369 = other_output_reload_insns[j] = 0;
7370 other_input_address_reload_insns = 0;
7371 other_input_reload_insns = 0;
7372 operand_reload_insns = 0;
7373 other_operand_reload_insns = 0;
7375 /* Dump reloads into the dump file. */
7376 if (dump_file)
7378 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7379 debug_reload_to_stream (dump_file);
7382 /* Now output the instructions to copy the data into and out of the
7383 reload registers. Do these in the order that the reloads were reported,
7384 since reloads of base and index registers precede reloads of operands
7385 and the operands may need the base and index registers reloaded. */
7387 for (j = 0; j < n_reloads; j++)
7389 if (rld[j].reg_rtx
7390 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7391 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
7393 do_input_reload (chain, rld + j, j);
7394 do_output_reload (chain, rld + j, j);
7397 /* Now write all the insns we made for reloads in the order expected by
7398 the allocation functions. Prior to the insn being reloaded, we write
7399 the following reloads:
7401 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7403 RELOAD_OTHER reloads.
7405 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7406 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7407 RELOAD_FOR_INPUT reload for the operand.
7409 RELOAD_FOR_OPADDR_ADDRS reloads.
7411 RELOAD_FOR_OPERAND_ADDRESS reloads.
7413 After the insn being reloaded, we write the following:
7415 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7416 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7417 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7418 reloads for the operand. The RELOAD_OTHER output reloads are
7419 output in descending order by reload number. */
7421 emit_insn_before (other_input_address_reload_insns, insn);
7422 emit_insn_before (other_input_reload_insns, insn);
7424 for (j = 0; j < reload_n_operands; j++)
7426 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7427 emit_insn_before (input_address_reload_insns[j], insn);
7428 emit_insn_before (input_reload_insns[j], insn);
7431 emit_insn_before (other_operand_reload_insns, insn);
7432 emit_insn_before (operand_reload_insns, insn);
7434 for (j = 0; j < reload_n_operands; j++)
7436 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7437 x = emit_insn_after (output_address_reload_insns[j], x);
7438 x = emit_insn_after (output_reload_insns[j], x);
7439 emit_insn_after (other_output_reload_insns[j], x);
7442 /* For all the spill regs newly reloaded in this instruction,
7443 record what they were reloaded from, so subsequent instructions
7444 can inherit the reloads.
7446 Update spill_reg_store for the reloads of this insn.
7447 Copy the elements that were updated in the loop above. */
7449 for (j = 0; j < n_reloads; j++)
7451 int r = reload_order[j];
7452 int i = reload_spill_index[r];
7454 /* If this is a non-inherited input reload from a pseudo, we must
7455 clear any memory of a previous store to the same pseudo. Only do
7456 something if there will not be an output reload for the pseudo
7457 being reloaded. */
7458 if (rld[r].in_reg != 0
7459 && ! (reload_inherited[r] || reload_override_in[r]))
7461 rtx reg = rld[r].in_reg;
7463 if (GET_CODE (reg) == SUBREG)
7464 reg = SUBREG_REG (reg);
7466 if (REG_P (reg)
7467 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7468 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
7470 int nregno = REGNO (reg);
7472 if (reg_last_reload_reg[nregno])
7474 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7476 if (reg_reloaded_contents[last_regno] == nregno)
7477 spill_reg_store[last_regno] = 0;
7482 /* I is nonneg if this reload used a register.
7483 If rld[r].reg_rtx is 0, this is an optional reload
7484 that we opted to ignore. */
7486 if (i >= 0 && rld[r].reg_rtx != 0)
7488 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7489 int k;
7490 int part_reaches_end = 0;
7491 int all_reaches_end = 1;
7493 /* For a multi register reload, we need to check if all or part
7494 of the value lives to the end. */
7495 for (k = 0; k < nr; k++)
7497 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7498 rld[r].when_needed))
7499 part_reaches_end = 1;
7500 else
7501 all_reaches_end = 0;
7504 /* Ignore reloads that don't reach the end of the insn in
7505 entirety. */
7506 if (all_reaches_end)
7508 /* First, clear out memory of what used to be in this spill reg.
7509 If consecutive registers are used, clear them all. */
7511 for (k = 0; k < nr; k++)
7513 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7514 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7517 /* Maybe the spill reg contains a copy of reload_out. */
7518 if (rld[r].out != 0
7519 && (REG_P (rld[r].out)
7520 #ifdef AUTO_INC_DEC
7521 || ! rld[r].out_reg
7522 #endif
7523 || REG_P (rld[r].out_reg)))
7525 rtx out = (REG_P (rld[r].out)
7526 ? rld[r].out
7527 : rld[r].out_reg
7528 ? rld[r].out_reg
7529 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7530 int nregno = REGNO (out);
7531 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7532 : hard_regno_nregs[nregno]
7533 [GET_MODE (rld[r].reg_rtx)]);
7534 bool piecemeal;
7536 spill_reg_store[i] = new_spill_reg_store[i];
7537 spill_reg_stored_to[i] = out;
7538 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7540 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7541 && nr == nnr
7542 && inherit_piecemeal_p (r, nregno));
7544 /* If NREGNO is a hard register, it may occupy more than
7545 one register. If it does, say what is in the
7546 rest of the registers assuming that both registers
7547 agree on how many words the object takes. If not,
7548 invalidate the subsequent registers. */
7550 if (nregno < FIRST_PSEUDO_REGISTER)
7551 for (k = 1; k < nnr; k++)
7552 reg_last_reload_reg[nregno + k]
7553 = (piecemeal
7554 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7555 : 0);
7557 /* Now do the inverse operation. */
7558 for (k = 0; k < nr; k++)
7560 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7561 reg_reloaded_contents[i + k]
7562 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7563 ? nregno
7564 : nregno + k);
7565 reg_reloaded_insn[i + k] = insn;
7566 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7567 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (out)))
7568 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7572 /* Maybe the spill reg contains a copy of reload_in. Only do
7573 something if there will not be an output reload for
7574 the register being reloaded. */
7575 else if (rld[r].out_reg == 0
7576 && rld[r].in != 0
7577 && ((REG_P (rld[r].in)
7578 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7579 && !REGNO_REG_SET_P (&reg_has_output_reload,
7580 REGNO (rld[r].in)))
7581 || (REG_P (rld[r].in_reg)
7582 && !REGNO_REG_SET_P (&reg_has_output_reload,
7583 REGNO (rld[r].in_reg))))
7584 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7586 int nregno;
7587 int nnr;
7588 rtx in;
7589 bool piecemeal;
7591 if (REG_P (rld[r].in)
7592 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7593 in = rld[r].in;
7594 else if (REG_P (rld[r].in_reg))
7595 in = rld[r].in_reg;
7596 else
7597 in = XEXP (rld[r].in_reg, 0);
7598 nregno = REGNO (in);
7600 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7601 : hard_regno_nregs[nregno]
7602 [GET_MODE (rld[r].reg_rtx)]);
7604 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7606 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7607 && nr == nnr
7608 && inherit_piecemeal_p (r, nregno));
7610 if (nregno < FIRST_PSEUDO_REGISTER)
7611 for (k = 1; k < nnr; k++)
7612 reg_last_reload_reg[nregno + k]
7613 = (piecemeal
7614 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7615 : 0);
7617 /* Unless we inherited this reload, show we haven't
7618 recently done a store.
7619 Previous stores of inherited auto_inc expressions
7620 also have to be discarded. */
7621 if (! reload_inherited[r]
7622 || (rld[r].out && ! rld[r].out_reg))
7623 spill_reg_store[i] = 0;
7625 for (k = 0; k < nr; k++)
7627 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7628 reg_reloaded_contents[i + k]
7629 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7630 ? nregno
7631 : nregno + k);
7632 reg_reloaded_insn[i + k] = insn;
7633 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7634 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (in)))
7635 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7640 /* However, if part of the reload reaches the end, then we must
7641 invalidate the old info for the part that survives to the end. */
7642 else if (part_reaches_end)
7644 for (k = 0; k < nr; k++)
7645 if (reload_reg_reaches_end_p (i + k,
7646 rld[r].opnum,
7647 rld[r].when_needed))
7648 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7652 /* The following if-statement was #if 0'd in 1.34 (or before...).
7653 It's reenabled in 1.35 because supposedly nothing else
7654 deals with this problem. */
7656 /* If a register gets output-reloaded from a non-spill register,
7657 that invalidates any previous reloaded copy of it.
7658 But forget_old_reloads_1 won't get to see it, because
7659 it thinks only about the original insn. So invalidate it here.
7660 Also do the same thing for RELOAD_OTHER constraints where the
7661 output is discarded. */
7662 if (i < 0
7663 && ((rld[r].out != 0
7664 && (REG_P (rld[r].out)
7665 || (MEM_P (rld[r].out)
7666 && REG_P (rld[r].out_reg))))
7667 || (rld[r].out == 0 && rld[r].out_reg
7668 && REG_P (rld[r].out_reg))))
7670 rtx out = ((rld[r].out && REG_P (rld[r].out))
7671 ? rld[r].out : rld[r].out_reg);
7672 int nregno = REGNO (out);
7674 /* REG_RTX is now set or clobbered by the main instruction.
7675 As the comment above explains, forget_old_reloads_1 only
7676 sees the original instruction, and there is no guarantee
7677 that the original instruction also clobbered REG_RTX.
7678 For example, if find_reloads sees that the input side of
7679 a matched operand pair dies in this instruction, it may
7680 use the input register as the reload register.
7682 Calling forget_old_reloads_1 is a waste of effort if
7683 REG_RTX is also the output register.
7685 If we know that REG_RTX holds the value of a pseudo
7686 register, the code after the call will record that fact. */
7687 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
7688 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
7690 if (nregno >= FIRST_PSEUDO_REGISTER)
7692 rtx src_reg, store_insn = NULL_RTX;
7694 reg_last_reload_reg[nregno] = 0;
7696 /* If we can find a hard register that is stored, record
7697 the storing insn so that we may delete this insn with
7698 delete_output_reload. */
7699 src_reg = rld[r].reg_rtx;
7701 /* If this is an optional reload, try to find the source reg
7702 from an input reload. */
7703 if (! src_reg)
7705 rtx set = single_set (insn);
7706 if (set && SET_DEST (set) == rld[r].out)
7708 int k;
7710 src_reg = SET_SRC (set);
7711 store_insn = insn;
7712 for (k = 0; k < n_reloads; k++)
7714 if (rld[k].in == src_reg)
7716 src_reg = rld[k].reg_rtx;
7717 break;
7722 else
7723 store_insn = new_spill_reg_store[REGNO (src_reg)];
7724 if (src_reg && REG_P (src_reg)
7725 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7727 int src_regno = REGNO (src_reg);
7728 int nr = hard_regno_nregs[src_regno][rld[r].mode];
7729 /* The place where to find a death note varies with
7730 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7731 necessarily checked exactly in the code that moves
7732 notes, so just check both locations. */
7733 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7734 if (! note && store_insn)
7735 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7736 while (nr-- > 0)
7738 spill_reg_store[src_regno + nr] = store_insn;
7739 spill_reg_stored_to[src_regno + nr] = out;
7740 reg_reloaded_contents[src_regno + nr] = nregno;
7741 reg_reloaded_insn[src_regno + nr] = store_insn;
7742 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7743 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7744 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + nr,
7745 GET_MODE (src_reg)))
7746 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7747 src_regno + nr);
7748 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7749 if (note)
7750 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7751 else
7752 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7754 reg_last_reload_reg[nregno] = src_reg;
7755 /* We have to set reg_has_output_reload here, or else
7756 forget_old_reloads_1 will clear reg_last_reload_reg
7757 right away. */
7758 SET_REGNO_REG_SET (&reg_has_output_reload,
7759 nregno);
7762 else
7764 int num_regs = hard_regno_nregs[nregno][GET_MODE (out)];
7766 while (num_regs-- > 0)
7767 reg_last_reload_reg[nregno + num_regs] = 0;
7771 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7774 /* Go through the motions to emit INSN and test if it is strictly valid.
7775 Return the emitted insn if valid, else return NULL. */
7777 static rtx
7778 emit_insn_if_valid_for_reload (rtx insn)
7780 rtx last = get_last_insn ();
7781 int code;
7783 insn = emit_insn (insn);
7784 code = recog_memoized (insn);
7786 if (code >= 0)
7788 extract_insn (insn);
7789 /* We want constrain operands to treat this insn strictly in its
7790 validity determination, i.e., the way it would after reload has
7791 completed. */
7792 if (constrain_operands (1))
7793 return insn;
7796 delete_insns_since (last);
7797 return NULL;
7800 /* Emit code to perform a reload from IN (which may be a reload register) to
7801 OUT (which may also be a reload register). IN or OUT is from operand
7802 OPNUM with reload type TYPE.
7804 Returns first insn emitted. */
7806 static rtx
7807 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7809 rtx last = get_last_insn ();
7810 rtx tem;
7812 /* If IN is a paradoxical SUBREG, remove it and try to put the
7813 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7814 if (GET_CODE (in) == SUBREG
7815 && (GET_MODE_SIZE (GET_MODE (in))
7816 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7817 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7818 in = SUBREG_REG (in), out = tem;
7819 else if (GET_CODE (out) == SUBREG
7820 && (GET_MODE_SIZE (GET_MODE (out))
7821 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7822 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7823 out = SUBREG_REG (out), in = tem;
7825 /* How to do this reload can get quite tricky. Normally, we are being
7826 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7827 register that didn't get a hard register. In that case we can just
7828 call emit_move_insn.
7830 We can also be asked to reload a PLUS that adds a register or a MEM to
7831 another register, constant or MEM. This can occur during frame pointer
7832 elimination and while reloading addresses. This case is handled by
7833 trying to emit a single insn to perform the add. If it is not valid,
7834 we use a two insn sequence.
7836 Or we can be asked to reload an unary operand that was a fragment of
7837 an addressing mode, into a register. If it isn't recognized as-is,
7838 we try making the unop operand and the reload-register the same:
7839 (set reg:X (unop:X expr:Y))
7840 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
7842 Finally, we could be called to handle an 'o' constraint by putting
7843 an address into a register. In that case, we first try to do this
7844 with a named pattern of "reload_load_address". If no such pattern
7845 exists, we just emit a SET insn and hope for the best (it will normally
7846 be valid on machines that use 'o').
7848 This entire process is made complex because reload will never
7849 process the insns we generate here and so we must ensure that
7850 they will fit their constraints and also by the fact that parts of
7851 IN might be being reloaded separately and replaced with spill registers.
7852 Because of this, we are, in some sense, just guessing the right approach
7853 here. The one listed above seems to work.
7855 ??? At some point, this whole thing needs to be rethought. */
7857 if (GET_CODE (in) == PLUS
7858 && (REG_P (XEXP (in, 0))
7859 || GET_CODE (XEXP (in, 0)) == SUBREG
7860 || MEM_P (XEXP (in, 0)))
7861 && (REG_P (XEXP (in, 1))
7862 || GET_CODE (XEXP (in, 1)) == SUBREG
7863 || CONSTANT_P (XEXP (in, 1))
7864 || MEM_P (XEXP (in, 1))))
7866 /* We need to compute the sum of a register or a MEM and another
7867 register, constant, or MEM, and put it into the reload
7868 register. The best possible way of doing this is if the machine
7869 has a three-operand ADD insn that accepts the required operands.
7871 The simplest approach is to try to generate such an insn and see if it
7872 is recognized and matches its constraints. If so, it can be used.
7874 It might be better not to actually emit the insn unless it is valid,
7875 but we need to pass the insn as an operand to `recog' and
7876 `extract_insn' and it is simpler to emit and then delete the insn if
7877 not valid than to dummy things up. */
7879 rtx op0, op1, tem, insn;
7880 int code;
7882 op0 = find_replacement (&XEXP (in, 0));
7883 op1 = find_replacement (&XEXP (in, 1));
7885 /* Since constraint checking is strict, commutativity won't be
7886 checked, so we need to do that here to avoid spurious failure
7887 if the add instruction is two-address and the second operand
7888 of the add is the same as the reload reg, which is frequently
7889 the case. If the insn would be A = B + A, rearrange it so
7890 it will be A = A + B as constrain_operands expects. */
7892 if (REG_P (XEXP (in, 1))
7893 && REGNO (out) == REGNO (XEXP (in, 1)))
7894 tem = op0, op0 = op1, op1 = tem;
7896 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
7897 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
7899 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
7900 if (insn)
7901 return insn;
7903 /* If that failed, we must use a conservative two-insn sequence.
7905 Use a move to copy one operand into the reload register. Prefer
7906 to reload a constant, MEM or pseudo since the move patterns can
7907 handle an arbitrary operand. If OP1 is not a constant, MEM or
7908 pseudo and OP1 is not a valid operand for an add instruction, then
7909 reload OP1.
7911 After reloading one of the operands into the reload register, add
7912 the reload register to the output register.
7914 If there is another way to do this for a specific machine, a
7915 DEFINE_PEEPHOLE should be specified that recognizes the sequence
7916 we emit below. */
7918 code = (int) add_optab->handlers[(int) GET_MODE (out)].insn_code;
7920 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
7921 || (REG_P (op1)
7922 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
7923 || (code != CODE_FOR_nothing
7924 && ! ((*insn_data[code].operand[2].predicate)
7925 (op1, insn_data[code].operand[2].mode))))
7926 tem = op0, op0 = op1, op1 = tem;
7928 gen_reload (out, op0, opnum, type);
7930 /* If OP0 and OP1 are the same, we can use OUT for OP1.
7931 This fixes a problem on the 32K where the stack pointer cannot
7932 be used as an operand of an add insn. */
7934 if (rtx_equal_p (op0, op1))
7935 op1 = out;
7937 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
7938 if (insn)
7940 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
7941 set_unique_reg_note (insn, REG_EQUIV, in);
7942 return insn;
7945 /* If that failed, copy the address register to the reload register.
7946 Then add the constant to the reload register. */
7948 gcc_assert (!reg_overlap_mentioned_p (out, op0));
7949 gen_reload (out, op1, opnum, type);
7950 insn = emit_insn (gen_add2_insn (out, op0));
7951 set_unique_reg_note (insn, REG_EQUIV, in);
7954 #ifdef SECONDARY_MEMORY_NEEDED
7955 /* If we need a memory location to do the move, do it that way. */
7956 else if ((REG_P (in) || GET_CODE (in) == SUBREG)
7957 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
7958 && (REG_P (out) || GET_CODE (out) == SUBREG)
7959 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
7960 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
7961 REGNO_REG_CLASS (reg_or_subregno (out)),
7962 GET_MODE (out)))
7964 /* Get the memory to use and rewrite both registers to its mode. */
7965 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
7967 if (GET_MODE (loc) != GET_MODE (out))
7968 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
7970 if (GET_MODE (loc) != GET_MODE (in))
7971 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
7973 gen_reload (loc, in, opnum, type);
7974 gen_reload (out, loc, opnum, type);
7976 #endif
7977 else if (REG_P (out) && UNARY_P (in))
7979 rtx insn;
7980 rtx op1;
7981 rtx out_moded;
7982 rtx set;
7984 op1 = find_replacement (&XEXP (in, 0));
7985 if (op1 != XEXP (in, 0))
7986 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
7988 /* First, try a plain SET. */
7989 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
7990 if (set)
7991 return set;
7993 /* If that failed, move the inner operand to the reload
7994 register, and try the same unop with the inner expression
7995 replaced with the reload register. */
7997 if (GET_MODE (op1) != GET_MODE (out))
7998 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
7999 else
8000 out_moded = out;
8002 gen_reload (out_moded, op1, opnum, type);
8004 insn
8005 = gen_rtx_SET (VOIDmode, out,
8006 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
8007 out_moded));
8008 insn = emit_insn_if_valid_for_reload (insn);
8009 if (insn)
8011 set_unique_reg_note (insn, REG_EQUIV, in);
8012 return insn;
8015 fatal_insn ("Failure trying to reload:", set);
8017 /* If IN is a simple operand, use gen_move_insn. */
8018 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8020 tem = emit_insn (gen_move_insn (out, in));
8021 /* IN may contain a LABEL_REF, if so add a REG_LABEL note. */
8022 mark_jump_label (in, tem, 0);
8025 #ifdef HAVE_reload_load_address
8026 else if (HAVE_reload_load_address)
8027 emit_insn (gen_reload_load_address (out, in));
8028 #endif
8030 /* Otherwise, just write (set OUT IN) and hope for the best. */
8031 else
8032 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8034 /* Return the first insn emitted.
8035 We can not just return get_last_insn, because there may have
8036 been multiple instructions emitted. Also note that gen_move_insn may
8037 emit more than one insn itself, so we can not assume that there is one
8038 insn emitted per emit_insn_before call. */
8040 return last ? NEXT_INSN (last) : get_insns ();
8043 /* Delete a previously made output-reload whose result we now believe
8044 is not needed. First we double-check.
8046 INSN is the insn now being processed.
8047 LAST_RELOAD_REG is the hard register number for which we want to delete
8048 the last output reload.
8049 J is the reload-number that originally used REG. The caller has made
8050 certain that reload J doesn't use REG any longer for input. */
8052 static void
8053 delete_output_reload (rtx insn, int j, int last_reload_reg)
8055 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8056 rtx reg = spill_reg_stored_to[last_reload_reg];
8057 int k;
8058 int n_occurrences;
8059 int n_inherited = 0;
8060 rtx i1;
8061 rtx substed;
8063 /* It is possible that this reload has been only used to set another reload
8064 we eliminated earlier and thus deleted this instruction too. */
8065 if (INSN_DELETED_P (output_reload_insn))
8066 return;
8068 /* Get the raw pseudo-register referred to. */
8070 while (GET_CODE (reg) == SUBREG)
8071 reg = SUBREG_REG (reg);
8072 substed = reg_equiv_memory_loc[REGNO (reg)];
8074 /* This is unsafe if the operand occurs more often in the current
8075 insn than it is inherited. */
8076 for (k = n_reloads - 1; k >= 0; k--)
8078 rtx reg2 = rld[k].in;
8079 if (! reg2)
8080 continue;
8081 if (MEM_P (reg2) || reload_override_in[k])
8082 reg2 = rld[k].in_reg;
8083 #ifdef AUTO_INC_DEC
8084 if (rld[k].out && ! rld[k].out_reg)
8085 reg2 = XEXP (rld[k].in_reg, 0);
8086 #endif
8087 while (GET_CODE (reg2) == SUBREG)
8088 reg2 = SUBREG_REG (reg2);
8089 if (rtx_equal_p (reg2, reg))
8091 if (reload_inherited[k] || reload_override_in[k] || k == j)
8092 n_inherited++;
8093 else
8094 return;
8097 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
8098 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
8099 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
8100 reg, 0);
8101 if (substed)
8102 n_occurrences += count_occurrences (PATTERN (insn),
8103 eliminate_regs (substed, 0,
8104 NULL_RTX), 0);
8105 for (i1 = reg_equiv_alt_mem_list [REGNO (reg)]; i1; i1 = XEXP (i1, 1))
8107 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8108 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8110 if (n_occurrences > n_inherited)
8111 return;
8113 /* If the pseudo-reg we are reloading is no longer referenced
8114 anywhere between the store into it and here,
8115 and we're within the same basic block, then the value can only
8116 pass through the reload reg and end up here.
8117 Otherwise, give up--return. */
8118 for (i1 = NEXT_INSN (output_reload_insn);
8119 i1 != insn; i1 = NEXT_INSN (i1))
8121 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8122 return;
8123 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8124 && reg_mentioned_p (reg, PATTERN (i1)))
8126 /* If this is USE in front of INSN, we only have to check that
8127 there are no more references than accounted for by inheritance. */
8128 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8130 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8131 i1 = NEXT_INSN (i1);
8133 if (n_occurrences <= n_inherited && i1 == insn)
8134 break;
8135 return;
8139 /* We will be deleting the insn. Remove the spill reg information. */
8140 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8142 spill_reg_store[last_reload_reg + k] = 0;
8143 spill_reg_stored_to[last_reload_reg + k] = 0;
8146 /* The caller has already checked that REG dies or is set in INSN.
8147 It has also checked that we are optimizing, and thus some
8148 inaccuracies in the debugging information are acceptable.
8149 So we could just delete output_reload_insn. But in some cases
8150 we can improve the debugging information without sacrificing
8151 optimization - maybe even improving the code: See if the pseudo
8152 reg has been completely replaced with reload regs. If so, delete
8153 the store insn and forget we had a stack slot for the pseudo. */
8154 if (rld[j].out != rld[j].in
8155 && REG_N_DEATHS (REGNO (reg)) == 1
8156 && REG_N_SETS (REGNO (reg)) == 1
8157 && REG_BASIC_BLOCK (REGNO (reg)) >= 0
8158 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8160 rtx i2;
8162 /* We know that it was used only between here and the beginning of
8163 the current basic block. (We also know that the last use before
8164 INSN was the output reload we are thinking of deleting, but never
8165 mind that.) Search that range; see if any ref remains. */
8166 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8168 rtx set = single_set (i2);
8170 /* Uses which just store in the pseudo don't count,
8171 since if they are the only uses, they are dead. */
8172 if (set != 0 && SET_DEST (set) == reg)
8173 continue;
8174 if (LABEL_P (i2)
8175 || JUMP_P (i2))
8176 break;
8177 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8178 && reg_mentioned_p (reg, PATTERN (i2)))
8180 /* Some other ref remains; just delete the output reload we
8181 know to be dead. */
8182 delete_address_reloads (output_reload_insn, insn);
8183 delete_insn (output_reload_insn);
8184 return;
8188 /* Delete the now-dead stores into this pseudo. Note that this
8189 loop also takes care of deleting output_reload_insn. */
8190 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8192 rtx set = single_set (i2);
8194 if (set != 0 && SET_DEST (set) == reg)
8196 delete_address_reloads (i2, insn);
8197 delete_insn (i2);
8199 if (LABEL_P (i2)
8200 || JUMP_P (i2))
8201 break;
8204 /* For the debugging info, say the pseudo lives in this reload reg. */
8205 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8206 alter_reg (REGNO (reg), -1);
8208 else
8210 delete_address_reloads (output_reload_insn, insn);
8211 delete_insn (output_reload_insn);
8215 /* We are going to delete DEAD_INSN. Recursively delete loads of
8216 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8217 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8218 static void
8219 delete_address_reloads (rtx dead_insn, rtx current_insn)
8221 rtx set = single_set (dead_insn);
8222 rtx set2, dst, prev, next;
8223 if (set)
8225 rtx dst = SET_DEST (set);
8226 if (MEM_P (dst))
8227 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8229 /* If we deleted the store from a reloaded post_{in,de}c expression,
8230 we can delete the matching adds. */
8231 prev = PREV_INSN (dead_insn);
8232 next = NEXT_INSN (dead_insn);
8233 if (! prev || ! next)
8234 return;
8235 set = single_set (next);
8236 set2 = single_set (prev);
8237 if (! set || ! set2
8238 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8239 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8240 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8241 return;
8242 dst = SET_DEST (set);
8243 if (! rtx_equal_p (dst, SET_DEST (set2))
8244 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8245 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8246 || (INTVAL (XEXP (SET_SRC (set), 1))
8247 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8248 return;
8249 delete_related_insns (prev);
8250 delete_related_insns (next);
8253 /* Subfunction of delete_address_reloads: process registers found in X. */
8254 static void
8255 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8257 rtx prev, set, dst, i2;
8258 int i, j;
8259 enum rtx_code code = GET_CODE (x);
8261 if (code != REG)
8263 const char *fmt = GET_RTX_FORMAT (code);
8264 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8266 if (fmt[i] == 'e')
8267 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8268 else if (fmt[i] == 'E')
8270 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8271 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8272 current_insn);
8275 return;
8278 if (spill_reg_order[REGNO (x)] < 0)
8279 return;
8281 /* Scan backwards for the insn that sets x. This might be a way back due
8282 to inheritance. */
8283 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8285 code = GET_CODE (prev);
8286 if (code == CODE_LABEL || code == JUMP_INSN)
8287 return;
8288 if (!INSN_P (prev))
8289 continue;
8290 if (reg_set_p (x, PATTERN (prev)))
8291 break;
8292 if (reg_referenced_p (x, PATTERN (prev)))
8293 return;
8295 if (! prev || INSN_UID (prev) < reload_first_uid)
8296 return;
8297 /* Check that PREV only sets the reload register. */
8298 set = single_set (prev);
8299 if (! set)
8300 return;
8301 dst = SET_DEST (set);
8302 if (!REG_P (dst)
8303 || ! rtx_equal_p (dst, x))
8304 return;
8305 if (! reg_set_p (dst, PATTERN (dead_insn)))
8307 /* Check if DST was used in a later insn -
8308 it might have been inherited. */
8309 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8311 if (LABEL_P (i2))
8312 break;
8313 if (! INSN_P (i2))
8314 continue;
8315 if (reg_referenced_p (dst, PATTERN (i2)))
8317 /* If there is a reference to the register in the current insn,
8318 it might be loaded in a non-inherited reload. If no other
8319 reload uses it, that means the register is set before
8320 referenced. */
8321 if (i2 == current_insn)
8323 for (j = n_reloads - 1; j >= 0; j--)
8324 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8325 || reload_override_in[j] == dst)
8326 return;
8327 for (j = n_reloads - 1; j >= 0; j--)
8328 if (rld[j].in && rld[j].reg_rtx == dst)
8329 break;
8330 if (j >= 0)
8331 break;
8333 return;
8335 if (JUMP_P (i2))
8336 break;
8337 /* If DST is still live at CURRENT_INSN, check if it is used for
8338 any reload. Note that even if CURRENT_INSN sets DST, we still
8339 have to check the reloads. */
8340 if (i2 == current_insn)
8342 for (j = n_reloads - 1; j >= 0; j--)
8343 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8344 || reload_override_in[j] == dst)
8345 return;
8346 /* ??? We can't finish the loop here, because dst might be
8347 allocated to a pseudo in this block if no reload in this
8348 block needs any of the classes containing DST - see
8349 spill_hard_reg. There is no easy way to tell this, so we
8350 have to scan till the end of the basic block. */
8352 if (reg_set_p (dst, PATTERN (i2)))
8353 break;
8356 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8357 reg_reloaded_contents[REGNO (dst)] = -1;
8358 delete_insn (prev);
8361 /* Output reload-insns to reload VALUE into RELOADREG.
8362 VALUE is an autoincrement or autodecrement RTX whose operand
8363 is a register or memory location;
8364 so reloading involves incrementing that location.
8365 IN is either identical to VALUE, or some cheaper place to reload from.
8367 INC_AMOUNT is the number to increment or decrement by (always positive).
8368 This cannot be deduced from VALUE.
8370 Return the instruction that stores into RELOADREG. */
8372 static rtx
8373 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
8375 /* REG or MEM to be copied and incremented. */
8376 rtx incloc = find_replacement (&XEXP (value, 0));
8377 /* Nonzero if increment after copying. */
8378 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
8379 || GET_CODE (value) == POST_MODIFY);
8380 rtx last;
8381 rtx inc;
8382 rtx add_insn;
8383 int code;
8384 rtx store;
8385 rtx real_in = in == value ? incloc : in;
8387 /* No hard register is equivalent to this register after
8388 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
8389 we could inc/dec that register as well (maybe even using it for
8390 the source), but I'm not sure it's worth worrying about. */
8391 if (REG_P (incloc))
8392 reg_last_reload_reg[REGNO (incloc)] = 0;
8394 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
8396 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
8397 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
8399 else
8401 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8402 inc_amount = -inc_amount;
8404 inc = GEN_INT (inc_amount);
8407 /* If this is post-increment, first copy the location to the reload reg. */
8408 if (post && real_in != reloadreg)
8409 emit_insn (gen_move_insn (reloadreg, real_in));
8411 if (in == value)
8413 /* See if we can directly increment INCLOC. Use a method similar to
8414 that in gen_reload. */
8416 last = get_last_insn ();
8417 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8418 gen_rtx_PLUS (GET_MODE (incloc),
8419 incloc, inc)));
8421 code = recog_memoized (add_insn);
8422 if (code >= 0)
8424 extract_insn (add_insn);
8425 if (constrain_operands (1))
8427 /* If this is a pre-increment and we have incremented the value
8428 where it lives, copy the incremented value to RELOADREG to
8429 be used as an address. */
8431 if (! post)
8432 emit_insn (gen_move_insn (reloadreg, incloc));
8434 return add_insn;
8437 delete_insns_since (last);
8440 /* If couldn't do the increment directly, must increment in RELOADREG.
8441 The way we do this depends on whether this is pre- or post-increment.
8442 For pre-increment, copy INCLOC to the reload register, increment it
8443 there, then save back. */
8445 if (! post)
8447 if (in != reloadreg)
8448 emit_insn (gen_move_insn (reloadreg, real_in));
8449 emit_insn (gen_add2_insn (reloadreg, inc));
8450 store = emit_insn (gen_move_insn (incloc, reloadreg));
8452 else
8454 /* Postincrement.
8455 Because this might be a jump insn or a compare, and because RELOADREG
8456 may not be available after the insn in an input reload, we must do
8457 the incrementation before the insn being reloaded for.
8459 We have already copied IN to RELOADREG. Increment the copy in
8460 RELOADREG, save that back, then decrement RELOADREG so it has
8461 the original value. */
8463 emit_insn (gen_add2_insn (reloadreg, inc));
8464 store = emit_insn (gen_move_insn (incloc, reloadreg));
8465 if (GET_CODE (inc) == CONST_INT)
8466 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
8467 else
8468 emit_insn (gen_sub2_insn (reloadreg, inc));
8471 return store;
8474 #ifdef AUTO_INC_DEC
8475 static void
8476 add_auto_inc_notes (rtx insn, rtx x)
8478 enum rtx_code code = GET_CODE (x);
8479 const char *fmt;
8480 int i, j;
8482 if (code == MEM && auto_inc_p (XEXP (x, 0)))
8484 REG_NOTES (insn)
8485 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
8486 return;
8489 /* Scan all the operand sub-expressions. */
8490 fmt = GET_RTX_FORMAT (code);
8491 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8493 if (fmt[i] == 'e')
8494 add_auto_inc_notes (insn, XEXP (x, i));
8495 else if (fmt[i] == 'E')
8496 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8497 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8500 #endif
8502 /* Copy EH notes from an insn to its reloads. */
8503 static void
8504 copy_eh_notes (rtx insn, rtx x)
8506 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8507 if (eh_note)
8509 for (; x != 0; x = NEXT_INSN (x))
8511 if (may_trap_p (PATTERN (x)))
8512 REG_NOTES (x)
8513 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8514 REG_NOTES (x));
8519 /* This is used by reload pass, that does emit some instructions after
8520 abnormal calls moving basic block end, but in fact it wants to emit
8521 them on the edge. Looks for abnormal call edges, find backward the
8522 proper call and fix the damage.
8524 Similar handle instructions throwing exceptions internally. */
8525 void
8526 fixup_abnormal_edges (void)
8528 bool inserted = false;
8529 basic_block bb;
8531 FOR_EACH_BB (bb)
8533 edge e;
8534 edge_iterator ei;
8536 /* Look for cases we are interested in - calls or instructions causing
8537 exceptions. */
8538 FOR_EACH_EDGE (e, ei, bb->succs)
8540 if (e->flags & EDGE_ABNORMAL_CALL)
8541 break;
8542 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8543 == (EDGE_ABNORMAL | EDGE_EH))
8544 break;
8546 if (e && !CALL_P (BB_END (bb))
8547 && !can_throw_internal (BB_END (bb)))
8549 rtx insn;
8551 /* Get past the new insns generated. Allow notes, as the insns
8552 may be already deleted. */
8553 insn = BB_END (bb);
8554 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8555 && !can_throw_internal (insn)
8556 && insn != BB_HEAD (bb))
8557 insn = PREV_INSN (insn);
8559 if (CALL_P (insn) || can_throw_internal (insn))
8561 rtx stop, next;
8563 stop = NEXT_INSN (BB_END (bb));
8564 BB_END (bb) = insn;
8565 insn = NEXT_INSN (insn);
8567 FOR_EACH_EDGE (e, ei, bb->succs)
8568 if (e->flags & EDGE_FALLTHRU)
8569 break;
8571 while (insn && insn != stop)
8573 next = NEXT_INSN (insn);
8574 if (INSN_P (insn))
8576 delete_insn (insn);
8578 /* Sometimes there's still the return value USE.
8579 If it's placed after a trapping call (i.e. that
8580 call is the last insn anyway), we have no fallthru
8581 edge. Simply delete this use and don't try to insert
8582 on the non-existent edge. */
8583 if (GET_CODE (PATTERN (insn)) != USE)
8585 /* We're not deleting it, we're moving it. */
8586 INSN_DELETED_P (insn) = 0;
8587 PREV_INSN (insn) = NULL_RTX;
8588 NEXT_INSN (insn) = NULL_RTX;
8590 insert_insn_on_edge (insn, e);
8591 inserted = true;
8594 insn = next;
8598 /* It may be that we don't find any such trapping insn. In this
8599 case we discovered quite late that the insn that had been
8600 marked as can_throw_internal in fact couldn't trap at all.
8601 So we should in fact delete the EH edges out of the block. */
8602 else
8603 purge_dead_edges (bb);
8607 /* We've possibly turned single trapping insn into multiple ones. */
8608 if (flag_non_call_exceptions)
8610 sbitmap blocks;
8611 blocks = sbitmap_alloc (last_basic_block);
8612 sbitmap_ones (blocks);
8613 find_many_sub_basic_blocks (blocks);
8616 if (inserted)
8617 commit_edge_insertions ();
8619 #ifdef ENABLE_CHECKING
8620 /* Verify that we didn't turn one trapping insn into many, and that
8621 we found and corrected all of the problems wrt fixups on the
8622 fallthru edge. */
8623 verify_flow_info ();
8624 #endif