2008-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / reload1.c
blobb133a8c01b16f180f18a6399dcd4d0180988cf4a
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. */
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 bool inherit_piecemeal_p (int, int);
439 static void emit_reload_insns (struct insn_chain *);
440 static void delete_output_reload (rtx, int, int);
441 static void delete_address_reloads (rtx, rtx);
442 static void delete_address_reloads_1 (rtx, rtx, rtx);
443 static rtx inc_for_reload (rtx, rtx, rtx, int);
444 #ifdef AUTO_INC_DEC
445 static void add_auto_inc_notes (rtx, rtx);
446 #endif
447 static void copy_eh_notes (rtx, rtx);
448 static int reloads_conflict (int, int);
449 static rtx gen_reload (rtx, rtx, int, enum reload_type);
450 static rtx emit_insn_if_valid_for_reload (rtx);
452 /* Initialize the reload pass. This is called at the beginning of compilation
453 and may be called again if the target is reinitialized. */
455 void
456 init_reload (void)
458 int i;
460 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
461 Set spill_indirect_levels to the number of levels such addressing is
462 permitted, zero if it is not permitted at all. */
464 rtx tem
465 = gen_rtx_MEM (Pmode,
466 gen_rtx_PLUS (Pmode,
467 gen_rtx_REG (Pmode,
468 LAST_VIRTUAL_REGISTER + 1),
469 GEN_INT (4)));
470 spill_indirect_levels = 0;
472 while (memory_address_p (QImode, tem))
474 spill_indirect_levels++;
475 tem = gen_rtx_MEM (Pmode, tem);
478 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
480 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
481 indirect_symref_ok = memory_address_p (QImode, tem);
483 /* See if reg+reg is a valid (and offsettable) address. */
485 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487 tem = gen_rtx_PLUS (Pmode,
488 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
489 gen_rtx_REG (Pmode, i));
491 /* This way, we make sure that reg+reg is an offsettable address. */
492 tem = plus_constant (tem, 4);
494 if (memory_address_p (QImode, tem))
496 double_reg_address_ok = 1;
497 break;
501 /* Initialize obstack for our rtl allocation. */
502 gcc_obstack_init (&reload_obstack);
503 reload_startobj = obstack_alloc (&reload_obstack, 0);
505 INIT_REG_SET (&spilled_pseudos);
506 INIT_REG_SET (&pseudos_counted);
509 /* List of insn chains that are currently unused. */
510 static struct insn_chain *unused_insn_chains = 0;
512 /* Allocate an empty insn_chain structure. */
513 struct insn_chain *
514 new_insn_chain (void)
516 struct insn_chain *c;
518 if (unused_insn_chains == 0)
520 c = obstack_alloc (&reload_obstack, sizeof (struct insn_chain));
521 INIT_REG_SET (&c->live_throughout);
522 INIT_REG_SET (&c->dead_or_set);
524 else
526 c = unused_insn_chains;
527 unused_insn_chains = c->next;
529 c->is_caller_save_insn = 0;
530 c->need_operand_change = 0;
531 c->need_reload = 0;
532 c->need_elim = 0;
533 return c;
536 /* Small utility function to set all regs in hard reg set TO which are
537 allocated to pseudos in regset FROM. */
539 void
540 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
542 unsigned int regno;
543 reg_set_iterator rsi;
545 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
547 int r = reg_renumber[regno];
549 if (r < 0)
551 /* reload_combine uses the information from
552 DF_LIVE_IN (BASIC_BLOCK), which might still
553 contain registers that have not actually been allocated
554 since they have an equivalence. */
555 gcc_assert (flag_ira || reload_completed);
557 else
558 add_to_hard_reg_set (to, PSEUDO_REGNO_MODE (regno), r);
562 /* Replace all pseudos found in LOC with their corresponding
563 equivalences. */
565 static void
566 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
568 rtx x = *loc;
569 enum rtx_code code;
570 const char *fmt;
571 int i, j;
573 if (! x)
574 return;
576 code = GET_CODE (x);
577 if (code == REG)
579 unsigned int regno = REGNO (x);
581 if (regno < FIRST_PSEUDO_REGISTER)
582 return;
584 x = eliminate_regs (x, mem_mode, usage);
585 if (x != *loc)
587 *loc = x;
588 replace_pseudos_in (loc, mem_mode, usage);
589 return;
592 if (reg_equiv_constant[regno])
593 *loc = reg_equiv_constant[regno];
594 else if (reg_equiv_mem[regno])
595 *loc = reg_equiv_mem[regno];
596 else if (reg_equiv_address[regno])
597 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
598 else
600 gcc_assert (!REG_P (regno_reg_rtx[regno])
601 || REGNO (regno_reg_rtx[regno]) != regno);
602 *loc = regno_reg_rtx[regno];
605 return;
607 else if (code == MEM)
609 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
610 return;
613 /* Process each of our operands recursively. */
614 fmt = GET_RTX_FORMAT (code);
615 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
616 if (*fmt == 'e')
617 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
618 else if (*fmt == 'E')
619 for (j = 0; j < XVECLEN (x, i); j++)
620 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
623 /* Determine if the current function has an exception receiver block
624 that reaches the exit block via non-exceptional edges */
626 static bool
627 has_nonexceptional_receiver (void)
629 edge e;
630 edge_iterator ei;
631 basic_block *tos, *worklist, bb;
633 /* If we're not optimizing, then just err on the safe side. */
634 if (!optimize)
635 return true;
637 /* First determine which blocks can reach exit via normal paths. */
638 tos = worklist = xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
640 FOR_EACH_BB (bb)
641 bb->flags &= ~BB_REACHABLE;
643 /* Place the exit block on our worklist. */
644 EXIT_BLOCK_PTR->flags |= BB_REACHABLE;
645 *tos++ = EXIT_BLOCK_PTR;
647 /* Iterate: find everything reachable from what we've already seen. */
648 while (tos != worklist)
650 bb = *--tos;
652 FOR_EACH_EDGE (e, ei, bb->preds)
653 if (!(e->flags & EDGE_ABNORMAL))
655 basic_block src = e->src;
657 if (!(src->flags & BB_REACHABLE))
659 src->flags |= BB_REACHABLE;
660 *tos++ = src;
664 free (worklist);
666 /* Now see if there's a reachable block with an exceptional incoming
667 edge. */
668 FOR_EACH_BB (bb)
669 if (bb->flags & BB_REACHABLE)
670 FOR_EACH_EDGE (e, ei, bb->preds)
671 if (e->flags & EDGE_ABNORMAL)
672 return true;
674 /* No exceptional block reached exit unexceptionally. */
675 return false;
679 /* Global variables used by reload and its subroutines. */
681 /* Set during calculate_needs if an insn needs register elimination. */
682 static int something_needs_elimination;
683 /* Set during calculate_needs if an insn needs an operand changed. */
684 static int something_needs_operands_changed;
686 /* Nonzero means we couldn't get enough spill regs. */
687 static int failure;
689 /* Temporary array of pseudo-register number. */
690 static int *temp_pseudo_reg_arr;
692 /* Main entry point for the reload pass.
694 FIRST is the first insn of the function being compiled.
696 GLOBAL nonzero means we were called from global_alloc
697 and should attempt to reallocate any pseudoregs that we
698 displace from hard regs we will use for reloads.
699 If GLOBAL is zero, we do not have enough information to do that,
700 so any pseudo reg that is spilled must go to the stack.
702 Return value is nonzero if reload failed
703 and we must not do any more for this function. */
706 reload (rtx first, int global)
708 int i, n;
709 rtx insn;
710 struct elim_table *ep;
711 basic_block bb;
713 /* Make sure even insns with volatile mem refs are recognizable. */
714 init_recog ();
716 failure = 0;
718 reload_firstobj = obstack_alloc (&reload_obstack, 0);
720 /* Make sure that the last insn in the chain
721 is not something that needs reloading. */
722 emit_note (NOTE_INSN_DELETED);
724 /* Enable find_equiv_reg to distinguish insns made by reload. */
725 reload_first_uid = get_max_uid ();
727 #ifdef SECONDARY_MEMORY_NEEDED
728 /* Initialize the secondary memory table. */
729 clear_secondary_mem ();
730 #endif
732 /* We don't have a stack slot for any spill reg yet. */
733 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
734 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
736 /* Initialize the save area information for caller-save, in case some
737 are needed. */
738 init_save_areas ();
740 /* Compute which hard registers are now in use
741 as homes for pseudo registers.
742 This is done here rather than (eg) in global_alloc
743 because this point is reached even if not optimizing. */
744 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
745 mark_home_live (i);
747 /* A function that has a nonlocal label that can reach the exit
748 block via non-exceptional paths must save all call-saved
749 registers. */
750 if (current_function_has_nonlocal_label
751 && has_nonexceptional_receiver ())
752 current_function_saves_all_registers = 1;
754 if (current_function_saves_all_registers)
755 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
756 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
757 df_set_regs_ever_live (i, true);
759 /* Find all the pseudo registers that didn't get hard regs
760 but do have known equivalent constants or memory slots.
761 These include parameters (known equivalent to parameter slots)
762 and cse'd or loop-moved constant memory addresses.
764 Record constant equivalents in reg_equiv_constant
765 so they will be substituted by find_reloads.
766 Record memory equivalents in reg_mem_equiv so they can
767 be substituted eventually by altering the REG-rtx's. */
769 reg_equiv_constant = XCNEWVEC (rtx, max_regno);
770 reg_equiv_invariant = XCNEWVEC (rtx, max_regno);
771 reg_equiv_mem = XCNEWVEC (rtx, max_regno);
772 reg_equiv_alt_mem_list = XCNEWVEC (rtx, max_regno);
773 reg_equiv_address = XCNEWVEC (rtx, max_regno);
774 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
775 reg_old_renumber = XCNEWVEC (short, max_regno);
776 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
777 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
778 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
780 CLEAR_HARD_REG_SET (bad_spill_regs_global);
782 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
783 to. Also find all paradoxical subregs and find largest such for
784 each pseudo. */
786 num_eliminable_invariants = 0;
787 for (insn = first; insn; insn = NEXT_INSN (insn))
789 rtx set = single_set (insn);
791 /* We may introduce USEs that we want to remove at the end, so
792 we'll mark them with QImode. Make sure there are no
793 previously-marked insns left by say regmove. */
794 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
795 && GET_MODE (insn) != VOIDmode)
796 PUT_MODE (insn, VOIDmode);
798 if (INSN_P (insn))
799 scan_paradoxical_subregs (PATTERN (insn));
801 if (set != 0 && REG_P (SET_DEST (set)))
803 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
804 rtx x;
806 if (! note)
807 continue;
809 i = REGNO (SET_DEST (set));
810 x = XEXP (note, 0);
812 if (i <= LAST_VIRTUAL_REGISTER)
813 continue;
815 if (! function_invariant_p (x)
816 || ! flag_pic
817 /* A function invariant is often CONSTANT_P but may
818 include a register. We promise to only pass
819 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
820 || (CONSTANT_P (x)
821 && LEGITIMATE_PIC_OPERAND_P (x)))
823 /* It can happen that a REG_EQUIV note contains a MEM
824 that is not a legitimate memory operand. As later
825 stages of reload assume that all addresses found
826 in the reg_equiv_* arrays were originally legitimate,
827 we ignore such REG_EQUIV notes. */
828 if (memory_operand (x, VOIDmode))
830 /* Always unshare the equivalence, so we can
831 substitute into this insn without touching the
832 equivalence. */
833 reg_equiv_memory_loc[i] = copy_rtx (x);
835 else if (function_invariant_p (x))
837 if (GET_CODE (x) == PLUS)
839 /* This is PLUS of frame pointer and a constant,
840 and might be shared. Unshare it. */
841 reg_equiv_invariant[i] = copy_rtx (x);
842 num_eliminable_invariants++;
844 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
846 reg_equiv_invariant[i] = x;
847 num_eliminable_invariants++;
849 else if (LEGITIMATE_CONSTANT_P (x))
850 reg_equiv_constant[i] = x;
851 else
853 reg_equiv_memory_loc[i]
854 = force_const_mem (GET_MODE (SET_DEST (set)), x);
855 if (! reg_equiv_memory_loc[i])
856 reg_equiv_init[i] = NULL_RTX;
859 else
861 reg_equiv_init[i] = NULL_RTX;
862 continue;
865 else
866 reg_equiv_init[i] = NULL_RTX;
870 if (dump_file)
871 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
872 if (reg_equiv_init[i])
874 fprintf (dump_file, "init_insns for %u: ", i);
875 print_inline_rtx (dump_file, reg_equiv_init[i], 20);
876 fprintf (dump_file, "\n");
879 init_elim_table ();
881 first_label_num = get_first_label_num ();
882 num_labels = max_label_num () - first_label_num;
884 /* Allocate the tables used to store offset information at labels. */
885 /* We used to use alloca here, but the size of what it would try to
886 allocate would occasionally cause it to exceed the stack limit and
887 cause a core dump. */
888 offsets_known_at = XNEWVEC (char, num_labels);
889 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
891 /* Alter each pseudo-reg rtx to contain its hard reg number. Assign
892 stack slots to the pseudos that lack hard regs or equivalents.
893 Do not touch virtual registers. */
895 temp_pseudo_reg_arr = XNEWVEC (int, max_regno - LAST_VIRTUAL_REGISTER - 1);
896 for (n = 0, i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
897 temp_pseudo_reg_arr [n++] = i;
899 if (flag_ira)
900 sort_regnos_for_alter_reg (temp_pseudo_reg_arr, n, reg_max_ref_width);
902 for (i = 0; i < n; i++)
903 alter_reg (temp_pseudo_reg_arr [i], -1, false);
905 /* If we have some registers we think can be eliminated, scan all insns to
906 see if there is an insn that sets one of these registers to something
907 other than itself plus a constant. If so, the register cannot be
908 eliminated. Doing this scan here eliminates an extra pass through the
909 main reload loop in the most common case where register elimination
910 cannot be done. */
911 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
912 if (INSN_P (insn))
913 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
915 maybe_fix_stack_asms ();
917 insns_need_reload = 0;
918 something_needs_elimination = 0;
920 /* Initialize to -1, which means take the first spill register. */
921 last_spill_reg = -1;
923 /* Spill any hard regs that we know we can't eliminate. */
924 CLEAR_HARD_REG_SET (used_spill_regs);
925 /* There can be multiple ways to eliminate a register;
926 they should be listed adjacently.
927 Elimination for any register fails only if all possible ways fail. */
928 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
930 int from = ep->from;
931 int can_eliminate = 0;
934 can_eliminate |= ep->can_eliminate;
935 ep++;
937 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
938 if (! can_eliminate)
939 spill_hard_reg (from, 1);
942 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
943 if (frame_pointer_needed)
944 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
945 #endif
946 finish_spills (global);
948 /* From now on, we may need to generate moves differently. We may also
949 allow modifications of insns which cause them to not be recognized.
950 Any such modifications will be cleaned up during reload itself. */
951 reload_in_progress = 1;
953 /* This loop scans the entire function each go-round
954 and repeats until one repetition spills no additional hard regs. */
955 for (;;)
957 int something_changed;
958 int did_spill;
959 HOST_WIDE_INT starting_frame_size;
961 starting_frame_size = get_frame_size ();
963 set_initial_elim_offsets ();
964 set_initial_label_offsets ();
966 /* For each pseudo register that has an equivalent location defined,
967 try to eliminate any eliminable registers (such as the frame pointer)
968 assuming initial offsets for the replacement register, which
969 is the normal case.
971 If the resulting location is directly addressable, substitute
972 the MEM we just got directly for the old REG.
974 If it is not addressable but is a constant or the sum of a hard reg
975 and constant, it is probably not addressable because the constant is
976 out of range, in that case record the address; we will generate
977 hairy code to compute the address in a register each time it is
978 needed. Similarly if it is a hard register, but one that is not
979 valid as an address register.
981 If the location is not addressable, but does not have one of the
982 above forms, assign a stack slot. We have to do this to avoid the
983 potential of producing lots of reloads if, e.g., a location involves
984 a pseudo that didn't get a hard register and has an equivalent memory
985 location that also involves a pseudo that didn't get a hard register.
987 Perhaps at some point we will improve reload_when_needed handling
988 so this problem goes away. But that's very hairy. */
990 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
991 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
993 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
995 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
996 XEXP (x, 0)))
997 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
998 else if (CONSTANT_P (XEXP (x, 0))
999 || (REG_P (XEXP (x, 0))
1000 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
1001 || (GET_CODE (XEXP (x, 0)) == PLUS
1002 && REG_P (XEXP (XEXP (x, 0), 0))
1003 && (REGNO (XEXP (XEXP (x, 0), 0))
1004 < FIRST_PSEUDO_REGISTER)
1005 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
1006 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
1007 else
1009 /* Make a new stack slot. Then indicate that something
1010 changed so we go back and recompute offsets for
1011 eliminable registers because the allocation of memory
1012 below might change some offset. reg_equiv_{mem,address}
1013 will be set up for this pseudo on the next pass around
1014 the loop. */
1015 reg_equiv_memory_loc[i] = 0;
1016 reg_equiv_init[i] = 0;
1017 alter_reg (i, -1, true);
1021 if (caller_save_needed)
1022 setup_save_areas ();
1024 /* If we allocated another stack slot, redo elimination bookkeeping. */
1025 if (starting_frame_size != get_frame_size ())
1026 continue;
1027 if (starting_frame_size && cfun->stack_alignment_needed)
1029 /* If we have a stack frame, we must align it now. The
1030 stack size may be a part of the offset computation for
1031 register elimination. So if this changes the stack size,
1032 then repeat the elimination bookkeeping. We don't
1033 realign when there is no stack, as that will cause a
1034 stack frame when none is needed should
1035 STARTING_FRAME_OFFSET not be already aligned to
1036 STACK_BOUNDARY. */
1037 assign_stack_local (BLKmode, 0, cfun->stack_alignment_needed);
1038 if (starting_frame_size != get_frame_size ())
1039 continue;
1042 if (caller_save_needed)
1044 save_call_clobbered_regs ();
1045 /* That might have allocated new insn_chain structures. */
1046 reload_firstobj = obstack_alloc (&reload_obstack, 0);
1049 calculate_needs_all_insns (global);
1051 if (! flag_ira)
1052 CLEAR_REG_SET (&spilled_pseudos);
1053 did_spill = 0;
1055 something_changed = 0;
1057 /* If we allocated any new memory locations, make another pass
1058 since it might have changed elimination offsets. */
1059 if (starting_frame_size != get_frame_size ())
1060 something_changed = 1;
1062 /* Even if the frame size remained the same, we might still have
1063 changed elimination offsets, e.g. if find_reloads called
1064 force_const_mem requiring the back end to allocate a constant
1065 pool base register that needs to be saved on the stack. */
1066 else if (!verify_initial_elim_offsets ())
1067 something_changed = 1;
1070 HARD_REG_SET to_spill;
1071 CLEAR_HARD_REG_SET (to_spill);
1072 update_eliminables (&to_spill);
1073 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
1075 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1076 if (TEST_HARD_REG_BIT (to_spill, i))
1078 spill_hard_reg (i, 1);
1079 did_spill = 1;
1081 /* Regardless of the state of spills, if we previously had
1082 a register that we thought we could eliminate, but now can
1083 not eliminate, we must run another pass.
1085 Consider pseudos which have an entry in reg_equiv_* which
1086 reference an eliminable register. We must make another pass
1087 to update reg_equiv_* so that we do not substitute in the
1088 old value from when we thought the elimination could be
1089 performed. */
1090 something_changed = 1;
1094 select_reload_regs ();
1095 if (failure)
1096 goto failed;
1098 if (insns_need_reload != 0 || did_spill)
1099 something_changed |= finish_spills (global);
1101 if (! something_changed)
1102 break;
1104 if (caller_save_needed)
1105 delete_caller_save_insns ();
1107 obstack_free (&reload_obstack, reload_firstobj);
1110 if (flag_ira)
1111 sort_insn_chain (FALSE);
1113 /* If global-alloc was run, notify it of any register eliminations we have
1114 done. */
1115 if (global)
1116 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1117 if (ep->can_eliminate)
1118 mark_elimination (ep->from, ep->to);
1120 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1121 If that insn didn't set the register (i.e., it copied the register to
1122 memory), just delete that insn instead of the equivalencing insn plus
1123 anything now dead. If we call delete_dead_insn on that insn, we may
1124 delete the insn that actually sets the register if the register dies
1125 there and that is incorrect. */
1127 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1129 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1131 rtx list;
1132 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1134 rtx equiv_insn = XEXP (list, 0);
1136 /* If we already deleted the insn or if it may trap, we can't
1137 delete it. The latter case shouldn't happen, but can
1138 if an insn has a variable address, gets a REG_EH_REGION
1139 note added to it, and then gets converted into a load
1140 from a constant address. */
1141 if (NOTE_P (equiv_insn)
1142 || can_throw_internal (equiv_insn))
1144 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1145 delete_dead_insn (equiv_insn);
1146 else
1147 SET_INSN_DELETED (equiv_insn);
1152 /* Use the reload registers where necessary
1153 by generating move instructions to move the must-be-register
1154 values into or out of the reload registers. */
1156 if (insns_need_reload != 0 || something_needs_elimination
1157 || something_needs_operands_changed)
1159 HOST_WIDE_INT old_frame_size = get_frame_size ();
1161 reload_as_needed (global);
1163 gcc_assert (old_frame_size == get_frame_size ());
1165 gcc_assert (verify_initial_elim_offsets ());
1168 /* If we were able to eliminate the frame pointer, show that it is no
1169 longer live at the start of any basic block. If it ls live by
1170 virtue of being in a pseudo, that pseudo will be marked live
1171 and hence the frame pointer will be known to be live via that
1172 pseudo. */
1174 if (! frame_pointer_needed)
1175 FOR_EACH_BB (bb)
1176 bitmap_clear_bit (df_get_live_in (bb), HARD_FRAME_POINTER_REGNUM);
1178 /* Come here (with failure set nonzero) if we can't get enough spill
1179 regs. */
1180 failed:
1182 CLEAR_REG_SET (&spilled_pseudos);
1183 reload_in_progress = 0;
1185 /* Now eliminate all pseudo regs by modifying them into
1186 their equivalent memory references.
1187 The REG-rtx's for the pseudos are modified in place,
1188 so all insns that used to refer to them now refer to memory.
1190 For a reg that has a reg_equiv_address, all those insns
1191 were changed by reloading so that no insns refer to it any longer;
1192 but the DECL_RTL of a variable decl may refer to it,
1193 and if so this causes the debugging info to mention the variable. */
1195 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1197 rtx addr = 0;
1199 if (reg_equiv_mem[i])
1200 addr = XEXP (reg_equiv_mem[i], 0);
1202 if (reg_equiv_address[i])
1203 addr = reg_equiv_address[i];
1205 if (addr)
1207 if (reg_renumber[i] < 0)
1209 rtx reg = regno_reg_rtx[i];
1211 REG_USERVAR_P (reg) = 0;
1212 PUT_CODE (reg, MEM);
1213 XEXP (reg, 0) = addr;
1214 if (reg_equiv_memory_loc[i])
1215 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1216 else
1218 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1219 MEM_ATTRS (reg) = 0;
1221 MEM_NOTRAP_P (reg) = 1;
1223 else if (reg_equiv_mem[i])
1224 XEXP (reg_equiv_mem[i], 0) = addr;
1228 /* We must set reload_completed now since the cleanup_subreg_operands call
1229 below will re-recognize each insn and reload may have generated insns
1230 which are only valid during and after reload. */
1231 reload_completed = 1;
1233 /* Make a pass over all the insns and delete all USEs which we inserted
1234 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1235 notes. Delete all CLOBBER insns, except those that refer to the return
1236 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1237 from misarranging variable-array code, and simplify (subreg (reg))
1238 operands. Also remove all REG_RETVAL and REG_LIBCALL notes since they
1239 are no longer useful or accurate. Strip and regenerate REG_INC notes
1240 that may have been moved around. */
1242 for (insn = first; insn; insn = NEXT_INSN (insn))
1243 if (INSN_P (insn))
1245 rtx *pnote;
1247 if (CALL_P (insn))
1249 HARD_REG_SET used_function_regs;
1251 get_call_invalidated_used_regs (insn, &used_function_regs, false);
1252 IOR_HARD_REG_SET (cfun->emit->call_used_regs, used_function_regs);
1253 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1254 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1257 if ((GET_CODE (PATTERN (insn)) == USE
1258 /* We mark with QImode USEs introduced by reload itself. */
1259 && (GET_MODE (insn) == QImode
1260 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1261 || (GET_CODE (PATTERN (insn)) == CLOBBER
1262 && (!MEM_P (XEXP (PATTERN (insn), 0))
1263 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1264 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1265 && XEXP (XEXP (PATTERN (insn), 0), 0)
1266 != stack_pointer_rtx))
1267 && (!REG_P (XEXP (PATTERN (insn), 0))
1268 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1270 delete_insn (insn);
1271 continue;
1274 /* Some CLOBBERs may survive until here and still reference unassigned
1275 pseudos with const equivalent, which may in turn cause ICE in later
1276 passes if the reference remains in place. */
1277 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1278 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1279 VOIDmode, PATTERN (insn));
1281 /* Discard obvious no-ops, even without -O. This optimization
1282 is fast and doesn't interfere with debugging. */
1283 if (NONJUMP_INSN_P (insn)
1284 && GET_CODE (PATTERN (insn)) == SET
1285 && REG_P (SET_SRC (PATTERN (insn)))
1286 && REG_P (SET_DEST (PATTERN (insn)))
1287 && (REGNO (SET_SRC (PATTERN (insn)))
1288 == REGNO (SET_DEST (PATTERN (insn)))))
1290 delete_insn (insn);
1291 continue;
1294 pnote = &REG_NOTES (insn);
1295 while (*pnote != 0)
1297 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1298 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1299 || REG_NOTE_KIND (*pnote) == REG_INC
1300 || REG_NOTE_KIND (*pnote) == REG_RETVAL
1301 || REG_NOTE_KIND (*pnote) == REG_LIBCALL)
1302 *pnote = XEXP (*pnote, 1);
1303 else
1304 pnote = &XEXP (*pnote, 1);
1307 #ifdef AUTO_INC_DEC
1308 add_auto_inc_notes (insn, PATTERN (insn));
1309 #endif
1311 /* Simplify (subreg (reg)) if it appears as an operand. */
1312 cleanup_subreg_operands (insn);
1314 /* Clean up invalid ASMs so that they don't confuse later passes.
1315 See PR 21299. */
1316 if (asm_noperands (PATTERN (insn)) >= 0)
1318 extract_insn (insn);
1319 if (!constrain_operands (1))
1321 error_for_asm (insn,
1322 "%<asm%> operand has impossible constraints");
1323 delete_insn (insn);
1324 continue;
1329 /* If we are doing stack checking, give a warning if this function's
1330 frame size is larger than we expect. */
1331 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1333 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1334 static int verbose_warned = 0;
1336 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1337 if (df_regs_ever_live_p (i) && ! fixed_regs[i] && call_used_regs[i])
1338 size += UNITS_PER_WORD;
1340 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1342 warning (0, "frame size too large for reliable stack checking");
1343 if (! verbose_warned)
1345 warning (0, "try reducing the number of local variables");
1346 verbose_warned = 1;
1351 /* Indicate that we no longer have known memory locations or constants. */
1352 if (reg_equiv_constant)
1353 free (reg_equiv_constant);
1354 if (reg_equiv_invariant)
1355 free (reg_equiv_invariant);
1356 reg_equiv_constant = 0;
1357 reg_equiv_invariant = 0;
1358 VEC_free (rtx, gc, reg_equiv_memory_loc_vec);
1359 reg_equiv_memory_loc = 0;
1361 free (temp_pseudo_reg_arr);
1363 if (offsets_known_at)
1364 free (offsets_known_at);
1365 if (offsets_at)
1366 free (offsets_at);
1368 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1369 if (reg_equiv_alt_mem_list[i])
1370 free_EXPR_LIST_list (&reg_equiv_alt_mem_list[i]);
1371 free (reg_equiv_alt_mem_list);
1373 free (reg_equiv_mem);
1374 reg_equiv_init = 0;
1375 free (reg_equiv_address);
1376 free (reg_max_ref_width);
1377 free (reg_old_renumber);
1378 free (pseudo_previous_regs);
1379 free (pseudo_forbidden_regs);
1381 CLEAR_HARD_REG_SET (used_spill_regs);
1382 for (i = 0; i < n_spills; i++)
1383 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1385 /* Free all the insn_chain structures at once. */
1386 obstack_free (&reload_obstack, reload_startobj);
1387 unused_insn_chains = 0;
1388 fixup_abnormal_edges ();
1390 /* Replacing pseudos with their memory equivalents might have
1391 created shared rtx. Subsequent passes would get confused
1392 by this, so unshare everything here. */
1393 unshare_all_rtl_again (first);
1395 #ifdef STACK_BOUNDARY
1396 /* init_emit has set the alignment of the hard frame pointer
1397 to STACK_BOUNDARY. It is very likely no longer valid if
1398 the hard frame pointer was used for register allocation. */
1399 if (!frame_pointer_needed)
1400 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1401 #endif
1403 return failure;
1406 /* Yet another special case. Unfortunately, reg-stack forces people to
1407 write incorrect clobbers in asm statements. These clobbers must not
1408 cause the register to appear in bad_spill_regs, otherwise we'll call
1409 fatal_insn later. We clear the corresponding regnos in the live
1410 register sets to avoid this.
1411 The whole thing is rather sick, I'm afraid. */
1413 static void
1414 maybe_fix_stack_asms (void)
1416 #ifdef STACK_REGS
1417 const char *constraints[MAX_RECOG_OPERANDS];
1418 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1419 struct insn_chain *chain;
1421 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1423 int i, noperands;
1424 HARD_REG_SET clobbered, allowed;
1425 rtx pat;
1427 if (! INSN_P (chain->insn)
1428 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1429 continue;
1430 pat = PATTERN (chain->insn);
1431 if (GET_CODE (pat) != PARALLEL)
1432 continue;
1434 CLEAR_HARD_REG_SET (clobbered);
1435 CLEAR_HARD_REG_SET (allowed);
1437 /* First, make a mask of all stack regs that are clobbered. */
1438 for (i = 0; i < XVECLEN (pat, 0); i++)
1440 rtx t = XVECEXP (pat, 0, i);
1441 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1442 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1445 /* Get the operand values and constraints out of the insn. */
1446 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1447 constraints, operand_mode, NULL);
1449 /* For every operand, see what registers are allowed. */
1450 for (i = 0; i < noperands; i++)
1452 const char *p = constraints[i];
1453 /* For every alternative, we compute the class of registers allowed
1454 for reloading in CLS, and merge its contents into the reg set
1455 ALLOWED. */
1456 int cls = (int) NO_REGS;
1458 for (;;)
1460 char c = *p;
1462 if (c == '\0' || c == ',' || c == '#')
1464 /* End of one alternative - mark the regs in the current
1465 class, and reset the class. */
1466 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1467 cls = NO_REGS;
1468 p++;
1469 if (c == '#')
1470 do {
1471 c = *p++;
1472 } while (c != '\0' && c != ',');
1473 if (c == '\0')
1474 break;
1475 continue;
1478 switch (c)
1480 case '=': case '+': case '*': case '%': case '?': case '!':
1481 case '0': case '1': case '2': case '3': case '4': case 'm':
1482 case '<': case '>': case 'V': case 'o': case '&': case 'E':
1483 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
1484 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
1485 case 'P':
1486 break;
1488 case 'p':
1489 cls = (int) reg_class_subunion[cls]
1490 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1491 break;
1493 case 'g':
1494 case 'r':
1495 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1496 break;
1498 default:
1499 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1500 cls = (int) reg_class_subunion[cls]
1501 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1502 else
1503 cls = (int) reg_class_subunion[cls]
1504 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1506 p += CONSTRAINT_LEN (c, p);
1509 /* Those of the registers which are clobbered, but allowed by the
1510 constraints, must be usable as reload registers. So clear them
1511 out of the life information. */
1512 AND_HARD_REG_SET (allowed, clobbered);
1513 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1514 if (TEST_HARD_REG_BIT (allowed, i))
1516 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1517 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1521 #endif
1524 /* Copy the global variables n_reloads and rld into the corresponding elts
1525 of CHAIN. */
1526 static void
1527 copy_reloads (struct insn_chain *chain)
1529 chain->n_reloads = n_reloads;
1530 chain->rld = obstack_alloc (&reload_obstack,
1531 n_reloads * sizeof (struct reload));
1532 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1533 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1536 /* Walk the chain of insns, and determine for each whether it needs reloads
1537 and/or eliminations. Build the corresponding insns_need_reload list, and
1538 set something_needs_elimination as appropriate. */
1539 static void
1540 calculate_needs_all_insns (int global)
1542 struct insn_chain **pprev_reload = &insns_need_reload;
1543 struct insn_chain *chain, *next = 0;
1545 something_needs_elimination = 0;
1547 reload_insn_firstobj = obstack_alloc (&reload_obstack, 0);
1548 for (chain = reload_insn_chain; chain != 0; chain = next)
1550 rtx insn = chain->insn;
1552 next = chain->next;
1554 /* Clear out the shortcuts. */
1555 chain->n_reloads = 0;
1556 chain->need_elim = 0;
1557 chain->need_reload = 0;
1558 chain->need_operand_change = 0;
1560 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1561 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1562 what effects this has on the known offsets at labels. */
1564 if (LABEL_P (insn) || JUMP_P (insn)
1565 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1566 set_label_offsets (insn, insn, 0);
1568 if (INSN_P (insn))
1570 rtx old_body = PATTERN (insn);
1571 int old_code = INSN_CODE (insn);
1572 rtx old_notes = REG_NOTES (insn);
1573 int did_elimination = 0;
1574 int operands_changed = 0;
1575 rtx set = single_set (insn);
1577 /* Skip insns that only set an equivalence. */
1578 if (set && REG_P (SET_DEST (set))
1579 && reg_renumber[REGNO (SET_DEST (set))] < 0
1580 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1581 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1582 && reg_equiv_init[REGNO (SET_DEST (set))])
1583 continue;
1585 /* If needed, eliminate any eliminable registers. */
1586 if (num_eliminable || num_eliminable_invariants)
1587 did_elimination = eliminate_regs_in_insn (insn, 0);
1589 /* Analyze the instruction. */
1590 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1591 global, spill_reg_order);
1593 /* If a no-op set needs more than one reload, this is likely
1594 to be something that needs input address reloads. We
1595 can't get rid of this cleanly later, and it is of no use
1596 anyway, so discard it now.
1597 We only do this when expensive_optimizations is enabled,
1598 since this complements reload inheritance / output
1599 reload deletion, and it can make debugging harder. */
1600 if (flag_expensive_optimizations && n_reloads > 1)
1602 rtx set = single_set (insn);
1603 if (set
1605 ((SET_SRC (set) == SET_DEST (set)
1606 && REG_P (SET_SRC (set))
1607 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1608 || (REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
1609 && reg_renumber [REGNO (SET_SRC (set))] < 0
1610 && reg_renumber [REGNO (SET_DEST (set))] < 0
1611 && reg_equiv_memory_loc[REGNO (SET_SRC (set))] != NULL
1612 && reg_equiv_memory_loc[REGNO (SET_DEST (set))] != NULL
1613 && rtx_equal_p (reg_equiv_memory_loc
1614 [REGNO (SET_SRC (set))],
1615 reg_equiv_memory_loc
1616 [REGNO (SET_DEST (set))]))))
1618 if (flag_ira)
1619 mark_memory_move_deletion (REGNO (SET_DEST (set)),
1620 REGNO (SET_SRC (set)));
1621 delete_insn (insn);
1622 /* Delete it from the reload chain. */
1623 if (chain->prev)
1624 chain->prev->next = next;
1625 else
1626 reload_insn_chain = next;
1627 if (next)
1628 next->prev = chain->prev;
1629 chain->next = unused_insn_chains;
1630 unused_insn_chains = chain;
1631 continue;
1634 if (num_eliminable)
1635 update_eliminable_offsets ();
1637 /* Remember for later shortcuts which insns had any reloads or
1638 register eliminations. */
1639 chain->need_elim = did_elimination;
1640 chain->need_reload = n_reloads > 0;
1641 chain->need_operand_change = operands_changed;
1643 /* Discard any register replacements done. */
1644 if (did_elimination)
1646 obstack_free (&reload_obstack, reload_insn_firstobj);
1647 PATTERN (insn) = old_body;
1648 INSN_CODE (insn) = old_code;
1649 REG_NOTES (insn) = old_notes;
1650 something_needs_elimination = 1;
1653 something_needs_operands_changed |= operands_changed;
1655 if (n_reloads != 0)
1657 copy_reloads (chain);
1658 *pprev_reload = chain;
1659 pprev_reload = &chain->next_need_reload;
1663 *pprev_reload = 0;
1666 /* Comparison function for qsort to decide which of two reloads
1667 should be handled first. *P1 and *P2 are the reload numbers. */
1669 static int
1670 reload_reg_class_lower (const void *r1p, const void *r2p)
1672 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1673 int t;
1675 /* Consider required reloads before optional ones. */
1676 t = rld[r1].optional - rld[r2].optional;
1677 if (t != 0)
1678 return t;
1680 /* Count all solitary classes before non-solitary ones. */
1681 t = ((reg_class_size[(int) rld[r2].class] == 1)
1682 - (reg_class_size[(int) rld[r1].class] == 1));
1683 if (t != 0)
1684 return t;
1686 /* Aside from solitaires, consider all multi-reg groups first. */
1687 t = rld[r2].nregs - rld[r1].nregs;
1688 if (t != 0)
1689 return t;
1691 /* Consider reloads in order of increasing reg-class number. */
1692 t = (int) rld[r1].class - (int) rld[r2].class;
1693 if (t != 0)
1694 return t;
1696 /* If reloads are equally urgent, sort by reload number,
1697 so that the results of qsort leave nothing to chance. */
1698 return r1 - r2;
1701 /* The cost of spilling each hard reg. */
1702 static int spill_cost[FIRST_PSEUDO_REGISTER];
1704 /* When spilling multiple hard registers, we use SPILL_COST for the first
1705 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1706 only the first hard reg for a multi-reg pseudo. */
1707 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1709 /* Map of hard regno to pseudo regno currently occupying the hard
1710 reg. */
1711 static int hard_regno_to_pseudo_regno [FIRST_PSEUDO_REGISTER];
1713 /* Update the spill cost arrays, considering that pseudo REG is live. */
1715 static void
1716 count_pseudo (int reg)
1718 int freq = REG_FREQ (reg);
1719 int r = reg_renumber[reg];
1720 int nregs;
1722 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1723 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1724 || (flag_ira && r < 0))
1725 return;
1727 SET_REGNO_REG_SET (&pseudos_counted, reg);
1729 gcc_assert (r >= 0);
1731 spill_add_cost[r] += freq;
1732 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1733 while (nregs-- > 0)
1735 hard_regno_to_pseudo_regno [r + nregs] = reg;
1736 spill_cost[r + nregs] += freq;
1740 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1741 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1743 static void
1744 order_regs_for_reload (struct insn_chain *chain)
1746 unsigned i;
1747 HARD_REG_SET used_by_pseudos;
1748 HARD_REG_SET used_by_pseudos2;
1749 reg_set_iterator rsi;
1751 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1753 memset (spill_cost, 0, sizeof spill_cost);
1754 memset (spill_add_cost, 0, sizeof spill_add_cost);
1755 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1756 hard_regno_to_pseudo_regno [i] = -1;
1758 /* Count number of uses of each hard reg by pseudo regs allocated to it
1759 and then order them by decreasing use. First exclude hard registers
1760 that are live in or across this insn. */
1762 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1763 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1764 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1765 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1767 /* Now find out which pseudos are allocated to it, and update
1768 hard_reg_n_uses. */
1769 CLEAR_REG_SET (&pseudos_counted);
1771 EXECUTE_IF_SET_IN_REG_SET
1772 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1774 count_pseudo (i);
1776 EXECUTE_IF_SET_IN_REG_SET
1777 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1779 count_pseudo (i);
1781 CLEAR_REG_SET (&pseudos_counted);
1784 /* Vector of reload-numbers showing the order in which the reloads should
1785 be processed. */
1786 static short reload_order[MAX_RELOADS];
1788 /* This is used to keep track of the spill regs used in one insn. */
1789 static HARD_REG_SET used_spill_regs_local;
1791 /* We decided to spill hard register SPILLED, which has a size of
1792 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1793 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1794 update SPILL_COST/SPILL_ADD_COST. */
1796 static void
1797 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1799 int freq = REG_FREQ (reg);
1800 int r = reg_renumber[reg];
1801 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1803 if ((flag_ira && r < 0)
1804 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1805 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1806 return;
1808 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1810 spill_add_cost[r] -= freq;
1811 while (nregs-- > 0)
1813 hard_regno_to_pseudo_regno [r + nregs] = -1;
1814 spill_cost[r + nregs] -= freq;
1818 /* Find reload register to use for reload number ORDER. */
1820 static int
1821 find_reg (struct insn_chain *chain, int order)
1823 int rnum = reload_order[order];
1824 struct reload *rl = rld + rnum;
1825 int best_cost = INT_MAX;
1826 int best_reg = -1;
1827 unsigned int i, j, n;
1828 int k;
1829 HARD_REG_SET not_usable;
1830 HARD_REG_SET used_by_other_reload;
1831 reg_set_iterator rsi;
1832 static int regno_pseudo_regs [FIRST_PSEUDO_REGISTER];
1833 static int best_regno_pseudo_regs [FIRST_PSEUDO_REGISTER];
1835 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1836 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1837 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->class]);
1839 CLEAR_HARD_REG_SET (used_by_other_reload);
1840 for (k = 0; k < order; k++)
1842 int other = reload_order[k];
1844 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1845 for (j = 0; j < rld[other].nregs; j++)
1846 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1849 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1851 unsigned int regno = i;
1853 if (! TEST_HARD_REG_BIT (not_usable, regno)
1854 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1855 && HARD_REGNO_MODE_OK (regno, rl->mode))
1857 int this_cost = spill_cost[regno];
1858 int ok = 1;
1859 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1861 for (j = 1; j < this_nregs; j++)
1863 this_cost += spill_add_cost[regno + j];
1864 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1865 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1866 ok = 0;
1868 if (! ok)
1869 continue;
1871 if (flag_ira)
1873 for (n = j = 0; j < this_nregs; j++)
1875 int r = hard_regno_to_pseudo_regno [regno + j];
1877 if (r < 0)
1878 continue;
1879 if (n == 0 || regno_pseudo_regs [n - 1] != r)
1880 regno_pseudo_regs [n++] = r;
1882 regno_pseudo_regs [n++] = -1;
1883 if (best_reg < 0
1884 || better_spill_reload_regno_p (regno_pseudo_regs,
1885 best_regno_pseudo_regs,
1886 rl->in, rl->out, chain->insn))
1888 best_reg = regno;
1889 for (j = 0;; j++)
1891 best_regno_pseudo_regs [j] = regno_pseudo_regs [j];
1892 if (regno_pseudo_regs [j] < 0)
1893 break;
1896 continue;
1899 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1900 this_cost--;
1901 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1902 this_cost--;
1903 if (this_cost < best_cost
1904 /* Among registers with equal cost, prefer caller-saved ones, or
1905 use REG_ALLOC_ORDER if it is defined. */
1906 || (this_cost == best_cost
1907 #ifdef REG_ALLOC_ORDER
1908 && (inv_reg_alloc_order[regno]
1909 < inv_reg_alloc_order[best_reg])
1910 #else
1911 && call_used_regs[regno]
1912 && ! call_used_regs[best_reg]
1913 #endif
1916 best_reg = regno;
1917 best_cost = this_cost;
1921 if (best_reg == -1)
1922 return 0;
1924 if (dump_file)
1925 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1927 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1928 rl->regno = best_reg;
1930 EXECUTE_IF_SET_IN_REG_SET
1931 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1933 count_spilled_pseudo (best_reg, rl->nregs, j);
1936 EXECUTE_IF_SET_IN_REG_SET
1937 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1939 count_spilled_pseudo (best_reg, rl->nregs, j);
1942 for (i = 0; i < rl->nregs; i++)
1944 gcc_assert (spill_cost[best_reg + i] == 0);
1945 gcc_assert (spill_add_cost[best_reg + i] == 0);
1946 gcc_assert (hard_regno_to_pseudo_regno [best_reg + i] == -1);
1947 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1949 return 1;
1952 /* Find more reload regs to satisfy the remaining need of an insn, which
1953 is given by CHAIN.
1954 Do it by ascending class number, since otherwise a reg
1955 might be spilled for a big class and might fail to count
1956 for a smaller class even though it belongs to that class. */
1958 static void
1959 find_reload_regs (struct insn_chain *chain)
1961 int i;
1963 /* In order to be certain of getting the registers we need,
1964 we must sort the reloads into order of increasing register class.
1965 Then our grabbing of reload registers will parallel the process
1966 that provided the reload registers. */
1967 for (i = 0; i < chain->n_reloads; i++)
1969 /* Show whether this reload already has a hard reg. */
1970 if (chain->rld[i].reg_rtx)
1972 int regno = REGNO (chain->rld[i].reg_rtx);
1973 chain->rld[i].regno = regno;
1974 chain->rld[i].nregs
1975 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
1977 else
1978 chain->rld[i].regno = -1;
1979 reload_order[i] = i;
1982 n_reloads = chain->n_reloads;
1983 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
1985 CLEAR_HARD_REG_SET (used_spill_regs_local);
1987 if (dump_file)
1988 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
1990 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
1992 /* Compute the order of preference for hard registers to spill. */
1994 order_regs_for_reload (chain);
1996 for (i = 0; i < n_reloads; i++)
1998 int r = reload_order[i];
2000 /* Ignore reloads that got marked inoperative. */
2001 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
2002 && ! rld[r].optional
2003 && rld[r].regno == -1)
2004 if (! find_reg (chain, i))
2006 if (dump_file)
2007 fprintf (dump_file, "reload failure for reload %d\n", r);
2008 spill_failure (chain->insn, rld[r].class);
2009 failure = 1;
2010 return;
2014 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
2015 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
2017 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
2020 static void
2021 select_reload_regs (void)
2023 struct insn_chain *chain;
2025 /* Try to satisfy the needs for each insn. */
2026 for (chain = insns_need_reload; chain != 0;
2027 chain = chain->next_need_reload)
2028 find_reload_regs (chain);
2031 /* Delete all insns that were inserted by emit_caller_save_insns during
2032 this iteration. */
2033 static void
2034 delete_caller_save_insns (void)
2036 struct insn_chain *c = reload_insn_chain;
2038 while (c != 0)
2040 while (c != 0 && c->is_caller_save_insn)
2042 struct insn_chain *next = c->next;
2043 rtx insn = c->insn;
2045 if (c == reload_insn_chain)
2046 reload_insn_chain = next;
2047 delete_insn (insn);
2049 if (next)
2050 next->prev = c->prev;
2051 if (c->prev)
2052 c->prev->next = next;
2053 c->next = unused_insn_chains;
2054 unused_insn_chains = c;
2055 c = next;
2057 if (c != 0)
2058 c = c->next;
2062 /* Handle the failure to find a register to spill.
2063 INSN should be one of the insns which needed this particular spill reg. */
2065 static void
2066 spill_failure (rtx insn, enum reg_class class)
2068 if (asm_noperands (PATTERN (insn)) >= 0)
2069 error_for_asm (insn, "can't find a register in class %qs while "
2070 "reloading %<asm%>",
2071 reg_class_names[class]);
2072 else
2074 error ("unable to find a register to spill in class %qs",
2075 reg_class_names[class]);
2077 if (dump_file)
2079 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2080 debug_reload_to_stream (dump_file);
2082 fatal_insn ("this is the insn:", insn);
2086 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2087 data that is dead in INSN. */
2089 static void
2090 delete_dead_insn (rtx insn)
2092 rtx prev = prev_real_insn (insn);
2093 rtx prev_dest;
2095 /* If the previous insn sets a register that dies in our insn, delete it
2096 too. */
2097 if (prev && GET_CODE (PATTERN (prev)) == SET
2098 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2099 && reg_mentioned_p (prev_dest, PATTERN (insn))
2100 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2101 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2102 delete_dead_insn (prev);
2104 SET_INSN_DELETED (insn);
2107 /* Modify the home of pseudo-reg I.
2108 The new home is present in reg_renumber[I].
2110 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2111 or it may be -1, meaning there is none or it is not relevant.
2112 This is used so that all pseudos spilled from a given hard reg
2113 can share one stack slot. */
2115 static void
2116 alter_reg (int i, int from_reg, bool dont_share_p)
2118 /* When outputting an inline function, this can happen
2119 for a reg that isn't actually used. */
2120 if (regno_reg_rtx[i] == 0)
2121 return;
2123 /* If the reg got changed to a MEM at rtl-generation time,
2124 ignore it. */
2125 if (!REG_P (regno_reg_rtx[i]))
2126 return;
2128 /* Modify the reg-rtx to contain the new hard reg
2129 number or else to contain its pseudo reg number. */
2130 SET_REGNO (regno_reg_rtx[i],
2131 reg_renumber[i] >= 0 ? reg_renumber[i] : i);
2133 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2134 allocate a stack slot for it. */
2136 if (reg_renumber[i] < 0
2137 && REG_N_REFS (i) > 0
2138 && reg_equiv_constant[i] == 0
2139 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
2140 && reg_equiv_memory_loc[i] == 0)
2142 rtx x;
2143 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2144 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2145 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2146 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2147 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2148 int adjust = 0;
2149 bool shared_p = false;
2151 if (flag_ira)
2152 SET_REGNO_REG_SET (&spilled_pseudos, i);
2153 x = (dont_share_p || ! flag_ira
2154 ? NULL_RTX : reuse_stack_slot (i, inherent_size, total_size));
2155 if (x)
2156 shared_p = true;
2157 /* Each pseudo reg has an inherent size which comes from its own mode,
2158 and a total size which provides room for paradoxical subregs
2159 which refer to the pseudo reg in wider modes.
2161 We can use a slot already allocated if it provides both
2162 enough inherent space and enough total space.
2163 Otherwise, we allocate a new slot, making sure that it has no less
2164 inherent space, and no less total space, then the previous slot. */
2165 else if (from_reg == -1 || (! dont_share_p && flag_ira))
2167 alias_set_type alias_set = new_alias_set ();
2169 /* No known place to spill from => no slot to reuse. */
2170 x = assign_stack_local (mode, total_size,
2171 min_align > inherent_align
2172 || total_size > inherent_size ? -1 : 0);
2173 if (BYTES_BIG_ENDIAN)
2174 /* Cancel the big-endian correction done in assign_stack_local.
2175 Get the address of the beginning of the slot.
2176 This is so we can do a big-endian correction unconditionally
2177 below. */
2178 adjust = inherent_size - total_size;
2180 /* Nothing can alias this slot except this pseudo. */
2181 set_mem_alias_set (x, alias_set);
2182 dse_record_singleton_alias_set (alias_set, mode);
2184 if (! dont_share_p && flag_ira)
2185 mark_new_stack_slot (x, i, total_size);
2188 /* Reuse a stack slot if possible. */
2189 else if (spill_stack_slot[from_reg] != 0
2190 && spill_stack_slot_width[from_reg] >= total_size
2191 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2192 >= inherent_size)
2193 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2194 x = spill_stack_slot[from_reg];
2195 /* Allocate a bigger slot. */
2196 else
2198 /* Compute maximum size needed, both for inherent size
2199 and for total size. */
2200 rtx stack_slot;
2202 if (spill_stack_slot[from_reg])
2204 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2205 > inherent_size)
2206 mode = GET_MODE (spill_stack_slot[from_reg]);
2207 if (spill_stack_slot_width[from_reg] > total_size)
2208 total_size = spill_stack_slot_width[from_reg];
2209 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2210 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2213 /* Make a slot with that size. */
2214 x = assign_stack_local (mode, total_size,
2215 min_align > inherent_align
2216 || total_size > inherent_size ? -1 : 0);
2217 stack_slot = x;
2219 /* All pseudos mapped to this slot can alias each other. */
2220 if (spill_stack_slot[from_reg])
2222 alias_set_type alias_set
2223 = MEM_ALIAS_SET (spill_stack_slot[from_reg]);
2224 set_mem_alias_set (x, alias_set);
2225 dse_invalidate_singleton_alias_set (alias_set);
2227 else
2229 alias_set_type alias_set = new_alias_set ();
2230 set_mem_alias_set (x, alias_set);
2231 dse_record_singleton_alias_set (alias_set, mode);
2234 if (BYTES_BIG_ENDIAN)
2236 /* Cancel the big-endian correction done in assign_stack_local.
2237 Get the address of the beginning of the slot.
2238 This is so we can do a big-endian correction unconditionally
2239 below. */
2240 adjust = GET_MODE_SIZE (mode) - total_size;
2241 if (adjust)
2242 stack_slot
2243 = adjust_address_nv (x, mode_for_size (total_size
2244 * BITS_PER_UNIT,
2245 MODE_INT, 1),
2246 adjust);
2249 spill_stack_slot[from_reg] = stack_slot;
2250 spill_stack_slot_width[from_reg] = total_size;
2253 /* On a big endian machine, the "address" of the slot
2254 is the address of the low part that fits its inherent mode. */
2255 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2256 adjust += (total_size - inherent_size);
2258 /* If we have any adjustment to make, or if the stack slot is the
2259 wrong mode, make a new stack slot. */
2260 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2262 /* If we have a decl for the original register, set it for the
2263 memory. If this is a shared MEM, make a copy. */
2264 if (shared_p)
2266 x = copy_rtx (x);
2267 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2269 else if (REG_EXPR (regno_reg_rtx[i])
2270 && DECL_P (REG_EXPR (regno_reg_rtx[i])))
2272 rtx decl = DECL_RTL_IF_SET (REG_EXPR (regno_reg_rtx[i]));
2274 /* We can do this only for the DECLs home pseudo, not for
2275 any copies of it, since otherwise when the stack slot
2276 is reused, nonoverlapping_memrefs_p might think they
2277 cannot overlap. */
2278 if (decl && REG_P (decl) && REGNO (decl) == (unsigned) i)
2280 if (from_reg != -1 && spill_stack_slot[from_reg] == x)
2281 x = copy_rtx (x);
2283 set_mem_attrs_from_reg (x, regno_reg_rtx[i]);
2287 /* Save the stack slot for later. */
2288 reg_equiv_memory_loc[i] = x;
2292 /* Mark the slots in regs_ever_live for the hard regs used by
2293 pseudo-reg number REGNO, accessed in MODE. */
2295 static void
2296 mark_home_live_1 (int regno, enum machine_mode mode)
2298 int i, lim;
2300 i = reg_renumber[regno];
2301 if (i < 0)
2302 return;
2303 lim = end_hard_regno (mode, i);
2304 while (i < lim)
2305 df_set_regs_ever_live(i++, true);
2308 /* Mark the slots in regs_ever_live for the hard regs
2309 used by pseudo-reg number REGNO. */
2311 void
2312 mark_home_live (int regno)
2314 if (reg_renumber[regno] >= 0)
2315 mark_home_live_1 (regno, PSEUDO_REGNO_MODE (regno));
2318 /* This function handles the tracking of elimination offsets around branches.
2320 X is a piece of RTL being scanned.
2322 INSN is the insn that it came from, if any.
2324 INITIAL_P is nonzero if we are to set the offset to be the initial
2325 offset and zero if we are setting the offset of the label to be the
2326 current offset. */
2328 static void
2329 set_label_offsets (rtx x, rtx insn, int initial_p)
2331 enum rtx_code code = GET_CODE (x);
2332 rtx tem;
2333 unsigned int i;
2334 struct elim_table *p;
2336 switch (code)
2338 case LABEL_REF:
2339 if (LABEL_REF_NONLOCAL_P (x))
2340 return;
2342 x = XEXP (x, 0);
2344 /* ... fall through ... */
2346 case CODE_LABEL:
2347 /* If we know nothing about this label, set the desired offsets. Note
2348 that this sets the offset at a label to be the offset before a label
2349 if we don't know anything about the label. This is not correct for
2350 the label after a BARRIER, but is the best guess we can make. If
2351 we guessed wrong, we will suppress an elimination that might have
2352 been possible had we been able to guess correctly. */
2354 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2356 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2357 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2358 = (initial_p ? reg_eliminate[i].initial_offset
2359 : reg_eliminate[i].offset);
2360 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2363 /* Otherwise, if this is the definition of a label and it is
2364 preceded by a BARRIER, set our offsets to the known offset of
2365 that label. */
2367 else if (x == insn
2368 && (tem = prev_nonnote_insn (insn)) != 0
2369 && BARRIER_P (tem))
2370 set_offsets_for_label (insn);
2371 else
2372 /* If neither of the above cases is true, compare each offset
2373 with those previously recorded and suppress any eliminations
2374 where the offsets disagree. */
2376 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2377 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2378 != (initial_p ? reg_eliminate[i].initial_offset
2379 : reg_eliminate[i].offset))
2380 reg_eliminate[i].can_eliminate = 0;
2382 return;
2384 case JUMP_INSN:
2385 set_label_offsets (PATTERN (insn), insn, initial_p);
2387 /* ... fall through ... */
2389 case INSN:
2390 case CALL_INSN:
2391 /* Any labels mentioned in REG_LABEL_OPERAND notes can be branched
2392 to indirectly and hence must have all eliminations at their
2393 initial offsets. */
2394 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2395 if (REG_NOTE_KIND (tem) == REG_LABEL_OPERAND)
2396 set_label_offsets (XEXP (tem, 0), insn, 1);
2397 return;
2399 case PARALLEL:
2400 case ADDR_VEC:
2401 case ADDR_DIFF_VEC:
2402 /* Each of the labels in the parallel or address vector must be
2403 at their initial offsets. We want the first field for PARALLEL
2404 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2406 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2407 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2408 insn, initial_p);
2409 return;
2411 case SET:
2412 /* We only care about setting PC. If the source is not RETURN,
2413 IF_THEN_ELSE, or a label, disable any eliminations not at
2414 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2415 isn't one of those possibilities. For branches to a label,
2416 call ourselves recursively.
2418 Note that this can disable elimination unnecessarily when we have
2419 a non-local goto since it will look like a non-constant jump to
2420 someplace in the current function. This isn't a significant
2421 problem since such jumps will normally be when all elimination
2422 pairs are back to their initial offsets. */
2424 if (SET_DEST (x) != pc_rtx)
2425 return;
2427 switch (GET_CODE (SET_SRC (x)))
2429 case PC:
2430 case RETURN:
2431 return;
2433 case LABEL_REF:
2434 set_label_offsets (SET_SRC (x), insn, initial_p);
2435 return;
2437 case IF_THEN_ELSE:
2438 tem = XEXP (SET_SRC (x), 1);
2439 if (GET_CODE (tem) == LABEL_REF)
2440 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2441 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2442 break;
2444 tem = XEXP (SET_SRC (x), 2);
2445 if (GET_CODE (tem) == LABEL_REF)
2446 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2447 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2448 break;
2449 return;
2451 default:
2452 break;
2455 /* If we reach here, all eliminations must be at their initial
2456 offset because we are doing a jump to a variable address. */
2457 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2458 if (p->offset != p->initial_offset)
2459 p->can_eliminate = 0;
2460 break;
2462 default:
2463 break;
2467 /* Scan X and replace any eliminable registers (such as fp) with a
2468 replacement (such as sp), plus an offset.
2470 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2471 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2472 MEM, we are allowed to replace a sum of a register and the constant zero
2473 with the register, which we cannot do outside a MEM. In addition, we need
2474 to record the fact that a register is referenced outside a MEM.
2476 If INSN is an insn, it is the insn containing X. If we replace a REG
2477 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2478 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2479 the REG is being modified.
2481 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2482 That's used when we eliminate in expressions stored in notes.
2483 This means, do not set ref_outside_mem even if the reference
2484 is outside of MEMs.
2486 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2487 replacements done assuming all offsets are at their initial values. If
2488 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2489 encounter, return the actual location so that find_reloads will do
2490 the proper thing. */
2492 static rtx
2493 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2494 bool may_use_invariant)
2496 enum rtx_code code = GET_CODE (x);
2497 struct elim_table *ep;
2498 int regno;
2499 rtx new;
2500 int i, j;
2501 const char *fmt;
2502 int copied = 0;
2504 if (! current_function_decl)
2505 return x;
2507 switch (code)
2509 case CONST_INT:
2510 case CONST_DOUBLE:
2511 case CONST_FIXED:
2512 case CONST_VECTOR:
2513 case CONST:
2514 case SYMBOL_REF:
2515 case CODE_LABEL:
2516 case PC:
2517 case CC0:
2518 case ASM_INPUT:
2519 case ADDR_VEC:
2520 case ADDR_DIFF_VEC:
2521 case RETURN:
2522 return x;
2524 case REG:
2525 regno = REGNO (x);
2527 /* First handle the case where we encounter a bare register that
2528 is eliminable. Replace it with a PLUS. */
2529 if (regno < FIRST_PSEUDO_REGISTER)
2531 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2532 ep++)
2533 if (ep->from_rtx == x && ep->can_eliminate)
2534 return plus_constant (ep->to_rtx, ep->previous_offset);
2537 else if (reg_renumber && reg_renumber[regno] < 0
2538 && reg_equiv_invariant && reg_equiv_invariant[regno])
2540 if (may_use_invariant)
2541 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2542 mem_mode, insn, true);
2543 /* There exists at least one use of REGNO that cannot be
2544 eliminated. Prevent the defining insn from being deleted. */
2545 reg_equiv_init[regno] = NULL_RTX;
2546 alter_reg (regno, -1, true);
2548 return x;
2550 /* You might think handling MINUS in a manner similar to PLUS is a
2551 good idea. It is not. It has been tried multiple times and every
2552 time the change has had to have been reverted.
2554 Other parts of reload know a PLUS is special (gen_reload for example)
2555 and require special code to handle code a reloaded PLUS operand.
2557 Also consider backends where the flags register is clobbered by a
2558 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2559 lea instruction comes to mind). If we try to reload a MINUS, we
2560 may kill the flags register that was holding a useful value.
2562 So, please before trying to handle MINUS, consider reload as a
2563 whole instead of this little section as well as the backend issues. */
2564 case PLUS:
2565 /* If this is the sum of an eliminable register and a constant, rework
2566 the sum. */
2567 if (REG_P (XEXP (x, 0))
2568 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2569 && CONSTANT_P (XEXP (x, 1)))
2571 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2572 ep++)
2573 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2575 /* The only time we want to replace a PLUS with a REG (this
2576 occurs when the constant operand of the PLUS is the negative
2577 of the offset) is when we are inside a MEM. We won't want
2578 to do so at other times because that would change the
2579 structure of the insn in a way that reload can't handle.
2580 We special-case the commonest situation in
2581 eliminate_regs_in_insn, so just replace a PLUS with a
2582 PLUS here, unless inside a MEM. */
2583 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2584 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2585 return ep->to_rtx;
2586 else
2587 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2588 plus_constant (XEXP (x, 1),
2589 ep->previous_offset));
2592 /* If the register is not eliminable, we are done since the other
2593 operand is a constant. */
2594 return x;
2597 /* If this is part of an address, we want to bring any constant to the
2598 outermost PLUS. We will do this by doing register replacement in
2599 our operands and seeing if a constant shows up in one of them.
2601 Note that there is no risk of modifying the structure of the insn,
2602 since we only get called for its operands, thus we are either
2603 modifying the address inside a MEM, or something like an address
2604 operand of a load-address insn. */
2607 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2608 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2610 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2612 /* If one side is a PLUS and the other side is a pseudo that
2613 didn't get a hard register but has a reg_equiv_constant,
2614 we must replace the constant here since it may no longer
2615 be in the position of any operand. */
2616 if (GET_CODE (new0) == PLUS && REG_P (new1)
2617 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2618 && reg_renumber[REGNO (new1)] < 0
2619 && reg_equiv_constant != 0
2620 && reg_equiv_constant[REGNO (new1)] != 0)
2621 new1 = reg_equiv_constant[REGNO (new1)];
2622 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2623 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2624 && reg_renumber[REGNO (new0)] < 0
2625 && reg_equiv_constant[REGNO (new0)] != 0)
2626 new0 = reg_equiv_constant[REGNO (new0)];
2628 new = form_sum (new0, new1);
2630 /* As above, if we are not inside a MEM we do not want to
2631 turn a PLUS into something else. We might try to do so here
2632 for an addition of 0 if we aren't optimizing. */
2633 if (! mem_mode && GET_CODE (new) != PLUS)
2634 return gen_rtx_PLUS (GET_MODE (x), new, const0_rtx);
2635 else
2636 return new;
2639 return x;
2641 case MULT:
2642 /* If this is the product of an eliminable register and a
2643 constant, apply the distribute law and move the constant out
2644 so that we have (plus (mult ..) ..). This is needed in order
2645 to keep load-address insns valid. This case is pathological.
2646 We ignore the possibility of overflow here. */
2647 if (REG_P (XEXP (x, 0))
2648 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2649 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2650 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2651 ep++)
2652 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2654 if (! mem_mode
2655 /* Refs inside notes don't count for this purpose. */
2656 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2657 || GET_CODE (insn) == INSN_LIST)))
2658 ep->ref_outside_mem = 1;
2660 return
2661 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2662 ep->previous_offset * INTVAL (XEXP (x, 1)));
2665 /* ... fall through ... */
2667 case CALL:
2668 case COMPARE:
2669 /* See comments before PLUS about handling MINUS. */
2670 case MINUS:
2671 case DIV: case UDIV:
2672 case MOD: case UMOD:
2673 case AND: case IOR: case XOR:
2674 case ROTATERT: case ROTATE:
2675 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2676 case NE: case EQ:
2677 case GE: case GT: case GEU: case GTU:
2678 case LE: case LT: case LEU: case LTU:
2680 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2681 rtx new1 = XEXP (x, 1)
2682 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false) : 0;
2684 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2685 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2687 return x;
2689 case EXPR_LIST:
2690 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2691 if (XEXP (x, 0))
2693 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2694 if (new != XEXP (x, 0))
2696 /* If this is a REG_DEAD note, it is not valid anymore.
2697 Using the eliminated version could result in creating a
2698 REG_DEAD note for the stack or frame pointer. */
2699 if (GET_MODE (x) == REG_DEAD)
2700 return (XEXP (x, 1)
2701 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true)
2702 : NULL_RTX);
2704 x = gen_rtx_EXPR_LIST (REG_NOTE_KIND (x), new, XEXP (x, 1));
2708 /* ... fall through ... */
2710 case INSN_LIST:
2711 /* Now do eliminations in the rest of the chain. If this was
2712 an EXPR_LIST, this might result in allocating more memory than is
2713 strictly needed, but it simplifies the code. */
2714 if (XEXP (x, 1))
2716 new = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2717 if (new != XEXP (x, 1))
2718 return
2719 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new);
2721 return x;
2723 case PRE_INC:
2724 case POST_INC:
2725 case PRE_DEC:
2726 case POST_DEC:
2727 /* We do not support elimination of a register that is modified.
2728 elimination_effects has already make sure that this does not
2729 happen. */
2730 return x;
2732 case PRE_MODIFY:
2733 case POST_MODIFY:
2734 /* We do not support elimination of a register that is modified.
2735 elimination_effects has already make sure that this does not
2736 happen. The only remaining case we need to consider here is
2737 that the increment value may be an eliminable register. */
2738 if (GET_CODE (XEXP (x, 1)) == PLUS
2739 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2741 rtx new = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2742 insn, true);
2744 if (new != XEXP (XEXP (x, 1), 1))
2745 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2746 gen_rtx_PLUS (GET_MODE (x),
2747 XEXP (x, 0), new));
2749 return x;
2751 case STRICT_LOW_PART:
2752 case NEG: case NOT:
2753 case SIGN_EXTEND: case ZERO_EXTEND:
2754 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2755 case FLOAT: case FIX:
2756 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2757 case ABS:
2758 case SQRT:
2759 case FFS:
2760 case CLZ:
2761 case CTZ:
2762 case POPCOUNT:
2763 case PARITY:
2764 case BSWAP:
2765 new = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2766 if (new != XEXP (x, 0))
2767 return gen_rtx_fmt_e (code, GET_MODE (x), new);
2768 return x;
2770 case SUBREG:
2771 /* Similar to above processing, but preserve SUBREG_BYTE.
2772 Convert (subreg (mem)) to (mem) if not paradoxical.
2773 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2774 pseudo didn't get a hard reg, we must replace this with the
2775 eliminated version of the memory location because push_reload
2776 may do the replacement in certain circumstances. */
2777 if (REG_P (SUBREG_REG (x))
2778 && (GET_MODE_SIZE (GET_MODE (x))
2779 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2780 && reg_equiv_memory_loc != 0
2781 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2783 new = SUBREG_REG (x);
2785 else
2786 new = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false);
2788 if (new != SUBREG_REG (x))
2790 int x_size = GET_MODE_SIZE (GET_MODE (x));
2791 int new_size = GET_MODE_SIZE (GET_MODE (new));
2793 if (MEM_P (new)
2794 && ((x_size < new_size
2795 #ifdef WORD_REGISTER_OPERATIONS
2796 /* On these machines, combine can create rtl of the form
2797 (set (subreg:m1 (reg:m2 R) 0) ...)
2798 where m1 < m2, and expects something interesting to
2799 happen to the entire word. Moreover, it will use the
2800 (reg:m2 R) later, expecting all bits to be preserved.
2801 So if the number of words is the same, preserve the
2802 subreg so that push_reload can see it. */
2803 && ! ((x_size - 1) / UNITS_PER_WORD
2804 == (new_size -1 ) / UNITS_PER_WORD)
2805 #endif
2807 || x_size == new_size)
2809 return adjust_address_nv (new, GET_MODE (x), SUBREG_BYTE (x));
2810 else
2811 return gen_rtx_SUBREG (GET_MODE (x), new, SUBREG_BYTE (x));
2814 return x;
2816 case MEM:
2817 /* Our only special processing is to pass the mode of the MEM to our
2818 recursive call and copy the flags. While we are here, handle this
2819 case more efficiently. */
2820 return
2821 replace_equiv_address_nv (x,
2822 eliminate_regs_1 (XEXP (x, 0), GET_MODE (x),
2823 insn, true));
2825 case USE:
2826 /* Handle insn_list USE that a call to a pure function may generate. */
2827 new = eliminate_regs_1 (XEXP (x, 0), 0, insn, false);
2828 if (new != XEXP (x, 0))
2829 return gen_rtx_USE (GET_MODE (x), new);
2830 return x;
2832 case CLOBBER:
2833 case ASM_OPERANDS:
2834 case SET:
2835 gcc_unreachable ();
2837 default:
2838 break;
2841 /* Process each of our operands recursively. If any have changed, make a
2842 copy of the rtx. */
2843 fmt = GET_RTX_FORMAT (code);
2844 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2846 if (*fmt == 'e')
2848 new = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false);
2849 if (new != XEXP (x, i) && ! copied)
2851 x = shallow_copy_rtx (x);
2852 copied = 1;
2854 XEXP (x, i) = new;
2856 else if (*fmt == 'E')
2858 int copied_vec = 0;
2859 for (j = 0; j < XVECLEN (x, i); j++)
2861 new = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false);
2862 if (new != XVECEXP (x, i, j) && ! copied_vec)
2864 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2865 XVEC (x, i)->elem);
2866 if (! copied)
2868 x = shallow_copy_rtx (x);
2869 copied = 1;
2871 XVEC (x, i) = new_v;
2872 copied_vec = 1;
2874 XVECEXP (x, i, j) = new;
2879 return x;
2883 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2885 return eliminate_regs_1 (x, mem_mode, insn, false);
2888 /* Scan rtx X for modifications of elimination target registers. Update
2889 the table of eliminables to reflect the changed state. MEM_MODE is
2890 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2892 static void
2893 elimination_effects (rtx x, enum machine_mode mem_mode)
2895 enum rtx_code code = GET_CODE (x);
2896 struct elim_table *ep;
2897 int regno;
2898 int i, j;
2899 const char *fmt;
2901 switch (code)
2903 case CONST_INT:
2904 case CONST_DOUBLE:
2905 case CONST_FIXED:
2906 case CONST_VECTOR:
2907 case CONST:
2908 case SYMBOL_REF:
2909 case CODE_LABEL:
2910 case PC:
2911 case CC0:
2912 case ASM_INPUT:
2913 case ADDR_VEC:
2914 case ADDR_DIFF_VEC:
2915 case RETURN:
2916 return;
2918 case REG:
2919 regno = REGNO (x);
2921 /* First handle the case where we encounter a bare register that
2922 is eliminable. Replace it with a PLUS. */
2923 if (regno < FIRST_PSEUDO_REGISTER)
2925 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2926 ep++)
2927 if (ep->from_rtx == x && ep->can_eliminate)
2929 if (! mem_mode)
2930 ep->ref_outside_mem = 1;
2931 return;
2935 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2936 && reg_equiv_constant[regno]
2937 && ! function_invariant_p (reg_equiv_constant[regno]))
2938 elimination_effects (reg_equiv_constant[regno], mem_mode);
2939 return;
2941 case PRE_INC:
2942 case POST_INC:
2943 case PRE_DEC:
2944 case POST_DEC:
2945 case POST_MODIFY:
2946 case PRE_MODIFY:
2947 /* If we modify the source of an elimination rule, disable it. */
2948 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2949 if (ep->from_rtx == XEXP (x, 0))
2950 ep->can_eliminate = 0;
2952 /* If we modify the target of an elimination rule by adding a constant,
2953 update its offset. If we modify the target in any other way, we'll
2954 have to disable the rule as well. */
2955 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2956 if (ep->to_rtx == XEXP (x, 0))
2958 int size = GET_MODE_SIZE (mem_mode);
2960 /* If more bytes than MEM_MODE are pushed, account for them. */
2961 #ifdef PUSH_ROUNDING
2962 if (ep->to_rtx == stack_pointer_rtx)
2963 size = PUSH_ROUNDING (size);
2964 #endif
2965 if (code == PRE_DEC || code == POST_DEC)
2966 ep->offset += size;
2967 else if (code == PRE_INC || code == POST_INC)
2968 ep->offset -= size;
2969 else if (code == PRE_MODIFY || code == POST_MODIFY)
2971 if (GET_CODE (XEXP (x, 1)) == PLUS
2972 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
2973 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
2974 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
2975 else
2976 ep->can_eliminate = 0;
2980 /* These two aren't unary operators. */
2981 if (code == POST_MODIFY || code == PRE_MODIFY)
2982 break;
2984 /* Fall through to generic unary operation case. */
2985 case STRICT_LOW_PART:
2986 case NEG: case NOT:
2987 case SIGN_EXTEND: case ZERO_EXTEND:
2988 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2989 case FLOAT: case FIX:
2990 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2991 case ABS:
2992 case SQRT:
2993 case FFS:
2994 case CLZ:
2995 case CTZ:
2996 case POPCOUNT:
2997 case PARITY:
2998 case BSWAP:
2999 elimination_effects (XEXP (x, 0), mem_mode);
3000 return;
3002 case SUBREG:
3003 if (REG_P (SUBREG_REG (x))
3004 && (GET_MODE_SIZE (GET_MODE (x))
3005 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3006 && reg_equiv_memory_loc != 0
3007 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3008 return;
3010 elimination_effects (SUBREG_REG (x), mem_mode);
3011 return;
3013 case USE:
3014 /* If using a register that is the source of an eliminate we still
3015 think can be performed, note it cannot be performed since we don't
3016 know how this register is used. */
3017 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3018 if (ep->from_rtx == XEXP (x, 0))
3019 ep->can_eliminate = 0;
3021 elimination_effects (XEXP (x, 0), mem_mode);
3022 return;
3024 case CLOBBER:
3025 /* If clobbering a register that is the replacement register for an
3026 elimination we still think can be performed, note that it cannot
3027 be performed. Otherwise, we need not be concerned about it. */
3028 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3029 if (ep->to_rtx == XEXP (x, 0))
3030 ep->can_eliminate = 0;
3032 elimination_effects (XEXP (x, 0), mem_mode);
3033 return;
3035 case SET:
3036 /* Check for setting a register that we know about. */
3037 if (REG_P (SET_DEST (x)))
3039 /* See if this is setting the replacement register for an
3040 elimination.
3042 If DEST is the hard frame pointer, we do nothing because we
3043 assume that all assignments to the frame pointer are for
3044 non-local gotos and are being done at a time when they are valid
3045 and do not disturb anything else. Some machines want to
3046 eliminate a fake argument pointer (or even a fake frame pointer)
3047 with either the real frame or the stack pointer. Assignments to
3048 the hard frame pointer must not prevent this elimination. */
3050 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3051 ep++)
3052 if (ep->to_rtx == SET_DEST (x)
3053 && SET_DEST (x) != hard_frame_pointer_rtx)
3055 /* If it is being incremented, adjust the offset. Otherwise,
3056 this elimination can't be done. */
3057 rtx src = SET_SRC (x);
3059 if (GET_CODE (src) == PLUS
3060 && XEXP (src, 0) == SET_DEST (x)
3061 && GET_CODE (XEXP (src, 1)) == CONST_INT)
3062 ep->offset -= INTVAL (XEXP (src, 1));
3063 else
3064 ep->can_eliminate = 0;
3068 elimination_effects (SET_DEST (x), 0);
3069 elimination_effects (SET_SRC (x), 0);
3070 return;
3072 case MEM:
3073 /* Our only special processing is to pass the mode of the MEM to our
3074 recursive call. */
3075 elimination_effects (XEXP (x, 0), GET_MODE (x));
3076 return;
3078 default:
3079 break;
3082 fmt = GET_RTX_FORMAT (code);
3083 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3085 if (*fmt == 'e')
3086 elimination_effects (XEXP (x, i), mem_mode);
3087 else if (*fmt == 'E')
3088 for (j = 0; j < XVECLEN (x, i); j++)
3089 elimination_effects (XVECEXP (x, i, j), mem_mode);
3093 /* Descend through rtx X and verify that no references to eliminable registers
3094 remain. If any do remain, mark the involved register as not
3095 eliminable. */
3097 static void
3098 check_eliminable_occurrences (rtx x)
3100 const char *fmt;
3101 int i;
3102 enum rtx_code code;
3104 if (x == 0)
3105 return;
3107 code = GET_CODE (x);
3109 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3111 struct elim_table *ep;
3113 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3114 if (ep->from_rtx == x)
3115 ep->can_eliminate = 0;
3116 return;
3119 fmt = GET_RTX_FORMAT (code);
3120 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3122 if (*fmt == 'e')
3123 check_eliminable_occurrences (XEXP (x, i));
3124 else if (*fmt == 'E')
3126 int j;
3127 for (j = 0; j < XVECLEN (x, i); j++)
3128 check_eliminable_occurrences (XVECEXP (x, i, j));
3133 /* Scan INSN and eliminate all eliminable registers in it.
3135 If REPLACE is nonzero, do the replacement destructively. Also
3136 delete the insn as dead it if it is setting an eliminable register.
3138 If REPLACE is zero, do all our allocations in reload_obstack.
3140 If no eliminations were done and this insn doesn't require any elimination
3141 processing (these are not identical conditions: it might be updating sp,
3142 but not referencing fp; this needs to be seen during reload_as_needed so
3143 that the offset between fp and sp can be taken into consideration), zero
3144 is returned. Otherwise, 1 is returned. */
3146 static int
3147 eliminate_regs_in_insn (rtx insn, int replace)
3149 int icode = recog_memoized (insn);
3150 rtx old_body = PATTERN (insn);
3151 int insn_is_asm = asm_noperands (old_body) >= 0;
3152 rtx old_set = single_set (insn);
3153 rtx new_body;
3154 int val = 0;
3155 int i;
3156 rtx substed_operand[MAX_RECOG_OPERANDS];
3157 rtx orig_operand[MAX_RECOG_OPERANDS];
3158 struct elim_table *ep;
3159 rtx plus_src, plus_cst_src;
3161 if (! insn_is_asm && icode < 0)
3163 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3164 || GET_CODE (PATTERN (insn)) == CLOBBER
3165 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3166 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3167 || GET_CODE (PATTERN (insn)) == ASM_INPUT);
3168 return 0;
3171 if (old_set != 0 && REG_P (SET_DEST (old_set))
3172 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3174 /* Check for setting an eliminable register. */
3175 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3176 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3178 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3179 /* If this is setting the frame pointer register to the
3180 hardware frame pointer register and this is an elimination
3181 that will be done (tested above), this insn is really
3182 adjusting the frame pointer downward to compensate for
3183 the adjustment done before a nonlocal goto. */
3184 if (ep->from == FRAME_POINTER_REGNUM
3185 && ep->to == HARD_FRAME_POINTER_REGNUM)
3187 rtx base = SET_SRC (old_set);
3188 rtx base_insn = insn;
3189 HOST_WIDE_INT offset = 0;
3191 while (base != ep->to_rtx)
3193 rtx prev_insn, prev_set;
3195 if (GET_CODE (base) == PLUS
3196 && GET_CODE (XEXP (base, 1)) == CONST_INT)
3198 offset += INTVAL (XEXP (base, 1));
3199 base = XEXP (base, 0);
3201 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3202 && (prev_set = single_set (prev_insn)) != 0
3203 && rtx_equal_p (SET_DEST (prev_set), base))
3205 base = SET_SRC (prev_set);
3206 base_insn = prev_insn;
3208 else
3209 break;
3212 if (base == ep->to_rtx)
3214 rtx src
3215 = plus_constant (ep->to_rtx, offset - ep->offset);
3217 new_body = old_body;
3218 if (! replace)
3220 new_body = copy_insn (old_body);
3221 if (REG_NOTES (insn))
3222 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3224 PATTERN (insn) = new_body;
3225 old_set = single_set (insn);
3227 /* First see if this insn remains valid when we
3228 make the change. If not, keep the INSN_CODE
3229 the same and let reload fit it up. */
3230 validate_change (insn, &SET_SRC (old_set), src, 1);
3231 validate_change (insn, &SET_DEST (old_set),
3232 ep->to_rtx, 1);
3233 if (! apply_change_group ())
3235 SET_SRC (old_set) = src;
3236 SET_DEST (old_set) = ep->to_rtx;
3239 val = 1;
3240 goto done;
3243 #endif
3245 /* In this case this insn isn't serving a useful purpose. We
3246 will delete it in reload_as_needed once we know that this
3247 elimination is, in fact, being done.
3249 If REPLACE isn't set, we can't delete this insn, but needn't
3250 process it since it won't be used unless something changes. */
3251 if (replace)
3253 delete_dead_insn (insn);
3254 return 1;
3256 val = 1;
3257 goto done;
3261 /* We allow one special case which happens to work on all machines we
3262 currently support: a single set with the source or a REG_EQUAL
3263 note being a PLUS of an eliminable register and a constant. */
3264 plus_src = plus_cst_src = 0;
3265 if (old_set && REG_P (SET_DEST (old_set)))
3267 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3268 plus_src = SET_SRC (old_set);
3269 /* First see if the source is of the form (plus (...) CST). */
3270 if (plus_src
3271 && GET_CODE (XEXP (plus_src, 1)) == CONST_INT)
3272 plus_cst_src = plus_src;
3273 else if (REG_P (SET_SRC (old_set))
3274 || plus_src)
3276 /* Otherwise, see if we have a REG_EQUAL note of the form
3277 (plus (...) CST). */
3278 rtx links;
3279 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3281 if ((REG_NOTE_KIND (links) == REG_EQUAL
3282 || REG_NOTE_KIND (links) == REG_EQUIV)
3283 && GET_CODE (XEXP (links, 0)) == PLUS
3284 && GET_CODE (XEXP (XEXP (links, 0), 1)) == CONST_INT)
3286 plus_cst_src = XEXP (links, 0);
3287 break;
3292 /* Check that the first operand of the PLUS is a hard reg or
3293 the lowpart subreg of one. */
3294 if (plus_cst_src)
3296 rtx reg = XEXP (plus_cst_src, 0);
3297 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3298 reg = SUBREG_REG (reg);
3300 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3301 plus_cst_src = 0;
3304 if (plus_cst_src)
3306 rtx reg = XEXP (plus_cst_src, 0);
3307 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3309 if (GET_CODE (reg) == SUBREG)
3310 reg = SUBREG_REG (reg);
3312 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3313 if (ep->from_rtx == reg && ep->can_eliminate)
3315 rtx to_rtx = ep->to_rtx;
3316 offset += ep->offset;
3317 offset = trunc_int_for_mode (offset, GET_MODE (reg));
3319 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3320 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3321 to_rtx);
3322 /* If we have a nonzero offset, and the source is already
3323 a simple REG, the following transformation would
3324 increase the cost of the insn by replacing a simple REG
3325 with (plus (reg sp) CST). So try only when we already
3326 had a PLUS before. */
3327 if (offset == 0 || plus_src)
3329 rtx new_src = plus_constant (to_rtx, offset);
3331 new_body = old_body;
3332 if (! replace)
3334 new_body = copy_insn (old_body);
3335 if (REG_NOTES (insn))
3336 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3338 PATTERN (insn) = new_body;
3339 old_set = single_set (insn);
3341 /* First see if this insn remains valid when we make the
3342 change. If not, try to replace the whole pattern with
3343 a simple set (this may help if the original insn was a
3344 PARALLEL that was only recognized as single_set due to
3345 REG_UNUSED notes). If this isn't valid either, keep
3346 the INSN_CODE the same and let reload fix it up. */
3347 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3349 rtx new_pat = gen_rtx_SET (VOIDmode,
3350 SET_DEST (old_set), new_src);
3352 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3353 SET_SRC (old_set) = new_src;
3356 else
3357 break;
3359 val = 1;
3360 /* This can't have an effect on elimination offsets, so skip right
3361 to the end. */
3362 goto done;
3366 /* Determine the effects of this insn on elimination offsets. */
3367 elimination_effects (old_body, 0);
3369 /* Eliminate all eliminable registers occurring in operands that
3370 can be handled by reload. */
3371 extract_insn (insn);
3372 for (i = 0; i < recog_data.n_operands; i++)
3374 orig_operand[i] = recog_data.operand[i];
3375 substed_operand[i] = recog_data.operand[i];
3377 /* For an asm statement, every operand is eliminable. */
3378 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3380 bool is_set_src, in_plus;
3382 /* Check for setting a register that we know about. */
3383 if (recog_data.operand_type[i] != OP_IN
3384 && REG_P (orig_operand[i]))
3386 /* If we are assigning to a register that can be eliminated, it
3387 must be as part of a PARALLEL, since the code above handles
3388 single SETs. We must indicate that we can no longer
3389 eliminate this reg. */
3390 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3391 ep++)
3392 if (ep->from_rtx == orig_operand[i])
3393 ep->can_eliminate = 0;
3396 /* Companion to the above plus substitution, we can allow
3397 invariants as the source of a plain move. */
3398 is_set_src = false;
3399 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3400 is_set_src = true;
3401 in_plus = false;
3402 if (plus_src
3403 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3404 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3405 in_plus = true;
3407 substed_operand[i]
3408 = eliminate_regs_1 (recog_data.operand[i], 0,
3409 replace ? insn : NULL_RTX,
3410 is_set_src || in_plus);
3411 if (substed_operand[i] != orig_operand[i])
3412 val = 1;
3413 /* Terminate the search in check_eliminable_occurrences at
3414 this point. */
3415 *recog_data.operand_loc[i] = 0;
3417 /* If an output operand changed from a REG to a MEM and INSN is an
3418 insn, write a CLOBBER insn. */
3419 if (recog_data.operand_type[i] != OP_IN
3420 && REG_P (orig_operand[i])
3421 && MEM_P (substed_operand[i])
3422 && replace)
3423 emit_insn_after (gen_rtx_CLOBBER (VOIDmode, orig_operand[i]),
3424 insn);
3428 for (i = 0; i < recog_data.n_dups; i++)
3429 *recog_data.dup_loc[i]
3430 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3432 /* If any eliminable remain, they aren't eliminable anymore. */
3433 check_eliminable_occurrences (old_body);
3435 /* Substitute the operands; the new values are in the substed_operand
3436 array. */
3437 for (i = 0; i < recog_data.n_operands; i++)
3438 *recog_data.operand_loc[i] = substed_operand[i];
3439 for (i = 0; i < recog_data.n_dups; i++)
3440 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3442 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3443 re-recognize the insn. We do this in case we had a simple addition
3444 but now can do this as a load-address. This saves an insn in this
3445 common case.
3446 If re-recognition fails, the old insn code number will still be used,
3447 and some register operands may have changed into PLUS expressions.
3448 These will be handled by find_reloads by loading them into a register
3449 again. */
3451 if (val)
3453 /* If we aren't replacing things permanently and we changed something,
3454 make another copy to ensure that all the RTL is new. Otherwise
3455 things can go wrong if find_reload swaps commutative operands
3456 and one is inside RTL that has been copied while the other is not. */
3457 new_body = old_body;
3458 if (! replace)
3460 new_body = copy_insn (old_body);
3461 if (REG_NOTES (insn))
3462 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3464 PATTERN (insn) = new_body;
3466 /* If we had a move insn but now we don't, rerecognize it. This will
3467 cause spurious re-recognition if the old move had a PARALLEL since
3468 the new one still will, but we can't call single_set without
3469 having put NEW_BODY into the insn and the re-recognition won't
3470 hurt in this rare case. */
3471 /* ??? Why this huge if statement - why don't we just rerecognize the
3472 thing always? */
3473 if (! insn_is_asm
3474 && old_set != 0
3475 && ((REG_P (SET_SRC (old_set))
3476 && (GET_CODE (new_body) != SET
3477 || !REG_P (SET_SRC (new_body))))
3478 /* If this was a load from or store to memory, compare
3479 the MEM in recog_data.operand to the one in the insn.
3480 If they are not equal, then rerecognize the insn. */
3481 || (old_set != 0
3482 && ((MEM_P (SET_SRC (old_set))
3483 && SET_SRC (old_set) != recog_data.operand[1])
3484 || (MEM_P (SET_DEST (old_set))
3485 && SET_DEST (old_set) != recog_data.operand[0])))
3486 /* If this was an add insn before, rerecognize. */
3487 || GET_CODE (SET_SRC (old_set)) == PLUS))
3489 int new_icode = recog (PATTERN (insn), insn, 0);
3490 if (new_icode >= 0)
3491 INSN_CODE (insn) = new_icode;
3495 /* Restore the old body. If there were any changes to it, we made a copy
3496 of it while the changes were still in place, so we'll correctly return
3497 a modified insn below. */
3498 if (! replace)
3500 /* Restore the old body. */
3501 for (i = 0; i < recog_data.n_operands; i++)
3502 *recog_data.operand_loc[i] = orig_operand[i];
3503 for (i = 0; i < recog_data.n_dups; i++)
3504 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3507 /* Update all elimination pairs to reflect the status after the current
3508 insn. The changes we make were determined by the earlier call to
3509 elimination_effects.
3511 We also detect cases where register elimination cannot be done,
3512 namely, if a register would be both changed and referenced outside a MEM
3513 in the resulting insn since such an insn is often undefined and, even if
3514 not, we cannot know what meaning will be given to it. Note that it is
3515 valid to have a register used in an address in an insn that changes it
3516 (presumably with a pre- or post-increment or decrement).
3518 If anything changes, return nonzero. */
3520 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3522 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3523 ep->can_eliminate = 0;
3525 ep->ref_outside_mem = 0;
3527 if (ep->previous_offset != ep->offset)
3528 val = 1;
3531 done:
3532 /* If we changed something, perform elimination in REG_NOTES. This is
3533 needed even when REPLACE is zero because a REG_DEAD note might refer
3534 to a register that we eliminate and could cause a different number
3535 of spill registers to be needed in the final reload pass than in
3536 the pre-passes. */
3537 if (val && REG_NOTES (insn) != 0)
3538 REG_NOTES (insn)
3539 = eliminate_regs_1 (REG_NOTES (insn), 0, REG_NOTES (insn), true);
3541 return val;
3544 /* Loop through all elimination pairs.
3545 Recalculate the number not at initial offset.
3547 Compute the maximum offset (minimum offset if the stack does not
3548 grow downward) for each elimination pair. */
3550 static void
3551 update_eliminable_offsets (void)
3553 struct elim_table *ep;
3555 num_not_at_initial_offset = 0;
3556 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3558 ep->previous_offset = ep->offset;
3559 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3560 num_not_at_initial_offset++;
3564 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3565 replacement we currently believe is valid, mark it as not eliminable if X
3566 modifies DEST in any way other than by adding a constant integer to it.
3568 If DEST is the frame pointer, we do nothing because we assume that
3569 all assignments to the hard frame pointer are nonlocal gotos and are being
3570 done at a time when they are valid and do not disturb anything else.
3571 Some machines want to eliminate a fake argument pointer with either the
3572 frame or stack pointer. Assignments to the hard frame pointer must not
3573 prevent this elimination.
3575 Called via note_stores from reload before starting its passes to scan
3576 the insns of the function. */
3578 static void
3579 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3581 unsigned int i;
3583 /* A SUBREG of a hard register here is just changing its mode. We should
3584 not see a SUBREG of an eliminable hard register, but check just in
3585 case. */
3586 if (GET_CODE (dest) == SUBREG)
3587 dest = SUBREG_REG (dest);
3589 if (dest == hard_frame_pointer_rtx)
3590 return;
3592 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3593 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3594 && (GET_CODE (x) != SET
3595 || GET_CODE (SET_SRC (x)) != PLUS
3596 || XEXP (SET_SRC (x), 0) != dest
3597 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3599 reg_eliminate[i].can_eliminate_previous
3600 = reg_eliminate[i].can_eliminate = 0;
3601 num_eliminable--;
3605 /* Verify that the initial elimination offsets did not change since the
3606 last call to set_initial_elim_offsets. This is used to catch cases
3607 where something illegal happened during reload_as_needed that could
3608 cause incorrect code to be generated if we did not check for it. */
3610 static bool
3611 verify_initial_elim_offsets (void)
3613 HOST_WIDE_INT t;
3615 if (!num_eliminable)
3616 return true;
3618 #ifdef ELIMINABLE_REGS
3620 struct elim_table *ep;
3622 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3624 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3625 if (t != ep->initial_offset)
3626 return false;
3629 #else
3630 INITIAL_FRAME_POINTER_OFFSET (t);
3631 if (t != reg_eliminate[0].initial_offset)
3632 return false;
3633 #endif
3635 return true;
3638 /* Reset all offsets on eliminable registers to their initial values. */
3640 static void
3641 set_initial_elim_offsets (void)
3643 struct elim_table *ep = reg_eliminate;
3645 #ifdef ELIMINABLE_REGS
3646 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3648 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3649 ep->previous_offset = ep->offset = ep->initial_offset;
3651 #else
3652 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3653 ep->previous_offset = ep->offset = ep->initial_offset;
3654 #endif
3656 num_not_at_initial_offset = 0;
3659 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3661 static void
3662 set_initial_eh_label_offset (rtx label)
3664 set_label_offsets (label, NULL_RTX, 1);
3667 /* Initialize the known label offsets.
3668 Set a known offset for each forced label to be at the initial offset
3669 of each elimination. We do this because we assume that all
3670 computed jumps occur from a location where each elimination is
3671 at its initial offset.
3672 For all other labels, show that we don't know the offsets. */
3674 static void
3675 set_initial_label_offsets (void)
3677 rtx x;
3678 memset (offsets_known_at, 0, num_labels);
3680 for (x = forced_labels; x; x = XEXP (x, 1))
3681 if (XEXP (x, 0))
3682 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3684 for_each_eh_label (set_initial_eh_label_offset);
3687 /* Set all elimination offsets to the known values for the code label given
3688 by INSN. */
3690 static void
3691 set_offsets_for_label (rtx insn)
3693 unsigned int i;
3694 int label_nr = CODE_LABEL_NUMBER (insn);
3695 struct elim_table *ep;
3697 num_not_at_initial_offset = 0;
3698 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3700 ep->offset = ep->previous_offset
3701 = offsets_at[label_nr - first_label_num][i];
3702 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3703 num_not_at_initial_offset++;
3707 /* See if anything that happened changes which eliminations are valid.
3708 For example, on the SPARC, whether or not the frame pointer can
3709 be eliminated can depend on what registers have been used. We need
3710 not check some conditions again (such as flag_omit_frame_pointer)
3711 since they can't have changed. */
3713 static void
3714 update_eliminables (HARD_REG_SET *pset)
3716 int previous_frame_pointer_needed = frame_pointer_needed;
3717 struct elim_table *ep;
3719 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3720 if ((ep->from == HARD_FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
3721 #ifdef ELIMINABLE_REGS
3722 || ! CAN_ELIMINATE (ep->from, ep->to)
3723 #endif
3725 ep->can_eliminate = 0;
3727 /* Look for the case where we have discovered that we can't replace
3728 register A with register B and that means that we will now be
3729 trying to replace register A with register C. This means we can
3730 no longer replace register C with register B and we need to disable
3731 such an elimination, if it exists. This occurs often with A == ap,
3732 B == sp, and C == fp. */
3734 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3736 struct elim_table *op;
3737 int new_to = -1;
3739 if (! ep->can_eliminate && ep->can_eliminate_previous)
3741 /* Find the current elimination for ep->from, if there is a
3742 new one. */
3743 for (op = reg_eliminate;
3744 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3745 if (op->from == ep->from && op->can_eliminate)
3747 new_to = op->to;
3748 break;
3751 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3752 disable it. */
3753 for (op = reg_eliminate;
3754 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3755 if (op->from == new_to && op->to == ep->to)
3756 op->can_eliminate = 0;
3760 /* See if any registers that we thought we could eliminate the previous
3761 time are no longer eliminable. If so, something has changed and we
3762 must spill the register. Also, recompute the number of eliminable
3763 registers and see if the frame pointer is needed; it is if there is
3764 no elimination of the frame pointer that we can perform. */
3766 frame_pointer_needed = 1;
3767 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3769 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM
3770 && ep->to != HARD_FRAME_POINTER_REGNUM)
3771 frame_pointer_needed = 0;
3773 if (! ep->can_eliminate && ep->can_eliminate_previous)
3775 ep->can_eliminate_previous = 0;
3776 SET_HARD_REG_BIT (*pset, ep->from);
3777 num_eliminable--;
3781 /* If we didn't need a frame pointer last time, but we do now, spill
3782 the hard frame pointer. */
3783 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3784 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3787 /* Return true if X is used as the target register of an elimination. */
3789 bool
3790 elimination_target_reg_p (rtx x)
3792 struct elim_table *ep;
3794 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3795 if (ep->to_rtx == x && ep->can_eliminate)
3796 return true;
3798 return false;
3801 /* Initialize the table of registers to eliminate. */
3803 static void
3804 init_elim_table (void)
3806 struct elim_table *ep;
3807 #ifdef ELIMINABLE_REGS
3808 const struct elim_table_1 *ep1;
3809 #endif
3811 if (!reg_eliminate)
3812 reg_eliminate = xcalloc (sizeof (struct elim_table), NUM_ELIMINABLE_REGS);
3814 /* Does this function require a frame pointer? */
3816 frame_pointer_needed = (! flag_omit_frame_pointer
3817 /* ?? If EXIT_IGNORE_STACK is set, we will not save
3818 and restore sp for alloca. So we can't eliminate
3819 the frame pointer in that case. At some point,
3820 we should improve this by emitting the
3821 sp-adjusting insns for this case. */
3822 || (current_function_calls_alloca
3823 && EXIT_IGNORE_STACK)
3824 || current_function_accesses_prior_frames
3825 || FRAME_POINTER_REQUIRED);
3827 num_eliminable = 0;
3829 #ifdef ELIMINABLE_REGS
3830 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3831 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3833 ep->from = ep1->from;
3834 ep->to = ep1->to;
3835 ep->can_eliminate = ep->can_eliminate_previous
3836 = (CAN_ELIMINATE (ep->from, ep->to)
3837 && ! (ep->to == STACK_POINTER_REGNUM && frame_pointer_needed));
3839 #else
3840 reg_eliminate[0].from = reg_eliminate_1[0].from;
3841 reg_eliminate[0].to = reg_eliminate_1[0].to;
3842 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3843 = ! frame_pointer_needed;
3844 #endif
3846 /* Count the number of eliminable registers and build the FROM and TO
3847 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3848 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3849 We depend on this. */
3850 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3852 num_eliminable += ep->can_eliminate;
3853 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3854 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3858 /* Kick all pseudos out of hard register REGNO.
3860 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3861 because we found we can't eliminate some register. In the case, no pseudos
3862 are allowed to be in the register, even if they are only in a block that
3863 doesn't require spill registers, unlike the case when we are spilling this
3864 hard reg to produce another spill register.
3866 Return nonzero if any pseudos needed to be kicked out. */
3868 static void
3869 spill_hard_reg (unsigned int regno, int cant_eliminate)
3871 int i;
3873 if (cant_eliminate)
3875 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3876 df_set_regs_ever_live (regno, true);
3879 /* Spill every pseudo reg that was allocated to this reg
3880 or to something that overlaps this reg. */
3882 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3883 if (reg_renumber[i] >= 0
3884 && (unsigned int) reg_renumber[i] <= regno
3885 && end_hard_regno (PSEUDO_REGNO_MODE (i), reg_renumber[i]) > regno)
3886 SET_REGNO_REG_SET (&spilled_pseudos, i);
3889 /* After find_reload_regs has been run for all insn that need reloads,
3890 and/or spill_hard_regs was called, this function is used to actually
3891 spill pseudo registers and try to reallocate them. It also sets up the
3892 spill_regs array for use by choose_reload_regs. */
3894 static int
3895 finish_spills (int global)
3897 struct insn_chain *chain;
3898 int something_changed = 0;
3899 unsigned i;
3900 reg_set_iterator rsi;
3902 /* Build the spill_regs array for the function. */
3903 /* If there are some registers still to eliminate and one of the spill regs
3904 wasn't ever used before, additional stack space may have to be
3905 allocated to store this register. Thus, we may have changed the offset
3906 between the stack and frame pointers, so mark that something has changed.
3908 One might think that we need only set VAL to 1 if this is a call-used
3909 register. However, the set of registers that must be saved by the
3910 prologue is not identical to the call-used set. For example, the
3911 register used by the call insn for the return PC is a call-used register,
3912 but must be saved by the prologue. */
3914 n_spills = 0;
3915 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3916 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3918 spill_reg_order[i] = n_spills;
3919 spill_regs[n_spills++] = i;
3920 if (num_eliminable && ! df_regs_ever_live_p (i))
3921 something_changed = 1;
3922 df_set_regs_ever_live (i, true);
3924 else
3925 spill_reg_order[i] = -1;
3927 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
3928 if (! flag_ira || reg_renumber[i] >= 0)
3930 /* Record the current hard register the pseudo is allocated to
3931 in pseudo_previous_regs so we avoid reallocating it to the
3932 same hard reg in a later pass. */
3933 gcc_assert (reg_renumber[i] >= 0);
3935 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3936 /* Mark it as no longer having a hard register home. */
3937 reg_renumber[i] = -1;
3938 if (flag_ira)
3939 mark_allocation_change (i);
3940 /* We will need to scan everything again. */
3941 something_changed = 1;
3944 /* Retry global register allocation if possible. */
3945 if (global)
3947 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
3948 /* For every insn that needs reloads, set the registers used as spill
3949 regs in pseudo_forbidden_regs for every pseudo live across the
3950 insn. */
3951 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
3953 EXECUTE_IF_SET_IN_REG_SET
3954 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
3956 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3957 chain->used_spill_regs);
3959 EXECUTE_IF_SET_IN_REG_SET
3960 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
3962 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
3963 chain->used_spill_regs);
3967 /* Retry allocating the spilled pseudos. For each reg, merge the
3968 various reg sets that indicate which hard regs can't be used,
3969 and call retry_global_alloc.
3970 We change spill_pseudos here to only contain pseudos that did not
3971 get a new hard register. */
3972 if (! flag_ira)
3974 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
3975 if (reg_old_renumber[i] != reg_renumber[i])
3977 HARD_REG_SET forbidden;
3979 COPY_HARD_REG_SET (forbidden, bad_spill_regs_global);
3980 IOR_HARD_REG_SET (forbidden, pseudo_forbidden_regs[i]);
3981 IOR_HARD_REG_SET (forbidden, pseudo_previous_regs[i]);
3982 retry_global_alloc (i, forbidden);
3983 if (reg_renumber[i] >= 0)
3984 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3987 else
3989 unsigned int n;
3991 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
3992 if (reg_old_renumber[i] != reg_renumber[i])
3994 if (reg_renumber [i] < 0)
3995 temp_pseudo_reg_arr [n++] = i;
3996 else
3997 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
3999 if (reassign_pseudos (temp_pseudo_reg_arr, n, bad_spill_regs_global,
4000 pseudo_forbidden_regs, pseudo_previous_regs,
4001 &spilled_pseudos))
4002 something_changed = 1;
4006 /* Fix up the register information in the insn chain.
4007 This involves deleting those of the spilled pseudos which did not get
4008 a new hard register home from the live_{before,after} sets. */
4009 for (chain = reload_insn_chain; chain; chain = chain->next)
4011 HARD_REG_SET used_by_pseudos;
4012 HARD_REG_SET used_by_pseudos2;
4014 if (! flag_ira)
4016 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
4017 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
4019 /* Mark any unallocated hard regs as available for spills. That
4020 makes inheritance work somewhat better. */
4021 if (chain->need_reload)
4023 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
4024 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
4025 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4027 /* Save the old value for the sanity test below. */
4028 COPY_HARD_REG_SET (used_by_pseudos2, chain->used_spill_regs);
4030 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
4031 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
4032 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4033 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4035 /* Make sure we only enlarge the set. */
4036 gcc_assert (hard_reg_set_subset_p (used_by_pseudos2,
4037 chain->used_spill_regs));
4041 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4042 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
4044 int regno = reg_renumber[i];
4045 if (reg_old_renumber[i] == regno)
4046 continue;
4048 alter_reg (i, reg_old_renumber[i], false);
4049 reg_old_renumber[i] = regno;
4050 if (dump_file)
4052 if (regno == -1)
4053 fprintf (dump_file, " Register %d now on stack.\n\n", i);
4054 else
4055 fprintf (dump_file, " Register %d now in %d.\n\n",
4056 i, reg_renumber[i]);
4060 return something_changed;
4063 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
4065 static void
4066 scan_paradoxical_subregs (rtx x)
4068 int i;
4069 const char *fmt;
4070 enum rtx_code code = GET_CODE (x);
4072 switch (code)
4074 case REG:
4075 case CONST_INT:
4076 case CONST:
4077 case SYMBOL_REF:
4078 case LABEL_REF:
4079 case CONST_DOUBLE:
4080 case CONST_FIXED:
4081 case CONST_VECTOR: /* shouldn't happen, but just in case. */
4082 case CC0:
4083 case PC:
4084 case USE:
4085 case CLOBBER:
4086 return;
4088 case SUBREG:
4089 if (REG_P (SUBREG_REG (x))
4090 && (GET_MODE_SIZE (GET_MODE (x))
4091 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
4093 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4094 = GET_MODE_SIZE (GET_MODE (x));
4095 mark_home_live_1 (REGNO (SUBREG_REG (x)), GET_MODE (x));
4097 return;
4099 default:
4100 break;
4103 fmt = GET_RTX_FORMAT (code);
4104 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4106 if (fmt[i] == 'e')
4107 scan_paradoxical_subregs (XEXP (x, i));
4108 else if (fmt[i] == 'E')
4110 int j;
4111 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4112 scan_paradoxical_subregs (XVECEXP (x, i, j));
4117 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
4118 examine all of the reload insns between PREV and NEXT exclusive, and
4119 annotate all that may trap. */
4121 static void
4122 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
4124 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4125 unsigned int trap_count;
4126 rtx i;
4128 if (note == NULL)
4129 return;
4131 if (may_trap_p (PATTERN (insn)))
4132 trap_count = 1;
4133 else
4135 remove_note (insn, note);
4136 trap_count = 0;
4139 for (i = NEXT_INSN (prev); i != next; i = NEXT_INSN (i))
4140 if (INSN_P (i) && i != insn && may_trap_p (PATTERN (i)))
4142 trap_count++;
4143 REG_NOTES (i)
4144 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (note, 0), REG_NOTES (i));
4148 /* Reload pseudo-registers into hard regs around each insn as needed.
4149 Additional register load insns are output before the insn that needs it
4150 and perhaps store insns after insns that modify the reloaded pseudo reg.
4152 reg_last_reload_reg and reg_reloaded_contents keep track of
4153 which registers are already available in reload registers.
4154 We update these for the reloads that we perform,
4155 as the insns are scanned. */
4157 static void
4158 reload_as_needed (int live_known)
4160 struct insn_chain *chain;
4161 #if defined (AUTO_INC_DEC)
4162 int i;
4163 #endif
4164 rtx x;
4166 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
4167 memset (spill_reg_store, 0, sizeof spill_reg_store);
4168 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
4169 INIT_REG_SET (&reg_has_output_reload);
4170 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4171 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
4173 set_initial_elim_offsets ();
4175 for (chain = reload_insn_chain; chain; chain = chain->next)
4177 rtx prev = 0;
4178 rtx insn = chain->insn;
4179 rtx old_next = NEXT_INSN (insn);
4181 /* If we pass a label, copy the offsets from the label information
4182 into the current offsets of each elimination. */
4183 if (LABEL_P (insn))
4184 set_offsets_for_label (insn);
4186 else if (INSN_P (insn))
4188 regset_head regs_to_forget;
4189 INIT_REG_SET (&regs_to_forget);
4190 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
4192 /* If this is a USE and CLOBBER of a MEM, ensure that any
4193 references to eliminable registers have been removed. */
4195 if ((GET_CODE (PATTERN (insn)) == USE
4196 || GET_CODE (PATTERN (insn)) == CLOBBER)
4197 && MEM_P (XEXP (PATTERN (insn), 0)))
4198 XEXP (XEXP (PATTERN (insn), 0), 0)
4199 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4200 GET_MODE (XEXP (PATTERN (insn), 0)),
4201 NULL_RTX);
4203 /* If we need to do register elimination processing, do so.
4204 This might delete the insn, in which case we are done. */
4205 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4207 eliminate_regs_in_insn (insn, 1);
4208 if (NOTE_P (insn))
4210 update_eliminable_offsets ();
4211 CLEAR_REG_SET (&regs_to_forget);
4212 continue;
4216 /* If need_elim is nonzero but need_reload is zero, one might think
4217 that we could simply set n_reloads to 0. However, find_reloads
4218 could have done some manipulation of the insn (such as swapping
4219 commutative operands), and these manipulations are lost during
4220 the first pass for every insn that needs register elimination.
4221 So the actions of find_reloads must be redone here. */
4223 if (! chain->need_elim && ! chain->need_reload
4224 && ! chain->need_operand_change)
4225 n_reloads = 0;
4226 /* First find the pseudo regs that must be reloaded for this insn.
4227 This info is returned in the tables reload_... (see reload.h).
4228 Also modify the body of INSN by substituting RELOAD
4229 rtx's for those pseudo regs. */
4230 else
4232 CLEAR_REG_SET (&reg_has_output_reload);
4233 CLEAR_HARD_REG_SET (reg_is_output_reload);
4235 find_reloads (insn, 1, spill_indirect_levels, live_known,
4236 spill_reg_order);
4239 if (n_reloads > 0)
4241 rtx next = NEXT_INSN (insn);
4242 rtx p;
4244 prev = PREV_INSN (insn);
4246 /* Now compute which reload regs to reload them into. Perhaps
4247 reusing reload regs from previous insns, or else output
4248 load insns to reload them. Maybe output store insns too.
4249 Record the choices of reload reg in reload_reg_rtx. */
4250 choose_reload_regs (chain);
4252 /* Merge any reloads that we didn't combine for fear of
4253 increasing the number of spill registers needed but now
4254 discover can be safely merged. */
4255 if (SMALL_REGISTER_CLASSES)
4256 merge_assigned_reloads (insn);
4258 /* Generate the insns to reload operands into or out of
4259 their reload regs. */
4260 emit_reload_insns (chain);
4262 /* Substitute the chosen reload regs from reload_reg_rtx
4263 into the insn's body (or perhaps into the bodies of other
4264 load and store insn that we just made for reloading
4265 and that we moved the structure into). */
4266 subst_reloads (insn);
4268 /* Adjust the exception region notes for loads and stores. */
4269 if (flag_non_call_exceptions && !CALL_P (insn))
4270 fixup_eh_region_note (insn, prev, next);
4272 /* If this was an ASM, make sure that all the reload insns
4273 we have generated are valid. If not, give an error
4274 and delete them. */
4275 if (asm_noperands (PATTERN (insn)) >= 0)
4276 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4277 if (p != insn && INSN_P (p)
4278 && GET_CODE (PATTERN (p)) != USE
4279 && (recog_memoized (p) < 0
4280 || (extract_insn (p), ! constrain_operands (1))))
4282 error_for_asm (insn,
4283 "%<asm%> operand requires "
4284 "impossible reload");
4285 delete_insn (p);
4289 if (num_eliminable && chain->need_elim)
4290 update_eliminable_offsets ();
4292 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4293 is no longer validly lying around to save a future reload.
4294 Note that this does not detect pseudos that were reloaded
4295 for this insn in order to be stored in
4296 (obeying register constraints). That is correct; such reload
4297 registers ARE still valid. */
4298 forget_marked_reloads (&regs_to_forget);
4299 CLEAR_REG_SET (&regs_to_forget);
4301 /* There may have been CLOBBER insns placed after INSN. So scan
4302 between INSN and NEXT and use them to forget old reloads. */
4303 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4304 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4305 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4307 #ifdef AUTO_INC_DEC
4308 /* Likewise for regs altered by auto-increment in this insn.
4309 REG_INC notes have been changed by reloading:
4310 find_reloads_address_1 records substitutions for them,
4311 which have been performed by subst_reloads above. */
4312 for (i = n_reloads - 1; i >= 0; i--)
4314 rtx in_reg = rld[i].in_reg;
4315 if (in_reg)
4317 enum rtx_code code = GET_CODE (in_reg);
4318 /* PRE_INC / PRE_DEC will have the reload register ending up
4319 with the same value as the stack slot, but that doesn't
4320 hold true for POST_INC / POST_DEC. Either we have to
4321 convert the memory access to a true POST_INC / POST_DEC,
4322 or we can't use the reload register for inheritance. */
4323 if ((code == POST_INC || code == POST_DEC)
4324 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4325 REGNO (rld[i].reg_rtx))
4326 /* Make sure it is the inc/dec pseudo, and not
4327 some other (e.g. output operand) pseudo. */
4328 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4329 == REGNO (XEXP (in_reg, 0))))
4332 rtx reload_reg = rld[i].reg_rtx;
4333 enum machine_mode mode = GET_MODE (reload_reg);
4334 int n = 0;
4335 rtx p;
4337 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4339 /* We really want to ignore REG_INC notes here, so
4340 use PATTERN (p) as argument to reg_set_p . */
4341 if (reg_set_p (reload_reg, PATTERN (p)))
4342 break;
4343 n = count_occurrences (PATTERN (p), reload_reg, 0);
4344 if (! n)
4345 continue;
4346 if (n == 1)
4348 n = validate_replace_rtx (reload_reg,
4349 gen_rtx_fmt_e (code,
4350 mode,
4351 reload_reg),
4354 /* We must also verify that the constraints
4355 are met after the replacement. */
4356 extract_insn (p);
4357 if (n)
4358 n = constrain_operands (1);
4359 else
4360 break;
4362 /* If the constraints were not met, then
4363 undo the replacement. */
4364 if (!n)
4366 validate_replace_rtx (gen_rtx_fmt_e (code,
4367 mode,
4368 reload_reg),
4369 reload_reg, p);
4370 break;
4374 break;
4376 if (n == 1)
4378 REG_NOTES (p)
4379 = gen_rtx_EXPR_LIST (REG_INC, reload_reg,
4380 REG_NOTES (p));
4381 /* Mark this as having an output reload so that the
4382 REG_INC processing code below won't invalidate
4383 the reload for inheritance. */
4384 SET_HARD_REG_BIT (reg_is_output_reload,
4385 REGNO (reload_reg));
4386 SET_REGNO_REG_SET (&reg_has_output_reload,
4387 REGNO (XEXP (in_reg, 0)));
4389 else
4390 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4391 NULL);
4393 else if ((code == PRE_INC || code == PRE_DEC)
4394 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4395 REGNO (rld[i].reg_rtx))
4396 /* Make sure it is the inc/dec pseudo, and not
4397 some other (e.g. output operand) pseudo. */
4398 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4399 == REGNO (XEXP (in_reg, 0))))
4401 SET_HARD_REG_BIT (reg_is_output_reload,
4402 REGNO (rld[i].reg_rtx));
4403 SET_REGNO_REG_SET (&reg_has_output_reload,
4404 REGNO (XEXP (in_reg, 0)));
4408 /* If a pseudo that got a hard register is auto-incremented,
4409 we must purge records of copying it into pseudos without
4410 hard registers. */
4411 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4412 if (REG_NOTE_KIND (x) == REG_INC)
4414 /* See if this pseudo reg was reloaded in this insn.
4415 If so, its last-reload info is still valid
4416 because it is based on this insn's reload. */
4417 for (i = 0; i < n_reloads; i++)
4418 if (rld[i].out == XEXP (x, 0))
4419 break;
4421 if (i == n_reloads)
4422 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4424 #endif
4426 /* A reload reg's contents are unknown after a label. */
4427 if (LABEL_P (insn))
4428 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4430 /* Don't assume a reload reg is still good after a call insn
4431 if it is a call-used reg, or if it contains a value that will
4432 be partially clobbered by the call. */
4433 else if (CALL_P (insn))
4435 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4436 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4440 /* Clean up. */
4441 free (reg_last_reload_reg);
4442 CLEAR_REG_SET (&reg_has_output_reload);
4445 /* Discard all record of any value reloaded from X,
4446 or reloaded in X from someplace else;
4447 unless X is an output reload reg of the current insn.
4449 X may be a hard reg (the reload reg)
4450 or it may be a pseudo reg that was reloaded from.
4452 When DATA is non-NULL just mark the registers in regset
4453 to be forgotten later. */
4455 static void
4456 forget_old_reloads_1 (rtx x, const_rtx ignored ATTRIBUTE_UNUSED,
4457 void *data)
4459 unsigned int regno;
4460 unsigned int nr;
4461 regset regs = (regset) data;
4463 /* note_stores does give us subregs of hard regs,
4464 subreg_regno_offset requires a hard reg. */
4465 while (GET_CODE (x) == SUBREG)
4467 /* We ignore the subreg offset when calculating the regno,
4468 because we are using the entire underlying hard register
4469 below. */
4470 x = SUBREG_REG (x);
4473 if (!REG_P (x))
4474 return;
4476 regno = REGNO (x);
4478 if (regno >= FIRST_PSEUDO_REGISTER)
4479 nr = 1;
4480 else
4482 unsigned int i;
4484 nr = hard_regno_nregs[regno][GET_MODE (x)];
4485 /* Storing into a spilled-reg invalidates its contents.
4486 This can happen if a block-local pseudo is allocated to that reg
4487 and it wasn't spilled because this block's total need is 0.
4488 Then some insn might have an optional reload and use this reg. */
4489 if (!regs)
4490 for (i = 0; i < nr; i++)
4491 /* But don't do this if the reg actually serves as an output
4492 reload reg in the current instruction. */
4493 if (n_reloads == 0
4494 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4496 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4497 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, regno + i);
4498 spill_reg_store[regno + i] = 0;
4502 if (regs)
4503 while (nr-- > 0)
4504 SET_REGNO_REG_SET (regs, regno + nr);
4505 else
4507 /* Since value of X has changed,
4508 forget any value previously copied from it. */
4510 while (nr-- > 0)
4511 /* But don't forget a copy if this is the output reload
4512 that establishes the copy's validity. */
4513 if (n_reloads == 0
4514 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4515 reg_last_reload_reg[regno + nr] = 0;
4519 /* Forget the reloads marked in regset by previous function. */
4520 static void
4521 forget_marked_reloads (regset regs)
4523 unsigned int reg;
4524 reg_set_iterator rsi;
4525 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4527 if (reg < FIRST_PSEUDO_REGISTER
4528 /* But don't do this if the reg actually serves as an output
4529 reload reg in the current instruction. */
4530 && (n_reloads == 0
4531 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4533 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4534 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, reg);
4535 spill_reg_store[reg] = 0;
4537 if (n_reloads == 0
4538 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4539 reg_last_reload_reg[reg] = 0;
4543 /* The following HARD_REG_SETs indicate when each hard register is
4544 used for a reload of various parts of the current insn. */
4546 /* If reg is unavailable for all reloads. */
4547 static HARD_REG_SET reload_reg_unavailable;
4548 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4549 static HARD_REG_SET reload_reg_used;
4550 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4551 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4552 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4553 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4554 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4555 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4556 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4557 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4558 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4559 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4560 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4561 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4562 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4563 static HARD_REG_SET reload_reg_used_in_op_addr;
4564 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4565 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4566 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4567 static HARD_REG_SET reload_reg_used_in_insn;
4568 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4569 static HARD_REG_SET reload_reg_used_in_other_addr;
4571 /* If reg is in use as a reload reg for any sort of reload. */
4572 static HARD_REG_SET reload_reg_used_at_all;
4574 /* If reg is use as an inherited reload. We just mark the first register
4575 in the group. */
4576 static HARD_REG_SET reload_reg_used_for_inherit;
4578 /* Records which hard regs are used in any way, either as explicit use or
4579 by being allocated to a pseudo during any point of the current insn. */
4580 static HARD_REG_SET reg_used_in_insn;
4582 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4583 TYPE. MODE is used to indicate how many consecutive regs are
4584 actually used. */
4586 static void
4587 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4588 enum machine_mode mode)
4590 unsigned int nregs = hard_regno_nregs[regno][mode];
4591 unsigned int i;
4593 for (i = regno; i < nregs + regno; i++)
4595 switch (type)
4597 case RELOAD_OTHER:
4598 SET_HARD_REG_BIT (reload_reg_used, i);
4599 break;
4601 case RELOAD_FOR_INPUT_ADDRESS:
4602 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4603 break;
4605 case RELOAD_FOR_INPADDR_ADDRESS:
4606 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4607 break;
4609 case RELOAD_FOR_OUTPUT_ADDRESS:
4610 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4611 break;
4613 case RELOAD_FOR_OUTADDR_ADDRESS:
4614 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4615 break;
4617 case RELOAD_FOR_OPERAND_ADDRESS:
4618 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4619 break;
4621 case RELOAD_FOR_OPADDR_ADDR:
4622 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4623 break;
4625 case RELOAD_FOR_OTHER_ADDRESS:
4626 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4627 break;
4629 case RELOAD_FOR_INPUT:
4630 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4631 break;
4633 case RELOAD_FOR_OUTPUT:
4634 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4635 break;
4637 case RELOAD_FOR_INSN:
4638 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4639 break;
4642 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4646 /* Similarly, but show REGNO is no longer in use for a reload. */
4648 static void
4649 clear_reload_reg_in_use (unsigned int regno, int opnum,
4650 enum reload_type type, enum machine_mode mode)
4652 unsigned int nregs = hard_regno_nregs[regno][mode];
4653 unsigned int start_regno, end_regno, r;
4654 int i;
4655 /* A complication is that for some reload types, inheritance might
4656 allow multiple reloads of the same types to share a reload register.
4657 We set check_opnum if we have to check only reloads with the same
4658 operand number, and check_any if we have to check all reloads. */
4659 int check_opnum = 0;
4660 int check_any = 0;
4661 HARD_REG_SET *used_in_set;
4663 switch (type)
4665 case RELOAD_OTHER:
4666 used_in_set = &reload_reg_used;
4667 break;
4669 case RELOAD_FOR_INPUT_ADDRESS:
4670 used_in_set = &reload_reg_used_in_input_addr[opnum];
4671 break;
4673 case RELOAD_FOR_INPADDR_ADDRESS:
4674 check_opnum = 1;
4675 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4676 break;
4678 case RELOAD_FOR_OUTPUT_ADDRESS:
4679 used_in_set = &reload_reg_used_in_output_addr[opnum];
4680 break;
4682 case RELOAD_FOR_OUTADDR_ADDRESS:
4683 check_opnum = 1;
4684 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4685 break;
4687 case RELOAD_FOR_OPERAND_ADDRESS:
4688 used_in_set = &reload_reg_used_in_op_addr;
4689 break;
4691 case RELOAD_FOR_OPADDR_ADDR:
4692 check_any = 1;
4693 used_in_set = &reload_reg_used_in_op_addr_reload;
4694 break;
4696 case RELOAD_FOR_OTHER_ADDRESS:
4697 used_in_set = &reload_reg_used_in_other_addr;
4698 check_any = 1;
4699 break;
4701 case RELOAD_FOR_INPUT:
4702 used_in_set = &reload_reg_used_in_input[opnum];
4703 break;
4705 case RELOAD_FOR_OUTPUT:
4706 used_in_set = &reload_reg_used_in_output[opnum];
4707 break;
4709 case RELOAD_FOR_INSN:
4710 used_in_set = &reload_reg_used_in_insn;
4711 break;
4712 default:
4713 gcc_unreachable ();
4715 /* We resolve conflicts with remaining reloads of the same type by
4716 excluding the intervals of reload registers by them from the
4717 interval of freed reload registers. Since we only keep track of
4718 one set of interval bounds, we might have to exclude somewhat
4719 more than what would be necessary if we used a HARD_REG_SET here.
4720 But this should only happen very infrequently, so there should
4721 be no reason to worry about it. */
4723 start_regno = regno;
4724 end_regno = regno + nregs;
4725 if (check_opnum || check_any)
4727 for (i = n_reloads - 1; i >= 0; i--)
4729 if (rld[i].when_needed == type
4730 && (check_any || rld[i].opnum == opnum)
4731 && rld[i].reg_rtx)
4733 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4734 unsigned int conflict_end
4735 = end_hard_regno (rld[i].mode, conflict_start);
4737 /* If there is an overlap with the first to-be-freed register,
4738 adjust the interval start. */
4739 if (conflict_start <= start_regno && conflict_end > start_regno)
4740 start_regno = conflict_end;
4741 /* Otherwise, if there is a conflict with one of the other
4742 to-be-freed registers, adjust the interval end. */
4743 if (conflict_start > start_regno && conflict_start < end_regno)
4744 end_regno = conflict_start;
4749 for (r = start_regno; r < end_regno; r++)
4750 CLEAR_HARD_REG_BIT (*used_in_set, r);
4753 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4754 specified by OPNUM and TYPE. */
4756 static int
4757 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4759 int i;
4761 /* In use for a RELOAD_OTHER means it's not available for anything. */
4762 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4763 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4764 return 0;
4766 switch (type)
4768 case RELOAD_OTHER:
4769 /* In use for anything means we can't use it for RELOAD_OTHER. */
4770 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4771 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4772 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4773 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4774 return 0;
4776 for (i = 0; i < reload_n_operands; i++)
4777 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4778 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4779 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4780 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4781 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4782 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4783 return 0;
4785 return 1;
4787 case RELOAD_FOR_INPUT:
4788 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4789 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4790 return 0;
4792 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4793 return 0;
4795 /* If it is used for some other input, can't use it. */
4796 for (i = 0; i < reload_n_operands; i++)
4797 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4798 return 0;
4800 /* If it is used in a later operand's address, can't use it. */
4801 for (i = opnum + 1; 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 return 0;
4806 return 1;
4808 case RELOAD_FOR_INPUT_ADDRESS:
4809 /* Can't use a register if it is used for an input address for this
4810 operand or used as an input in an earlier one. */
4811 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4812 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4813 return 0;
4815 for (i = 0; i < opnum; i++)
4816 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4817 return 0;
4819 return 1;
4821 case RELOAD_FOR_INPADDR_ADDRESS:
4822 /* Can't use a register if it is used for an input address
4823 for this operand or used as an input in an earlier
4824 one. */
4825 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4826 return 0;
4828 for (i = 0; i < opnum; i++)
4829 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4830 return 0;
4832 return 1;
4834 case RELOAD_FOR_OUTPUT_ADDRESS:
4835 /* Can't use a register if it is used for an output address for this
4836 operand or used as an output in this or a later operand. Note
4837 that multiple output operands are emitted in reverse order, so
4838 the conflicting ones are those with lower indices. */
4839 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4840 return 0;
4842 for (i = 0; i <= opnum; i++)
4843 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4844 return 0;
4846 return 1;
4848 case RELOAD_FOR_OUTADDR_ADDRESS:
4849 /* Can't use a register if it is used for an output address
4850 for this operand or used as an output in this or a
4851 later operand. Note that multiple output operands are
4852 emitted in reverse order, so the conflicting ones are
4853 those with lower indices. */
4854 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4855 return 0;
4857 for (i = 0; i <= opnum; i++)
4858 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4859 return 0;
4861 return 1;
4863 case RELOAD_FOR_OPERAND_ADDRESS:
4864 for (i = 0; i < reload_n_operands; i++)
4865 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4866 return 0;
4868 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4869 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4871 case RELOAD_FOR_OPADDR_ADDR:
4872 for (i = 0; i < reload_n_operands; i++)
4873 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4874 return 0;
4876 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4878 case RELOAD_FOR_OUTPUT:
4879 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4880 outputs, or an operand address for this or an earlier output.
4881 Note that multiple output operands are emitted in reverse order,
4882 so the conflicting ones are those with higher indices. */
4883 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4884 return 0;
4886 for (i = 0; i < reload_n_operands; i++)
4887 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4888 return 0;
4890 for (i = opnum; i < reload_n_operands; i++)
4891 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4892 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4893 return 0;
4895 return 1;
4897 case RELOAD_FOR_INSN:
4898 for (i = 0; i < reload_n_operands; i++)
4899 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4900 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4901 return 0;
4903 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4904 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4906 case RELOAD_FOR_OTHER_ADDRESS:
4907 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4909 default:
4910 gcc_unreachable ();
4914 /* Return 1 if the value in reload reg REGNO, as used by a reload
4915 needed for the part of the insn specified by OPNUM and TYPE,
4916 is still available in REGNO at the end of the insn.
4918 We can assume that the reload reg was already tested for availability
4919 at the time it is needed, and we should not check this again,
4920 in case the reg has already been marked in use. */
4922 static int
4923 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4925 int i;
4927 switch (type)
4929 case RELOAD_OTHER:
4930 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4931 its value must reach the end. */
4932 return 1;
4934 /* If this use is for part of the insn,
4935 its value reaches if no subsequent part uses the same register.
4936 Just like the above function, don't try to do this with lots
4937 of fallthroughs. */
4939 case RELOAD_FOR_OTHER_ADDRESS:
4940 /* Here we check for everything else, since these don't conflict
4941 with anything else and everything comes later. */
4943 for (i = 0; i < reload_n_operands; i++)
4944 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4945 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4946 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
4947 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4948 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4949 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4950 return 0;
4952 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4953 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4954 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4955 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
4957 case RELOAD_FOR_INPUT_ADDRESS:
4958 case RELOAD_FOR_INPADDR_ADDRESS:
4959 /* Similar, except that we check only for this and subsequent inputs
4960 and the address of only subsequent inputs and we do not need
4961 to check for RELOAD_OTHER objects since they are known not to
4962 conflict. */
4964 for (i = opnum; i < reload_n_operands; i++)
4965 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4966 return 0;
4968 for (i = opnum + 1; i < reload_n_operands; i++)
4969 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4970 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4971 return 0;
4973 for (i = 0; i < reload_n_operands; i++)
4974 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4975 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4976 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4977 return 0;
4979 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4980 return 0;
4982 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4983 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4984 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
4986 case RELOAD_FOR_INPUT:
4987 /* Similar to input address, except we start at the next operand for
4988 both input and input address and we do not check for
4989 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
4990 would conflict. */
4992 for (i = opnum + 1; i < reload_n_operands; i++)
4993 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4994 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4995 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4996 return 0;
4998 /* ... fall through ... */
5000 case RELOAD_FOR_OPERAND_ADDRESS:
5001 /* Check outputs and their addresses. */
5003 for (i = 0; i < reload_n_operands; i++)
5004 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5005 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5006 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5007 return 0;
5009 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
5011 case RELOAD_FOR_OPADDR_ADDR:
5012 for (i = 0; i < reload_n_operands; i++)
5013 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5014 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5015 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5016 return 0;
5018 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5019 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5020 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5022 case RELOAD_FOR_INSN:
5023 /* These conflict with other outputs with RELOAD_OTHER. So
5024 we need only check for output addresses. */
5026 opnum = reload_n_operands;
5028 /* ... fall through ... */
5030 case RELOAD_FOR_OUTPUT:
5031 case RELOAD_FOR_OUTPUT_ADDRESS:
5032 case RELOAD_FOR_OUTADDR_ADDRESS:
5033 /* We already know these can't conflict with a later output. So the
5034 only thing to check are later output addresses.
5035 Note that multiple output operands are emitted in reverse order,
5036 so the conflicting ones are those with lower indices. */
5037 for (i = 0; i < opnum; 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 return 0;
5042 return 1;
5044 default:
5045 gcc_unreachable ();
5050 /* Returns whether R1 and R2 are uniquely chained: the value of one
5051 is used by the other, and that value is not used by any other
5052 reload for this insn. This is used to partially undo the decision
5053 made in find_reloads when in the case of multiple
5054 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
5055 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
5056 reloads. This code tries to avoid the conflict created by that
5057 change. It might be cleaner to explicitly keep track of which
5058 RELOAD_FOR_OPADDR_ADDR reload is associated with which
5059 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
5060 this after the fact. */
5061 static bool
5062 reloads_unique_chain_p (int r1, int r2)
5064 int i;
5066 /* We only check input reloads. */
5067 if (! rld[r1].in || ! rld[r2].in)
5068 return false;
5070 /* Avoid anything with output reloads. */
5071 if (rld[r1].out || rld[r2].out)
5072 return false;
5074 /* "chained" means one reload is a component of the other reload,
5075 not the same as the other reload. */
5076 if (rld[r1].opnum != rld[r2].opnum
5077 || rtx_equal_p (rld[r1].in, rld[r2].in)
5078 || rld[r1].optional || rld[r2].optional
5079 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
5080 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
5081 return false;
5083 for (i = 0; i < n_reloads; i ++)
5084 /* Look for input reloads that aren't our two */
5085 if (i != r1 && i != r2 && rld[i].in)
5087 /* If our reload is mentioned at all, it isn't a simple chain. */
5088 if (reg_mentioned_p (rld[r1].in, rld[i].in))
5089 return false;
5091 return true;
5094 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5095 Return 0 otherwise.
5097 This function uses the same algorithm as reload_reg_free_p above. */
5099 static int
5100 reloads_conflict (int r1, int r2)
5102 enum reload_type r1_type = rld[r1].when_needed;
5103 enum reload_type r2_type = rld[r2].when_needed;
5104 int r1_opnum = rld[r1].opnum;
5105 int r2_opnum = rld[r2].opnum;
5107 /* RELOAD_OTHER conflicts with everything. */
5108 if (r2_type == RELOAD_OTHER)
5109 return 1;
5111 /* Otherwise, check conflicts differently for each type. */
5113 switch (r1_type)
5115 case RELOAD_FOR_INPUT:
5116 return (r2_type == RELOAD_FOR_INSN
5117 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5118 || r2_type == RELOAD_FOR_OPADDR_ADDR
5119 || r2_type == RELOAD_FOR_INPUT
5120 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5121 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5122 && r2_opnum > r1_opnum));
5124 case RELOAD_FOR_INPUT_ADDRESS:
5125 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5126 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5128 case RELOAD_FOR_INPADDR_ADDRESS:
5129 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5130 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5132 case RELOAD_FOR_OUTPUT_ADDRESS:
5133 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5134 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5136 case RELOAD_FOR_OUTADDR_ADDRESS:
5137 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5138 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5140 case RELOAD_FOR_OPERAND_ADDRESS:
5141 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5142 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
5143 && !reloads_unique_chain_p (r1, r2)));
5145 case RELOAD_FOR_OPADDR_ADDR:
5146 return (r2_type == RELOAD_FOR_INPUT
5147 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5149 case RELOAD_FOR_OUTPUT:
5150 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5151 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5152 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5153 && r2_opnum >= r1_opnum));
5155 case RELOAD_FOR_INSN:
5156 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5157 || r2_type == RELOAD_FOR_INSN
5158 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5160 case RELOAD_FOR_OTHER_ADDRESS:
5161 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5163 case RELOAD_OTHER:
5164 return 1;
5166 default:
5167 gcc_unreachable ();
5171 /* Indexed by reload number, 1 if incoming value
5172 inherited from previous insns. */
5173 static char reload_inherited[MAX_RELOADS];
5175 /* For an inherited reload, this is the insn the reload was inherited from,
5176 if we know it. Otherwise, this is 0. */
5177 static rtx reload_inheritance_insn[MAX_RELOADS];
5179 /* If nonzero, this is a place to get the value of the reload,
5180 rather than using reload_in. */
5181 static rtx reload_override_in[MAX_RELOADS];
5183 /* For each reload, the hard register number of the register used,
5184 or -1 if we did not need a register for this reload. */
5185 static int reload_spill_index[MAX_RELOADS];
5187 /* Subroutine of free_for_value_p, used to check a single register.
5188 START_REGNO is the starting regno of the full reload register
5189 (possibly comprising multiple hard registers) that we are considering. */
5191 static int
5192 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
5193 enum reload_type type, rtx value, rtx out,
5194 int reloadnum, int ignore_address_reloads)
5196 int time1;
5197 /* Set if we see an input reload that must not share its reload register
5198 with any new earlyclobber, but might otherwise share the reload
5199 register with an output or input-output reload. */
5200 int check_earlyclobber = 0;
5201 int i;
5202 int copy = 0;
5204 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5205 return 0;
5207 if (out == const0_rtx)
5209 copy = 1;
5210 out = NULL_RTX;
5213 /* We use some pseudo 'time' value to check if the lifetimes of the
5214 new register use would overlap with the one of a previous reload
5215 that is not read-only or uses a different value.
5216 The 'time' used doesn't have to be linear in any shape or form, just
5217 monotonic.
5218 Some reload types use different 'buckets' for each operand.
5219 So there are MAX_RECOG_OPERANDS different time values for each
5220 such reload type.
5221 We compute TIME1 as the time when the register for the prospective
5222 new reload ceases to be live, and TIME2 for each existing
5223 reload as the time when that the reload register of that reload
5224 becomes live.
5225 Where there is little to be gained by exact lifetime calculations,
5226 we just make conservative assumptions, i.e. a longer lifetime;
5227 this is done in the 'default:' cases. */
5228 switch (type)
5230 case RELOAD_FOR_OTHER_ADDRESS:
5231 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
5232 time1 = copy ? 0 : 1;
5233 break;
5234 case RELOAD_OTHER:
5235 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5236 break;
5237 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5238 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5239 respectively, to the time values for these, we get distinct time
5240 values. To get distinct time values for each operand, we have to
5241 multiply opnum by at least three. We round that up to four because
5242 multiply by four is often cheaper. */
5243 case RELOAD_FOR_INPADDR_ADDRESS:
5244 time1 = opnum * 4 + 2;
5245 break;
5246 case RELOAD_FOR_INPUT_ADDRESS:
5247 time1 = opnum * 4 + 3;
5248 break;
5249 case RELOAD_FOR_INPUT:
5250 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5251 executes (inclusive). */
5252 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5253 break;
5254 case RELOAD_FOR_OPADDR_ADDR:
5255 /* opnum * 4 + 4
5256 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5257 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5258 break;
5259 case RELOAD_FOR_OPERAND_ADDRESS:
5260 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5261 is executed. */
5262 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5263 break;
5264 case RELOAD_FOR_OUTADDR_ADDRESS:
5265 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5266 break;
5267 case RELOAD_FOR_OUTPUT_ADDRESS:
5268 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5269 break;
5270 default:
5271 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5274 for (i = 0; i < n_reloads; i++)
5276 rtx reg = rld[i].reg_rtx;
5277 if (reg && REG_P (reg)
5278 && ((unsigned) regno - true_regnum (reg)
5279 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5280 && i != reloadnum)
5282 rtx other_input = rld[i].in;
5284 /* If the other reload loads the same input value, that
5285 will not cause a conflict only if it's loading it into
5286 the same register. */
5287 if (true_regnum (reg) != start_regno)
5288 other_input = NULL_RTX;
5289 if (! other_input || ! rtx_equal_p (other_input, value)
5290 || rld[i].out || out)
5292 int time2;
5293 switch (rld[i].when_needed)
5295 case RELOAD_FOR_OTHER_ADDRESS:
5296 time2 = 0;
5297 break;
5298 case RELOAD_FOR_INPADDR_ADDRESS:
5299 /* find_reloads makes sure that a
5300 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5301 by at most one - the first -
5302 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5303 address reload is inherited, the address address reload
5304 goes away, so we can ignore this conflict. */
5305 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5306 && ignore_address_reloads
5307 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5308 Then the address address is still needed to store
5309 back the new address. */
5310 && ! rld[reloadnum].out)
5311 continue;
5312 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5313 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5314 reloads go away. */
5315 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5316 && ignore_address_reloads
5317 /* Unless we are reloading an auto_inc expression. */
5318 && ! rld[reloadnum].out)
5319 continue;
5320 time2 = rld[i].opnum * 4 + 2;
5321 break;
5322 case RELOAD_FOR_INPUT_ADDRESS:
5323 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5324 && ignore_address_reloads
5325 && ! rld[reloadnum].out)
5326 continue;
5327 time2 = rld[i].opnum * 4 + 3;
5328 break;
5329 case RELOAD_FOR_INPUT:
5330 time2 = rld[i].opnum * 4 + 4;
5331 check_earlyclobber = 1;
5332 break;
5333 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5334 == MAX_RECOG_OPERAND * 4 */
5335 case RELOAD_FOR_OPADDR_ADDR:
5336 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5337 && ignore_address_reloads
5338 && ! rld[reloadnum].out)
5339 continue;
5340 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5341 break;
5342 case RELOAD_FOR_OPERAND_ADDRESS:
5343 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5344 check_earlyclobber = 1;
5345 break;
5346 case RELOAD_FOR_INSN:
5347 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5348 break;
5349 case RELOAD_FOR_OUTPUT:
5350 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5351 instruction is executed. */
5352 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5353 break;
5354 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5355 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5356 value. */
5357 case RELOAD_FOR_OUTADDR_ADDRESS:
5358 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5359 && ignore_address_reloads
5360 && ! rld[reloadnum].out)
5361 continue;
5362 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5363 break;
5364 case RELOAD_FOR_OUTPUT_ADDRESS:
5365 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5366 break;
5367 case RELOAD_OTHER:
5368 /* If there is no conflict in the input part, handle this
5369 like an output reload. */
5370 if (! rld[i].in || rtx_equal_p (other_input, value))
5372 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5373 /* Earlyclobbered outputs must conflict with inputs. */
5374 if (earlyclobber_operand_p (rld[i].out))
5375 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5377 break;
5379 time2 = 1;
5380 /* RELOAD_OTHER might be live beyond instruction execution,
5381 but this is not obvious when we set time2 = 1. So check
5382 here if there might be a problem with the new reload
5383 clobbering the register used by the RELOAD_OTHER. */
5384 if (out)
5385 return 0;
5386 break;
5387 default:
5388 return 0;
5390 if ((time1 >= time2
5391 && (! rld[i].in || rld[i].out
5392 || ! rtx_equal_p (other_input, value)))
5393 || (out && rld[reloadnum].out_reg
5394 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5395 return 0;
5400 /* Earlyclobbered outputs must conflict with inputs. */
5401 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5402 return 0;
5404 return 1;
5407 /* Return 1 if the value in reload reg REGNO, as used by a reload
5408 needed for the part of the insn specified by OPNUM and TYPE,
5409 may be used to load VALUE into it.
5411 MODE is the mode in which the register is used, this is needed to
5412 determine how many hard regs to test.
5414 Other read-only reloads with the same value do not conflict
5415 unless OUT is nonzero and these other reloads have to live while
5416 output reloads live.
5417 If OUT is CONST0_RTX, this is a special case: it means that the
5418 test should not be for using register REGNO as reload register, but
5419 for copying from register REGNO into the reload register.
5421 RELOADNUM is the number of the reload we want to load this value for;
5422 a reload does not conflict with itself.
5424 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5425 reloads that load an address for the very reload we are considering.
5427 The caller has to make sure that there is no conflict with the return
5428 register. */
5430 static int
5431 free_for_value_p (int regno, enum machine_mode mode, int opnum,
5432 enum reload_type type, rtx value, rtx out, int reloadnum,
5433 int ignore_address_reloads)
5435 int nregs = hard_regno_nregs[regno][mode];
5436 while (nregs-- > 0)
5437 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5438 value, out, reloadnum,
5439 ignore_address_reloads))
5440 return 0;
5441 return 1;
5444 /* Return nonzero if the rtx X is invariant over the current function. */
5445 /* ??? Actually, the places where we use this expect exactly what is
5446 tested here, and not everything that is function invariant. In
5447 particular, the frame pointer and arg pointer are special cased;
5448 pic_offset_table_rtx is not, and we must not spill these things to
5449 memory. */
5452 function_invariant_p (const_rtx x)
5454 if (CONSTANT_P (x))
5455 return 1;
5456 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
5457 return 1;
5458 if (GET_CODE (x) == PLUS
5459 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
5460 && CONSTANT_P (XEXP (x, 1)))
5461 return 1;
5462 return 0;
5465 /* Determine whether the reload reg X overlaps any rtx'es used for
5466 overriding inheritance. Return nonzero if so. */
5468 static int
5469 conflicts_with_override (rtx x)
5471 int i;
5472 for (i = 0; i < n_reloads; i++)
5473 if (reload_override_in[i]
5474 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5475 return 1;
5476 return 0;
5479 /* Give an error message saying we failed to find a reload for INSN,
5480 and clear out reload R. */
5481 static void
5482 failed_reload (rtx insn, int r)
5484 if (asm_noperands (PATTERN (insn)) < 0)
5485 /* It's the compiler's fault. */
5486 fatal_insn ("could not find a spill register", insn);
5488 /* It's the user's fault; the operand's mode and constraint
5489 don't match. Disable this reload so we don't crash in final. */
5490 error_for_asm (insn,
5491 "%<asm%> operand constraint incompatible with operand size");
5492 rld[r].in = 0;
5493 rld[r].out = 0;
5494 rld[r].reg_rtx = 0;
5495 rld[r].optional = 1;
5496 rld[r].secondary_p = 1;
5499 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5500 for reload R. If it's valid, get an rtx for it. Return nonzero if
5501 successful. */
5502 static int
5503 set_reload_reg (int i, int r)
5505 int regno;
5506 rtx reg = spill_reg_rtx[i];
5508 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5509 spill_reg_rtx[i] = reg
5510 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5512 regno = true_regnum (reg);
5514 /* Detect when the reload reg can't hold the reload mode.
5515 This used to be one `if', but Sequent compiler can't handle that. */
5516 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5518 enum machine_mode test_mode = VOIDmode;
5519 if (rld[r].in)
5520 test_mode = GET_MODE (rld[r].in);
5521 /* If rld[r].in has VOIDmode, it means we will load it
5522 in whatever mode the reload reg has: to wit, rld[r].mode.
5523 We have already tested that for validity. */
5524 /* Aside from that, we need to test that the expressions
5525 to reload from or into have modes which are valid for this
5526 reload register. Otherwise the reload insns would be invalid. */
5527 if (! (rld[r].in != 0 && test_mode != VOIDmode
5528 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5529 if (! (rld[r].out != 0
5530 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5532 /* The reg is OK. */
5533 last_spill_reg = i;
5535 /* Mark as in use for this insn the reload regs we use
5536 for this. */
5537 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5538 rld[r].when_needed, rld[r].mode);
5540 rld[r].reg_rtx = reg;
5541 reload_spill_index[r] = spill_regs[i];
5542 return 1;
5545 return 0;
5548 /* Find a spill register to use as a reload register for reload R.
5549 LAST_RELOAD is nonzero if this is the last reload for the insn being
5550 processed.
5552 Set rld[R].reg_rtx to the register allocated.
5554 We return 1 if successful, or 0 if we couldn't find a spill reg and
5555 we didn't change anything. */
5557 static int
5558 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5559 int last_reload)
5561 int i, pass, count;
5563 /* If we put this reload ahead, thinking it is a group,
5564 then insist on finding a group. Otherwise we can grab a
5565 reg that some other reload needs.
5566 (That can happen when we have a 68000 DATA_OR_FP_REG
5567 which is a group of data regs or one fp reg.)
5568 We need not be so restrictive if there are no more reloads
5569 for this insn.
5571 ??? Really it would be nicer to have smarter handling
5572 for that kind of reg class, where a problem like this is normal.
5573 Perhaps those classes should be avoided for reloading
5574 by use of more alternatives. */
5576 int force_group = rld[r].nregs > 1 && ! last_reload;
5578 /* If we want a single register and haven't yet found one,
5579 take any reg in the right class and not in use.
5580 If we want a consecutive group, here is where we look for it.
5582 We use two passes so we can first look for reload regs to
5583 reuse, which are already in use for other reloads in this insn,
5584 and only then use additional registers.
5585 I think that maximizing reuse is needed to make sure we don't
5586 run out of reload regs. Suppose we have three reloads, and
5587 reloads A and B can share regs. These need two regs.
5588 Suppose A and B are given different regs.
5589 That leaves none for C. */
5590 for (pass = 0; pass < 2; pass++)
5592 /* I is the index in spill_regs.
5593 We advance it round-robin between insns to use all spill regs
5594 equally, so that inherited reloads have a chance
5595 of leapfrogging each other. */
5597 i = last_spill_reg;
5599 for (count = 0; count < n_spills; count++)
5601 int class = (int) rld[r].class;
5602 int regnum;
5604 i++;
5605 if (i >= n_spills)
5606 i -= n_spills;
5607 regnum = spill_regs[i];
5609 if ((reload_reg_free_p (regnum, rld[r].opnum,
5610 rld[r].when_needed)
5611 || (rld[r].in
5612 /* We check reload_reg_used to make sure we
5613 don't clobber the return register. */
5614 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5615 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5616 rld[r].when_needed, rld[r].in,
5617 rld[r].out, r, 1)))
5618 && TEST_HARD_REG_BIT (reg_class_contents[class], regnum)
5619 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5620 /* Look first for regs to share, then for unshared. But
5621 don't share regs used for inherited reloads; they are
5622 the ones we want to preserve. */
5623 && (pass
5624 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5625 regnum)
5626 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5627 regnum))))
5629 int nr = hard_regno_nregs[regnum][rld[r].mode];
5630 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5631 (on 68000) got us two FP regs. If NR is 1,
5632 we would reject both of them. */
5633 if (force_group)
5634 nr = rld[r].nregs;
5635 /* If we need only one reg, we have already won. */
5636 if (nr == 1)
5638 /* But reject a single reg if we demand a group. */
5639 if (force_group)
5640 continue;
5641 break;
5643 /* Otherwise check that as many consecutive regs as we need
5644 are available here. */
5645 while (nr > 1)
5647 int regno = regnum + nr - 1;
5648 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
5649 && spill_reg_order[regno] >= 0
5650 && reload_reg_free_p (regno, rld[r].opnum,
5651 rld[r].when_needed)))
5652 break;
5653 nr--;
5655 if (nr == 1)
5656 break;
5660 /* If we found something on pass 1, omit pass 2. */
5661 if (count < n_spills)
5662 break;
5665 /* We should have found a spill register by now. */
5666 if (count >= n_spills)
5667 return 0;
5669 /* I is the index in SPILL_REG_RTX of the reload register we are to
5670 allocate. Get an rtx for it and find its register number. */
5672 return set_reload_reg (i, r);
5675 /* Initialize all the tables needed to allocate reload registers.
5676 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5677 is the array we use to restore the reg_rtx field for every reload. */
5679 static void
5680 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5682 int i;
5684 for (i = 0; i < n_reloads; i++)
5685 rld[i].reg_rtx = save_reload_reg_rtx[i];
5687 memset (reload_inherited, 0, MAX_RELOADS);
5688 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5689 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5691 CLEAR_HARD_REG_SET (reload_reg_used);
5692 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5693 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5694 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5695 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5696 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5698 CLEAR_HARD_REG_SET (reg_used_in_insn);
5700 HARD_REG_SET tmp;
5701 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5702 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5703 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5704 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5705 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5706 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5709 for (i = 0; i < reload_n_operands; i++)
5711 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5712 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5713 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5714 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5715 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5716 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5719 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5721 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5723 for (i = 0; i < n_reloads; i++)
5724 /* If we have already decided to use a certain register,
5725 don't use it in another way. */
5726 if (rld[i].reg_rtx)
5727 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5728 rld[i].when_needed, rld[i].mode);
5731 /* Assign hard reg targets for the pseudo-registers we must reload
5732 into hard regs for this insn.
5733 Also output the instructions to copy them in and out of the hard regs.
5735 For machines with register classes, we are responsible for
5736 finding a reload reg in the proper class. */
5738 static void
5739 choose_reload_regs (struct insn_chain *chain)
5741 rtx insn = chain->insn;
5742 int i, j;
5743 unsigned int max_group_size = 1;
5744 enum reg_class group_class = NO_REGS;
5745 int pass, win, inheritance;
5747 rtx save_reload_reg_rtx[MAX_RELOADS];
5749 /* In order to be certain of getting the registers we need,
5750 we must sort the reloads into order of increasing register class.
5751 Then our grabbing of reload registers will parallel the process
5752 that provided the reload registers.
5754 Also note whether any of the reloads wants a consecutive group of regs.
5755 If so, record the maximum size of the group desired and what
5756 register class contains all the groups needed by this insn. */
5758 for (j = 0; j < n_reloads; j++)
5760 reload_order[j] = j;
5761 if (rld[j].reg_rtx != NULL_RTX)
5763 gcc_assert (REG_P (rld[j].reg_rtx)
5764 && HARD_REGISTER_P (rld[j].reg_rtx));
5765 reload_spill_index[j] = REGNO (rld[j].reg_rtx);
5767 else
5768 reload_spill_index[j] = -1;
5770 if (rld[j].nregs > 1)
5772 max_group_size = MAX (rld[j].nregs, max_group_size);
5773 group_class
5774 = reg_class_superunion[(int) rld[j].class][(int) group_class];
5777 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5780 if (n_reloads > 1)
5781 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5783 /* If -O, try first with inheritance, then turning it off.
5784 If not -O, don't do inheritance.
5785 Using inheritance when not optimizing leads to paradoxes
5786 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5787 because one side of the comparison might be inherited. */
5788 win = 0;
5789 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
5791 choose_reload_regs_init (chain, save_reload_reg_rtx);
5793 /* Process the reloads in order of preference just found.
5794 Beyond this point, subregs can be found in reload_reg_rtx.
5796 This used to look for an existing reloaded home for all of the
5797 reloads, and only then perform any new reloads. But that could lose
5798 if the reloads were done out of reg-class order because a later
5799 reload with a looser constraint might have an old home in a register
5800 needed by an earlier reload with a tighter constraint.
5802 To solve this, we make two passes over the reloads, in the order
5803 described above. In the first pass we try to inherit a reload
5804 from a previous insn. If there is a later reload that needs a
5805 class that is a proper subset of the class being processed, we must
5806 also allocate a spill register during the first pass.
5808 Then make a second pass over the reloads to allocate any reloads
5809 that haven't been given registers yet. */
5811 for (j = 0; j < n_reloads; j++)
5813 int r = reload_order[j];
5814 rtx search_equiv = NULL_RTX;
5816 /* Ignore reloads that got marked inoperative. */
5817 if (rld[r].out == 0 && rld[r].in == 0
5818 && ! rld[r].secondary_p)
5819 continue;
5821 /* If find_reloads chose to use reload_in or reload_out as a reload
5822 register, we don't need to chose one. Otherwise, try even if it
5823 found one since we might save an insn if we find the value lying
5824 around.
5825 Try also when reload_in is a pseudo without a hard reg. */
5826 if (rld[r].in != 0 && rld[r].reg_rtx != 0
5827 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
5828 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
5829 && !MEM_P (rld[r].in)
5830 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
5831 continue;
5833 #if 0 /* No longer needed for correct operation.
5834 It might give better code, or might not; worth an experiment? */
5835 /* If this is an optional reload, we can't inherit from earlier insns
5836 until we are sure that any non-optional reloads have been allocated.
5837 The following code takes advantage of the fact that optional reloads
5838 are at the end of reload_order. */
5839 if (rld[r].optional != 0)
5840 for (i = 0; i < j; i++)
5841 if ((rld[reload_order[i]].out != 0
5842 || rld[reload_order[i]].in != 0
5843 || rld[reload_order[i]].secondary_p)
5844 && ! rld[reload_order[i]].optional
5845 && rld[reload_order[i]].reg_rtx == 0)
5846 allocate_reload_reg (chain, reload_order[i], 0);
5847 #endif
5849 /* First see if this pseudo is already available as reloaded
5850 for a previous insn. We cannot try to inherit for reloads
5851 that are smaller than the maximum number of registers needed
5852 for groups unless the register we would allocate cannot be used
5853 for the groups.
5855 We could check here to see if this is a secondary reload for
5856 an object that is already in a register of the desired class.
5857 This would avoid the need for the secondary reload register.
5858 But this is complex because we can't easily determine what
5859 objects might want to be loaded via this reload. So let a
5860 register be allocated here. In `emit_reload_insns' we suppress
5861 one of the loads in the case described above. */
5863 if (inheritance)
5865 int byte = 0;
5866 int regno = -1;
5867 enum machine_mode mode = VOIDmode;
5869 if (rld[r].in == 0)
5871 else if (REG_P (rld[r].in))
5873 regno = REGNO (rld[r].in);
5874 mode = GET_MODE (rld[r].in);
5876 else if (REG_P (rld[r].in_reg))
5878 regno = REGNO (rld[r].in_reg);
5879 mode = GET_MODE (rld[r].in_reg);
5881 else if (GET_CODE (rld[r].in_reg) == SUBREG
5882 && REG_P (SUBREG_REG (rld[r].in_reg)))
5884 regno = REGNO (SUBREG_REG (rld[r].in_reg));
5885 if (regno < FIRST_PSEUDO_REGISTER)
5886 regno = subreg_regno (rld[r].in_reg);
5887 else
5888 byte = SUBREG_BYTE (rld[r].in_reg);
5889 mode = GET_MODE (rld[r].in_reg);
5891 #ifdef AUTO_INC_DEC
5892 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
5893 && REG_P (XEXP (rld[r].in_reg, 0)))
5895 regno = REGNO (XEXP (rld[r].in_reg, 0));
5896 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
5897 rld[r].out = rld[r].in;
5899 #endif
5900 #if 0
5901 /* This won't work, since REGNO can be a pseudo reg number.
5902 Also, it takes much more hair to keep track of all the things
5903 that can invalidate an inherited reload of part of a pseudoreg. */
5904 else if (GET_CODE (rld[r].in) == SUBREG
5905 && REG_P (SUBREG_REG (rld[r].in)))
5906 regno = subreg_regno (rld[r].in);
5907 #endif
5909 if (regno >= 0
5910 && reg_last_reload_reg[regno] != 0
5911 #ifdef CANNOT_CHANGE_MODE_CLASS
5912 /* Verify that the register it's in can be used in
5913 mode MODE. */
5914 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
5915 GET_MODE (reg_last_reload_reg[regno]),
5916 mode)
5917 #endif
5920 enum reg_class class = rld[r].class, last_class;
5921 rtx last_reg = reg_last_reload_reg[regno];
5922 enum machine_mode need_mode;
5924 i = REGNO (last_reg);
5925 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
5926 last_class = REGNO_REG_CLASS (i);
5928 if (byte == 0)
5929 need_mode = mode;
5930 else
5931 need_mode
5932 = smallest_mode_for_size (GET_MODE_BITSIZE (mode)
5933 + byte * BITS_PER_UNIT,
5934 GET_MODE_CLASS (mode));
5936 if ((GET_MODE_SIZE (GET_MODE (last_reg))
5937 >= GET_MODE_SIZE (need_mode))
5938 && reg_reloaded_contents[i] == regno
5939 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
5940 && HARD_REGNO_MODE_OK (i, rld[r].mode)
5941 && (TEST_HARD_REG_BIT (reg_class_contents[(int) class], i)
5942 /* Even if we can't use this register as a reload
5943 register, we might use it for reload_override_in,
5944 if copying it to the desired class is cheap
5945 enough. */
5946 || ((REGISTER_MOVE_COST (mode, last_class, class)
5947 < MEMORY_MOVE_COST (mode, class, 1))
5948 && (secondary_reload_class (1, class, mode,
5949 last_reg)
5950 == NO_REGS)
5951 #ifdef SECONDARY_MEMORY_NEEDED
5952 && ! SECONDARY_MEMORY_NEEDED (last_class, class,
5953 mode)
5954 #endif
5957 && (rld[r].nregs == max_group_size
5958 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
5960 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
5961 rld[r].when_needed, rld[r].in,
5962 const0_rtx, r, 1))
5964 /* If a group is needed, verify that all the subsequent
5965 registers still have their values intact. */
5966 int nr = hard_regno_nregs[i][rld[r].mode];
5967 int k;
5969 for (k = 1; k < nr; k++)
5970 if (reg_reloaded_contents[i + k] != regno
5971 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
5972 break;
5974 if (k == nr)
5976 int i1;
5977 int bad_for_class;
5979 last_reg = (GET_MODE (last_reg) == mode
5980 ? last_reg : gen_rtx_REG (mode, i));
5982 bad_for_class = 0;
5983 for (k = 0; k < nr; k++)
5984 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
5985 i+k);
5987 /* We found a register that contains the
5988 value we need. If this register is the
5989 same as an `earlyclobber' operand of the
5990 current insn, just mark it as a place to
5991 reload from since we can't use it as the
5992 reload register itself. */
5994 for (i1 = 0; i1 < n_earlyclobbers; i1++)
5995 if (reg_overlap_mentioned_for_reload_p
5996 (reg_last_reload_reg[regno],
5997 reload_earlyclobbers[i1]))
5998 break;
6000 if (i1 != n_earlyclobbers
6001 || ! (free_for_value_p (i, rld[r].mode,
6002 rld[r].opnum,
6003 rld[r].when_needed, rld[r].in,
6004 rld[r].out, r, 1))
6005 /* Don't use it if we'd clobber a pseudo reg. */
6006 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6007 && rld[r].out
6008 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6009 /* Don't clobber the frame pointer. */
6010 || (i == HARD_FRAME_POINTER_REGNUM
6011 && frame_pointer_needed
6012 && rld[r].out)
6013 /* Don't really use the inherited spill reg
6014 if we need it wider than we've got it. */
6015 || (GET_MODE_SIZE (rld[r].mode)
6016 > GET_MODE_SIZE (mode))
6017 || bad_for_class
6019 /* If find_reloads chose reload_out as reload
6020 register, stay with it - that leaves the
6021 inherited register for subsequent reloads. */
6022 || (rld[r].out && rld[r].reg_rtx
6023 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6025 if (! rld[r].optional)
6027 reload_override_in[r] = last_reg;
6028 reload_inheritance_insn[r]
6029 = reg_reloaded_insn[i];
6032 else
6034 int k;
6035 /* We can use this as a reload reg. */
6036 /* Mark the register as in use for this part of
6037 the insn. */
6038 mark_reload_reg_in_use (i,
6039 rld[r].opnum,
6040 rld[r].when_needed,
6041 rld[r].mode);
6042 rld[r].reg_rtx = last_reg;
6043 reload_inherited[r] = 1;
6044 reload_inheritance_insn[r]
6045 = reg_reloaded_insn[i];
6046 reload_spill_index[r] = i;
6047 for (k = 0; k < nr; k++)
6048 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6049 i + k);
6056 /* Here's another way to see if the value is already lying around. */
6057 if (inheritance
6058 && rld[r].in != 0
6059 && ! reload_inherited[r]
6060 && rld[r].out == 0
6061 && (CONSTANT_P (rld[r].in)
6062 || GET_CODE (rld[r].in) == PLUS
6063 || REG_P (rld[r].in)
6064 || MEM_P (rld[r].in))
6065 && (rld[r].nregs == max_group_size
6066 || ! reg_classes_intersect_p (rld[r].class, group_class)))
6067 search_equiv = rld[r].in;
6068 /* If this is an output reload from a simple move insn, look
6069 if an equivalence for the input is available. */
6070 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6072 rtx set = single_set (insn);
6074 if (set
6075 && rtx_equal_p (rld[r].out, SET_DEST (set))
6076 && CONSTANT_P (SET_SRC (set)))
6077 search_equiv = SET_SRC (set);
6080 if (search_equiv)
6082 rtx equiv
6083 = find_equiv_reg (search_equiv, insn, rld[r].class,
6084 -1, NULL, 0, rld[r].mode);
6085 int regno = 0;
6087 if (equiv != 0)
6089 if (REG_P (equiv))
6090 regno = REGNO (equiv);
6091 else
6093 /* This must be a SUBREG of a hard register.
6094 Make a new REG since this might be used in an
6095 address and not all machines support SUBREGs
6096 there. */
6097 gcc_assert (GET_CODE (equiv) == SUBREG);
6098 regno = subreg_regno (equiv);
6099 equiv = gen_rtx_REG (rld[r].mode, regno);
6100 /* If we choose EQUIV as the reload register, but the
6101 loop below decides to cancel the inheritance, we'll
6102 end up reloading EQUIV in rld[r].mode, not the mode
6103 it had originally. That isn't safe when EQUIV isn't
6104 available as a spill register since its value might
6105 still be live at this point. */
6106 for (i = regno; i < regno + (int) rld[r].nregs; i++)
6107 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
6108 equiv = 0;
6112 /* If we found a spill reg, reject it unless it is free
6113 and of the desired class. */
6114 if (equiv != 0)
6116 int regs_used = 0;
6117 int bad_for_class = 0;
6118 int max_regno = regno + rld[r].nregs;
6120 for (i = regno; i < max_regno; i++)
6122 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
6124 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].class],
6128 if ((regs_used
6129 && ! free_for_value_p (regno, rld[r].mode,
6130 rld[r].opnum, rld[r].when_needed,
6131 rld[r].in, rld[r].out, r, 1))
6132 || bad_for_class)
6133 equiv = 0;
6136 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6137 equiv = 0;
6139 /* We found a register that contains the value we need.
6140 If this register is the same as an `earlyclobber' operand
6141 of the current insn, just mark it as a place to reload from
6142 since we can't use it as the reload register itself. */
6144 if (equiv != 0)
6145 for (i = 0; i < n_earlyclobbers; i++)
6146 if (reg_overlap_mentioned_for_reload_p (equiv,
6147 reload_earlyclobbers[i]))
6149 if (! rld[r].optional)
6150 reload_override_in[r] = equiv;
6151 equiv = 0;
6152 break;
6155 /* If the equiv register we have found is explicitly clobbered
6156 in the current insn, it depends on the reload type if we
6157 can use it, use it for reload_override_in, or not at all.
6158 In particular, we then can't use EQUIV for a
6159 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6161 if (equiv != 0)
6163 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
6164 switch (rld[r].when_needed)
6166 case RELOAD_FOR_OTHER_ADDRESS:
6167 case RELOAD_FOR_INPADDR_ADDRESS:
6168 case RELOAD_FOR_INPUT_ADDRESS:
6169 case RELOAD_FOR_OPADDR_ADDR:
6170 break;
6171 case RELOAD_OTHER:
6172 case RELOAD_FOR_INPUT:
6173 case RELOAD_FOR_OPERAND_ADDRESS:
6174 if (! rld[r].optional)
6175 reload_override_in[r] = equiv;
6176 /* Fall through. */
6177 default:
6178 equiv = 0;
6179 break;
6181 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
6182 switch (rld[r].when_needed)
6184 case RELOAD_FOR_OTHER_ADDRESS:
6185 case RELOAD_FOR_INPADDR_ADDRESS:
6186 case RELOAD_FOR_INPUT_ADDRESS:
6187 case RELOAD_FOR_OPADDR_ADDR:
6188 case RELOAD_FOR_OPERAND_ADDRESS:
6189 case RELOAD_FOR_INPUT:
6190 break;
6191 case RELOAD_OTHER:
6192 if (! rld[r].optional)
6193 reload_override_in[r] = equiv;
6194 /* Fall through. */
6195 default:
6196 equiv = 0;
6197 break;
6201 /* If we found an equivalent reg, say no code need be generated
6202 to load it, and use it as our reload reg. */
6203 if (equiv != 0
6204 && (regno != HARD_FRAME_POINTER_REGNUM
6205 || !frame_pointer_needed))
6207 int nr = hard_regno_nregs[regno][rld[r].mode];
6208 int k;
6209 rld[r].reg_rtx = equiv;
6210 reload_inherited[r] = 1;
6212 /* If reg_reloaded_valid is not set for this register,
6213 there might be a stale spill_reg_store lying around.
6214 We must clear it, since otherwise emit_reload_insns
6215 might delete the store. */
6216 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6217 spill_reg_store[regno] = NULL_RTX;
6218 /* If any of the hard registers in EQUIV are spill
6219 registers, mark them as in use for this insn. */
6220 for (k = 0; k < nr; k++)
6222 i = spill_reg_order[regno + k];
6223 if (i >= 0)
6225 mark_reload_reg_in_use (regno, rld[r].opnum,
6226 rld[r].when_needed,
6227 rld[r].mode);
6228 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6229 regno + k);
6235 /* If we found a register to use already, or if this is an optional
6236 reload, we are done. */
6237 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6238 continue;
6240 #if 0
6241 /* No longer needed for correct operation. Might or might
6242 not give better code on the average. Want to experiment? */
6244 /* See if there is a later reload that has a class different from our
6245 class that intersects our class or that requires less register
6246 than our reload. If so, we must allocate a register to this
6247 reload now, since that reload might inherit a previous reload
6248 and take the only available register in our class. Don't do this
6249 for optional reloads since they will force all previous reloads
6250 to be allocated. Also don't do this for reloads that have been
6251 turned off. */
6253 for (i = j + 1; i < n_reloads; i++)
6255 int s = reload_order[i];
6257 if ((rld[s].in == 0 && rld[s].out == 0
6258 && ! rld[s].secondary_p)
6259 || rld[s].optional)
6260 continue;
6262 if ((rld[s].class != rld[r].class
6263 && reg_classes_intersect_p (rld[r].class,
6264 rld[s].class))
6265 || rld[s].nregs < rld[r].nregs)
6266 break;
6269 if (i == n_reloads)
6270 continue;
6272 allocate_reload_reg (chain, r, j == n_reloads - 1);
6273 #endif
6276 /* Now allocate reload registers for anything non-optional that
6277 didn't get one yet. */
6278 for (j = 0; j < n_reloads; j++)
6280 int r = reload_order[j];
6282 /* Ignore reloads that got marked inoperative. */
6283 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6284 continue;
6286 /* Skip reloads that already have a register allocated or are
6287 optional. */
6288 if (rld[r].reg_rtx != 0 || rld[r].optional)
6289 continue;
6291 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6292 break;
6295 /* If that loop got all the way, we have won. */
6296 if (j == n_reloads)
6298 win = 1;
6299 break;
6302 /* Loop around and try without any inheritance. */
6305 if (! win)
6307 /* First undo everything done by the failed attempt
6308 to allocate with inheritance. */
6309 choose_reload_regs_init (chain, save_reload_reg_rtx);
6311 /* Some sanity tests to verify that the reloads found in the first
6312 pass are identical to the ones we have now. */
6313 gcc_assert (chain->n_reloads == n_reloads);
6315 for (i = 0; i < n_reloads; i++)
6317 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6318 continue;
6319 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6320 for (j = 0; j < n_spills; j++)
6321 if (spill_regs[j] == chain->rld[i].regno)
6322 if (! set_reload_reg (j, i))
6323 failed_reload (chain->insn, i);
6327 /* If we thought we could inherit a reload, because it seemed that
6328 nothing else wanted the same reload register earlier in the insn,
6329 verify that assumption, now that all reloads have been assigned.
6330 Likewise for reloads where reload_override_in has been set. */
6332 /* If doing expensive optimizations, do one preliminary pass that doesn't
6333 cancel any inheritance, but removes reloads that have been needed only
6334 for reloads that we know can be inherited. */
6335 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6337 for (j = 0; j < n_reloads; j++)
6339 int r = reload_order[j];
6340 rtx check_reg;
6341 if (reload_inherited[r] && rld[r].reg_rtx)
6342 check_reg = rld[r].reg_rtx;
6343 else if (reload_override_in[r]
6344 && (REG_P (reload_override_in[r])
6345 || GET_CODE (reload_override_in[r]) == SUBREG))
6346 check_reg = reload_override_in[r];
6347 else
6348 continue;
6349 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6350 rld[r].opnum, rld[r].when_needed, rld[r].in,
6351 (reload_inherited[r]
6352 ? rld[r].out : const0_rtx),
6353 r, 1))
6355 if (pass)
6356 continue;
6357 reload_inherited[r] = 0;
6358 reload_override_in[r] = 0;
6360 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6361 reload_override_in, then we do not need its related
6362 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6363 likewise for other reload types.
6364 We handle this by removing a reload when its only replacement
6365 is mentioned in reload_in of the reload we are going to inherit.
6366 A special case are auto_inc expressions; even if the input is
6367 inherited, we still need the address for the output. We can
6368 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6369 If we succeeded removing some reload and we are doing a preliminary
6370 pass just to remove such reloads, make another pass, since the
6371 removal of one reload might allow us to inherit another one. */
6372 else if (rld[r].in
6373 && rld[r].out != rld[r].in
6374 && remove_address_replacements (rld[r].in) && pass)
6375 pass = 2;
6379 /* Now that reload_override_in is known valid,
6380 actually override reload_in. */
6381 for (j = 0; j < n_reloads; j++)
6382 if (reload_override_in[j])
6383 rld[j].in = reload_override_in[j];
6385 /* If this reload won't be done because it has been canceled or is
6386 optional and not inherited, clear reload_reg_rtx so other
6387 routines (such as subst_reloads) don't get confused. */
6388 for (j = 0; j < n_reloads; j++)
6389 if (rld[j].reg_rtx != 0
6390 && ((rld[j].optional && ! reload_inherited[j])
6391 || (rld[j].in == 0 && rld[j].out == 0
6392 && ! rld[j].secondary_p)))
6394 int regno = true_regnum (rld[j].reg_rtx);
6396 if (spill_reg_order[regno] >= 0)
6397 clear_reload_reg_in_use (regno, rld[j].opnum,
6398 rld[j].when_needed, rld[j].mode);
6399 rld[j].reg_rtx = 0;
6400 reload_spill_index[j] = -1;
6403 /* Record which pseudos and which spill regs have output reloads. */
6404 for (j = 0; j < n_reloads; j++)
6406 int r = reload_order[j];
6408 i = reload_spill_index[r];
6410 /* I is nonneg if this reload uses a register.
6411 If rld[r].reg_rtx is 0, this is an optional reload
6412 that we opted to ignore. */
6413 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
6414 && rld[r].reg_rtx != 0)
6416 int nregno = REGNO (rld[r].out_reg);
6417 int nr = 1;
6419 if (nregno < FIRST_PSEUDO_REGISTER)
6420 nr = hard_regno_nregs[nregno][rld[r].mode];
6422 while (--nr >= 0)
6423 SET_REGNO_REG_SET (&reg_has_output_reload,
6424 nregno + nr);
6426 if (i >= 0)
6428 nr = hard_regno_nregs[i][rld[r].mode];
6429 while (--nr >= 0)
6430 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6433 gcc_assert (rld[r].when_needed == RELOAD_OTHER
6434 || rld[r].when_needed == RELOAD_FOR_OUTPUT
6435 || rld[r].when_needed == RELOAD_FOR_INSN);
6440 /* Deallocate the reload register for reload R. This is called from
6441 remove_address_replacements. */
6443 void
6444 deallocate_reload_reg (int r)
6446 int regno;
6448 if (! rld[r].reg_rtx)
6449 return;
6450 regno = true_regnum (rld[r].reg_rtx);
6451 rld[r].reg_rtx = 0;
6452 if (spill_reg_order[regno] >= 0)
6453 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6454 rld[r].mode);
6455 reload_spill_index[r] = -1;
6458 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
6459 reloads of the same item for fear that we might not have enough reload
6460 registers. However, normally they will get the same reload register
6461 and hence actually need not be loaded twice.
6463 Here we check for the most common case of this phenomenon: when we have
6464 a number of reloads for the same object, each of which were allocated
6465 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6466 reload, and is not modified in the insn itself. If we find such,
6467 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6468 This will not increase the number of spill registers needed and will
6469 prevent redundant code. */
6471 static void
6472 merge_assigned_reloads (rtx insn)
6474 int i, j;
6476 /* Scan all the reloads looking for ones that only load values and
6477 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6478 assigned and not modified by INSN. */
6480 for (i = 0; i < n_reloads; i++)
6482 int conflicting_input = 0;
6483 int max_input_address_opnum = -1;
6484 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6486 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6487 || rld[i].out != 0 || rld[i].reg_rtx == 0
6488 || reg_set_p (rld[i].reg_rtx, insn))
6489 continue;
6491 /* Look at all other reloads. Ensure that the only use of this
6492 reload_reg_rtx is in a reload that just loads the same value
6493 as we do. Note that any secondary reloads must be of the identical
6494 class since the values, modes, and result registers are the
6495 same, so we need not do anything with any secondary reloads. */
6497 for (j = 0; j < n_reloads; j++)
6499 if (i == j || rld[j].reg_rtx == 0
6500 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6501 rld[i].reg_rtx))
6502 continue;
6504 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6505 && rld[j].opnum > max_input_address_opnum)
6506 max_input_address_opnum = rld[j].opnum;
6508 /* If the reload regs aren't exactly the same (e.g, different modes)
6509 or if the values are different, we can't merge this reload.
6510 But if it is an input reload, we might still merge
6511 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6513 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6514 || rld[j].out != 0 || rld[j].in == 0
6515 || ! rtx_equal_p (rld[i].in, rld[j].in))
6517 if (rld[j].when_needed != RELOAD_FOR_INPUT
6518 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6519 || rld[i].opnum > rld[j].opnum)
6520 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6521 break;
6522 conflicting_input = 1;
6523 if (min_conflicting_input_opnum > rld[j].opnum)
6524 min_conflicting_input_opnum = rld[j].opnum;
6528 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6529 we, in fact, found any matching reloads. */
6531 if (j == n_reloads
6532 && max_input_address_opnum <= min_conflicting_input_opnum)
6534 gcc_assert (rld[i].when_needed != RELOAD_FOR_OUTPUT);
6536 for (j = 0; j < n_reloads; j++)
6537 if (i != j && rld[j].reg_rtx != 0
6538 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6539 && (! conflicting_input
6540 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6541 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6543 rld[i].when_needed = RELOAD_OTHER;
6544 rld[j].in = 0;
6545 reload_spill_index[j] = -1;
6546 transfer_replacements (i, j);
6549 /* If this is now RELOAD_OTHER, look for any reloads that
6550 load parts of this operand and set them to
6551 RELOAD_FOR_OTHER_ADDRESS if they were for inputs,
6552 RELOAD_OTHER for outputs. Note that this test is
6553 equivalent to looking for reloads for this operand
6554 number.
6556 We must take special care with RELOAD_FOR_OUTPUT_ADDRESS;
6557 it may share registers with a RELOAD_FOR_INPUT, so we can
6558 not change it to RELOAD_FOR_OTHER_ADDRESS. We should
6559 never need to, since we do not modify RELOAD_FOR_OUTPUT.
6561 It is possible that the RELOAD_FOR_OPERAND_ADDRESS
6562 instruction is assigned the same register as the earlier
6563 RELOAD_FOR_OTHER_ADDRESS instruction. Merging these two
6564 instructions will cause the RELOAD_FOR_OTHER_ADDRESS
6565 instruction to be deleted later on. */
6567 if (rld[i].when_needed == RELOAD_OTHER)
6568 for (j = 0; j < n_reloads; j++)
6569 if (rld[j].in != 0
6570 && rld[j].when_needed != RELOAD_OTHER
6571 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6572 && rld[j].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
6573 && rld[j].when_needed != RELOAD_FOR_OPERAND_ADDRESS
6574 && (! conflicting_input
6575 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6576 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6577 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6578 rld[i].in))
6580 int k;
6582 rld[j].when_needed
6583 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6584 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6585 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6587 /* Check to see if we accidentally converted two
6588 reloads that use the same reload register with
6589 different inputs to the same type. If so, the
6590 resulting code won't work. */
6591 if (rld[j].reg_rtx)
6592 for (k = 0; k < j; k++)
6593 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6594 || rld[k].when_needed != rld[j].when_needed
6595 || !rtx_equal_p (rld[k].reg_rtx,
6596 rld[j].reg_rtx)
6597 || rtx_equal_p (rld[k].in,
6598 rld[j].in));
6604 /* These arrays are filled by emit_reload_insns and its subroutines. */
6605 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6606 static rtx other_input_address_reload_insns = 0;
6607 static rtx other_input_reload_insns = 0;
6608 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6609 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6610 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6611 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6612 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6613 static rtx operand_reload_insns = 0;
6614 static rtx other_operand_reload_insns = 0;
6615 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6617 /* Values to be put in spill_reg_store are put here first. */
6618 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6619 static HARD_REG_SET reg_reloaded_died;
6621 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
6622 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
6623 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
6624 adjusted register, and return true. Otherwise, return false. */
6625 static bool
6626 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
6627 enum reg_class new_class,
6628 enum machine_mode new_mode)
6631 rtx reg;
6633 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
6635 unsigned regno = REGNO (reg);
6637 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
6638 continue;
6639 if (GET_MODE (reg) != new_mode)
6641 if (!HARD_REGNO_MODE_OK (regno, new_mode))
6642 continue;
6643 if (hard_regno_nregs[regno][new_mode]
6644 > hard_regno_nregs[regno][GET_MODE (reg)])
6645 continue;
6646 reg = reload_adjust_reg_for_mode (reg, new_mode);
6648 *reload_reg = reg;
6649 return true;
6651 return false;
6654 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
6655 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
6656 nonzero, if that is suitable. On success, change *RELOAD_REG to the
6657 adjusted register, and return true. Otherwise, return false. */
6658 static bool
6659 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
6660 enum insn_code icode)
6663 enum reg_class new_class = scratch_reload_class (icode);
6664 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
6666 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
6667 new_class, new_mode);
6670 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6671 has the number J. OLD contains the value to be used as input. */
6673 static void
6674 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6675 rtx old, int j)
6677 rtx insn = chain->insn;
6678 rtx reloadreg = rl->reg_rtx;
6679 rtx oldequiv_reg = 0;
6680 rtx oldequiv = 0;
6681 int special = 0;
6682 enum machine_mode mode;
6683 rtx *where;
6685 /* Determine the mode to reload in.
6686 This is very tricky because we have three to choose from.
6687 There is the mode the insn operand wants (rl->inmode).
6688 There is the mode of the reload register RELOADREG.
6689 There is the intrinsic mode of the operand, which we could find
6690 by stripping some SUBREGs.
6691 It turns out that RELOADREG's mode is irrelevant:
6692 we can change that arbitrarily.
6694 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
6695 then the reload reg may not support QImode moves, so use SImode.
6696 If foo is in memory due to spilling a pseudo reg, this is safe,
6697 because the QImode value is in the least significant part of a
6698 slot big enough for a SImode. If foo is some other sort of
6699 memory reference, then it is impossible to reload this case,
6700 so previous passes had better make sure this never happens.
6702 Then consider a one-word union which has SImode and one of its
6703 members is a float, being fetched as (SUBREG:SF union:SI).
6704 We must fetch that as SFmode because we could be loading into
6705 a float-only register. In this case OLD's mode is correct.
6707 Consider an immediate integer: it has VOIDmode. Here we need
6708 to get a mode from something else.
6710 In some cases, there is a fourth mode, the operand's
6711 containing mode. If the insn specifies a containing mode for
6712 this operand, it overrides all others.
6714 I am not sure whether the algorithm here is always right,
6715 but it does the right things in those cases. */
6717 mode = GET_MODE (old);
6718 if (mode == VOIDmode)
6719 mode = rl->inmode;
6721 /* delete_output_reload is only invoked properly if old contains
6722 the original pseudo register. Since this is replaced with a
6723 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6724 find the pseudo in RELOAD_IN_REG. */
6725 if (reload_override_in[j]
6726 && REG_P (rl->in_reg))
6728 oldequiv = old;
6729 old = rl->in_reg;
6731 if (oldequiv == 0)
6732 oldequiv = old;
6733 else if (REG_P (oldequiv))
6734 oldequiv_reg = oldequiv;
6735 else if (GET_CODE (oldequiv) == SUBREG)
6736 oldequiv_reg = SUBREG_REG (oldequiv);
6738 /* If we are reloading from a register that was recently stored in
6739 with an output-reload, see if we can prove there was
6740 actually no need to store the old value in it. */
6742 if (optimize && REG_P (oldequiv)
6743 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6744 && spill_reg_store[REGNO (oldequiv)]
6745 && REG_P (old)
6746 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6747 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6748 rl->out_reg)))
6749 delete_output_reload (insn, j, REGNO (oldequiv));
6751 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
6752 then load RELOADREG from OLDEQUIV. Note that we cannot use
6753 gen_lowpart_common since it can do the wrong thing when
6754 RELOADREG has a multi-word mode. Note that RELOADREG
6755 must always be a REG here. */
6757 if (GET_MODE (reloadreg) != mode)
6758 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
6759 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6760 oldequiv = SUBREG_REG (oldequiv);
6761 if (GET_MODE (oldequiv) != VOIDmode
6762 && mode != GET_MODE (oldequiv))
6763 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6765 /* Switch to the right place to emit the reload insns. */
6766 switch (rl->when_needed)
6768 case RELOAD_OTHER:
6769 where = &other_input_reload_insns;
6770 break;
6771 case RELOAD_FOR_INPUT:
6772 where = &input_reload_insns[rl->opnum];
6773 break;
6774 case RELOAD_FOR_INPUT_ADDRESS:
6775 where = &input_address_reload_insns[rl->opnum];
6776 break;
6777 case RELOAD_FOR_INPADDR_ADDRESS:
6778 where = &inpaddr_address_reload_insns[rl->opnum];
6779 break;
6780 case RELOAD_FOR_OUTPUT_ADDRESS:
6781 where = &output_address_reload_insns[rl->opnum];
6782 break;
6783 case RELOAD_FOR_OUTADDR_ADDRESS:
6784 where = &outaddr_address_reload_insns[rl->opnum];
6785 break;
6786 case RELOAD_FOR_OPERAND_ADDRESS:
6787 where = &operand_reload_insns;
6788 break;
6789 case RELOAD_FOR_OPADDR_ADDR:
6790 where = &other_operand_reload_insns;
6791 break;
6792 case RELOAD_FOR_OTHER_ADDRESS:
6793 where = &other_input_address_reload_insns;
6794 break;
6795 default:
6796 gcc_unreachable ();
6799 push_to_sequence (*where);
6801 /* Auto-increment addresses must be reloaded in a special way. */
6802 if (rl->out && ! rl->out_reg)
6804 /* We are not going to bother supporting the case where a
6805 incremented register can't be copied directly from
6806 OLDEQUIV since this seems highly unlikely. */
6807 gcc_assert (rl->secondary_in_reload < 0);
6809 if (reload_inherited[j])
6810 oldequiv = reloadreg;
6812 old = XEXP (rl->in_reg, 0);
6814 if (optimize && REG_P (oldequiv)
6815 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6816 && spill_reg_store[REGNO (oldequiv)]
6817 && REG_P (old)
6818 && (dead_or_set_p (insn,
6819 spill_reg_stored_to[REGNO (oldequiv)])
6820 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6821 old)))
6822 delete_output_reload (insn, j, REGNO (oldequiv));
6824 /* Prevent normal processing of this reload. */
6825 special = 1;
6826 /* Output a special code sequence for this case. */
6827 new_spill_reg_store[REGNO (reloadreg)]
6828 = inc_for_reload (reloadreg, oldequiv, rl->out,
6829 rl->inc);
6832 /* If we are reloading a pseudo-register that was set by the previous
6833 insn, see if we can get rid of that pseudo-register entirely
6834 by redirecting the previous insn into our reload register. */
6836 else if (optimize && REG_P (old)
6837 && REGNO (old) >= FIRST_PSEUDO_REGISTER
6838 && dead_or_set_p (insn, old)
6839 /* This is unsafe if some other reload
6840 uses the same reg first. */
6841 && ! conflicts_with_override (reloadreg)
6842 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
6843 rl->when_needed, old, rl->out, j, 0))
6845 rtx temp = PREV_INSN (insn);
6846 while (temp && NOTE_P (temp))
6847 temp = PREV_INSN (temp);
6848 if (temp
6849 && NONJUMP_INSN_P (temp)
6850 && GET_CODE (PATTERN (temp)) == SET
6851 && SET_DEST (PATTERN (temp)) == old
6852 /* Make sure we can access insn_operand_constraint. */
6853 && asm_noperands (PATTERN (temp)) < 0
6854 /* This is unsafe if operand occurs more than once in current
6855 insn. Perhaps some occurrences aren't reloaded. */
6856 && count_occurrences (PATTERN (insn), old, 0) == 1)
6858 rtx old = SET_DEST (PATTERN (temp));
6859 /* Store into the reload register instead of the pseudo. */
6860 SET_DEST (PATTERN (temp)) = reloadreg;
6862 /* Verify that resulting insn is valid. */
6863 extract_insn (temp);
6864 if (constrain_operands (1))
6866 /* If the previous insn is an output reload, the source is
6867 a reload register, and its spill_reg_store entry will
6868 contain the previous destination. This is now
6869 invalid. */
6870 if (REG_P (SET_SRC (PATTERN (temp)))
6871 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
6873 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6874 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
6877 /* If these are the only uses of the pseudo reg,
6878 pretend for GDB it lives in the reload reg we used. */
6879 if (REG_N_DEATHS (REGNO (old)) == 1
6880 && REG_N_SETS (REGNO (old)) == 1)
6882 reg_renumber[REGNO (old)] = REGNO (rl->reg_rtx);
6883 if (flag_ira)
6884 mark_allocation_change (REGNO (old));
6885 alter_reg (REGNO (old), -1, false);
6887 special = 1;
6889 else
6891 SET_DEST (PATTERN (temp)) = old;
6896 /* We can't do that, so output an insn to load RELOADREG. */
6898 /* If we have a secondary reload, pick up the secondary register
6899 and icode, if any. If OLDEQUIV and OLD are different or
6900 if this is an in-out reload, recompute whether or not we
6901 still need a secondary register and what the icode should
6902 be. If we still need a secondary register and the class or
6903 icode is different, go back to reloading from OLD if using
6904 OLDEQUIV means that we got the wrong type of register. We
6905 cannot have different class or icode due to an in-out reload
6906 because we don't make such reloads when both the input and
6907 output need secondary reload registers. */
6909 if (! special && rl->secondary_in_reload >= 0)
6911 rtx second_reload_reg = 0;
6912 rtx third_reload_reg = 0;
6913 int secondary_reload = rl->secondary_in_reload;
6914 rtx real_oldequiv = oldequiv;
6915 rtx real_old = old;
6916 rtx tmp;
6917 enum insn_code icode;
6918 enum insn_code tertiary_icode = CODE_FOR_nothing;
6920 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
6921 and similarly for OLD.
6922 See comments in get_secondary_reload in reload.c. */
6923 /* If it is a pseudo that cannot be replaced with its
6924 equivalent MEM, we must fall back to reload_in, which
6925 will have all the necessary substitutions registered.
6926 Likewise for a pseudo that can't be replaced with its
6927 equivalent constant.
6929 Take extra care for subregs of such pseudos. Note that
6930 we cannot use reg_equiv_mem in this case because it is
6931 not in the right mode. */
6933 tmp = oldequiv;
6934 if (GET_CODE (tmp) == SUBREG)
6935 tmp = SUBREG_REG (tmp);
6936 if (REG_P (tmp)
6937 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6938 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6939 || reg_equiv_constant[REGNO (tmp)] != 0))
6941 if (! reg_equiv_mem[REGNO (tmp)]
6942 || num_not_at_initial_offset
6943 || GET_CODE (oldequiv) == SUBREG)
6944 real_oldequiv = rl->in;
6945 else
6946 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
6949 tmp = old;
6950 if (GET_CODE (tmp) == SUBREG)
6951 tmp = SUBREG_REG (tmp);
6952 if (REG_P (tmp)
6953 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
6954 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
6955 || reg_equiv_constant[REGNO (tmp)] != 0))
6957 if (! reg_equiv_mem[REGNO (tmp)]
6958 || num_not_at_initial_offset
6959 || GET_CODE (old) == SUBREG)
6960 real_old = rl->in;
6961 else
6962 real_old = reg_equiv_mem[REGNO (tmp)];
6965 second_reload_reg = rld[secondary_reload].reg_rtx;
6966 if (rld[secondary_reload].secondary_in_reload >= 0)
6968 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
6970 third_reload_reg = rld[tertiary_reload].reg_rtx;
6971 tertiary_icode = rld[secondary_reload].secondary_in_icode;
6972 /* We'd have to add more code for quartary reloads. */
6973 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
6975 icode = rl->secondary_in_icode;
6977 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
6978 || (rl->in != 0 && rl->out != 0))
6980 secondary_reload_info sri, sri2;
6981 enum reg_class new_class, new_t_class;
6983 sri.icode = CODE_FOR_nothing;
6984 sri.prev_sri = NULL;
6985 new_class = targetm.secondary_reload (1, real_oldequiv, rl->class,
6986 mode, &sri);
6988 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
6989 second_reload_reg = 0;
6990 else if (new_class == NO_REGS)
6992 if (reload_adjust_reg_for_icode (&second_reload_reg,
6993 third_reload_reg, sri.icode))
6994 icode = sri.icode, third_reload_reg = 0;
6995 else
6996 oldequiv = old, real_oldequiv = real_old;
6998 else if (sri.icode != CODE_FOR_nothing)
6999 /* We currently lack a way to express this in reloads. */
7000 gcc_unreachable ();
7001 else
7003 sri2.icode = CODE_FOR_nothing;
7004 sri2.prev_sri = &sri;
7005 new_t_class = targetm.secondary_reload (1, real_oldequiv,
7006 new_class, mode, &sri);
7007 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
7009 if (reload_adjust_reg_for_temp (&second_reload_reg,
7010 third_reload_reg,
7011 new_class, mode))
7012 third_reload_reg = 0, tertiary_icode = sri2.icode;
7013 else
7014 oldequiv = old, real_oldequiv = real_old;
7016 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
7018 rtx intermediate = second_reload_reg;
7020 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7021 new_class, mode)
7022 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
7023 sri2.icode))
7025 second_reload_reg = intermediate;
7026 tertiary_icode = sri2.icode;
7028 else
7029 oldequiv = old, real_oldequiv = real_old;
7031 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
7033 rtx intermediate = second_reload_reg;
7035 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7036 new_class, mode)
7037 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
7038 new_t_class, mode))
7040 second_reload_reg = intermediate;
7041 tertiary_icode = sri2.icode;
7043 else
7044 oldequiv = old, real_oldequiv = real_old;
7046 else
7047 /* This could be handled more intelligently too. */
7048 oldequiv = old, real_oldequiv = real_old;
7052 /* If we still need a secondary reload register, check
7053 to see if it is being used as a scratch or intermediate
7054 register and generate code appropriately. If we need
7055 a scratch register, use REAL_OLDEQUIV since the form of
7056 the insn may depend on the actual address if it is
7057 a MEM. */
7059 if (second_reload_reg)
7061 if (icode != CODE_FOR_nothing)
7063 /* We'd have to add extra code to handle this case. */
7064 gcc_assert (!third_reload_reg);
7066 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7067 second_reload_reg));
7068 special = 1;
7070 else
7072 /* See if we need a scratch register to load the
7073 intermediate register (a tertiary reload). */
7074 if (tertiary_icode != CODE_FOR_nothing)
7076 emit_insn ((GEN_FCN (tertiary_icode)
7077 (second_reload_reg, real_oldequiv,
7078 third_reload_reg)));
7080 else if (third_reload_reg)
7082 gen_reload (third_reload_reg, real_oldequiv,
7083 rl->opnum,
7084 rl->when_needed);
7085 gen_reload (second_reload_reg, third_reload_reg,
7086 rl->opnum,
7087 rl->when_needed);
7089 else
7090 gen_reload (second_reload_reg, real_oldequiv,
7091 rl->opnum,
7092 rl->when_needed);
7094 oldequiv = second_reload_reg;
7099 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7101 rtx real_oldequiv = oldequiv;
7103 if ((REG_P (oldequiv)
7104 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7105 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
7106 || reg_equiv_constant[REGNO (oldequiv)] != 0))
7107 || (GET_CODE (oldequiv) == SUBREG
7108 && REG_P (SUBREG_REG (oldequiv))
7109 && (REGNO (SUBREG_REG (oldequiv))
7110 >= FIRST_PSEUDO_REGISTER)
7111 && ((reg_equiv_memory_loc
7112 [REGNO (SUBREG_REG (oldequiv))] != 0)
7113 || (reg_equiv_constant
7114 [REGNO (SUBREG_REG (oldequiv))] != 0)))
7115 || (CONSTANT_P (oldequiv)
7116 && (PREFERRED_RELOAD_CLASS (oldequiv,
7117 REGNO_REG_CLASS (REGNO (reloadreg)))
7118 == NO_REGS)))
7119 real_oldequiv = rl->in;
7120 gen_reload (reloadreg, real_oldequiv, rl->opnum,
7121 rl->when_needed);
7124 if (flag_non_call_exceptions)
7125 copy_eh_notes (insn, get_insns ());
7127 /* End this sequence. */
7128 *where = get_insns ();
7129 end_sequence ();
7131 /* Update reload_override_in so that delete_address_reloads_1
7132 can see the actual register usage. */
7133 if (oldequiv_reg)
7134 reload_override_in[j] = oldequiv;
7137 /* Generate insns to for the output reload RL, which is for the insn described
7138 by CHAIN and has the number J. */
7139 static void
7140 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
7141 int j)
7143 rtx reloadreg = rl->reg_rtx;
7144 rtx insn = chain->insn;
7145 int special = 0;
7146 rtx old = rl->out;
7147 enum machine_mode mode = GET_MODE (old);
7148 rtx p;
7150 if (rl->when_needed == RELOAD_OTHER)
7151 start_sequence ();
7152 else
7153 push_to_sequence (output_reload_insns[rl->opnum]);
7155 /* Determine the mode to reload in.
7156 See comments above (for input reloading). */
7158 if (mode == VOIDmode)
7160 /* VOIDmode should never happen for an output. */
7161 if (asm_noperands (PATTERN (insn)) < 0)
7162 /* It's the compiler's fault. */
7163 fatal_insn ("VOIDmode on an output", insn);
7164 error_for_asm (insn, "output operand is constant in %<asm%>");
7165 /* Prevent crash--use something we know is valid. */
7166 mode = word_mode;
7167 old = gen_rtx_REG (mode, REGNO (reloadreg));
7170 if (GET_MODE (reloadreg) != mode)
7171 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7173 /* If we need two reload regs, set RELOADREG to the intermediate
7174 one, since it will be stored into OLD. We might need a secondary
7175 register only for an input reload, so check again here. */
7177 if (rl->secondary_out_reload >= 0)
7179 rtx real_old = old;
7180 int secondary_reload = rl->secondary_out_reload;
7181 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
7183 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
7184 && reg_equiv_mem[REGNO (old)] != 0)
7185 real_old = reg_equiv_mem[REGNO (old)];
7187 if (secondary_reload_class (0, rl->class, mode, real_old) != NO_REGS)
7189 rtx second_reloadreg = reloadreg;
7190 reloadreg = rld[secondary_reload].reg_rtx;
7192 /* See if RELOADREG is to be used as a scratch register
7193 or as an intermediate register. */
7194 if (rl->secondary_out_icode != CODE_FOR_nothing)
7196 /* We'd have to add extra code to handle this case. */
7197 gcc_assert (tertiary_reload < 0);
7199 emit_insn ((GEN_FCN (rl->secondary_out_icode)
7200 (real_old, second_reloadreg, reloadreg)));
7201 special = 1;
7203 else
7205 /* See if we need both a scratch and intermediate reload
7206 register. */
7208 enum insn_code tertiary_icode
7209 = rld[secondary_reload].secondary_out_icode;
7211 /* We'd have to add more code for quartary reloads. */
7212 gcc_assert (tertiary_reload < 0
7213 || rld[tertiary_reload].secondary_out_reload < 0);
7215 if (GET_MODE (reloadreg) != mode)
7216 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7218 if (tertiary_icode != CODE_FOR_nothing)
7220 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7221 rtx tem;
7223 /* Copy primary reload reg to secondary reload reg.
7224 (Note that these have been swapped above, then
7225 secondary reload reg to OLD using our insn.) */
7227 /* If REAL_OLD is a paradoxical SUBREG, remove it
7228 and try to put the opposite SUBREG on
7229 RELOADREG. */
7230 if (GET_CODE (real_old) == SUBREG
7231 && (GET_MODE_SIZE (GET_MODE (real_old))
7232 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7233 && 0 != (tem = gen_lowpart_common
7234 (GET_MODE (SUBREG_REG (real_old)),
7235 reloadreg)))
7236 real_old = SUBREG_REG (real_old), reloadreg = tem;
7238 gen_reload (reloadreg, second_reloadreg,
7239 rl->opnum, rl->when_needed);
7240 emit_insn ((GEN_FCN (tertiary_icode)
7241 (real_old, reloadreg, third_reloadreg)));
7242 special = 1;
7245 else
7247 /* Copy between the reload regs here and then to
7248 OUT later. */
7250 gen_reload (reloadreg, second_reloadreg,
7251 rl->opnum, rl->when_needed);
7252 if (tertiary_reload >= 0)
7254 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7256 gen_reload (third_reloadreg, reloadreg,
7257 rl->opnum, rl->when_needed);
7258 reloadreg = third_reloadreg;
7265 /* Output the last reload insn. */
7266 if (! special)
7268 rtx set;
7270 /* Don't output the last reload if OLD is not the dest of
7271 INSN and is in the src and is clobbered by INSN. */
7272 if (! flag_expensive_optimizations
7273 || !REG_P (old)
7274 || !(set = single_set (insn))
7275 || rtx_equal_p (old, SET_DEST (set))
7276 || !reg_mentioned_p (old, SET_SRC (set))
7277 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7278 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7279 gen_reload (old, reloadreg, rl->opnum,
7280 rl->when_needed);
7283 /* Look at all insns we emitted, just to be safe. */
7284 for (p = get_insns (); p; p = NEXT_INSN (p))
7285 if (INSN_P (p))
7287 rtx pat = PATTERN (p);
7289 /* If this output reload doesn't come from a spill reg,
7290 clear any memory of reloaded copies of the pseudo reg.
7291 If this output reload comes from a spill reg,
7292 reg_has_output_reload will make this do nothing. */
7293 note_stores (pat, forget_old_reloads_1, NULL);
7295 if (reg_mentioned_p (rl->reg_rtx, pat))
7297 rtx set = single_set (insn);
7298 if (reload_spill_index[j] < 0
7299 && set
7300 && SET_SRC (set) == rl->reg_rtx)
7302 int src = REGNO (SET_SRC (set));
7304 reload_spill_index[j] = src;
7305 SET_HARD_REG_BIT (reg_is_output_reload, src);
7306 if (find_regno_note (insn, REG_DEAD, src))
7307 SET_HARD_REG_BIT (reg_reloaded_died, src);
7309 if (REGNO (rl->reg_rtx) < FIRST_PSEUDO_REGISTER)
7311 int s = rl->secondary_out_reload;
7312 set = single_set (p);
7313 /* If this reload copies only to the secondary reload
7314 register, the secondary reload does the actual
7315 store. */
7316 if (s >= 0 && set == NULL_RTX)
7317 /* We can't tell what function the secondary reload
7318 has and where the actual store to the pseudo is
7319 made; leave new_spill_reg_store alone. */
7321 else if (s >= 0
7322 && SET_SRC (set) == rl->reg_rtx
7323 && SET_DEST (set) == rld[s].reg_rtx)
7325 /* Usually the next instruction will be the
7326 secondary reload insn; if we can confirm
7327 that it is, setting new_spill_reg_store to
7328 that insn will allow an extra optimization. */
7329 rtx s_reg = rld[s].reg_rtx;
7330 rtx next = NEXT_INSN (p);
7331 rld[s].out = rl->out;
7332 rld[s].out_reg = rl->out_reg;
7333 set = single_set (next);
7334 if (set && SET_SRC (set) == s_reg
7335 && ! new_spill_reg_store[REGNO (s_reg)])
7337 SET_HARD_REG_BIT (reg_is_output_reload,
7338 REGNO (s_reg));
7339 new_spill_reg_store[REGNO (s_reg)] = next;
7342 else
7343 new_spill_reg_store[REGNO (rl->reg_rtx)] = p;
7348 if (rl->when_needed == RELOAD_OTHER)
7350 emit_insn (other_output_reload_insns[rl->opnum]);
7351 other_output_reload_insns[rl->opnum] = get_insns ();
7353 else
7354 output_reload_insns[rl->opnum] = get_insns ();
7356 if (flag_non_call_exceptions)
7357 copy_eh_notes (insn, get_insns ());
7359 end_sequence ();
7362 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7363 and has the number J. */
7364 static void
7365 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7367 rtx insn = chain->insn;
7368 rtx old = (rl->in && MEM_P (rl->in)
7369 ? rl->in_reg : rl->in);
7371 if (old != 0
7372 /* AUTO_INC reloads need to be handled even if inherited. We got an
7373 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7374 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7375 && ! rtx_equal_p (rl->reg_rtx, old)
7376 && rl->reg_rtx != 0)
7377 emit_input_reload_insns (chain, rld + j, old, j);
7379 /* When inheriting a wider reload, we have a MEM in rl->in,
7380 e.g. inheriting a SImode output reload for
7381 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7382 if (optimize && reload_inherited[j] && rl->in
7383 && MEM_P (rl->in)
7384 && MEM_P (rl->in_reg)
7385 && reload_spill_index[j] >= 0
7386 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7387 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7389 /* If we are reloading a register that was recently stored in with an
7390 output-reload, see if we can prove there was
7391 actually no need to store the old value in it. */
7393 if (optimize
7394 && (reload_inherited[j] || reload_override_in[j])
7395 && rl->reg_rtx
7396 && REG_P (rl->reg_rtx)
7397 && spill_reg_store[REGNO (rl->reg_rtx)] != 0
7398 #if 0
7399 /* There doesn't seem to be any reason to restrict this to pseudos
7400 and doing so loses in the case where we are copying from a
7401 register of the wrong class. */
7402 && (REGNO (spill_reg_stored_to[REGNO (rl->reg_rtx)])
7403 >= FIRST_PSEUDO_REGISTER)
7404 #endif
7405 /* The insn might have already some references to stackslots
7406 replaced by MEMs, while reload_out_reg still names the
7407 original pseudo. */
7408 && (dead_or_set_p (insn,
7409 spill_reg_stored_to[REGNO (rl->reg_rtx)])
7410 || rtx_equal_p (spill_reg_stored_to[REGNO (rl->reg_rtx)],
7411 rl->out_reg)))
7412 delete_output_reload (insn, j, REGNO (rl->reg_rtx));
7415 /* Do output reloading for reload RL, which is for the insn described by
7416 CHAIN and has the number J.
7417 ??? At some point we need to support handling output reloads of
7418 JUMP_INSNs or insns that set cc0. */
7419 static void
7420 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7422 rtx note, old;
7423 rtx insn = chain->insn;
7424 /* If this is an output reload that stores something that is
7425 not loaded in this same reload, see if we can eliminate a previous
7426 store. */
7427 rtx pseudo = rl->out_reg;
7429 if (pseudo
7430 && optimize
7431 && REG_P (pseudo)
7432 && ! rtx_equal_p (rl->in_reg, pseudo)
7433 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7434 && reg_last_reload_reg[REGNO (pseudo)])
7436 int pseudo_no = REGNO (pseudo);
7437 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7439 /* We don't need to test full validity of last_regno for
7440 inherit here; we only want to know if the store actually
7441 matches the pseudo. */
7442 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7443 && reg_reloaded_contents[last_regno] == pseudo_no
7444 && spill_reg_store[last_regno]
7445 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7446 delete_output_reload (insn, j, last_regno);
7449 old = rl->out_reg;
7450 if (old == 0
7451 || rl->reg_rtx == old
7452 || rl->reg_rtx == 0)
7453 return;
7455 /* An output operand that dies right away does need a reload,
7456 but need not be copied from it. Show the new location in the
7457 REG_UNUSED note. */
7458 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7459 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7461 XEXP (note, 0) = rl->reg_rtx;
7462 return;
7464 /* Likewise for a SUBREG of an operand that dies. */
7465 else if (GET_CODE (old) == SUBREG
7466 && REG_P (SUBREG_REG (old))
7467 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7468 SUBREG_REG (old))))
7470 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old),
7471 rl->reg_rtx);
7472 return;
7474 else if (GET_CODE (old) == SCRATCH)
7475 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7476 but we don't want to make an output reload. */
7477 return;
7479 /* If is a JUMP_INSN, we can't support output reloads yet. */
7480 gcc_assert (NONJUMP_INSN_P (insn));
7482 emit_output_reload_insns (chain, rld + j, j);
7485 /* Reload number R reloads from or to a group of hard registers starting at
7486 register REGNO. Return true if it can be treated for inheritance purposes
7487 like a group of reloads, each one reloading a single hard register.
7488 The caller has already checked that the spill register and REGNO use
7489 the same number of registers to store the reload value. */
7491 static bool
7492 inherit_piecemeal_p (int r ATTRIBUTE_UNUSED, int regno ATTRIBUTE_UNUSED)
7494 #ifdef CANNOT_CHANGE_MODE_CLASS
7495 return (!REG_CANNOT_CHANGE_MODE_P (reload_spill_index[r],
7496 GET_MODE (rld[r].reg_rtx),
7497 reg_raw_mode[reload_spill_index[r]])
7498 && !REG_CANNOT_CHANGE_MODE_P (regno,
7499 GET_MODE (rld[r].reg_rtx),
7500 reg_raw_mode[regno]));
7501 #else
7502 return true;
7503 #endif
7506 /* Output insns to reload values in and out of the chosen reload regs. */
7508 static void
7509 emit_reload_insns (struct insn_chain *chain)
7511 rtx insn = chain->insn;
7513 int j;
7515 CLEAR_HARD_REG_SET (reg_reloaded_died);
7517 for (j = 0; j < reload_n_operands; j++)
7518 input_reload_insns[j] = input_address_reload_insns[j]
7519 = inpaddr_address_reload_insns[j]
7520 = output_reload_insns[j] = output_address_reload_insns[j]
7521 = outaddr_address_reload_insns[j]
7522 = other_output_reload_insns[j] = 0;
7523 other_input_address_reload_insns = 0;
7524 other_input_reload_insns = 0;
7525 operand_reload_insns = 0;
7526 other_operand_reload_insns = 0;
7528 /* Dump reloads into the dump file. */
7529 if (dump_file)
7531 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7532 debug_reload_to_stream (dump_file);
7535 /* Now output the instructions to copy the data into and out of the
7536 reload registers. Do these in the order that the reloads were reported,
7537 since reloads of base and index registers precede reloads of operands
7538 and the operands may need the base and index registers reloaded. */
7540 for (j = 0; j < n_reloads; j++)
7542 if (rld[j].reg_rtx
7543 && REGNO (rld[j].reg_rtx) < FIRST_PSEUDO_REGISTER)
7544 new_spill_reg_store[REGNO (rld[j].reg_rtx)] = 0;
7546 do_input_reload (chain, rld + j, j);
7547 do_output_reload (chain, rld + j, j);
7550 /* Now write all the insns we made for reloads in the order expected by
7551 the allocation functions. Prior to the insn being reloaded, we write
7552 the following reloads:
7554 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7556 RELOAD_OTHER reloads.
7558 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7559 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7560 RELOAD_FOR_INPUT reload for the operand.
7562 RELOAD_FOR_OPADDR_ADDRS reloads.
7564 RELOAD_FOR_OPERAND_ADDRESS reloads.
7566 After the insn being reloaded, we write the following:
7568 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7569 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7570 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7571 reloads for the operand. The RELOAD_OTHER output reloads are
7572 output in descending order by reload number. */
7574 emit_insn_before (other_input_address_reload_insns, insn);
7575 emit_insn_before (other_input_reload_insns, insn);
7577 for (j = 0; j < reload_n_operands; j++)
7579 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7580 emit_insn_before (input_address_reload_insns[j], insn);
7581 emit_insn_before (input_reload_insns[j], insn);
7584 emit_insn_before (other_operand_reload_insns, insn);
7585 emit_insn_before (operand_reload_insns, insn);
7587 for (j = 0; j < reload_n_operands; j++)
7589 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7590 x = emit_insn_after (output_address_reload_insns[j], x);
7591 x = emit_insn_after (output_reload_insns[j], x);
7592 emit_insn_after (other_output_reload_insns[j], x);
7595 /* For all the spill regs newly reloaded in this instruction,
7596 record what they were reloaded from, so subsequent instructions
7597 can inherit the reloads.
7599 Update spill_reg_store for the reloads of this insn.
7600 Copy the elements that were updated in the loop above. */
7602 for (j = 0; j < n_reloads; j++)
7604 int r = reload_order[j];
7605 int i = reload_spill_index[r];
7607 /* If this is a non-inherited input reload from a pseudo, we must
7608 clear any memory of a previous store to the same pseudo. Only do
7609 something if there will not be an output reload for the pseudo
7610 being reloaded. */
7611 if (rld[r].in_reg != 0
7612 && ! (reload_inherited[r] || reload_override_in[r]))
7614 rtx reg = rld[r].in_reg;
7616 if (GET_CODE (reg) == SUBREG)
7617 reg = SUBREG_REG (reg);
7619 if (REG_P (reg)
7620 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7621 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
7623 int nregno = REGNO (reg);
7625 if (reg_last_reload_reg[nregno])
7627 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7629 if (reg_reloaded_contents[last_regno] == nregno)
7630 spill_reg_store[last_regno] = 0;
7635 /* I is nonneg if this reload used a register.
7636 If rld[r].reg_rtx is 0, this is an optional reload
7637 that we opted to ignore. */
7639 if (i >= 0 && rld[r].reg_rtx != 0)
7641 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7642 int k;
7643 int part_reaches_end = 0;
7644 int all_reaches_end = 1;
7646 /* For a multi register reload, we need to check if all or part
7647 of the value lives to the end. */
7648 for (k = 0; k < nr; k++)
7650 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7651 rld[r].when_needed))
7652 part_reaches_end = 1;
7653 else
7654 all_reaches_end = 0;
7657 /* Ignore reloads that don't reach the end of the insn in
7658 entirety. */
7659 if (all_reaches_end)
7661 /* First, clear out memory of what used to be in this spill reg.
7662 If consecutive registers are used, clear them all. */
7664 for (k = 0; k < nr; k++)
7666 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7667 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7670 /* Maybe the spill reg contains a copy of reload_out. */
7671 if (rld[r].out != 0
7672 && (REG_P (rld[r].out)
7673 #ifdef AUTO_INC_DEC
7674 || ! rld[r].out_reg
7675 #endif
7676 || REG_P (rld[r].out_reg)))
7678 rtx out = (REG_P (rld[r].out)
7679 ? rld[r].out
7680 : rld[r].out_reg
7681 ? rld[r].out_reg
7682 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7683 int nregno = REGNO (out);
7684 int nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7685 : hard_regno_nregs[nregno]
7686 [GET_MODE (rld[r].reg_rtx)]);
7687 bool piecemeal;
7689 spill_reg_store[i] = new_spill_reg_store[i];
7690 spill_reg_stored_to[i] = out;
7691 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7693 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7694 && nr == nnr
7695 && inherit_piecemeal_p (r, nregno));
7697 /* If NREGNO is a hard register, it may occupy more than
7698 one register. If it does, say what is in the
7699 rest of the registers assuming that both registers
7700 agree on how many words the object takes. If not,
7701 invalidate the subsequent registers. */
7703 if (nregno < FIRST_PSEUDO_REGISTER)
7704 for (k = 1; k < nnr; k++)
7705 reg_last_reload_reg[nregno + k]
7706 = (piecemeal
7707 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7708 : 0);
7710 /* Now do the inverse operation. */
7711 for (k = 0; k < nr; k++)
7713 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7714 reg_reloaded_contents[i + k]
7715 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7716 ? nregno
7717 : nregno + k);
7718 reg_reloaded_insn[i + k] = insn;
7719 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7720 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (out)))
7721 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7725 /* Maybe the spill reg contains a copy of reload_in. Only do
7726 something if there will not be an output reload for
7727 the register being reloaded. */
7728 else if (rld[r].out_reg == 0
7729 && rld[r].in != 0
7730 && ((REG_P (rld[r].in)
7731 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER
7732 && !REGNO_REG_SET_P (&reg_has_output_reload,
7733 REGNO (rld[r].in)))
7734 || (REG_P (rld[r].in_reg)
7735 && !REGNO_REG_SET_P (&reg_has_output_reload,
7736 REGNO (rld[r].in_reg))))
7737 && ! reg_set_p (rld[r].reg_rtx, PATTERN (insn)))
7739 int nregno;
7740 int nnr;
7741 rtx in;
7742 bool piecemeal;
7744 if (REG_P (rld[r].in)
7745 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
7746 in = rld[r].in;
7747 else if (REG_P (rld[r].in_reg))
7748 in = rld[r].in_reg;
7749 else
7750 in = XEXP (rld[r].in_reg, 0);
7751 nregno = REGNO (in);
7753 nnr = (nregno >= FIRST_PSEUDO_REGISTER ? 1
7754 : hard_regno_nregs[nregno]
7755 [GET_MODE (rld[r].reg_rtx)]);
7757 reg_last_reload_reg[nregno] = rld[r].reg_rtx;
7759 piecemeal = (nregno < FIRST_PSEUDO_REGISTER
7760 && nr == nnr
7761 && inherit_piecemeal_p (r, nregno));
7763 if (nregno < FIRST_PSEUDO_REGISTER)
7764 for (k = 1; k < nnr; k++)
7765 reg_last_reload_reg[nregno + k]
7766 = (piecemeal
7767 ? regno_reg_rtx[REGNO (rld[r].reg_rtx) + k]
7768 : 0);
7770 /* Unless we inherited this reload, show we haven't
7771 recently done a store.
7772 Previous stores of inherited auto_inc expressions
7773 also have to be discarded. */
7774 if (! reload_inherited[r]
7775 || (rld[r].out && ! rld[r].out_reg))
7776 spill_reg_store[i] = 0;
7778 for (k = 0; k < nr; k++)
7780 CLEAR_HARD_REG_BIT (reg_reloaded_dead, i + k);
7781 reg_reloaded_contents[i + k]
7782 = (nregno >= FIRST_PSEUDO_REGISTER || !piecemeal
7783 ? nregno
7784 : nregno + k);
7785 reg_reloaded_insn[i + k] = insn;
7786 SET_HARD_REG_BIT (reg_reloaded_valid, i + k);
7787 if (HARD_REGNO_CALL_PART_CLOBBERED (i + k, GET_MODE (in)))
7788 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered, i + k);
7793 /* However, if part of the reload reaches the end, then we must
7794 invalidate the old info for the part that survives to the end. */
7795 else if (part_reaches_end)
7797 for (k = 0; k < nr; k++)
7798 if (reload_reg_reaches_end_p (i + k,
7799 rld[r].opnum,
7800 rld[r].when_needed))
7801 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7805 /* The following if-statement was #if 0'd in 1.34 (or before...).
7806 It's reenabled in 1.35 because supposedly nothing else
7807 deals with this problem. */
7809 /* If a register gets output-reloaded from a non-spill register,
7810 that invalidates any previous reloaded copy of it.
7811 But forget_old_reloads_1 won't get to see it, because
7812 it thinks only about the original insn. So invalidate it here.
7813 Also do the same thing for RELOAD_OTHER constraints where the
7814 output is discarded. */
7815 if (i < 0
7816 && ((rld[r].out != 0
7817 && (REG_P (rld[r].out)
7818 || (MEM_P (rld[r].out)
7819 && REG_P (rld[r].out_reg))))
7820 || (rld[r].out == 0 && rld[r].out_reg
7821 && REG_P (rld[r].out_reg))))
7823 rtx out = ((rld[r].out && REG_P (rld[r].out))
7824 ? rld[r].out : rld[r].out_reg);
7825 int nregno = REGNO (out);
7827 /* REG_RTX is now set or clobbered by the main instruction.
7828 As the comment above explains, forget_old_reloads_1 only
7829 sees the original instruction, and there is no guarantee
7830 that the original instruction also clobbered REG_RTX.
7831 For example, if find_reloads sees that the input side of
7832 a matched operand pair dies in this instruction, it may
7833 use the input register as the reload register.
7835 Calling forget_old_reloads_1 is a waste of effort if
7836 REG_RTX is also the output register.
7838 If we know that REG_RTX holds the value of a pseudo
7839 register, the code after the call will record that fact. */
7840 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
7841 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
7843 if (nregno >= FIRST_PSEUDO_REGISTER)
7845 rtx src_reg, store_insn = NULL_RTX;
7847 reg_last_reload_reg[nregno] = 0;
7849 /* If we can find a hard register that is stored, record
7850 the storing insn so that we may delete this insn with
7851 delete_output_reload. */
7852 src_reg = rld[r].reg_rtx;
7854 /* If this is an optional reload, try to find the source reg
7855 from an input reload. */
7856 if (! src_reg)
7858 rtx set = single_set (insn);
7859 if (set && SET_DEST (set) == rld[r].out)
7861 int k;
7863 src_reg = SET_SRC (set);
7864 store_insn = insn;
7865 for (k = 0; k < n_reloads; k++)
7867 if (rld[k].in == src_reg)
7869 src_reg = rld[k].reg_rtx;
7870 break;
7875 else
7876 store_insn = new_spill_reg_store[REGNO (src_reg)];
7877 if (src_reg && REG_P (src_reg)
7878 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
7880 int src_regno = REGNO (src_reg);
7881 int nr = hard_regno_nregs[src_regno][rld[r].mode];
7882 /* The place where to find a death note varies with
7883 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
7884 necessarily checked exactly in the code that moves
7885 notes, so just check both locations. */
7886 rtx note = find_regno_note (insn, REG_DEAD, src_regno);
7887 if (! note && store_insn)
7888 note = find_regno_note (store_insn, REG_DEAD, src_regno);
7889 while (nr-- > 0)
7891 spill_reg_store[src_regno + nr] = store_insn;
7892 spill_reg_stored_to[src_regno + nr] = out;
7893 reg_reloaded_contents[src_regno + nr] = nregno;
7894 reg_reloaded_insn[src_regno + nr] = store_insn;
7895 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + nr);
7896 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + nr);
7897 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + nr,
7898 GET_MODE (src_reg)))
7899 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7900 src_regno + nr);
7901 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + nr);
7902 if (note)
7903 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
7904 else
7905 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
7907 reg_last_reload_reg[nregno] = src_reg;
7908 /* We have to set reg_has_output_reload here, or else
7909 forget_old_reloads_1 will clear reg_last_reload_reg
7910 right away. */
7911 SET_REGNO_REG_SET (&reg_has_output_reload,
7912 nregno);
7915 else
7917 int num_regs = hard_regno_nregs[nregno][GET_MODE (out)];
7919 while (num_regs-- > 0)
7920 reg_last_reload_reg[nregno + num_regs] = 0;
7924 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
7927 /* Go through the motions to emit INSN and test if it is strictly valid.
7928 Return the emitted insn if valid, else return NULL. */
7930 static rtx
7931 emit_insn_if_valid_for_reload (rtx insn)
7933 rtx last = get_last_insn ();
7934 int code;
7936 insn = emit_insn (insn);
7937 code = recog_memoized (insn);
7939 if (code >= 0)
7941 extract_insn (insn);
7942 /* We want constrain operands to treat this insn strictly in its
7943 validity determination, i.e., the way it would after reload has
7944 completed. */
7945 if (constrain_operands (1))
7946 return insn;
7949 delete_insns_since (last);
7950 return NULL;
7953 /* Emit code to perform a reload from IN (which may be a reload register) to
7954 OUT (which may also be a reload register). IN or OUT is from operand
7955 OPNUM with reload type TYPE.
7957 Returns first insn emitted. */
7959 static rtx
7960 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
7962 rtx last = get_last_insn ();
7963 rtx tem;
7965 /* If IN is a paradoxical SUBREG, remove it and try to put the
7966 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
7967 if (GET_CODE (in) == SUBREG
7968 && (GET_MODE_SIZE (GET_MODE (in))
7969 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
7970 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
7971 in = SUBREG_REG (in), out = tem;
7972 else if (GET_CODE (out) == SUBREG
7973 && (GET_MODE_SIZE (GET_MODE (out))
7974 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
7975 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
7976 out = SUBREG_REG (out), in = tem;
7978 /* How to do this reload can get quite tricky. Normally, we are being
7979 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
7980 register that didn't get a hard register. In that case we can just
7981 call emit_move_insn.
7983 We can also be asked to reload a PLUS that adds a register or a MEM to
7984 another register, constant or MEM. This can occur during frame pointer
7985 elimination and while reloading addresses. This case is handled by
7986 trying to emit a single insn to perform the add. If it is not valid,
7987 we use a two insn sequence.
7989 Or we can be asked to reload an unary operand that was a fragment of
7990 an addressing mode, into a register. If it isn't recognized as-is,
7991 we try making the unop operand and the reload-register the same:
7992 (set reg:X (unop:X expr:Y))
7993 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
7995 Finally, we could be called to handle an 'o' constraint by putting
7996 an address into a register. In that case, we first try to do this
7997 with a named pattern of "reload_load_address". If no such pattern
7998 exists, we just emit a SET insn and hope for the best (it will normally
7999 be valid on machines that use 'o').
8001 This entire process is made complex because reload will never
8002 process the insns we generate here and so we must ensure that
8003 they will fit their constraints and also by the fact that parts of
8004 IN might be being reloaded separately and replaced with spill registers.
8005 Because of this, we are, in some sense, just guessing the right approach
8006 here. The one listed above seems to work.
8008 ??? At some point, this whole thing needs to be rethought. */
8010 if (GET_CODE (in) == PLUS
8011 && (REG_P (XEXP (in, 0))
8012 || GET_CODE (XEXP (in, 0)) == SUBREG
8013 || MEM_P (XEXP (in, 0)))
8014 && (REG_P (XEXP (in, 1))
8015 || GET_CODE (XEXP (in, 1)) == SUBREG
8016 || CONSTANT_P (XEXP (in, 1))
8017 || MEM_P (XEXP (in, 1))))
8019 /* We need to compute the sum of a register or a MEM and another
8020 register, constant, or MEM, and put it into the reload
8021 register. The best possible way of doing this is if the machine
8022 has a three-operand ADD insn that accepts the required operands.
8024 The simplest approach is to try to generate such an insn and see if it
8025 is recognized and matches its constraints. If so, it can be used.
8027 It might be better not to actually emit the insn unless it is valid,
8028 but we need to pass the insn as an operand to `recog' and
8029 `extract_insn' and it is simpler to emit and then delete the insn if
8030 not valid than to dummy things up. */
8032 rtx op0, op1, tem, insn;
8033 int code;
8035 op0 = find_replacement (&XEXP (in, 0));
8036 op1 = find_replacement (&XEXP (in, 1));
8038 /* Since constraint checking is strict, commutativity won't be
8039 checked, so we need to do that here to avoid spurious failure
8040 if the add instruction is two-address and the second operand
8041 of the add is the same as the reload reg, which is frequently
8042 the case. If the insn would be A = B + A, rearrange it so
8043 it will be A = A + B as constrain_operands expects. */
8045 if (REG_P (XEXP (in, 1))
8046 && REGNO (out) == REGNO (XEXP (in, 1)))
8047 tem = op0, op0 = op1, op1 = tem;
8049 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
8050 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
8052 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8053 if (insn)
8054 return insn;
8056 /* If that failed, we must use a conservative two-insn sequence.
8058 Use a move to copy one operand into the reload register. Prefer
8059 to reload a constant, MEM or pseudo since the move patterns can
8060 handle an arbitrary operand. If OP1 is not a constant, MEM or
8061 pseudo and OP1 is not a valid operand for an add instruction, then
8062 reload OP1.
8064 After reloading one of the operands into the reload register, add
8065 the reload register to the output register.
8067 If there is another way to do this for a specific machine, a
8068 DEFINE_PEEPHOLE should be specified that recognizes the sequence
8069 we emit below. */
8071 code = (int) optab_handler (add_optab, GET_MODE (out))->insn_code;
8073 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
8074 || (REG_P (op1)
8075 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
8076 || (code != CODE_FOR_nothing
8077 && ! ((*insn_data[code].operand[2].predicate)
8078 (op1, insn_data[code].operand[2].mode))))
8079 tem = op0, op0 = op1, op1 = tem;
8081 gen_reload (out, op0, opnum, type);
8083 /* If OP0 and OP1 are the same, we can use OUT for OP1.
8084 This fixes a problem on the 32K where the stack pointer cannot
8085 be used as an operand of an add insn. */
8087 if (rtx_equal_p (op0, op1))
8088 op1 = out;
8090 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
8091 if (insn)
8093 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
8094 set_unique_reg_note (insn, REG_EQUIV, in);
8095 return insn;
8098 /* If that failed, copy the address register to the reload register.
8099 Then add the constant to the reload register. */
8101 gcc_assert (!reg_overlap_mentioned_p (out, op0));
8102 gen_reload (out, op1, opnum, type);
8103 insn = emit_insn (gen_add2_insn (out, op0));
8104 set_unique_reg_note (insn, REG_EQUIV, in);
8107 #ifdef SECONDARY_MEMORY_NEEDED
8108 /* If we need a memory location to do the move, do it that way. */
8109 else if ((REG_P (in) || GET_CODE (in) == SUBREG)
8110 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
8111 && (REG_P (out) || GET_CODE (out) == SUBREG)
8112 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
8113 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
8114 REGNO_REG_CLASS (reg_or_subregno (out)),
8115 GET_MODE (out)))
8117 /* Get the memory to use and rewrite both registers to its mode. */
8118 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
8120 if (GET_MODE (loc) != GET_MODE (out))
8121 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
8123 if (GET_MODE (loc) != GET_MODE (in))
8124 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
8126 gen_reload (loc, in, opnum, type);
8127 gen_reload (out, loc, opnum, type);
8129 #endif
8130 else if (REG_P (out) && UNARY_P (in))
8132 rtx insn;
8133 rtx op1;
8134 rtx out_moded;
8135 rtx set;
8137 op1 = find_replacement (&XEXP (in, 0));
8138 if (op1 != XEXP (in, 0))
8139 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
8141 /* First, try a plain SET. */
8142 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8143 if (set)
8144 return set;
8146 /* If that failed, move the inner operand to the reload
8147 register, and try the same unop with the inner expression
8148 replaced with the reload register. */
8150 if (GET_MODE (op1) != GET_MODE (out))
8151 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
8152 else
8153 out_moded = out;
8155 gen_reload (out_moded, op1, opnum, type);
8157 insn
8158 = gen_rtx_SET (VOIDmode, out,
8159 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
8160 out_moded));
8161 insn = emit_insn_if_valid_for_reload (insn);
8162 if (insn)
8164 set_unique_reg_note (insn, REG_EQUIV, in);
8165 return insn;
8168 fatal_insn ("Failure trying to reload:", set);
8170 /* If IN is a simple operand, use gen_move_insn. */
8171 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8173 tem = emit_insn (gen_move_insn (out, in));
8174 /* IN may contain a LABEL_REF, if so add a REG_LABEL_OPERAND note. */
8175 mark_jump_label (in, tem, 0);
8178 #ifdef HAVE_reload_load_address
8179 else if (HAVE_reload_load_address)
8180 emit_insn (gen_reload_load_address (out, in));
8181 #endif
8183 /* Otherwise, just write (set OUT IN) and hope for the best. */
8184 else
8185 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8187 /* Return the first insn emitted.
8188 We can not just return get_last_insn, because there may have
8189 been multiple instructions emitted. Also note that gen_move_insn may
8190 emit more than one insn itself, so we can not assume that there is one
8191 insn emitted per emit_insn_before call. */
8193 return last ? NEXT_INSN (last) : get_insns ();
8196 /* Delete a previously made output-reload whose result we now believe
8197 is not needed. First we double-check.
8199 INSN is the insn now being processed.
8200 LAST_RELOAD_REG is the hard register number for which we want to delete
8201 the last output reload.
8202 J is the reload-number that originally used REG. The caller has made
8203 certain that reload J doesn't use REG any longer for input. */
8205 static void
8206 delete_output_reload (rtx insn, int j, int last_reload_reg)
8208 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8209 rtx reg = spill_reg_stored_to[last_reload_reg];
8210 int k;
8211 int n_occurrences;
8212 int n_inherited = 0;
8213 rtx i1;
8214 rtx substed;
8216 /* It is possible that this reload has been only used to set another reload
8217 we eliminated earlier and thus deleted this instruction too. */
8218 if (INSN_DELETED_P (output_reload_insn))
8219 return;
8221 /* Get the raw pseudo-register referred to. */
8223 while (GET_CODE (reg) == SUBREG)
8224 reg = SUBREG_REG (reg);
8225 substed = reg_equiv_memory_loc[REGNO (reg)];
8227 /* This is unsafe if the operand occurs more often in the current
8228 insn than it is inherited. */
8229 for (k = n_reloads - 1; k >= 0; k--)
8231 rtx reg2 = rld[k].in;
8232 if (! reg2)
8233 continue;
8234 if (MEM_P (reg2) || reload_override_in[k])
8235 reg2 = rld[k].in_reg;
8236 #ifdef AUTO_INC_DEC
8237 if (rld[k].out && ! rld[k].out_reg)
8238 reg2 = XEXP (rld[k].in_reg, 0);
8239 #endif
8240 while (GET_CODE (reg2) == SUBREG)
8241 reg2 = SUBREG_REG (reg2);
8242 if (rtx_equal_p (reg2, reg))
8244 if (reload_inherited[k] || reload_override_in[k] || k == j)
8245 n_inherited++;
8246 else
8247 return;
8250 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
8251 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
8252 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
8253 reg, 0);
8254 if (substed)
8255 n_occurrences += count_occurrences (PATTERN (insn),
8256 eliminate_regs (substed, 0,
8257 NULL_RTX), 0);
8258 for (i1 = reg_equiv_alt_mem_list [REGNO (reg)]; i1; i1 = XEXP (i1, 1))
8260 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8261 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8263 if (n_occurrences > n_inherited)
8264 return;
8266 /* If the pseudo-reg we are reloading is no longer referenced
8267 anywhere between the store into it and here,
8268 and we're within the same basic block, then the value can only
8269 pass through the reload reg and end up here.
8270 Otherwise, give up--return. */
8271 for (i1 = NEXT_INSN (output_reload_insn);
8272 i1 != insn; i1 = NEXT_INSN (i1))
8274 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8275 return;
8276 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8277 && reg_mentioned_p (reg, PATTERN (i1)))
8279 /* If this is USE in front of INSN, we only have to check that
8280 there are no more references than accounted for by inheritance. */
8281 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8283 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8284 i1 = NEXT_INSN (i1);
8286 if (n_occurrences <= n_inherited && i1 == insn)
8287 break;
8288 return;
8292 /* We will be deleting the insn. Remove the spill reg information. */
8293 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8295 spill_reg_store[last_reload_reg + k] = 0;
8296 spill_reg_stored_to[last_reload_reg + k] = 0;
8299 /* The caller has already checked that REG dies or is set in INSN.
8300 It has also checked that we are optimizing, and thus some
8301 inaccuracies in the debugging information are acceptable.
8302 So we could just delete output_reload_insn. But in some cases
8303 we can improve the debugging information without sacrificing
8304 optimization - maybe even improving the code: See if the pseudo
8305 reg has been completely replaced with reload regs. If so, delete
8306 the store insn and forget we had a stack slot for the pseudo. */
8307 if (rld[j].out != rld[j].in
8308 && REG_N_DEATHS (REGNO (reg)) == 1
8309 && REG_N_SETS (REGNO (reg)) == 1
8310 && REG_BASIC_BLOCK (REGNO (reg)) >= NUM_FIXED_BLOCKS
8311 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8313 rtx i2;
8315 /* We know that it was used only between here and the beginning of
8316 the current basic block. (We also know that the last use before
8317 INSN was the output reload we are thinking of deleting, but never
8318 mind that.) Search that range; see if any ref remains. */
8319 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8321 rtx set = single_set (i2);
8323 /* Uses which just store in the pseudo don't count,
8324 since if they are the only uses, they are dead. */
8325 if (set != 0 && SET_DEST (set) == reg)
8326 continue;
8327 if (LABEL_P (i2)
8328 || JUMP_P (i2))
8329 break;
8330 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8331 && reg_mentioned_p (reg, PATTERN (i2)))
8333 /* Some other ref remains; just delete the output reload we
8334 know to be dead. */
8335 delete_address_reloads (output_reload_insn, insn);
8336 delete_insn (output_reload_insn);
8337 return;
8341 /* Delete the now-dead stores into this pseudo. Note that this
8342 loop also takes care of deleting output_reload_insn. */
8343 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8345 rtx set = single_set (i2);
8347 if (set != 0 && SET_DEST (set) == reg)
8349 delete_address_reloads (i2, insn);
8350 delete_insn (i2);
8352 if (LABEL_P (i2)
8353 || JUMP_P (i2))
8354 break;
8357 /* For the debugging info, say the pseudo lives in this reload reg. */
8358 reg_renumber[REGNO (reg)] = REGNO (rld[j].reg_rtx);
8359 if (flag_ira)
8360 mark_allocation_change (REGNO (reg));
8361 alter_reg (REGNO (reg), -1, false);
8363 else
8365 delete_address_reloads (output_reload_insn, insn);
8366 delete_insn (output_reload_insn);
8370 /* We are going to delete DEAD_INSN. Recursively delete loads of
8371 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8372 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8373 static void
8374 delete_address_reloads (rtx dead_insn, rtx current_insn)
8376 rtx set = single_set (dead_insn);
8377 rtx set2, dst, prev, next;
8378 if (set)
8380 rtx dst = SET_DEST (set);
8381 if (MEM_P (dst))
8382 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8384 /* If we deleted the store from a reloaded post_{in,de}c expression,
8385 we can delete the matching adds. */
8386 prev = PREV_INSN (dead_insn);
8387 next = NEXT_INSN (dead_insn);
8388 if (! prev || ! next)
8389 return;
8390 set = single_set (next);
8391 set2 = single_set (prev);
8392 if (! set || ! set2
8393 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8394 || GET_CODE (XEXP (SET_SRC (set), 1)) != CONST_INT
8395 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT)
8396 return;
8397 dst = SET_DEST (set);
8398 if (! rtx_equal_p (dst, SET_DEST (set2))
8399 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8400 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8401 || (INTVAL (XEXP (SET_SRC (set), 1))
8402 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8403 return;
8404 delete_related_insns (prev);
8405 delete_related_insns (next);
8408 /* Subfunction of delete_address_reloads: process registers found in X. */
8409 static void
8410 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8412 rtx prev, set, dst, i2;
8413 int i, j;
8414 enum rtx_code code = GET_CODE (x);
8416 if (code != REG)
8418 const char *fmt = GET_RTX_FORMAT (code);
8419 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8421 if (fmt[i] == 'e')
8422 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8423 else if (fmt[i] == 'E')
8425 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8426 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8427 current_insn);
8430 return;
8433 if (spill_reg_order[REGNO (x)] < 0)
8434 return;
8436 /* Scan backwards for the insn that sets x. This might be a way back due
8437 to inheritance. */
8438 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8440 code = GET_CODE (prev);
8441 if (code == CODE_LABEL || code == JUMP_INSN)
8442 return;
8443 if (!INSN_P (prev))
8444 continue;
8445 if (reg_set_p (x, PATTERN (prev)))
8446 break;
8447 if (reg_referenced_p (x, PATTERN (prev)))
8448 return;
8450 if (! prev || INSN_UID (prev) < reload_first_uid)
8451 return;
8452 /* Check that PREV only sets the reload register. */
8453 set = single_set (prev);
8454 if (! set)
8455 return;
8456 dst = SET_DEST (set);
8457 if (!REG_P (dst)
8458 || ! rtx_equal_p (dst, x))
8459 return;
8460 if (! reg_set_p (dst, PATTERN (dead_insn)))
8462 /* Check if DST was used in a later insn -
8463 it might have been inherited. */
8464 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8466 if (LABEL_P (i2))
8467 break;
8468 if (! INSN_P (i2))
8469 continue;
8470 if (reg_referenced_p (dst, PATTERN (i2)))
8472 /* If there is a reference to the register in the current insn,
8473 it might be loaded in a non-inherited reload. If no other
8474 reload uses it, that means the register is set before
8475 referenced. */
8476 if (i2 == current_insn)
8478 for (j = n_reloads - 1; j >= 0; j--)
8479 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8480 || reload_override_in[j] == dst)
8481 return;
8482 for (j = n_reloads - 1; j >= 0; j--)
8483 if (rld[j].in && rld[j].reg_rtx == dst)
8484 break;
8485 if (j >= 0)
8486 break;
8488 return;
8490 if (JUMP_P (i2))
8491 break;
8492 /* If DST is still live at CURRENT_INSN, check if it is used for
8493 any reload. Note that even if CURRENT_INSN sets DST, we still
8494 have to check the reloads. */
8495 if (i2 == current_insn)
8497 for (j = n_reloads - 1; j >= 0; j--)
8498 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8499 || reload_override_in[j] == dst)
8500 return;
8501 /* ??? We can't finish the loop here, because dst might be
8502 allocated to a pseudo in this block if no reload in this
8503 block needs any of the classes containing DST - see
8504 spill_hard_reg. There is no easy way to tell this, so we
8505 have to scan till the end of the basic block. */
8507 if (reg_set_p (dst, PATTERN (i2)))
8508 break;
8511 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8512 reg_reloaded_contents[REGNO (dst)] = -1;
8513 delete_insn (prev);
8516 /* Output reload-insns to reload VALUE into RELOADREG.
8517 VALUE is an autoincrement or autodecrement RTX whose operand
8518 is a register or memory location;
8519 so reloading involves incrementing that location.
8520 IN is either identical to VALUE, or some cheaper place to reload from.
8522 INC_AMOUNT is the number to increment or decrement by (always positive).
8523 This cannot be deduced from VALUE.
8525 Return the instruction that stores into RELOADREG. */
8527 static rtx
8528 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
8530 /* REG or MEM to be copied and incremented. */
8531 rtx incloc = find_replacement (&XEXP (value, 0));
8532 /* Nonzero if increment after copying. */
8533 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
8534 || GET_CODE (value) == POST_MODIFY);
8535 rtx last;
8536 rtx inc;
8537 rtx add_insn;
8538 int code;
8539 rtx store;
8540 rtx real_in = in == value ? incloc : in;
8542 /* No hard register is equivalent to this register after
8543 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
8544 we could inc/dec that register as well (maybe even using it for
8545 the source), but I'm not sure it's worth worrying about. */
8546 if (REG_P (incloc))
8547 reg_last_reload_reg[REGNO (incloc)] = 0;
8549 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
8551 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
8552 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
8554 else
8556 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8557 inc_amount = -inc_amount;
8559 inc = GEN_INT (inc_amount);
8562 /* If this is post-increment, first copy the location to the reload reg. */
8563 if (post && real_in != reloadreg)
8564 emit_insn (gen_move_insn (reloadreg, real_in));
8566 if (in == value)
8568 /* See if we can directly increment INCLOC. Use a method similar to
8569 that in gen_reload. */
8571 last = get_last_insn ();
8572 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8573 gen_rtx_PLUS (GET_MODE (incloc),
8574 incloc, inc)));
8576 code = recog_memoized (add_insn);
8577 if (code >= 0)
8579 extract_insn (add_insn);
8580 if (constrain_operands (1))
8582 /* If this is a pre-increment and we have incremented the value
8583 where it lives, copy the incremented value to RELOADREG to
8584 be used as an address. */
8586 if (! post)
8587 emit_insn (gen_move_insn (reloadreg, incloc));
8589 return add_insn;
8592 delete_insns_since (last);
8595 /* If couldn't do the increment directly, must increment in RELOADREG.
8596 The way we do this depends on whether this is pre- or post-increment.
8597 For pre-increment, copy INCLOC to the reload register, increment it
8598 there, then save back. */
8600 if (! post)
8602 if (in != reloadreg)
8603 emit_insn (gen_move_insn (reloadreg, real_in));
8604 emit_insn (gen_add2_insn (reloadreg, inc));
8605 store = emit_insn (gen_move_insn (incloc, reloadreg));
8607 else
8609 /* Postincrement.
8610 Because this might be a jump insn or a compare, and because RELOADREG
8611 may not be available after the insn in an input reload, we must do
8612 the incrementation before the insn being reloaded for.
8614 We have already copied IN to RELOADREG. Increment the copy in
8615 RELOADREG, save that back, then decrement RELOADREG so it has
8616 the original value. */
8618 emit_insn (gen_add2_insn (reloadreg, inc));
8619 store = emit_insn (gen_move_insn (incloc, reloadreg));
8620 if (GET_CODE (inc) == CONST_INT)
8621 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
8622 else
8623 emit_insn (gen_sub2_insn (reloadreg, inc));
8626 return store;
8629 #ifdef AUTO_INC_DEC
8630 static void
8631 add_auto_inc_notes (rtx insn, rtx x)
8633 enum rtx_code code = GET_CODE (x);
8634 const char *fmt;
8635 int i, j;
8637 if (code == MEM && auto_inc_p (XEXP (x, 0)))
8639 REG_NOTES (insn)
8640 = gen_rtx_EXPR_LIST (REG_INC, XEXP (XEXP (x, 0), 0), REG_NOTES (insn));
8641 return;
8644 /* Scan all the operand sub-expressions. */
8645 fmt = GET_RTX_FORMAT (code);
8646 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8648 if (fmt[i] == 'e')
8649 add_auto_inc_notes (insn, XEXP (x, i));
8650 else if (fmt[i] == 'E')
8651 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8652 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8655 #endif
8657 /* Copy EH notes from an insn to its reloads. */
8658 static void
8659 copy_eh_notes (rtx insn, rtx x)
8661 rtx eh_note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
8662 if (eh_note)
8664 for (; x != 0; x = NEXT_INSN (x))
8666 if (may_trap_p (PATTERN (x)))
8667 REG_NOTES (x)
8668 = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (eh_note, 0),
8669 REG_NOTES (x));
8674 /* This is used by reload pass, that does emit some instructions after
8675 abnormal calls moving basic block end, but in fact it wants to emit
8676 them on the edge. Looks for abnormal call edges, find backward the
8677 proper call and fix the damage.
8679 Similar handle instructions throwing exceptions internally. */
8680 void
8681 fixup_abnormal_edges (void)
8683 bool inserted = false;
8684 basic_block bb;
8686 FOR_EACH_BB (bb)
8688 edge e;
8689 edge_iterator ei;
8691 /* Look for cases we are interested in - calls or instructions causing
8692 exceptions. */
8693 FOR_EACH_EDGE (e, ei, bb->succs)
8695 if (e->flags & EDGE_ABNORMAL_CALL)
8696 break;
8697 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8698 == (EDGE_ABNORMAL | EDGE_EH))
8699 break;
8701 if (e && !CALL_P (BB_END (bb))
8702 && !can_throw_internal (BB_END (bb)))
8704 rtx insn;
8706 /* Get past the new insns generated. Allow notes, as the insns
8707 may be already deleted. */
8708 insn = BB_END (bb);
8709 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8710 && !can_throw_internal (insn)
8711 && insn != BB_HEAD (bb))
8712 insn = PREV_INSN (insn);
8714 if (CALL_P (insn) || can_throw_internal (insn))
8716 rtx stop, next;
8718 stop = NEXT_INSN (BB_END (bb));
8719 BB_END (bb) = insn;
8720 insn = NEXT_INSN (insn);
8722 FOR_EACH_EDGE (e, ei, bb->succs)
8723 if (e->flags & EDGE_FALLTHRU)
8724 break;
8726 while (insn && insn != stop)
8728 next = NEXT_INSN (insn);
8729 if (INSN_P (insn))
8731 delete_insn (insn);
8733 /* Sometimes there's still the return value USE.
8734 If it's placed after a trapping call (i.e. that
8735 call is the last insn anyway), we have no fallthru
8736 edge. Simply delete this use and don't try to insert
8737 on the non-existent edge. */
8738 if (GET_CODE (PATTERN (insn)) != USE)
8740 /* We're not deleting it, we're moving it. */
8741 INSN_DELETED_P (insn) = 0;
8742 PREV_INSN (insn) = NULL_RTX;
8743 NEXT_INSN (insn) = NULL_RTX;
8745 insert_insn_on_edge (insn, e);
8746 inserted = true;
8749 else if (!BARRIER_P (insn))
8750 set_block_for_insn (insn, NULL);
8751 insn = next;
8755 /* It may be that we don't find any such trapping insn. In this
8756 case we discovered quite late that the insn that had been
8757 marked as can_throw_internal in fact couldn't trap at all.
8758 So we should in fact delete the EH edges out of the block. */
8759 else
8760 purge_dead_edges (bb);
8764 /* We've possibly turned single trapping insn into multiple ones. */
8765 if (flag_non_call_exceptions)
8767 sbitmap blocks;
8768 blocks = sbitmap_alloc (last_basic_block);
8769 sbitmap_ones (blocks);
8770 find_many_sub_basic_blocks (blocks);
8771 sbitmap_free (blocks);
8774 if (inserted)
8775 commit_edge_insertions ();
8777 #ifdef ENABLE_CHECKING
8778 /* Verify that we didn't turn one trapping insn into many, and that
8779 we found and corrected all of the problems wrt fixups on the
8780 fallthru edge. */
8781 verify_flow_info ();
8782 #endif