2008-04-10 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / reload1.c
blob4ed8a10b5a79630022a83d89fcd815bff44dcddf
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
27 #include "machmode.h"
28 #include "hard-reg-set.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "obstack.h"
32 #include "insn-config.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "expr.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "addresses.h"
39 #include "basic-block.h"
40 #include "reload.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "real.h"
44 #include "toplev.h"
45 #include "except.h"
46 #include "tree.h"
47 #include "ira.h"
48 #include "df.h"
49 #include "target.h"
50 #include "dse.h"
52 /* This file contains the reload pass of the compiler, which is
53 run after register allocation has been done. It checks that
54 each insn is valid (operands required to be in registers really
55 are in registers of the proper class) and fixes up invalid ones
56 by copying values temporarily into registers for the insns
57 that need them.
59 The results of register allocation are described by the vector
60 reg_renumber; the insns still contain pseudo regs, but reg_renumber
61 can be used to find which hard reg, if any, a pseudo reg is in.
63 The technique we always use is to free up a few hard regs that are
64 called ``reload regs'', and for each place where a pseudo reg
65 must be in a hard reg, copy it temporarily into one of the reload regs.
67 Reload regs are allocated locally for every instruction that needs
68 reloads. When there are pseudos which are allocated to a register that
69 has been chosen as a reload reg, such pseudos must be ``spilled''.
70 This means that they go to other hard regs, or to stack slots if no other
71 available hard regs can be found. Spilling can invalidate more
72 insns, requiring additional need for reloads, so we must keep checking
73 until the process stabilizes.
75 For machines with different classes of registers, we must keep track
76 of the register class needed for each reload, and make sure that
77 we allocate enough reload registers of each class.
79 The file reload.c contains the code that checks one insn for
80 validity and reports the reloads that it needs. This file
81 is in charge of scanning the entire rtl code, accumulating the
82 reload needs, spilling, assigning reload registers to use for
83 fixing up each insn, and generating the new insns to copy values
84 into the reload registers. */
86 /* During reload_as_needed, element N contains a REG rtx for the hard reg
87 into which reg N has been reloaded (perhaps for a previous insn). */
88 static rtx *reg_last_reload_reg;
90 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
91 for an output reload that stores into reg N. */
92 static regset_head reg_has_output_reload;
94 /* Indicates which hard regs are reload-registers for an output reload
95 in the current insn. */
96 static HARD_REG_SET reg_is_output_reload;
98 /* Element N is the constant value to which pseudo reg N is equivalent,
99 or zero if pseudo reg N is not equivalent to a constant.
100 find_reloads looks at this in order to replace pseudo reg N
101 with the constant it stands for. */
102 rtx *reg_equiv_constant;
104 /* Element N is an invariant value to which pseudo reg N is equivalent.
105 eliminate_regs_in_insn uses this to replace pseudos in particular
106 contexts. */
107 rtx *reg_equiv_invariant;
109 /* Element N is a memory location to which pseudo reg N is equivalent,
110 prior to any register elimination (such as frame pointer to stack
111 pointer). Depending on whether or not it is a valid address, this value
112 is transferred to either reg_equiv_address or reg_equiv_mem. */
113 rtx *reg_equiv_memory_loc;
115 /* We allocate reg_equiv_memory_loc inside a varray so that the garbage
116 collector can keep track of what is inside. */
117 VEC(rtx,gc) *reg_equiv_memory_loc_vec;
119 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
120 This is used when the address is not valid as a memory address
121 (because its displacement is too big for the machine.) */
122 rtx *reg_equiv_address;
124 /* Element N is the memory slot to which pseudo reg N is equivalent,
125 or zero if pseudo reg N is not equivalent to a memory slot. */
126 rtx *reg_equiv_mem;
128 /* Element N is an EXPR_LIST of REG_EQUIVs containing MEMs with
129 alternate representations of the location of pseudo reg N. */
130 rtx *reg_equiv_alt_mem_list;
132 /* Widest width in which each pseudo reg is referred to (via subreg). */
133 static unsigned int *reg_max_ref_width;
135 /* Element N is the list of insns that initialized reg N from its equivalent
136 constant or memory slot. */
137 rtx *reg_equiv_init;
138 int reg_equiv_init_size;
140 /* Vector to remember old contents of reg_renumber before spilling. */
141 static short *reg_old_renumber;
143 /* During reload_as_needed, element N contains the last pseudo regno reloaded
144 into hard register N. If that pseudo reg occupied more than one register,
145 reg_reloaded_contents points to that pseudo for each spill register in
146 use; all of these must remain set for an inheritance to occur. */
147 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
149 /* During reload_as_needed, element N contains the insn for which
150 hard register N was last used. Its contents are significant only
151 when reg_reloaded_valid is set for this register. */
152 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
154 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
155 static HARD_REG_SET reg_reloaded_valid;
156 /* Indicate if the register was dead at the end of the reload.
157 This is only valid if reg_reloaded_contents is set and valid. */
158 static HARD_REG_SET reg_reloaded_dead;
160 /* Indicate whether the register's current value is one that is not
161 safe to retain across a call, even for registers that are normally
162 call-saved. This is only meaningful for members of reg_reloaded_valid. */
163 static HARD_REG_SET reg_reloaded_call_part_clobbered;
165 /* Number of spill-regs so far; number of valid elements of spill_regs. */
166 static int n_spills;
168 /* In parallel with spill_regs, contains REG rtx's for those regs.
169 Holds the last rtx used for any given reg, or 0 if it has never
170 been used for spilling yet. This rtx is reused, provided it has
171 the proper mode. */
172 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
174 /* In parallel with spill_regs, contains nonzero for a spill reg
175 that was stored after the last time it was used.
176 The precise value is the insn generated to do the store. */
177 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
179 /* This is the register that was stored with spill_reg_store. This is a
180 copy of reload_out / reload_out_reg when the value was stored; if
181 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
182 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
184 /* This table is the inverse mapping of spill_regs:
185 indexed by hard reg number,
186 it contains the position of that reg in spill_regs,
187 or -1 for something that is not in spill_regs.
189 ?!? This is no longer accurate. */
190 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
192 /* This reg set indicates registers that can't be used as spill registers for
193 the currently processed insn. These are the hard registers which are live
194 during the insn, but not allocated to pseudos, as well as fixed
195 registers. */
196 static HARD_REG_SET bad_spill_regs;
198 /* These are the hard registers that can't be used as spill register for any
199 insn. This includes registers used for user variables and registers that
200 we can't eliminate. A register that appears in this set also can't be used
201 to retry register allocation. */
202 static HARD_REG_SET bad_spill_regs_global;
204 /* Describes order of use of registers for reloading
205 of spilled pseudo-registers. `n_spills' is the number of
206 elements that are actually valid; new ones are added at the end.
208 Both spill_regs and spill_reg_order are used on two occasions:
209 once during find_reload_regs, where they keep track of the spill registers
210 for a single insn, but also during reload_as_needed where they show all
211 the registers ever used by reload. For the latter case, the information
212 is calculated during finish_spills. */
213 static short spill_regs[FIRST_PSEUDO_REGISTER];
215 /* This vector of reg sets indicates, for each pseudo, which hard registers
216 may not be used for retrying global allocation because the register was
217 formerly spilled from one of them. If we allowed reallocating a pseudo to
218 a register that it was already allocated to, reload might not
219 terminate. */
220 static HARD_REG_SET *pseudo_previous_regs;
222 /* This vector of reg sets indicates, for each pseudo, which hard
223 registers may not be used for retrying global allocation because they
224 are used as spill registers during one of the insns in which the
225 pseudo is live. */
226 static HARD_REG_SET *pseudo_forbidden_regs;
228 /* All hard regs that have been used as spill registers for any insn are
229 marked in this set. */
230 static HARD_REG_SET used_spill_regs;
232 /* Index of last register assigned as a spill register. We allocate in
233 a round-robin fashion. */
234 static int last_spill_reg;
236 /* Nonzero if indirect addressing is supported on the machine; this means
237 that spilling (REG n) does not require reloading it into a register in
238 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
239 value indicates the level of indirect addressing supported, e.g., two
240 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
241 a hard register. */
242 static char spill_indirect_levels;
244 /* Nonzero if indirect addressing is supported when the innermost MEM is
245 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
246 which these are valid is the same as spill_indirect_levels, above. */
247 char indirect_symref_ok;
249 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
250 char double_reg_address_ok;
252 /* Record the stack slot for each spilled hard register. */
253 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
255 /* Width allocated so far for that stack slot. */
256 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
258 /* Record which pseudos needed to be spilled. */
259 static regset_head spilled_pseudos;
261 /* Used for communication between order_regs_for_reload and count_pseudo.
262 Used to avoid counting one pseudo twice. */
263 static regset_head pseudos_counted;
265 /* First uid used by insns created by reload in this function.
266 Used in find_equiv_reg. */
267 int reload_first_uid;
269 /* Flag set by local-alloc or global-alloc if anything is live in
270 a call-clobbered reg across calls. */
271 int caller_save_needed;
273 /* Set to 1 while reload_as_needed is operating.
274 Required by some machines to handle any generated moves differently. */
275 int reload_in_progress = 0;
277 /* These arrays record the insn_code of insns that may be needed to
278 perform input and output reloads of special objects. They provide a
279 place to pass a scratch register. */
280 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
281 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
283 /* This obstack is used for allocation of rtl during register elimination.
284 The allocated storage can be freed once find_reloads has processed the
285 insn. */
286 static struct obstack reload_obstack;
288 /* Points to the beginning of the reload_obstack. All insn_chain structures
289 are allocated first. */
290 static char *reload_startobj;
292 /* The point after all insn_chain structures. Used to quickly deallocate
293 memory allocated in copy_reloads during calculate_needs_all_insns. */
294 static char *reload_firstobj;
296 /* This points before all local rtl generated by register elimination.
297 Used to quickly free all memory after processing one insn. */
298 static char *reload_insn_firstobj;
300 /* List of insn_chain instructions, one for every insn that reload needs to
301 examine. */
302 struct insn_chain *reload_insn_chain;
304 /* List of all insns needing reloads. */
305 static struct insn_chain *insns_need_reload;
307 /* This structure is used to record information about register eliminations.
308 Each array entry describes one possible way of eliminating a register
309 in favor of another. If there is more than one way of eliminating a
310 particular register, the most preferred should be specified first. */
312 struct elim_table
314 int from; /* Register number to be eliminated. */
315 int to; /* Register number used as replacement. */
316 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
317 int can_eliminate; /* Nonzero if this elimination can be done. */
318 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
319 insns made by reload. */
320 HOST_WIDE_INT offset; /* Current offset between the two regs. */
321 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
322 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
323 rtx from_rtx; /* REG rtx for the register to be eliminated.
324 We cannot simply compare the number since
325 we might then spuriously replace a hard
326 register corresponding to a pseudo
327 assigned to the reg to be eliminated. */
328 rtx to_rtx; /* REG rtx for the replacement. */
331 static struct elim_table *reg_eliminate = 0;
333 /* This is an intermediate structure to initialize the table. It has
334 exactly the members provided by ELIMINABLE_REGS. */
335 static const struct elim_table_1
337 const int from;
338 const int to;
339 } reg_eliminate_1[] =
341 /* If a set of eliminable registers was specified, define the table from it.
342 Otherwise, default to the normal case of the frame pointer being
343 replaced by the stack pointer. */
345 #ifdef ELIMINABLE_REGS
346 ELIMINABLE_REGS;
347 #else
348 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
349 #endif
351 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
353 /* Record the number of pending eliminations that have an offset not equal
354 to their initial offset. If nonzero, we use a new copy of each
355 replacement result in any insns encountered. */
356 int num_not_at_initial_offset;
358 /* Count the number of registers that we may be able to eliminate. */
359 static int num_eliminable;
360 /* And the number of registers that are equivalent to a constant that
361 can be eliminated to frame_pointer / arg_pointer + constant. */
362 static int num_eliminable_invariants;
364 /* For each label, we record the offset of each elimination. If we reach
365 a label by more than one path and an offset differs, we cannot do the
366 elimination. This information is indexed by the difference of the
367 number of the label and the first label number. We can't offset the
368 pointer itself as this can cause problems on machines with segmented
369 memory. The first table is an array of flags that records whether we
370 have yet encountered a label and the second table is an array of arrays,
371 one entry in the latter array for each elimination. */
373 static int first_label_num;
374 static char *offsets_known_at;
375 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
377 /* Number of labels in the current function. */
379 static int num_labels;
381 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
382 static void maybe_fix_stack_asms (void);
383 static void copy_reloads (struct insn_chain *);
384 static void calculate_needs_all_insns (int);
385 static int find_reg (struct insn_chain *, int);
386 static void find_reload_regs (struct insn_chain *);
387 static void select_reload_regs (void);
388 static void delete_caller_save_insns (void);
390 static void spill_failure (rtx, enum reg_class);
391 static void count_spilled_pseudo (int, int, int);
392 static void delete_dead_insn (rtx);
393 static void alter_reg (int, int, bool);
394 static void set_label_offsets (rtx, rtx, int);
395 static void check_eliminable_occurrences (rtx);
396 static void elimination_effects (rtx, enum machine_mode);
397 static int eliminate_regs_in_insn (rtx, int);
398 static void update_eliminable_offsets (void);
399 static void mark_not_eliminable (rtx, const_rtx, void *);
400 static void set_initial_elim_offsets (void);
401 static bool verify_initial_elim_offsets (void);
402 static void set_initial_label_offsets (void);
403 static void set_offsets_for_label (rtx);
404 static void init_elim_table (void);
405 static void update_eliminables (HARD_REG_SET *);
406 static void spill_hard_reg (unsigned int, int);
407 static int finish_spills (int);
408 static void scan_paradoxical_subregs (rtx);
409 static void count_pseudo (int);
410 static void order_regs_for_reload (struct insn_chain *);
411 static void reload_as_needed (int);
412 static void forget_old_reloads_1 (rtx, const_rtx, void *);
413 static void forget_marked_reloads (regset);
414 static int reload_reg_class_lower (const void *, const void *);
415 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
416 enum machine_mode);
417 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
418 enum machine_mode);
419 static int reload_reg_free_p (unsigned int, int, enum reload_type);
420 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
421 rtx, rtx, int, int);
422 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
423 rtx, rtx, int, int);
424 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
425 static int allocate_reload_reg (struct insn_chain *, int, int);
426 static int conflicts_with_override (rtx);
427 static void failed_reload (rtx, int);
428 static int set_reload_reg (int, int);
429 static void choose_reload_regs_init (struct insn_chain *, rtx *);
430 static void choose_reload_regs (struct insn_chain *);
431 static void merge_assigned_reloads (rtx);
432 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
433 rtx, int);
434 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
435 int);
436 static void do_input_reload (struct insn_chain *, struct reload *, int);
437 static void do_output_reload (struct insn_chain *, struct reload *, int);
438 static void emit_reload_insns (struct insn_chain *);
439 static void delete_output_reload (rtx, int, int, rtx);
440 static void delete_address_reloads (rtx, rtx);
441 static void delete_address_reloads_1 (rtx, rtx, rtx);
442 static rtx inc_for_reload (rtx, rtx, rtx, int);
443 #ifdef AUTO_INC_DEC
444 static void add_auto_inc_notes (rtx, rtx);
445 #endif
446 static void copy_eh_notes (rtx, rtx);
447 static void substitute (rtx *, const_rtx, rtx);
448 static bool gen_reload_chain_without_interm_reg_p (int, int);
449 static int reloads_conflict (int, int);
450 static rtx gen_reload (rtx, rtx, int, enum reload_type);
451 static rtx emit_insn_if_valid_for_reload (rtx);
453 /* Initialize the reload pass. This is called at the beginning of compilation
454 and may be called again if the target is reinitialized. */
456 void
457 init_reload (void)
459 int i;
461 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
462 Set spill_indirect_levels to the number of levels such addressing is
463 permitted, zero if it is not permitted at all. */
465 rtx tem
466 = gen_rtx_MEM (Pmode,
467 gen_rtx_PLUS (Pmode,
468 gen_rtx_REG (Pmode,
469 LAST_VIRTUAL_REGISTER + 1),
470 GEN_INT (4)));
471 spill_indirect_levels = 0;
473 while (memory_address_p (QImode, tem))
475 spill_indirect_levels++;
476 tem = gen_rtx_MEM (Pmode, tem);
479 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
481 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
482 indirect_symref_ok = memory_address_p (QImode, tem);
484 /* See if reg+reg is a valid (and offsettable) address. */
486 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
488 tem = gen_rtx_PLUS (Pmode,
489 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
490 gen_rtx_REG (Pmode, i));
492 /* This way, we make sure that reg+reg is an offsettable address. */
493 tem = plus_constant (tem, 4);
495 if (memory_address_p (QImode, tem))
497 double_reg_address_ok = 1;
498 break;
502 /* Initialize obstack for our rtl allocation. */
503 gcc_obstack_init (&reload_obstack);
504 reload_startobj = obstack_alloc (&reload_obstack, 0);
506 INIT_REG_SET (&spilled_pseudos);
507 INIT_REG_SET (&pseudos_counted);
510 /* List of insn chains that are currently unused. */
511 static struct insn_chain *unused_insn_chains = 0;
513 /* Allocate an empty insn_chain structure. */
514 struct insn_chain *
515 new_insn_chain (void)
517 struct insn_chain *c;
519 if (unused_insn_chains == 0)
521 c = obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
522 INIT_REG_SET (&c->live_throughout);
523 INIT_REG_SET (&c->dead_or_set);
525 else
527 c = unused_insn_chains;
528 unused_insn_chains = c->next;
530 c->is_caller_save_insn = 0;
531 c->need_operand_change = 0;
532 c->need_reload = 0;
533 c->need_elim = 0;
534 return c;
537 /* Small utility function to set all regs in hard reg set TO which are
538 allocated to pseudos in regset FROM. */
540 void
541 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
543 unsigned int regno;
544 reg_set_iterator rsi;
546 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
548 int r = reg_renumber[regno];
550 if (r < 0)
552 /* reload_combine uses the information from DF_LIVE_IN,
553 which might still contain registers that have not
554 actually been allocated since they have an
555 equivalence. */
556 gcc_assert (flag_ira || reload_completed);
558 else
559 add_to_hard_reg_set (to, PSEUDO_REGNO_MODE (regno), r);
563 /* Replace all pseudos found in LOC with their corresponding
564 equivalences. */
566 static void
567 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
569 rtx x = *loc;
570 enum rtx_code code;
571 const char *fmt;
572 int i, j;
574 if (! x)
575 return;
577 code = GET_CODE (x);
578 if (code == REG)
580 unsigned int regno = REGNO (x);
582 if (regno < FIRST_PSEUDO_REGISTER)
583 return;
585 x = eliminate_regs (x, mem_mode, usage);
586 if (x != *loc)
588 *loc = x;
589 replace_pseudos_in (loc, mem_mode, usage);
590 return;
593 if (reg_equiv_constant[regno])
594 *loc = reg_equiv_constant[regno];
595 else if (reg_equiv_mem[regno])
596 *loc = reg_equiv_mem[regno];
597 else if (reg_equiv_address[regno])
598 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
599 else
601 gcc_assert (!REG_P (regno_reg_rtx[regno])
602 || REGNO (regno_reg_rtx[regno]) != regno);
603 *loc = regno_reg_rtx[regno];
606 return;
608 else if (code == MEM)
610 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
611 return;
614 /* Process each of our operands recursively. */
615 fmt = GET_RTX_FORMAT (code);
616 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
617 if (*fmt == 'e')
618 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
619 else if (*fmt == 'E')
620 for (j = 0; j < XVECLEN (x, i); j++)
621 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
624 /* Determine if the current function has an exception receiver block
625 that reaches the exit block via non-exceptional edges */
627 static bool
628 has_nonexceptional_receiver (void)
630 edge e;
631 edge_iterator ei;
632 basic_block *tos, *worklist, bb;
634 /* If we're not optimizing, then just err on the safe side. */
635 if (!optimize)
636 return true;
638 /* First determine which blocks can reach exit via normal paths. */
639 tos = worklist = xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
641 FOR_EACH_BB (bb)
642 bb->flags &= ~BB_REACHABLE;
644 /* Place the exit block on our worklist. */
645 EXIT_BLOCK_PTR->flags |= BB_REACHABLE;
646 *tos++ = EXIT_BLOCK_PTR;
648 /* Iterate: find everything reachable from what we've already seen. */
649 while (tos != worklist)
651 bb = *--tos;
653 FOR_EACH_EDGE (e, ei, bb->preds)
654 if (!(e->flags & EDGE_ABNORMAL))
656 basic_block src = e->src;
658 if (!(src->flags & BB_REACHABLE))
660 src->flags |= BB_REACHABLE;
661 *tos++ = src;
665 free (worklist);
667 /* Now see if there's a reachable block with an exceptional incoming
668 edge. */
669 FOR_EACH_BB (bb)
670 if (bb->flags & BB_REACHABLE)
671 FOR_EACH_EDGE (e, ei, bb->preds)
672 if (e->flags & EDGE_ABNORMAL)
673 return true;
675 /* No exceptional block reached exit unexceptionally. */
676 return false;
680 /* Global variables used by reload and its subroutines. */
682 /* Set during calculate_needs if an insn needs register elimination. */
683 static int something_needs_elimination;
684 /* Set during calculate_needs if an insn needs an operand changed. */
685 static int something_needs_operands_changed;
687 /* Nonzero means we couldn't get enough spill regs. */
688 static int failure;
690 /* Temporary array of pseudo-register number. */
691 static int *temp_pseudo_reg_arr;
693 /* Main entry point for the reload pass.
695 FIRST is the first insn of the function being compiled.
697 GLOBAL nonzero means we were called from global_alloc
698 and should attempt to reallocate any pseudoregs that we
699 displace from hard regs we will use for reloads.
700 If GLOBAL is zero, we do not have enough information to do that,
701 so any pseudo reg that is spilled must go to the stack.
703 Return value is nonzero if reload failed
704 and we must not do any more for this function. */
707 reload (rtx first, int global)
709 int i, n;
710 rtx insn;
711 struct elim_table *ep;
712 basic_block bb;
714 /* Make sure even insns with volatile mem refs are recognizable. */
715 init_recog ();
717 failure = 0;
719 reload_firstobj = obstack_alloc (&reload_obstack, 0);
721 /* Make sure that the last insn in the chain
722 is not something that needs reloading. */
723 emit_note (NOTE_INSN_DELETED);
725 /* Enable find_equiv_reg to distinguish insns made by reload. */
726 reload_first_uid = get_max_uid ();
728 #ifdef SECONDARY_MEMORY_NEEDED
729 /* Initialize the secondary memory table. */
730 clear_secondary_mem ();
731 #endif
733 /* We don't have a stack slot for any spill reg yet. */
734 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
735 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
737 /* Initialize the save area information for caller-save, in case some
738 are needed. */
739 init_save_areas ();
741 /* Compute which hard registers are now in use
742 as homes for pseudo registers.
743 This is done here rather than (eg) in global_alloc
744 because this point is reached even if not optimizing. */
745 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
746 mark_home_live (i);
748 /* A function that has a nonlocal label that can reach the exit
749 block via non-exceptional paths must save all call-saved
750 registers. */
751 if (current_function_has_nonlocal_label
752 && has_nonexceptional_receiver ())
753 current_function_saves_all_registers = 1;
755 if (current_function_saves_all_registers)
756 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
757 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
758 df_set_regs_ever_live (i, true);
760 /* Find all the pseudo registers that didn't get hard regs
761 but do have known equivalent constants or memory slots.
762 These include parameters (known equivalent to parameter slots)
763 and cse'd or loop-moved constant memory addresses.
765 Record constant equivalents in reg_equiv_constant
766 so they will be substituted by find_reloads.
767 Record memory equivalents in reg_mem_equiv so they can
768 be substituted eventually by altering the REG-rtx's. */
770 reg_equiv_constant = XCNEWVEC (rtx, max_regno);
771 reg_equiv_invariant = XCNEWVEC (rtx, max_regno);
772 reg_equiv_mem = XCNEWVEC (rtx, max_regno);
773 reg_equiv_alt_mem_list = XCNEWVEC (rtx, max_regno);
774 reg_equiv_address = XCNEWVEC (rtx, max_regno);
775 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
776 reg_old_renumber = XCNEWVEC (short, max_regno);
777 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
778 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
779 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
781 CLEAR_HARD_REG_SET (bad_spill_regs_global);
783 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
784 to. Also find all paradoxical subregs and find largest such for
785 each pseudo. */
787 num_eliminable_invariants = 0;
788 for (insn = first; insn; insn = NEXT_INSN (insn))
790 rtx set = single_set (insn);
792 /* We may introduce USEs that we want to remove at the end, so
793 we'll mark them with QImode. Make sure there are no
794 previously-marked insns left by say regmove. */
795 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
796 && GET_MODE (insn) != VOIDmode)
797 PUT_MODE (insn, VOIDmode);
799 if (INSN_P (insn))
800 scan_paradoxical_subregs (PATTERN (insn));
802 if (set != 0 && REG_P (SET_DEST (set)))
804 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
805 rtx x;
807 if (! note)
808 continue;
810 i = REGNO (SET_DEST (set));
811 x = XEXP (note, 0);
813 if (i <= LAST_VIRTUAL_REGISTER)
814 continue;
816 if (! function_invariant_p (x)
817 || ! flag_pic
818 /* A function invariant is often CONSTANT_P but may
819 include a register. We promise to only pass
820 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
821 || (CONSTANT_P (x)
822 && LEGITIMATE_PIC_OPERAND_P (x)))
824 /* It can happen that a REG_EQUIV note contains a MEM
825 that is not a legitimate memory operand. As later
826 stages of reload assume that all addresses found
827 in the reg_equiv_* arrays were originally legitimate,
828 we ignore such REG_EQUIV notes. */
829 if (memory_operand (x, VOIDmode))
831 /* Always unshare the equivalence, so we can
832 substitute into this insn without touching the
833 equivalence. */
834 reg_equiv_memory_loc[i] = copy_rtx (x);
836 else if (function_invariant_p (x))
838 if (GET_CODE (x) == PLUS)
840 /* This is PLUS of frame pointer and a constant,
841 and might be shared. Unshare it. */
842 reg_equiv_invariant[i] = copy_rtx (x);
843 num_eliminable_invariants++;
845 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
847 reg_equiv_invariant[i] = x;
848 num_eliminable_invariants++;
850 else if (LEGITIMATE_CONSTANT_P (x))
851 reg_equiv_constant[i] = x;
852 else
854 reg_equiv_memory_loc[i]
855 = force_const_mem (GET_MODE (SET_DEST (set)), x);
856 if (! reg_equiv_memory_loc[i])
857 reg_equiv_init[i] = NULL_RTX;
860 else
862 reg_equiv_init[i] = NULL_RTX;
863 continue;
866 else
867 reg_equiv_init[i] = NULL_RTX;
871 if (dump_file)
872 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
873 if (reg_equiv_init[i])
875 fprintf (dump_file, "init_insns for %u: ", i);
876 print_inline_rtx (dump_file, reg_equiv_init[i], 20);
877 fprintf (dump_file, "\n");
880 init_elim_table ();
882 first_label_num = get_first_label_num ();
883 num_labels = max_label_num () - first_label_num;
885 /* Allocate the tables used to store offset information at labels. */
886 /* We used to use alloca here, but the size of what it would try to
887 allocate would occasionally cause it to exceed the stack limit and
888 cause a core dump. */
889 offsets_known_at = XNEWVEC (char, num_labels);
890 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
892 /* Alter each pseudo-reg rtx to contain its hard reg number. Assign
893 stack slots to the pseudos that lack hard regs or equivalents.
894 Do not touch virtual registers. */
896 temp_pseudo_reg_arr = XNEWVEC (int, max_regno - LAST_VIRTUAL_REGISTER - 1);
897 for (n = 0, i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
898 temp_pseudo_reg_arr[n++] = i;
900 if (flag_ira)
901 /* Ask IRA to order pseudo-registers for better stack slot
902 sharing. */
903 sort_regnos_for_alter_reg (temp_pseudo_reg_arr, n, reg_max_ref_width);
905 for (i = 0; i < n; i++)
906 alter_reg (temp_pseudo_reg_arr[i], -1, false);
908 /* If we have some registers we think can be eliminated, scan all insns to
909 see if there is an insn that sets one of these registers to something
910 other than itself plus a constant. If so, the register cannot be
911 eliminated. Doing this scan here eliminates an extra pass through the
912 main reload loop in the most common case where register elimination
913 cannot be done. */
914 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
915 if (INSN_P (insn))
916 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
918 maybe_fix_stack_asms ();
920 insns_need_reload = 0;
921 something_needs_elimination = 0;
923 /* Initialize to -1, which means take the first spill register. */
924 last_spill_reg = -1;
926 /* Spill any hard regs that we know we can't eliminate. */
927 CLEAR_HARD_REG_SET (used_spill_regs);
928 /* There can be multiple ways to eliminate a register;
929 they should be listed adjacently.
930 Elimination for any register fails only if all possible ways fail. */
931 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
933 int from = ep->from;
934 int can_eliminate = 0;
937 can_eliminate |= ep->can_eliminate;
938 ep++;
940 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
941 if (! can_eliminate)
942 spill_hard_reg (from, 1);
945 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
946 if (frame_pointer_needed)
947 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
948 #endif
949 finish_spills (global);
951 /* From now on, we may need to generate moves differently. We may also
952 allow modifications of insns which cause them to not be recognized.
953 Any such modifications will be cleaned up during reload itself. */
954 reload_in_progress = 1;
956 /* This loop scans the entire function each go-round
957 and repeats until one repetition spills no additional hard regs. */
958 for (;;)
960 int something_changed;
961 int did_spill;
962 HOST_WIDE_INT starting_frame_size;
964 starting_frame_size = get_frame_size ();
966 set_initial_elim_offsets ();
967 set_initial_label_offsets ();
969 /* For each pseudo register that has an equivalent location defined,
970 try to eliminate any eliminable registers (such as the frame pointer)
971 assuming initial offsets for the replacement register, which
972 is the normal case.
974 If the resulting location is directly addressable, substitute
975 the MEM we just got directly for the old REG.
977 If it is not addressable but is a constant or the sum of a hard reg
978 and constant, it is probably not addressable because the constant is
979 out of range, in that case record the address; we will generate
980 hairy code to compute the address in a register each time it is
981 needed. Similarly if it is a hard register, but one that is not
982 valid as an address register.
984 If the location is not addressable, but does not have one of the
985 above forms, assign a stack slot. We have to do this to avoid the
986 potential of producing lots of reloads if, e.g., a location involves
987 a pseudo that didn't get a hard register and has an equivalent memory
988 location that also involves a pseudo that didn't get a hard register.
990 Perhaps at some point we will improve reload_when_needed handling
991 so this problem goes away. But that's very hairy. */
993 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
994 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
996 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
998 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
999 XEXP (x, 0)))
1000 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
1001 else if (CONSTANT_P (XEXP (x, 0))
1002 || (REG_P (XEXP (x, 0))
1003 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
1004 || (GET_CODE (XEXP (x, 0)) == PLUS
1005 && REG_P (XEXP (XEXP (x, 0), 0))
1006 && (REGNO (XEXP (XEXP (x, 0), 0))
1007 < FIRST_PSEUDO_REGISTER)
1008 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
1009 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
1010 else
1012 /* Make a new stack slot. Then indicate that something
1013 changed so we go back and recompute offsets for
1014 eliminable registers because the allocation of memory
1015 below might change some offset. reg_equiv_{mem,address}
1016 will be set up for this pseudo on the next pass around
1017 the loop. */
1018 reg_equiv_memory_loc[i] = 0;
1019 reg_equiv_init[i] = 0;
1020 alter_reg (i, -1, true);
1024 if (caller_save_needed)
1025 setup_save_areas ();
1027 /* If we allocated another stack slot, redo elimination bookkeeping. */
1028 if (starting_frame_size != get_frame_size ())
1029 continue;
1030 if (starting_frame_size && cfun->stack_alignment_needed)
1032 /* If we have a stack frame, we must align it now. The
1033 stack size may be a part of the offset computation for
1034 register elimination. So if this changes the stack size,
1035 then repeat the elimination bookkeeping. We don't
1036 realign when there is no stack, as that will cause a
1037 stack frame when none is needed should
1038 STARTING_FRAME_OFFSET not be already aligned to
1039 STACK_BOUNDARY. */
1040 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
1041 if (starting_frame_size != get_frame_size ())
1042 continue;
1045 if (caller_save_needed)
1047 save_call_clobbered_regs ();
1048 /* That might have allocated new insn_chain structures. */
1049 reload_firstobj = obstack_alloc (&reload_obstack, 0);
1052 calculate_needs_all_insns (global);
1054 if (! flag_ira)
1055 /* Don't do it for IRA. We need this info because we don't
1056 change live_throughout and dead_or_set for chains when IRA
1057 is used. */
1058 CLEAR_REG_SET (&spilled_pseudos);
1060 did_spill = 0;
1062 something_changed = 0;
1064 /* If we allocated any new memory locations, make another pass
1065 since it might have changed elimination offsets. */
1066 if (starting_frame_size != get_frame_size ())
1067 something_changed = 1;
1069 /* Even if the frame size remained the same, we might still have
1070 changed elimination offsets, e.g. if find_reloads called
1071 force_const_mem requiring the back end to allocate a constant
1072 pool base register that needs to be saved on the stack. */
1073 else if (!verify_initial_elim_offsets ())
1074 something_changed = 1;
1077 HARD_REG_SET to_spill;
1078 CLEAR_HARD_REG_SET (to_spill);
1079 update_eliminables (&to_spill);
1080 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
1082 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1083 if (TEST_HARD_REG_BIT (to_spill, i))
1085 spill_hard_reg (i, 1);
1086 did_spill = 1;
1088 /* Regardless of the state of spills, if we previously had
1089 a register that we thought we could eliminate, but now can
1090 not eliminate, we must run another pass.
1092 Consider pseudos which have an entry in reg_equiv_* which
1093 reference an eliminable register. We must make another pass
1094 to update reg_equiv_* so that we do not substitute in the
1095 old value from when we thought the elimination could be
1096 performed. */
1097 something_changed = 1;
1101 select_reload_regs ();
1102 if (failure)
1103 goto failed;
1105 if (insns_need_reload != 0 || did_spill)
1106 something_changed |= finish_spills (global);
1108 if (! something_changed)
1109 break;
1111 if (caller_save_needed)
1112 delete_caller_save_insns ();
1114 obstack_free (&reload_obstack, reload_firstobj);
1117 if (flag_ira)
1118 /* Restore the original insn chain order for correct reload
1119 work. */
1120 sort_insn_chain (FALSE);
1122 /* If global-alloc was run, notify it of any register eliminations we have
1123 done. */
1124 if (global)
1125 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1126 if (ep->can_eliminate)
1127 mark_elimination (ep->from, ep->to);
1129 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1130 If that insn didn't set the register (i.e., it copied the register to
1131 memory), just delete that insn instead of the equivalencing insn plus
1132 anything now dead. If we call delete_dead_insn on that insn, we may
1133 delete the insn that actually sets the register if the register dies
1134 there and that is incorrect. */
1136 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1138 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1140 rtx list;
1141 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1143 rtx equiv_insn = XEXP (list, 0);
1145 /* If we already deleted the insn or if it may trap, we can't
1146 delete it. The latter case shouldn't happen, but can
1147 if an insn has a variable address, gets a REG_EH_REGION
1148 note added to it, and then gets converted into a load
1149 from a constant address. */
1150 if (NOTE_P (equiv_insn)
1151 || can_throw_internal (equiv_insn))
1153 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1154 delete_dead_insn (equiv_insn);
1155 else
1156 SET_INSN_DELETED (equiv_insn);
1161 /* Use the reload registers where necessary
1162 by generating move instructions to move the must-be-register
1163 values into or out of the reload registers. */
1165 if (insns_need_reload != 0 || something_needs_elimination
1166 || something_needs_operands_changed)
1168 HOST_WIDE_INT old_frame_size = get_frame_size ();
1170 reload_as_needed (global);
1172 gcc_assert (old_frame_size == get_frame_size ());
1174 gcc_assert (verify_initial_elim_offsets ());
1177 /* If we were able to eliminate the frame pointer, show that it is no
1178 longer live at the start of any basic block. If it ls live by
1179 virtue of being in a pseudo, that pseudo will be marked live
1180 and hence the frame pointer will be known to be live via that
1181 pseudo. */
1183 if (! frame_pointer_needed)
1184 FOR_EACH_BB (bb)
1185 bitmap_clear_bit (df_get_live_in (bb), HARD_FRAME_POINTER_REGNUM);
1187 /* Come here (with failure set nonzero) if we can't get enough spill
1188 regs. */
1189 failed:
1191 CLEAR_REG_SET (&spilled_pseudos);
1192 reload_in_progress = 0;
1194 /* Now eliminate all pseudo regs by modifying them into
1195 their equivalent memory references.
1196 The REG-rtx's for the pseudos are modified in place,
1197 so all insns that used to refer to them now refer to memory.
1199 For a reg that has a reg_equiv_address, all those insns
1200 were changed by reloading so that no insns refer to it any longer;
1201 but the DECL_RTL of a variable decl may refer to it,
1202 and if so this causes the debugging info to mention the variable. */
1204 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1206 rtx addr = 0;
1208 if (reg_equiv_mem[i])
1209 addr = XEXP (reg_equiv_mem[i], 0);
1211 if (reg_equiv_address[i])
1212 addr = reg_equiv_address[i];
1214 if (addr)
1216 if (reg_renumber[i] < 0)
1218 rtx reg = regno_reg_rtx[i];
1220 REG_USERVAR_P (reg) = 0;
1221 PUT_CODE (reg, MEM);
1222 XEXP (reg, 0) = addr;
1223 if (reg_equiv_memory_loc[i])
1224 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1225 else
1227 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1228 MEM_ATTRS (reg) = 0;
1230 MEM_NOTRAP_P (reg) = 1;
1232 else if (reg_equiv_mem[i])
1233 XEXP (reg_equiv_mem[i], 0) = addr;
1237 /* We must set reload_completed now since the cleanup_subreg_operands call
1238 below will re-recognize each insn and reload may have generated insns
1239 which are only valid during and after reload. */
1240 reload_completed = 1;
1242 /* Make a pass over all the insns and delete all USEs which we inserted
1243 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1244 notes. Delete all CLOBBER insns, except those that refer to the return
1245 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1246 from misarranging variable-array code, and simplify (subreg (reg))
1247 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1248 are no longer useful or accurate. Strip and regenerate REG_INC notes
1249 that may have been moved around. */
1251 for (insn = first; insn; insn = NEXT_INSN (insn))
1252 if (INSN_P (insn))
1254 rtx *pnote;
1256 if (CALL_P (insn))
1258 HARD_REG_SET used_function_regs;
1260 get_call_invalidated_used_regs (insn, &used_function_regs, false);
1261 IOR_HARD_REG_SET (cfun->emit->call_used_regs, used_function_regs);
1262 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1263 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1266 if ((GET_CODE (PATTERN (insn)) == USE
1267 /* We mark with QImode USEs introduced by reload itself. */
1268 && (GET_MODE (insn) == QImode
1269 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1270 || (GET_CODE (PATTERN (insn)) == CLOBBER
1271 && (!MEM_P (XEXP (PATTERN (insn), 0))
1272 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1273 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1274 && XEXP (XEXP (PATTERN (insn), 0), 0)
1275 != stack_pointer_rtx))
1276 && (!REG_P (XEXP (PATTERN (insn), 0))
1277 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1279 delete_insn (insn);
1280 continue;
1283 /* Some CLOBBERs may survive until here and still reference unassigned
1284 pseudos with const equivalent, which may in turn cause ICE in later
1285 passes if the reference remains in place. */
1286 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1287 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1288 VOIDmode, PATTERN (insn));
1290 /* Discard obvious no-ops, even without -O. This optimization
1291 is fast and doesn't interfere with debugging. */
1292 if (NONJUMP_INSN_P (insn)
1293 && GET_CODE (PATTERN (insn)) == SET
1294 && REG_P (SET_SRC (PATTERN (insn)))
1295 && REG_P (SET_DEST (PATTERN (insn)))
1296 && (REGNO (SET_SRC (PATTERN (insn)))
1297 == REGNO (SET_DEST (PATTERN (insn)))))
1299 delete_insn (insn);
1300 continue;
1303 pnote = &REG_NOTES (insn);
1304 while (*pnote != 0)
1306 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1307 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1308 || REG_NOTE_KIND (*pnote) == REG_INC
1309 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1310 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1311 *pnote = XEXP (*pnote, 1);
1312 else
1313 pnote = &XEXP (*pnote, 1);
1316 #ifdef AUTO_INC_DEC
1317 add_auto_inc_notes (insn, PATTERN (insn));
1318 #endif
1320 /* Simplify (subreg (reg)) if it appears as an operand. */
1321 cleanup_subreg_operands (insn);
1323 /* Clean up invalid ASMs so that they don't confuse later passes.
1324 See PR 21299. */
1325 if (asm_noperands (PATTERN (insn)) >= 0)
1327 extract_insn (insn);
1328 if (!constrain_operands (1))
1330 error_for_asm (insn,
1331 "%<asm%> operand has impossible constraints");
1332 delete_insn (insn);
1333 continue;
1338 /* If we are doing stack checking, give a warning if this function's
1339 frame size is larger than we expect. */
1340 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1342 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1343 static int verbose_warned = 0;
1345 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1346 if (df_regs_ever_live_p (i) && ! fixed_regs[i] && call_used_regs[i])
1347 size += UNITS_PER_WORD;
1349 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1351 warning (0, "frame size too large for reliable stack checking");
1352 if (! verbose_warned)
1354 warning (0, "try reducing the number of local variables");
1355 verbose_warned = 1;
1360 /* Indicate that we no longer have known memory locations or constants. */
1361 if (reg_equiv_constant)
1362 free (reg_equiv_constant);
1363 if (reg_equiv_invariant)
1364 free (reg_equiv_invariant);
1365 reg_equiv_constant = 0;
1366 reg_equiv_invariant = 0;
1367 VEC_free (rtx, gc, reg_equiv_memory_loc_vec);
1368 reg_equiv_memory_loc = 0;
1370 free (temp_pseudo_reg_arr);
1372 if (offsets_known_at)
1373 free (offsets_known_at);
1374 if (offsets_at)
1375 free (offsets_at);
1377 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1378 if (reg_equiv_alt_mem_list[i])
1379 free_EXPR_LIST_list (&reg_equiv_alt_mem_list[i]);
1380 free (reg_equiv_alt_mem_list);
1382 free (reg_equiv_mem);
1383 reg_equiv_init = 0;
1384 free (reg_equiv_address);
1385 free (reg_max_ref_width);
1386 free (reg_old_renumber);
1387 free (pseudo_previous_regs);
1388 free (pseudo_forbidden_regs);
1390 CLEAR_HARD_REG_SET (used_spill_regs);
1391 for (i = 0; i < n_spills; i++)
1392 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1394 /* Free all the insn_chain structures at once. */
1395 obstack_free (&reload_obstack, reload_startobj);
1396 unused_insn_chains = 0;
1397 fixup_abnormal_edges ();
1399 /* Replacing pseudos with their memory equivalents might have
1400 created shared rtx. Subsequent passes would get confused
1401 by this, so unshare everything here. */
1402 unshare_all_rtl_again (first);
1404 #ifdef STACK_BOUNDARY
1405 /* init_emit has set the alignment of the hard frame pointer
1406 to STACK_BOUNDARY. It is very likely no longer valid if
1407 the hard frame pointer was used for register allocation. */
1408 if (!frame_pointer_needed)
1409 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1410 #endif
1412 return failure;
1415 /* Yet another special case. Unfortunately, reg-stack forces people to
1416 write incorrect clobbers in asm statements. These clobbers must not
1417 cause the register to appear in bad_spill_regs, otherwise we'll call
1418 fatal_insn later. We clear the corresponding regnos in the live
1419 register sets to avoid this.
1420 The whole thing is rather sick, I'm afraid. */
1422 static void
1423 maybe_fix_stack_asms (void)
1425 #ifdef STACK_REGS
1426 const char *constraints[MAX_RECOG_OPERANDS];
1427 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1428 struct insn_chain *chain;
1430 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1432 int i, noperands;
1433 HARD_REG_SET clobbered, allowed;
1434 rtx pat;
1436 if (! INSN_P (chain->insn)
1437 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1438 continue;
1439 pat = PATTERN (chain->insn);
1440 if (GET_CODE (pat) != PARALLEL)
1441 continue;
1443 CLEAR_HARD_REG_SET (clobbered);
1444 CLEAR_HARD_REG_SET (allowed);
1446 /* First, make a mask of all stack regs that are clobbered. */
1447 for (i = 0; i < XVECLEN (pat, 0); i++)
1449 rtx t = XVECEXP (pat, 0, i);
1450 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1451 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1454 /* Get the operand values and constraints out of the insn. */
1455 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1456 constraints, operand_mode, NULL);
1458 /* For every operand, see what registers are allowed. */
1459 for (i = 0; i < noperands; i++)
1461 const char *p = constraints[i];
1462 /* For every alternative, we compute the class of registers allowed
1463 for reloading in CLS, and merge its contents into the reg set
1464 ALLOWED. */
1465 int cls = (int) NO_REGS;
1467 for (;;)
1469 char c = *p;
1471 if (c == '\0' || c == ',' || c == '#')
1473 /* End of one alternative - mark the regs in the current
1474 class, and reset the class. */
1475 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1476 cls = NO_REGS;
1477 p++;
1478 if (c == '#')
1479 do {
1480 c = *p++;
1481 } while (c != '\0' && c != ',');
1482 if (c == '\0')
1483 break;
1484 continue;
1487 switch (c)
1489 case '=': case '+': case '*': case '%': case '?': case '!':
1490 case '0': case '1': case '2': case '3': case '4': case 'm':
1491 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1492 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1493 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1494 case 'P':
1495 break;
1497 case 'p':
1498 cls = (int) reg_class_subunion[cls]
1499 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1500 break;
1502 case 'g':
1503 case 'r':
1504 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1505 break;
1507 default:
1508 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1509 cls = (int) reg_class_subunion[cls]
1510 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1511 else
1512 cls = (int) reg_class_subunion[cls]
1513 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1515 p += CONSTRAINT_LEN (c, p);
1518 /* Those of the registers which are clobbered, but allowed by the
1519 constraints, must be usable as reload registers. So clear them
1520 out of the life information. */
1521 AND_HARD_REG_SET (allowed, clobbered);
1522 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1523 if (TEST_HARD_REG_BIT (allowed, i))
1525 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1526 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1530 #endif
1533 /* Copy the global variables n_reloads and rld into the corresponding elts
1534 of CHAIN. */
1535 static void
1536 copy_reloads (struct insn_chain *chain)
1538 chain->n_reloads = n_reloads;
1539 chain->rld = obstack_alloc (&reload_obstack,
1540 n_reloads * sizeof (struct reload));
1541 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1542 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1545 /* Walk the chain of insns, and determine for each whether it needs reloads
1546 and/or eliminations. Build the corresponding insns_need_reload list, and
1547 set something_needs_elimination as appropriate. */
1548 static void
1549 calculate_needs_all_insns (int global)
1551 struct insn_chain **pprev_reload = &insns_need_reload;
1552 struct insn_chain *chain, *next = 0;
1554 something_needs_elimination = 0;
1556 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1557 for (chain = reload_insn_chain; chain != 0; chain = next)
1559 rtx insn = chain->insn;
1561 next = chain->next;
1563 /* Clear out the shortcuts. */
1564 chain->n_reloads = 0;
1565 chain->need_elim = 0;
1566 chain->need_reload = 0;
1567 chain->need_operand_change = 0;
1569 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1570 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1571 what effects this has on the known offsets at labels. */
1573 if (LABEL_P (insn) || JUMP_P (insn)
1574 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1575 set_label_offsets (insn, insn, 0);
1577 if (INSN_P (insn))
1579 rtx old_body = PATTERN (insn);
1580 int old_code = INSN_CODE (insn);
1581 rtx old_notes = REG_NOTES (insn);
1582 int did_elimination = 0;
1583 int operands_changed = 0;
1584 rtx set = single_set (insn);
1586 /* Skip insns that only set an equivalence. */
1587 if (set && REG_P (SET_DEST (set))
1588 && reg_renumber[REGNO (SET_DEST (set))] < 0
1589 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1590 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1591 && reg_equiv_init[REGNO (SET_DEST (set))])
1592 continue;
1594 /* If needed, eliminate any eliminable registers. */
1595 if (num_eliminable || num_eliminable_invariants)
1596 did_elimination = eliminate_regs_in_insn (insn, 0);
1598 /* Analyze the instruction. */
1599 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1600 global, spill_reg_order);
1602 /* If a no-op set needs more than one reload, this is likely
1603 to be something that needs input address reloads. We
1604 can't get rid of this cleanly later, and it is of no use
1605 anyway, so discard it now.
1606 We only do this when expensive_optimizations is enabled,
1607 since this complements reload inheritance / output
1608 reload deletion, and it can make debugging harder. */
1609 if (flag_expensive_optimizations && n_reloads > 1)
1611 rtx set = single_set (insn);
1612 if (set
1614 ((SET_SRC (set) == SET_DEST (set)
1615 && REG_P (SET_SRC (set))
1616 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1617 || (REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
1618 && reg_renumber[REGNO (SET_SRC (set))] < 0
1619 && reg_renumber[REGNO (SET_DEST (set))] < 0
1620 && reg_equiv_memory_loc[REGNO (SET_SRC (set))] != NULL
1621 && reg_equiv_memory_loc[REGNO (SET_DEST (set))] != NULL
1622 && rtx_equal_p (reg_equiv_memory_loc
1623 [REGNO (SET_SRC (set))],
1624 reg_equiv_memory_loc
1625 [REGNO (SET_DEST (set))]))))
1627 if (flag_ira)
1628 /* Inform IRA about the insn deletion. */
1629 mark_memory_move_deletion (REGNO (SET_DEST (set)),
1630 REGNO (SET_SRC (set)));
1631 delete_insn (insn);
1632 /* Delete it from the reload chain. */
1633 if (chain->prev)
1634 chain->prev->next = next;
1635 else
1636 reload_insn_chain = next;
1637 if (next)
1638 next->prev = chain->prev;
1639 chain->next = unused_insn_chains;
1640 unused_insn_chains = chain;
1641 continue;
1644 if (num_eliminable)
1645 update_eliminable_offsets ();
1647 /* Remember for later shortcuts which insns had any reloads or
1648 register eliminations. */
1649 chain->need_elim = did_elimination;
1650 chain->need_reload = n_reloads > 0;
1651 chain->need_operand_change = operands_changed;
1653 /* Discard any register replacements done. */
1654 if (did_elimination)
1656 obstack_free (&reload_obstack, reload_insn_firstobj);
1657 PATTERN (insn) = old_body;
1658 INSN_CODE (insn) = old_code;
1659 REG_NOTES (insn) = old_notes;
1660 something_needs_elimination = 1;
1663 something_needs_operands_changed |= operands_changed;
1665 if (n_reloads != 0)
1667 copy_reloads (chain);
1668 *pprev_reload = chain;
1669 pprev_reload = &chain->next_need_reload;
1673 *pprev_reload = 0;
1676 /* Comparison function for qsort to decide which of two reloads
1677 should be handled first. *P1 and *P2 are the reload numbers. */
1679 static int
1680 reload_reg_class_lower (const void *r1p, const void *r2p)
1682 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1683 int t;
1685 /* Consider required reloads before optional ones. */
1686 t = rld[r1].optional - rld[r2].optional;
1687 if (t != 0)
1688 return t;
1690 /* Count all solitary classes before non-solitary ones. */
1691 t = ((reg_class_size[(int) rld[r2].class] == 1)
1692 - (reg_class_size[(int) rld[r1].class] == 1));
1693 if (t != 0)
1694 return t;
1696 /* Aside from solitaires, consider all multi-reg groups first. */
1697 t = rld[r2].nregs - rld[r1].nregs;
1698 if (t != 0)
1699 return t;
1701 /* Consider reloads in order of increasing reg-class number. */
1702 t = (int) rld[r1].class - (int) rld[r2].class;
1703 if (t != 0)
1704 return t;
1706 /* If reloads are equally urgent, sort by reload number,
1707 so that the results of qsort leave nothing to chance. */
1708 return r1 - r2;
1711 /* The cost of spilling each hard reg. */
1712 static int spill_cost[FIRST_PSEUDO_REGISTER];
1714 /* When spilling multiple hard registers, we use SPILL_COST for the first
1715 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1716 only the first hard reg for a multi-reg pseudo. */
1717 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1719 /* Map of hard regno to pseudo regno currently occupying the hard
1720 reg. */
1721 static int hard_regno_to_pseudo_regno[FIRST_PSEUDO_REGISTER];
1723 /* Update the spill cost arrays, considering that pseudo REG is live. */
1725 static void
1726 count_pseudo (int reg)
1728 int freq = REG_FREQ (reg);
1729 int r = reg_renumber[reg];
1730 int nregs;
1732 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1733 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1734 /* Ignore spilled pseudo-registers which can be here only if IRA
1735 is used. */
1736 || (flag_ira && r < 0))
1737 return;
1739 SET_REGNO_REG_SET (&pseudos_counted, reg);
1741 gcc_assert (r >= 0);
1743 spill_add_cost[r] += freq;
1744 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1745 while (nregs-- > 0)
1747 hard_regno_to_pseudo_regno[r + nregs] = reg;
1748 spill_cost[r + nregs] += freq;
1752 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1753 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1755 static void
1756 order_regs_for_reload (struct insn_chain *chain)
1758 unsigned i;
1759 HARD_REG_SET used_by_pseudos;
1760 HARD_REG_SET used_by_pseudos2;
1761 reg_set_iterator rsi;
1763 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1765 memset (spill_cost, 0, sizeof spill_cost);
1766 memset (spill_add_cost, 0, sizeof spill_add_cost);
1767 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1768 hard_regno_to_pseudo_regno[i] = -1;
1770 /* Count number of uses of each hard reg by pseudo regs allocated to it
1771 and then order them by decreasing use. First exclude hard registers
1772 that are live in or across this insn. */
1774 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1775 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1776 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1777 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1779 /* Now find out which pseudos are allocated to it, and update
1780 hard_reg_n_uses. */
1781 CLEAR_REG_SET (&pseudos_counted);
1783 EXECUTE_IF_SET_IN_REG_SET
1784 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1786 count_pseudo (i);
1788 EXECUTE_IF_SET_IN_REG_SET
1789 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1791 count_pseudo (i);
1793 CLEAR_REG_SET (&pseudos_counted);
1796 /* Vector of reload-numbers showing the order in which the reloads should
1797 be processed. */
1798 static short reload_order[MAX_RELOADS];
1800 /* This is used to keep track of the spill regs used in one insn. */
1801 static HARD_REG_SET used_spill_regs_local;
1803 /* We decided to spill hard register SPILLED, which has a size of
1804 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1805 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1806 update SPILL_COST/SPILL_ADD_COST. */
1808 static void
1809 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1811 int freq = REG_FREQ (reg);
1812 int r = reg_renumber[reg];
1813 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1815 /* Ignore spilled pseudo-registers which can be here only if IRA is
1816 used. */
1817 if ((flag_ira && r < 0)
1818 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1819 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1820 return;
1822 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1824 spill_add_cost[r] -= freq;
1825 while (nregs-- > 0)
1827 hard_regno_to_pseudo_regno[r + nregs] = -1;
1828 spill_cost[r + nregs] -= freq;
1832 /* Find reload register to use for reload number ORDER. */
1834 static int
1835 find_reg (struct insn_chain *chain, int order)
1837 int rnum = reload_order[order];
1838 struct reload *rl = rld + rnum;
1839 int best_cost = INT_MAX;
1840 int best_reg = -1;
1841 unsigned int i, j, n;
1842 int k;
1843 HARD_REG_SET not_usable;
1844 HARD_REG_SET used_by_other_reload;
1845 reg_set_iterator rsi;
1846 static int regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1847 static int best_regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1849 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1850 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1851 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1853 CLEAR_HARD_REG_SET (used_by_other_reload);
1854 for (k = 0; k < order; k++)
1856 int other = reload_order[k];
1858 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1859 for (j = 0; j < rld[other].nregs; j++)
1860 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1863 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1865 unsigned int regno = i;
1867 if (! TEST_HARD_REG_BIT (not_usable, regno)
1868 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1869 && HARD_REGNO_MODE_OK (regno, rl->mode))
1871 int this_cost = spill_cost[regno];
1872 int ok = 1;
1873 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1875 for (j = 1; j < this_nregs; j++)
1877 this_cost += spill_add_cost[regno + j];
1878 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1879 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1880 ok = 0;
1882 if (! ok)
1883 continue;
1885 if (flag_ira)
1887 /* Ask IRA to find a better pseudo-register for
1888 spilling. */
1889 for (n = j = 0; j < this_nregs; j++)
1891 int r = hard_regno_to_pseudo_regno[regno + j];
1893 if (r < 0)
1894 continue;
1895 if (n == 0 || regno_pseudo_regs[n - 1] != r)
1896 regno_pseudo_regs[n++] = r;
1898 regno_pseudo_regs[n++] = -1;
1899 if (best_reg < 0
1900 || better_spill_reload_regno_p (regno_pseudo_regs,
1901 best_regno_pseudo_regs,
1902 rl->in, rl->out,
1903 chain->insn))
1905 best_reg = regno;
1906 for (j = 0;; j++)
1908 best_regno_pseudo_regs[j] = regno_pseudo_regs[j];
1909 if (regno_pseudo_regs[j] < 0)
1910 break;
1913 continue;
1916 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1917 this_cost--;
1918 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1919 this_cost--;
1920 if (this_cost < best_cost
1921 /* Among registers with equal cost, prefer caller-saved ones, or
1922 use REG_ALLOC_ORDER if it is defined. */
1923 || (this_cost == best_cost
1924 #ifdef REG_ALLOC_ORDER
1925 && (inv_reg_alloc_order[regno]
1926 < inv_reg_alloc_order[best_reg])
1927 #else
1928 && call_used_regs[regno]
1929 && ! call_used_regs[best_reg]
1930 #endif
1933 best_reg = regno;
1934 best_cost = this_cost;
1938 if (best_reg == -1)
1939 return 0;
1941 if (dump_file)
1942 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1944 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1945 rl->regno = best_reg;
1947 EXECUTE_IF_SET_IN_REG_SET
1948 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1950 count_spilled_pseudo (best_reg, rl->nregs, j);
1953 EXECUTE_IF_SET_IN_REG_SET
1954 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1956 count_spilled_pseudo (best_reg, rl->nregs, j);
1959 for (i = 0; i < rl->nregs; i++)
1961 gcc_assert (spill_cost[best_reg + i] == 0);
1962 gcc_assert (spill_add_cost[best_reg + i] == 0);
1963 gcc_assert (hard_regno_to_pseudo_regno[best_reg + i] == -1);
1964 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1966 return 1;
1969 /* Find more reload regs to satisfy the remaining need of an insn, which
1970 is given by CHAIN.
1971 Do it by ascending class number, since otherwise a reg
1972 might be spilled for a big class and might fail to count
1973 for a smaller class even though it belongs to that class. */
1975 static void
1976 find_reload_regs (struct insn_chain *chain)
1978 int i;
1980 /* In order to be certain of getting the registers we need,
1981 we must sort the reloads into order of increasing register class.
1982 Then our grabbing of reload registers will parallel the process
1983 that provided the reload registers. */
1984 for (i = 0; i < chain->n_reloads; i++)
1986 /* Show whether this reload already has a hard reg. */
1987 if (chain->rld[i].reg_rtx)
1989 int regno = REGNO (chain->rld[i].reg_rtx);
1990 chain->rld[i].regno = regno;
1991 chain->rld[i].nregs
1992 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1994 else
1995 chain->rld[i].regno = -1;
1996 reload_order[i] = i;
1999 n_reloads = chain->n_reloads;
2000 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
2002 CLEAR_HARD_REG_SET (used_spill_regs_local);
2004 if (dump_file)
2005 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
2007 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
2009 /* Compute the order of preference for hard registers to spill. */
2011 order_regs_for_reload (chain);
2013 for (i = 0; i < n_reloads; i++)
2015 int r = reload_order[i];
2017 /* Ignore reloads that got marked inoperative. */
2018 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
2019 && ! rld[r].optional
2020 && rld[r].regno == -1)
2021 if (! find_reg (chain, i))
2023 if (dump_file)
2024 fprintf (dump_file, "reload failure for reload %d\n", r);
2025 spill_failure (chain->insn, rld[r].class);
2026 failure = 1;
2027 return;
2031 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
2032 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
2034 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
2037 static void
2038 select_reload_regs (void)
2040 struct insn_chain *chain;
2042 /* Try to satisfy the needs for each insn. */
2043 for (chain = insns_need_reload; chain != 0;
2044 chain = chain->next_need_reload)
2045 find_reload_regs (chain);
2048 /* Delete all insns that were inserted by emit_caller_save_insns during
2049 this iteration. */
2050 static void
2051 delete_caller_save_insns (void)
2053 struct insn_chain *c = reload_insn_chain;
2055 while (c != 0)
2057 while (c != 0 && c->is_caller_save_insn)
2059 struct insn_chain *next = c->next;
2060 rtx insn = c->insn;
2062 if (c == reload_insn_chain)
2063 reload_insn_chain = next;
2064 delete_insn (insn);
2066 if (next)
2067 next->prev = c->prev;
2068 if (c->prev)
2069 c->prev->next = next;
2070 c->next = unused_insn_chains;
2071 unused_insn_chains = c;
2072 c = next;
2074 if (c != 0)
2075 c = c->next;
2079 /* Handle the failure to find a register to spill.
2080 INSN should be one of the insns which needed this particular spill reg. */
2082 static void
2083 spill_failure (rtx insn, enum reg_class class)
2085 if (asm_noperands (PATTERN (insn)) >= 0)
2086 error_for_asm (insn, "can't find a register in class %qs while "
2087 "reloading %<asm%>",
2088 reg_class_names[class]);
2089 else
2091 error ("unable to find a register to spill in class %qs",
2092 reg_class_names[class]);
2094 if (dump_file)
2096 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2097 debug_reload_to_stream (dump_file);
2099 fatal_insn ("this is the insn:", insn);
2103 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2104 data that is dead in INSN. */
2106 static void
2107 delete_dead_insn (rtx insn)
2109 rtx prev = prev_real_insn (insn);
2110 rtx prev_dest;
2112 /* If the previous insn sets a register that dies in our insn, delete it
2113 too. */
2114 if (prev && GET_CODE (PATTERN (prev)) == SET
2115 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2116 && reg_mentioned_p (prev_dest, PATTERN (insn))
2117 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2118 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2119 delete_dead_insn (prev);
2121 SET_INSN_DELETED (insn);
2124 /* Modify the home of pseudo-reg I.
2125 The new home is present in reg_renumber[I].
2127 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2128 or it may be -1, meaning there is none or it is not relevant.
2129 This is used so that all pseudos spilled from a given hard reg
2130 can share one stack slot. */
2132 static void
2133 alter_reg (int i, int from_reg, bool dont_share_p)
2135 /* When outputting an inline function, this can happen
2136 for a reg that isn't actually used. */
2137 if (regno_reg_rtx[i] == 0)
2138 return;
2140 /* If the reg got changed to a MEM at rtl-generation time,
2141 ignore it. */
2142 if (!REG_P (regno_reg_rtx[i]))
2143 return;
2145 /* Modify the reg-rtx to contain the new hard reg
2146 number or else to contain its pseudo reg number. */
2147 SET_REGNO (regno_reg_rtx[i],
2148 reg_renumber[i] >= 0 ? reg_renumber[i] : i);
2150 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2151 allocate a stack slot for it. */
2153 if (reg_renumber[i] < 0
2154 && REG_N_REFS (i) > 0
2155 && reg_equiv_constant[i] == 0
2156 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
2157 && reg_equiv_memory_loc[i] == 0)
2159 rtx x;
2160 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2161 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2162 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2163 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2164 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2165 int adjust = 0;
2166 bool shared_p = false;
2168 if (flag_ira)
2169 /* Mark the spill for IRA. */
2170 SET_REGNO_REG_SET (&spilled_pseudos, i);
2171 x = (dont_share_p || ! flag_ira
2172 ? NULL_RTX : reuse_stack_slot (i, inherent_size, total_size));
2173 if (x)
2174 shared_p = true;
2175 /* Each pseudo reg has an inherent size which comes from its own mode,
2176 and a total size which provides room for paradoxical subregs
2177 which refer to the pseudo reg in wider modes.
2179 We can use a slot already allocated if it provides both
2180 enough inherent space and enough total space.
2181 Otherwise, we allocate a new slot, making sure that it has no less
2182 inherent space, and no less total space, then the previous slot. */
2183 else if (from_reg == -1 || (! dont_share_p && flag_ira))
2185 alias_set_type alias_set = new_alias_set ();
2187 /* No known place to spill from => no slot to reuse. */
2188 x = assign_stack_local (mode, total_size,
2189 min_align > inherent_align
2190 || total_size > inherent_size ? -1 : 0);
2191 if (BYTES_BIG_ENDIAN)
2192 /* Cancel the big-endian correction done in assign_stack_local.
2193 Get the address of the beginning of the slot.
2194 This is so we can do a big-endian correction unconditionally
2195 below. */
2196 adjust = inherent_size - total_size;
2198 /* Nothing can alias this slot except this pseudo. */
2199 set_mem_alias_set (x, alias_set);
2200 dse_record_singleton_alias_set (alias_set, mode);
2202 if (! dont_share_p && flag_ira)
2203 /* Inform IRA about allocation a new stack slot. */
2204 mark_new_stack_slot (x, i, total_size);
2207 /* Reuse a stack slot if possible. */
2208 else if (spill_stack_slot[from_reg] != 0
2209 && spill_stack_slot_width[from_reg] >= total_size
2210 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2211 >= inherent_size)
2212 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2213 x = spill_stack_slot[from_reg];
2214 /* Allocate a bigger slot. */
2215 else
2217 /* Compute maximum size needed, both for inherent size
2218 and for total size. */
2219 rtx stack_slot;
2221 if (spill_stack_slot[from_reg])
2223 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2224 > inherent_size)
2225 mode = GET_MODE (spill_stack_slot[from_reg]);
2226 if (spill_stack_slot_width[from_reg] > total_size)
2227 total_size = spill_stack_slot_width[from_reg];
2228 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2229 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2232 /* Make a slot with that size. */
2233 x = assign_stack_local (mode, total_size,
2234 min_align > inherent_align
2235 || total_size > inherent_size ? -1 : 0);
2236 stack_slot = x;
2238 /* All pseudos mapped to this slot can alias each other. */
2239 if (spill_stack_slot[from_reg])
2241 alias_set_type alias_set
2242 = MEM_ALIAS_SET (spill_stack_slot[from_reg]);
2243 set_mem_alias_set (x, alias_set);
2244 dse_invalidate_singleton_alias_set (alias_set);
2246 else
2248 alias_set_type alias_set = new_alias_set ();
2249 set_mem_alias_set (x, alias_set);
2250 dse_record_singleton_alias_set (alias_set, mode);
2253 if (BYTES_BIG_ENDIAN)
2255 /* Cancel the big-endian correction done in assign_stack_local.
2256 Get the address of the beginning of the slot.
2257 This is so we can do a big-endian correction unconditionally
2258 below. */
2259 adjust = GET_MODE_SIZE (mode) - total_size;
2260 if (adjust)
2261 stack_slot
2262 = adjust_address_nv (x, mode_for_size (total_size
2263 * BITS_PER_UNIT,
2264 MODE_INT, 1),
2265 adjust);
2268 spill_stack_slot[from_reg] = stack_slot;
2269 spill_stack_slot_width[from_reg] = total_size;
2272 /* On a big endian machine, the "address" of the slot
2273 is the address of the low part that fits its inherent mode. */
2274 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2275 adjust += (total_size - inherent_size);
2277 /* If we have any adjustment to make, or if the stack slot is the
2278 wrong mode, make a new stack slot. */
2279 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2281 /* If we have a decl for the original register, set it for the
2282 memory. If this is a shared MEM, make a copy. */
2283 if (shared_p)
2285 x = copy_rtx (x);
2286 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2288 else if (REG_EXPR (regno_reg_rtx[i])
2289 && DECL_P (REG_EXPR (regno_reg_rtx[i])))
2291 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2293 /* We can do this only for the DECLs home pseudo, not for
2294 any copies of it, since otherwise when the stack slot
2295 is reused, nonoverlapping_memrefs_p might think they
2296 cannot overlap. */
2297 if (decl && REG_P (decl) && REGNO (decl) == (unsigned) i)
2299 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2300 x = copy_rtx (x);
2302 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2306 /* Save the stack slot for later. */
2307 reg_equiv_memory_loc[i] = x;
2311 /* Mark the slots in regs_ever_live for the hard regs used by
2312 pseudo-reg number REGNO, accessed in MODE. */
2314 static void
2315 mark_home_live_1 (int regno, enum machine_mode mode)
2317 int i, lim;
2319 i = reg_renumber[regno];
2320 if (i < 0)
2321 return;
2322 lim = end_hard_regno (mode, i);
2323 while (i < lim)
2324 df_set_regs_ever_live(i++, true);
2327 /* Mark the slots in regs_ever_live for the hard regs
2328 used by pseudo-reg number REGNO. */
2330 void
2331 mark_home_live (int regno)
2333 if (reg_renumber[regno] >= 0)
2334 mark_home_live_1 (regno, PSEUDO_REGNO_MODE (regno));
2337 /* This function handles the tracking of elimination offsets around branches.
2339 X is a piece of RTL being scanned.
2341 INSN is the insn that it came from, if any.
2343 INITIAL_P is nonzero if we are to set the offset to be the initial
2344 offset and zero if we are setting the offset of the label to be the
2345 current offset. */
2347 static void
2348 set_label_offsets (rtx x, rtx insn, int initial_p)
2350 enum rtx_code code = GET_CODE (x);
2351 rtx tem;
2352 unsigned int i;
2353 struct elim_table *p;
2355 switch (code)
2357 case LABEL_REF:
2358 if (LABEL_REF_NONLOCAL_P (x))
2359 return;
2361 x = XEXP (x, 0);
2363 /* ... fall through ... */
2365 case CODE_LABEL:
2366 /* If we know nothing about this label, set the desired offsets. Note
2367 that this sets the offset at a label to be the offset before a label
2368 if we don't know anything about the label. This is not correct for
2369 the label after a BARRIER, but is the best guess we can make. If
2370 we guessed wrong, we will suppress an elimination that might have
2371 been possible had we been able to guess correctly. */
2373 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2375 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2376 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2377 = (initial_p ? reg_eliminate[i].initial_offset
2378 : reg_eliminate[i].offset);
2379 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2382 /* Otherwise, if this is the definition of a label and it is
2383 preceded by a BARRIER, set our offsets to the known offset of
2384 that label. */
2386 else if (x == insn
2387 && (tem = prev_nonnote_insn (insn)) != 0
2388 && BARRIER_P (tem))
2389 set_offsets_for_label (insn);
2390 else
2391 /* If neither of the above cases is true, compare each offset
2392 with those previously recorded and suppress any eliminations
2393 where the offsets disagree. */
2395 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2396 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2397 != (initial_p ? reg_eliminate[i].initial_offset
2398 : reg_eliminate[i].offset))
2399 reg_eliminate[i].can_eliminate = 0;
2401 return;
2403 case JUMP_INSN:
2404 set_label_offsets (PATTERN (insn), insn, initial_p);
2406 /* ... fall through ... */
2408 case INSN:
2409 case CALL_INSN:
2410 /* Any labels mentioned in REG_LABEL_OPERAND notes can be branched
2411 to indirectly and hence must have all eliminations at their
2412 initial offsets. */
2413 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2414 if (REG_NOTE_KIND (tem) == REG_LABEL_OPERAND)
2415 set_label_offsets (XEXP (tem, 0), insn, 1);
2416 return;
2418 case PARALLEL:
2419 case ADDR_VEC:
2420 case ADDR_DIFF_VEC:
2421 /* Each of the labels in the parallel or address vector must be
2422 at their initial offsets. We want the first field for PARALLEL
2423 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2425 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2426 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2427 insn, initial_p);
2428 return;
2430 case SET:
2431 /* We only care about setting PC. If the source is not RETURN,
2432 IF_THEN_ELSE, or a label, disable any eliminations not at
2433 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2434 isn't one of those possibilities. For branches to a label,
2435 call ourselves recursively.
2437 Note that this can disable elimination unnecessarily when we have
2438 a non-local goto since it will look like a non-constant jump to
2439 someplace in the current function. This isn't a significant
2440 problem since such jumps will normally be when all elimination
2441 pairs are back to their initial offsets. */
2443 if (SET_DEST (x) != pc_rtx)
2444 return;
2446 switch (GET_CODE (SET_SRC (x)))
2448 case PC:
2449 case RETURN:
2450 return;
2452 case LABEL_REF:
2453 set_label_offsets (SET_SRC (x), insn, initial_p);
2454 return;
2456 case IF_THEN_ELSE:
2457 tem = XEXP (SET_SRC (x), 1);
2458 if (GET_CODE (tem) == LABEL_REF)
2459 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2460 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2461 break;
2463 tem = XEXP (SET_SRC (x), 2);
2464 if (GET_CODE (tem) == LABEL_REF)
2465 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2466 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2467 break;
2468 return;
2470 default:
2471 break;
2474 /* If we reach here, all eliminations must be at their initial
2475 offset because we are doing a jump to a variable address. */
2476 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2477 if (p->offset != p->initial_offset)
2478 p->can_eliminate = 0;
2479 break;
2481 default:
2482 break;
2486 /* Scan X and replace any eliminable registers (such as fp) with a
2487 replacement (such as sp), plus an offset.
2489 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2490 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2491 MEM, we are allowed to replace a sum of a register and the constant zero
2492 with the register, which we cannot do outside a MEM. In addition, we need
2493 to record the fact that a register is referenced outside a MEM.
2495 If INSN is an insn, it is the insn containing X. If we replace a REG
2496 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2497 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2498 the REG is being modified.
2500 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2501 That's used when we eliminate in expressions stored in notes.
2502 This means, do not set ref_outside_mem even if the reference
2503 is outside of MEMs.
2505 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2506 replacements done assuming all offsets are at their initial values. If
2507 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2508 encounter, return the actual location so that find_reloads will do
2509 the proper thing. */
2511 static rtx
2512 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2513 bool may_use_invariant)
2515 enum rtx_code code = GET_CODE (x);
2516 struct elim_table *ep;
2517 int regno;
2518 rtx new;
2519 int i, j;
2520 const char *fmt;
2521 int copied = 0;
2523 if (! current_function_decl)
2524 return x;
2526 switch (code)
2528 case CONST_INT:
2529 case CONST_DOUBLE:
2530 case CONST_FIXED:
2531 case CONST_VECTOR:
2532 case CONST:
2533 case SYMBOL_REF:
2534 case CODE_LABEL:
2535 case PC:
2536 case CC0:
2537 case ASM_INPUT:
2538 case ADDR_VEC:
2539 case ADDR_DIFF_VEC:
2540 case RETURN:
2541 return x;
2543 case REG:
2544 regno = REGNO (x);
2546 /* First handle the case where we encounter a bare register that
2547 is eliminable. Replace it with a PLUS. */
2548 if (regno < FIRST_PSEUDO_REGISTER)
2550 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2551 ep++)
2552 if (ep->from_rtx == x && ep->can_eliminate)
2553 return plus_constant (ep->to_rtx, ep->previous_offset);
2556 else if (reg_renumber && reg_renumber[regno] < 0
2557 && reg_equiv_invariant && reg_equiv_invariant[regno])
2559 if (may_use_invariant)
2560 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2561 mem_mode, insn, true);
2562 /* There exists at least one use of REGNO that cannot be
2563 eliminated. Prevent the defining insn from being deleted. */
2564 reg_equiv_init[regno] = NULL_RTX;
2565 alter_reg (regno, -1, true);
2567 return x;
2569 /* You might think handling MINUS in a manner similar to PLUS is a
2570 good idea. It is not. It has been tried multiple times and every
2571 time the change has had to have been reverted.
2573 Other parts of reload know a PLUS is special (gen_reload for example)
2574 and require special code to handle code a reloaded PLUS operand.
2576 Also consider backends where the flags register is clobbered by a
2577 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2578 lea instruction comes to mind). If we try to reload a MINUS, we
2579 may kill the flags register that was holding a useful value.
2581 So, please before trying to handle MINUS, consider reload as a
2582 whole instead of this little section as well as the backend issues. */
2583 case PLUS:
2584 /* If this is the sum of an eliminable register and a constant, rework
2585 the sum. */
2586 if (REG_P (XEXP (x, 0))
2587 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2588 && CONSTANT_P (XEXP (x, 1)))
2590 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2591 ep++)
2592 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2594 /* The only time we want to replace a PLUS with a REG (this
2595 occurs when the constant operand of the PLUS is the negative
2596 of the offset) is when we are inside a MEM. We won't want
2597 to do so at other times because that would change the
2598 structure of the insn in a way that reload can't handle.
2599 We special-case the commonest situation in
2600 eliminate_regs_in_insn, so just replace a PLUS with a
2601 PLUS here, unless inside a MEM. */
2602 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2603 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2604 return ep->to_rtx;
2605 else
2606 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2607 plus_constant (XEXP (x, 1),
2608 ep->previous_offset));
2611 /* If the register is not eliminable, we are done since the other
2612 operand is a constant. */
2613 return x;
2616 /* If this is part of an address, we want to bring any constant to the
2617 outermost PLUS. We will do this by doing register replacement in
2618 our operands and seeing if a constant shows up in one of them.
2620 Note that there is no risk of modifying the structure of the insn,
2621 since we only get called for its operands, thus we are either
2622 modifying the address inside a MEM, or something like an address
2623 operand of a load-address insn. */
2626 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2627 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2629 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2631 /* If one side is a PLUS and the other side is a pseudo that
2632 didn't get a hard register but has a reg_equiv_constant,
2633 we must replace the constant here since it may no longer
2634 be in the position of any operand. */
2635 if (GET_CODE (new0) == PLUS && REG_P (new1)
2636 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2637 && reg_renumber[REGNO (new1)] < 0
2638 && reg_equiv_constant != 0
2639 && reg_equiv_constant[REGNO (new1)] != 0)
2640 new1 = reg_equiv_constant[REGNO (new1)];
2641 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2642 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2643 && reg_renumber[REGNO (new0)] < 0
2644 && reg_equiv_constant[REGNO (new0)] != 0)
2645 new0 = reg_equiv_constant[REGNO (new0)];
2647 new = form_sum (new0, new1);
2649 /* As above, if we are not inside a MEM we do not want to
2650 turn a PLUS into something else. We might try to do so here
2651 for an addition of 0 if we aren't optimizing. */
2652 if (! mem_mode && GET_CODE (new) != PLUS)
2653 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2654 else
2655 return new;
2658 return x;
2660 case MULT:
2661 /* If this is the product of an eliminable register and a
2662 constant, apply the distribute law and move the constant out
2663 so that we have (plus (mult ..) ..). This is needed in order
2664 to keep load-address insns valid. This case is pathological.
2665 We ignore the possibility of overflow here. */
2666 if (REG_P (XEXP (x, 0))
2667 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2668 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2669 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2670 ep++)
2671 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2673 if (! mem_mode
2674 /* Refs inside notes don't count for this purpose. */
2675 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2676 || GET_CODE (insn) == INSN_LIST)))
2677 ep->ref_outside_mem = 1;
2679 return
2680 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2681 ep->previous_offset * INTVAL (XEXP (x, 1)));
2684 /* ... fall through ... */
2686 case CALL:
2687 case COMPARE:
2688 /* See comments before PLUS about handling MINUS. */
2689 case MINUS:
2690 case DIV: case UDIV:
2691 case MOD: case UMOD:
2692 case AND: case IOR: case XOR:
2693 case ROTATERT: case ROTATE:
2694 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2695 case NE: case EQ:
2696 case GE: case GT: case GEU: case GTU:
2697 case LE: case LT: case LEU: case LTU:
2699 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2700 rtx new1 = XEXP (x, 1)
2701 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false) : 0;
2703 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2704 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2706 return x;
2708 case EXPR_LIST:
2709 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2710 if (XEXP (x, 0))
2712 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2713 if (new != XEXP (x, 0))
2715 /* If this is a REG_DEAD note, it is not valid anymore.
2716 Using the eliminated version could result in creating a
2717 REG_DEAD note for the stack or frame pointer. */
2718 if (GET_MODE (x) == REG_DEAD)
2719 return (XEXP (x, 1)
2720 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true)
2721 : NULL_RTX);
2723 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2727 /* ... fall through ... */
2729 case INSN_LIST:
2730 /* Now do eliminations in the rest of the chain. If this was
2731 an EXPR_LIST, this might result in allocating more memory than is
2732 strictly needed, but it simplifies the code. */
2733 if (XEXP (x, 1))
2735 new = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2736 if (new != XEXP (x, 1))
2737 return
2738 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2740 return x;
2742 case PRE_INC:
2743 case POST_INC:
2744 case PRE_DEC:
2745 case POST_DEC:
2746 /* We do not support elimination of a register that is modified.
2747 elimination_effects has already make sure that this does not
2748 happen. */
2749 return x;
2751 case PRE_MODIFY:
2752 case POST_MODIFY:
2753 /* We do not support elimination of a register that is modified.
2754 elimination_effects has already make sure that this does not
2755 happen. The only remaining case we need to consider here is
2756 that the increment value may be an eliminable register. */
2757 if (GET_CODE (XEXP (x, 1)) == PLUS
2758 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2760 rtx new = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2761 insn, true);
2763 if (new != XEXP (XEXP (x, 1), 1))
2764 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2765 gen_rtx_PLUS (GET_MODE (x),
2766 XEXP (x, 0), new));
2768 return x;
2770 case STRICT_LOW_PART:
2771 case NEG: case NOT:
2772 case SIGN_EXTEND: case ZERO_EXTEND:
2773 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2774 case FLOAT: case FIX:
2775 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2776 case ABS:
2777 case SQRT:
2778 case FFS:
2779 case CLZ:
2780 case CTZ:
2781 case POPCOUNT:
2782 case PARITY:
2783 case BSWAP:
2784 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2785 if (new != XEXP (x, 0))
2786 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2787 return x;
2789 case SUBREG:
2790 /* Similar to above processing, but preserve SUBREG_BYTE.
2791 Convert (subreg (mem)) to (mem) if not paradoxical.
2792 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2793 pseudo didn't get a hard reg, we must replace this with the
2794 eliminated version of the memory location because push_reload
2795 may do the replacement in certain circumstances. */
2796 if (REG_P (SUBREG_REG (x))
2797 && (GET_MODE_SIZE (GET_MODE (x))
2798 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2799 && reg_equiv_memory_loc != 0
2800 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2802 new = SUBREG_REG (x);
2804 else
2805 new = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false);
2807 if (new != SUBREG_REG (x))
2809 int x_size = GET_MODE_SIZE (GET_MODE (x));
2810 int new_size = GET_MODE_SIZE (GET_MODE (new));
2812 if (MEM_P (new)
2813 && ((x_size < new_size
2814 #ifdef WORD_REGISTER_OPERATIONS
2815 /* On these machines, combine can create rtl of the form
2816 (set (subreg:m1 (reg:m2 R) 0) ...)
2817 where m1 < m2, and expects something interesting to
2818 happen to the entire word. Moreover, it will use the
2819 (reg:m2 R) later, expecting all bits to be preserved.
2820 So if the number of words is the same, preserve the
2821 subreg so that push_reload can see it. */
2822 && ! ((x_size - 1) / UNITS_PER_WORD
2823 == (new_size -1 ) / UNITS_PER_WORD)
2824 #endif
2826 || x_size == new_size)
2828 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2829 else
2830 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2833 return x;
2835 case MEM:
2836 /* Our only special processing is to pass the mode of the MEM to our
2837 recursive call and copy the flags. While we are here, handle this
2838 case more efficiently. */
2839 return
2840 replace_equiv_address_nv (x,
2841 eliminate_regs_1 (XEXP (x, 0), GET_MODE (x),
2842 insn, true));
2844 case USE:
2845 /* Handle insn_list USE that a call to a pure function may generate. */
2846 new = eliminate_regs_1 (XEXP (x, 0), 0, insn, false);
2847 if (new != XEXP (x, 0))
2848 return gen_rtx_USE (GET_MODE (x), new);
2849 return x;
2851 case CLOBBER:
2852 case ASM_OPERANDS:
2853 case SET:
2854 gcc_unreachable ();
2856 default:
2857 break;
2860 /* Process each of our operands recursively. If any have changed, make a
2861 copy of the rtx. */
2862 fmt = GET_RTX_FORMAT (code);
2863 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2865 if (*fmt == 'e')
2867 new = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false);
2868 if (new != XEXP (x, i) && ! copied)
2870 x = shallow_copy_rtx (x);
2871 copied = 1;
2873 XEXP (x, i) = new;
2875 else if (*fmt == 'E')
2877 int copied_vec = 0;
2878 for (j = 0; j < XVECLEN (x, i); j++)
2880 new = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false);
2881 if (new != XVECEXP (x, i, j) && ! copied_vec)
2883 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2884 XVEC (x, i)->elem);
2885 if (! copied)
2887 x = shallow_copy_rtx (x);
2888 copied = 1;
2890 XVEC (x, i) = new_v;
2891 copied_vec = 1;
2893 XVECEXP (x, i, j) = new;
2898 return x;
2902 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2904 return eliminate_regs_1 (x, mem_mode, insn, false);
2907 /* Scan rtx X for modifications of elimination target registers. Update
2908 the table of eliminables to reflect the changed state. MEM_MODE is
2909 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2911 static void
2912 elimination_effects (rtx x, enum machine_mode mem_mode)
2914 enum rtx_code code = GET_CODE (x);
2915 struct elim_table *ep;
2916 int regno;
2917 int i, j;
2918 const char *fmt;
2920 switch (code)
2922 case CONST_INT:
2923 case CONST_DOUBLE:
2924 case CONST_FIXED:
2925 case CONST_VECTOR:
2926 case CONST:
2927 case SYMBOL_REF:
2928 case CODE_LABEL:
2929 case PC:
2930 case CC0:
2931 case ASM_INPUT:
2932 case ADDR_VEC:
2933 case ADDR_DIFF_VEC:
2934 case RETURN:
2935 return;
2937 case REG:
2938 regno = REGNO (x);
2940 /* First handle the case where we encounter a bare register that
2941 is eliminable. Replace it with a PLUS. */
2942 if (regno < FIRST_PSEUDO_REGISTER)
2944 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2945 ep++)
2946 if (ep->from_rtx == x && ep->can_eliminate)
2948 if (! mem_mode)
2949 ep->ref_outside_mem = 1;
2950 return;
2954 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2955 && reg_equiv_constant[regno]
2956 && ! function_invariant_p (reg_equiv_constant[regno]))
2957 elimination_effects (reg_equiv_constant[regno], mem_mode);
2958 return;
2960 case PRE_INC:
2961 case POST_INC:
2962 case PRE_DEC:
2963 case POST_DEC:
2964 case POST_MODIFY:
2965 case PRE_MODIFY:
2966 /* If we modify the source of an elimination rule, disable it. */
2967 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2968 if (ep->from_rtx == XEXP (x, 0))
2969 ep->can_eliminate = 0;
2971 /* If we modify the target of an elimination rule by adding a constant,
2972 update its offset. If we modify the target in any other way, we'll
2973 have to disable the rule as well. */
2974 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2975 if (ep->to_rtx == XEXP (x, 0))
2977 int size = GET_MODE_SIZE (mem_mode);
2979 /* If more bytes than MEM_MODE are pushed, account for them. */
2980 #ifdef PUSH_ROUNDING
2981 if (ep->to_rtx == stack_pointer_rtx)
2982 size = PUSH_ROUNDING (size);
2983 #endif
2984 if (code == PRE_DEC || code == POST_DEC)
2985 ep->offset += size;
2986 else if (code == PRE_INC || code == POST_INC)
2987 ep->offset -= size;
2988 else if (code == PRE_MODIFY || code == POST_MODIFY)
2990 if (GET_CODE (XEXP (x, 1)) == PLUS
2991 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2992 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
2993 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2994 else
2995 ep->can_eliminate = 0;
2999 /* These two aren't unary operators. */
3000 if (code == POST_MODIFY || code == PRE_MODIFY)
3001 break;
3003 /* Fall through to generic unary operation case. */
3004 case STRICT_LOW_PART:
3005 case NEG: case NOT:
3006 case SIGN_EXTEND: case ZERO_EXTEND:
3007 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3008 case FLOAT: case FIX:
3009 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3010 case ABS:
3011 case SQRT:
3012 case FFS:
3013 case CLZ:
3014 case CTZ:
3015 case POPCOUNT:
3016 case PARITY:
3017 case BSWAP:
3018 elimination_effects (XEXP (x, 0), mem_mode);
3019 return;
3021 case SUBREG:
3022 if (REG_P (SUBREG_REG (x))
3023 && (GET_MODE_SIZE (GET_MODE (x))
3024 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3025 && reg_equiv_memory_loc != 0
3026 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3027 return;
3029 elimination_effects (SUBREG_REG (x), mem_mode);
3030 return;
3032 case USE:
3033 /* If using a register that is the source of an eliminate we still
3034 think can be performed, note it cannot be performed since we don't
3035 know how this register is used. */
3036 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3037 if (ep->from_rtx == XEXP (x, 0))
3038 ep->can_eliminate = 0;
3040 elimination_effects (XEXP (x, 0), mem_mode);
3041 return;
3043 case CLOBBER:
3044 /* If clobbering a register that is the replacement register for an
3045 elimination we still think can be performed, note that it cannot
3046 be performed. Otherwise, we need not be concerned about it. */
3047 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3048 if (ep->to_rtx == XEXP (x, 0))
3049 ep->can_eliminate = 0;
3051 elimination_effects (XEXP (x, 0), mem_mode);
3052 return;
3054 case SET:
3055 /* Check for setting a register that we know about. */
3056 if (REG_P (SET_DEST (x)))
3058 /* See if this is setting the replacement register for an
3059 elimination.
3061 If DEST is the hard frame pointer, we do nothing because we
3062 assume that all assignments to the frame pointer are for
3063 non-local gotos and are being done at a time when they are valid
3064 and do not disturb anything else. Some machines want to
3065 eliminate a fake argument pointer (or even a fake frame pointer)
3066 with either the real frame or the stack pointer. Assignments to
3067 the hard frame pointer must not prevent this elimination. */
3069 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3070 ep++)
3071 if (ep->to_rtx == SET_DEST (x)
3072 && SET_DEST (x) != hard_frame_pointer_rtx)
3074 /* If it is being incremented, adjust the offset. Otherwise,
3075 this elimination can't be done. */
3076 rtx src = SET_SRC (x);
3078 if (GET_CODE (src) == PLUS
3079 && XEXP (src, 0) == SET_DEST (x)
3080 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3081 ep->offset -= INTVAL (XEXP (src, 1));
3082 else
3083 ep->can_eliminate = 0;
3087 elimination_effects (SET_DEST (x), 0);
3088 elimination_effects (SET_SRC (x), 0);
3089 return;
3091 case MEM:
3092 /* Our only special processing is to pass the mode of the MEM to our
3093 recursive call. */
3094 elimination_effects (XEXP (x, 0), GET_MODE (x));
3095 return;
3097 default:
3098 break;
3101 fmt = GET_RTX_FORMAT (code);
3102 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3104 if (*fmt == 'e')
3105 elimination_effects (XEXP (x, i), mem_mode);
3106 else if (*fmt == 'E')
3107 for (j = 0; j < XVECLEN (x, i); j++)
3108 elimination_effects (XVECEXP (x, i, j), mem_mode);
3112 /* Descend through rtx X and verify that no references to eliminable registers
3113 remain. If any do remain, mark the involved register as not
3114 eliminable. */
3116 static void
3117 check_eliminable_occurrences (rtx x)
3119 const char *fmt;
3120 int i;
3121 enum rtx_code code;
3123 if (x == 0)
3124 return;
3126 code = GET_CODE (x);
3128 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3130 struct elim_table *ep;
3132 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3133 if (ep->from_rtx == x)
3134 ep->can_eliminate = 0;
3135 return;
3138 fmt = GET_RTX_FORMAT (code);
3139 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3141 if (*fmt == 'e')
3142 check_eliminable_occurrences (XEXP (x, i));
3143 else if (*fmt == 'E')
3145 int j;
3146 for (j = 0; j < XVECLEN (x, i); j++)
3147 check_eliminable_occurrences (XVECEXP (x, i, j));
3152 /* Scan INSN and eliminate all eliminable registers in it.
3154 If REPLACE is nonzero, do the replacement destructively. Also
3155 delete the insn as dead it if it is setting an eliminable register.
3157 If REPLACE is zero, do all our allocations in reload_obstack.
3159 If no eliminations were done and this insn doesn't require any elimination
3160 processing (these are not identical conditions: it might be updating sp,
3161 but not referencing fp; this needs to be seen during reload_as_needed so
3162 that the offset between fp and sp can be taken into consideration), zero
3163 is returned. Otherwise, 1 is returned. */
3165 static int
3166 eliminate_regs_in_insn (rtx insn, int replace)
3168 int icode = recog_memoized (insn);
3169 rtx old_body = PATTERN (insn);
3170 int insn_is_asm = asm_noperands (old_body) >= 0;
3171 rtx old_set = single_set (insn);
3172 rtx new_body;
3173 int val = 0;
3174 int i;
3175 rtx substed_operand[MAX_RECOG_OPERANDS];
3176 rtx orig_operand[MAX_RECOG_OPERANDS];
3177 struct elim_table *ep;
3178 rtx plus_src, plus_cst_src;
3180 if (! insn_is_asm && icode < 0)
3182 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3183 || GET_CODE (PATTERN (insn)) == CLOBBER
3184 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3185 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3186 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
3187 return 0;
3190 if (old_set != 0 && REG_P (SET_DEST (old_set))
3191 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3193 /* Check for setting an eliminable register. */
3194 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3195 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3197 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3198 /* If this is setting the frame pointer register to the
3199 hardware frame pointer register and this is an elimination
3200 that will be done (tested above), this insn is really
3201 adjusting the frame pointer downward to compensate for
3202 the adjustment done before a nonlocal goto. */
3203 if (ep->from == FRAME_POINTER_REGNUM
3204 && ep->to == HARD_FRAME_POINTER_REGNUM)
3206 rtx base = SET_SRC (old_set);
3207 rtx base_insn = insn;
3208 HOST_WIDE_INT offset = 0;
3210 while (base != ep->to_rtx)
3212 rtx prev_insn, prev_set;
3214 if (GET_CODE (base) == PLUS
3215 && GET_CODE (XEXP (base, 1)) == CONST_INT)
3217 offset += INTVAL (XEXP (base, 1));
3218 base = XEXP (base, 0);
3220 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3221 && (prev_set = single_set (prev_insn)) != 0
3222 && rtx_equal_p (SET_DEST (prev_set), base))
3224 base = SET_SRC (prev_set);
3225 base_insn = prev_insn;
3227 else
3228 break;
3231 if (base == ep->to_rtx)
3233 rtx src
3234 = plus_constant (ep->to_rtx, offset - ep->offset);
3236 new_body = old_body;
3237 if (! replace)
3239 new_body = copy_insn (old_body);
3240 if (REG_NOTES (insn))
3241 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3243 PATTERN (insn) = new_body;
3244 old_set = single_set (insn);
3246 /* First see if this insn remains valid when we
3247 make the change. If not, keep the INSN_CODE
3248 the same and let reload fit it up. */
3249 validate_change (insn, &SET_SRC (old_set), src, 1);
3250 validate_change (insn, &SET_DEST (old_set),
3251 ep->to_rtx, 1);
3252 if (! apply_change_group ())
3254 SET_SRC (old_set) = src;
3255 SET_DEST (old_set) = ep->to_rtx;
3258 val = 1;
3259 goto done;
3262 #endif
3264 /* In this case this insn isn't serving a useful purpose. We
3265 will delete it in reload_as_needed once we know that this
3266 elimination is, in fact, being done.
3268 If REPLACE isn't set, we can't delete this insn, but needn't
3269 process it since it won't be used unless something changes. */
3270 if (replace)
3272 delete_dead_insn (insn);
3273 return 1;
3275 val = 1;
3276 goto done;
3280 /* We allow one special case which happens to work on all machines we
3281 currently support: a single set with the source or a REG_EQUAL
3282 note being a PLUS of an eliminable register and a constant. */
3283 plus_src = plus_cst_src = 0;
3284 if (old_set && REG_P (SET_DEST (old_set)))
3286 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3287 plus_src = SET_SRC (old_set);
3288 /* First see if the source is of the form (plus (...) CST). */
3289 if (plus_src
3290 && GET_CODE (XEXP (plus_src, 1)) == CONST_INT)
3291 plus_cst_src = plus_src;
3292 else if (REG_P (SET_SRC (old_set))
3293 || plus_src)
3295 /* Otherwise, see if we have a REG_EQUAL note of the form
3296 (plus (...) CST). */
3297 rtx links;
3298 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3300 if ((REG_NOTE_KIND (links) == REG_EQUAL
3301 || REG_NOTE_KIND (links) == REG_EQUIV)
3302 && GET_CODE (XEXP (links, 0)) == PLUS
3303 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT)
3305 plus_cst_src = XEXP (links, 0);
3306 break;
3311 /* Check that the first operand of the PLUS is a hard reg or
3312 the lowpart subreg of one. */
3313 if (plus_cst_src)
3315 rtx reg = XEXP (plus_cst_src, 0);
3316 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3317 reg = SUBREG_REG (reg);
3319 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3320 plus_cst_src = 0;
3323 if (plus_cst_src)
3325 rtx reg = XEXP (plus_cst_src, 0);
3326 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3328 if (GET_CODE (reg) == SUBREG)
3329 reg = SUBREG_REG (reg);
3331 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3332 if (ep->from_rtx == reg && ep->can_eliminate)
3334 rtx to_rtx = ep->to_rtx;
3335 offset += ep->offset;
3336 offset = trunc_int_for_mode (offset, GET_MODE (reg));
3338 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3339 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3340 to_rtx);
3341 /* If we have a nonzero offset, and the source is already
3342 a simple REG, the following transformation would
3343 increase the cost of the insn by replacing a simple REG
3344 with (plus (reg sp) CST). So try only when we already
3345 had a PLUS before. */
3346 if (offset == 0 || plus_src)
3348 rtx new_src = plus_constant (to_rtx, offset);
3350 new_body = old_body;
3351 if (! replace)
3353 new_body = copy_insn (old_body);
3354 if (REG_NOTES (insn))
3355 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3357 PATTERN (insn) = new_body;
3358 old_set = single_set (insn);
3360 /* First see if this insn remains valid when we make the
3361 change. If not, try to replace the whole pattern with
3362 a simple set (this may help if the original insn was a
3363 PARALLEL that was only recognized as single_set due to
3364 REG_UNUSED notes). If this isn't valid either, keep
3365 the INSN_CODE the same and let reload fix it up. */
3366 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3368 rtx new_pat = gen_rtx_SET (VOIDmode,
3369 SET_DEST (old_set), new_src);
3371 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3372 SET_SRC (old_set) = new_src;
3375 else
3376 break;
3378 val = 1;
3379 /* This can't have an effect on elimination offsets, so skip right
3380 to the end. */
3381 goto done;
3385 /* Determine the effects of this insn on elimination offsets. */
3386 elimination_effects (old_body, 0);
3388 /* Eliminate all eliminable registers occurring in operands that
3389 can be handled by reload. */
3390 extract_insn (insn);
3391 for (i = 0; i < recog_data.n_operands; i++)
3393 orig_operand[i] = recog_data.operand[i];
3394 substed_operand[i] = recog_data.operand[i];
3396 /* For an asm statement, every operand is eliminable. */
3397 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3399 bool is_set_src, in_plus;
3401 /* Check for setting a register that we know about. */
3402 if (recog_data.operand_type[i] != OP_IN
3403 && REG_P (orig_operand[i]))
3405 /* If we are assigning to a register that can be eliminated, it
3406 must be as part of a PARALLEL, since the code above handles
3407 single SETs. We must indicate that we can no longer
3408 eliminate this reg. */
3409 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3410 ep++)
3411 if (ep->from_rtx == orig_operand[i])
3412 ep->can_eliminate = 0;
3415 /* Companion to the above plus substitution, we can allow
3416 invariants as the source of a plain move. */
3417 is_set_src = false;
3418 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3419 is_set_src = true;
3420 in_plus = false;
3421 if (plus_src
3422 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3423 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3424 in_plus = true;
3426 substed_operand[i]
3427 = eliminate_regs_1 (recog_data.operand[i], 0,
3428 replace ? insn : NULL_RTX,
3429 is_set_src || in_plus);
3430 if (substed_operand[i] != orig_operand[i])
3431 val = 1;
3432 /* Terminate the search in check_eliminable_occurrences at
3433 this point. */
3434 *recog_data.operand_loc[i] = 0;
3436 /* If an output operand changed from a REG to a MEM and INSN is an
3437 insn, write a CLOBBER insn. */
3438 if (recog_data.operand_type[i] != OP_IN
3439 && REG_P (orig_operand[i])
3440 && MEM_P (substed_operand[i])
3441 && replace)
3442 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3443 insn);
3447 for (i = 0; i < recog_data.n_dups; i++)
3448 *recog_data.dup_loc[i]
3449 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3451 /* If any eliminable remain, they aren't eliminable anymore. */
3452 check_eliminable_occurrences (old_body);
3454 /* Substitute the operands; the new values are in the substed_operand
3455 array. */
3456 for (i = 0; i < recog_data.n_operands; i++)
3457 *recog_data.operand_loc[i] = substed_operand[i];
3458 for (i = 0; i < recog_data.n_dups; i++)
3459 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3461 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3462 re-recognize the insn. We do this in case we had a simple addition
3463 but now can do this as a load-address. This saves an insn in this
3464 common case.
3465 If re-recognition fails, the old insn code number will still be used,
3466 and some register operands may have changed into PLUS expressions.
3467 These will be handled by find_reloads by loading them into a register
3468 again. */
3470 if (val)
3472 /* If we aren't replacing things permanently and we changed something,
3473 make another copy to ensure that all the RTL is new. Otherwise
3474 things can go wrong if find_reload swaps commutative operands
3475 and one is inside RTL that has been copied while the other is not. */
3476 new_body = old_body;
3477 if (! replace)
3479 new_body = copy_insn (old_body);
3480 if (REG_NOTES (insn))
3481 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3483 PATTERN (insn) = new_body;
3485 /* If we had a move insn but now we don't, rerecognize it. This will
3486 cause spurious re-recognition if the old move had a PARALLEL since
3487 the new one still will, but we can't call single_set without
3488 having put NEW_BODY into the insn and the re-recognition won't
3489 hurt in this rare case. */
3490 /* ??? Why this huge if statement - why don't we just rerecognize the
3491 thing always? */
3492 if (! insn_is_asm
3493 && old_set != 0
3494 && ((REG_P (SET_SRC (old_set))
3495 && (GET_CODE (new_body) != SET
3496 || !REG_P (SET_SRC (new_body))))
3497 /* If this was a load from or store to memory, compare
3498 the MEM in recog_data.operand to the one in the insn.
3499 If they are not equal, then rerecognize the insn. */
3500 || (old_set != 0
3501 && ((MEM_P (SET_SRC (old_set))
3502 && SET_SRC (old_set) != recog_data.operand[1])
3503 || (MEM_P (SET_DEST (old_set))
3504 && SET_DEST (old_set) != recog_data.operand[0])))
3505 /* If this was an add insn before, rerecognize. */
3506 || GET_CODE (SET_SRC (old_set)) == PLUS))
3508 int new_icode = recog (PATTERN (insn), insn, 0);
3509 if (new_icode >= 0)
3510 INSN_CODE (insn) = new_icode;
3514 /* Restore the old body. If there were any changes to it, we made a copy
3515 of it while the changes were still in place, so we'll correctly return
3516 a modified insn below. */
3517 if (! replace)
3519 /* Restore the old body. */
3520 for (i = 0; i < recog_data.n_operands; i++)
3521 *recog_data.operand_loc[i] = orig_operand[i];
3522 for (i = 0; i < recog_data.n_dups; i++)
3523 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3526 /* Update all elimination pairs to reflect the status after the current
3527 insn. The changes we make were determined by the earlier call to
3528 elimination_effects.
3530 We also detect cases where register elimination cannot be done,
3531 namely, if a register would be both changed and referenced outside a MEM
3532 in the resulting insn since such an insn is often undefined and, even if
3533 not, we cannot know what meaning will be given to it. Note that it is
3534 valid to have a register used in an address in an insn that changes it
3535 (presumably with a pre- or post-increment or decrement).
3537 If anything changes, return nonzero. */
3539 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3541 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3542 ep->can_eliminate = 0;
3544 ep->ref_outside_mem = 0;
3546 if (ep->previous_offset != ep->offset)
3547 val = 1;
3550 done:
3551 /* If we changed something, perform elimination in REG_NOTES. This is
3552 needed even when REPLACE is zero because a REG_DEAD note might refer
3553 to a register that we eliminate and could cause a different number
3554 of spill registers to be needed in the final reload pass than in
3555 the pre-passes. */
3556 if (val && REG_NOTES (insn) != 0)
3557 REG_NOTES (insn)
3558 = eliminate_regs_1 (REG_NOTES (insn), 0, REG_NOTES (insn), true);
3560 return val;
3563 /* Loop through all elimination pairs.
3564 Recalculate the number not at initial offset.
3566 Compute the maximum offset (minimum offset if the stack does not
3567 grow downward) for each elimination pair. */
3569 static void
3570 update_eliminable_offsets (void)
3572 struct elim_table *ep;
3574 num_not_at_initial_offset = 0;
3575 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3577 ep->previous_offset = ep->offset;
3578 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3579 num_not_at_initial_offset++;
3583 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3584 replacement we currently believe is valid, mark it as not eliminable if X
3585 modifies DEST in any way other than by adding a constant integer to it.
3587 If DEST is the frame pointer, we do nothing because we assume that
3588 all assignments to the hard frame pointer are nonlocal gotos and are being
3589 done at a time when they are valid and do not disturb anything else.
3590 Some machines want to eliminate a fake argument pointer with either the
3591 frame or stack pointer. Assignments to the hard frame pointer must not
3592 prevent this elimination.
3594 Called via note_stores from reload before starting its passes to scan
3595 the insns of the function. */
3597 static void
3598 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3600 unsigned int i;
3602 /* A SUBREG of a hard register here is just changing its mode. We should
3603 not see a SUBREG of an eliminable hard register, but check just in
3604 case. */
3605 if (GET_CODE (dest) == SUBREG)
3606 dest = SUBREG_REG (dest);
3608 if (dest == hard_frame_pointer_rtx)
3609 return;
3611 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3612 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3613 && (GET_CODE (x) != SET
3614 || GET_CODE (SET_SRC (x)) != PLUS
3615 || XEXP (SET_SRC (x), 0) != dest
3616 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3618 reg_eliminate[i].can_eliminate_previous
3619 = reg_eliminate[i].can_eliminate = 0;
3620 num_eliminable--;
3624 /* Verify that the initial elimination offsets did not change since the
3625 last call to set_initial_elim_offsets. This is used to catch cases
3626 where something illegal happened during reload_as_needed that could
3627 cause incorrect code to be generated if we did not check for it. */
3629 static bool
3630 verify_initial_elim_offsets (void)
3632 HOST_WIDE_INT t;
3634 if (!num_eliminable)
3635 return true;
3637 #ifdef ELIMINABLE_REGS
3639 struct elim_table *ep;
3641 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3643 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3644 if (t != ep->initial_offset)
3645 return false;
3648 #else
3649 INITIAL_FRAME_POINTER_OFFSET (t);
3650 if (t != reg_eliminate[0].initial_offset)
3651 return false;
3652 #endif
3654 return true;
3657 /* Reset all offsets on eliminable registers to their initial values. */
3659 static void
3660 set_initial_elim_offsets (void)
3662 struct elim_table *ep = reg_eliminate;
3664 #ifdef ELIMINABLE_REGS
3665 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3667 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3668 ep->previous_offset = ep->offset = ep->initial_offset;
3670 #else
3671 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3672 ep->previous_offset = ep->offset = ep->initial_offset;
3673 #endif
3675 num_not_at_initial_offset = 0;
3678 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3680 static void
3681 set_initial_eh_label_offset (rtx label)
3683 set_label_offsets (label, NULL_RTX, 1);
3686 /* Initialize the known label offsets.
3687 Set a known offset for each forced label to be at the initial offset
3688 of each elimination. We do this because we assume that all
3689 computed jumps occur from a location where each elimination is
3690 at its initial offset.
3691 For all other labels, show that we don't know the offsets. */
3693 static void
3694 set_initial_label_offsets (void)
3696 rtx x;
3697 memset (offsets_known_at, 0, num_labels);
3699 for (x = forced_labels; x; x = XEXP (x, 1))
3700 if (XEXP (x, 0))
3701 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3703 for_each_eh_label (set_initial_eh_label_offset);
3706 /* Set all elimination offsets to the known values for the code label given
3707 by INSN. */
3709 static void
3710 set_offsets_for_label (rtx insn)
3712 unsigned int i;
3713 int label_nr = CODE_LABEL_NUMBER (insn);
3714 struct elim_table *ep;
3716 num_not_at_initial_offset = 0;
3717 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3719 ep->offset = ep->previous_offset
3720 = offsets_at[label_nr - first_label_num][i];
3721 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3722 num_not_at_initial_offset++;
3726 /* See if anything that happened changes which eliminations are valid.
3727 For example, on the SPARC, whether or not the frame pointer can
3728 be eliminated can depend on what registers have been used. We need
3729 not check some conditions again (such as flag_omit_frame_pointer)
3730 since they can't have changed. */
3732 static void
3733 update_eliminables (HARD_REG_SET *pset)
3735 int previous_frame_pointer_needed = frame_pointer_needed;
3736 struct elim_table *ep;
3738 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3739 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3740 #ifdef ELIMINABLE_REGS
3741 || ! CAN_ELIMINATE (ep->from, ep->to)
3742 #endif
3744 ep->can_eliminate = 0;
3746 /* Look for the case where we have discovered that we can't replace
3747 register A with register B and that means that we will now be
3748 trying to replace register A with register C. This means we can
3749 no longer replace register C with register B and we need to disable
3750 such an elimination, if it exists. This occurs often with A == ap,
3751 B == sp, and C == fp. */
3753 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3755 struct elim_table *op;
3756 int new_to = -1;
3758 if (! ep->can_eliminate && ep->can_eliminate_previous)
3760 /* Find the current elimination for ep->from, if there is a
3761 new one. */
3762 for (op = reg_eliminate;
3763 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3764 if (op->from == ep->from && op->can_eliminate)
3766 new_to = op->to;
3767 break;
3770 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3771 disable it. */
3772 for (op = reg_eliminate;
3773 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3774 if (op->from == new_to && op->to == ep->to)
3775 op->can_eliminate = 0;
3779 /* See if any registers that we thought we could eliminate the previous
3780 time are no longer eliminable. If so, something has changed and we
3781 must spill the register. Also, recompute the number of eliminable
3782 registers and see if the frame pointer is needed; it is if there is
3783 no elimination of the frame pointer that we can perform. */
3785 frame_pointer_needed = 1;
3786 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3788 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3789 && ep->to != HARD_FRAME_POINTER_REGNUM)
3790 frame_pointer_needed = 0;
3792 if (! ep->can_eliminate && ep->can_eliminate_previous)
3794 ep->can_eliminate_previous = 0;
3795 SET_HARD_REG_BIT (*pset, ep->from);
3796 num_eliminable--;
3800 /* If we didn't need a frame pointer last time, but we do now, spill
3801 the hard frame pointer. */
3802 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3803 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3806 /* Return true if X is used as the target register of an elimination. */
3808 bool
3809 elimination_target_reg_p (rtx x)
3811 struct elim_table *ep;
3813 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3814 if (ep->to_rtx == x && ep->can_eliminate)
3815 return true;
3817 return false;
3820 /* Initialize the table of registers to eliminate. */
3822 static void
3823 init_elim_table (void)
3825 struct elim_table *ep;
3826 #ifdef ELIMINABLE_REGS
3827 const struct elim_table_1 *ep1;
3828 #endif
3830 if (!reg_eliminate)
3831 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3833 /* Does this function require a frame pointer? */
3835 frame_pointer_needed = (! flag_omit_frame_pointer
3836 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3837 and restore sp for alloca. So we can't eliminate
3838 the frame pointer in that case. At some point,
3839 we should improve this by emitting the
3840 sp-adjusting insns for this case. */
3841 || (current_function_calls_alloca
3842 && EXIT_IGNORE_STACK)
3843 || current_function_accesses_prior_frames
3844 || FRAME_POINTER_REQUIRED);
3846 num_eliminable = 0;
3848 #ifdef ELIMINABLE_REGS
3849 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3850 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3852 ep->from = ep1->from;
3853 ep->to = ep1->to;
3854 ep->can_eliminate = ep->can_eliminate_previous
3855 = (CAN_ELIMINATE (ep->from, ep->to)
3856 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3858 #else
3859 reg_eliminate[0].from = reg_eliminate_1[0].from;
3860 reg_eliminate[0].to = reg_eliminate_1[0].to;
3861 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3862 = ! frame_pointer_needed;
3863 #endif
3865 /* Count the number of eliminable registers and build the FROM and TO
3866 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3867 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3868 We depend on this. */
3869 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3871 num_eliminable += ep->can_eliminate;
3872 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3873 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3877 /* Kick all pseudos out of hard register REGNO.
3879 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3880 because we found we can't eliminate some register. In the case, no pseudos
3881 are allowed to be in the register, even if they are only in a block that
3882 doesn't require spill registers, unlike the case when we are spilling this
3883 hard reg to produce another spill register.
3885 Return nonzero if any pseudos needed to be kicked out. */
3887 static void
3888 spill_hard_reg (unsigned int regno, int cant_eliminate)
3890 int i;
3892 if (cant_eliminate)
3894 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3895 df_set_regs_ever_live (regno, true);
3898 /* Spill every pseudo reg that was allocated to this reg
3899 or to something that overlaps this reg. */
3901 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3902 if (reg_renumber[i] >= 0
3903 && (unsigned int) reg_renumber[i] <= regno
3904 && end_hard_regno (PSEUDO_REGNO_MODE (i), reg_renumber[i]) > regno)
3905 SET_REGNO_REG_SET (&spilled_pseudos, i);
3908 /* After find_reload_regs has been run for all insn that need reloads,
3909 and/or spill_hard_regs was called, this function is used to actually
3910 spill pseudo registers and try to reallocate them. It also sets up the
3911 spill_regs array for use by choose_reload_regs. */
3913 static int
3914 finish_spills (int global)
3916 struct insn_chain *chain;
3917 int something_changed = 0;
3918 unsigned i;
3919 reg_set_iterator rsi;
3921 /* Build the spill_regs array for the function. */
3922 /* If there are some registers still to eliminate and one of the spill regs
3923 wasn't ever used before, additional stack space may have to be
3924 allocated to store this register. Thus, we may have changed the offset
3925 between the stack and frame pointers, so mark that something has changed.
3927 One might think that we need only set VAL to 1 if this is a call-used
3928 register. However, the set of registers that must be saved by the
3929 prologue is not identical to the call-used set. For example, the
3930 register used by the call insn for the return PC is a call-used register,
3931 but must be saved by the prologue. */
3933 n_spills = 0;
3934 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3935 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3937 spill_reg_order[i] = n_spills;
3938 spill_regs[n_spills++] = i;
3939 if (num_eliminable && ! df_regs_ever_live_p (i))
3940 something_changed = 1;
3941 df_set_regs_ever_live (i, true);
3943 else
3944 spill_reg_order[i] = -1;
3946 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
3947 if (! flag_ira || reg_renumber[i] >= 0)
3949 /* Record the current hard register the pseudo is allocated to
3950 in pseudo_previous_regs so we avoid reallocating it to the
3951 same hard reg in a later pass. */
3952 gcc_assert (reg_renumber[i] >= 0);
3954 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3955 /* Mark it as no longer having a hard register home. */
3956 reg_renumber[i] = -1;
3957 if (flag_ira)
3958 /* Inform IRA about the change. */
3959 mark_allocation_change (i);
3960 /* We will need to scan everything again. */
3961 something_changed = 1;
3964 /* Retry global register allocation if possible. */
3965 if (global)
3967 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3968 /* For every insn that needs reloads, set the registers used as spill
3969 regs in pseudo_forbidden_regs for every pseudo live across the
3970 insn. */
3971 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3973 EXECUTE_IF_SET_IN_REG_SET
3974 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
3976 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3977 chain->used_spill_regs);
3979 EXECUTE_IF_SET_IN_REG_SET
3980 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
3982 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3983 chain->used_spill_regs);
3987 if (! flag_ira)
3989 /* Retry allocating the spilled pseudos. For each reg,
3990 merge the various reg sets that indicate which hard regs
3991 can't be used, and call retry_global_alloc. We change
3992 spill_pseudos here to only contain pseudos that did not
3993 get a new hard register. */
3994 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3995 if (reg_old_renumber[i] != reg_renumber[i])
3997 HARD_REG_SET forbidden;
3999 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
4000 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
4001 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
4002 retry_global_alloc (i, forbidden);
4003 if (reg_renumber[i] >= 0)
4004 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
4007 else
4009 /* Retry allocating the pseudos spilled in IRA and the
4010 reload. For each reg, merge the various reg sets that
4011 indicate which hard regs can't be used, and call
4012 reassign_pseudos. */
4013 unsigned int n;
4015 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
4016 if (reg_old_renumber[i] != reg_renumber[i])
4018 if (reg_renumber[i] < 0)
4019 temp_pseudo_reg_arr[n++] = i;
4020 else
4021 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
4023 if (reassign_pseudos (temp_pseudo_reg_arr, n, bad_spill_regs_global,
4024 pseudo_forbidden_regs, pseudo_previous_regs,
4025 &spilled_pseudos))
4026 something_changed = 1;
4030 /* Fix up the register information in the insn chain.
4031 This involves deleting those of the spilled pseudos which did not get
4032 a new hard register home from the live_{before,after} sets. */
4033 for (chain = reload_insn_chain; chain; chain = chain->next)
4035 HARD_REG_SET used_by_pseudos;
4036 HARD_REG_SET used_by_pseudos2;
4038 if (! flag_ira)
4040 /* Don't do it for IRA because IRA and the reload still can
4041 assign hard registers to the spilled pseudos on next
4042 reload iterations. */
4043 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
4044 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
4046 /* Mark any unallocated hard regs as available for spills. That
4047 makes inheritance work somewhat better. */
4048 if (chain->need_reload)
4050 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
4051 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
4052 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4054 /* Save the old value for the sanity test below. */
4055 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
4057 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
4058 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
4059 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4060 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4062 /* Make sure we only enlarge the set. */
4063 gcc_assert (hard_reg_set_subset_p (used_by_pseudos2,
4064 chain->used_spill_regs));
4068 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4069 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
4071 int regno = reg_renumber[i];
4072 if (reg_old_renumber[i] == regno)
4073 continue;
4075 alter_reg (i, reg_old_renumber[i], false);
4076 reg_old_renumber[i] = regno;
4077 if (dump_file)
4079 if (regno == -1)
4080 fprintf (dump_file, " Register %d now on stack.\n\n", i);
4081 else
4082 fprintf (dump_file, " Register %d now in %d.\n\n",
4083 i, reg_renumber[i]);
4087 return something_changed;
4090 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
4092 static void
4093 scan_paradoxical_subregs (rtx x)
4095 int i;
4096 const char *fmt;
4097 enum rtx_code code = GET_CODE (x);
4099 switch (code)
4101 case REG:
4102 case CONST_INT:
4103 case CONST:
4104 case SYMBOL_REF:
4105 case LABEL_REF:
4106 case CONST_DOUBLE:
4107 case CONST_FIXED:
4108 case CONST_VECTOR: /* shouldn't happen, but just in case. */
4109 case CC0:
4110 case PC:
4111 case USE:
4112 case CLOBBER:
4113 return;
4115 case SUBREG:
4116 if (REG_P (SUBREG_REG (x))
4117 && (GET_MODE_SIZE (GET_MODE (x))
4118 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
4120 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4121 = GET_MODE_SIZE (GET_MODE (x));
4122 mark_home_live_1 (REGNO (SUBREG_REG (x)), GET_MODE (x));
4124 return;
4126 default:
4127 break;
4130 fmt = GET_RTX_FORMAT (code);
4131 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4133 if (fmt[i] == 'e')
4134 scan_paradoxical_subregs (XEXP (x, i));
4135 else if (fmt[i] == 'E')
4137 int j;
4138 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4139 scan_paradoxical_subregs (XVECEXP (x, i, j));
4144 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
4145 examine all of the reload insns between PREV and NEXT exclusive, and
4146 annotate all that may trap. */
4148 static void
4149 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
4151 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4152 unsigned int trap_count;
4153 rtx i;
4155 if (note == NULL)
4156 return;
4158 if (may_trap_p (PATTERN (insn)))
4159 trap_count = 1;
4160 else
4162 remove_note (insn, note);
4163 trap_count = 0;
4166 for (i = NEXT_INSN (prev); i != next; i = NEXT_INSN (i))
4167 if (INSN_P (i) && i != insn && may_trap_p (PATTERN (i)))
4169 trap_count++;
4170 REG_NOTES (i)
4171 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (note, 0), REG_NOTES (i));
4175 /* Reload pseudo-registers into hard regs around each insn as needed.
4176 Additional register load insns are output before the insn that needs it
4177 and perhaps store insns after insns that modify the reloaded pseudo reg.
4179 reg_last_reload_reg and reg_reloaded_contents keep track of
4180 which registers are already available in reload registers.
4181 We update these for the reloads that we perform,
4182 as the insns are scanned. */
4184 static void
4185 reload_as_needed (int live_known)
4187 struct insn_chain *chain;
4188 #if defined (AUTO_INC_DEC)
4189 int i;
4190 #endif
4191 rtx x;
4193 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
4194 memset (spill_reg_store, 0, sizeof spill_reg_store);
4195 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
4196 INIT_REG_SET (&reg_has_output_reload);
4197 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4198 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
4200 set_initial_elim_offsets ();
4202 for (chain = reload_insn_chain; chain; chain = chain->next)
4204 rtx prev = 0;
4205 rtx insn = chain->insn;
4206 rtx old_next = NEXT_INSN (insn);
4208 /* If we pass a label, copy the offsets from the label information
4209 into the current offsets of each elimination. */
4210 if (LABEL_P (insn))
4211 set_offsets_for_label (insn);
4213 else if (INSN_P (insn))
4215 regset_head regs_to_forget;
4216 INIT_REG_SET (&regs_to_forget);
4217 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
4219 /* If this is a USE and CLOBBER of a MEM, ensure that any
4220 references to eliminable registers have been removed. */
4222 if ((GET_CODE (PATTERN (insn)) == USE
4223 || GET_CODE (PATTERN (insn)) == CLOBBER)
4224 && MEM_P (XEXP (PATTERN (insn), 0)))
4225 XEXP (XEXP (PATTERN (insn), 0), 0)
4226 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4227 GET_MODE (XEXP (PATTERN (insn), 0)),
4228 NULL_RTX);
4230 /* If we need to do register elimination processing, do so.
4231 This might delete the insn, in which case we are done. */
4232 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4234 eliminate_regs_in_insn (insn, 1);
4235 if (NOTE_P (insn))
4237 update_eliminable_offsets ();
4238 CLEAR_REG_SET (&regs_to_forget);
4239 continue;
4243 /* If need_elim is nonzero but need_reload is zero, one might think
4244 that we could simply set n_reloads to 0. However, find_reloads
4245 could have done some manipulation of the insn (such as swapping
4246 commutative operands), and these manipulations are lost during
4247 the first pass for every insn that needs register elimination.
4248 So the actions of find_reloads must be redone here. */
4250 if (! chain->need_elim && ! chain->need_reload
4251 && ! chain->need_operand_change)
4252 n_reloads = 0;
4253 /* First find the pseudo regs that must be reloaded for this insn.
4254 This info is returned in the tables reload_... (see reload.h).
4255 Also modify the body of INSN by substituting RELOAD
4256 rtx's for those pseudo regs. */
4257 else
4259 CLEAR_REG_SET (&reg_has_output_reload);
4260 CLEAR_HARD_REG_SET (reg_is_output_reload);
4262 find_reloads (insn, 1, spill_indirect_levels, live_known,
4263 spill_reg_order);
4266 if (n_reloads > 0)
4268 rtx next = NEXT_INSN (insn);
4269 rtx p;
4271 prev = PREV_INSN (insn);
4273 /* Now compute which reload regs to reload them into. Perhaps
4274 reusing reload regs from previous insns, or else output
4275 load insns to reload them. Maybe output store insns too.
4276 Record the choices of reload reg in reload_reg_rtx. */
4277 choose_reload_regs (chain);
4279 /* Merge any reloads that we didn't combine for fear of
4280 increasing the number of spill registers needed but now
4281 discover can be safely merged. */
4282 if (SMALL_REGISTER_CLASSES)
4283 merge_assigned_reloads (insn);
4285 /* Generate the insns to reload operands into or out of
4286 their reload regs. */
4287 emit_reload_insns (chain);
4289 /* Substitute the chosen reload regs from reload_reg_rtx
4290 into the insn's body (or perhaps into the bodies of other
4291 load and store insn that we just made for reloading
4292 and that we moved the structure into). */
4293 subst_reloads (insn);
4295 /* Adjust the exception region notes for loads and stores. */
4296 if (flag_non_call_exceptions && !CALL_P (insn))
4297 fixup_eh_region_note (insn, prev, next);
4299 /* If this was an ASM, make sure that all the reload insns
4300 we have generated are valid. If not, give an error
4301 and delete them. */
4302 if (asm_noperands (PATTERN (insn)) >= 0)
4303 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4304 if (p != insn && INSN_P (p)
4305 && GET_CODE (PATTERN (p)) != USE
4306 && (recog_memoized (p) < 0
4307 || (extract_insn (p), ! constrain_operands (1))))
4309 error_for_asm (insn,
4310 "%<asm%> operand requires "
4311 "impossible reload");
4312 delete_insn (p);
4316 if (num_eliminable && chain->need_elim)
4317 update_eliminable_offsets ();
4319 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4320 is no longer validly lying around to save a future reload.
4321 Note that this does not detect pseudos that were reloaded
4322 for this insn in order to be stored in
4323 (obeying register constraints). That is correct; such reload
4324 registers ARE still valid. */
4325 forget_marked_reloads (&regs_to_forget);
4326 CLEAR_REG_SET (&regs_to_forget);
4328 /* There may have been CLOBBER insns placed after INSN. So scan
4329 between INSN and NEXT and use them to forget old reloads. */
4330 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4331 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4332 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4334 #ifdef AUTO_INC_DEC
4335 /* Likewise for regs altered by auto-increment in this insn.
4336 REG_INC notes have been changed by reloading:
4337 find_reloads_address_1 records substitutions for them,
4338 which have been performed by subst_reloads above. */
4339 for (i = n_reloads - 1; i >= 0; i--)
4341 rtx in_reg = rld[i].in_reg;
4342 if (in_reg)
4344 enum rtx_code code = GET_CODE (in_reg);
4345 /* PRE_INC / PRE_DEC will have the reload register ending up
4346 with the same value as the stack slot, but that doesn't
4347 hold true for POST_INC / POST_DEC. Either we have to
4348 convert the memory access to a true POST_INC / POST_DEC,
4349 or we can't use the reload register for inheritance. */
4350 if ((code == POST_INC || code == POST_DEC)
4351 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4352 REGNO (rld[i].reg_rtx))
4353 /* Make sure it is the inc/dec pseudo, and not
4354 some other (e.g. output operand) pseudo. */
4355 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4356 == REGNO (XEXP (in_reg, 0))))
4359 rtx reload_reg = rld[i].reg_rtx;
4360 enum machine_mode mode = GET_MODE (reload_reg);
4361 int n = 0;
4362 rtx p;
4364 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4366 /* We really want to ignore REG_INC notes here, so
4367 use PATTERN (p) as argument to reg_set_p . */
4368 if (reg_set_p (reload_reg, PATTERN (p)))
4369 break;
4370 n = count_occurrences (PATTERN (p), reload_reg, 0);
4371 if (! n)
4372 continue;
4373 if (n == 1)
4375 n = validate_replace_rtx (reload_reg,
4376 gen_rtx_fmt_e (code,
4377 mode,
4378 reload_reg),
4381 /* We must also verify that the constraints
4382 are met after the replacement. */
4383 extract_insn (p);
4384 if (n)
4385 n = constrain_operands (1);
4386 else
4387 break;
4389 /* If the constraints were not met, then
4390 undo the replacement. */
4391 if (!n)
4393 validate_replace_rtx (gen_rtx_fmt_e (code,
4394 mode,
4395 reload_reg),
4396 reload_reg, p);
4397 break;
4401 break;
4403 if (n == 1)
4405 REG_NOTES (p)
4406 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4407 REG_NOTES (p));
4408 /* Mark this as having an output reload so that the
4409 REG_INC processing code below won't invalidate
4410 the reload for inheritance. */
4411 SET_HARD_REG_BIT (reg_is_output_reload,
4412 REGNO (reload_reg));
4413 SET_REGNO_REG_SET (&reg_has_output_reload,
4414 REGNO (XEXP (in_reg, 0)));
4416 else
4417 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4418 NULL);
4420 else if ((code == PRE_INC || code == PRE_DEC)
4421 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4422 REGNO (rld[i].reg_rtx))
4423 /* Make sure it is the inc/dec pseudo, and not
4424 some other (e.g. output operand) pseudo. */
4425 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4426 == REGNO (XEXP (in_reg, 0))))
4428 SET_HARD_REG_BIT (reg_is_output_reload,
4429 REGNO (rld[i].reg_rtx));
4430 SET_REGNO_REG_SET (&reg_has_output_reload,
4431 REGNO (XEXP (in_reg, 0)));
4435 /* If a pseudo that got a hard register is auto-incremented,
4436 we must purge records of copying it into pseudos without
4437 hard registers. */
4438 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4439 if (REG_NOTE_KIND (x) == REG_INC)
4441 /* See if this pseudo reg was reloaded in this insn.
4442 If so, its last-reload info is still valid
4443 because it is based on this insn's reload. */
4444 for (i = 0; i < n_reloads; i++)
4445 if (rld[i].out == XEXP (x, 0))
4446 break;
4448 if (i == n_reloads)
4449 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4451 #endif
4453 /* A reload reg's contents are unknown after a label. */
4454 if (LABEL_P (insn))
4455 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4457 /* Don't assume a reload reg is still good after a call insn
4458 if it is a call-used reg, or if it contains a value that will
4459 be partially clobbered by the call. */
4460 else if (CALL_P (insn))
4462 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4463 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4467 /* Clean up. */
4468 free (reg_last_reload_reg);
4469 CLEAR_REG_SET (&reg_has_output_reload);
4472 /* Discard all record of any value reloaded from X,
4473 or reloaded in X from someplace else;
4474 unless X is an output reload reg of the current insn.
4476 X may be a hard reg (the reload reg)
4477 or it may be a pseudo reg that was reloaded from.
4479 When DATA is non-NULL just mark the registers in regset
4480 to be forgotten later. */
4482 static void
4483 forget_old_reloads_1 (rtx x, const_rtx ignored ATTRIBUTE_UNUSED,
4484 void *data)
4486 unsigned int regno;
4487 unsigned int nr;
4488 regset regs = (regset) data;
4490 /* note_stores does give us subregs of hard regs,
4491 subreg_regno_offset requires a hard reg. */
4492 while (GET_CODE (x) == SUBREG)
4494 /* We ignore the subreg offset when calculating the regno,
4495 because we are using the entire underlying hard register
4496 below. */
4497 x = SUBREG_REG (x);
4500 if (!REG_P (x))
4501 return;
4503 regno = REGNO (x);
4505 if (regno >= FIRST_PSEUDO_REGISTER)
4506 nr = 1;
4507 else
4509 unsigned int i;
4511 nr = hard_regno_nregs[regno][GET_MODE (x)];
4512 /* Storing into a spilled-reg invalidates its contents.
4513 This can happen if a block-local pseudo is allocated to that reg
4514 and it wasn't spilled because this block's total need is 0.
4515 Then some insn might have an optional reload and use this reg. */
4516 if (!regs)
4517 for (i = 0; i < nr; i++)
4518 /* But don't do this if the reg actually serves as an output
4519 reload reg in the current instruction. */
4520 if (n_reloads == 0
4521 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4523 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4524 spill_reg_store[regno + i] = 0;
4528 if (regs)
4529 while (nr-- > 0)
4530 SET_REGNO_REG_SET (regs, regno + nr);
4531 else
4533 /* Since value of X has changed,
4534 forget any value previously copied from it. */
4536 while (nr-- > 0)
4537 /* But don't forget a copy if this is the output reload
4538 that establishes the copy's validity. */
4539 if (n_reloads == 0
4540 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4541 reg_last_reload_reg[regno + nr] = 0;
4545 /* Forget the reloads marked in regset by previous function. */
4546 static void
4547 forget_marked_reloads (regset regs)
4549 unsigned int reg;
4550 reg_set_iterator rsi;
4551 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4553 if (reg < FIRST_PSEUDO_REGISTER
4554 /* But don't do this if the reg actually serves as an output
4555 reload reg in the current instruction. */
4556 && (n_reloads == 0
4557 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4559 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4560 spill_reg_store[reg] = 0;
4562 if (n_reloads == 0
4563 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4564 reg_last_reload_reg[reg] = 0;
4568 /* The following HARD_REG_SETs indicate when each hard register is
4569 used for a reload of various parts of the current insn. */
4571 /* If reg is unavailable for all reloads. */
4572 static HARD_REG_SET reload_reg_unavailable;
4573 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4574 static HARD_REG_SET reload_reg_used;
4575 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4576 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4577 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4578 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4579 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4580 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4581 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4582 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4583 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4584 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4585 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4586 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4587 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4588 static HARD_REG_SET reload_reg_used_in_op_addr;
4589 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4590 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4591 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4592 static HARD_REG_SET reload_reg_used_in_insn;
4593 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4594 static HARD_REG_SET reload_reg_used_in_other_addr;
4596 /* If reg is in use as a reload reg for any sort of reload. */
4597 static HARD_REG_SET reload_reg_used_at_all;
4599 /* If reg is use as an inherited reload. We just mark the first register
4600 in the group. */
4601 static HARD_REG_SET reload_reg_used_for_inherit;
4603 /* Records which hard regs are used in any way, either as explicit use or
4604 by being allocated to a pseudo during any point of the current insn. */
4605 static HARD_REG_SET reg_used_in_insn;
4607 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4608 TYPE. MODE is used to indicate how many consecutive regs are
4609 actually used. */
4611 static void
4612 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4613 enum machine_mode mode)
4615 unsigned int nregs = hard_regno_nregs[regno][mode];
4616 unsigned int i;
4618 for (i = regno; i < nregs + regno; i++)
4620 switch (type)
4622 case RELOAD_OTHER:
4623 SET_HARD_REG_BIT (reload_reg_used, i);
4624 break;
4626 case RELOAD_FOR_INPUT_ADDRESS:
4627 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4628 break;
4630 case RELOAD_FOR_INPADDR_ADDRESS:
4631 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4632 break;
4634 case RELOAD_FOR_OUTPUT_ADDRESS:
4635 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4636 break;
4638 case RELOAD_FOR_OUTADDR_ADDRESS:
4639 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4640 break;
4642 case RELOAD_FOR_OPERAND_ADDRESS:
4643 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4644 break;
4646 case RELOAD_FOR_OPADDR_ADDR:
4647 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4648 break;
4650 case RELOAD_FOR_OTHER_ADDRESS:
4651 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4652 break;
4654 case RELOAD_FOR_INPUT:
4655 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4656 break;
4658 case RELOAD_FOR_OUTPUT:
4659 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4660 break;
4662 case RELOAD_FOR_INSN:
4663 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4664 break;
4667 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4671 /* Similarly, but show REGNO is no longer in use for a reload. */
4673 static void
4674 clear_reload_reg_in_use (unsigned int regno, int opnum,
4675 enum reload_type type, enum machine_mode mode)
4677 unsigned int nregs = hard_regno_nregs[regno][mode];
4678 unsigned int start_regno, end_regno, r;
4679 int i;
4680 /* A complication is that for some reload types, inheritance might
4681 allow multiple reloads of the same types to share a reload register.
4682 We set check_opnum if we have to check only reloads with the same
4683 operand number, and check_any if we have to check all reloads. */
4684 int check_opnum = 0;
4685 int check_any = 0;
4686 HARD_REG_SET *used_in_set;
4688 switch (type)
4690 case RELOAD_OTHER:
4691 used_in_set = &reload_reg_used;
4692 break;
4694 case RELOAD_FOR_INPUT_ADDRESS:
4695 used_in_set = &reload_reg_used_in_input_addr[opnum];
4696 break;
4698 case RELOAD_FOR_INPADDR_ADDRESS:
4699 check_opnum = 1;
4700 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4701 break;
4703 case RELOAD_FOR_OUTPUT_ADDRESS:
4704 used_in_set = &reload_reg_used_in_output_addr[opnum];
4705 break;
4707 case RELOAD_FOR_OUTADDR_ADDRESS:
4708 check_opnum = 1;
4709 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4710 break;
4712 case RELOAD_FOR_OPERAND_ADDRESS:
4713 used_in_set = &reload_reg_used_in_op_addr;
4714 break;
4716 case RELOAD_FOR_OPADDR_ADDR:
4717 check_any = 1;
4718 used_in_set = &reload_reg_used_in_op_addr_reload;
4719 break;
4721 case RELOAD_FOR_OTHER_ADDRESS:
4722 used_in_set = &reload_reg_used_in_other_addr;
4723 check_any = 1;
4724 break;
4726 case RELOAD_FOR_INPUT:
4727 used_in_set = &reload_reg_used_in_input[opnum];
4728 break;
4730 case RELOAD_FOR_OUTPUT:
4731 used_in_set = &reload_reg_used_in_output[opnum];
4732 break;
4734 case RELOAD_FOR_INSN:
4735 used_in_set = &reload_reg_used_in_insn;
4736 break;
4737 default:
4738 gcc_unreachable ();
4740 /* We resolve conflicts with remaining reloads of the same type by
4741 excluding the intervals of reload registers by them from the
4742 interval of freed reload registers. Since we only keep track of
4743 one set of interval bounds, we might have to exclude somewhat
4744 more than what would be necessary if we used a HARD_REG_SET here.
4745 But this should only happen very infrequently, so there should
4746 be no reason to worry about it. */
4748 start_regno = regno;
4749 end_regno = regno + nregs;
4750 if (check_opnum || check_any)
4752 for (i = n_reloads - 1; i >= 0; i--)
4754 if (rld[i].when_needed == type
4755 && (check_any || rld[i].opnum == opnum)
4756 && rld[i].reg_rtx)
4758 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4759 unsigned int conflict_end
4760 = end_hard_regno (rld[i].mode, conflict_start);
4762 /* If there is an overlap with the first to-be-freed register,
4763 adjust the interval start. */
4764 if (conflict_start <= start_regno && conflict_end > start_regno)
4765 start_regno = conflict_end;
4766 /* Otherwise, if there is a conflict with one of the other
4767 to-be-freed registers, adjust the interval end. */
4768 if (conflict_start > start_regno && conflict_start < end_regno)
4769 end_regno = conflict_start;
4774 for (r = start_regno; r < end_regno; r++)
4775 CLEAR_HARD_REG_BIT (*used_in_set, r);
4778 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4779 specified by OPNUM and TYPE. */
4781 static int
4782 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4784 int i;
4786 /* In use for a RELOAD_OTHER means it's not available for anything. */
4787 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4788 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4789 return 0;
4791 switch (type)
4793 case RELOAD_OTHER:
4794 /* In use for anything means we can't use it for RELOAD_OTHER. */
4795 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4796 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4797 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4798 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4799 return 0;
4801 for (i = 0; i < reload_n_operands; i++)
4802 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4803 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4804 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4805 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4806 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4807 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4808 return 0;
4810 return 1;
4812 case RELOAD_FOR_INPUT:
4813 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4814 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4815 return 0;
4817 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4818 return 0;
4820 /* If it is used for some other input, can't use it. */
4821 for (i = 0; i < reload_n_operands; i++)
4822 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4823 return 0;
4825 /* If it is used in a later operand's address, can't use it. */
4826 for (i = opnum + 1; i < reload_n_operands; i++)
4827 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4828 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4829 return 0;
4831 return 1;
4833 case RELOAD_FOR_INPUT_ADDRESS:
4834 /* Can't use a register if it is used for an input address for this
4835 operand or used as an input in an earlier one. */
4836 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4837 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4838 return 0;
4840 for (i = 0; i < opnum; i++)
4841 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4842 return 0;
4844 return 1;
4846 case RELOAD_FOR_INPADDR_ADDRESS:
4847 /* Can't use a register if it is used for an input address
4848 for this operand or used as an input in an earlier
4849 one. */
4850 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4851 return 0;
4853 for (i = 0; i < opnum; i++)
4854 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4855 return 0;
4857 return 1;
4859 case RELOAD_FOR_OUTPUT_ADDRESS:
4860 /* Can't use a register if it is used for an output address for this
4861 operand or used as an output in this or a later operand. Note
4862 that multiple output operands are emitted in reverse order, so
4863 the conflicting ones are those with lower indices. */
4864 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4865 return 0;
4867 for (i = 0; i <= opnum; i++)
4868 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4869 return 0;
4871 return 1;
4873 case RELOAD_FOR_OUTADDR_ADDRESS:
4874 /* Can't use a register if it is used for an output address
4875 for this operand or used as an output in this or a
4876 later operand. Note that multiple output operands are
4877 emitted in reverse order, so the conflicting ones are
4878 those with lower indices. */
4879 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4880 return 0;
4882 for (i = 0; i <= opnum; i++)
4883 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4884 return 0;
4886 return 1;
4888 case RELOAD_FOR_OPERAND_ADDRESS:
4889 for (i = 0; i < reload_n_operands; i++)
4890 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4891 return 0;
4893 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4894 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4896 case RELOAD_FOR_OPADDR_ADDR:
4897 for (i = 0; i < reload_n_operands; i++)
4898 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4899 return 0;
4901 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4903 case RELOAD_FOR_OUTPUT:
4904 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4905 outputs, or an operand address for this or an earlier output.
4906 Note that multiple output operands are emitted in reverse order,
4907 so the conflicting ones are those with higher indices. */
4908 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4909 return 0;
4911 for (i = 0; i < reload_n_operands; i++)
4912 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4913 return 0;
4915 for (i = opnum; i < reload_n_operands; i++)
4916 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4917 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4918 return 0;
4920 return 1;
4922 case RELOAD_FOR_INSN:
4923 for (i = 0; i < reload_n_operands; i++)
4924 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4925 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4926 return 0;
4928 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4929 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4931 case RELOAD_FOR_OTHER_ADDRESS:
4932 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4934 default:
4935 gcc_unreachable ();
4939 /* Return 1 if the value in reload reg REGNO, as used by a reload
4940 needed for the part of the insn specified by OPNUM and TYPE,
4941 is still available in REGNO at the end of the insn.
4943 We can assume that the reload reg was already tested for availability
4944 at the time it is needed, and we should not check this again,
4945 in case the reg has already been marked in use. */
4947 static int
4948 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4950 int i;
4952 switch (type)
4954 case RELOAD_OTHER:
4955 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4956 its value must reach the end. */
4957 return 1;
4959 /* If this use is for part of the insn,
4960 its value reaches if no subsequent part uses the same register.
4961 Just like the above function, don't try to do this with lots
4962 of fallthroughs. */
4964 case RELOAD_FOR_OTHER_ADDRESS:
4965 /* Here we check for everything else, since these don't conflict
4966 with anything else and everything comes later. */
4968 for (i = 0; i < reload_n_operands; i++)
4969 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4970 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4971 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4972 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4973 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4974 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4975 return 0;
4977 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4978 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4979 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4980 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4982 case RELOAD_FOR_INPUT_ADDRESS:
4983 case RELOAD_FOR_INPADDR_ADDRESS:
4984 /* Similar, except that we check only for this and subsequent inputs
4985 and the address of only subsequent inputs and we do not need
4986 to check for RELOAD_OTHER objects since they are known not to
4987 conflict. */
4989 for (i = opnum; i < reload_n_operands; i++)
4990 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4991 return 0;
4993 for (i = opnum + 1; i < reload_n_operands; i++)
4994 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4995 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4996 return 0;
4998 for (i = 0; i < reload_n_operands; i++)
4999 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5000 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5001 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5002 return 0;
5004 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5005 return 0;
5007 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5008 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5009 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5011 case RELOAD_FOR_INPUT:
5012 /* Similar to input address, except we start at the next operand for
5013 both input and input address and we do not check for
5014 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5015 would conflict. */
5017 for (i = opnum + 1; i < reload_n_operands; i++)
5018 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5019 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5020 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5021 return 0;
5023 /* ... fall through ... */
5025 case RELOAD_FOR_OPERAND_ADDRESS:
5026 /* Check outputs and their addresses. */
5028 for (i = 0; i < reload_n_operands; i++)
5029 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5030 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5031 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5032 return 0;
5034 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
5036 case RELOAD_FOR_OPADDR_ADDR:
5037 for (i = 0; i < reload_n_operands; i++)
5038 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5039 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5040 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5041 return 0;
5043 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5044 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5045 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5047 case RELOAD_FOR_INSN:
5048 /* These conflict with other outputs with RELOAD_OTHER. So
5049 we need only check for output addresses. */
5051 opnum = reload_n_operands;
5053 /* ... fall through ... */
5055 case RELOAD_FOR_OUTPUT:
5056 case RELOAD_FOR_OUTPUT_ADDRESS:
5057 case RELOAD_FOR_OUTADDR_ADDRESS:
5058 /* We already know these can't conflict with a later output. So the
5059 only thing to check are later output addresses.
5060 Note that multiple output operands are emitted in reverse order,
5061 so the conflicting ones are those with lower indices. */
5062 for (i = 0; i < opnum; i++)
5063 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5064 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5065 return 0;
5067 return 1;
5069 default:
5070 gcc_unreachable ();
5074 /* Like reload_reg_reaches_end_p, but check that the condition holds for
5075 every register in the range [REGNO, REGNO + NREGS). */
5077 static bool
5078 reload_regs_reach_end_p (unsigned int regno, int nregs,
5079 int opnum, enum reload_type type)
5081 int i;
5083 for (i = 0; i < nregs; i++)
5084 if (!reload_reg_reaches_end_p (regno + i, opnum, type))
5085 return false;
5086 return true;
5090 /* Returns whether R1 and R2 are uniquely chained: the value of one
5091 is used by the other, and that value is not used by any other
5092 reload for this insn. This is used to partially undo the decision
5093 made in find_reloads when in the case of multiple
5094 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
5095 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
5096 reloads. This code tries to avoid the conflict created by that
5097 change. It might be cleaner to explicitly keep track of which
5098 RELOAD_FOR_OPADDR_ADDR reload is associated with which
5099 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
5100 this after the fact. */
5101 static bool
5102 reloads_unique_chain_p (int r1, int r2)
5104 int i;
5106 /* We only check input reloads. */
5107 if (! rld[r1].in || ! rld[r2].in)
5108 return false;
5110 /* Avoid anything with output reloads. */
5111 if (rld[r1].out || rld[r2].out)
5112 return false;
5114 /* "chained" means one reload is a component of the other reload,
5115 not the same as the other reload. */
5116 if (rld[r1].opnum != rld[r2].opnum
5117 || rtx_equal_p (rld[r1].in, rld[r2].in)
5118 || rld[r1].optional || rld[r2].optional
5119 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
5120 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
5121 return false;
5123 for (i = 0; i < n_reloads; i ++)
5124 /* Look for input reloads that aren't our two */
5125 if (i != r1 && i != r2 && rld[i].in)
5127 /* If our reload is mentioned at all, it isn't a simple chain. */
5128 if (reg_mentioned_p (rld[r1].in, rld[i].in))
5129 return false;
5131 return true;
5134 /* The recursive function change all occurrences of WHAT in *WHERE
5135 onto REPL. */
5136 static void
5137 substitute (rtx *where, const_rtx what, rtx repl)
5139 const char *fmt;
5140 int i;
5141 enum rtx_code code;
5143 if (*where == 0)
5144 return;
5146 if (*where == what || rtx_equal_p (*where, what))
5148 *where = repl;
5149 return;
5152 code = GET_CODE (*where);
5153 fmt = GET_RTX_FORMAT (code);
5154 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5156 if (fmt[i] == 'E')
5158 int j;
5160 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
5161 substitute (&XVECEXP (*where, i, j), what, repl);
5163 else if (fmt[i] == 'e')
5164 substitute (&XEXP (*where, i), what, repl);
5168 /* The function returns TRUE if chain of reload R1 and R2 (in any
5169 order) can be evaluated without usage of intermediate register for
5170 the reload containing another reload. It is important to see
5171 gen_reload to understand what the function is trying to do. As an
5172 example, let us have reload chain
5174 r2: const
5175 r1: <something> + const
5177 and reload R2 got reload reg HR. The function returns true if
5178 there is a correct insn HR = HR + <something>. Otherwise,
5179 gen_reload will use intermediate register (and this is the reload
5180 reg for R1) to reload <something>.
5182 We need this function to find a conflict for chain reloads. In our
5183 example, if HR = HR + <something> is incorrect insn, then we cannot
5184 use HR as a reload register for R2. If we do use it then we get a
5185 wrong code:
5187 HR = const
5188 HR = <something>
5189 HR = HR + HR
5192 static bool
5193 gen_reload_chain_without_interm_reg_p (int r1, int r2)
5195 bool result;
5196 int regno, n, code;
5197 rtx out, in, tem, insn;
5198 rtx last = get_last_insn ();
5200 /* Make r2 a component of r1. */
5201 if (reg_mentioned_p (rld[r1].in, rld[r2].in))
5203 n = r1;
5204 r1 = r2;
5205 r2 = n;
5207 gcc_assert (reg_mentioned_p (rld[r2].in, rld[r1].in));
5208 regno = rld[r1].regno >= 0 ? rld[r1].regno : rld[r2].regno;
5209 gcc_assert (regno >= 0);
5210 out = gen_rtx_REG (rld[r1].mode, regno);
5211 in = copy_rtx (rld[r1].in);
5212 substitute (&in, rld[r2].in, gen_rtx_REG (rld[r2].mode, regno));
5214 /* If IN is a paradoxical SUBREG, remove it and try to put the
5215 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
5216 if (GET_CODE (in) == SUBREG
5217 && (GET_MODE_SIZE (GET_MODE (in))
5218 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
5219 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
5220 in = SUBREG_REG (in), out = tem;
5222 if (GET_CODE (in) == PLUS
5223 && (REG_P (XEXP (in, 0))
5224 || GET_CODE (XEXP (in, 0)) == SUBREG
5225 || MEM_P (XEXP (in, 0)))
5226 && (REG_P (XEXP (in, 1))
5227 || GET_CODE (XEXP (in, 1)) == SUBREG
5228 || CONSTANT_P (XEXP (in, 1))
5229 || MEM_P (XEXP (in, 1))))
5231 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
5232 code = recog_memoized (insn);
5233 result = false;
5235 if (code >= 0)
5237 extract_insn (insn);
5238 /* We want constrain operands to treat this insn strictly in
5239 its validity determination, i.e., the way it would after
5240 reload has completed. */
5241 result = constrain_operands (1);
5244 delete_insns_since (last);
5245 return result;
5248 /* It looks like other cases in gen_reload are not possible for
5249 chain reloads or do need an intermediate hard registers. */
5250 return true;
5253 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5254 Return 0 otherwise.
5256 This function uses the same algorithm as reload_reg_free_p above. */
5258 static int
5259 reloads_conflict (int r1, int r2)
5261 enum reload_type r1_type = rld[r1].when_needed;
5262 enum reload_type r2_type = rld[r2].when_needed;
5263 int r1_opnum = rld[r1].opnum;
5264 int r2_opnum = rld[r2].opnum;
5266 /* RELOAD_OTHER conflicts with everything. */
5267 if (r2_type == RELOAD_OTHER)
5268 return 1;
5270 /* Otherwise, check conflicts differently for each type. */
5272 switch (r1_type)
5274 case RELOAD_FOR_INPUT:
5275 return (r2_type == RELOAD_FOR_INSN
5276 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5277 || r2_type == RELOAD_FOR_OPADDR_ADDR
5278 || r2_type == RELOAD_FOR_INPUT
5279 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5280 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5281 && r2_opnum > r1_opnum));
5283 case RELOAD_FOR_INPUT_ADDRESS:
5284 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5285 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5287 case RELOAD_FOR_INPADDR_ADDRESS:
5288 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5289 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5291 case RELOAD_FOR_OUTPUT_ADDRESS:
5292 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5293 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5295 case RELOAD_FOR_OUTADDR_ADDRESS:
5296 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5297 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5299 case RELOAD_FOR_OPERAND_ADDRESS:
5300 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5301 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
5302 && (!reloads_unique_chain_p (r1, r2)
5303 || !gen_reload_chain_without_interm_reg_p (r1, r2))));
5305 case RELOAD_FOR_OPADDR_ADDR:
5306 return (r2_type == RELOAD_FOR_INPUT
5307 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5309 case RELOAD_FOR_OUTPUT:
5310 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5311 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5312 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5313 && r2_opnum >= r1_opnum));
5315 case RELOAD_FOR_INSN:
5316 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5317 || r2_type == RELOAD_FOR_INSN
5318 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5320 case RELOAD_FOR_OTHER_ADDRESS:
5321 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5323 case RELOAD_OTHER:
5324 return 1;
5326 default:
5327 gcc_unreachable ();
5331 /* Indexed by reload number, 1 if incoming value
5332 inherited from previous insns. */
5333 static char reload_inherited[MAX_RELOADS];
5335 /* For an inherited reload, this is the insn the reload was inherited from,
5336 if we know it. Otherwise, this is 0. */
5337 static rtx reload_inheritance_insn[MAX_RELOADS];
5339 /* If nonzero, this is a place to get the value of the reload,
5340 rather than using reload_in. */
5341 static rtx reload_override_in[MAX_RELOADS];
5343 /* For each reload, the hard register number of the register used,
5344 or -1 if we did not need a register for this reload. */
5345 static int reload_spill_index[MAX_RELOADS];
5347 /* Index X is the value of rld[X].reg_rtx, adjusted for the input mode. */
5348 static rtx reload_reg_rtx_for_input[MAX_RELOADS];
5350 /* Index X is the value of rld[X].reg_rtx, adjusted for the output mode. */
5351 static rtx reload_reg_rtx_for_output[MAX_RELOADS];
5353 /* Subroutine of free_for_value_p, used to check a single register.
5354 START_REGNO is the starting regno of the full reload register
5355 (possibly comprising multiple hard registers) that we are considering. */
5357 static int
5358 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
5359 enum reload_type type, rtx value, rtx out,
5360 int reloadnum, int ignore_address_reloads)
5362 int time1;
5363 /* Set if we see an input reload that must not share its reload register
5364 with any new earlyclobber, but might otherwise share the reload
5365 register with an output or input-output reload. */
5366 int check_earlyclobber = 0;
5367 int i;
5368 int copy = 0;
5370 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5371 return 0;
5373 if (out == const0_rtx)
5375 copy = 1;
5376 out = NULL_RTX;
5379 /* We use some pseudo 'time' value to check if the lifetimes of the
5380 new register use would overlap with the one of a previous reload
5381 that is not read-only or uses a different value.
5382 The 'time' used doesn't have to be linear in any shape or form, just
5383 monotonic.
5384 Some reload types use different 'buckets' for each operand.
5385 So there are MAX_RECOG_OPERANDS different time values for each
5386 such reload type.
5387 We compute TIME1 as the time when the register for the prospective
5388 new reload ceases to be live, and TIME2 for each existing
5389 reload as the time when that the reload register of that reload
5390 becomes live.
5391 Where there is little to be gained by exact lifetime calculations,
5392 we just make conservative assumptions, i.e. a longer lifetime;
5393 this is done in the 'default:' cases. */
5394 switch (type)
5396 case RELOAD_FOR_OTHER_ADDRESS:
5397 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
5398 time1 = copy ? 0 : 1;
5399 break;
5400 case RELOAD_OTHER:
5401 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5402 break;
5403 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5404 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5405 respectively, to the time values for these, we get distinct time
5406 values. To get distinct time values for each operand, we have to
5407 multiply opnum by at least three. We round that up to four because
5408 multiply by four is often cheaper. */
5409 case RELOAD_FOR_INPADDR_ADDRESS:
5410 time1 = opnum * 4 + 2;
5411 break;
5412 case RELOAD_FOR_INPUT_ADDRESS:
5413 time1 = opnum * 4 + 3;
5414 break;
5415 case RELOAD_FOR_INPUT:
5416 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5417 executes (inclusive). */
5418 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5419 break;
5420 case RELOAD_FOR_OPADDR_ADDR:
5421 /* opnum * 4 + 4
5422 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5423 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5424 break;
5425 case RELOAD_FOR_OPERAND_ADDRESS:
5426 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5427 is executed. */
5428 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5429 break;
5430 case RELOAD_FOR_OUTADDR_ADDRESS:
5431 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5432 break;
5433 case RELOAD_FOR_OUTPUT_ADDRESS:
5434 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5435 break;
5436 default:
5437 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5440 for (i = 0; i < n_reloads; i++)
5442 rtx reg = rld[i].reg_rtx;
5443 if (reg && REG_P (reg)
5444 && ((unsigned) regno - true_regnum (reg)
5445 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5446 && i != reloadnum)
5448 rtx other_input = rld[i].in;
5450 /* If the other reload loads the same input value, that
5451 will not cause a conflict only if it's loading it into
5452 the same register. */
5453 if (true_regnum (reg) != start_regno)
5454 other_input = NULL_RTX;
5455 if (! other_input || ! rtx_equal_p (other_input, value)
5456 || rld[i].out || out)
5458 int time2;
5459 switch (rld[i].when_needed)
5461 case RELOAD_FOR_OTHER_ADDRESS:
5462 time2 = 0;
5463 break;
5464 case RELOAD_FOR_INPADDR_ADDRESS:
5465 /* find_reloads makes sure that a
5466 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5467 by at most one - the first -
5468 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5469 address reload is inherited, the address address reload
5470 goes away, so we can ignore this conflict. */
5471 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5472 && ignore_address_reloads
5473 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5474 Then the address address is still needed to store
5475 back the new address. */
5476 && ! rld[reloadnum].out)
5477 continue;
5478 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5479 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5480 reloads go away. */
5481 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5482 && ignore_address_reloads
5483 /* Unless we are reloading an auto_inc expression. */
5484 && ! rld[reloadnum].out)
5485 continue;
5486 time2 = rld[i].opnum * 4 + 2;
5487 break;
5488 case RELOAD_FOR_INPUT_ADDRESS:
5489 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5490 && ignore_address_reloads
5491 && ! rld[reloadnum].out)
5492 continue;
5493 time2 = rld[i].opnum * 4 + 3;
5494 break;
5495 case RELOAD_FOR_INPUT:
5496 time2 = rld[i].opnum * 4 + 4;
5497 check_earlyclobber = 1;
5498 break;
5499 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5500 == MAX_RECOG_OPERAND * 4 */
5501 case RELOAD_FOR_OPADDR_ADDR:
5502 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5503 && ignore_address_reloads
5504 && ! rld[reloadnum].out)
5505 continue;
5506 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5507 break;
5508 case RELOAD_FOR_OPERAND_ADDRESS:
5509 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5510 check_earlyclobber = 1;
5511 break;
5512 case RELOAD_FOR_INSN:
5513 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5514 break;
5515 case RELOAD_FOR_OUTPUT:
5516 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5517 instruction is executed. */
5518 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5519 break;
5520 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5521 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5522 value. */
5523 case RELOAD_FOR_OUTADDR_ADDRESS:
5524 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5525 && ignore_address_reloads
5526 && ! rld[reloadnum].out)
5527 continue;
5528 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5529 break;
5530 case RELOAD_FOR_OUTPUT_ADDRESS:
5531 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5532 break;
5533 case RELOAD_OTHER:
5534 /* If there is no conflict in the input part, handle this
5535 like an output reload. */
5536 if (! rld[i].in || rtx_equal_p (other_input, value))
5538 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5539 /* Earlyclobbered outputs must conflict with inputs. */
5540 if (earlyclobber_operand_p (rld[i].out))
5541 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5543 break;
5545 time2 = 1;
5546 /* RELOAD_OTHER might be live beyond instruction execution,
5547 but this is not obvious when we set time2 = 1. So check
5548 here if there might be a problem with the new reload
5549 clobbering the register used by the RELOAD_OTHER. */
5550 if (out)
5551 return 0;
5552 break;
5553 default:
5554 return 0;
5556 if ((time1 >= time2
5557 && (! rld[i].in || rld[i].out
5558 || ! rtx_equal_p (other_input, value)))
5559 || (out && rld[reloadnum].out_reg
5560 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5561 return 0;
5566 /* Earlyclobbered outputs must conflict with inputs. */
5567 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5568 return 0;
5570 return 1;
5573 /* Return 1 if the value in reload reg REGNO, as used by a reload
5574 needed for the part of the insn specified by OPNUM and TYPE,
5575 may be used to load VALUE into it.
5577 MODE is the mode in which the register is used, this is needed to
5578 determine how many hard regs to test.
5580 Other read-only reloads with the same value do not conflict
5581 unless OUT is nonzero and these other reloads have to live while
5582 output reloads live.
5583 If OUT is CONST0_RTX, this is a special case: it means that the
5584 test should not be for using register REGNO as reload register, but
5585 for copying from register REGNO into the reload register.
5587 RELOADNUM is the number of the reload we want to load this value for;
5588 a reload does not conflict with itself.
5590 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5591 reloads that load an address for the very reload we are considering.
5593 The caller has to make sure that there is no conflict with the return
5594 register. */
5596 static int
5597 free_for_value_p (int regno, enum machine_mode mode, int opnum,
5598 enum reload_type type, rtx value, rtx out, int reloadnum,
5599 int ignore_address_reloads)
5601 int nregs = hard_regno_nregs[regno][mode];
5602 while (nregs-- > 0)
5603 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5604 value, out, reloadnum,
5605 ignore_address_reloads))
5606 return 0;
5607 return 1;
5610 /* Return nonzero if the rtx X is invariant over the current function. */
5611 /* ??? Actually, the places where we use this expect exactly what is
5612 tested here, and not everything that is function invariant. In
5613 particular, the frame pointer and arg pointer are special cased;
5614 pic_offset_table_rtx is not, and we must not spill these things to
5615 memory. */
5618 function_invariant_p (const_rtx x)
5620 if (CONSTANT_P (x))
5621 return 1;
5622 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
5623 return 1;
5624 if (GET_CODE (x) == PLUS
5625 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
5626 && CONSTANT_P (XEXP (x, 1)))
5627 return 1;
5628 return 0;
5631 /* Determine whether the reload reg X overlaps any rtx'es used for
5632 overriding inheritance. Return nonzero if so. */
5634 static int
5635 conflicts_with_override (rtx x)
5637 int i;
5638 for (i = 0; i < n_reloads; i++)
5639 if (reload_override_in[i]
5640 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5641 return 1;
5642 return 0;
5645 /* Give an error message saying we failed to find a reload for INSN,
5646 and clear out reload R. */
5647 static void
5648 failed_reload (rtx insn, int r)
5650 if (asm_noperands (PATTERN (insn)) < 0)
5651 /* It's the compiler's fault. */
5652 fatal_insn ("could not find a spill register", insn);
5654 /* It's the user's fault; the operand's mode and constraint
5655 don't match. Disable this reload so we don't crash in final. */
5656 error_for_asm (insn,
5657 "%<asm%> operand constraint incompatible with operand size");
5658 rld[r].in = 0;
5659 rld[r].out = 0;
5660 rld[r].reg_rtx = 0;
5661 rld[r].optional = 1;
5662 rld[r].secondary_p = 1;
5665 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5666 for reload R. If it's valid, get an rtx for it. Return nonzero if
5667 successful. */
5668 static int
5669 set_reload_reg (int i, int r)
5671 int regno;
5672 rtx reg = spill_reg_rtx[i];
5674 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5675 spill_reg_rtx[i] = reg
5676 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5678 regno = true_regnum (reg);
5680 /* Detect when the reload reg can't hold the reload mode.
5681 This used to be one `if', but Sequent compiler can't handle that. */
5682 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5684 enum machine_mode test_mode = VOIDmode;
5685 if (rld[r].in)
5686 test_mode = GET_MODE (rld[r].in);
5687 /* If rld[r].in has VOIDmode, it means we will load it
5688 in whatever mode the reload reg has: to wit, rld[r].mode.
5689 We have already tested that for validity. */
5690 /* Aside from that, we need to test that the expressions
5691 to reload from or into have modes which are valid for this
5692 reload register. Otherwise the reload insns would be invalid. */
5693 if (! (rld[r].in != 0 && test_mode != VOIDmode
5694 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5695 if (! (rld[r].out != 0
5696 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5698 /* The reg is OK. */
5699 last_spill_reg = i;
5701 /* Mark as in use for this insn the reload regs we use
5702 for this. */
5703 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5704 rld[r].when_needed, rld[r].mode);
5706 rld[r].reg_rtx = reg;
5707 reload_spill_index[r] = spill_regs[i];
5708 return 1;
5711 return 0;
5714 /* Find a spill register to use as a reload register for reload R.
5715 LAST_RELOAD is nonzero if this is the last reload for the insn being
5716 processed.
5718 Set rld[R].reg_rtx to the register allocated.
5720 We return 1 if successful, or 0 if we couldn't find a spill reg and
5721 we didn't change anything. */
5723 static int
5724 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5725 int last_reload)
5727 int i, pass, count;
5729 /* If we put this reload ahead, thinking it is a group,
5730 then insist on finding a group. Otherwise we can grab a
5731 reg that some other reload needs.
5732 (That can happen when we have a 68000 DATA_OR_FP_REG
5733 which is a group of data regs or one fp reg.)
5734 We need not be so restrictive if there are no more reloads
5735 for this insn.
5737 ??? Really it would be nicer to have smarter handling
5738 for that kind of reg class, where a problem like this is normal.
5739 Perhaps those classes should be avoided for reloading
5740 by use of more alternatives. */
5742 int force_group = rld[r].nregs > 1 && ! last_reload;
5744 /* If we want a single register and haven't yet found one,
5745 take any reg in the right class and not in use.
5746 If we want a consecutive group, here is where we look for it.
5748 We use two passes so we can first look for reload regs to
5749 reuse, which are already in use for other reloads in this insn,
5750 and only then use additional registers.
5751 I think that maximizing reuse is needed to make sure we don't
5752 run out of reload regs. Suppose we have three reloads, and
5753 reloads A and B can share regs. These need two regs.
5754 Suppose A and B are given different regs.
5755 That leaves none for C. */
5756 for (pass = 0; pass < 2; pass++)
5758 /* I is the index in spill_regs.
5759 We advance it round-robin between insns to use all spill regs
5760 equally, so that inherited reloads have a chance
5761 of leapfrogging each other. */
5763 i = last_spill_reg;
5765 for (count = 0; count < n_spills; count++)
5767 int class = (int) rld[r].class;
5768 int regnum;
5770 i++;
5771 if (i >= n_spills)
5772 i -= n_spills;
5773 regnum = spill_regs[i];
5775 if ((reload_reg_free_p (regnum, rld[r].opnum,
5776 rld[r].when_needed)
5777 || (rld[r].in
5778 /* We check reload_reg_used to make sure we
5779 don't clobber the return register. */
5780 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5781 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5782 rld[r].when_needed, rld[r].in,
5783 rld[r].out, r, 1)))
5784 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5785 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5786 /* Look first for regs to share, then for unshared. But
5787 don't share regs used for inherited reloads; they are
5788 the ones we want to preserve. */
5789 && (pass
5790 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5791 regnum)
5792 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5793 regnum))))
5795 int nr = hard_regno_nregs[regnum][rld[r].mode];
5796 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5797 (on 68000) got us two FP regs. If NR is 1,
5798 we would reject both of them. */
5799 if (force_group)
5800 nr = rld[r].nregs;
5801 /* If we need only one reg, we have already won. */
5802 if (nr == 1)
5804 /* But reject a single reg if we demand a group. */
5805 if (force_group)
5806 continue;
5807 break;
5809 /* Otherwise check that as many consecutive regs as we need
5810 are available here. */
5811 while (nr > 1)
5813 int regno = regnum + nr - 1;
5814 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5815 && spill_reg_order[regno] >= 0
5816 && reload_reg_free_p (regno, rld[r].opnum,
5817 rld[r].when_needed)))
5818 break;
5819 nr--;
5821 if (nr == 1)
5822 break;
5826 /* If we found something on pass 1, omit pass 2. */
5827 if (count < n_spills)
5828 break;
5831 /* We should have found a spill register by now. */
5832 if (count >= n_spills)
5833 return 0;
5835 /* I is the index in SPILL_REG_RTX of the reload register we are to
5836 allocate. Get an rtx for it and find its register number. */
5838 return set_reload_reg (i, r);
5841 /* Initialize all the tables needed to allocate reload registers.
5842 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5843 is the array we use to restore the reg_rtx field for every reload. */
5845 static void
5846 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5848 int i;
5850 for (i = 0; i < n_reloads; i++)
5851 rld[i].reg_rtx = save_reload_reg_rtx[i];
5853 memset (reload_inherited, 0, MAX_RELOADS);
5854 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5855 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5857 CLEAR_HARD_REG_SET (reload_reg_used);
5858 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5859 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5860 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5861 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5862 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5864 CLEAR_HARD_REG_SET (reg_used_in_insn);
5866 HARD_REG_SET tmp;
5867 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5868 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5869 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5870 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5871 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5872 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5875 for (i = 0; i < reload_n_operands; i++)
5877 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5878 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5879 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5880 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5881 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5882 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5885 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5887 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5889 for (i = 0; i < n_reloads; i++)
5890 /* If we have already decided to use a certain register,
5891 don't use it in another way. */
5892 if (rld[i].reg_rtx)
5893 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5894 rld[i].when_needed, rld[i].mode);
5897 /* Assign hard reg targets for the pseudo-registers we must reload
5898 into hard regs for this insn.
5899 Also output the instructions to copy them in and out of the hard regs.
5901 For machines with register classes, we are responsible for
5902 finding a reload reg in the proper class. */
5904 static void
5905 choose_reload_regs (struct insn_chain *chain)
5907 rtx insn = chain->insn;
5908 int i, j;
5909 unsigned int max_group_size = 1;
5910 enum reg_class group_class = NO_REGS;
5911 int pass, win, inheritance;
5913 rtx save_reload_reg_rtx[MAX_RELOADS];
5915 /* In order to be certain of getting the registers we need,
5916 we must sort the reloads into order of increasing register class.
5917 Then our grabbing of reload registers will parallel the process
5918 that provided the reload registers.
5920 Also note whether any of the reloads wants a consecutive group of regs.
5921 If so, record the maximum size of the group desired and what
5922 register class contains all the groups needed by this insn. */
5924 for (j = 0; j < n_reloads; j++)
5926 reload_order[j] = j;
5927 if (rld[j].reg_rtx != NULL_RTX)
5929 gcc_assert (REG_P (rld[j].reg_rtx)
5930 && HARD_REGISTER_P (rld[j].reg_rtx));
5931 reload_spill_index[j] = REGNO (rld[j].reg_rtx);
5933 else
5934 reload_spill_index[j] = -1;
5936 if (rld[j].nregs > 1)
5938 max_group_size = MAX (rld[j].nregs, max_group_size);
5939 group_class
5940 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5943 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5946 if (n_reloads > 1)
5947 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5949 /* If -O, try first with inheritance, then turning it off.
5950 If not -O, don't do inheritance.
5951 Using inheritance when not optimizing leads to paradoxes
5952 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5953 because one side of the comparison might be inherited. */
5954 win = 0;
5955 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5957 choose_reload_regs_init (chain, save_reload_reg_rtx);
5959 /* Process the reloads in order of preference just found.
5960 Beyond this point, subregs can be found in reload_reg_rtx.
5962 This used to look for an existing reloaded home for all of the
5963 reloads, and only then perform any new reloads. But that could lose
5964 if the reloads were done out of reg-class order because a later
5965 reload with a looser constraint might have an old home in a register
5966 needed by an earlier reload with a tighter constraint.
5968 To solve this, we make two passes over the reloads, in the order
5969 described above. In the first pass we try to inherit a reload
5970 from a previous insn. If there is a later reload that needs a
5971 class that is a proper subset of the class being processed, we must
5972 also allocate a spill register during the first pass.
5974 Then make a second pass over the reloads to allocate any reloads
5975 that haven't been given registers yet. */
5977 for (j = 0; j < n_reloads; j++)
5979 int r = reload_order[j];
5980 rtx search_equiv = NULL_RTX;
5982 /* Ignore reloads that got marked inoperative. */
5983 if (rld[r].out == 0 && rld[r].in == 0
5984 && ! rld[r].secondary_p)
5985 continue;
5987 /* If find_reloads chose to use reload_in or reload_out as a reload
5988 register, we don't need to chose one. Otherwise, try even if it
5989 found one since we might save an insn if we find the value lying
5990 around.
5991 Try also when reload_in is a pseudo without a hard reg. */
5992 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5993 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5994 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5995 && !MEM_P (rld[r].in)
5996 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5997 continue;
5999 #if 0 /* No longer needed for correct operation.
6000 It might give better code, or might not; worth an experiment? */
6001 /* If this is an optional reload, we can't inherit from earlier insns
6002 until we are sure that any non-optional reloads have been allocated.
6003 The following code takes advantage of the fact that optional reloads
6004 are at the end of reload_order. */
6005 if (rld[r].optional != 0)
6006 for (i = 0; i < j; i++)
6007 if ((rld[reload_order[i]].out != 0
6008 || rld[reload_order[i]].in != 0
6009 || rld[reload_order[i]].secondary_p)
6010 && ! rld[reload_order[i]].optional
6011 && rld[reload_order[i]].reg_rtx == 0)
6012 allocate_reload_reg (chain, reload_order[i], 0);
6013 #endif
6015 /* First see if this pseudo is already available as reloaded
6016 for a previous insn. We cannot try to inherit for reloads
6017 that are smaller than the maximum number of registers needed
6018 for groups unless the register we would allocate cannot be used
6019 for the groups.
6021 We could check here to see if this is a secondary reload for
6022 an object that is already in a register of the desired class.
6023 This would avoid the need for the secondary reload register.
6024 But this is complex because we can't easily determine what
6025 objects might want to be loaded via this reload. So let a
6026 register be allocated here. In `emit_reload_insns' we suppress
6027 one of the loads in the case described above. */
6029 if (inheritance)
6031 int byte = 0;
6032 int regno = -1;
6033 enum machine_mode mode = VOIDmode;
6035 if (rld[r].in == 0)
6037 else if (REG_P (rld[r].in))
6039 regno = REGNO (rld[r].in);
6040 mode = GET_MODE (rld[r].in);
6042 else if (REG_P (rld[r].in_reg))
6044 regno = REGNO (rld[r].in_reg);
6045 mode = GET_MODE (rld[r].in_reg);
6047 else if (GET_CODE (rld[r].in_reg) == SUBREG
6048 && REG_P (SUBREG_REG (rld[r].in_reg)))
6050 regno = REGNO (SUBREG_REG (rld[r].in_reg));
6051 if (regno < FIRST_PSEUDO_REGISTER)
6052 regno = subreg_regno (rld[r].in_reg);
6053 else
6054 byte = SUBREG_BYTE (rld[r].in_reg);
6055 mode = GET_MODE (rld[r].in_reg);
6057 #ifdef AUTO_INC_DEC
6058 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
6059 && REG_P (XEXP (rld[r].in_reg, 0)))
6061 regno = REGNO (XEXP (rld[r].in_reg, 0));
6062 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
6063 rld[r].out = rld[r].in;
6065 #endif
6066 #if 0
6067 /* This won't work, since REGNO can be a pseudo reg number.
6068 Also, it takes much more hair to keep track of all the things
6069 that can invalidate an inherited reload of part of a pseudoreg. */
6070 else if (GET_CODE (rld[r].in) == SUBREG
6071 && REG_P (SUBREG_REG (rld[r].in)))
6072 regno = subreg_regno (rld[r].in);
6073 #endif
6075 if (regno >= 0
6076 && reg_last_reload_reg[regno] != 0
6077 #ifdef CANNOT_CHANGE_MODE_CLASS
6078 /* Verify that the register it's in can be used in
6079 mode MODE. */
6080 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
6081 GET_MODE (reg_last_reload_reg[regno]),
6082 mode)
6083 #endif
6086 enum reg_class class = rld[r].class, last_class;
6087 rtx last_reg = reg_last_reload_reg[regno];
6088 enum machine_mode need_mode;
6090 i = REGNO (last_reg);
6091 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
6092 last_class = REGNO_REG_CLASS (i);
6094 if (byte == 0)
6095 need_mode = mode;
6096 else
6097 need_mode
6098 = smallest_mode_for_size (GET_MODE_BITSIZE (mode)
6099 + byte * BITS_PER_UNIT,
6100 GET_MODE_CLASS (mode));
6102 if ((GET_MODE_SIZE (GET_MODE (last_reg))
6103 >= GET_MODE_SIZE (need_mode))
6104 && reg_reloaded_contents[i] == regno
6105 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
6106 && HARD_REGNO_MODE_OK (i, rld[r].mode)
6107 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
6108 /* Even if we can't use this register as a reload
6109 register, we might use it for reload_override_in,
6110 if copying it to the desired class is cheap
6111 enough. */
6112 || ((REGISTER_MOVE_COST (mode, last_class, class)
6113 < MEMORY_MOVE_COST (mode, class, 1))
6114 && (secondary_reload_class (1, class, mode,
6115 last_reg)
6116 == NO_REGS)
6117 #ifdef SECONDARY_MEMORY_NEEDED
6118 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
6119 mode)
6120 #endif
6123 && (rld[r].nregs == max_group_size
6124 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
6126 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
6127 rld[r].when_needed, rld[r].in,
6128 const0_rtx, r, 1))
6130 /* If a group is needed, verify that all the subsequent
6131 registers still have their values intact. */
6132 int nr = hard_regno_nregs[i][rld[r].mode];
6133 int k;
6135 for (k = 1; k < nr; k++)
6136 if (reg_reloaded_contents[i + k] != regno
6137 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6138 break;
6140 if (k == nr)
6142 int i1;
6143 int bad_for_class;
6145 last_reg = (GET_MODE (last_reg) == mode
6146 ? last_reg : gen_rtx_REG (mode, i));
6148 bad_for_class = 0;
6149 for (k = 0; k < nr; k++)
6150 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6151 i+k);
6153 /* We found a register that contains the
6154 value we need. If this register is the
6155 same as an `earlyclobber' operand of the
6156 current insn, just mark it as a place to
6157 reload from since we can't use it as the
6158 reload register itself. */
6160 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6161 if (reg_overlap_mentioned_for_reload_p
6162 (reg_last_reload_reg[regno],
6163 reload_earlyclobbers[i1]))
6164 break;
6166 if (i1 != n_earlyclobbers
6167 || ! (free_for_value_p (i, rld[r].mode,
6168 rld[r].opnum,
6169 rld[r].when_needed, rld[r].in,
6170 rld[r].out, r, 1))
6171 /* Don't use it if we'd clobber a pseudo reg. */
6172 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6173 && rld[r].out
6174 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6175 /* Don't clobber the frame pointer. */
6176 || (i == HARD_FRAME_POINTER_REGNUM
6177 && frame_pointer_needed
6178 && rld[r].out)
6179 /* Don't really use the inherited spill reg
6180 if we need it wider than we've got it. */
6181 || (GET_MODE_SIZE (rld[r].mode)
6182 > GET_MODE_SIZE (mode))
6183 || bad_for_class
6185 /* If find_reloads chose reload_out as reload
6186 register, stay with it - that leaves the
6187 inherited register for subsequent reloads. */
6188 || (rld[r].out && rld[r].reg_rtx
6189 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6191 if (! rld[r].optional)
6193 reload_override_in[r] = last_reg;
6194 reload_inheritance_insn[r]
6195 = reg_reloaded_insn[i];
6198 else
6200 int k;
6201 /* We can use this as a reload reg. */
6202 /* Mark the register as in use for this part of
6203 the insn. */
6204 mark_reload_reg_in_use (i,
6205 rld[r].opnum,
6206 rld[r].when_needed,
6207 rld[r].mode);
6208 rld[r].reg_rtx = last_reg;
6209 reload_inherited[r] = 1;
6210 reload_inheritance_insn[r]
6211 = reg_reloaded_insn[i];
6212 reload_spill_index[r] = i;
6213 for (k = 0; k < nr; k++)
6214 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6215 i + k);
6222 /* Here's another way to see if the value is already lying around. */
6223 if (inheritance
6224 && rld[r].in != 0
6225 && ! reload_inherited[r]
6226 && rld[r].out == 0
6227 && (CONSTANT_P (rld[r].in)
6228 || GET_CODE (rld[r].in) == PLUS
6229 || REG_P (rld[r].in)
6230 || MEM_P (rld[r].in))
6231 && (rld[r].nregs == max_group_size
6232 || ! reg_classes_intersect_p (rld[r].class, group_class)))
6233 search_equiv = rld[r].in;
6234 /* If this is an output reload from a simple move insn, look
6235 if an equivalence for the input is available. */
6236 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6238 rtx set = single_set (insn);
6240 if (set
6241 && rtx_equal_p (rld[r].out, SET_DEST (set))
6242 && CONSTANT_P (SET_SRC (set)))
6243 search_equiv = SET_SRC (set);
6246 if (search_equiv)
6248 rtx equiv
6249 = find_equiv_reg (search_equiv, insn, rld[r].class,
6250 -1, NULL, 0, rld[r].mode);
6251 int regno = 0;
6253 if (equiv != 0)
6255 if (REG_P (equiv))
6256 regno = REGNO (equiv);
6257 else
6259 /* This must be a SUBREG of a hard register.
6260 Make a new REG since this might be used in an
6261 address and not all machines support SUBREGs
6262 there. */
6263 gcc_assert (GET_CODE (equiv) == SUBREG);
6264 regno = subreg_regno (equiv);
6265 equiv = gen_rtx_REG (rld[r].mode, regno);
6266 /* If we choose EQUIV as the reload register, but the
6267 loop below decides to cancel the inheritance, we'll
6268 end up reloading EQUIV in rld[r].mode, not the mode
6269 it had originally. That isn't safe when EQUIV isn't
6270 available as a spill register since its value might
6271 still be live at this point. */
6272 for (i = regno; i < regno + (int) rld[r].nregs; i++)
6273 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
6274 equiv = 0;
6278 /* If we found a spill reg, reject it unless it is free
6279 and of the desired class. */
6280 if (equiv != 0)
6282 int regs_used = 0;
6283 int bad_for_class = 0;
6284 int max_regno = regno + rld[r].nregs;
6286 for (i = regno; i < max_regno; i++)
6288 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
6290 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6294 if ((regs_used
6295 && ! free_for_value_p (regno, rld[r].mode,
6296 rld[r].opnum, rld[r].when_needed,
6297 rld[r].in, rld[r].out, r, 1))
6298 || bad_for_class)
6299 equiv = 0;
6302 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6303 equiv = 0;
6305 /* We found a register that contains the value we need.
6306 If this register is the same as an `earlyclobber' operand
6307 of the current insn, just mark it as a place to reload from
6308 since we can't use it as the reload register itself. */
6310 if (equiv != 0)
6311 for (i = 0; i < n_earlyclobbers; i++)
6312 if (reg_overlap_mentioned_for_reload_p (equiv,
6313 reload_earlyclobbers[i]))
6315 if (! rld[r].optional)
6316 reload_override_in[r] = equiv;
6317 equiv = 0;
6318 break;
6321 /* If the equiv register we have found is explicitly clobbered
6322 in the current insn, it depends on the reload type if we
6323 can use it, use it for reload_override_in, or not at all.
6324 In particular, we then can't use EQUIV for a
6325 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6327 if (equiv != 0)
6329 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
6330 switch (rld[r].when_needed)
6332 case RELOAD_FOR_OTHER_ADDRESS:
6333 case RELOAD_FOR_INPADDR_ADDRESS:
6334 case RELOAD_FOR_INPUT_ADDRESS:
6335 case RELOAD_FOR_OPADDR_ADDR:
6336 break;
6337 case RELOAD_OTHER:
6338 case RELOAD_FOR_INPUT:
6339 case RELOAD_FOR_OPERAND_ADDRESS:
6340 if (! rld[r].optional)
6341 reload_override_in[r] = equiv;
6342 /* Fall through. */
6343 default:
6344 equiv = 0;
6345 break;
6347 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
6348 switch (rld[r].when_needed)
6350 case RELOAD_FOR_OTHER_ADDRESS:
6351 case RELOAD_FOR_INPADDR_ADDRESS:
6352 case RELOAD_FOR_INPUT_ADDRESS:
6353 case RELOAD_FOR_OPADDR_ADDR:
6354 case RELOAD_FOR_OPERAND_ADDRESS:
6355 case RELOAD_FOR_INPUT:
6356 break;
6357 case RELOAD_OTHER:
6358 if (! rld[r].optional)
6359 reload_override_in[r] = equiv;
6360 /* Fall through. */
6361 default:
6362 equiv = 0;
6363 break;
6367 /* If we found an equivalent reg, say no code need be generated
6368 to load it, and use it as our reload reg. */
6369 if (equiv != 0
6370 && (regno != HARD_FRAME_POINTER_REGNUM
6371 || !frame_pointer_needed))
6373 int nr = hard_regno_nregs[regno][rld[r].mode];
6374 int k;
6375 rld[r].reg_rtx = equiv;
6376 reload_inherited[r] = 1;
6378 /* If reg_reloaded_valid is not set for this register,
6379 there might be a stale spill_reg_store lying around.
6380 We must clear it, since otherwise emit_reload_insns
6381 might delete the store. */
6382 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6383 spill_reg_store[regno] = NULL_RTX;
6384 /* If any of the hard registers in EQUIV are spill
6385 registers, mark them as in use for this insn. */
6386 for (k = 0; k < nr; k++)
6388 i = spill_reg_order[regno + k];
6389 if (i >= 0)
6391 mark_reload_reg_in_use (regno, rld[r].opnum,
6392 rld[r].when_needed,
6393 rld[r].mode);
6394 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6395 regno + k);
6401 /* If we found a register to use already, or if this is an optional
6402 reload, we are done. */
6403 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6404 continue;
6406 #if 0
6407 /* No longer needed for correct operation. Might or might
6408 not give better code on the average. Want to experiment? */
6410 /* See if there is a later reload that has a class different from our
6411 class that intersects our class or that requires less register
6412 than our reload. If so, we must allocate a register to this
6413 reload now, since that reload might inherit a previous reload
6414 and take the only available register in our class. Don't do this
6415 for optional reloads since they will force all previous reloads
6416 to be allocated. Also don't do this for reloads that have been
6417 turned off. */
6419 for (i = j + 1; i < n_reloads; i++)
6421 int s = reload_order[i];
6423 if ((rld[s].in == 0 && rld[s].out == 0
6424 && ! rld[s].secondary_p)
6425 || rld[s].optional)
6426 continue;
6428 if ((rld[s].class != rld[r].class
6429 && reg_classes_intersect_p (rld[r].class,
6430 rld[s].class))
6431 || rld[s].nregs < rld[r].nregs)
6432 break;
6435 if (i == n_reloads)
6436 continue;
6438 allocate_reload_reg (chain, r, j == n_reloads - 1);
6439 #endif
6442 /* Now allocate reload registers for anything non-optional that
6443 didn't get one yet. */
6444 for (j = 0; j < n_reloads; j++)
6446 int r = reload_order[j];
6448 /* Ignore reloads that got marked inoperative. */
6449 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6450 continue;
6452 /* Skip reloads that already have a register allocated or are
6453 optional. */
6454 if (rld[r].reg_rtx != 0 || rld[r].optional)
6455 continue;
6457 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6458 break;
6461 /* If that loop got all the way, we have won. */
6462 if (j == n_reloads)
6464 win = 1;
6465 break;
6468 /* Loop around and try without any inheritance. */
6471 if (! win)
6473 /* First undo everything done by the failed attempt
6474 to allocate with inheritance. */
6475 choose_reload_regs_init (chain, save_reload_reg_rtx);
6477 /* Some sanity tests to verify that the reloads found in the first
6478 pass are identical to the ones we have now. */
6479 gcc_assert (chain->n_reloads == n_reloads);
6481 for (i = 0; i < n_reloads; i++)
6483 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6484 continue;
6485 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6486 for (j = 0; j < n_spills; j++)
6487 if (spill_regs[j] == chain->rld[i].regno)
6488 if (! set_reload_reg (j, i))
6489 failed_reload (chain->insn, i);
6493 /* If we thought we could inherit a reload, because it seemed that
6494 nothing else wanted the same reload register earlier in the insn,
6495 verify that assumption, now that all reloads have been assigned.
6496 Likewise for reloads where reload_override_in has been set. */
6498 /* If doing expensive optimizations, do one preliminary pass that doesn't
6499 cancel any inheritance, but removes reloads that have been needed only
6500 for reloads that we know can be inherited. */
6501 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6503 for (j = 0; j < n_reloads; j++)
6505 int r = reload_order[j];
6506 rtx check_reg;
6507 if (reload_inherited[r] && rld[r].reg_rtx)
6508 check_reg = rld[r].reg_rtx;
6509 else if (reload_override_in[r]
6510 && (REG_P (reload_override_in[r])
6511 || GET_CODE (reload_override_in[r]) == SUBREG))
6512 check_reg = reload_override_in[r];
6513 else
6514 continue;
6515 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6516 rld[r].opnum, rld[r].when_needed, rld[r].in,
6517 (reload_inherited[r]
6518 ? rld[r].out : const0_rtx),
6519 r, 1))
6521 if (pass)
6522 continue;
6523 reload_inherited[r] = 0;
6524 reload_override_in[r] = 0;
6526 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6527 reload_override_in, then we do not need its related
6528 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6529 likewise for other reload types.
6530 We handle this by removing a reload when its only replacement
6531 is mentioned in reload_in of the reload we are going to inherit.
6532 A special case are auto_inc expressions; even if the input is
6533 inherited, we still need the address for the output. We can
6534 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6535 If we succeeded removing some reload and we are doing a preliminary
6536 pass just to remove such reloads, make another pass, since the
6537 removal of one reload might allow us to inherit another one. */
6538 else if (rld[r].in
6539 && rld[r].out != rld[r].in
6540 && remove_address_replacements (rld[r].in) && pass)
6541 pass = 2;
6545 /* Now that reload_override_in is known valid,
6546 actually override reload_in. */
6547 for (j = 0; j < n_reloads; j++)
6548 if (reload_override_in[j])
6549 rld[j].in = reload_override_in[j];
6551 /* If this reload won't be done because it has been canceled or is
6552 optional and not inherited, clear reload_reg_rtx so other
6553 routines (such as subst_reloads) don't get confused. */
6554 for (j = 0; j < n_reloads; j++)
6555 if (rld[j].reg_rtx != 0
6556 && ((rld[j].optional && ! reload_inherited[j])
6557 || (rld[j].in == 0 && rld[j].out == 0
6558 && ! rld[j].secondary_p)))
6560 int regno = true_regnum (rld[j].reg_rtx);
6562 if (spill_reg_order[regno] >= 0)
6563 clear_reload_reg_in_use (regno, rld[j].opnum,
6564 rld[j].when_needed, rld[j].mode);
6565 rld[j].reg_rtx = 0;
6566 reload_spill_index[j] = -1;
6569 /* Record which pseudos and which spill regs have output reloads. */
6570 for (j = 0; j < n_reloads; j++)
6572 int r = reload_order[j];
6574 i = reload_spill_index[r];
6576 /* I is nonneg if this reload uses a register.
6577 If rld[r].reg_rtx is 0, this is an optional reload
6578 that we opted to ignore. */
6579 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
6580 && rld[r].reg_rtx != 0)
6582 int nregno = REGNO (rld[r].out_reg);
6583 int nr = 1;
6585 if (nregno < FIRST_PSEUDO_REGISTER)
6586 nr = hard_regno_nregs[nregno][rld[r].mode];
6588 while (--nr >= 0)
6589 SET_REGNO_REG_SET (&reg_has_output_reload,
6590 nregno + nr);
6592 if (i >= 0)
6594 nr = hard_regno_nregs[i][rld[r].mode];
6595 while (--nr >= 0)
6596 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6599 gcc_assert (rld[r].when_needed == RELOAD_OTHER
6600 || rld[r].when_needed == RELOAD_FOR_OUTPUT
6601 || rld[r].when_needed == RELOAD_FOR_INSN);
6606 /* Deallocate the reload register for reload R. This is called from
6607 remove_address_replacements. */
6609 void
6610 deallocate_reload_reg (int r)
6612 int regno;
6614 if (! rld[r].reg_rtx)
6615 return;
6616 regno = true_regnum (rld[r].reg_rtx);
6617 rld[r].reg_rtx = 0;
6618 if (spill_reg_order[regno] >= 0)
6619 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6620 rld[r].mode);
6621 reload_spill_index[r] = -1;
6624 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
6625 reloads of the same item for fear that we might not have enough reload
6626 registers. However, normally they will get the same reload register
6627 and hence actually need not be loaded twice.
6629 Here we check for the most common case of this phenomenon: when we have
6630 a number of reloads for the same object, each of which were allocated
6631 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6632 reload, and is not modified in the insn itself. If we find such,
6633 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6634 This will not increase the number of spill registers needed and will
6635 prevent redundant code. */
6637 static void
6638 merge_assigned_reloads (rtx insn)
6640 int i, j;
6642 /* Scan all the reloads looking for ones that only load values and
6643 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6644 assigned and not modified by INSN. */
6646 for (i = 0; i < n_reloads; i++)
6648 int conflicting_input = 0;
6649 int max_input_address_opnum = -1;
6650 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6652 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6653 || rld[i].out != 0 || rld[i].reg_rtx == 0
6654 || reg_set_p (rld[i].reg_rtx, insn))
6655 continue;
6657 /* Look at all other reloads. Ensure that the only use of this
6658 reload_reg_rtx is in a reload that just loads the same value
6659 as we do. Note that any secondary reloads must be of the identical
6660 class since the values, modes, and result registers are the
6661 same, so we need not do anything with any secondary reloads. */
6663 for (j = 0; j < n_reloads; j++)
6665 if (i == j || rld[j].reg_rtx == 0
6666 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6667 rld[i].reg_rtx))
6668 continue;
6670 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6671 && rld[j].opnum > max_input_address_opnum)
6672 max_input_address_opnum = rld[j].opnum;
6674 /* If the reload regs aren't exactly the same (e.g, different modes)
6675 or if the values are different, we can't merge this reload.
6676 But if it is an input reload, we might still merge
6677 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6679 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6680 || rld[j].out != 0 || rld[j].in == 0
6681 || ! rtx_equal_p (rld[i].in, rld[j].in))
6683 if (rld[j].when_needed != RELOAD_FOR_INPUT
6684 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6685 || rld[i].opnum > rld[j].opnum)
6686 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6687 break;
6688 conflicting_input = 1;
6689 if (min_conflicting_input_opnum > rld[j].opnum)
6690 min_conflicting_input_opnum = rld[j].opnum;
6694 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6695 we, in fact, found any matching reloads. */
6697 if (j == n_reloads
6698 && max_input_address_opnum <= min_conflicting_input_opnum)
6700 gcc_assert (rld[i].when_needed != RELOAD_FOR_OUTPUT);
6702 for (j = 0; j < n_reloads; j++)
6703 if (i != j && rld[j].reg_rtx != 0
6704 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6705 && (! conflicting_input
6706 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6707 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6709 rld[i].when_needed = RELOAD_OTHER;
6710 rld[j].in = 0;
6711 reload_spill_index[j] = -1;
6712 transfer_replacements (i, j);
6715 /* If this is now RELOAD_OTHER, look for any reloads that
6716 load parts of this operand and set them to
6717 RELOAD_FOR_OTHER_ADDRESS if they were for inputs,
6718 RELOAD_OTHER for outputs. Note that this test is
6719 equivalent to looking for reloads for this operand
6720 number.
6722 We must take special care with RELOAD_FOR_OUTPUT_ADDRESS;
6723 it may share registers with a RELOAD_FOR_INPUT, so we can
6724 not change it to RELOAD_FOR_OTHER_ADDRESS. We should
6725 never need to, since we do not modify RELOAD_FOR_OUTPUT.
6727 It is possible that the RELOAD_FOR_OPERAND_ADDRESS
6728 instruction is assigned the same register as the earlier
6729 RELOAD_FOR_OTHER_ADDRESS instruction. Merging these two
6730 instructions will cause the RELOAD_FOR_OTHER_ADDRESS
6731 instruction to be deleted later on. */
6733 if (rld[i].when_needed == RELOAD_OTHER)
6734 for (j = 0; j < n_reloads; j++)
6735 if (rld[j].in != 0
6736 && rld[j].when_needed != RELOAD_OTHER
6737 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6738 && rld[j].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
6739 && rld[j].when_needed != RELOAD_FOR_OPERAND_ADDRESS
6740 && (! conflicting_input
6741 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6742 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6743 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6744 rld[i].in))
6746 int k;
6748 rld[j].when_needed
6749 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6750 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6751 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6753 /* Check to see if we accidentally converted two
6754 reloads that use the same reload register with
6755 different inputs to the same type. If so, the
6756 resulting code won't work. */
6757 if (rld[j].reg_rtx)
6758 for (k = 0; k < j; k++)
6759 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6760 || rld[k].when_needed != rld[j].when_needed
6761 || !rtx_equal_p (rld[k].reg_rtx,
6762 rld[j].reg_rtx)
6763 || rtx_equal_p (rld[k].in,
6764 rld[j].in));
6770 /* These arrays are filled by emit_reload_insns and its subroutines. */
6771 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6772 static rtx other_input_address_reload_insns = 0;
6773 static rtx other_input_reload_insns = 0;
6774 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6775 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6776 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6777 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6778 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6779 static rtx operand_reload_insns = 0;
6780 static rtx other_operand_reload_insns = 0;
6781 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6783 /* Values to be put in spill_reg_store are put here first. */
6784 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6785 static HARD_REG_SET reg_reloaded_died;
6787 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
6788 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
6789 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
6790 adjusted register, and return true. Otherwise, return false. */
6791 static bool
6792 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
6793 enum reg_class new_class,
6794 enum machine_mode new_mode)
6797 rtx reg;
6799 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
6801 unsigned regno = REGNO (reg);
6803 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
6804 continue;
6805 if (GET_MODE (reg) != new_mode)
6807 if (!HARD_REGNO_MODE_OK (regno, new_mode))
6808 continue;
6809 if (hard_regno_nregs[regno][new_mode]
6810 > hard_regno_nregs[regno][GET_MODE (reg)])
6811 continue;
6812 reg = reload_adjust_reg_for_mode (reg, new_mode);
6814 *reload_reg = reg;
6815 return true;
6817 return false;
6820 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
6821 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
6822 nonzero, if that is suitable. On success, change *RELOAD_REG to the
6823 adjusted register, and return true. Otherwise, return false. */
6824 static bool
6825 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
6826 enum insn_code icode)
6829 enum reg_class new_class = scratch_reload_class (icode);
6830 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
6832 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
6833 new_class, new_mode);
6836 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6837 has the number J. OLD contains the value to be used as input. */
6839 static void
6840 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6841 rtx old, int j)
6843 rtx insn = chain->insn;
6844 rtx reloadreg;
6845 rtx oldequiv_reg = 0;
6846 rtx oldequiv = 0;
6847 int special = 0;
6848 enum machine_mode mode;
6849 rtx *where;
6851 /* delete_output_reload is only invoked properly if old contains
6852 the original pseudo register. Since this is replaced with a
6853 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6854 find the pseudo in RELOAD_IN_REG. */
6855 if (reload_override_in[j]
6856 && REG_P (rl->in_reg))
6858 oldequiv = old;
6859 old = rl->in_reg;
6861 if (oldequiv == 0)
6862 oldequiv = old;
6863 else if (REG_P (oldequiv))
6864 oldequiv_reg = oldequiv;
6865 else if (GET_CODE (oldequiv) == SUBREG)
6866 oldequiv_reg = SUBREG_REG (oldequiv);
6868 reloadreg = reload_reg_rtx_for_input[j];
6869 mode = GET_MODE (reloadreg);
6871 /* If we are reloading from a register that was recently stored in
6872 with an output-reload, see if we can prove there was
6873 actually no need to store the old value in it. */
6875 if (optimize && REG_P (oldequiv)
6876 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6877 && spill_reg_store[REGNO (oldequiv)]
6878 && REG_P (old)
6879 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6880 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6881 rl->out_reg)))
6882 delete_output_reload (insn, j, REGNO (oldequiv), reloadreg);
6884 /* Encapsulate OLDEQUIV into the reload mode, then load RELOADREG from
6885 OLDEQUIV. */
6887 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6888 oldequiv = SUBREG_REG (oldequiv);
6889 if (GET_MODE (oldequiv) != VOIDmode
6890 && mode != GET_MODE (oldequiv))
6891 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6893 /* Switch to the right place to emit the reload insns. */
6894 switch (rl->when_needed)
6896 case RELOAD_OTHER:
6897 where = &other_input_reload_insns;
6898 break;
6899 case RELOAD_FOR_INPUT:
6900 where = &input_reload_insns[rl->opnum];
6901 break;
6902 case RELOAD_FOR_INPUT_ADDRESS:
6903 where = &input_address_reload_insns[rl->opnum];
6904 break;
6905 case RELOAD_FOR_INPADDR_ADDRESS:
6906 where = &inpaddr_address_reload_insns[rl->opnum];
6907 break;
6908 case RELOAD_FOR_OUTPUT_ADDRESS:
6909 where = &output_address_reload_insns[rl->opnum];
6910 break;
6911 case RELOAD_FOR_OUTADDR_ADDRESS:
6912 where = &outaddr_address_reload_insns[rl->opnum];
6913 break;
6914 case RELOAD_FOR_OPERAND_ADDRESS:
6915 where = &operand_reload_insns;
6916 break;
6917 case RELOAD_FOR_OPADDR_ADDR:
6918 where = &other_operand_reload_insns;
6919 break;
6920 case RELOAD_FOR_OTHER_ADDRESS:
6921 where = &other_input_address_reload_insns;
6922 break;
6923 default:
6924 gcc_unreachable ();
6927 push_to_sequence (*where);
6929 /* Auto-increment addresses must be reloaded in a special way. */
6930 if (rl->out && ! rl->out_reg)
6932 /* We are not going to bother supporting the case where a
6933 incremented register can't be copied directly from
6934 OLDEQUIV since this seems highly unlikely. */
6935 gcc_assert (rl->secondary_in_reload < 0);
6937 if (reload_inherited[j])
6938 oldequiv = reloadreg;
6940 old = XEXP (rl->in_reg, 0);
6942 if (optimize && REG_P (oldequiv)
6943 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6944 && spill_reg_store[REGNO (oldequiv)]
6945 && REG_P (old)
6946 && (dead_or_set_p (insn,
6947 spill_reg_stored_to[REGNO (oldequiv)])
6948 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6949 old)))
6950 delete_output_reload (insn, j, REGNO (oldequiv), reloadreg);
6952 /* Prevent normal processing of this reload. */
6953 special = 1;
6954 /* Output a special code sequence for this case. */
6955 new_spill_reg_store[REGNO (reloadreg)]
6956 = inc_for_reload (reloadreg, oldequiv, rl->out,
6957 rl->inc);
6960 /* If we are reloading a pseudo-register that was set by the previous
6961 insn, see if we can get rid of that pseudo-register entirely
6962 by redirecting the previous insn into our reload register. */
6964 else if (optimize && REG_P (old)
6965 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6966 && dead_or_set_p (insn, old)
6967 /* This is unsafe if some other reload
6968 uses the same reg first. */
6969 && ! conflicts_with_override (reloadreg)
6970 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6971 rl->when_needed, old, rl->out, j, 0))
6973 rtx temp = PREV_INSN (insn);
6974 while (temp && NOTE_P (temp))
6975 temp = PREV_INSN (temp);
6976 if (temp
6977 && NONJUMP_INSN_P (temp)
6978 && GET_CODE (PATTERN (temp)) == SET
6979 && SET_DEST (PATTERN (temp)) == old
6980 /* Make sure we can access insn_operand_constraint. */
6981 && asm_noperands (PATTERN (temp)) < 0
6982 /* This is unsafe if operand occurs more than once in current
6983 insn. Perhaps some occurrences aren't reloaded. */
6984 && count_occurrences (PATTERN (insn), old, 0) == 1)
6986 rtx old = SET_DEST (PATTERN (temp));
6987 /* Store into the reload register instead of the pseudo. */
6988 SET_DEST (PATTERN (temp)) = reloadreg;
6990 /* Verify that resulting insn is valid. */
6991 extract_insn (temp);
6992 if (constrain_operands (1))
6994 /* If the previous insn is an output reload, the source is
6995 a reload register, and its spill_reg_store entry will
6996 contain the previous destination. This is now
6997 invalid. */
6998 if (REG_P (SET_SRC (PATTERN (temp)))
6999 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
7001 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7002 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7005 /* If these are the only uses of the pseudo reg,
7006 pretend for GDB it lives in the reload reg we used. */
7007 if (REG_N_DEATHS (REGNO (old)) == 1
7008 && REG_N_SETS (REGNO (old)) == 1)
7010 reg_renumber[REGNO (old)] = REGNO (reloadreg);
7011 if (flag_ira)
7012 /* Inform IRA about the change. */
7013 mark_allocation_change (REGNO (old));
7014 alter_reg (REGNO (old), -1, false);
7016 special = 1;
7018 else
7020 SET_DEST (PATTERN (temp)) = old;
7025 /* We can't do that, so output an insn to load RELOADREG. */
7027 /* If we have a secondary reload, pick up the secondary register
7028 and icode, if any. If OLDEQUIV and OLD are different or
7029 if this is an in-out reload, recompute whether or not we
7030 still need a secondary register and what the icode should
7031 be. If we still need a secondary register and the class or
7032 icode is different, go back to reloading from OLD if using
7033 OLDEQUIV means that we got the wrong type of register. We
7034 cannot have different class or icode due to an in-out reload
7035 because we don't make such reloads when both the input and
7036 output need secondary reload registers. */
7038 if (! special && rl->secondary_in_reload >= 0)
7040 rtx second_reload_reg = 0;
7041 rtx third_reload_reg = 0;
7042 int secondary_reload = rl->secondary_in_reload;
7043 rtx real_oldequiv = oldequiv;
7044 rtx real_old = old;
7045 rtx tmp;
7046 enum insn_code icode;
7047 enum insn_code tertiary_icode = CODE_FOR_nothing;
7049 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
7050 and similarly for OLD.
7051 See comments in get_secondary_reload in reload.c. */
7052 /* If it is a pseudo that cannot be replaced with its
7053 equivalent MEM, we must fall back to reload_in, which
7054 will have all the necessary substitutions registered.
7055 Likewise for a pseudo that can't be replaced with its
7056 equivalent constant.
7058 Take extra care for subregs of such pseudos. Note that
7059 we cannot use reg_equiv_mem in this case because it is
7060 not in the right mode. */
7062 tmp = oldequiv;
7063 if (GET_CODE (tmp) == SUBREG)
7064 tmp = SUBREG_REG (tmp);
7065 if (REG_P (tmp)
7066 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7067 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
7068 || reg_equiv_constant[REGNO (tmp)] != 0))
7070 if (! reg_equiv_mem[REGNO (tmp)]
7071 || num_not_at_initial_offset
7072 || GET_CODE (oldequiv) == SUBREG)
7073 real_oldequiv = rl->in;
7074 else
7075 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
7078 tmp = old;
7079 if (GET_CODE (tmp) == SUBREG)
7080 tmp = SUBREG_REG (tmp);
7081 if (REG_P (tmp)
7082 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7083 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
7084 || reg_equiv_constant[REGNO (tmp)] != 0))
7086 if (! reg_equiv_mem[REGNO (tmp)]
7087 || num_not_at_initial_offset
7088 || GET_CODE (old) == SUBREG)
7089 real_old = rl->in;
7090 else
7091 real_old = reg_equiv_mem[REGNO (tmp)];
7094 second_reload_reg = rld[secondary_reload].reg_rtx;
7095 if (rld[secondary_reload].secondary_in_reload >= 0)
7097 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
7099 third_reload_reg = rld[tertiary_reload].reg_rtx;
7100 tertiary_icode = rld[secondary_reload].secondary_in_icode;
7101 /* We'd have to add more code for quartary reloads. */
7102 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
7104 icode = rl->secondary_in_icode;
7106 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
7107 || (rl->in != 0 && rl->out != 0))
7109 secondary_reload_info sri, sri2;
7110 enum reg_class new_class, new_t_class;
7112 sri.icode = CODE_FOR_nothing;
7113 sri.prev_sri = NULL;
7114 new_class = targetm.secondary_reload (1, real_oldequiv, rl->class,
7115 mode, &sri);
7117 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
7118 second_reload_reg = 0;
7119 else if (new_class == NO_REGS)
7121 if (reload_adjust_reg_for_icode (&second_reload_reg,
7122 third_reload_reg, sri.icode))
7123 icode = sri.icode, third_reload_reg = 0;
7124 else
7125 oldequiv = old, real_oldequiv = real_old;
7127 else if (sri.icode != CODE_FOR_nothing)
7128 /* We currently lack a way to express this in reloads. */
7129 gcc_unreachable ();
7130 else
7132 sri2.icode = CODE_FOR_nothing;
7133 sri2.prev_sri = &sri;
7134 new_t_class = targetm.secondary_reload (1, real_oldequiv,
7135 new_class, mode, &sri);
7136 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
7138 if (reload_adjust_reg_for_temp (&second_reload_reg,
7139 third_reload_reg,
7140 new_class, mode))
7141 third_reload_reg = 0, tertiary_icode = sri2.icode;
7142 else
7143 oldequiv = old, real_oldequiv = real_old;
7145 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
7147 rtx intermediate = second_reload_reg;
7149 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7150 new_class, mode)
7151 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
7152 sri2.icode))
7154 second_reload_reg = intermediate;
7155 tertiary_icode = sri2.icode;
7157 else
7158 oldequiv = old, real_oldequiv = real_old;
7160 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
7162 rtx intermediate = second_reload_reg;
7164 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7165 new_class, mode)
7166 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
7167 new_t_class, mode))
7169 second_reload_reg = intermediate;
7170 tertiary_icode = sri2.icode;
7172 else
7173 oldequiv = old, real_oldequiv = real_old;
7175 else
7176 /* This could be handled more intelligently too. */
7177 oldequiv = old, real_oldequiv = real_old;
7181 /* If we still need a secondary reload register, check
7182 to see if it is being used as a scratch or intermediate
7183 register and generate code appropriately. If we need
7184 a scratch register, use REAL_OLDEQUIV since the form of
7185 the insn may depend on the actual address if it is
7186 a MEM. */
7188 if (second_reload_reg)
7190 if (icode != CODE_FOR_nothing)
7192 /* We'd have to add extra code to handle this case. */
7193 gcc_assert (!third_reload_reg);
7195 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7196 second_reload_reg));
7197 special = 1;
7199 else
7201 /* See if we need a scratch register to load the
7202 intermediate register (a tertiary reload). */
7203 if (tertiary_icode != CODE_FOR_nothing)
7205 emit_insn ((GEN_FCN (tertiary_icode)
7206 (second_reload_reg, real_oldequiv,
7207 third_reload_reg)));
7209 else if (third_reload_reg)
7211 gen_reload (third_reload_reg, real_oldequiv,
7212 rl->opnum,
7213 rl->when_needed);
7214 gen_reload (second_reload_reg, third_reload_reg,
7215 rl->opnum,
7216 rl->when_needed);
7218 else
7219 gen_reload (second_reload_reg, real_oldequiv,
7220 rl->opnum,
7221 rl->when_needed);
7223 oldequiv = second_reload_reg;
7228 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7230 rtx real_oldequiv = oldequiv;
7232 if ((REG_P (oldequiv)
7233 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7234 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
7235 || reg_equiv_constant[REGNO (oldequiv)] != 0))
7236 || (GET_CODE (oldequiv) == SUBREG
7237 && REG_P (SUBREG_REG (oldequiv))
7238 && (REGNO (SUBREG_REG (oldequiv))
7239 >= FIRST_PSEUDO_REGISTER)
7240 && ((reg_equiv_memory_loc
7241 [REGNO (SUBREG_REG (oldequiv))] != 0)
7242 || (reg_equiv_constant
7243 [REGNO (SUBREG_REG (oldequiv))] != 0)))
7244 || (CONSTANT_P (oldequiv)
7245 && (PREFERRED_RELOAD_CLASS (oldequiv,
7246 REGNO_REG_CLASS (REGNO (reloadreg)))
7247 == NO_REGS)))
7248 real_oldequiv = rl->in;
7249 gen_reload (reloadreg, real_oldequiv, rl->opnum,
7250 rl->when_needed);
7253 if (flag_non_call_exceptions)
7254 copy_eh_notes (insn, get_insns ());
7256 /* End this sequence. */
7257 *where = get_insns ();
7258 end_sequence ();
7260 /* Update reload_override_in so that delete_address_reloads_1
7261 can see the actual register usage. */
7262 if (oldequiv_reg)
7263 reload_override_in[j] = oldequiv;
7266 /* Generate insns to for the output reload RL, which is for the insn described
7267 by CHAIN and has the number J. */
7268 static void
7269 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
7270 int j)
7272 rtx reloadreg;
7273 rtx insn = chain->insn;
7274 int special = 0;
7275 rtx old = rl->out;
7276 enum machine_mode mode;
7277 rtx p;
7278 rtx rl_reg_rtx;
7280 if (rl->when_needed == RELOAD_OTHER)
7281 start_sequence ();
7282 else
7283 push_to_sequence (output_reload_insns[rl->opnum]);
7285 rl_reg_rtx = reload_reg_rtx_for_output[j];
7286 mode = GET_MODE (rl_reg_rtx);
7288 reloadreg = rl_reg_rtx;
7290 /* If we need two reload regs, set RELOADREG to the intermediate
7291 one, since it will be stored into OLD. We might need a secondary
7292 register only for an input reload, so check again here. */
7294 if (rl->secondary_out_reload >= 0)
7296 rtx real_old = old;
7297 int secondary_reload = rl->secondary_out_reload;
7298 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
7300 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
7301 && reg_equiv_mem[REGNO (old)] != 0)
7302 real_old = reg_equiv_mem[REGNO (old)];
7304 if (secondary_reload_class (0, rl->class, mode, real_old) != NO_REGS)
7306 rtx second_reloadreg = reloadreg;
7307 reloadreg = rld[secondary_reload].reg_rtx;
7309 /* See if RELOADREG is to be used as a scratch register
7310 or as an intermediate register. */
7311 if (rl->secondary_out_icode != CODE_FOR_nothing)
7313 /* We'd have to add extra code to handle this case. */
7314 gcc_assert (tertiary_reload < 0);
7316 emit_insn ((GEN_FCN (rl->secondary_out_icode)
7317 (real_old, second_reloadreg, reloadreg)));
7318 special = 1;
7320 else
7322 /* See if we need both a scratch and intermediate reload
7323 register. */
7325 enum insn_code tertiary_icode
7326 = rld[secondary_reload].secondary_out_icode;
7328 /* We'd have to add more code for quartary reloads. */
7329 gcc_assert (tertiary_reload < 0
7330 || rld[tertiary_reload].secondary_out_reload < 0);
7332 if (GET_MODE (reloadreg) != mode)
7333 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7335 if (tertiary_icode != CODE_FOR_nothing)
7337 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7338 rtx tem;
7340 /* Copy primary reload reg to secondary reload reg.
7341 (Note that these have been swapped above, then
7342 secondary reload reg to OLD using our insn.) */
7344 /* If REAL_OLD is a paradoxical SUBREG, remove it
7345 and try to put the opposite SUBREG on
7346 RELOADREG. */
7347 if (GET_CODE (real_old) == SUBREG
7348 && (GET_MODE_SIZE (GET_MODE (real_old))
7349 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7350 && 0 != (tem = gen_lowpart_common
7351 (GET_MODE (SUBREG_REG (real_old)),
7352 reloadreg)))
7353 real_old = SUBREG_REG (real_old), reloadreg = tem;
7355 gen_reload (reloadreg, second_reloadreg,
7356 rl->opnum, rl->when_needed);
7357 emit_insn ((GEN_FCN (tertiary_icode)
7358 (real_old, reloadreg, third_reloadreg)));
7359 special = 1;
7362 else
7364 /* Copy between the reload regs here and then to
7365 OUT later. */
7367 gen_reload (reloadreg, second_reloadreg,
7368 rl->opnum, rl->when_needed);
7369 if (tertiary_reload >= 0)
7371 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7373 gen_reload (third_reloadreg, reloadreg,
7374 rl->opnum, rl->when_needed);
7375 reloadreg = third_reloadreg;
7382 /* Output the last reload insn. */
7383 if (! special)
7385 rtx set;
7387 /* Don't output the last reload if OLD is not the dest of
7388 INSN and is in the src and is clobbered by INSN. */
7389 if (! flag_expensive_optimizations
7390 || !REG_P (old)
7391 || !(set = single_set (insn))
7392 || rtx_equal_p (old, SET_DEST (set))
7393 || !reg_mentioned_p (old, SET_SRC (set))
7394 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7395 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7396 gen_reload (old, reloadreg, rl->opnum,
7397 rl->when_needed);
7400 /* Look at all insns we emitted, just to be safe. */
7401 for (p = get_insns (); p; p = NEXT_INSN (p))
7402 if (INSN_P (p))
7404 rtx pat = PATTERN (p);
7406 /* If this output reload doesn't come from a spill reg,
7407 clear any memory of reloaded copies of the pseudo reg.
7408 If this output reload comes from a spill reg,
7409 reg_has_output_reload will make this do nothing. */
7410 note_stores (pat, forget_old_reloads_1, NULL);
7412 if (reg_mentioned_p (rl_reg_rtx, pat))
7414 rtx set = single_set (insn);
7415 if (reload_spill_index[j] < 0
7416 && set
7417 && SET_SRC (set) == rl_reg_rtx)
7419 int src = REGNO (SET_SRC (set));
7421 reload_spill_index[j] = src;
7422 SET_HARD_REG_BIT (reg_is_output_reload, src);
7423 if (find_regno_note (insn, REG_DEAD, src))
7424 SET_HARD_REG_BIT (reg_reloaded_died, src);
7426 if (HARD_REGISTER_P (rl_reg_rtx))
7428 int s = rl->secondary_out_reload;
7429 set = single_set (p);
7430 /* If this reload copies only to the secondary reload
7431 register, the secondary reload does the actual
7432 store. */
7433 if (s >= 0 && set == NULL_RTX)
7434 /* We can't tell what function the secondary reload
7435 has and where the actual store to the pseudo is
7436 made; leave new_spill_reg_store alone. */
7438 else if (s >= 0
7439 && SET_SRC (set) == rl_reg_rtx
7440 && SET_DEST (set) == rld[s].reg_rtx)
7442 /* Usually the next instruction will be the
7443 secondary reload insn; if we can confirm
7444 that it is, setting new_spill_reg_store to
7445 that insn will allow an extra optimization. */
7446 rtx s_reg = rld[s].reg_rtx;
7447 rtx next = NEXT_INSN (p);
7448 rld[s].out = rl->out;
7449 rld[s].out_reg = rl->out_reg;
7450 set = single_set (next);
7451 if (set && SET_SRC (set) == s_reg
7452 && ! new_spill_reg_store[REGNO (s_reg)])
7454 SET_HARD_REG_BIT (reg_is_output_reload,
7455 REGNO (s_reg));
7456 new_spill_reg_store[REGNO (s_reg)] = next;
7459 else
7460 new_spill_reg_store[REGNO (rl_reg_rtx)] = p;
7465 if (rl->when_needed == RELOAD_OTHER)
7467 emit_insn (other_output_reload_insns[rl->opnum]);
7468 other_output_reload_insns[rl->opnum] = get_insns ();
7470 else
7471 output_reload_insns[rl->opnum] = get_insns ();
7473 if (flag_non_call_exceptions)
7474 copy_eh_notes (insn, get_insns ());
7476 end_sequence ();
7479 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7480 and has the number J. */
7481 static void
7482 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7484 rtx insn = chain->insn;
7485 rtx old = (rl->in && MEM_P (rl->in)
7486 ? rl->in_reg : rl->in);
7487 rtx reg_rtx = rl->reg_rtx;
7489 if (old && reg_rtx)
7491 enum machine_mode mode;
7493 /* Determine the mode to reload in.
7494 This is very tricky because we have three to choose from.
7495 There is the mode the insn operand wants (rl->inmode).
7496 There is the mode of the reload register RELOADREG.
7497 There is the intrinsic mode of the operand, which we could find
7498 by stripping some SUBREGs.
7499 It turns out that RELOADREG's mode is irrelevant:
7500 we can change that arbitrarily.
7502 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
7503 then the reload reg may not support QImode moves, so use SImode.
7504 If foo is in memory due to spilling a pseudo reg, this is safe,
7505 because the QImode value is in the least significant part of a
7506 slot big enough for a SImode. If foo is some other sort of
7507 memory reference, then it is impossible to reload this case,
7508 so previous passes had better make sure this never happens.
7510 Then consider a one-word union which has SImode and one of its
7511 members is a float, being fetched as (SUBREG:SF union:SI).
7512 We must fetch that as SFmode because we could be loading into
7513 a float-only register. In this case OLD's mode is correct.
7515 Consider an immediate integer: it has VOIDmode. Here we need
7516 to get a mode from something else.
7518 In some cases, there is a fourth mode, the operand's
7519 containing mode. If the insn specifies a containing mode for
7520 this operand, it overrides all others.
7522 I am not sure whether the algorithm here is always right,
7523 but it does the right things in those cases. */
7525 mode = GET_MODE (old);
7526 if (mode == VOIDmode)
7527 mode = rl->inmode;
7529 /* We cannot use gen_lowpart_common since it can do the wrong thing
7530 when REG_RTX has a multi-word mode. Note that REG_RTX must
7531 always be a REG here. */
7532 if (GET_MODE (reg_rtx) != mode)
7533 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7535 reload_reg_rtx_for_input[j] = reg_rtx;
7537 if (old != 0
7538 /* AUTO_INC reloads need to be handled even if inherited. We got an
7539 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7540 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7541 && ! rtx_equal_p (reg_rtx, old)
7542 && reg_rtx != 0)
7543 emit_input_reload_insns (chain, rld + j, old, j);
7545 /* When inheriting a wider reload, we have a MEM in rl->in,
7546 e.g. inheriting a SImode output reload for
7547 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7548 if (optimize && reload_inherited[j] && rl->in
7549 && MEM_P (rl->in)
7550 && MEM_P (rl->in_reg)
7551 && reload_spill_index[j] >= 0
7552 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7553 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7555 /* If we are reloading a register that was recently stored in with an
7556 output-reload, see if we can prove there was
7557 actually no need to store the old value in it. */
7559 if (optimize
7560 && (reload_inherited[j] || reload_override_in[j])
7561 && reg_rtx
7562 && REG_P (reg_rtx)
7563 && spill_reg_store[REGNO (reg_rtx)] != 0
7564 #if 0
7565 /* There doesn't seem to be any reason to restrict this to pseudos
7566 and doing so loses in the case where we are copying from a
7567 register of the wrong class. */
7568 && !HARD_REGISTER_P (spill_reg_stored_to[REGNO (reg_rtx)])
7569 #endif
7570 /* The insn might have already some references to stackslots
7571 replaced by MEMs, while reload_out_reg still names the
7572 original pseudo. */
7573 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (reg_rtx)])
7574 || rtx_equal_p (spill_reg_stored_to[REGNO (reg_rtx)], rl->out_reg)))
7575 delete_output_reload (insn, j, REGNO (reg_rtx), reg_rtx);
7578 /* Do output reloading for reload RL, which is for the insn described by
7579 CHAIN and has the number J.
7580 ??? At some point we need to support handling output reloads of
7581 JUMP_INSNs or insns that set cc0. */
7582 static void
7583 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7585 rtx note, old;
7586 rtx insn = chain->insn;
7587 /* If this is an output reload that stores something that is
7588 not loaded in this same reload, see if we can eliminate a previous
7589 store. */
7590 rtx pseudo = rl->out_reg;
7591 rtx reg_rtx = rl->reg_rtx;
7593 if (rl->out && reg_rtx)
7595 enum machine_mode mode;
7597 /* Determine the mode to reload in.
7598 See comments above (for input reloading). */
7599 mode = GET_MODE (rl->out);
7600 if (mode == VOIDmode)
7602 /* VOIDmode should never happen for an output. */
7603 if (asm_noperands (PATTERN (insn)) < 0)
7604 /* It's the compiler's fault. */
7605 fatal_insn ("VOIDmode on an output", insn);
7606 error_for_asm (insn, "output operand is constant in %<asm%>");
7607 /* Prevent crash--use something we know is valid. */
7608 mode = word_mode;
7609 rl->out = gen_rtx_REG (mode, REGNO (reg_rtx));
7611 if (GET_MODE (reg_rtx) != mode)
7612 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7614 reload_reg_rtx_for_output[j] = reg_rtx;
7616 if (pseudo
7617 && optimize
7618 && REG_P (pseudo)
7619 && ! rtx_equal_p (rl->in_reg, pseudo)
7620 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7621 && reg_last_reload_reg[REGNO (pseudo)])
7623 int pseudo_no = REGNO (pseudo);
7624 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7626 /* We don't need to test full validity of last_regno for
7627 inherit here; we only want to know if the store actually
7628 matches the pseudo. */
7629 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7630 && reg_reloaded_contents[last_regno] == pseudo_no
7631 && spill_reg_store[last_regno]
7632 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7633 delete_output_reload (insn, j, last_regno, reg_rtx);
7636 old = rl->out_reg;
7637 if (old == 0
7638 || reg_rtx == 0
7639 || rtx_equal_p (old, reg_rtx))
7640 return;
7642 /* An output operand that dies right away does need a reload,
7643 but need not be copied from it. Show the new location in the
7644 REG_UNUSED note. */
7645 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7646 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7648 XEXP (note, 0) = reg_rtx;
7649 return;
7651 /* Likewise for a SUBREG of an operand that dies. */
7652 else if (GET_CODE (old) == SUBREG
7653 && REG_P (SUBREG_REG (old))
7654 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7655 SUBREG_REG (old))))
7657 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old), reg_rtx);
7658 return;
7660 else if (GET_CODE (old) == SCRATCH)
7661 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7662 but we don't want to make an output reload. */
7663 return;
7665 /* If is a JUMP_INSN, we can't support output reloads yet. */
7666 gcc_assert (NONJUMP_INSN_P (insn));
7668 emit_output_reload_insns (chain, rld + j, j);
7671 /* A reload copies values of MODE from register SRC to register DEST.
7672 Return true if it can be treated for inheritance purposes like a
7673 group of reloads, each one reloading a single hard register. The
7674 caller has already checked that (reg:MODE SRC) and (reg:MODE DEST)
7675 occupy the same number of hard registers. */
7677 static bool
7678 inherit_piecemeal_p (int dest ATTRIBUTE_UNUSED,
7679 int src ATTRIBUTE_UNUSED,
7680 enum machine_mode mode ATTRIBUTE_UNUSED)
7682 #ifdef CANNOT_CHANGE_MODE_CLASS
7683 return (!REG_CANNOT_CHANGE_MODE_P (dest, mode, reg_raw_mode[dest])
7684 && !REG_CANNOT_CHANGE_MODE_P (src, mode, reg_raw_mode[src]));
7685 #else
7686 return true;
7687 #endif
7690 /* Output insns to reload values in and out of the chosen reload regs. */
7692 static void
7693 emit_reload_insns (struct insn_chain *chain)
7695 rtx insn = chain->insn;
7697 int j;
7699 CLEAR_HARD_REG_SET (reg_reloaded_died);
7701 for (j = 0; j < reload_n_operands; j++)
7702 input_reload_insns[j] = input_address_reload_insns[j]
7703 = inpaddr_address_reload_insns[j]
7704 = output_reload_insns[j] = output_address_reload_insns[j]
7705 = outaddr_address_reload_insns[j]
7706 = other_output_reload_insns[j] = 0;
7707 other_input_address_reload_insns = 0;
7708 other_input_reload_insns = 0;
7709 operand_reload_insns = 0;
7710 other_operand_reload_insns = 0;
7712 /* Dump reloads into the dump file. */
7713 if (dump_file)
7715 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7716 debug_reload_to_stream (dump_file);
7719 /* Now output the instructions to copy the data into and out of the
7720 reload registers. Do these in the order that the reloads were reported,
7721 since reloads of base and index registers precede reloads of operands
7722 and the operands may need the base and index registers reloaded. */
7724 for (j = 0; j < n_reloads; j++)
7726 if (rld[j].reg_rtx && HARD_REGISTER_P (rld[j].reg_rtx))
7728 unsigned int i;
7730 for (i = REGNO (rld[j].reg_rtx); i < END_REGNO (rld[j].reg_rtx); i++)
7731 new_spill_reg_store[i] = 0;
7734 do_input_reload (chain, rld + j, j);
7735 do_output_reload (chain, rld + j, j);
7738 /* Now write all the insns we made for reloads in the order expected by
7739 the allocation functions. Prior to the insn being reloaded, we write
7740 the following reloads:
7742 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7744 RELOAD_OTHER reloads.
7746 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7747 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7748 RELOAD_FOR_INPUT reload for the operand.
7750 RELOAD_FOR_OPADDR_ADDRS reloads.
7752 RELOAD_FOR_OPERAND_ADDRESS reloads.
7754 After the insn being reloaded, we write the following:
7756 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7757 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7758 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7759 reloads for the operand. The RELOAD_OTHER output reloads are
7760 output in descending order by reload number. */
7762 emit_insn_before (other_input_address_reload_insns, insn);
7763 emit_insn_before (other_input_reload_insns, insn);
7765 for (j = 0; j < reload_n_operands; j++)
7767 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7768 emit_insn_before (input_address_reload_insns[j], insn);
7769 emit_insn_before (input_reload_insns[j], insn);
7772 emit_insn_before (other_operand_reload_insns, insn);
7773 emit_insn_before (operand_reload_insns, insn);
7775 for (j = 0; j < reload_n_operands; j++)
7777 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7778 x = emit_insn_after (output_address_reload_insns[j], x);
7779 x = emit_insn_after (output_reload_insns[j], x);
7780 emit_insn_after (other_output_reload_insns[j], x);
7783 /* For all the spill regs newly reloaded in this instruction,
7784 record what they were reloaded from, so subsequent instructions
7785 can inherit the reloads.
7787 Update spill_reg_store for the reloads of this insn.
7788 Copy the elements that were updated in the loop above. */
7790 for (j = 0; j < n_reloads; j++)
7792 int r = reload_order[j];
7793 int i = reload_spill_index[r];
7795 /* If this is a non-inherited input reload from a pseudo, we must
7796 clear any memory of a previous store to the same pseudo. Only do
7797 something if there will not be an output reload for the pseudo
7798 being reloaded. */
7799 if (rld[r].in_reg != 0
7800 && ! (reload_inherited[r] || reload_override_in[r]))
7802 rtx reg = rld[r].in_reg;
7804 if (GET_CODE (reg) == SUBREG)
7805 reg = SUBREG_REG (reg);
7807 if (REG_P (reg)
7808 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7809 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
7811 int nregno = REGNO (reg);
7813 if (reg_last_reload_reg[nregno])
7815 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7817 if (reg_reloaded_contents[last_regno] == nregno)
7818 spill_reg_store[last_regno] = 0;
7823 /* I is nonneg if this reload used a register.
7824 If rld[r].reg_rtx is 0, this is an optional reload
7825 that we opted to ignore. */
7827 if (i >= 0 && rld[r].reg_rtx != 0)
7829 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7830 int k;
7832 /* For a multi register reload, we need to check if all or part
7833 of the value lives to the end. */
7834 for (k = 0; k < nr; k++)
7835 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7836 rld[r].when_needed))
7837 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7839 /* Maybe the spill reg contains a copy of reload_out. */
7840 if (rld[r].out != 0
7841 && (REG_P (rld[r].out)
7842 #ifdef AUTO_INC_DEC
7843 || ! rld[r].out_reg
7844 #endif
7845 || REG_P (rld[r].out_reg)))
7847 rtx reg;
7848 enum machine_mode mode;
7849 int regno, nregs;
7851 reg = reload_reg_rtx_for_output[r];
7852 mode = GET_MODE (reg);
7853 regno = REGNO (reg);
7854 nregs = hard_regno_nregs[regno][mode];
7855 if (reload_regs_reach_end_p (regno, nregs, rld[r].opnum,
7856 rld[r].when_needed))
7858 rtx out = (REG_P (rld[r].out)
7859 ? rld[r].out
7860 : rld[r].out_reg
7861 ? rld[r].out_reg
7862 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7863 int out_regno = REGNO (out);
7864 int out_nregs = (!HARD_REGISTER_NUM_P (out_regno) ? 1
7865 : hard_regno_nregs[out_regno][mode]);
7866 bool piecemeal;
7868 spill_reg_store[regno] = new_spill_reg_store[regno];
7869 spill_reg_stored_to[regno] = out;
7870 reg_last_reload_reg[out_regno] = reg;
7872 piecemeal = (HARD_REGISTER_NUM_P (out_regno)
7873 && nregs == out_nregs
7874 && inherit_piecemeal_p (out_regno, regno, mode));
7876 /* If OUT_REGNO is a hard register, it may occupy more than
7877 one register. If it does, say what is in the
7878 rest of the registers assuming that both registers
7879 agree on how many words the object takes. If not,
7880 invalidate the subsequent registers. */
7882 if (HARD_REGISTER_NUM_P (out_regno))
7883 for (k = 1; k < out_nregs; k++)
7884 reg_last_reload_reg[out_regno + k]
7885 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
7887 /* Now do the inverse operation. */
7888 for (k = 0; k < nregs; k++)
7890 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
7891 reg_reloaded_contents[regno + k]
7892 = (!HARD_REGISTER_NUM_P (out_regno) || !piecemeal
7893 ? out_regno
7894 : out_regno + k);
7895 reg_reloaded_insn[regno + k] = insn;
7896 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
7897 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
7898 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7899 regno + k);
7900 else
7901 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7902 regno + k);
7906 /* Maybe the spill reg contains a copy of reload_in. Only do
7907 something if there will not be an output reload for
7908 the register being reloaded. */
7909 else if (rld[r].out_reg == 0
7910 && rld[r].in != 0
7911 && ((REG_P (rld[r].in)
7912 && !HARD_REGISTER_P (rld[r].in)
7913 && !REGNO_REG_SET_P (&reg_has_output_reload,
7914 REGNO (rld[r].in)))
7915 || (REG_P (rld[r].in_reg)
7916 && !REGNO_REG_SET_P (&reg_has_output_reload,
7917 REGNO (rld[r].in_reg))))
7918 && !reg_set_p (reload_reg_rtx_for_input[r], PATTERN (insn)))
7920 rtx reg;
7921 enum machine_mode mode;
7922 int regno, nregs;
7924 reg = reload_reg_rtx_for_input[r];
7925 mode = GET_MODE (reg);
7926 regno = REGNO (reg);
7927 nregs = hard_regno_nregs[regno][mode];
7928 if (reload_regs_reach_end_p (regno, nregs, rld[r].opnum,
7929 rld[r].when_needed))
7931 int in_regno;
7932 int in_nregs;
7933 rtx in;
7934 bool piecemeal;
7936 if (REG_P (rld[r].in)
7937 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7938 in = rld[r].in;
7939 else if (REG_P (rld[r].in_reg))
7940 in = rld[r].in_reg;
7941 else
7942 in = XEXP (rld[r].in_reg, 0);
7943 in_regno = REGNO (in);
7945 in_nregs = (!HARD_REGISTER_NUM_P (in_regno) ? 1
7946 : hard_regno_nregs[in_regno][mode]);
7948 reg_last_reload_reg[in_regno] = reg;
7950 piecemeal = (HARD_REGISTER_NUM_P (in_regno)
7951 && nregs == in_nregs
7952 && inherit_piecemeal_p (regno, in_regno, mode));
7954 if (HARD_REGISTER_NUM_P (in_regno))
7955 for (k = 1; k < in_nregs; k++)
7956 reg_last_reload_reg[in_regno + k]
7957 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
7959 /* Unless we inherited this reload, show we haven't
7960 recently done a store.
7961 Previous stores of inherited auto_inc expressions
7962 also have to be discarded. */
7963 if (! reload_inherited[r]
7964 || (rld[r].out && ! rld[r].out_reg))
7965 spill_reg_store[regno] = 0;
7967 for (k = 0; k < nregs; k++)
7969 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
7970 reg_reloaded_contents[regno + k]
7971 = (!HARD_REGISTER_NUM_P (in_regno) || !piecemeal
7972 ? in_regno
7973 : in_regno + k);
7974 reg_reloaded_insn[regno + k] = insn;
7975 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
7976 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
7977 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7978 regno + k);
7979 else
7980 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7981 regno + k);
7987 /* The following if-statement was #if 0'd in 1.34 (or before...).
7988 It's reenabled in 1.35 because supposedly nothing else
7989 deals with this problem. */
7991 /* If a register gets output-reloaded from a non-spill register,
7992 that invalidates any previous reloaded copy of it.
7993 But forget_old_reloads_1 won't get to see it, because
7994 it thinks only about the original insn. So invalidate it here.
7995 Also do the same thing for RELOAD_OTHER constraints where the
7996 output is discarded. */
7997 if (i < 0
7998 && ((rld[r].out != 0
7999 && (REG_P (rld[r].out)
8000 || (MEM_P (rld[r].out)
8001 && REG_P (rld[r].out_reg))))
8002 || (rld[r].out == 0 && rld[r].out_reg
8003 && REG_P (rld[r].out_reg))))
8005 rtx out = ((rld[r].out && REG_P (rld[r].out))
8006 ? rld[r].out : rld[r].out_reg);
8007 int out_regno = REGNO (out);
8008 enum machine_mode mode = GET_MODE (out);
8010 /* REG_RTX is now set or clobbered by the main instruction.
8011 As the comment above explains, forget_old_reloads_1 only
8012 sees the original instruction, and there is no guarantee
8013 that the original instruction also clobbered REG_RTX.
8014 For example, if find_reloads sees that the input side of
8015 a matched operand pair dies in this instruction, it may
8016 use the input register as the reload register.
8018 Calling forget_old_reloads_1 is a waste of effort if
8019 REG_RTX is also the output register.
8021 If we know that REG_RTX holds the value of a pseudo
8022 register, the code after the call will record that fact. */
8023 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
8024 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
8026 if (!HARD_REGISTER_NUM_P (out_regno))
8028 rtx src_reg, store_insn = NULL_RTX;
8030 reg_last_reload_reg[out_regno] = 0;
8032 /* If we can find a hard register that is stored, record
8033 the storing insn so that we may delete this insn with
8034 delete_output_reload. */
8035 src_reg = reload_reg_rtx_for_output[r];
8037 /* If this is an optional reload, try to find the source reg
8038 from an input reload. */
8039 if (! src_reg)
8041 rtx set = single_set (insn);
8042 if (set && SET_DEST (set) == rld[r].out)
8044 int k;
8046 src_reg = SET_SRC (set);
8047 store_insn = insn;
8048 for (k = 0; k < n_reloads; k++)
8050 if (rld[k].in == src_reg)
8052 src_reg = reload_reg_rtx_for_input[k];
8053 break;
8058 else
8059 store_insn = new_spill_reg_store[REGNO (src_reg)];
8060 if (src_reg && REG_P (src_reg)
8061 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
8063 int src_regno, src_nregs, k;
8064 rtx note;
8066 gcc_assert (GET_MODE (src_reg) == mode);
8067 src_regno = REGNO (src_reg);
8068 src_nregs = hard_regno_nregs[src_regno][mode];
8069 /* The place where to find a death note varies with
8070 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
8071 necessarily checked exactly in the code that moves
8072 notes, so just check both locations. */
8073 note = find_regno_note (insn, REG_DEAD, src_regno);
8074 if (! note && store_insn)
8075 note = find_regno_note (store_insn, REG_DEAD, src_regno);
8076 for (k = 0; k < src_nregs; k++)
8078 spill_reg_store[src_regno + k] = store_insn;
8079 spill_reg_stored_to[src_regno + k] = out;
8080 reg_reloaded_contents[src_regno + k] = out_regno;
8081 reg_reloaded_insn[src_regno + k] = store_insn;
8082 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + k);
8083 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + k);
8084 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + k,
8085 mode))
8086 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8087 src_regno + k);
8088 else
8089 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8090 src_regno + k);
8091 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + k);
8092 if (note)
8093 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
8094 else
8095 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
8097 reg_last_reload_reg[out_regno] = src_reg;
8098 /* We have to set reg_has_output_reload here, or else
8099 forget_old_reloads_1 will clear reg_last_reload_reg
8100 right away. */
8101 SET_REGNO_REG_SET (&reg_has_output_reload,
8102 out_regno);
8105 else
8107 int k, out_nregs = hard_regno_nregs[out_regno][mode];
8109 for (k = 0; k < out_nregs; k++)
8110 reg_last_reload_reg[out_regno + k] = 0;
8114 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
8117 /* Go through the motions to emit INSN and test if it is strictly valid.
8118 Return the emitted insn if valid, else return NULL. */
8120 static rtx
8121 emit_insn_if_valid_for_reload (rtx insn)
8123 rtx last = get_last_insn ();
8124 int code;
8126 insn = emit_insn (insn);
8127 code = recog_memoized (insn);
8129 if (code >= 0)
8131 extract_insn (insn);
8132 /* We want constrain operands to treat this insn strictly in its
8133 validity determination, i.e., the way it would after reload has
8134 completed. */
8135 if (constrain_operands (1))
8136 return insn;
8139 delete_insns_since (last);
8140 return NULL;
8143 /* Emit code to perform a reload from IN (which may be a reload register) to
8144 OUT (which may also be a reload register). IN or OUT is from operand
8145 OPNUM with reload type TYPE.
8147 Returns first insn emitted. */
8149 static rtx
8150 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
8152 rtx last = get_last_insn ();
8153 rtx tem;
8155 /* If IN is a paradoxical SUBREG, remove it and try to put the
8156 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
8157 if (GET_CODE (in) == SUBREG
8158 && (GET_MODE_SIZE (GET_MODE (in))
8159 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
8160 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
8161 in = SUBREG_REG (in), out = tem;
8162 else if (GET_CODE (out) == SUBREG
8163 && (GET_MODE_SIZE (GET_MODE (out))
8164 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
8165 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
8166 out = SUBREG_REG (out), in = tem;
8168 /* How to do this reload can get quite tricky. Normally, we are being
8169 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
8170 register that didn't get a hard register. In that case we can just
8171 call emit_move_insn.
8173 We can also be asked to reload a PLUS that adds a register or a MEM to
8174 another register, constant or MEM. This can occur during frame pointer
8175 elimination and while reloading addresses. This case is handled by
8176 trying to emit a single insn to perform the add. If it is not valid,
8177 we use a two insn sequence.
8179 Or we can be asked to reload an unary operand that was a fragment of
8180 an addressing mode, into a register. If it isn't recognized as-is,
8181 we try making the unop operand and the reload-register the same:
8182 (set reg:X (unop:X expr:Y))
8183 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
8185 Finally, we could be called to handle an 'o' constraint by putting
8186 an address into a register. In that case, we first try to do this
8187 with a named pattern of "reload_load_address". If no such pattern
8188 exists, we just emit a SET insn and hope for the best (it will normally
8189 be valid on machines that use 'o').
8191 This entire process is made complex because reload will never
8192 process the insns we generate here and so we must ensure that
8193 they will fit their constraints and also by the fact that parts of
8194 IN might be being reloaded separately and replaced with spill registers.
8195 Because of this, we are, in some sense, just guessing the right approach
8196 here. The one listed above seems to work.
8198 ??? At some point, this whole thing needs to be rethought. */
8200 if (GET_CODE (in) == PLUS
8201 && (REG_P (XEXP (in, 0))
8202 || GET_CODE (XEXP (in, 0)) == SUBREG
8203 || MEM_P (XEXP (in, 0)))
8204 && (REG_P (XEXP (in, 1))
8205 || GET_CODE (XEXP (in, 1)) == SUBREG
8206 || CONSTANT_P (XEXP (in, 1))
8207 || MEM_P (XEXP (in, 1))))
8209 /* We need to compute the sum of a register or a MEM and another
8210 register, constant, or MEM, and put it into the reload
8211 register. The best possible way of doing this is if the machine
8212 has a three-operand ADD insn that accepts the required operands.
8214 The simplest approach is to try to generate such an insn and see if it
8215 is recognized and matches its constraints. If so, it can be used.
8217 It might be better not to actually emit the insn unless it is valid,
8218 but we need to pass the insn as an operand to `recog' and
8219 `extract_insn' and it is simpler to emit and then delete the insn if
8220 not valid than to dummy things up. */
8222 rtx op0, op1, tem, insn;
8223 int code;
8225 op0 = find_replacement (&XEXP (in, 0));
8226 op1 = find_replacement (&XEXP (in, 1));
8228 /* Since constraint checking is strict, commutativity won't be
8229 checked, so we need to do that here to avoid spurious failure
8230 if the add instruction is two-address and the second operand
8231 of the add is the same as the reload reg, which is frequently
8232 the case. If the insn would be A = B + A, rearrange it so
8233 it will be A = A + B as constrain_operands expects. */
8235 if (REG_P (XEXP (in, 1))
8236 && REGNO (out) == REGNO (XEXP (in, 1)))
8237 tem = op0, op0 = op1, op1 = tem;
8239 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
8240 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
8242 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8243 if (insn)
8244 return insn;
8246 /* If that failed, we must use a conservative two-insn sequence.
8248 Use a move to copy one operand into the reload register. Prefer
8249 to reload a constant, MEM or pseudo since the move patterns can
8250 handle an arbitrary operand. If OP1 is not a constant, MEM or
8251 pseudo and OP1 is not a valid operand for an add instruction, then
8252 reload OP1.
8254 After reloading one of the operands into the reload register, add
8255 the reload register to the output register.
8257 If there is another way to do this for a specific machine, a
8258 DEFINE_PEEPHOLE should be specified that recognizes the sequence
8259 we emit below. */
8261 code = (int) optab_handler (add_optab, GET_MODE (out))->insn_code;
8263 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
8264 || (REG_P (op1)
8265 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
8266 || (code != CODE_FOR_nothing
8267 && ! ((*insn_data[code].operand[2].predicate)
8268 (op1, insn_data[code].operand[2].mode))))
8269 tem = op0, op0 = op1, op1 = tem;
8271 gen_reload (out, op0, opnum, type);
8273 /* If OP0 and OP1 are the same, we can use OUT for OP1.
8274 This fixes a problem on the 32K where the stack pointer cannot
8275 be used as an operand of an add insn. */
8277 if (rtx_equal_p (op0, op1))
8278 op1 = out;
8280 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
8281 if (insn)
8283 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
8284 set_unique_reg_note (insn, REG_EQUIV, in);
8285 return insn;
8288 /* If that failed, copy the address register to the reload register.
8289 Then add the constant to the reload register. */
8291 gcc_assert (!reg_overlap_mentioned_p (out, op0));
8292 gen_reload (out, op1, opnum, type);
8293 insn = emit_insn (gen_add2_insn (out, op0));
8294 set_unique_reg_note (insn, REG_EQUIV, in);
8297 #ifdef SECONDARY_MEMORY_NEEDED
8298 /* If we need a memory location to do the move, do it that way. */
8299 else if ((REG_P (in) || GET_CODE (in) == SUBREG)
8300 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
8301 && (REG_P (out) || GET_CODE (out) == SUBREG)
8302 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
8303 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
8304 REGNO_REG_CLASS (reg_or_subregno (out)),
8305 GET_MODE (out)))
8307 /* Get the memory to use and rewrite both registers to its mode. */
8308 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
8310 if (GET_MODE (loc) != GET_MODE (out))
8311 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
8313 if (GET_MODE (loc) != GET_MODE (in))
8314 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
8316 gen_reload (loc, in, opnum, type);
8317 gen_reload (out, loc, opnum, type);
8319 #endif
8320 else if (REG_P (out) && UNARY_P (in))
8322 rtx insn;
8323 rtx op1;
8324 rtx out_moded;
8325 rtx set;
8327 op1 = find_replacement (&XEXP (in, 0));
8328 if (op1 != XEXP (in, 0))
8329 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
8331 /* First, try a plain SET. */
8332 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8333 if (set)
8334 return set;
8336 /* If that failed, move the inner operand to the reload
8337 register, and try the same unop with the inner expression
8338 replaced with the reload register. */
8340 if (GET_MODE (op1) != GET_MODE (out))
8341 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
8342 else
8343 out_moded = out;
8345 gen_reload (out_moded, op1, opnum, type);
8347 insn
8348 = gen_rtx_SET (VOIDmode, out,
8349 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
8350 out_moded));
8351 insn = emit_insn_if_valid_for_reload (insn);
8352 if (insn)
8354 set_unique_reg_note (insn, REG_EQUIV, in);
8355 return insn;
8358 fatal_insn ("Failure trying to reload:", set);
8360 /* If IN is a simple operand, use gen_move_insn. */
8361 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8363 tem = emit_insn (gen_move_insn (out, in));
8364 /* IN may contain a LABEL_REF, if so add a REG_LABEL_OPERAND note. */
8365 mark_jump_label (in, tem, 0);
8368 #ifdef HAVE_reload_load_address
8369 else if (HAVE_reload_load_address)
8370 emit_insn (gen_reload_load_address (out, in));
8371 #endif
8373 /* Otherwise, just write (set OUT IN) and hope for the best. */
8374 else
8375 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8377 /* Return the first insn emitted.
8378 We can not just return get_last_insn, because there may have
8379 been multiple instructions emitted. Also note that gen_move_insn may
8380 emit more than one insn itself, so we can not assume that there is one
8381 insn emitted per emit_insn_before call. */
8383 return last ? NEXT_INSN (last) : get_insns ();
8386 /* Delete a previously made output-reload whose result we now believe
8387 is not needed. First we double-check.
8389 INSN is the insn now being processed.
8390 LAST_RELOAD_REG is the hard register number for which we want to delete
8391 the last output reload.
8392 J is the reload-number that originally used REG. The caller has made
8393 certain that reload J doesn't use REG any longer for input.
8394 NEW_RELOAD_REG is reload register that reload J is using for REG. */
8396 static void
8397 delete_output_reload (rtx insn, int j, int last_reload_reg, rtx new_reload_reg)
8399 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8400 rtx reg = spill_reg_stored_to[last_reload_reg];
8401 int k;
8402 int n_occurrences;
8403 int n_inherited = 0;
8404 rtx i1;
8405 rtx substed;
8407 /* It is possible that this reload has been only used to set another reload
8408 we eliminated earlier and thus deleted this instruction too. */
8409 if (INSN_DELETED_P (output_reload_insn))
8410 return;
8412 /* Get the raw pseudo-register referred to. */
8414 while (GET_CODE (reg) == SUBREG)
8415 reg = SUBREG_REG (reg);
8416 substed = reg_equiv_memory_loc[REGNO (reg)];
8418 /* This is unsafe if the operand occurs more often in the current
8419 insn than it is inherited. */
8420 for (k = n_reloads - 1; k >= 0; k--)
8422 rtx reg2 = rld[k].in;
8423 if (! reg2)
8424 continue;
8425 if (MEM_P (reg2) || reload_override_in[k])
8426 reg2 = rld[k].in_reg;
8427 #ifdef AUTO_INC_DEC
8428 if (rld[k].out && ! rld[k].out_reg)
8429 reg2 = XEXP (rld[k].in_reg, 0);
8430 #endif
8431 while (GET_CODE (reg2) == SUBREG)
8432 reg2 = SUBREG_REG (reg2);
8433 if (rtx_equal_p (reg2, reg))
8435 if (reload_inherited[k] || reload_override_in[k] || k == j)
8436 n_inherited++;
8437 else
8438 return;
8441 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
8442 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
8443 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
8444 reg, 0);
8445 if (substed)
8446 n_occurrences += count_occurrences (PATTERN (insn),
8447 eliminate_regs (substed, 0,
8448 NULL_RTX), 0);
8449 for (i1 = reg_equiv_alt_mem_list[REGNO (reg)]; i1; i1 = XEXP (i1, 1))
8451 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8452 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8454 if (n_occurrences > n_inherited)
8455 return;
8457 /* If the pseudo-reg we are reloading is no longer referenced
8458 anywhere between the store into it and here,
8459 and we're within the same basic block, then the value can only
8460 pass through the reload reg and end up here.
8461 Otherwise, give up--return. */
8462 for (i1 = NEXT_INSN (output_reload_insn);
8463 i1 != insn; i1 = NEXT_INSN (i1))
8465 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8466 return;
8467 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8468 && reg_mentioned_p (reg, PATTERN (i1)))
8470 /* If this is USE in front of INSN, we only have to check that
8471 there are no more references than accounted for by inheritance. */
8472 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8474 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8475 i1 = NEXT_INSN (i1);
8477 if (n_occurrences <= n_inherited && i1 == insn)
8478 break;
8479 return;
8483 /* We will be deleting the insn. Remove the spill reg information. */
8484 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8486 spill_reg_store[last_reload_reg + k] = 0;
8487 spill_reg_stored_to[last_reload_reg + k] = 0;
8490 /* The caller has already checked that REG dies or is set in INSN.
8491 It has also checked that we are optimizing, and thus some
8492 inaccuracies in the debugging information are acceptable.
8493 So we could just delete output_reload_insn. But in some cases
8494 we can improve the debugging information without sacrificing
8495 optimization - maybe even improving the code: See if the pseudo
8496 reg has been completely replaced with reload regs. If so, delete
8497 the store insn and forget we had a stack slot for the pseudo. */
8498 if (rld[j].out != rld[j].in
8499 && REG_N_DEATHS (REGNO (reg)) == 1
8500 && REG_N_SETS (REGNO (reg)) == 1
8501 && REG_BASIC_BLOCK (REGNO (reg)) >= NUM_FIXED_BLOCKS
8502 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8504 rtx i2;
8506 /* We know that it was used only between here and the beginning of
8507 the current basic block. (We also know that the last use before
8508 INSN was the output reload we are thinking of deleting, but never
8509 mind that.) Search that range; see if any ref remains. */
8510 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8512 rtx set = single_set (i2);
8514 /* Uses which just store in the pseudo don't count,
8515 since if they are the only uses, they are dead. */
8516 if (set != 0 && SET_DEST (set) == reg)
8517 continue;
8518 if (LABEL_P (i2)
8519 || JUMP_P (i2))
8520 break;
8521 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8522 && reg_mentioned_p (reg, PATTERN (i2)))
8524 /* Some other ref remains; just delete the output reload we
8525 know to be dead. */
8526 delete_address_reloads (output_reload_insn, insn);
8527 delete_insn (output_reload_insn);
8528 return;
8532 /* Delete the now-dead stores into this pseudo. Note that this
8533 loop also takes care of deleting output_reload_insn. */
8534 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8536 rtx set = single_set (i2);
8538 if (set != 0 && SET_DEST (set) == reg)
8540 delete_address_reloads (i2, insn);
8541 delete_insn (i2);
8543 if (LABEL_P (i2)
8544 || JUMP_P (i2))
8545 break;
8548 /* For the debugging info, say the pseudo lives in this reload reg. */
8549 reg_renumber[REGNO (reg)] = REGNO (new_reload_reg);
8550 if (flag_ira)
8551 /* Inform IRA about the change. */
8552 mark_allocation_change (REGNO (reg));
8553 alter_reg (REGNO (reg), -1, false);
8555 else
8557 delete_address_reloads (output_reload_insn, insn);
8558 delete_insn (output_reload_insn);
8562 /* We are going to delete DEAD_INSN. Recursively delete loads of
8563 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8564 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8565 static void
8566 delete_address_reloads (rtx dead_insn, rtx current_insn)
8568 rtx set = single_set (dead_insn);
8569 rtx set2, dst, prev, next;
8570 if (set)
8572 rtx dst = SET_DEST (set);
8573 if (MEM_P (dst))
8574 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8576 /* If we deleted the store from a reloaded post_{in,de}c expression,
8577 we can delete the matching adds. */
8578 prev = PREV_INSN (dead_insn);
8579 next = NEXT_INSN (dead_insn);
8580 if (! prev || ! next)
8581 return;
8582 set = single_set (next);
8583 set2 = single_set (prev);
8584 if (! set || ! set2
8585 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8586 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8587 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8588 return;
8589 dst = SET_DEST (set);
8590 if (! rtx_equal_p (dst, SET_DEST (set2))
8591 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8592 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8593 || (INTVAL (XEXP (SET_SRC (set), 1))
8594 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8595 return;
8596 delete_related_insns (prev);
8597 delete_related_insns (next);
8600 /* Subfunction of delete_address_reloads: process registers found in X. */
8601 static void
8602 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8604 rtx prev, set, dst, i2;
8605 int i, j;
8606 enum rtx_code code = GET_CODE (x);
8608 if (code != REG)
8610 const char *fmt = GET_RTX_FORMAT (code);
8611 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8613 if (fmt[i] == 'e')
8614 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8615 else if (fmt[i] == 'E')
8617 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8618 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8619 current_insn);
8622 return;
8625 if (spill_reg_order[REGNO (x)] < 0)
8626 return;
8628 /* Scan backwards for the insn that sets x. This might be a way back due
8629 to inheritance. */
8630 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8632 code = GET_CODE (prev);
8633 if (code == CODE_LABEL || code == JUMP_INSN)
8634 return;
8635 if (!INSN_P (prev))
8636 continue;
8637 if (reg_set_p (x, PATTERN (prev)))
8638 break;
8639 if (reg_referenced_p (x, PATTERN (prev)))
8640 return;
8642 if (! prev || INSN_UID (prev) < reload_first_uid)
8643 return;
8644 /* Check that PREV only sets the reload register. */
8645 set = single_set (prev);
8646 if (! set)
8647 return;
8648 dst = SET_DEST (set);
8649 if (!REG_P (dst)
8650 || ! rtx_equal_p (dst, x))
8651 return;
8652 if (! reg_set_p (dst, PATTERN (dead_insn)))
8654 /* Check if DST was used in a later insn -
8655 it might have been inherited. */
8656 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8658 if (LABEL_P (i2))
8659 break;
8660 if (! INSN_P (i2))
8661 continue;
8662 if (reg_referenced_p (dst, PATTERN (i2)))
8664 /* If there is a reference to the register in the current insn,
8665 it might be loaded in a non-inherited reload. If no other
8666 reload uses it, that means the register is set before
8667 referenced. */
8668 if (i2 == current_insn)
8670 for (j = n_reloads - 1; j >= 0; j--)
8671 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8672 || reload_override_in[j] == dst)
8673 return;
8674 for (j = n_reloads - 1; j >= 0; j--)
8675 if (rld[j].in && rld[j].reg_rtx == dst)
8676 break;
8677 if (j >= 0)
8678 break;
8680 return;
8682 if (JUMP_P (i2))
8683 break;
8684 /* If DST is still live at CURRENT_INSN, check if it is used for
8685 any reload. Note that even if CURRENT_INSN sets DST, we still
8686 have to check the reloads. */
8687 if (i2 == current_insn)
8689 for (j = n_reloads - 1; j >= 0; j--)
8690 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8691 || reload_override_in[j] == dst)
8692 return;
8693 /* ??? We can't finish the loop here, because dst might be
8694 allocated to a pseudo in this block if no reload in this
8695 block needs any of the classes containing DST - see
8696 spill_hard_reg. There is no easy way to tell this, so we
8697 have to scan till the end of the basic block. */
8699 if (reg_set_p (dst, PATTERN (i2)))
8700 break;
8703 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8704 reg_reloaded_contents[REGNO (dst)] = -1;
8705 delete_insn (prev);
8708 /* Output reload-insns to reload VALUE into RELOADREG.
8709 VALUE is an autoincrement or autodecrement RTX whose operand
8710 is a register or memory location;
8711 so reloading involves incrementing that location.
8712 IN is either identical to VALUE, or some cheaper place to reload from.
8714 INC_AMOUNT is the number to increment or decrement by (always positive).
8715 This cannot be deduced from VALUE.
8717 Return the instruction that stores into RELOADREG. */
8719 static rtx
8720 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
8722 /* REG or MEM to be copied and incremented. */
8723 rtx incloc = find_replacement (&XEXP (value, 0));
8724 /* Nonzero if increment after copying. */
8725 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
8726 || GET_CODE (value) == POST_MODIFY);
8727 rtx last;
8728 rtx inc;
8729 rtx add_insn;
8730 int code;
8731 rtx store;
8732 rtx real_in = in == value ? incloc : in;
8734 /* No hard register is equivalent to this register after
8735 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
8736 we could inc/dec that register as well (maybe even using it for
8737 the source), but I'm not sure it's worth worrying about. */
8738 if (REG_P (incloc))
8739 reg_last_reload_reg[REGNO (incloc)] = 0;
8741 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
8743 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
8744 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
8746 else
8748 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8749 inc_amount = -inc_amount;
8751 inc = GEN_INT (inc_amount);
8754 /* If this is post-increment, first copy the location to the reload reg. */
8755 if (post && real_in != reloadreg)
8756 emit_insn (gen_move_insn (reloadreg, real_in));
8758 if (in == value)
8760 /* See if we can directly increment INCLOC. Use a method similar to
8761 that in gen_reload. */
8763 last = get_last_insn ();
8764 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8765 gen_rtx_PLUS (GET_MODE (incloc),
8766 incloc, inc)));
8768 code = recog_memoized (add_insn);
8769 if (code >= 0)
8771 extract_insn (add_insn);
8772 if (constrain_operands (1))
8774 /* If this is a pre-increment and we have incremented the value
8775 where it lives, copy the incremented value to RELOADREG to
8776 be used as an address. */
8778 if (! post)
8779 emit_insn (gen_move_insn (reloadreg, incloc));
8781 return add_insn;
8784 delete_insns_since (last);
8787 /* If couldn't do the increment directly, must increment in RELOADREG.
8788 The way we do this depends on whether this is pre- or post-increment.
8789 For pre-increment, copy INCLOC to the reload register, increment it
8790 there, then save back. */
8792 if (! post)
8794 if (in != reloadreg)
8795 emit_insn (gen_move_insn (reloadreg, real_in));
8796 emit_insn (gen_add2_insn (reloadreg, inc));
8797 store = emit_insn (gen_move_insn (incloc, reloadreg));
8799 else
8801 /* Postincrement.
8802 Because this might be a jump insn or a compare, and because RELOADREG
8803 may not be available after the insn in an input reload, we must do
8804 the incrementation before the insn being reloaded for.
8806 We have already copied IN to RELOADREG. Increment the copy in
8807 RELOADREG, save that back, then decrement RELOADREG so it has
8808 the original value. */
8810 emit_insn (gen_add2_insn (reloadreg, inc));
8811 store = emit_insn (gen_move_insn (incloc, reloadreg));
8812 if (GET_CODE (inc) == CONST_INT)
8813 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
8814 else
8815 emit_insn (gen_sub2_insn (reloadreg, inc));
8818 return store;
8821 #ifdef AUTO_INC_DEC
8822 static void
8823 add_auto_inc_notes (rtx insn, rtx x)
8825 enum rtx_code code = GET_CODE (x);
8826 const char *fmt;
8827 int i, j;
8829 if (code == MEM && auto_inc_p (XEXP (x, 0)))
8831 REG_NOTES (insn)
8832 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
8833 return;
8836 /* Scan all the operand sub-expressions. */
8837 fmt = GET_RTX_FORMAT (code);
8838 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8840 if (fmt[i] == 'e')
8841 add_auto_inc_notes (insn, XEXP (x, i));
8842 else if (fmt[i] == 'E')
8843 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8844 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8847 #endif
8849 /* Copy EH notes from an insn to its reloads. */
8850 static void
8851 copy_eh_notes (rtx insn, rtx x)
8853 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8854 if (eh_note)
8856 for (; x != 0; x = NEXT_INSN (x))
8858 if (may_trap_p (PATTERN (x)))
8859 REG_NOTES (x)
8860 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8861 REG_NOTES (x));
8866 /* This is used by reload pass, that does emit some instructions after
8867 abnormal calls moving basic block end, but in fact it wants to emit
8868 them on the edge. Looks for abnormal call edges, find backward the
8869 proper call and fix the damage.
8871 Similar handle instructions throwing exceptions internally. */
8872 void
8873 fixup_abnormal_edges (void)
8875 bool inserted = false;
8876 basic_block bb;
8878 FOR_EACH_BB (bb)
8880 edge e;
8881 edge_iterator ei;
8883 /* Look for cases we are interested in - calls or instructions causing
8884 exceptions. */
8885 FOR_EACH_EDGE (e, ei, bb->succs)
8887 if (e->flags & EDGE_ABNORMAL_CALL)
8888 break;
8889 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8890 == (EDGE_ABNORMAL | EDGE_EH))
8891 break;
8893 if (e && !CALL_P (BB_END (bb))
8894 && !can_throw_internal (BB_END (bb)))
8896 rtx insn;
8898 /* Get past the new insns generated. Allow notes, as the insns
8899 may be already deleted. */
8900 insn = BB_END (bb);
8901 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8902 && !can_throw_internal (insn)
8903 && insn != BB_HEAD (bb))
8904 insn = PREV_INSN (insn);
8906 if (CALL_P (insn) || can_throw_internal (insn))
8908 rtx stop, next;
8910 stop = NEXT_INSN (BB_END (bb));
8911 BB_END (bb) = insn;
8912 insn = NEXT_INSN (insn);
8914 FOR_EACH_EDGE (e, ei, bb->succs)
8915 if (e->flags & EDGE_FALLTHRU)
8916 break;
8918 while (insn && insn != stop)
8920 next = NEXT_INSN (insn);
8921 if (INSN_P (insn))
8923 delete_insn (insn);
8925 /* Sometimes there's still the return value USE.
8926 If it's placed after a trapping call (i.e. that
8927 call is the last insn anyway), we have no fallthru
8928 edge. Simply delete this use and don't try to insert
8929 on the non-existent edge. */
8930 if (GET_CODE (PATTERN (insn)) != USE)
8932 /* We're not deleting it, we're moving it. */
8933 INSN_DELETED_P (insn) = 0;
8934 PREV_INSN (insn) = NULL_RTX;
8935 NEXT_INSN (insn) = NULL_RTX;
8937 insert_insn_on_edge (insn, e);
8938 inserted = true;
8941 else if (!BARRIER_P (insn))
8942 set_block_for_insn (insn, NULL);
8943 insn = next;
8947 /* It may be that we don't find any such trapping insn. In this
8948 case we discovered quite late that the insn that had been
8949 marked as can_throw_internal in fact couldn't trap at all.
8950 So we should in fact delete the EH edges out of the block. */
8951 else
8952 purge_dead_edges (bb);
8956 /* We've possibly turned single trapping insn into multiple ones. */
8957 if (flag_non_call_exceptions)
8959 sbitmap blocks;
8960 blocks = sbitmap_alloc (last_basic_block);
8961 sbitmap_ones (blocks);
8962 find_many_sub_basic_blocks (blocks);
8963 sbitmap_free (blocks);
8966 if (inserted)
8967 commit_edge_insertions ();
8969 #ifdef ENABLE_CHECKING
8970 /* Verify that we didn't turn one trapping insn into many, and that
8971 we found and corrected all of the problems wrt fixups on the
8972 fallthru edge. */
8973 verify_flow_info ();
8974 #endif