PR tree-optimization/43833
[official-gcc/alias-decl.git] / gcc / reload1.c
blobbd590cbc9c9b37ada64f3eccfadd5cc098a221cc
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, 2008, 2009, 2010
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 "emit-rtl.h"
52 /* This file contains the reload pass of the compiler, which is
53 run after register allocation has been done. It checks that
54 each insn is valid (operands required to be in registers really
55 are in registers of the proper class) and fixes up invalid ones
56 by copying values temporarily into registers for the insns
57 that need them.
59 The results of register allocation are described by the vector
60 reg_renumber; the insns still contain pseudo regs, but reg_renumber
61 can be used to find which hard reg, if any, a pseudo reg is in.
63 The technique we always use is to free up a few hard regs that are
64 called ``reload regs'', and for each place where a pseudo reg
65 must be in a hard reg, copy it temporarily into one of the reload regs.
67 Reload regs are allocated locally for every instruction that needs
68 reloads. When there are pseudos which are allocated to a register that
69 has been chosen as a reload reg, such pseudos must be ``spilled''.
70 This means that they go to other hard regs, or to stack slots if no other
71 available hard regs can be found. Spilling can invalidate more
72 insns, requiring additional need for reloads, so we must keep checking
73 until the process stabilizes.
75 For machines with different classes of registers, we must keep track
76 of the register class needed for each reload, and make sure that
77 we allocate enough reload registers of each class.
79 The file reload.c contains the code that checks one insn for
80 validity and reports the reloads that it needs. This file
81 is in charge of scanning the entire rtl code, accumulating the
82 reload needs, spilling, assigning reload registers to use for
83 fixing up each insn, and generating the new insns to copy values
84 into the reload registers. */
86 /* During reload_as_needed, element N contains a REG rtx for the hard reg
87 into which reg N has been reloaded (perhaps for a previous insn). */
88 static rtx *reg_last_reload_reg;
90 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
91 for an output reload that stores into reg N. */
92 static regset_head reg_has_output_reload;
94 /* Indicates which hard regs are reload-registers for an output reload
95 in the current insn. */
96 static HARD_REG_SET reg_is_output_reload;
98 /* Element N is the constant value to which pseudo reg N is equivalent,
99 or zero if pseudo reg N is not equivalent to a constant.
100 find_reloads looks at this in order to replace pseudo reg N
101 with the constant it stands for. */
102 rtx *reg_equiv_constant;
104 /* Element N is an invariant value to which pseudo reg N is equivalent.
105 eliminate_regs_in_insn uses this to replace pseudos in particular
106 contexts. */
107 rtx *reg_equiv_invariant;
109 /* Element N is a memory location to which pseudo reg N is equivalent,
110 prior to any register elimination (such as frame pointer to stack
111 pointer). Depending on whether or not it is a valid address, this value
112 is transferred to either reg_equiv_address or reg_equiv_mem. */
113 rtx *reg_equiv_memory_loc;
115 /* We allocate reg_equiv_memory_loc inside a varray so that the garbage
116 collector can keep track of what is inside. */
117 VEC(rtx,gc) *reg_equiv_memory_loc_vec;
119 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
120 This is used when the address is not valid as a memory address
121 (because its displacement is too big for the machine.) */
122 rtx *reg_equiv_address;
124 /* Element N is the memory slot to which pseudo reg N is equivalent,
125 or zero if pseudo reg N is not equivalent to a memory slot. */
126 rtx *reg_equiv_mem;
128 /* Element N is an EXPR_LIST of REG_EQUIVs containing MEMs with
129 alternate representations of the location of pseudo reg N. */
130 rtx *reg_equiv_alt_mem_list;
132 /* Widest width in which each pseudo reg is referred to (via subreg). */
133 static unsigned int *reg_max_ref_width;
135 /* Element N is the list of insns that initialized reg N from its equivalent
136 constant or memory slot. */
137 rtx *reg_equiv_init;
138 int reg_equiv_init_size;
140 /* Vector to remember old contents of reg_renumber before spilling. */
141 static short *reg_old_renumber;
143 /* During reload_as_needed, element N contains the last pseudo regno reloaded
144 into hard register N. If that pseudo reg occupied more than one register,
145 reg_reloaded_contents points to that pseudo for each spill register in
146 use; all of these must remain set for an inheritance to occur. */
147 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
149 /* During reload_as_needed, element N contains the insn for which
150 hard register N was last used. Its contents are significant only
151 when reg_reloaded_valid is set for this register. */
152 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
154 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
155 static HARD_REG_SET reg_reloaded_valid;
156 /* Indicate if the register was dead at the end of the reload.
157 This is only valid if reg_reloaded_contents is set and valid. */
158 static HARD_REG_SET reg_reloaded_dead;
160 /* Indicate whether the register's current value is one that is not
161 safe to retain across a call, even for registers that are normally
162 call-saved. This is only meaningful for members of reg_reloaded_valid. */
163 static HARD_REG_SET reg_reloaded_call_part_clobbered;
165 /* Number of spill-regs so far; number of valid elements of spill_regs. */
166 static int n_spills;
168 /* In parallel with spill_regs, contains REG rtx's for those regs.
169 Holds the last rtx used for any given reg, or 0 if it has never
170 been used for spilling yet. This rtx is reused, provided it has
171 the proper mode. */
172 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
174 /* In parallel with spill_regs, contains nonzero for a spill reg
175 that was stored after the last time it was used.
176 The precise value is the insn generated to do the store. */
177 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
179 /* This is the register that was stored with spill_reg_store. This is a
180 copy of reload_out / reload_out_reg when the value was stored; if
181 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
182 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
184 /* This table is the inverse mapping of spill_regs:
185 indexed by hard reg number,
186 it contains the position of that reg in spill_regs,
187 or -1 for something that is not in spill_regs.
189 ?!? This is no longer accurate. */
190 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
192 /* This reg set indicates registers that can't be used as spill registers for
193 the currently processed insn. These are the hard registers which are live
194 during the insn, but not allocated to pseudos, as well as fixed
195 registers. */
196 static HARD_REG_SET bad_spill_regs;
198 /* These are the hard registers that can't be used as spill register for any
199 insn. This includes registers used for user variables and registers that
200 we can't eliminate. A register that appears in this set also can't be used
201 to retry register allocation. */
202 static HARD_REG_SET bad_spill_regs_global;
204 /* Describes order of use of registers for reloading
205 of spilled pseudo-registers. `n_spills' is the number of
206 elements that are actually valid; new ones are added at the end.
208 Both spill_regs and spill_reg_order are used on two occasions:
209 once during find_reload_regs, where they keep track of the spill registers
210 for a single insn, but also during reload_as_needed where they show all
211 the registers ever used by reload. For the latter case, the information
212 is calculated during finish_spills. */
213 static short spill_regs[FIRST_PSEUDO_REGISTER];
215 /* This vector of reg sets indicates, for each pseudo, which hard registers
216 may not be used for retrying global allocation because the register was
217 formerly spilled from one of them. If we allowed reallocating a pseudo to
218 a register that it was already allocated to, reload might not
219 terminate. */
220 static HARD_REG_SET *pseudo_previous_regs;
222 /* This vector of reg sets indicates, for each pseudo, which hard
223 registers may not be used for retrying global allocation because they
224 are used as spill registers during one of the insns in which the
225 pseudo is live. */
226 static HARD_REG_SET *pseudo_forbidden_regs;
228 /* All hard regs that have been used as spill registers for any insn are
229 marked in this set. */
230 static HARD_REG_SET used_spill_regs;
232 /* Index of last register assigned as a spill register. We allocate in
233 a round-robin fashion. */
234 static int last_spill_reg;
236 /* Nonzero if indirect addressing is supported on the machine; this means
237 that spilling (REG n) does not require reloading it into a register in
238 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
239 value indicates the level of indirect addressing supported, e.g., two
240 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
241 a hard register. */
242 static char spill_indirect_levels;
244 /* Nonzero if indirect addressing is supported when the innermost MEM is
245 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
246 which these are valid is the same as spill_indirect_levels, above. */
247 char indirect_symref_ok;
249 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
250 char double_reg_address_ok;
252 /* Record the stack slot for each spilled hard register. */
253 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
255 /* Width allocated so far for that stack slot. */
256 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
258 /* Record which pseudos needed to be spilled. */
259 static regset_head spilled_pseudos;
261 /* Record which pseudos changed their allocation in finish_spills. */
262 static regset_head changed_allocation_pseudos;
264 /* Used for communication between order_regs_for_reload and count_pseudo.
265 Used to avoid counting one pseudo twice. */
266 static regset_head pseudos_counted;
268 /* First uid used by insns created by reload in this function.
269 Used in find_equiv_reg. */
270 int reload_first_uid;
272 /* Flag set by local-alloc or global-alloc if anything is live in
273 a call-clobbered reg across calls. */
274 int caller_save_needed;
276 /* Set to 1 while reload_as_needed is operating.
277 Required by some machines to handle any generated moves differently. */
278 int reload_in_progress = 0;
280 /* These arrays record the insn_code of insns that may be needed to
281 perform input and output reloads of special objects. They provide a
282 place to pass a scratch register. */
283 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
284 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
286 /* This obstack is used for allocation of rtl during register elimination.
287 The allocated storage can be freed once find_reloads has processed the
288 insn. */
289 static struct obstack reload_obstack;
291 /* Points to the beginning of the reload_obstack. All insn_chain structures
292 are allocated first. */
293 static char *reload_startobj;
295 /* The point after all insn_chain structures. Used to quickly deallocate
296 memory allocated in copy_reloads during calculate_needs_all_insns. */
297 static char *reload_firstobj;
299 /* This points before all local rtl generated by register elimination.
300 Used to quickly free all memory after processing one insn. */
301 static char *reload_insn_firstobj;
303 /* List of insn_chain instructions, one for every insn that reload needs to
304 examine. */
305 struct insn_chain *reload_insn_chain;
307 /* List of all insns needing reloads. */
308 static struct insn_chain *insns_need_reload;
310 /* This structure is used to record information about register eliminations.
311 Each array entry describes one possible way of eliminating a register
312 in favor of another. If there is more than one way of eliminating a
313 particular register, the most preferred should be specified first. */
315 struct elim_table
317 int from; /* Register number to be eliminated. */
318 int to; /* Register number used as replacement. */
319 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
320 int can_eliminate; /* Nonzero if this elimination can be done. */
321 int can_eliminate_previous; /* Value returned by TARGET_CAN_ELIMINATE
322 target hook in previous scan over insns
323 made by reload. */
324 HOST_WIDE_INT offset; /* Current offset between the two regs. */
325 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
326 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
327 rtx from_rtx; /* REG rtx for the register to be eliminated.
328 We cannot simply compare the number since
329 we might then spuriously replace a hard
330 register corresponding to a pseudo
331 assigned to the reg to be eliminated. */
332 rtx to_rtx; /* REG rtx for the replacement. */
335 static struct elim_table *reg_eliminate = 0;
337 /* This is an intermediate structure to initialize the table. It has
338 exactly the members provided by ELIMINABLE_REGS. */
339 static const struct elim_table_1
341 const int from;
342 const int to;
343 } reg_eliminate_1[] =
345 /* If a set of eliminable registers was specified, define the table from it.
346 Otherwise, default to the normal case of the frame pointer being
347 replaced by the stack pointer. */
349 #ifdef ELIMINABLE_REGS
350 ELIMINABLE_REGS;
351 #else
352 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
353 #endif
355 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
357 /* Record the number of pending eliminations that have an offset not equal
358 to their initial offset. If nonzero, we use a new copy of each
359 replacement result in any insns encountered. */
360 int num_not_at_initial_offset;
362 /* Count the number of registers that we may be able to eliminate. */
363 static int num_eliminable;
364 /* And the number of registers that are equivalent to a constant that
365 can be eliminated to frame_pointer / arg_pointer + constant. */
366 static int num_eliminable_invariants;
368 /* For each label, we record the offset of each elimination. If we reach
369 a label by more than one path and an offset differs, we cannot do the
370 elimination. This information is indexed by the difference of the
371 number of the label and the first label number. We can't offset the
372 pointer itself as this can cause problems on machines with segmented
373 memory. The first table is an array of flags that records whether we
374 have yet encountered a label and the second table is an array of arrays,
375 one entry in the latter array for each elimination. */
377 static int first_label_num;
378 static char *offsets_known_at;
379 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
381 /* Stack of addresses where an rtx has been changed. We can undo the
382 changes by popping items off the stack and restoring the original
383 value at each location.
385 We use this simplistic undo capability rather than copy_rtx as copy_rtx
386 will not make a deep copy of a normally sharable rtx, such as
387 (const (plus (symbol_ref) (const_int))). If such an expression appears
388 as R1 in gen_reload_chain_without_interm_reg_p, then a shared
389 rtx expression would be changed. See PR 42431. */
391 typedef rtx *rtx_p;
392 DEF_VEC_P(rtx_p);
393 DEF_VEC_ALLOC_P(rtx_p,heap);
394 static VEC(rtx_p,heap) *substitute_stack;
396 /* Number of labels in the current function. */
398 static int num_labels;
400 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
401 static void maybe_fix_stack_asms (void);
402 static void copy_reloads (struct insn_chain *);
403 static void calculate_needs_all_insns (int);
404 static int find_reg (struct insn_chain *, int);
405 static void find_reload_regs (struct insn_chain *);
406 static void select_reload_regs (void);
407 static void delete_caller_save_insns (void);
409 static void spill_failure (rtx, enum reg_class);
410 static void count_spilled_pseudo (int, int, int);
411 static void delete_dead_insn (rtx);
412 static void alter_reg (int, int, bool);
413 static void set_label_offsets (rtx, rtx, int);
414 static void check_eliminable_occurrences (rtx);
415 static void elimination_effects (rtx, enum machine_mode);
416 static int eliminate_regs_in_insn (rtx, int);
417 static void update_eliminable_offsets (void);
418 static void mark_not_eliminable (rtx, const_rtx, void *);
419 static void set_initial_elim_offsets (void);
420 static bool verify_initial_elim_offsets (void);
421 static void set_initial_label_offsets (void);
422 static void set_offsets_for_label (rtx);
423 static void init_elim_table (void);
424 static void update_eliminables (HARD_REG_SET *);
425 static void spill_hard_reg (unsigned int, int);
426 static int finish_spills (int);
427 static void scan_paradoxical_subregs (rtx);
428 static void count_pseudo (int);
429 static void order_regs_for_reload (struct insn_chain *);
430 static void reload_as_needed (int);
431 static void forget_old_reloads_1 (rtx, const_rtx, void *);
432 static void forget_marked_reloads (regset);
433 static int reload_reg_class_lower (const void *, const void *);
434 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
435 enum machine_mode);
436 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
437 enum machine_mode);
438 static int reload_reg_free_p (unsigned int, int, enum reload_type);
439 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
440 rtx, rtx, int, int);
441 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
442 rtx, rtx, int, int);
443 static int reload_reg_reaches_end_p (unsigned int, int, enum reload_type);
444 static int allocate_reload_reg (struct insn_chain *, int, int);
445 static int conflicts_with_override (rtx);
446 static void failed_reload (rtx, int);
447 static int set_reload_reg (int, int);
448 static void choose_reload_regs_init (struct insn_chain *, rtx *);
449 static void choose_reload_regs (struct insn_chain *);
450 static void merge_assigned_reloads (rtx);
451 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
452 rtx, int);
453 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
454 int);
455 static void do_input_reload (struct insn_chain *, struct reload *, int);
456 static void do_output_reload (struct insn_chain *, struct reload *, int);
457 static void emit_reload_insns (struct insn_chain *);
458 static void delete_output_reload (rtx, int, int, rtx);
459 static void delete_address_reloads (rtx, rtx);
460 static void delete_address_reloads_1 (rtx, rtx, rtx);
461 static rtx inc_for_reload (rtx, rtx, rtx, int);
462 #ifdef AUTO_INC_DEC
463 static void add_auto_inc_notes (rtx, rtx);
464 #endif
465 static void substitute (rtx *, const_rtx, rtx);
466 static bool gen_reload_chain_without_interm_reg_p (int, int);
467 static int reloads_conflict (int, int);
468 static rtx gen_reload (rtx, rtx, int, enum reload_type);
469 static rtx emit_insn_if_valid_for_reload (rtx);
471 /* Initialize the reload pass. This is called at the beginning of compilation
472 and may be called again if the target is reinitialized. */
474 void
475 init_reload (void)
477 int i;
479 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
480 Set spill_indirect_levels to the number of levels such addressing is
481 permitted, zero if it is not permitted at all. */
483 rtx tem
484 = gen_rtx_MEM (Pmode,
485 gen_rtx_PLUS (Pmode,
486 gen_rtx_REG (Pmode,
487 LAST_VIRTUAL_REGISTER + 1),
488 GEN_INT (4)));
489 spill_indirect_levels = 0;
491 while (memory_address_p (QImode, tem))
493 spill_indirect_levels++;
494 tem = gen_rtx_MEM (Pmode, tem);
497 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
499 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
500 indirect_symref_ok = memory_address_p (QImode, tem);
502 /* See if reg+reg is a valid (and offsettable) address. */
504 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
506 tem = gen_rtx_PLUS (Pmode,
507 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
508 gen_rtx_REG (Pmode, i));
510 /* This way, we make sure that reg+reg is an offsettable address. */
511 tem = plus_constant (tem, 4);
513 if (memory_address_p (QImode, tem))
515 double_reg_address_ok = 1;
516 break;
520 /* Initialize obstack for our rtl allocation. */
521 gcc_obstack_init (&reload_obstack);
522 reload_startobj = XOBNEWVAR (&reload_obstack, char, 0);
524 INIT_REG_SET (&spilled_pseudos);
525 INIT_REG_SET (&changed_allocation_pseudos);
526 INIT_REG_SET (&pseudos_counted);
529 /* List of insn chains that are currently unused. */
530 static struct insn_chain *unused_insn_chains = 0;
532 /* Allocate an empty insn_chain structure. */
533 struct insn_chain *
534 new_insn_chain (void)
536 struct insn_chain *c;
538 if (unused_insn_chains == 0)
540 c = XOBNEW (&reload_obstack, struct insn_chain);
541 INIT_REG_SET (&c->live_throughout);
542 INIT_REG_SET (&c->dead_or_set);
544 else
546 c = unused_insn_chains;
547 unused_insn_chains = c->next;
549 c->is_caller_save_insn = 0;
550 c->need_operand_change = 0;
551 c->need_reload = 0;
552 c->need_elim = 0;
553 return c;
556 /* Small utility function to set all regs in hard reg set TO which are
557 allocated to pseudos in regset FROM. */
559 void
560 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
562 unsigned int regno;
563 reg_set_iterator rsi;
565 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
567 int r = reg_renumber[regno];
569 if (r < 0)
571 /* reload_combine uses the information from DF_LIVE_IN,
572 which might still contain registers that have not
573 actually been allocated since they have an
574 equivalence. */
575 gcc_assert (ira_conflicts_p || reload_completed);
577 else
578 add_to_hard_reg_set (to, PSEUDO_REGNO_MODE (regno), r);
582 /* Replace all pseudos found in LOC with their corresponding
583 equivalences. */
585 static void
586 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
588 rtx x = *loc;
589 enum rtx_code code;
590 const char *fmt;
591 int i, j;
593 if (! x)
594 return;
596 code = GET_CODE (x);
597 if (code == REG)
599 unsigned int regno = REGNO (x);
601 if (regno < FIRST_PSEUDO_REGISTER)
602 return;
604 x = eliminate_regs (x, mem_mode, usage);
605 if (x != *loc)
607 *loc = x;
608 replace_pseudos_in (loc, mem_mode, usage);
609 return;
612 if (reg_equiv_constant[regno])
613 *loc = reg_equiv_constant[regno];
614 else if (reg_equiv_mem[regno])
615 *loc = reg_equiv_mem[regno];
616 else if (reg_equiv_address[regno])
617 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address[regno]);
618 else
620 gcc_assert (!REG_P (regno_reg_rtx[regno])
621 || REGNO (regno_reg_rtx[regno]) != regno);
622 *loc = regno_reg_rtx[regno];
625 return;
627 else if (code == MEM)
629 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
630 return;
633 /* Process each of our operands recursively. */
634 fmt = GET_RTX_FORMAT (code);
635 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
636 if (*fmt == 'e')
637 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
638 else if (*fmt == 'E')
639 for (j = 0; j < XVECLEN (x, i); j++)
640 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
643 /* Determine if the current function has an exception receiver block
644 that reaches the exit block via non-exceptional edges */
646 static bool
647 has_nonexceptional_receiver (void)
649 edge e;
650 edge_iterator ei;
651 basic_block *tos, *worklist, bb;
653 /* If we're not optimizing, then just err on the safe side. */
654 if (!optimize)
655 return true;
657 /* First determine which blocks can reach exit via normal paths. */
658 tos = worklist = XNEWVEC (basic_block, n_basic_blocks + 1);
660 FOR_EACH_BB (bb)
661 bb->flags &= ~BB_REACHABLE;
663 /* Place the exit block on our worklist. */
664 EXIT_BLOCK_PTR->flags |= BB_REACHABLE;
665 *tos++ = EXIT_BLOCK_PTR;
667 /* Iterate: find everything reachable from what we've already seen. */
668 while (tos != worklist)
670 bb = *--tos;
672 FOR_EACH_EDGE (e, ei, bb->preds)
673 if (!(e->flags & EDGE_ABNORMAL))
675 basic_block src = e->src;
677 if (!(src->flags & BB_REACHABLE))
679 src->flags |= BB_REACHABLE;
680 *tos++ = src;
684 free (worklist);
686 /* Now see if there's a reachable block with an exceptional incoming
687 edge. */
688 FOR_EACH_BB (bb)
689 if (bb->flags & BB_REACHABLE)
690 FOR_EACH_EDGE (e, ei, bb->preds)
691 if (e->flags & EDGE_ABNORMAL)
692 return true;
694 /* No exceptional block reached exit unexceptionally. */
695 return false;
699 /* Global variables used by reload and its subroutines. */
701 /* Set during calculate_needs if an insn needs register elimination. */
702 static int something_needs_elimination;
703 /* Set during calculate_needs if an insn needs an operand changed. */
704 static int something_needs_operands_changed;
706 /* Nonzero means we couldn't get enough spill regs. */
707 static int failure;
709 /* Temporary array of pseudo-register number. */
710 static int *temp_pseudo_reg_arr;
712 /* Main entry point for the reload pass.
714 FIRST is the first insn of the function being compiled.
716 GLOBAL nonzero means we were called from global_alloc
717 and should attempt to reallocate any pseudoregs that we
718 displace from hard regs we will use for reloads.
719 If GLOBAL is zero, we do not have enough information to do that,
720 so any pseudo reg that is spilled must go to the stack.
722 Return value is nonzero if reload failed
723 and we must not do any more for this function. */
726 reload (rtx first, int global)
728 int i, n;
729 rtx insn;
730 struct elim_table *ep;
731 basic_block bb;
733 /* Make sure even insns with volatile mem refs are recognizable. */
734 init_recog ();
736 failure = 0;
738 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
740 /* Make sure that the last insn in the chain
741 is not something that needs reloading. */
742 emit_note (NOTE_INSN_DELETED);
744 /* Enable find_equiv_reg to distinguish insns made by reload. */
745 reload_first_uid = get_max_uid ();
747 #ifdef SECONDARY_MEMORY_NEEDED
748 /* Initialize the secondary memory table. */
749 clear_secondary_mem ();
750 #endif
752 /* We don't have a stack slot for any spill reg yet. */
753 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
754 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
756 /* Initialize the save area information for caller-save, in case some
757 are needed. */
758 init_save_areas ();
760 /* Compute which hard registers are now in use
761 as homes for pseudo registers.
762 This is done here rather than (eg) in global_alloc
763 because this point is reached even if not optimizing. */
764 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
765 mark_home_live (i);
767 /* A function that has a nonlocal label that can reach the exit
768 block via non-exceptional paths must save all call-saved
769 registers. */
770 if (cfun->has_nonlocal_label
771 && has_nonexceptional_receiver ())
772 crtl->saves_all_registers = 1;
774 if (crtl->saves_all_registers)
775 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
776 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
777 df_set_regs_ever_live (i, true);
779 /* Find all the pseudo registers that didn't get hard regs
780 but do have known equivalent constants or memory slots.
781 These include parameters (known equivalent to parameter slots)
782 and cse'd or loop-moved constant memory addresses.
784 Record constant equivalents in reg_equiv_constant
785 so they will be substituted by find_reloads.
786 Record memory equivalents in reg_mem_equiv so they can
787 be substituted eventually by altering the REG-rtx's. */
789 reg_equiv_constant = XCNEWVEC (rtx, max_regno);
790 reg_equiv_invariant = XCNEWVEC (rtx, max_regno);
791 reg_equiv_mem = XCNEWVEC (rtx, max_regno);
792 reg_equiv_alt_mem_list = XCNEWVEC (rtx, max_regno);
793 reg_equiv_address = XCNEWVEC (rtx, max_regno);
794 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
795 reg_old_renumber = XCNEWVEC (short, max_regno);
796 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
797 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
798 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
800 CLEAR_HARD_REG_SET (bad_spill_regs_global);
802 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
803 to. Also find all paradoxical subregs and find largest such for
804 each pseudo. */
806 num_eliminable_invariants = 0;
807 for (insn = first; insn; insn = NEXT_INSN (insn))
809 rtx set = single_set (insn);
811 /* We may introduce USEs that we want to remove at the end, so
812 we'll mark them with QImode. Make sure there are no
813 previously-marked insns left by say regmove. */
814 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
815 && GET_MODE (insn) != VOIDmode)
816 PUT_MODE (insn, VOIDmode);
818 if (NONDEBUG_INSN_P (insn))
819 scan_paradoxical_subregs (PATTERN (insn));
821 if (set != 0 && REG_P (SET_DEST (set)))
823 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
824 rtx x;
826 if (! note)
827 continue;
829 i = REGNO (SET_DEST (set));
830 x = XEXP (note, 0);
832 if (i <= LAST_VIRTUAL_REGISTER)
833 continue;
835 if (! function_invariant_p (x)
836 || ! flag_pic
837 /* A function invariant is often CONSTANT_P but may
838 include a register. We promise to only pass
839 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
840 || (CONSTANT_P (x)
841 && LEGITIMATE_PIC_OPERAND_P (x)))
843 /* It can happen that a REG_EQUIV note contains a MEM
844 that is not a legitimate memory operand. As later
845 stages of reload assume that all addresses found
846 in the reg_equiv_* arrays were originally legitimate,
847 we ignore such REG_EQUIV notes. */
848 if (memory_operand (x, VOIDmode))
850 /* Always unshare the equivalence, so we can
851 substitute into this insn without touching the
852 equivalence. */
853 reg_equiv_memory_loc[i] = copy_rtx (x);
855 else if (function_invariant_p (x))
857 if (GET_CODE (x) == PLUS)
859 /* This is PLUS of frame pointer and a constant,
860 and might be shared. Unshare it. */
861 reg_equiv_invariant[i] = copy_rtx (x);
862 num_eliminable_invariants++;
864 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
866 reg_equiv_invariant[i] = x;
867 num_eliminable_invariants++;
869 else if (LEGITIMATE_CONSTANT_P (x))
870 reg_equiv_constant[i] = x;
871 else
873 reg_equiv_memory_loc[i]
874 = force_const_mem (GET_MODE (SET_DEST (set)), x);
875 if (! reg_equiv_memory_loc[i])
876 reg_equiv_init[i] = NULL_RTX;
879 else
881 reg_equiv_init[i] = NULL_RTX;
882 continue;
885 else
886 reg_equiv_init[i] = NULL_RTX;
890 if (dump_file)
891 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
892 if (reg_equiv_init[i])
894 fprintf (dump_file, "init_insns for %u: ", i);
895 print_inline_rtx (dump_file, reg_equiv_init[i], 20);
896 fprintf (dump_file, "\n");
899 init_elim_table ();
901 first_label_num = get_first_label_num ();
902 num_labels = max_label_num () - first_label_num;
904 /* Allocate the tables used to store offset information at labels. */
905 /* We used to use alloca here, but the size of what it would try to
906 allocate would occasionally cause it to exceed the stack limit and
907 cause a core dump. */
908 offsets_known_at = XNEWVEC (char, num_labels);
909 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
911 /* Alter each pseudo-reg rtx to contain its hard reg number. Assign
912 stack slots to the pseudos that lack hard regs or equivalents.
913 Do not touch virtual registers. */
915 temp_pseudo_reg_arr = XNEWVEC (int, max_regno - LAST_VIRTUAL_REGISTER - 1);
916 for (n = 0, i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
917 temp_pseudo_reg_arr[n++] = i;
919 if (ira_conflicts_p)
920 /* Ask IRA to order pseudo-registers for better stack slot
921 sharing. */
922 ira_sort_regnos_for_alter_reg (temp_pseudo_reg_arr, n, reg_max_ref_width);
924 for (i = 0; i < n; i++)
925 alter_reg (temp_pseudo_reg_arr[i], -1, false);
927 /* If we have some registers we think can be eliminated, scan all insns to
928 see if there is an insn that sets one of these registers to something
929 other than itself plus a constant. If so, the register cannot be
930 eliminated. Doing this scan here eliminates an extra pass through the
931 main reload loop in the most common case where register elimination
932 cannot be done. */
933 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
934 if (INSN_P (insn))
935 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
937 maybe_fix_stack_asms ();
939 insns_need_reload = 0;
940 something_needs_elimination = 0;
942 /* Initialize to -1, which means take the first spill register. */
943 last_spill_reg = -1;
945 /* Spill any hard regs that we know we can't eliminate. */
946 CLEAR_HARD_REG_SET (used_spill_regs);
947 /* There can be multiple ways to eliminate a register;
948 they should be listed adjacently.
949 Elimination for any register fails only if all possible ways fail. */
950 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
952 int from = ep->from;
953 int can_eliminate = 0;
956 can_eliminate |= ep->can_eliminate;
957 ep++;
959 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
960 if (! can_eliminate)
961 spill_hard_reg (from, 1);
964 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
965 if (frame_pointer_needed)
966 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
967 #endif
968 finish_spills (global);
970 /* From now on, we may need to generate moves differently. We may also
971 allow modifications of insns which cause them to not be recognized.
972 Any such modifications will be cleaned up during reload itself. */
973 reload_in_progress = 1;
975 /* This loop scans the entire function each go-round
976 and repeats until one repetition spills no additional hard regs. */
977 for (;;)
979 int something_changed;
980 int did_spill;
981 HOST_WIDE_INT starting_frame_size;
983 starting_frame_size = get_frame_size ();
985 set_initial_elim_offsets ();
986 set_initial_label_offsets ();
988 /* For each pseudo register that has an equivalent location defined,
989 try to eliminate any eliminable registers (such as the frame pointer)
990 assuming initial offsets for the replacement register, which
991 is the normal case.
993 If the resulting location is directly addressable, substitute
994 the MEM we just got directly for the old REG.
996 If it is not addressable but is a constant or the sum of a hard reg
997 and constant, it is probably not addressable because the constant is
998 out of range, in that case record the address; we will generate
999 hairy code to compute the address in a register each time it is
1000 needed. Similarly if it is a hard register, but one that is not
1001 valid as an address register.
1003 If the location is not addressable, but does not have one of the
1004 above forms, assign a stack slot. We have to do this to avoid the
1005 potential of producing lots of reloads if, e.g., a location involves
1006 a pseudo that didn't get a hard register and has an equivalent memory
1007 location that also involves a pseudo that didn't get a hard register.
1009 Perhaps at some point we will improve reload_when_needed handling
1010 so this problem goes away. But that's very hairy. */
1012 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1013 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
1015 rtx x = eliminate_regs (reg_equiv_memory_loc[i], VOIDmode,
1016 NULL_RTX);
1018 if (strict_memory_address_addr_space_p
1019 (GET_MODE (regno_reg_rtx[i]), XEXP (x, 0),
1020 MEM_ADDR_SPACE (x)))
1021 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
1022 else if (CONSTANT_P (XEXP (x, 0))
1023 || (REG_P (XEXP (x, 0))
1024 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
1025 || (GET_CODE (XEXP (x, 0)) == PLUS
1026 && REG_P (XEXP (XEXP (x, 0), 0))
1027 && (REGNO (XEXP (XEXP (x, 0), 0))
1028 < FIRST_PSEUDO_REGISTER)
1029 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
1030 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
1031 else
1033 /* Make a new stack slot. Then indicate that something
1034 changed so we go back and recompute offsets for
1035 eliminable registers because the allocation of memory
1036 below might change some offset. reg_equiv_{mem,address}
1037 will be set up for this pseudo on the next pass around
1038 the loop. */
1039 reg_equiv_memory_loc[i] = 0;
1040 reg_equiv_init[i] = 0;
1041 alter_reg (i, -1, true);
1045 if (caller_save_needed)
1046 setup_save_areas ();
1048 /* If we allocated another stack slot, redo elimination bookkeeping. */
1049 if (starting_frame_size != get_frame_size ())
1050 continue;
1051 if (starting_frame_size && crtl->stack_alignment_needed)
1053 /* If we have a stack frame, we must align it now. The
1054 stack size may be a part of the offset computation for
1055 register elimination. So if this changes the stack size,
1056 then repeat the elimination bookkeeping. We don't
1057 realign when there is no stack, as that will cause a
1058 stack frame when none is needed should
1059 STARTING_FRAME_OFFSET not be already aligned to
1060 STACK_BOUNDARY. */
1061 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
1062 if (starting_frame_size != get_frame_size ())
1063 continue;
1066 if (caller_save_needed)
1068 save_call_clobbered_regs ();
1069 /* That might have allocated new insn_chain structures. */
1070 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1073 calculate_needs_all_insns (global);
1075 if (! ira_conflicts_p)
1076 /* Don't do it for IRA. We need this info because we don't
1077 change live_throughout and dead_or_set for chains when IRA
1078 is used. */
1079 CLEAR_REG_SET (&spilled_pseudos);
1081 did_spill = 0;
1083 something_changed = 0;
1085 /* If we allocated any new memory locations, make another pass
1086 since it might have changed elimination offsets. */
1087 if (starting_frame_size != get_frame_size ())
1088 something_changed = 1;
1090 /* Even if the frame size remained the same, we might still have
1091 changed elimination offsets, e.g. if find_reloads called
1092 force_const_mem requiring the back end to allocate a constant
1093 pool base register that needs to be saved on the stack. */
1094 else if (!verify_initial_elim_offsets ())
1095 something_changed = 1;
1098 HARD_REG_SET to_spill;
1099 CLEAR_HARD_REG_SET (to_spill);
1100 update_eliminables (&to_spill);
1101 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
1103 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1104 if (TEST_HARD_REG_BIT (to_spill, i))
1106 spill_hard_reg (i, 1);
1107 did_spill = 1;
1109 /* Regardless of the state of spills, if we previously had
1110 a register that we thought we could eliminate, but now can
1111 not eliminate, we must run another pass.
1113 Consider pseudos which have an entry in reg_equiv_* which
1114 reference an eliminable register. We must make another pass
1115 to update reg_equiv_* so that we do not substitute in the
1116 old value from when we thought the elimination could be
1117 performed. */
1118 something_changed = 1;
1122 select_reload_regs ();
1123 if (failure)
1124 goto failed;
1126 if (insns_need_reload != 0 || did_spill)
1127 something_changed |= finish_spills (global);
1129 if (! something_changed)
1130 break;
1132 if (caller_save_needed)
1133 delete_caller_save_insns ();
1135 obstack_free (&reload_obstack, reload_firstobj);
1138 /* If global-alloc was run, notify it of any register eliminations we have
1139 done. */
1140 if (global)
1141 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1142 if (ep->can_eliminate)
1143 mark_elimination (ep->from, ep->to);
1145 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1146 If that insn didn't set the register (i.e., it copied the register to
1147 memory), just delete that insn instead of the equivalencing insn plus
1148 anything now dead. If we call delete_dead_insn on that insn, we may
1149 delete the insn that actually sets the register if the register dies
1150 there and that is incorrect. */
1152 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1154 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0)
1156 rtx list;
1157 for (list = reg_equiv_init[i]; list; list = XEXP (list, 1))
1159 rtx equiv_insn = XEXP (list, 0);
1161 /* If we already deleted the insn or if it may trap, we can't
1162 delete it. The latter case shouldn't happen, but can
1163 if an insn has a variable address, gets a REG_EH_REGION
1164 note added to it, and then gets converted into a load
1165 from a constant address. */
1166 if (NOTE_P (equiv_insn)
1167 || can_throw_internal (equiv_insn))
1169 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1170 delete_dead_insn (equiv_insn);
1171 else
1172 SET_INSN_DELETED (equiv_insn);
1177 /* Use the reload registers where necessary
1178 by generating move instructions to move the must-be-register
1179 values into or out of the reload registers. */
1181 if (insns_need_reload != 0 || something_needs_elimination
1182 || something_needs_operands_changed)
1184 HOST_WIDE_INT old_frame_size = get_frame_size ();
1186 reload_as_needed (global);
1188 gcc_assert (old_frame_size == get_frame_size ());
1190 gcc_assert (verify_initial_elim_offsets ());
1193 /* If we were able to eliminate the frame pointer, show that it is no
1194 longer live at the start of any basic block. If it ls live by
1195 virtue of being in a pseudo, that pseudo will be marked live
1196 and hence the frame pointer will be known to be live via that
1197 pseudo. */
1199 if (! frame_pointer_needed)
1200 FOR_EACH_BB (bb)
1201 bitmap_clear_bit (df_get_live_in (bb), HARD_FRAME_POINTER_REGNUM);
1203 /* Come here (with failure set nonzero) if we can't get enough spill
1204 regs. */
1205 failed:
1207 CLEAR_REG_SET (&changed_allocation_pseudos);
1208 CLEAR_REG_SET (&spilled_pseudos);
1209 reload_in_progress = 0;
1211 /* Now eliminate all pseudo regs by modifying them into
1212 their equivalent memory references.
1213 The REG-rtx's for the pseudos are modified in place,
1214 so all insns that used to refer to them now refer to memory.
1216 For a reg that has a reg_equiv_address, all those insns
1217 were changed by reloading so that no insns refer to it any longer;
1218 but the DECL_RTL of a variable decl may refer to it,
1219 and if so this causes the debugging info to mention the variable. */
1221 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1223 rtx addr = 0;
1225 if (reg_equiv_mem[i])
1226 addr = XEXP (reg_equiv_mem[i], 0);
1228 if (reg_equiv_address[i])
1229 addr = reg_equiv_address[i];
1231 if (addr)
1233 if (reg_renumber[i] < 0)
1235 rtx reg = regno_reg_rtx[i];
1237 REG_USERVAR_P (reg) = 0;
1238 PUT_CODE (reg, MEM);
1239 XEXP (reg, 0) = addr;
1240 if (reg_equiv_memory_loc[i])
1241 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc[i]);
1242 else
1244 MEM_IN_STRUCT_P (reg) = MEM_SCALAR_P (reg) = 0;
1245 MEM_ATTRS (reg) = 0;
1247 MEM_NOTRAP_P (reg) = 1;
1249 else if (reg_equiv_mem[i])
1250 XEXP (reg_equiv_mem[i], 0) = addr;
1253 /* We don't want complex addressing modes in debug insns
1254 if simpler ones will do, so delegitimize equivalences
1255 in debug insns. */
1256 if (MAY_HAVE_DEBUG_INSNS && reg_renumber[i] < 0)
1258 rtx reg = regno_reg_rtx[i];
1259 rtx equiv = 0;
1260 df_ref use, next;
1262 if (reg_equiv_constant[i])
1263 equiv = reg_equiv_constant[i];
1264 else if (reg_equiv_invariant[i])
1265 equiv = reg_equiv_invariant[i];
1266 else if (reg && MEM_P (reg))
1267 equiv = targetm.delegitimize_address (reg);
1268 else if (reg && REG_P (reg) && (int)REGNO (reg) != i)
1269 equiv = reg;
1271 if (equiv == reg)
1272 continue;
1274 for (use = DF_REG_USE_CHAIN (i); use; use = next)
1276 insn = DF_REF_INSN (use);
1278 /* Make sure the next ref is for a different instruction,
1279 so that we're not affected by the rescan. */
1280 next = DF_REF_NEXT_REG (use);
1281 while (next && DF_REF_INSN (next) == insn)
1282 next = DF_REF_NEXT_REG (next);
1284 if (DEBUG_INSN_P (insn))
1286 if (!equiv)
1288 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
1289 df_insn_rescan_debug_internal (insn);
1291 else
1292 INSN_VAR_LOCATION_LOC (insn)
1293 = simplify_replace_rtx (INSN_VAR_LOCATION_LOC (insn),
1294 reg, equiv);
1300 /* We must set reload_completed now since the cleanup_subreg_operands call
1301 below will re-recognize each insn and reload may have generated insns
1302 which are only valid during and after reload. */
1303 reload_completed = 1;
1305 /* Make a pass over all the insns and delete all USEs which we inserted
1306 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1307 notes. Delete all CLOBBER insns, except those that refer to the return
1308 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1309 from misarranging variable-array code, and simplify (subreg (reg))
1310 operands. Strip and regenerate REG_INC notes that may have been moved
1311 around. */
1313 for (insn = first; insn; insn = NEXT_INSN (insn))
1314 if (INSN_P (insn))
1316 rtx *pnote;
1318 if (CALL_P (insn))
1319 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1320 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1322 if ((GET_CODE (PATTERN (insn)) == USE
1323 /* We mark with QImode USEs introduced by reload itself. */
1324 && (GET_MODE (insn) == QImode
1325 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1326 || (GET_CODE (PATTERN (insn)) == CLOBBER
1327 && (!MEM_P (XEXP (PATTERN (insn), 0))
1328 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1329 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1330 && XEXP (XEXP (PATTERN (insn), 0), 0)
1331 != stack_pointer_rtx))
1332 && (!REG_P (XEXP (PATTERN (insn), 0))
1333 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1335 delete_insn (insn);
1336 continue;
1339 /* Some CLOBBERs may survive until here and still reference unassigned
1340 pseudos with const equivalent, which may in turn cause ICE in later
1341 passes if the reference remains in place. */
1342 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1343 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1344 VOIDmode, PATTERN (insn));
1346 /* Discard obvious no-ops, even without -O. This optimization
1347 is fast and doesn't interfere with debugging. */
1348 if (NONJUMP_INSN_P (insn)
1349 && GET_CODE (PATTERN (insn)) == SET
1350 && REG_P (SET_SRC (PATTERN (insn)))
1351 && REG_P (SET_DEST (PATTERN (insn)))
1352 && (REGNO (SET_SRC (PATTERN (insn)))
1353 == REGNO (SET_DEST (PATTERN (insn)))))
1355 delete_insn (insn);
1356 continue;
1359 pnote = &REG_NOTES (insn);
1360 while (*pnote != 0)
1362 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1363 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1364 || REG_NOTE_KIND (*pnote) == REG_INC)
1365 *pnote = XEXP (*pnote, 1);
1366 else
1367 pnote = &XEXP (*pnote, 1);
1370 #ifdef AUTO_INC_DEC
1371 add_auto_inc_notes (insn, PATTERN (insn));
1372 #endif
1374 /* Simplify (subreg (reg)) if it appears as an operand. */
1375 cleanup_subreg_operands (insn);
1377 /* Clean up invalid ASMs so that they don't confuse later passes.
1378 See PR 21299. */
1379 if (asm_noperands (PATTERN (insn)) >= 0)
1381 extract_insn (insn);
1382 if (!constrain_operands (1))
1384 error_for_asm (insn,
1385 "%<asm%> operand has impossible constraints");
1386 delete_insn (insn);
1387 continue;
1392 /* If we are doing generic stack checking, give a warning if this
1393 function's frame size is larger than we expect. */
1394 if (flag_stack_check == GENERIC_STACK_CHECK)
1396 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1397 static int verbose_warned = 0;
1399 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1400 if (df_regs_ever_live_p (i) && ! fixed_regs[i] && call_used_regs[i])
1401 size += UNITS_PER_WORD;
1403 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1405 warning (0, "frame size too large for reliable stack checking");
1406 if (! verbose_warned)
1408 warning (0, "try reducing the number of local variables");
1409 verbose_warned = 1;
1414 /* Indicate that we no longer have known memory locations or constants. */
1415 if (reg_equiv_constant)
1416 free (reg_equiv_constant);
1417 if (reg_equiv_invariant)
1418 free (reg_equiv_invariant);
1419 reg_equiv_constant = 0;
1420 reg_equiv_invariant = 0;
1421 VEC_free (rtx, gc, reg_equiv_memory_loc_vec);
1422 reg_equiv_memory_loc = 0;
1424 free (temp_pseudo_reg_arr);
1426 if (offsets_known_at)
1427 free (offsets_known_at);
1428 if (offsets_at)
1429 free (offsets_at);
1431 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1432 if (reg_equiv_alt_mem_list[i])
1433 free_EXPR_LIST_list (&reg_equiv_alt_mem_list[i]);
1434 free (reg_equiv_alt_mem_list);
1436 free (reg_equiv_mem);
1437 reg_equiv_init = 0;
1438 free (reg_equiv_address);
1439 free (reg_max_ref_width);
1440 free (reg_old_renumber);
1441 free (pseudo_previous_regs);
1442 free (pseudo_forbidden_regs);
1444 CLEAR_HARD_REG_SET (used_spill_regs);
1445 for (i = 0; i < n_spills; i++)
1446 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1448 /* Free all the insn_chain structures at once. */
1449 obstack_free (&reload_obstack, reload_startobj);
1450 unused_insn_chains = 0;
1451 fixup_abnormal_edges ();
1453 /* Replacing pseudos with their memory equivalents might have
1454 created shared rtx. Subsequent passes would get confused
1455 by this, so unshare everything here. */
1456 unshare_all_rtl_again (first);
1458 #ifdef STACK_BOUNDARY
1459 /* init_emit has set the alignment of the hard frame pointer
1460 to STACK_BOUNDARY. It is very likely no longer valid if
1461 the hard frame pointer was used for register allocation. */
1462 if (!frame_pointer_needed)
1463 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1464 #endif
1466 VEC_free (rtx_p, heap, substitute_stack);
1468 return failure;
1471 /* Yet another special case. Unfortunately, reg-stack forces people to
1472 write incorrect clobbers in asm statements. These clobbers must not
1473 cause the register to appear in bad_spill_regs, otherwise we'll call
1474 fatal_insn later. We clear the corresponding regnos in the live
1475 register sets to avoid this.
1476 The whole thing is rather sick, I'm afraid. */
1478 static void
1479 maybe_fix_stack_asms (void)
1481 #ifdef STACK_REGS
1482 const char *constraints[MAX_RECOG_OPERANDS];
1483 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1484 struct insn_chain *chain;
1486 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1488 int i, noperands;
1489 HARD_REG_SET clobbered, allowed;
1490 rtx pat;
1492 if (! INSN_P (chain->insn)
1493 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1494 continue;
1495 pat = PATTERN (chain->insn);
1496 if (GET_CODE (pat) != PARALLEL)
1497 continue;
1499 CLEAR_HARD_REG_SET (clobbered);
1500 CLEAR_HARD_REG_SET (allowed);
1502 /* First, make a mask of all stack regs that are clobbered. */
1503 for (i = 0; i < XVECLEN (pat, 0); i++)
1505 rtx t = XVECEXP (pat, 0, i);
1506 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1507 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1510 /* Get the operand values and constraints out of the insn. */
1511 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1512 constraints, operand_mode, NULL);
1514 /* For every operand, see what registers are allowed. */
1515 for (i = 0; i < noperands; i++)
1517 const char *p = constraints[i];
1518 /* For every alternative, we compute the class of registers allowed
1519 for reloading in CLS, and merge its contents into the reg set
1520 ALLOWED. */
1521 int cls = (int) NO_REGS;
1523 for (;;)
1525 char c = *p;
1527 if (c == '\0' || c == ',' || c == '#')
1529 /* End of one alternative - mark the regs in the current
1530 class, and reset the class. */
1531 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1532 cls = NO_REGS;
1533 p++;
1534 if (c == '#')
1535 do {
1536 c = *p++;
1537 } while (c != '\0' && c != ',');
1538 if (c == '\0')
1539 break;
1540 continue;
1543 switch (c)
1545 case '=': case '+': case '*': case '%': case '?': case '!':
1546 case '0': case '1': case '2': case '3': case '4': case '<':
1547 case '>': case 'V': case 'o': case '&': case 'E': case 'F':
1548 case 's': case 'i': case 'n': case 'X': case 'I': case 'J':
1549 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P':
1550 case TARGET_MEM_CONSTRAINT:
1551 break;
1553 case 'p':
1554 cls = (int) reg_class_subunion[cls]
1555 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1556 break;
1558 case 'g':
1559 case 'r':
1560 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1561 break;
1563 default:
1564 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1565 cls = (int) reg_class_subunion[cls]
1566 [(int) base_reg_class (VOIDmode, ADDRESS, SCRATCH)];
1567 else
1568 cls = (int) reg_class_subunion[cls]
1569 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1571 p += CONSTRAINT_LEN (c, p);
1574 /* Those of the registers which are clobbered, but allowed by the
1575 constraints, must be usable as reload registers. So clear them
1576 out of the life information. */
1577 AND_HARD_REG_SET (allowed, clobbered);
1578 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1579 if (TEST_HARD_REG_BIT (allowed, i))
1581 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1582 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1586 #endif
1589 /* Copy the global variables n_reloads and rld into the corresponding elts
1590 of CHAIN. */
1591 static void
1592 copy_reloads (struct insn_chain *chain)
1594 chain->n_reloads = n_reloads;
1595 chain->rld = XOBNEWVEC (&reload_obstack, struct reload, n_reloads);
1596 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1597 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1600 /* Walk the chain of insns, and determine for each whether it needs reloads
1601 and/or eliminations. Build the corresponding insns_need_reload list, and
1602 set something_needs_elimination as appropriate. */
1603 static void
1604 calculate_needs_all_insns (int global)
1606 struct insn_chain **pprev_reload = &insns_need_reload;
1607 struct insn_chain *chain, *next = 0;
1609 something_needs_elimination = 0;
1611 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1612 for (chain = reload_insn_chain; chain != 0; chain = next)
1614 rtx insn = chain->insn;
1616 next = chain->next;
1618 /* Clear out the shortcuts. */
1619 chain->n_reloads = 0;
1620 chain->need_elim = 0;
1621 chain->need_reload = 0;
1622 chain->need_operand_change = 0;
1624 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1625 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1626 what effects this has on the known offsets at labels. */
1628 if (LABEL_P (insn) || JUMP_P (insn)
1629 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1630 set_label_offsets (insn, insn, 0);
1632 if (INSN_P (insn))
1634 rtx old_body = PATTERN (insn);
1635 int old_code = INSN_CODE (insn);
1636 rtx old_notes = REG_NOTES (insn);
1637 int did_elimination = 0;
1638 int operands_changed = 0;
1639 rtx set = single_set (insn);
1641 /* Skip insns that only set an equivalence. */
1642 if (set && REG_P (SET_DEST (set))
1643 && reg_renumber[REGNO (SET_DEST (set))] < 0
1644 && (reg_equiv_constant[REGNO (SET_DEST (set))]
1645 || (reg_equiv_invariant[REGNO (SET_DEST (set))]))
1646 && reg_equiv_init[REGNO (SET_DEST (set))])
1647 continue;
1649 /* If needed, eliminate any eliminable registers. */
1650 if (num_eliminable || num_eliminable_invariants)
1651 did_elimination = eliminate_regs_in_insn (insn, 0);
1653 /* Analyze the instruction. */
1654 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1655 global, spill_reg_order);
1657 /* If a no-op set needs more than one reload, this is likely
1658 to be something that needs input address reloads. We
1659 can't get rid of this cleanly later, and it is of no use
1660 anyway, so discard it now.
1661 We only do this when expensive_optimizations is enabled,
1662 since this complements reload inheritance / output
1663 reload deletion, and it can make debugging harder. */
1664 if (flag_expensive_optimizations && n_reloads > 1)
1666 rtx set = single_set (insn);
1667 if (set
1669 ((SET_SRC (set) == SET_DEST (set)
1670 && REG_P (SET_SRC (set))
1671 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1672 || (REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
1673 && reg_renumber[REGNO (SET_SRC (set))] < 0
1674 && reg_renumber[REGNO (SET_DEST (set))] < 0
1675 && reg_equiv_memory_loc[REGNO (SET_SRC (set))] != NULL
1676 && reg_equiv_memory_loc[REGNO (SET_DEST (set))] != NULL
1677 && rtx_equal_p (reg_equiv_memory_loc
1678 [REGNO (SET_SRC (set))],
1679 reg_equiv_memory_loc
1680 [REGNO (SET_DEST (set))]))))
1682 if (ira_conflicts_p)
1683 /* Inform IRA about the insn deletion. */
1684 ira_mark_memory_move_deletion (REGNO (SET_DEST (set)),
1685 REGNO (SET_SRC (set)));
1686 delete_insn (insn);
1687 /* Delete it from the reload chain. */
1688 if (chain->prev)
1689 chain->prev->next = next;
1690 else
1691 reload_insn_chain = next;
1692 if (next)
1693 next->prev = chain->prev;
1694 chain->next = unused_insn_chains;
1695 unused_insn_chains = chain;
1696 continue;
1699 if (num_eliminable)
1700 update_eliminable_offsets ();
1702 /* Remember for later shortcuts which insns had any reloads or
1703 register eliminations. */
1704 chain->need_elim = did_elimination;
1705 chain->need_reload = n_reloads > 0;
1706 chain->need_operand_change = operands_changed;
1708 /* Discard any register replacements done. */
1709 if (did_elimination)
1711 obstack_free (&reload_obstack, reload_insn_firstobj);
1712 PATTERN (insn) = old_body;
1713 INSN_CODE (insn) = old_code;
1714 REG_NOTES (insn) = old_notes;
1715 something_needs_elimination = 1;
1718 something_needs_operands_changed |= operands_changed;
1720 if (n_reloads != 0)
1722 copy_reloads (chain);
1723 *pprev_reload = chain;
1724 pprev_reload = &chain->next_need_reload;
1728 *pprev_reload = 0;
1731 /* Comparison function for qsort to decide which of two reloads
1732 should be handled first. *P1 and *P2 are the reload numbers. */
1734 static int
1735 reload_reg_class_lower (const void *r1p, const void *r2p)
1737 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1738 int t;
1740 /* Consider required reloads before optional ones. */
1741 t = rld[r1].optional - rld[r2].optional;
1742 if (t != 0)
1743 return t;
1745 /* Count all solitary classes before non-solitary ones. */
1746 t = ((reg_class_size[(int) rld[r2].rclass] == 1)
1747 - (reg_class_size[(int) rld[r1].rclass] == 1));
1748 if (t != 0)
1749 return t;
1751 /* Aside from solitaires, consider all multi-reg groups first. */
1752 t = rld[r2].nregs - rld[r1].nregs;
1753 if (t != 0)
1754 return t;
1756 /* Consider reloads in order of increasing reg-class number. */
1757 t = (int) rld[r1].rclass - (int) rld[r2].rclass;
1758 if (t != 0)
1759 return t;
1761 /* If reloads are equally urgent, sort by reload number,
1762 so that the results of qsort leave nothing to chance. */
1763 return r1 - r2;
1766 /* The cost of spilling each hard reg. */
1767 static int spill_cost[FIRST_PSEUDO_REGISTER];
1769 /* When spilling multiple hard registers, we use SPILL_COST for the first
1770 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1771 only the first hard reg for a multi-reg pseudo. */
1772 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1774 /* Map of hard regno to pseudo regno currently occupying the hard
1775 reg. */
1776 static int hard_regno_to_pseudo_regno[FIRST_PSEUDO_REGISTER];
1778 /* Update the spill cost arrays, considering that pseudo REG is live. */
1780 static void
1781 count_pseudo (int reg)
1783 int freq = REG_FREQ (reg);
1784 int r = reg_renumber[reg];
1785 int nregs;
1787 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1788 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1789 /* Ignore spilled pseudo-registers which can be here only if IRA
1790 is used. */
1791 || (ira_conflicts_p && r < 0))
1792 return;
1794 SET_REGNO_REG_SET (&pseudos_counted, reg);
1796 gcc_assert (r >= 0);
1798 spill_add_cost[r] += freq;
1799 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1800 while (nregs-- > 0)
1802 hard_regno_to_pseudo_regno[r + nregs] = reg;
1803 spill_cost[r + nregs] += freq;
1807 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1808 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1810 static void
1811 order_regs_for_reload (struct insn_chain *chain)
1813 unsigned i;
1814 HARD_REG_SET used_by_pseudos;
1815 HARD_REG_SET used_by_pseudos2;
1816 reg_set_iterator rsi;
1818 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1820 memset (spill_cost, 0, sizeof spill_cost);
1821 memset (spill_add_cost, 0, sizeof spill_add_cost);
1822 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1823 hard_regno_to_pseudo_regno[i] = -1;
1825 /* Count number of uses of each hard reg by pseudo regs allocated to it
1826 and then order them by decreasing use. First exclude hard registers
1827 that are live in or across this insn. */
1829 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1830 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1831 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1832 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1834 /* Now find out which pseudos are allocated to it, and update
1835 hard_reg_n_uses. */
1836 CLEAR_REG_SET (&pseudos_counted);
1838 EXECUTE_IF_SET_IN_REG_SET
1839 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1841 count_pseudo (i);
1843 EXECUTE_IF_SET_IN_REG_SET
1844 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1846 count_pseudo (i);
1848 CLEAR_REG_SET (&pseudos_counted);
1851 /* Vector of reload-numbers showing the order in which the reloads should
1852 be processed. */
1853 static short reload_order[MAX_RELOADS];
1855 /* This is used to keep track of the spill regs used in one insn. */
1856 static HARD_REG_SET used_spill_regs_local;
1858 /* We decided to spill hard register SPILLED, which has a size of
1859 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1860 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1861 update SPILL_COST/SPILL_ADD_COST. */
1863 static void
1864 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1866 int freq = REG_FREQ (reg);
1867 int r = reg_renumber[reg];
1868 int nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1870 /* Ignore spilled pseudo-registers which can be here only if IRA is
1871 used. */
1872 if ((ira_conflicts_p && r < 0)
1873 || REGNO_REG_SET_P (&spilled_pseudos, reg)
1874 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1875 return;
1877 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1879 spill_add_cost[r] -= freq;
1880 while (nregs-- > 0)
1882 hard_regno_to_pseudo_regno[r + nregs] = -1;
1883 spill_cost[r + nregs] -= freq;
1887 /* Find reload register to use for reload number ORDER. */
1889 static int
1890 find_reg (struct insn_chain *chain, int order)
1892 int rnum = reload_order[order];
1893 struct reload *rl = rld + rnum;
1894 int best_cost = INT_MAX;
1895 int best_reg = -1;
1896 unsigned int i, j, n;
1897 int k;
1898 HARD_REG_SET not_usable;
1899 HARD_REG_SET used_by_other_reload;
1900 reg_set_iterator rsi;
1901 static int regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1902 static int best_regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1904 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1905 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1906 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->rclass]);
1908 CLEAR_HARD_REG_SET (used_by_other_reload);
1909 for (k = 0; k < order; k++)
1911 int other = reload_order[k];
1913 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1914 for (j = 0; j < rld[other].nregs; j++)
1915 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1918 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1920 #ifdef REG_ALLOC_ORDER
1921 unsigned int regno = reg_alloc_order[i];
1922 #else
1923 unsigned int regno = i;
1924 #endif
1926 if (! TEST_HARD_REG_BIT (not_usable, regno)
1927 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1928 && HARD_REGNO_MODE_OK (regno, rl->mode))
1930 int this_cost = spill_cost[regno];
1931 int ok = 1;
1932 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1934 for (j = 1; j < this_nregs; j++)
1936 this_cost += spill_add_cost[regno + j];
1937 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1938 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1939 ok = 0;
1941 if (! ok)
1942 continue;
1944 if (ira_conflicts_p)
1946 /* Ask IRA to find a better pseudo-register for
1947 spilling. */
1948 for (n = j = 0; j < this_nregs; j++)
1950 int r = hard_regno_to_pseudo_regno[regno + j];
1952 if (r < 0)
1953 continue;
1954 if (n == 0 || regno_pseudo_regs[n - 1] != r)
1955 regno_pseudo_regs[n++] = r;
1957 regno_pseudo_regs[n++] = -1;
1958 if (best_reg < 0
1959 || ira_better_spill_reload_regno_p (regno_pseudo_regs,
1960 best_regno_pseudo_regs,
1961 rl->in, rl->out,
1962 chain->insn))
1964 best_reg = regno;
1965 for (j = 0;; j++)
1967 best_regno_pseudo_regs[j] = regno_pseudo_regs[j];
1968 if (regno_pseudo_regs[j] < 0)
1969 break;
1972 continue;
1975 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1976 this_cost--;
1977 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1978 this_cost--;
1979 if (this_cost < best_cost
1980 /* Among registers with equal cost, prefer caller-saved ones, or
1981 use REG_ALLOC_ORDER if it is defined. */
1982 || (this_cost == best_cost
1983 #ifdef REG_ALLOC_ORDER
1984 && (inv_reg_alloc_order[regno]
1985 < inv_reg_alloc_order[best_reg])
1986 #else
1987 && call_used_regs[regno]
1988 && ! call_used_regs[best_reg]
1989 #endif
1992 best_reg = regno;
1993 best_cost = this_cost;
1997 if (best_reg == -1)
1998 return 0;
2000 if (dump_file)
2001 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
2003 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
2004 rl->regno = best_reg;
2006 EXECUTE_IF_SET_IN_REG_SET
2007 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
2009 count_spilled_pseudo (best_reg, rl->nregs, j);
2012 EXECUTE_IF_SET_IN_REG_SET
2013 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
2015 count_spilled_pseudo (best_reg, rl->nregs, j);
2018 for (i = 0; i < rl->nregs; i++)
2020 gcc_assert (spill_cost[best_reg + i] == 0);
2021 gcc_assert (spill_add_cost[best_reg + i] == 0);
2022 gcc_assert (hard_regno_to_pseudo_regno[best_reg + i] == -1);
2023 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
2025 return 1;
2028 /* Find more reload regs to satisfy the remaining need of an insn, which
2029 is given by CHAIN.
2030 Do it by ascending class number, since otherwise a reg
2031 might be spilled for a big class and might fail to count
2032 for a smaller class even though it belongs to that class. */
2034 static void
2035 find_reload_regs (struct insn_chain *chain)
2037 int i;
2039 /* In order to be certain of getting the registers we need,
2040 we must sort the reloads into order of increasing register class.
2041 Then our grabbing of reload registers will parallel the process
2042 that provided the reload registers. */
2043 for (i = 0; i < chain->n_reloads; i++)
2045 /* Show whether this reload already has a hard reg. */
2046 if (chain->rld[i].reg_rtx)
2048 int regno = REGNO (chain->rld[i].reg_rtx);
2049 chain->rld[i].regno = regno;
2050 chain->rld[i].nregs
2051 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
2053 else
2054 chain->rld[i].regno = -1;
2055 reload_order[i] = i;
2058 n_reloads = chain->n_reloads;
2059 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
2061 CLEAR_HARD_REG_SET (used_spill_regs_local);
2063 if (dump_file)
2064 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
2066 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
2068 /* Compute the order of preference for hard registers to spill. */
2070 order_regs_for_reload (chain);
2072 for (i = 0; i < n_reloads; i++)
2074 int r = reload_order[i];
2076 /* Ignore reloads that got marked inoperative. */
2077 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
2078 && ! rld[r].optional
2079 && rld[r].regno == -1)
2080 if (! find_reg (chain, i))
2082 if (dump_file)
2083 fprintf (dump_file, "reload failure for reload %d\n", r);
2084 spill_failure (chain->insn, rld[r].rclass);
2085 failure = 1;
2086 return;
2090 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
2091 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
2093 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
2096 static void
2097 select_reload_regs (void)
2099 struct insn_chain *chain;
2101 /* Try to satisfy the needs for each insn. */
2102 for (chain = insns_need_reload; chain != 0;
2103 chain = chain->next_need_reload)
2104 find_reload_regs (chain);
2107 /* Delete all insns that were inserted by emit_caller_save_insns during
2108 this iteration. */
2109 static void
2110 delete_caller_save_insns (void)
2112 struct insn_chain *c = reload_insn_chain;
2114 while (c != 0)
2116 while (c != 0 && c->is_caller_save_insn)
2118 struct insn_chain *next = c->next;
2119 rtx insn = c->insn;
2121 if (c == reload_insn_chain)
2122 reload_insn_chain = next;
2123 delete_insn (insn);
2125 if (next)
2126 next->prev = c->prev;
2127 if (c->prev)
2128 c->prev->next = next;
2129 c->next = unused_insn_chains;
2130 unused_insn_chains = c;
2131 c = next;
2133 if (c != 0)
2134 c = c->next;
2138 /* Handle the failure to find a register to spill.
2139 INSN should be one of the insns which needed this particular spill reg. */
2141 static void
2142 spill_failure (rtx insn, enum reg_class rclass)
2144 if (asm_noperands (PATTERN (insn)) >= 0)
2145 error_for_asm (insn, "can't find a register in class %qs while "
2146 "reloading %<asm%>",
2147 reg_class_names[rclass]);
2148 else
2150 error ("unable to find a register to spill in class %qs",
2151 reg_class_names[rclass]);
2153 if (dump_file)
2155 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2156 debug_reload_to_stream (dump_file);
2158 fatal_insn ("this is the insn:", insn);
2162 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2163 data that is dead in INSN. */
2165 static void
2166 delete_dead_insn (rtx insn)
2168 rtx prev = prev_real_insn (insn);
2169 rtx prev_dest;
2171 /* If the previous insn sets a register that dies in our insn, delete it
2172 too. */
2173 if (prev && GET_CODE (PATTERN (prev)) == SET
2174 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2175 && reg_mentioned_p (prev_dest, PATTERN (insn))
2176 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2177 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2178 delete_dead_insn (prev);
2180 SET_INSN_DELETED (insn);
2183 /* Modify the home of pseudo-reg I.
2184 The new home is present in reg_renumber[I].
2186 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2187 or it may be -1, meaning there is none or it is not relevant.
2188 This is used so that all pseudos spilled from a given hard reg
2189 can share one stack slot. */
2191 static void
2192 alter_reg (int i, int from_reg, bool dont_share_p)
2194 /* When outputting an inline function, this can happen
2195 for a reg that isn't actually used. */
2196 if (regno_reg_rtx[i] == 0)
2197 return;
2199 /* If the reg got changed to a MEM at rtl-generation time,
2200 ignore it. */
2201 if (!REG_P (regno_reg_rtx[i]))
2202 return;
2204 /* Modify the reg-rtx to contain the new hard reg
2205 number or else to contain its pseudo reg number. */
2206 SET_REGNO (regno_reg_rtx[i],
2207 reg_renumber[i] >= 0 ? reg_renumber[i] : i);
2209 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2210 allocate a stack slot for it. */
2212 if (reg_renumber[i] < 0
2213 && REG_N_REFS (i) > 0
2214 && reg_equiv_constant[i] == 0
2215 && (reg_equiv_invariant[i] == 0 || reg_equiv_init[i] == 0)
2216 && reg_equiv_memory_loc[i] == 0)
2218 rtx x = NULL_RTX;
2219 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2220 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2221 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2222 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2223 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2224 int adjust = 0;
2226 if (ira_conflicts_p)
2228 /* Mark the spill for IRA. */
2229 SET_REGNO_REG_SET (&spilled_pseudos, i);
2230 if (!dont_share_p)
2231 x = ira_reuse_stack_slot (i, inherent_size, total_size);
2234 if (x)
2237 /* Each pseudo reg has an inherent size which comes from its own mode,
2238 and a total size which provides room for paradoxical subregs
2239 which refer to the pseudo reg in wider modes.
2241 We can use a slot already allocated if it provides both
2242 enough inherent space and enough total space.
2243 Otherwise, we allocate a new slot, making sure that it has no less
2244 inherent space, and no less total space, then the previous slot. */
2245 else if (from_reg == -1 || (!dont_share_p && ira_conflicts_p))
2247 rtx stack_slot;
2249 /* No known place to spill from => no slot to reuse. */
2250 x = assign_stack_local (mode, total_size,
2251 min_align > inherent_align
2252 || total_size > inherent_size ? -1 : 0);
2254 stack_slot = x;
2256 /* Cancel the big-endian correction done in assign_stack_local.
2257 Get the address of the beginning of the slot. This is so we
2258 can do a big-endian correction unconditionally below. */
2259 if (BYTES_BIG_ENDIAN)
2261 adjust = inherent_size - total_size;
2262 if (adjust)
2263 stack_slot
2264 = adjust_address_nv (x, mode_for_size (total_size
2265 * BITS_PER_UNIT,
2266 MODE_INT, 1),
2267 adjust);
2270 if (! dont_share_p && ira_conflicts_p)
2271 /* Inform IRA about allocation a new stack slot. */
2272 ira_mark_new_stack_slot (stack_slot, i, total_size);
2275 /* Reuse a stack slot if possible. */
2276 else if (spill_stack_slot[from_reg] != 0
2277 && spill_stack_slot_width[from_reg] >= total_size
2278 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2279 >= inherent_size)
2280 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2281 x = spill_stack_slot[from_reg];
2283 /* Allocate a bigger slot. */
2284 else
2286 /* Compute maximum size needed, both for inherent size
2287 and for total size. */
2288 rtx stack_slot;
2290 if (spill_stack_slot[from_reg])
2292 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2293 > inherent_size)
2294 mode = GET_MODE (spill_stack_slot[from_reg]);
2295 if (spill_stack_slot_width[from_reg] > total_size)
2296 total_size = spill_stack_slot_width[from_reg];
2297 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2298 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2301 /* Make a slot with that size. */
2302 x = assign_stack_local (mode, total_size,
2303 min_align > inherent_align
2304 || total_size > inherent_size ? -1 : 0);
2305 stack_slot = x;
2307 /* Cancel the big-endian correction done in assign_stack_local.
2308 Get the address of the beginning of the slot. This is so we
2309 can do a big-endian correction unconditionally below. */
2310 if (BYTES_BIG_ENDIAN)
2312 adjust = GET_MODE_SIZE (mode) - total_size;
2313 if (adjust)
2314 stack_slot
2315 = adjust_address_nv (x, mode_for_size (total_size
2316 * BITS_PER_UNIT,
2317 MODE_INT, 1),
2318 adjust);
2321 spill_stack_slot[from_reg] = stack_slot;
2322 spill_stack_slot_width[from_reg] = total_size;
2325 /* On a big endian machine, the "address" of the slot
2326 is the address of the low part that fits its inherent mode. */
2327 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2328 adjust += (total_size - inherent_size);
2330 /* If we have any adjustment to make, or if the stack slot is the
2331 wrong mode, make a new stack slot. */
2332 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2334 /* Set all of the memory attributes as appropriate for a spill. */
2335 set_mem_attrs_for_spill (x);
2337 /* Save the stack slot for later. */
2338 reg_equiv_memory_loc[i] = x;
2342 /* Mark the slots in regs_ever_live for the hard regs used by
2343 pseudo-reg number REGNO, accessed in MODE. */
2345 static void
2346 mark_home_live_1 (int regno, enum machine_mode mode)
2348 int i, lim;
2350 i = reg_renumber[regno];
2351 if (i < 0)
2352 return;
2353 lim = end_hard_regno (mode, i);
2354 while (i < lim)
2355 df_set_regs_ever_live(i++, true);
2358 /* Mark the slots in regs_ever_live for the hard regs
2359 used by pseudo-reg number REGNO. */
2361 void
2362 mark_home_live (int regno)
2364 if (reg_renumber[regno] >= 0)
2365 mark_home_live_1 (regno, PSEUDO_REGNO_MODE (regno));
2368 /* This function handles the tracking of elimination offsets around branches.
2370 X is a piece of RTL being scanned.
2372 INSN is the insn that it came from, if any.
2374 INITIAL_P is nonzero if we are to set the offset to be the initial
2375 offset and zero if we are setting the offset of the label to be the
2376 current offset. */
2378 static void
2379 set_label_offsets (rtx x, rtx insn, int initial_p)
2381 enum rtx_code code = GET_CODE (x);
2382 rtx tem;
2383 unsigned int i;
2384 struct elim_table *p;
2386 switch (code)
2388 case LABEL_REF:
2389 if (LABEL_REF_NONLOCAL_P (x))
2390 return;
2392 x = XEXP (x, 0);
2394 /* ... fall through ... */
2396 case CODE_LABEL:
2397 /* If we know nothing about this label, set the desired offsets. Note
2398 that this sets the offset at a label to be the offset before a label
2399 if we don't know anything about the label. This is not correct for
2400 the label after a BARRIER, but is the best guess we can make. If
2401 we guessed wrong, we will suppress an elimination that might have
2402 been possible had we been able to guess correctly. */
2404 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2406 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2407 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2408 = (initial_p ? reg_eliminate[i].initial_offset
2409 : reg_eliminate[i].offset);
2410 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2413 /* Otherwise, if this is the definition of a label and it is
2414 preceded by a BARRIER, set our offsets to the known offset of
2415 that label. */
2417 else if (x == insn
2418 && (tem = prev_nonnote_insn (insn)) != 0
2419 && BARRIER_P (tem))
2420 set_offsets_for_label (insn);
2421 else
2422 /* If neither of the above cases is true, compare each offset
2423 with those previously recorded and suppress any eliminations
2424 where the offsets disagree. */
2426 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2427 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2428 != (initial_p ? reg_eliminate[i].initial_offset
2429 : reg_eliminate[i].offset))
2430 reg_eliminate[i].can_eliminate = 0;
2432 return;
2434 case JUMP_INSN:
2435 set_label_offsets (PATTERN (insn), insn, initial_p);
2437 /* ... fall through ... */
2439 case INSN:
2440 case CALL_INSN:
2441 /* Any labels mentioned in REG_LABEL_OPERAND notes can be branched
2442 to indirectly and hence must have all eliminations at their
2443 initial offsets. */
2444 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2445 if (REG_NOTE_KIND (tem) == REG_LABEL_OPERAND)
2446 set_label_offsets (XEXP (tem, 0), insn, 1);
2447 return;
2449 case PARALLEL:
2450 case ADDR_VEC:
2451 case ADDR_DIFF_VEC:
2452 /* Each of the labels in the parallel or address vector must be
2453 at their initial offsets. We want the first field for PARALLEL
2454 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2456 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2457 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2458 insn, initial_p);
2459 return;
2461 case SET:
2462 /* We only care about setting PC. If the source is not RETURN,
2463 IF_THEN_ELSE, or a label, disable any eliminations not at
2464 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2465 isn't one of those possibilities. For branches to a label,
2466 call ourselves recursively.
2468 Note that this can disable elimination unnecessarily when we have
2469 a non-local goto since it will look like a non-constant jump to
2470 someplace in the current function. This isn't a significant
2471 problem since such jumps will normally be when all elimination
2472 pairs are back to their initial offsets. */
2474 if (SET_DEST (x) != pc_rtx)
2475 return;
2477 switch (GET_CODE (SET_SRC (x)))
2479 case PC:
2480 case RETURN:
2481 return;
2483 case LABEL_REF:
2484 set_label_offsets (SET_SRC (x), insn, initial_p);
2485 return;
2487 case IF_THEN_ELSE:
2488 tem = XEXP (SET_SRC (x), 1);
2489 if (GET_CODE (tem) == LABEL_REF)
2490 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2491 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2492 break;
2494 tem = XEXP (SET_SRC (x), 2);
2495 if (GET_CODE (tem) == LABEL_REF)
2496 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2497 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2498 break;
2499 return;
2501 default:
2502 break;
2505 /* If we reach here, all eliminations must be at their initial
2506 offset because we are doing a jump to a variable address. */
2507 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2508 if (p->offset != p->initial_offset)
2509 p->can_eliminate = 0;
2510 break;
2512 default:
2513 break;
2517 /* Scan X and replace any eliminable registers (such as fp) with a
2518 replacement (such as sp), plus an offset.
2520 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2521 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2522 MEM, we are allowed to replace a sum of a register and the constant zero
2523 with the register, which we cannot do outside a MEM. In addition, we need
2524 to record the fact that a register is referenced outside a MEM.
2526 If INSN is an insn, it is the insn containing X. If we replace a REG
2527 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2528 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2529 the REG is being modified.
2531 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2532 That's used when we eliminate in expressions stored in notes.
2533 This means, do not set ref_outside_mem even if the reference
2534 is outside of MEMs.
2536 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2537 replacements done assuming all offsets are at their initial values. If
2538 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2539 encounter, return the actual location so that find_reloads will do
2540 the proper thing. */
2542 static rtx
2543 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2544 bool may_use_invariant)
2546 enum rtx_code code = GET_CODE (x);
2547 struct elim_table *ep;
2548 int regno;
2549 rtx new_rtx;
2550 int i, j;
2551 const char *fmt;
2552 int copied = 0;
2554 if (! current_function_decl)
2555 return x;
2557 switch (code)
2559 case CONST_INT:
2560 case CONST_DOUBLE:
2561 case CONST_FIXED:
2562 case CONST_VECTOR:
2563 case CONST:
2564 case SYMBOL_REF:
2565 case CODE_LABEL:
2566 case PC:
2567 case CC0:
2568 case ASM_INPUT:
2569 case ADDR_VEC:
2570 case ADDR_DIFF_VEC:
2571 case RETURN:
2572 return x;
2574 case REG:
2575 regno = REGNO (x);
2577 /* First handle the case where we encounter a bare register that
2578 is eliminable. Replace it with a PLUS. */
2579 if (regno < FIRST_PSEUDO_REGISTER)
2581 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2582 ep++)
2583 if (ep->from_rtx == x && ep->can_eliminate)
2584 return plus_constant (ep->to_rtx, ep->previous_offset);
2587 else if (reg_renumber && reg_renumber[regno] < 0
2588 && reg_equiv_invariant && reg_equiv_invariant[regno])
2590 if (may_use_invariant || (insn && DEBUG_INSN_P (insn)))
2591 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant[regno]),
2592 mem_mode, insn, true);
2593 /* There exists at least one use of REGNO that cannot be
2594 eliminated. Prevent the defining insn from being deleted. */
2595 reg_equiv_init[regno] = NULL_RTX;
2596 alter_reg (regno, -1, true);
2598 return x;
2600 /* You might think handling MINUS in a manner similar to PLUS is a
2601 good idea. It is not. It has been tried multiple times and every
2602 time the change has had to have been reverted.
2604 Other parts of reload know a PLUS is special (gen_reload for example)
2605 and require special code to handle code a reloaded PLUS operand.
2607 Also consider backends where the flags register is clobbered by a
2608 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2609 lea instruction comes to mind). If we try to reload a MINUS, we
2610 may kill the flags register that was holding a useful value.
2612 So, please before trying to handle MINUS, consider reload as a
2613 whole instead of this little section as well as the backend issues. */
2614 case PLUS:
2615 /* If this is the sum of an eliminable register and a constant, rework
2616 the sum. */
2617 if (REG_P (XEXP (x, 0))
2618 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2619 && CONSTANT_P (XEXP (x, 1)))
2621 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2622 ep++)
2623 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2625 /* The only time we want to replace a PLUS with a REG (this
2626 occurs when the constant operand of the PLUS is the negative
2627 of the offset) is when we are inside a MEM. We won't want
2628 to do so at other times because that would change the
2629 structure of the insn in a way that reload can't handle.
2630 We special-case the commonest situation in
2631 eliminate_regs_in_insn, so just replace a PLUS with a
2632 PLUS here, unless inside a MEM. */
2633 if (mem_mode != 0 && CONST_INT_P (XEXP (x, 1))
2634 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2635 return ep->to_rtx;
2636 else
2637 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2638 plus_constant (XEXP (x, 1),
2639 ep->previous_offset));
2642 /* If the register is not eliminable, we are done since the other
2643 operand is a constant. */
2644 return x;
2647 /* If this is part of an address, we want to bring any constant to the
2648 outermost PLUS. We will do this by doing register replacement in
2649 our operands and seeing if a constant shows up in one of them.
2651 Note that there is no risk of modifying the structure of the insn,
2652 since we only get called for its operands, thus we are either
2653 modifying the address inside a MEM, or something like an address
2654 operand of a load-address insn. */
2657 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2658 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2660 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2662 /* If one side is a PLUS and the other side is a pseudo that
2663 didn't get a hard register but has a reg_equiv_constant,
2664 we must replace the constant here since it may no longer
2665 be in the position of any operand. */
2666 if (GET_CODE (new0) == PLUS && REG_P (new1)
2667 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2668 && reg_renumber[REGNO (new1)] < 0
2669 && reg_equiv_constant != 0
2670 && reg_equiv_constant[REGNO (new1)] != 0)
2671 new1 = reg_equiv_constant[REGNO (new1)];
2672 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2673 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2674 && reg_renumber[REGNO (new0)] < 0
2675 && reg_equiv_constant[REGNO (new0)] != 0)
2676 new0 = reg_equiv_constant[REGNO (new0)];
2678 new_rtx = form_sum (GET_MODE (x), new0, new1);
2680 /* As above, if we are not inside a MEM we do not want to
2681 turn a PLUS into something else. We might try to do so here
2682 for an addition of 0 if we aren't optimizing. */
2683 if (! mem_mode && GET_CODE (new_rtx) != PLUS)
2684 return gen_rtx_PLUS (GET_MODE (x), new_rtx, const0_rtx);
2685 else
2686 return new_rtx;
2689 return x;
2691 case MULT:
2692 /* If this is the product of an eliminable register and a
2693 constant, apply the distribute law and move the constant out
2694 so that we have (plus (mult ..) ..). This is needed in order
2695 to keep load-address insns valid. This case is pathological.
2696 We ignore the possibility of overflow here. */
2697 if (REG_P (XEXP (x, 0))
2698 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2699 && CONST_INT_P (XEXP (x, 1)))
2700 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2701 ep++)
2702 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2704 if (! mem_mode
2705 /* Refs inside notes or in DEBUG_INSNs don't count for
2706 this purpose. */
2707 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2708 || GET_CODE (insn) == INSN_LIST
2709 || DEBUG_INSN_P (insn))))
2710 ep->ref_outside_mem = 1;
2712 return
2713 plus_constant (gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2714 ep->previous_offset * INTVAL (XEXP (x, 1)));
2717 /* ... fall through ... */
2719 case CALL:
2720 case COMPARE:
2721 /* See comments before PLUS about handling MINUS. */
2722 case MINUS:
2723 case DIV: case UDIV:
2724 case MOD: case UMOD:
2725 case AND: case IOR: case XOR:
2726 case ROTATERT: case ROTATE:
2727 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2728 case NE: case EQ:
2729 case GE: case GT: case GEU: case GTU:
2730 case LE: case LT: case LEU: case LTU:
2732 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2733 rtx new1 = XEXP (x, 1)
2734 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false) : 0;
2736 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2737 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2739 return x;
2741 case EXPR_LIST:
2742 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2743 if (XEXP (x, 0))
2745 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true);
2746 if (new_rtx != XEXP (x, 0))
2748 /* If this is a REG_DEAD note, it is not valid anymore.
2749 Using the eliminated version could result in creating a
2750 REG_DEAD note for the stack or frame pointer. */
2751 if (REG_NOTE_KIND (x) == REG_DEAD)
2752 return (XEXP (x, 1)
2753 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true)
2754 : NULL_RTX);
2756 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
2760 /* ... fall through ... */
2762 case INSN_LIST:
2763 /* Now do eliminations in the rest of the chain. If this was
2764 an EXPR_LIST, this might result in allocating more memory than is
2765 strictly needed, but it simplifies the code. */
2766 if (XEXP (x, 1))
2768 new_rtx = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true);
2769 if (new_rtx != XEXP (x, 1))
2770 return
2771 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new_rtx);
2773 return x;
2775 case PRE_INC:
2776 case POST_INC:
2777 case PRE_DEC:
2778 case POST_DEC:
2779 /* We do not support elimination of a register that is modified.
2780 elimination_effects has already make sure that this does not
2781 happen. */
2782 return x;
2784 case PRE_MODIFY:
2785 case POST_MODIFY:
2786 /* We do not support elimination of a register that is modified.
2787 elimination_effects has already make sure that this does not
2788 happen. The only remaining case we need to consider here is
2789 that the increment value may be an eliminable register. */
2790 if (GET_CODE (XEXP (x, 1)) == PLUS
2791 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2793 rtx new_rtx = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2794 insn, true);
2796 if (new_rtx != XEXP (XEXP (x, 1), 1))
2797 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2798 gen_rtx_PLUS (GET_MODE (x),
2799 XEXP (x, 0), new_rtx));
2801 return x;
2803 case STRICT_LOW_PART:
2804 case NEG: case NOT:
2805 case SIGN_EXTEND: case ZERO_EXTEND:
2806 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2807 case FLOAT: case FIX:
2808 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2809 case ABS:
2810 case SQRT:
2811 case FFS:
2812 case CLZ:
2813 case CTZ:
2814 case POPCOUNT:
2815 case PARITY:
2816 case BSWAP:
2817 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false);
2818 if (new_rtx != XEXP (x, 0))
2819 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
2820 return x;
2822 case SUBREG:
2823 /* Similar to above processing, but preserve SUBREG_BYTE.
2824 Convert (subreg (mem)) to (mem) if not paradoxical.
2825 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2826 pseudo didn't get a hard reg, we must replace this with the
2827 eliminated version of the memory location because push_reload
2828 may do the replacement in certain circumstances. */
2829 if (REG_P (SUBREG_REG (x))
2830 && (GET_MODE_SIZE (GET_MODE (x))
2831 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2832 && reg_equiv_memory_loc != 0
2833 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2835 new_rtx = SUBREG_REG (x);
2837 else
2838 new_rtx = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false);
2840 if (new_rtx != SUBREG_REG (x))
2842 int x_size = GET_MODE_SIZE (GET_MODE (x));
2843 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
2845 if (MEM_P (new_rtx)
2846 && ((x_size < new_size
2847 #ifdef WORD_REGISTER_OPERATIONS
2848 /* On these machines, combine can create rtl of the form
2849 (set (subreg:m1 (reg:m2 R) 0) ...)
2850 where m1 < m2, and expects something interesting to
2851 happen to the entire word. Moreover, it will use the
2852 (reg:m2 R) later, expecting all bits to be preserved.
2853 So if the number of words is the same, preserve the
2854 subreg so that push_reload can see it. */
2855 && ! ((x_size - 1) / UNITS_PER_WORD
2856 == (new_size -1 ) / UNITS_PER_WORD)
2857 #endif
2859 || x_size == new_size)
2861 return adjust_address_nv (new_rtx, GET_MODE (x), SUBREG_BYTE (x));
2862 else
2863 return gen_rtx_SUBREG (GET_MODE (x), new_rtx, SUBREG_BYTE (x));
2866 return x;
2868 case MEM:
2869 /* Our only special processing is to pass the mode of the MEM to our
2870 recursive call and copy the flags. While we are here, handle this
2871 case more efficiently. */
2872 return
2873 replace_equiv_address_nv (x,
2874 eliminate_regs_1 (XEXP (x, 0), GET_MODE (x),
2875 insn, true));
2877 case USE:
2878 /* Handle insn_list USE that a call to a pure function may generate. */
2879 new_rtx = eliminate_regs_1 (XEXP (x, 0), VOIDmode, insn, false);
2880 if (new_rtx != XEXP (x, 0))
2881 return gen_rtx_USE (GET_MODE (x), new_rtx);
2882 return x;
2884 case CLOBBER:
2885 gcc_assert (insn && DEBUG_INSN_P (insn));
2886 break;
2888 case ASM_OPERANDS:
2889 case SET:
2890 gcc_unreachable ();
2892 default:
2893 break;
2896 /* Process each of our operands recursively. If any have changed, make a
2897 copy of the rtx. */
2898 fmt = GET_RTX_FORMAT (code);
2899 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2901 if (*fmt == 'e')
2903 new_rtx = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false);
2904 if (new_rtx != XEXP (x, i) && ! copied)
2906 x = shallow_copy_rtx (x);
2907 copied = 1;
2909 XEXP (x, i) = new_rtx;
2911 else if (*fmt == 'E')
2913 int copied_vec = 0;
2914 for (j = 0; j < XVECLEN (x, i); j++)
2916 new_rtx = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false);
2917 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
2919 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2920 XVEC (x, i)->elem);
2921 if (! copied)
2923 x = shallow_copy_rtx (x);
2924 copied = 1;
2926 XVEC (x, i) = new_v;
2927 copied_vec = 1;
2929 XVECEXP (x, i, j) = new_rtx;
2934 return x;
2938 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2940 return eliminate_regs_1 (x, mem_mode, insn, false);
2943 /* Scan rtx X for modifications of elimination target registers. Update
2944 the table of eliminables to reflect the changed state. MEM_MODE is
2945 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2947 static void
2948 elimination_effects (rtx x, enum machine_mode mem_mode)
2950 enum rtx_code code = GET_CODE (x);
2951 struct elim_table *ep;
2952 int regno;
2953 int i, j;
2954 const char *fmt;
2956 switch (code)
2958 case CONST_INT:
2959 case CONST_DOUBLE:
2960 case CONST_FIXED:
2961 case CONST_VECTOR:
2962 case CONST:
2963 case SYMBOL_REF:
2964 case CODE_LABEL:
2965 case PC:
2966 case CC0:
2967 case ASM_INPUT:
2968 case ADDR_VEC:
2969 case ADDR_DIFF_VEC:
2970 case RETURN:
2971 return;
2973 case REG:
2974 regno = REGNO (x);
2976 /* First handle the case where we encounter a bare register that
2977 is eliminable. Replace it with a PLUS. */
2978 if (regno < FIRST_PSEUDO_REGISTER)
2980 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2981 ep++)
2982 if (ep->from_rtx == x && ep->can_eliminate)
2984 if (! mem_mode)
2985 ep->ref_outside_mem = 1;
2986 return;
2990 else if (reg_renumber[regno] < 0 && reg_equiv_constant
2991 && reg_equiv_constant[regno]
2992 && ! function_invariant_p (reg_equiv_constant[regno]))
2993 elimination_effects (reg_equiv_constant[regno], mem_mode);
2994 return;
2996 case PRE_INC:
2997 case POST_INC:
2998 case PRE_DEC:
2999 case POST_DEC:
3000 case POST_MODIFY:
3001 case PRE_MODIFY:
3002 /* If we modify the source of an elimination rule, disable it. */
3003 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3004 if (ep->from_rtx == XEXP (x, 0))
3005 ep->can_eliminate = 0;
3007 /* If we modify the target of an elimination rule by adding a constant,
3008 update its offset. If we modify the target in any other way, we'll
3009 have to disable the rule as well. */
3010 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3011 if (ep->to_rtx == XEXP (x, 0))
3013 int size = GET_MODE_SIZE (mem_mode);
3015 /* If more bytes than MEM_MODE are pushed, account for them. */
3016 #ifdef PUSH_ROUNDING
3017 if (ep->to_rtx == stack_pointer_rtx)
3018 size = PUSH_ROUNDING (size);
3019 #endif
3020 if (code == PRE_DEC || code == POST_DEC)
3021 ep->offset += size;
3022 else if (code == PRE_INC || code == POST_INC)
3023 ep->offset -= size;
3024 else if (code == PRE_MODIFY || code == POST_MODIFY)
3026 if (GET_CODE (XEXP (x, 1)) == PLUS
3027 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
3028 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
3029 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
3030 else
3031 ep->can_eliminate = 0;
3035 /* These two aren't unary operators. */
3036 if (code == POST_MODIFY || code == PRE_MODIFY)
3037 break;
3039 /* Fall through to generic unary operation case. */
3040 case STRICT_LOW_PART:
3041 case NEG: case NOT:
3042 case SIGN_EXTEND: case ZERO_EXTEND:
3043 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3044 case FLOAT: case FIX:
3045 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3046 case ABS:
3047 case SQRT:
3048 case FFS:
3049 case CLZ:
3050 case CTZ:
3051 case POPCOUNT:
3052 case PARITY:
3053 case BSWAP:
3054 elimination_effects (XEXP (x, 0), mem_mode);
3055 return;
3057 case SUBREG:
3058 if (REG_P (SUBREG_REG (x))
3059 && (GET_MODE_SIZE (GET_MODE (x))
3060 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3061 && reg_equiv_memory_loc != 0
3062 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
3063 return;
3065 elimination_effects (SUBREG_REG (x), mem_mode);
3066 return;
3068 case USE:
3069 /* If using a register that is the source of an eliminate we still
3070 think can be performed, note it cannot be performed since we don't
3071 know how this register is used. */
3072 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3073 if (ep->from_rtx == XEXP (x, 0))
3074 ep->can_eliminate = 0;
3076 elimination_effects (XEXP (x, 0), mem_mode);
3077 return;
3079 case CLOBBER:
3080 /* If clobbering a register that is the replacement register for an
3081 elimination we still think can be performed, note that it cannot
3082 be performed. Otherwise, we need not be concerned about it. */
3083 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3084 if (ep->to_rtx == XEXP (x, 0))
3085 ep->can_eliminate = 0;
3087 elimination_effects (XEXP (x, 0), mem_mode);
3088 return;
3090 case SET:
3091 /* Check for setting a register that we know about. */
3092 if (REG_P (SET_DEST (x)))
3094 /* See if this is setting the replacement register for an
3095 elimination.
3097 If DEST is the hard frame pointer, we do nothing because we
3098 assume that all assignments to the frame pointer are for
3099 non-local gotos and are being done at a time when they are valid
3100 and do not disturb anything else. Some machines want to
3101 eliminate a fake argument pointer (or even a fake frame pointer)
3102 with either the real frame or the stack pointer. Assignments to
3103 the hard frame pointer must not prevent this elimination. */
3105 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3106 ep++)
3107 if (ep->to_rtx == SET_DEST (x)
3108 && SET_DEST (x) != hard_frame_pointer_rtx)
3110 /* If it is being incremented, adjust the offset. Otherwise,
3111 this elimination can't be done. */
3112 rtx src = SET_SRC (x);
3114 if (GET_CODE (src) == PLUS
3115 && XEXP (src, 0) == SET_DEST (x)
3116 && CONST_INT_P (XEXP (src, 1)))
3117 ep->offset -= INTVAL (XEXP (src, 1));
3118 else
3119 ep->can_eliminate = 0;
3123 elimination_effects (SET_DEST (x), VOIDmode);
3124 elimination_effects (SET_SRC (x), VOIDmode);
3125 return;
3127 case MEM:
3128 /* Our only special processing is to pass the mode of the MEM to our
3129 recursive call. */
3130 elimination_effects (XEXP (x, 0), GET_MODE (x));
3131 return;
3133 default:
3134 break;
3137 fmt = GET_RTX_FORMAT (code);
3138 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3140 if (*fmt == 'e')
3141 elimination_effects (XEXP (x, i), mem_mode);
3142 else if (*fmt == 'E')
3143 for (j = 0; j < XVECLEN (x, i); j++)
3144 elimination_effects (XVECEXP (x, i, j), mem_mode);
3148 /* Descend through rtx X and verify that no references to eliminable registers
3149 remain. If any do remain, mark the involved register as not
3150 eliminable. */
3152 static void
3153 check_eliminable_occurrences (rtx x)
3155 const char *fmt;
3156 int i;
3157 enum rtx_code code;
3159 if (x == 0)
3160 return;
3162 code = GET_CODE (x);
3164 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3166 struct elim_table *ep;
3168 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3169 if (ep->from_rtx == x)
3170 ep->can_eliminate = 0;
3171 return;
3174 fmt = GET_RTX_FORMAT (code);
3175 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3177 if (*fmt == 'e')
3178 check_eliminable_occurrences (XEXP (x, i));
3179 else if (*fmt == 'E')
3181 int j;
3182 for (j = 0; j < XVECLEN (x, i); j++)
3183 check_eliminable_occurrences (XVECEXP (x, i, j));
3188 /* Scan INSN and eliminate all eliminable registers in it.
3190 If REPLACE is nonzero, do the replacement destructively. Also
3191 delete the insn as dead it if it is setting an eliminable register.
3193 If REPLACE is zero, do all our allocations in reload_obstack.
3195 If no eliminations were done and this insn doesn't require any elimination
3196 processing (these are not identical conditions: it might be updating sp,
3197 but not referencing fp; this needs to be seen during reload_as_needed so
3198 that the offset between fp and sp can be taken into consideration), zero
3199 is returned. Otherwise, 1 is returned. */
3201 static int
3202 eliminate_regs_in_insn (rtx insn, int replace)
3204 int icode = recog_memoized (insn);
3205 rtx old_body = PATTERN (insn);
3206 int insn_is_asm = asm_noperands (old_body) >= 0;
3207 rtx old_set = single_set (insn);
3208 rtx new_body;
3209 int val = 0;
3210 int i;
3211 rtx substed_operand[MAX_RECOG_OPERANDS];
3212 rtx orig_operand[MAX_RECOG_OPERANDS];
3213 struct elim_table *ep;
3214 rtx plus_src, plus_cst_src;
3216 if (! insn_is_asm && icode < 0)
3218 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3219 || GET_CODE (PATTERN (insn)) == CLOBBER
3220 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3221 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3222 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3223 || DEBUG_INSN_P (insn));
3224 if (DEBUG_INSN_P (insn))
3225 INSN_VAR_LOCATION_LOC (insn)
3226 = eliminate_regs (INSN_VAR_LOCATION_LOC (insn), VOIDmode, insn);
3227 return 0;
3230 if (old_set != 0 && REG_P (SET_DEST (old_set))
3231 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3233 /* Check for setting an eliminable register. */
3234 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3235 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3237 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3238 /* If this is setting the frame pointer register to the
3239 hardware frame pointer register and this is an elimination
3240 that will be done (tested above), this insn is really
3241 adjusting the frame pointer downward to compensate for
3242 the adjustment done before a nonlocal goto. */
3243 if (ep->from == FRAME_POINTER_REGNUM
3244 && ep->to == HARD_FRAME_POINTER_REGNUM)
3246 rtx base = SET_SRC (old_set);
3247 rtx base_insn = insn;
3248 HOST_WIDE_INT offset = 0;
3250 while (base != ep->to_rtx)
3252 rtx prev_insn, prev_set;
3254 if (GET_CODE (base) == PLUS
3255 && CONST_INT_P (XEXP (base, 1)))
3257 offset += INTVAL (XEXP (base, 1));
3258 base = XEXP (base, 0);
3260 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3261 && (prev_set = single_set (prev_insn)) != 0
3262 && rtx_equal_p (SET_DEST (prev_set), base))
3264 base = SET_SRC (prev_set);
3265 base_insn = prev_insn;
3267 else
3268 break;
3271 if (base == ep->to_rtx)
3273 rtx src
3274 = plus_constant (ep->to_rtx, offset - ep->offset);
3276 new_body = old_body;
3277 if (! replace)
3279 new_body = copy_insn (old_body);
3280 if (REG_NOTES (insn))
3281 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3283 PATTERN (insn) = new_body;
3284 old_set = single_set (insn);
3286 /* First see if this insn remains valid when we
3287 make the change. If not, keep the INSN_CODE
3288 the same and let reload fit it up. */
3289 validate_change (insn, &SET_SRC (old_set), src, 1);
3290 validate_change (insn, &SET_DEST (old_set),
3291 ep->to_rtx, 1);
3292 if (! apply_change_group ())
3294 SET_SRC (old_set) = src;
3295 SET_DEST (old_set) = ep->to_rtx;
3298 val = 1;
3299 goto done;
3302 #endif
3304 /* In this case this insn isn't serving a useful purpose. We
3305 will delete it in reload_as_needed once we know that this
3306 elimination is, in fact, being done.
3308 If REPLACE isn't set, we can't delete this insn, but needn't
3309 process it since it won't be used unless something changes. */
3310 if (replace)
3312 delete_dead_insn (insn);
3313 return 1;
3315 val = 1;
3316 goto done;
3320 /* We allow one special case which happens to work on all machines we
3321 currently support: a single set with the source or a REG_EQUAL
3322 note being a PLUS of an eliminable register and a constant. */
3323 plus_src = plus_cst_src = 0;
3324 if (old_set && REG_P (SET_DEST (old_set)))
3326 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3327 plus_src = SET_SRC (old_set);
3328 /* First see if the source is of the form (plus (...) CST). */
3329 if (plus_src
3330 && CONST_INT_P (XEXP (plus_src, 1)))
3331 plus_cst_src = plus_src;
3332 else if (REG_P (SET_SRC (old_set))
3333 || plus_src)
3335 /* Otherwise, see if we have a REG_EQUAL note of the form
3336 (plus (...) CST). */
3337 rtx links;
3338 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3340 if ((REG_NOTE_KIND (links) == REG_EQUAL
3341 || REG_NOTE_KIND (links) == REG_EQUIV)
3342 && GET_CODE (XEXP (links, 0)) == PLUS
3343 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3345 plus_cst_src = XEXP (links, 0);
3346 break;
3351 /* Check that the first operand of the PLUS is a hard reg or
3352 the lowpart subreg of one. */
3353 if (plus_cst_src)
3355 rtx reg = XEXP (plus_cst_src, 0);
3356 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3357 reg = SUBREG_REG (reg);
3359 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3360 plus_cst_src = 0;
3363 if (plus_cst_src)
3365 rtx reg = XEXP (plus_cst_src, 0);
3366 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3368 if (GET_CODE (reg) == SUBREG)
3369 reg = SUBREG_REG (reg);
3371 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3372 if (ep->from_rtx == reg && ep->can_eliminate)
3374 rtx to_rtx = ep->to_rtx;
3375 offset += ep->offset;
3376 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
3378 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3379 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3380 to_rtx);
3381 /* If we have a nonzero offset, and the source is already
3382 a simple REG, the following transformation would
3383 increase the cost of the insn by replacing a simple REG
3384 with (plus (reg sp) CST). So try only when we already
3385 had a PLUS before. */
3386 if (offset == 0 || plus_src)
3388 rtx new_src = plus_constant (to_rtx, offset);
3390 new_body = old_body;
3391 if (! replace)
3393 new_body = copy_insn (old_body);
3394 if (REG_NOTES (insn))
3395 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3397 PATTERN (insn) = new_body;
3398 old_set = single_set (insn);
3400 /* First see if this insn remains valid when we make the
3401 change. If not, try to replace the whole pattern with
3402 a simple set (this may help if the original insn was a
3403 PARALLEL that was only recognized as single_set due to
3404 REG_UNUSED notes). If this isn't valid either, keep
3405 the INSN_CODE the same and let reload fix it up. */
3406 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3408 rtx new_pat = gen_rtx_SET (VOIDmode,
3409 SET_DEST (old_set), new_src);
3411 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3412 SET_SRC (old_set) = new_src;
3415 else
3416 break;
3418 val = 1;
3419 /* This can't have an effect on elimination offsets, so skip right
3420 to the end. */
3421 goto done;
3425 /* Determine the effects of this insn on elimination offsets. */
3426 elimination_effects (old_body, VOIDmode);
3428 /* Eliminate all eliminable registers occurring in operands that
3429 can be handled by reload. */
3430 extract_insn (insn);
3431 for (i = 0; i < recog_data.n_operands; i++)
3433 orig_operand[i] = recog_data.operand[i];
3434 substed_operand[i] = recog_data.operand[i];
3436 /* For an asm statement, every operand is eliminable. */
3437 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3439 bool is_set_src, in_plus;
3441 /* Check for setting a register that we know about. */
3442 if (recog_data.operand_type[i] != OP_IN
3443 && REG_P (orig_operand[i]))
3445 /* If we are assigning to a register that can be eliminated, it
3446 must be as part of a PARALLEL, since the code above handles
3447 single SETs. We must indicate that we can no longer
3448 eliminate this reg. */
3449 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3450 ep++)
3451 if (ep->from_rtx == orig_operand[i])
3452 ep->can_eliminate = 0;
3455 /* Companion to the above plus substitution, we can allow
3456 invariants as the source of a plain move. */
3457 is_set_src = false;
3458 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3459 is_set_src = true;
3460 in_plus = false;
3461 if (plus_src
3462 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3463 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3464 in_plus = true;
3466 substed_operand[i]
3467 = eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3468 replace ? insn : NULL_RTX,
3469 is_set_src || in_plus);
3470 if (substed_operand[i] != orig_operand[i])
3471 val = 1;
3472 /* Terminate the search in check_eliminable_occurrences at
3473 this point. */
3474 *recog_data.operand_loc[i] = 0;
3476 /* If an output operand changed from a REG to a MEM and INSN is an
3477 insn, write a CLOBBER insn. */
3478 if (recog_data.operand_type[i] != OP_IN
3479 && REG_P (orig_operand[i])
3480 && MEM_P (substed_operand[i])
3481 && replace)
3482 emit_insn_after (gen_clobber (orig_operand[i]), insn);
3486 for (i = 0; i < recog_data.n_dups; i++)
3487 *recog_data.dup_loc[i]
3488 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3490 /* If any eliminable remain, they aren't eliminable anymore. */
3491 check_eliminable_occurrences (old_body);
3493 /* Substitute the operands; the new values are in the substed_operand
3494 array. */
3495 for (i = 0; i < recog_data.n_operands; i++)
3496 *recog_data.operand_loc[i] = substed_operand[i];
3497 for (i = 0; i < recog_data.n_dups; i++)
3498 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3500 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3501 re-recognize the insn. We do this in case we had a simple addition
3502 but now can do this as a load-address. This saves an insn in this
3503 common case.
3504 If re-recognition fails, the old insn code number will still be used,
3505 and some register operands may have changed into PLUS expressions.
3506 These will be handled by find_reloads by loading them into a register
3507 again. */
3509 if (val)
3511 /* If we aren't replacing things permanently and we changed something,
3512 make another copy to ensure that all the RTL is new. Otherwise
3513 things can go wrong if find_reload swaps commutative operands
3514 and one is inside RTL that has been copied while the other is not. */
3515 new_body = old_body;
3516 if (! replace)
3518 new_body = copy_insn (old_body);
3519 if (REG_NOTES (insn))
3520 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3522 PATTERN (insn) = new_body;
3524 /* If we had a move insn but now we don't, rerecognize it. This will
3525 cause spurious re-recognition if the old move had a PARALLEL since
3526 the new one still will, but we can't call single_set without
3527 having put NEW_BODY into the insn and the re-recognition won't
3528 hurt in this rare case. */
3529 /* ??? Why this huge if statement - why don't we just rerecognize the
3530 thing always? */
3531 if (! insn_is_asm
3532 && old_set != 0
3533 && ((REG_P (SET_SRC (old_set))
3534 && (GET_CODE (new_body) != SET
3535 || !REG_P (SET_SRC (new_body))))
3536 /* If this was a load from or store to memory, compare
3537 the MEM in recog_data.operand to the one in the insn.
3538 If they are not equal, then rerecognize the insn. */
3539 || (old_set != 0
3540 && ((MEM_P (SET_SRC (old_set))
3541 && SET_SRC (old_set) != recog_data.operand[1])
3542 || (MEM_P (SET_DEST (old_set))
3543 && SET_DEST (old_set) != recog_data.operand[0])))
3544 /* If this was an add insn before, rerecognize. */
3545 || GET_CODE (SET_SRC (old_set)) == PLUS))
3547 int new_icode = recog (PATTERN (insn), insn, 0);
3548 if (new_icode >= 0)
3549 INSN_CODE (insn) = new_icode;
3553 /* Restore the old body. If there were any changes to it, we made a copy
3554 of it while the changes were still in place, so we'll correctly return
3555 a modified insn below. */
3556 if (! replace)
3558 /* Restore the old body. */
3559 for (i = 0; i < recog_data.n_operands; i++)
3560 /* Restoring a top-level match_parallel would clobber the new_body
3561 we installed in the insn. */
3562 if (recog_data.operand_loc[i] != &PATTERN (insn))
3563 *recog_data.operand_loc[i] = orig_operand[i];
3564 for (i = 0; i < recog_data.n_dups; i++)
3565 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3568 /* Update all elimination pairs to reflect the status after the current
3569 insn. The changes we make were determined by the earlier call to
3570 elimination_effects.
3572 We also detect cases where register elimination cannot be done,
3573 namely, if a register would be both changed and referenced outside a MEM
3574 in the resulting insn since such an insn is often undefined and, even if
3575 not, we cannot know what meaning will be given to it. Note that it is
3576 valid to have a register used in an address in an insn that changes it
3577 (presumably with a pre- or post-increment or decrement).
3579 If anything changes, return nonzero. */
3581 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3583 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3584 ep->can_eliminate = 0;
3586 ep->ref_outside_mem = 0;
3588 if (ep->previous_offset != ep->offset)
3589 val = 1;
3592 done:
3593 /* If we changed something, perform elimination in REG_NOTES. This is
3594 needed even when REPLACE is zero because a REG_DEAD note might refer
3595 to a register that we eliminate and could cause a different number
3596 of spill registers to be needed in the final reload pass than in
3597 the pre-passes. */
3598 if (val && REG_NOTES (insn) != 0)
3599 REG_NOTES (insn)
3600 = eliminate_regs_1 (REG_NOTES (insn), VOIDmode, REG_NOTES (insn), true);
3602 return val;
3605 /* Loop through all elimination pairs.
3606 Recalculate the number not at initial offset.
3608 Compute the maximum offset (minimum offset if the stack does not
3609 grow downward) for each elimination pair. */
3611 static void
3612 update_eliminable_offsets (void)
3614 struct elim_table *ep;
3616 num_not_at_initial_offset = 0;
3617 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3619 ep->previous_offset = ep->offset;
3620 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3621 num_not_at_initial_offset++;
3625 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3626 replacement we currently believe is valid, mark it as not eliminable if X
3627 modifies DEST in any way other than by adding a constant integer to it.
3629 If DEST is the frame pointer, we do nothing because we assume that
3630 all assignments to the hard frame pointer are nonlocal gotos and are being
3631 done at a time when they are valid and do not disturb anything else.
3632 Some machines want to eliminate a fake argument pointer with either the
3633 frame or stack pointer. Assignments to the hard frame pointer must not
3634 prevent this elimination.
3636 Called via note_stores from reload before starting its passes to scan
3637 the insns of the function. */
3639 static void
3640 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3642 unsigned int i;
3644 /* A SUBREG of a hard register here is just changing its mode. We should
3645 not see a SUBREG of an eliminable hard register, but check just in
3646 case. */
3647 if (GET_CODE (dest) == SUBREG)
3648 dest = SUBREG_REG (dest);
3650 if (dest == hard_frame_pointer_rtx)
3651 return;
3653 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3654 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3655 && (GET_CODE (x) != SET
3656 || GET_CODE (SET_SRC (x)) != PLUS
3657 || XEXP (SET_SRC (x), 0) != dest
3658 || !CONST_INT_P (XEXP (SET_SRC (x), 1))))
3660 reg_eliminate[i].can_eliminate_previous
3661 = reg_eliminate[i].can_eliminate = 0;
3662 num_eliminable--;
3666 /* Verify that the initial elimination offsets did not change since the
3667 last call to set_initial_elim_offsets. This is used to catch cases
3668 where something illegal happened during reload_as_needed that could
3669 cause incorrect code to be generated if we did not check for it. */
3671 static bool
3672 verify_initial_elim_offsets (void)
3674 HOST_WIDE_INT t;
3676 if (!num_eliminable)
3677 return true;
3679 #ifdef ELIMINABLE_REGS
3681 struct elim_table *ep;
3683 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3685 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3686 if (t != ep->initial_offset)
3687 return false;
3690 #else
3691 INITIAL_FRAME_POINTER_OFFSET (t);
3692 if (t != reg_eliminate[0].initial_offset)
3693 return false;
3694 #endif
3696 return true;
3699 /* Reset all offsets on eliminable registers to their initial values. */
3701 static void
3702 set_initial_elim_offsets (void)
3704 struct elim_table *ep = reg_eliminate;
3706 #ifdef ELIMINABLE_REGS
3707 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3709 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3710 ep->previous_offset = ep->offset = ep->initial_offset;
3712 #else
3713 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3714 ep->previous_offset = ep->offset = ep->initial_offset;
3715 #endif
3717 num_not_at_initial_offset = 0;
3720 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3722 static void
3723 set_initial_eh_label_offset (rtx label)
3725 set_label_offsets (label, NULL_RTX, 1);
3728 /* Initialize the known label offsets.
3729 Set a known offset for each forced label to be at the initial offset
3730 of each elimination. We do this because we assume that all
3731 computed jumps occur from a location where each elimination is
3732 at its initial offset.
3733 For all other labels, show that we don't know the offsets. */
3735 static void
3736 set_initial_label_offsets (void)
3738 rtx x;
3739 memset (offsets_known_at, 0, num_labels);
3741 for (x = forced_labels; x; x = XEXP (x, 1))
3742 if (XEXP (x, 0))
3743 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3745 for_each_eh_label (set_initial_eh_label_offset);
3748 /* Set all elimination offsets to the known values for the code label given
3749 by INSN. */
3751 static void
3752 set_offsets_for_label (rtx insn)
3754 unsigned int i;
3755 int label_nr = CODE_LABEL_NUMBER (insn);
3756 struct elim_table *ep;
3758 num_not_at_initial_offset = 0;
3759 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3761 ep->offset = ep->previous_offset
3762 = offsets_at[label_nr - first_label_num][i];
3763 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3764 num_not_at_initial_offset++;
3768 /* See if anything that happened changes which eliminations are valid.
3769 For example, on the SPARC, whether or not the frame pointer can
3770 be eliminated can depend on what registers have been used. We need
3771 not check some conditions again (such as flag_omit_frame_pointer)
3772 since they can't have changed. */
3774 static void
3775 update_eliminables (HARD_REG_SET *pset)
3777 int previous_frame_pointer_needed = frame_pointer_needed;
3778 struct elim_table *ep;
3780 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3781 if ((ep->from == HARD_FRAME_POINTER_REGNUM
3782 && targetm.frame_pointer_required ())
3783 #ifdef ELIMINABLE_REGS
3784 || ! targetm.can_eliminate (ep->from, ep->to)
3785 #endif
3787 ep->can_eliminate = 0;
3789 /* Look for the case where we have discovered that we can't replace
3790 register A with register B and that means that we will now be
3791 trying to replace register A with register C. This means we can
3792 no longer replace register C with register B and we need to disable
3793 such an elimination, if it exists. This occurs often with A == ap,
3794 B == sp, and C == fp. */
3796 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3798 struct elim_table *op;
3799 int new_to = -1;
3801 if (! ep->can_eliminate && ep->can_eliminate_previous)
3803 /* Find the current elimination for ep->from, if there is a
3804 new one. */
3805 for (op = reg_eliminate;
3806 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3807 if (op->from == ep->from && op->can_eliminate)
3809 new_to = op->to;
3810 break;
3813 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3814 disable it. */
3815 for (op = reg_eliminate;
3816 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3817 if (op->from == new_to && op->to == ep->to)
3818 op->can_eliminate = 0;
3822 /* See if any registers that we thought we could eliminate the previous
3823 time are no longer eliminable. If so, something has changed and we
3824 must spill the register. Also, recompute the number of eliminable
3825 registers and see if the frame pointer is needed; it is if there is
3826 no elimination of the frame pointer that we can perform. */
3828 frame_pointer_needed = 1;
3829 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3831 if (ep->can_eliminate
3832 && ep->from == FRAME_POINTER_REGNUM
3833 && ep->to != HARD_FRAME_POINTER_REGNUM
3834 && (! SUPPORTS_STACK_ALIGNMENT
3835 || ! crtl->stack_realign_needed))
3836 frame_pointer_needed = 0;
3838 if (! ep->can_eliminate && ep->can_eliminate_previous)
3840 ep->can_eliminate_previous = 0;
3841 SET_HARD_REG_BIT (*pset, ep->from);
3842 num_eliminable--;
3846 /* If we didn't need a frame pointer last time, but we do now, spill
3847 the hard frame pointer. */
3848 if (frame_pointer_needed && ! previous_frame_pointer_needed)
3849 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
3852 /* Return true if X is used as the target register of an elimination. */
3854 bool
3855 elimination_target_reg_p (rtx x)
3857 struct elim_table *ep;
3859 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3860 if (ep->to_rtx == x && ep->can_eliminate)
3861 return true;
3863 return false;
3866 /* Initialize the table of registers to eliminate.
3867 Pre-condition: global flag frame_pointer_needed has been set before
3868 calling this function. */
3870 static void
3871 init_elim_table (void)
3873 struct elim_table *ep;
3874 #ifdef ELIMINABLE_REGS
3875 const struct elim_table_1 *ep1;
3876 #endif
3878 if (!reg_eliminate)
3879 reg_eliminate = XCNEWVEC (struct elim_table, NUM_ELIMINABLE_REGS);
3881 num_eliminable = 0;
3883 #ifdef ELIMINABLE_REGS
3884 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
3885 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
3887 ep->from = ep1->from;
3888 ep->to = ep1->to;
3889 ep->can_eliminate = ep->can_eliminate_previous
3890 = (targetm.can_eliminate (ep->from, ep->to)
3891 && ! (ep->to == STACK_POINTER_REGNUM
3892 && frame_pointer_needed
3893 && (! SUPPORTS_STACK_ALIGNMENT
3894 || ! stack_realign_fp)));
3896 #else
3897 reg_eliminate[0].from = reg_eliminate_1[0].from;
3898 reg_eliminate[0].to = reg_eliminate_1[0].to;
3899 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
3900 = ! frame_pointer_needed;
3901 #endif
3903 /* Count the number of eliminable registers and build the FROM and TO
3904 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
3905 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
3906 We depend on this. */
3907 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3909 num_eliminable += ep->can_eliminate;
3910 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
3911 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
3915 /* Kick all pseudos out of hard register REGNO.
3917 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3918 because we found we can't eliminate some register. In the case, no pseudos
3919 are allowed to be in the register, even if they are only in a block that
3920 doesn't require spill registers, unlike the case when we are spilling this
3921 hard reg to produce another spill register.
3923 Return nonzero if any pseudos needed to be kicked out. */
3925 static void
3926 spill_hard_reg (unsigned int regno, int cant_eliminate)
3928 int i;
3930 if (cant_eliminate)
3932 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
3933 df_set_regs_ever_live (regno, true);
3936 /* Spill every pseudo reg that was allocated to this reg
3937 or to something that overlaps this reg. */
3939 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3940 if (reg_renumber[i] >= 0
3941 && (unsigned int) reg_renumber[i] <= regno
3942 && end_hard_regno (PSEUDO_REGNO_MODE (i), reg_renumber[i]) > regno)
3943 SET_REGNO_REG_SET (&spilled_pseudos, i);
3946 /* After find_reload_regs has been run for all insn that need reloads,
3947 and/or spill_hard_regs was called, this function is used to actually
3948 spill pseudo registers and try to reallocate them. It also sets up the
3949 spill_regs array for use by choose_reload_regs. */
3951 static int
3952 finish_spills (int global)
3954 struct insn_chain *chain;
3955 int something_changed = 0;
3956 unsigned i;
3957 reg_set_iterator rsi;
3959 /* Build the spill_regs array for the function. */
3960 /* If there are some registers still to eliminate and one of the spill regs
3961 wasn't ever used before, additional stack space may have to be
3962 allocated to store this register. Thus, we may have changed the offset
3963 between the stack and frame pointers, so mark that something has changed.
3965 One might think that we need only set VAL to 1 if this is a call-used
3966 register. However, the set of registers that must be saved by the
3967 prologue is not identical to the call-used set. For example, the
3968 register used by the call insn for the return PC is a call-used register,
3969 but must be saved by the prologue. */
3971 n_spills = 0;
3972 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3973 if (TEST_HARD_REG_BIT (used_spill_regs, i))
3975 spill_reg_order[i] = n_spills;
3976 spill_regs[n_spills++] = i;
3977 if (num_eliminable && ! df_regs_ever_live_p (i))
3978 something_changed = 1;
3979 df_set_regs_ever_live (i, true);
3981 else
3982 spill_reg_order[i] = -1;
3984 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
3985 if (! ira_conflicts_p || reg_renumber[i] >= 0)
3987 /* Record the current hard register the pseudo is allocated to
3988 in pseudo_previous_regs so we avoid reallocating it to the
3989 same hard reg in a later pass. */
3990 gcc_assert (reg_renumber[i] >= 0);
3992 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
3993 /* Mark it as no longer having a hard register home. */
3994 reg_renumber[i] = -1;
3995 if (ira_conflicts_p)
3996 /* Inform IRA about the change. */
3997 ira_mark_allocation_change (i);
3998 /* We will need to scan everything again. */
3999 something_changed = 1;
4002 /* Retry global register allocation if possible. */
4003 if (global && ira_conflicts_p)
4005 unsigned int n;
4007 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
4008 /* For every insn that needs reloads, set the registers used as spill
4009 regs in pseudo_forbidden_regs for every pseudo live across the
4010 insn. */
4011 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
4013 EXECUTE_IF_SET_IN_REG_SET
4014 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
4016 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4017 chain->used_spill_regs);
4019 EXECUTE_IF_SET_IN_REG_SET
4020 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
4022 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4023 chain->used_spill_regs);
4027 /* Retry allocating the pseudos spilled in IRA and the
4028 reload. For each reg, merge the various reg sets that
4029 indicate which hard regs can't be used, and call
4030 ira_reassign_pseudos. */
4031 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
4032 if (reg_old_renumber[i] != reg_renumber[i])
4034 if (reg_renumber[i] < 0)
4035 temp_pseudo_reg_arr[n++] = i;
4036 else
4037 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
4039 if (ira_reassign_pseudos (temp_pseudo_reg_arr, n,
4040 bad_spill_regs_global,
4041 pseudo_forbidden_regs, pseudo_previous_regs,
4042 &spilled_pseudos))
4043 something_changed = 1;
4045 /* Fix up the register information in the insn chain.
4046 This involves deleting those of the spilled pseudos which did not get
4047 a new hard register home from the live_{before,after} sets. */
4048 for (chain = reload_insn_chain; chain; chain = chain->next)
4050 HARD_REG_SET used_by_pseudos;
4051 HARD_REG_SET used_by_pseudos2;
4053 if (! ira_conflicts_p)
4055 /* Don't do it for IRA because IRA and the reload still can
4056 assign hard registers to the spilled pseudos on next
4057 reload iterations. */
4058 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
4059 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
4061 /* Mark any unallocated hard regs as available for spills. That
4062 makes inheritance work somewhat better. */
4063 if (chain->need_reload)
4065 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
4066 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
4067 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4069 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
4070 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
4071 /* Value of chain->used_spill_regs from previous iteration
4072 may be not included in the value calculated here because
4073 of possible removing caller-saves insns (see function
4074 delete_caller_save_insns. */
4075 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4076 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4080 CLEAR_REG_SET (&changed_allocation_pseudos);
4081 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4082 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
4084 int regno = reg_renumber[i];
4085 if (reg_old_renumber[i] == regno)
4086 continue;
4088 SET_REGNO_REG_SET (&changed_allocation_pseudos, i);
4090 alter_reg (i, reg_old_renumber[i], false);
4091 reg_old_renumber[i] = regno;
4092 if (dump_file)
4094 if (regno == -1)
4095 fprintf (dump_file, " Register %d now on stack.\n\n", i);
4096 else
4097 fprintf (dump_file, " Register %d now in %d.\n\n",
4098 i, reg_renumber[i]);
4102 return something_changed;
4105 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
4107 static void
4108 scan_paradoxical_subregs (rtx x)
4110 int i;
4111 const char *fmt;
4112 enum rtx_code code = GET_CODE (x);
4114 switch (code)
4116 case REG:
4117 case CONST_INT:
4118 case CONST:
4119 case SYMBOL_REF:
4120 case LABEL_REF:
4121 case CONST_DOUBLE:
4122 case CONST_FIXED:
4123 case CONST_VECTOR: /* shouldn't happen, but just in case. */
4124 case CC0:
4125 case PC:
4126 case USE:
4127 case CLOBBER:
4128 return;
4130 case SUBREG:
4131 if (REG_P (SUBREG_REG (x))
4132 && (GET_MODE_SIZE (GET_MODE (x))
4133 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
4135 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4136 = GET_MODE_SIZE (GET_MODE (x));
4137 mark_home_live_1 (REGNO (SUBREG_REG (x)), GET_MODE (x));
4139 return;
4141 default:
4142 break;
4145 fmt = GET_RTX_FORMAT (code);
4146 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4148 if (fmt[i] == 'e')
4149 scan_paradoxical_subregs (XEXP (x, i));
4150 else if (fmt[i] == 'E')
4152 int j;
4153 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4154 scan_paradoxical_subregs (XVECEXP (x, i, j));
4159 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
4160 examine all of the reload insns between PREV and NEXT exclusive, and
4161 annotate all that may trap. */
4163 static void
4164 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
4166 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4167 if (note == NULL)
4168 return;
4169 if (!insn_could_throw_p (insn))
4170 remove_note (insn, note);
4171 copy_reg_eh_region_note_forward (note, NEXT_INSN (prev), next);
4174 /* Reload pseudo-registers into hard regs around each insn as needed.
4175 Additional register load insns are output before the insn that needs it
4176 and perhaps store insns after insns that modify the reloaded pseudo reg.
4178 reg_last_reload_reg and reg_reloaded_contents keep track of
4179 which registers are already available in reload registers.
4180 We update these for the reloads that we perform,
4181 as the insns are scanned. */
4183 static void
4184 reload_as_needed (int live_known)
4186 struct insn_chain *chain;
4187 #if defined (AUTO_INC_DEC)
4188 int i;
4189 #endif
4190 rtx x;
4192 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
4193 memset (spill_reg_store, 0, sizeof spill_reg_store);
4194 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
4195 INIT_REG_SET (&reg_has_output_reload);
4196 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4197 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
4199 set_initial_elim_offsets ();
4201 for (chain = reload_insn_chain; chain; chain = chain->next)
4203 rtx prev = 0;
4204 rtx insn = chain->insn;
4205 rtx old_next = NEXT_INSN (insn);
4206 #ifdef AUTO_INC_DEC
4207 rtx old_prev = PREV_INSN (insn);
4208 #endif
4210 /* If we pass a label, copy the offsets from the label information
4211 into the current offsets of each elimination. */
4212 if (LABEL_P (insn))
4213 set_offsets_for_label (insn);
4215 else if (INSN_P (insn))
4217 regset_head regs_to_forget;
4218 INIT_REG_SET (&regs_to_forget);
4219 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
4221 /* If this is a USE and CLOBBER of a MEM, ensure that any
4222 references to eliminable registers have been removed. */
4224 if ((GET_CODE (PATTERN (insn)) == USE
4225 || GET_CODE (PATTERN (insn)) == CLOBBER)
4226 && MEM_P (XEXP (PATTERN (insn), 0)))
4227 XEXP (XEXP (PATTERN (insn), 0), 0)
4228 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4229 GET_MODE (XEXP (PATTERN (insn), 0)),
4230 NULL_RTX);
4232 /* If we need to do register elimination processing, do so.
4233 This might delete the insn, in which case we are done. */
4234 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4236 eliminate_regs_in_insn (insn, 1);
4237 if (NOTE_P (insn))
4239 update_eliminable_offsets ();
4240 CLEAR_REG_SET (&regs_to_forget);
4241 continue;
4245 /* If need_elim is nonzero but need_reload is zero, one might think
4246 that we could simply set n_reloads to 0. However, find_reloads
4247 could have done some manipulation of the insn (such as swapping
4248 commutative operands), and these manipulations are lost during
4249 the first pass for every insn that needs register elimination.
4250 So the actions of find_reloads must be redone here. */
4252 if (! chain->need_elim && ! chain->need_reload
4253 && ! chain->need_operand_change)
4254 n_reloads = 0;
4255 /* First find the pseudo regs that must be reloaded for this insn.
4256 This info is returned in the tables reload_... (see reload.h).
4257 Also modify the body of INSN by substituting RELOAD
4258 rtx's for those pseudo regs. */
4259 else
4261 CLEAR_REG_SET (&reg_has_output_reload);
4262 CLEAR_HARD_REG_SET (reg_is_output_reload);
4264 find_reloads (insn, 1, spill_indirect_levels, live_known,
4265 spill_reg_order);
4268 if (n_reloads > 0)
4270 rtx next = NEXT_INSN (insn);
4271 rtx p;
4273 prev = PREV_INSN (insn);
4275 /* Now compute which reload regs to reload them into. Perhaps
4276 reusing reload regs from previous insns, or else output
4277 load insns to reload them. Maybe output store insns too.
4278 Record the choices of reload reg in reload_reg_rtx. */
4279 choose_reload_regs (chain);
4281 /* Merge any reloads that we didn't combine for fear of
4282 increasing the number of spill registers needed but now
4283 discover can be safely merged. */
4284 if (SMALL_REGISTER_CLASSES)
4285 merge_assigned_reloads (insn);
4287 /* Generate the insns to reload operands into or out of
4288 their reload regs. */
4289 emit_reload_insns (chain);
4291 /* Substitute the chosen reload regs from reload_reg_rtx
4292 into the insn's body (or perhaps into the bodies of other
4293 load and store insn that we just made for reloading
4294 and that we moved the structure into). */
4295 subst_reloads (insn);
4297 /* Adjust the exception region notes for loads and stores. */
4298 if (flag_non_call_exceptions && !CALL_P (insn))
4299 fixup_eh_region_note (insn, prev, next);
4301 /* If this was an ASM, make sure that all the reload insns
4302 we have generated are valid. If not, give an error
4303 and delete them. */
4304 if (asm_noperands (PATTERN (insn)) >= 0)
4305 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4306 if (p != insn && INSN_P (p)
4307 && GET_CODE (PATTERN (p)) != USE
4308 && (recog_memoized (p) < 0
4309 || (extract_insn (p), ! constrain_operands (1))))
4311 error_for_asm (insn,
4312 "%<asm%> operand requires "
4313 "impossible reload");
4314 delete_insn (p);
4318 if (num_eliminable && chain->need_elim)
4319 update_eliminable_offsets ();
4321 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4322 is no longer validly lying around to save a future reload.
4323 Note that this does not detect pseudos that were reloaded
4324 for this insn in order to be stored in
4325 (obeying register constraints). That is correct; such reload
4326 registers ARE still valid. */
4327 forget_marked_reloads (&regs_to_forget);
4328 CLEAR_REG_SET (&regs_to_forget);
4330 /* There may have been CLOBBER insns placed after INSN. So scan
4331 between INSN and NEXT and use them to forget old reloads. */
4332 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4333 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4334 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4336 #ifdef AUTO_INC_DEC
4337 /* Likewise for regs altered by auto-increment in this insn.
4338 REG_INC notes have been changed by reloading:
4339 find_reloads_address_1 records substitutions for them,
4340 which have been performed by subst_reloads above. */
4341 for (i = n_reloads - 1; i >= 0; i--)
4343 rtx in_reg = rld[i].in_reg;
4344 if (in_reg)
4346 enum rtx_code code = GET_CODE (in_reg);
4347 /* PRE_INC / PRE_DEC will have the reload register ending up
4348 with the same value as the stack slot, but that doesn't
4349 hold true for POST_INC / POST_DEC. Either we have to
4350 convert the memory access to a true POST_INC / POST_DEC,
4351 or we can't use the reload register for inheritance. */
4352 if ((code == POST_INC || code == POST_DEC)
4353 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4354 REGNO (rld[i].reg_rtx))
4355 /* Make sure it is the inc/dec pseudo, and not
4356 some other (e.g. output operand) pseudo. */
4357 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4358 == REGNO (XEXP (in_reg, 0))))
4361 rtx reload_reg = rld[i].reg_rtx;
4362 enum machine_mode mode = GET_MODE (reload_reg);
4363 int n = 0;
4364 rtx p;
4366 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4368 /* We really want to ignore REG_INC notes here, so
4369 use PATTERN (p) as argument to reg_set_p . */
4370 if (reg_set_p (reload_reg, PATTERN (p)))
4371 break;
4372 n = count_occurrences (PATTERN (p), reload_reg, 0);
4373 if (! n)
4374 continue;
4375 if (n == 1)
4377 rtx replace_reg
4378 = gen_rtx_fmt_e (code, mode, reload_reg);
4380 validate_replace_rtx_group (reload_reg,
4381 replace_reg, p);
4382 n = verify_changes (0);
4384 /* We must also verify that the constraints
4385 are met after the replacement. Make sure
4386 extract_insn is only called for an insn
4387 where the replacements were found to be
4388 valid so far. */
4389 if (n)
4391 extract_insn (p);
4392 n = constrain_operands (1);
4395 /* If the constraints were not met, then
4396 undo the replacement, else confirm it. */
4397 if (!n)
4398 cancel_changes (0);
4399 else
4400 confirm_change_group ();
4402 break;
4404 if (n == 1)
4406 add_reg_note (p, REG_INC, reload_reg);
4407 /* Mark this as having an output reload so that the
4408 REG_INC processing code below won't invalidate
4409 the reload for inheritance. */
4410 SET_HARD_REG_BIT (reg_is_output_reload,
4411 REGNO (reload_reg));
4412 SET_REGNO_REG_SET (&reg_has_output_reload,
4413 REGNO (XEXP (in_reg, 0)));
4415 else
4416 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4417 NULL);
4419 else if ((code == PRE_INC || code == PRE_DEC)
4420 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4421 REGNO (rld[i].reg_rtx))
4422 /* Make sure it is the inc/dec pseudo, and not
4423 some other (e.g. output operand) pseudo. */
4424 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4425 == REGNO (XEXP (in_reg, 0))))
4427 SET_HARD_REG_BIT (reg_is_output_reload,
4428 REGNO (rld[i].reg_rtx));
4429 SET_REGNO_REG_SET (&reg_has_output_reload,
4430 REGNO (XEXP (in_reg, 0)));
4432 else if (code == PRE_INC || code == PRE_DEC
4433 || code == POST_INC || code == POST_DEC)
4435 int in_regno = REGNO (XEXP (in_reg, 0));
4437 if (reg_last_reload_reg[in_regno] != NULL_RTX)
4439 int in_hard_regno;
4440 bool forget_p = true;
4442 in_hard_regno = REGNO (reg_last_reload_reg[in_regno]);
4443 if (TEST_HARD_REG_BIT (reg_reloaded_valid,
4444 in_hard_regno))
4446 for (x = old_prev ? NEXT_INSN (old_prev) : insn;
4447 x != old_next;
4448 x = NEXT_INSN (x))
4449 if (x == reg_reloaded_insn[in_hard_regno])
4451 forget_p = false;
4452 break;
4455 /* If for some reasons, we didn't set up
4456 reg_last_reload_reg in this insn,
4457 invalidate inheritance from previous
4458 insns for the incremented/decremented
4459 register. Such registers will be not in
4460 reg_has_output_reload. Invalidate it
4461 also if the corresponding element in
4462 reg_reloaded_insn is also
4463 invalidated. */
4464 if (forget_p)
4465 forget_old_reloads_1 (XEXP (in_reg, 0),
4466 NULL_RTX, NULL);
4471 /* If a pseudo that got a hard register is auto-incremented,
4472 we must purge records of copying it into pseudos without
4473 hard registers. */
4474 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4475 if (REG_NOTE_KIND (x) == REG_INC)
4477 /* See if this pseudo reg was reloaded in this insn.
4478 If so, its last-reload info is still valid
4479 because it is based on this insn's reload. */
4480 for (i = 0; i < n_reloads; i++)
4481 if (rld[i].out == XEXP (x, 0))
4482 break;
4484 if (i == n_reloads)
4485 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4487 #endif
4489 /* A reload reg's contents are unknown after a label. */
4490 if (LABEL_P (insn))
4491 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4493 /* Don't assume a reload reg is still good after a call insn
4494 if it is a call-used reg, or if it contains a value that will
4495 be partially clobbered by the call. */
4496 else if (CALL_P (insn))
4498 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4499 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4503 /* Clean up. */
4504 free (reg_last_reload_reg);
4505 CLEAR_REG_SET (&reg_has_output_reload);
4508 /* Discard all record of any value reloaded from X,
4509 or reloaded in X from someplace else;
4510 unless X is an output reload reg of the current insn.
4512 X may be a hard reg (the reload reg)
4513 or it may be a pseudo reg that was reloaded from.
4515 When DATA is non-NULL just mark the registers in regset
4516 to be forgotten later. */
4518 static void
4519 forget_old_reloads_1 (rtx x, const_rtx ignored ATTRIBUTE_UNUSED,
4520 void *data)
4522 unsigned int regno;
4523 unsigned int nr;
4524 regset regs = (regset) data;
4526 /* note_stores does give us subregs of hard regs,
4527 subreg_regno_offset requires a hard reg. */
4528 while (GET_CODE (x) == SUBREG)
4530 /* We ignore the subreg offset when calculating the regno,
4531 because we are using the entire underlying hard register
4532 below. */
4533 x = SUBREG_REG (x);
4536 if (!REG_P (x))
4537 return;
4539 regno = REGNO (x);
4541 if (regno >= FIRST_PSEUDO_REGISTER)
4542 nr = 1;
4543 else
4545 unsigned int i;
4547 nr = hard_regno_nregs[regno][GET_MODE (x)];
4548 /* Storing into a spilled-reg invalidates its contents.
4549 This can happen if a block-local pseudo is allocated to that reg
4550 and it wasn't spilled because this block's total need is 0.
4551 Then some insn might have an optional reload and use this reg. */
4552 if (!regs)
4553 for (i = 0; i < nr; i++)
4554 /* But don't do this if the reg actually serves as an output
4555 reload reg in the current instruction. */
4556 if (n_reloads == 0
4557 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4559 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4560 spill_reg_store[regno + i] = 0;
4564 if (regs)
4565 while (nr-- > 0)
4566 SET_REGNO_REG_SET (regs, regno + nr);
4567 else
4569 /* Since value of X has changed,
4570 forget any value previously copied from it. */
4572 while (nr-- > 0)
4573 /* But don't forget a copy if this is the output reload
4574 that establishes the copy's validity. */
4575 if (n_reloads == 0
4576 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4577 reg_last_reload_reg[regno + nr] = 0;
4581 /* Forget the reloads marked in regset by previous function. */
4582 static void
4583 forget_marked_reloads (regset regs)
4585 unsigned int reg;
4586 reg_set_iterator rsi;
4587 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4589 if (reg < FIRST_PSEUDO_REGISTER
4590 /* But don't do this if the reg actually serves as an output
4591 reload reg in the current instruction. */
4592 && (n_reloads == 0
4593 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4595 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4596 spill_reg_store[reg] = 0;
4598 if (n_reloads == 0
4599 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4600 reg_last_reload_reg[reg] = 0;
4604 /* The following HARD_REG_SETs indicate when each hard register is
4605 used for a reload of various parts of the current insn. */
4607 /* If reg is unavailable for all reloads. */
4608 static HARD_REG_SET reload_reg_unavailable;
4609 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4610 static HARD_REG_SET reload_reg_used;
4611 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4612 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4613 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4614 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4615 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4616 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4617 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
4618 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
4619 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
4620 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
4621 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
4622 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
4623 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
4624 static HARD_REG_SET reload_reg_used_in_op_addr;
4625 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
4626 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
4627 /* If reg is in use for a RELOAD_FOR_INSN reload. */
4628 static HARD_REG_SET reload_reg_used_in_insn;
4629 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
4630 static HARD_REG_SET reload_reg_used_in_other_addr;
4632 /* If reg is in use as a reload reg for any sort of reload. */
4633 static HARD_REG_SET reload_reg_used_at_all;
4635 /* If reg is use as an inherited reload. We just mark the first register
4636 in the group. */
4637 static HARD_REG_SET reload_reg_used_for_inherit;
4639 /* Records which hard regs are used in any way, either as explicit use or
4640 by being allocated to a pseudo during any point of the current insn. */
4641 static HARD_REG_SET reg_used_in_insn;
4643 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
4644 TYPE. MODE is used to indicate how many consecutive regs are
4645 actually used. */
4647 static void
4648 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
4649 enum machine_mode mode)
4651 unsigned int nregs = hard_regno_nregs[regno][mode];
4652 unsigned int i;
4654 for (i = regno; i < nregs + regno; i++)
4656 switch (type)
4658 case RELOAD_OTHER:
4659 SET_HARD_REG_BIT (reload_reg_used, i);
4660 break;
4662 case RELOAD_FOR_INPUT_ADDRESS:
4663 SET_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], i);
4664 break;
4666 case RELOAD_FOR_INPADDR_ADDRESS:
4667 SET_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], i);
4668 break;
4670 case RELOAD_FOR_OUTPUT_ADDRESS:
4671 SET_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], i);
4672 break;
4674 case RELOAD_FOR_OUTADDR_ADDRESS:
4675 SET_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], i);
4676 break;
4678 case RELOAD_FOR_OPERAND_ADDRESS:
4679 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
4680 break;
4682 case RELOAD_FOR_OPADDR_ADDR:
4683 SET_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, i);
4684 break;
4686 case RELOAD_FOR_OTHER_ADDRESS:
4687 SET_HARD_REG_BIT (reload_reg_used_in_other_addr, i);
4688 break;
4690 case RELOAD_FOR_INPUT:
4691 SET_HARD_REG_BIT (reload_reg_used_in_input[opnum], i);
4692 break;
4694 case RELOAD_FOR_OUTPUT:
4695 SET_HARD_REG_BIT (reload_reg_used_in_output[opnum], i);
4696 break;
4698 case RELOAD_FOR_INSN:
4699 SET_HARD_REG_BIT (reload_reg_used_in_insn, i);
4700 break;
4703 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
4707 /* Similarly, but show REGNO is no longer in use for a reload. */
4709 static void
4710 clear_reload_reg_in_use (unsigned int regno, int opnum,
4711 enum reload_type type, enum machine_mode mode)
4713 unsigned int nregs = hard_regno_nregs[regno][mode];
4714 unsigned int start_regno, end_regno, r;
4715 int i;
4716 /* A complication is that for some reload types, inheritance might
4717 allow multiple reloads of the same types to share a reload register.
4718 We set check_opnum if we have to check only reloads with the same
4719 operand number, and check_any if we have to check all reloads. */
4720 int check_opnum = 0;
4721 int check_any = 0;
4722 HARD_REG_SET *used_in_set;
4724 switch (type)
4726 case RELOAD_OTHER:
4727 used_in_set = &reload_reg_used;
4728 break;
4730 case RELOAD_FOR_INPUT_ADDRESS:
4731 used_in_set = &reload_reg_used_in_input_addr[opnum];
4732 break;
4734 case RELOAD_FOR_INPADDR_ADDRESS:
4735 check_opnum = 1;
4736 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
4737 break;
4739 case RELOAD_FOR_OUTPUT_ADDRESS:
4740 used_in_set = &reload_reg_used_in_output_addr[opnum];
4741 break;
4743 case RELOAD_FOR_OUTADDR_ADDRESS:
4744 check_opnum = 1;
4745 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
4746 break;
4748 case RELOAD_FOR_OPERAND_ADDRESS:
4749 used_in_set = &reload_reg_used_in_op_addr;
4750 break;
4752 case RELOAD_FOR_OPADDR_ADDR:
4753 check_any = 1;
4754 used_in_set = &reload_reg_used_in_op_addr_reload;
4755 break;
4757 case RELOAD_FOR_OTHER_ADDRESS:
4758 used_in_set = &reload_reg_used_in_other_addr;
4759 check_any = 1;
4760 break;
4762 case RELOAD_FOR_INPUT:
4763 used_in_set = &reload_reg_used_in_input[opnum];
4764 break;
4766 case RELOAD_FOR_OUTPUT:
4767 used_in_set = &reload_reg_used_in_output[opnum];
4768 break;
4770 case RELOAD_FOR_INSN:
4771 used_in_set = &reload_reg_used_in_insn;
4772 break;
4773 default:
4774 gcc_unreachable ();
4776 /* We resolve conflicts with remaining reloads of the same type by
4777 excluding the intervals of reload registers by them from the
4778 interval of freed reload registers. Since we only keep track of
4779 one set of interval bounds, we might have to exclude somewhat
4780 more than what would be necessary if we used a HARD_REG_SET here.
4781 But this should only happen very infrequently, so there should
4782 be no reason to worry about it. */
4784 start_regno = regno;
4785 end_regno = regno + nregs;
4786 if (check_opnum || check_any)
4788 for (i = n_reloads - 1; i >= 0; i--)
4790 if (rld[i].when_needed == type
4791 && (check_any || rld[i].opnum == opnum)
4792 && rld[i].reg_rtx)
4794 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
4795 unsigned int conflict_end
4796 = end_hard_regno (rld[i].mode, conflict_start);
4798 /* If there is an overlap with the first to-be-freed register,
4799 adjust the interval start. */
4800 if (conflict_start <= start_regno && conflict_end > start_regno)
4801 start_regno = conflict_end;
4802 /* Otherwise, if there is a conflict with one of the other
4803 to-be-freed registers, adjust the interval end. */
4804 if (conflict_start > start_regno && conflict_start < end_regno)
4805 end_regno = conflict_start;
4810 for (r = start_regno; r < end_regno; r++)
4811 CLEAR_HARD_REG_BIT (*used_in_set, r);
4814 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
4815 specified by OPNUM and TYPE. */
4817 static int
4818 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
4820 int i;
4822 /* In use for a RELOAD_OTHER means it's not available for anything. */
4823 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
4824 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
4825 return 0;
4827 switch (type)
4829 case RELOAD_OTHER:
4830 /* In use for anything means we can't use it for RELOAD_OTHER. */
4831 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
4832 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
4833 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
4834 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4835 return 0;
4837 for (i = 0; i < reload_n_operands; i++)
4838 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4839 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
4840 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4841 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
4842 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4843 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4844 return 0;
4846 return 1;
4848 case RELOAD_FOR_INPUT:
4849 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4850 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
4851 return 0;
4853 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
4854 return 0;
4856 /* If it is used for some other input, can't use it. */
4857 for (i = 0; i < reload_n_operands; i++)
4858 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4859 return 0;
4861 /* If it is used in a later operand's address, can't use it. */
4862 for (i = opnum + 1; i < reload_n_operands; i++)
4863 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
4864 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
4865 return 0;
4867 return 1;
4869 case RELOAD_FOR_INPUT_ADDRESS:
4870 /* Can't use a register if it is used for an input address for this
4871 operand or used as an input in an earlier one. */
4872 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
4873 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4874 return 0;
4876 for (i = 0; i < opnum; i++)
4877 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4878 return 0;
4880 return 1;
4882 case RELOAD_FOR_INPADDR_ADDRESS:
4883 /* Can't use a register if it is used for an input address
4884 for this operand or used as an input in an earlier
4885 one. */
4886 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
4887 return 0;
4889 for (i = 0; i < opnum; i++)
4890 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4891 return 0;
4893 return 1;
4895 case RELOAD_FOR_OUTPUT_ADDRESS:
4896 /* Can't use a register if it is used for an output address for this
4897 operand or used as an output in this or a later operand. Note
4898 that multiple output operands are emitted in reverse order, so
4899 the conflicting ones are those with lower indices. */
4900 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
4901 return 0;
4903 for (i = 0; i <= opnum; i++)
4904 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4905 return 0;
4907 return 1;
4909 case RELOAD_FOR_OUTADDR_ADDRESS:
4910 /* Can't use a register if it is used for an output address
4911 for this operand or used as an output in this or a
4912 later operand. Note that multiple output operands are
4913 emitted in reverse order, so the conflicting ones are
4914 those with lower indices. */
4915 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
4916 return 0;
4918 for (i = 0; i <= opnum; i++)
4919 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4920 return 0;
4922 return 1;
4924 case RELOAD_FOR_OPERAND_ADDRESS:
4925 for (i = 0; i < reload_n_operands; i++)
4926 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4927 return 0;
4929 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4930 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4932 case RELOAD_FOR_OPADDR_ADDR:
4933 for (i = 0; i < reload_n_operands; i++)
4934 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
4935 return 0;
4937 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
4939 case RELOAD_FOR_OUTPUT:
4940 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
4941 outputs, or an operand address for this or an earlier output.
4942 Note that multiple output operands are emitted in reverse order,
4943 so the conflicting ones are those with higher indices. */
4944 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
4945 return 0;
4947 for (i = 0; i < reload_n_operands; i++)
4948 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4949 return 0;
4951 for (i = opnum; i < reload_n_operands; i++)
4952 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
4953 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
4954 return 0;
4956 return 1;
4958 case RELOAD_FOR_INSN:
4959 for (i = 0; i < reload_n_operands; i++)
4960 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
4961 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
4962 return 0;
4964 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
4965 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
4967 case RELOAD_FOR_OTHER_ADDRESS:
4968 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
4970 default:
4971 gcc_unreachable ();
4975 /* Return 1 if the value in reload reg REGNO, as used by a reload
4976 needed for the part of the insn specified by OPNUM and TYPE,
4977 is still available in REGNO at the end of the insn.
4979 We can assume that the reload reg was already tested for availability
4980 at the time it is needed, and we should not check this again,
4981 in case the reg has already been marked in use. */
4983 static int
4984 reload_reg_reaches_end_p (unsigned int regno, int opnum, enum reload_type type)
4986 int i;
4988 switch (type)
4990 case RELOAD_OTHER:
4991 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
4992 its value must reach the end. */
4993 return 1;
4995 /* If this use is for part of the insn,
4996 its value reaches if no subsequent part uses the same register.
4997 Just like the above function, don't try to do this with lots
4998 of fallthroughs. */
5000 case RELOAD_FOR_OTHER_ADDRESS:
5001 /* Here we check for everything else, since these don't conflict
5002 with anything else and everything comes later. */
5004 for (i = 0; i < reload_n_operands; i++)
5005 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5006 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5007 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5008 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5009 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5010 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5011 return 0;
5013 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5014 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
5015 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5016 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5018 case RELOAD_FOR_INPUT_ADDRESS:
5019 case RELOAD_FOR_INPADDR_ADDRESS:
5020 /* Similar, except that we check only for this and subsequent inputs
5021 and the address of only subsequent inputs and we do not need
5022 to check for RELOAD_OTHER objects since they are known not to
5023 conflict. */
5025 for (i = opnum; i < reload_n_operands; i++)
5026 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5027 return 0;
5029 for (i = opnum + 1; i < reload_n_operands; i++)
5030 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5031 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5032 return 0;
5034 for (i = 0; i < reload_n_operands; i++)
5035 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5036 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5037 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5038 return 0;
5040 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5041 return 0;
5043 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5044 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5045 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5047 case RELOAD_FOR_INPUT:
5048 /* Similar to input address, except we start at the next operand for
5049 both input and input address and we do not check for
5050 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5051 would conflict. */
5053 for (i = opnum + 1; i < reload_n_operands; i++)
5054 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5055 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5056 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5057 return 0;
5059 /* ... fall through ... */
5061 case RELOAD_FOR_OPERAND_ADDRESS:
5062 /* Check outputs and their addresses. */
5064 for (i = 0; i < reload_n_operands; i++)
5065 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5066 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5067 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5068 return 0;
5070 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
5072 case RELOAD_FOR_OPADDR_ADDR:
5073 for (i = 0; i < reload_n_operands; i++)
5074 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5075 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5076 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5077 return 0;
5079 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5080 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5081 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5083 case RELOAD_FOR_INSN:
5084 /* These conflict with other outputs with RELOAD_OTHER. So
5085 we need only check for output addresses. */
5087 opnum = reload_n_operands;
5089 /* ... fall through ... */
5091 case RELOAD_FOR_OUTPUT:
5092 case RELOAD_FOR_OUTPUT_ADDRESS:
5093 case RELOAD_FOR_OUTADDR_ADDRESS:
5094 /* We already know these can't conflict with a later output. So the
5095 only thing to check are later output addresses.
5096 Note that multiple output operands are emitted in reverse order,
5097 so the conflicting ones are those with lower indices. */
5098 for (i = 0; i < opnum; i++)
5099 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5100 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5101 return 0;
5103 return 1;
5105 default:
5106 gcc_unreachable ();
5110 /* Like reload_reg_reaches_end_p, but check that the condition holds for
5111 every register in the range [REGNO, REGNO + NREGS). */
5113 static bool
5114 reload_regs_reach_end_p (unsigned int regno, int nregs,
5115 int opnum, enum reload_type type)
5117 int i;
5119 for (i = 0; i < nregs; i++)
5120 if (!reload_reg_reaches_end_p (regno + i, opnum, type))
5121 return false;
5122 return true;
5126 /* Returns whether R1 and R2 are uniquely chained: the value of one
5127 is used by the other, and that value is not used by any other
5128 reload for this insn. This is used to partially undo the decision
5129 made in find_reloads when in the case of multiple
5130 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
5131 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
5132 reloads. This code tries to avoid the conflict created by that
5133 change. It might be cleaner to explicitly keep track of which
5134 RELOAD_FOR_OPADDR_ADDR reload is associated with which
5135 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
5136 this after the fact. */
5137 static bool
5138 reloads_unique_chain_p (int r1, int r2)
5140 int i;
5142 /* We only check input reloads. */
5143 if (! rld[r1].in || ! rld[r2].in)
5144 return false;
5146 /* Avoid anything with output reloads. */
5147 if (rld[r1].out || rld[r2].out)
5148 return false;
5150 /* "chained" means one reload is a component of the other reload,
5151 not the same as the other reload. */
5152 if (rld[r1].opnum != rld[r2].opnum
5153 || rtx_equal_p (rld[r1].in, rld[r2].in)
5154 || rld[r1].optional || rld[r2].optional
5155 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
5156 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
5157 return false;
5159 for (i = 0; i < n_reloads; i ++)
5160 /* Look for input reloads that aren't our two */
5161 if (i != r1 && i != r2 && rld[i].in)
5163 /* If our reload is mentioned at all, it isn't a simple chain. */
5164 if (reg_mentioned_p (rld[r1].in, rld[i].in))
5165 return false;
5167 return true;
5170 /* The recursive function change all occurrences of WHAT in *WHERE
5171 to REPL. */
5172 static void
5173 substitute (rtx *where, const_rtx what, rtx repl)
5175 const char *fmt;
5176 int i;
5177 enum rtx_code code;
5179 if (*where == 0)
5180 return;
5182 if (*where == what || rtx_equal_p (*where, what))
5184 /* Record the location of the changed rtx. */
5185 VEC_safe_push (rtx_p, heap, substitute_stack, where);
5186 *where = repl;
5187 return;
5190 code = GET_CODE (*where);
5191 fmt = GET_RTX_FORMAT (code);
5192 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5194 if (fmt[i] == 'E')
5196 int j;
5198 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
5199 substitute (&XVECEXP (*where, i, j), what, repl);
5201 else if (fmt[i] == 'e')
5202 substitute (&XEXP (*where, i), what, repl);
5206 /* The function returns TRUE if chain of reload R1 and R2 (in any
5207 order) can be evaluated without usage of intermediate register for
5208 the reload containing another reload. It is important to see
5209 gen_reload to understand what the function is trying to do. As an
5210 example, let us have reload chain
5212 r2: const
5213 r1: <something> + const
5215 and reload R2 got reload reg HR. The function returns true if
5216 there is a correct insn HR = HR + <something>. Otherwise,
5217 gen_reload will use intermediate register (and this is the reload
5218 reg for R1) to reload <something>.
5220 We need this function to find a conflict for chain reloads. In our
5221 example, if HR = HR + <something> is incorrect insn, then we cannot
5222 use HR as a reload register for R2. If we do use it then we get a
5223 wrong code:
5225 HR = const
5226 HR = <something>
5227 HR = HR + HR
5230 static bool
5231 gen_reload_chain_without_interm_reg_p (int r1, int r2)
5233 /* Assume other cases in gen_reload are not possible for
5234 chain reloads or do need an intermediate hard registers. */
5235 bool result = true;
5236 int regno, n, code;
5237 rtx out, in, tem, insn;
5238 rtx last = get_last_insn ();
5240 /* Make r2 a component of r1. */
5241 if (reg_mentioned_p (rld[r1].in, rld[r2].in))
5243 n = r1;
5244 r1 = r2;
5245 r2 = n;
5247 gcc_assert (reg_mentioned_p (rld[r2].in, rld[r1].in));
5248 regno = rld[r1].regno >= 0 ? rld[r1].regno : rld[r2].regno;
5249 gcc_assert (regno >= 0);
5250 out = gen_rtx_REG (rld[r1].mode, regno);
5251 in = rld[r1].in;
5252 substitute (&in, rld[r2].in, gen_rtx_REG (rld[r2].mode, regno));
5254 /* If IN is a paradoxical SUBREG, remove it and try to put the
5255 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
5256 if (GET_CODE (in) == SUBREG
5257 && (GET_MODE_SIZE (GET_MODE (in))
5258 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
5259 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
5260 in = SUBREG_REG (in), out = tem;
5262 if (GET_CODE (in) == PLUS
5263 && (REG_P (XEXP (in, 0))
5264 || GET_CODE (XEXP (in, 0)) == SUBREG
5265 || MEM_P (XEXP (in, 0)))
5266 && (REG_P (XEXP (in, 1))
5267 || GET_CODE (XEXP (in, 1)) == SUBREG
5268 || CONSTANT_P (XEXP (in, 1))
5269 || MEM_P (XEXP (in, 1))))
5271 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
5272 code = recog_memoized (insn);
5273 result = false;
5275 if (code >= 0)
5277 extract_insn (insn);
5278 /* We want constrain operands to treat this insn strictly in
5279 its validity determination, i.e., the way it would after
5280 reload has completed. */
5281 result = constrain_operands (1);
5284 delete_insns_since (last);
5287 /* Restore the original value at each changed address within R1. */
5288 while (!VEC_empty (rtx_p, substitute_stack))
5290 rtx *where = VEC_pop (rtx_p, substitute_stack);
5291 *where = rld[r2].in;
5294 return result;
5297 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5298 Return 0 otherwise.
5300 This function uses the same algorithm as reload_reg_free_p above. */
5302 static int
5303 reloads_conflict (int r1, int r2)
5305 enum reload_type r1_type = rld[r1].when_needed;
5306 enum reload_type r2_type = rld[r2].when_needed;
5307 int r1_opnum = rld[r1].opnum;
5308 int r2_opnum = rld[r2].opnum;
5310 /* RELOAD_OTHER conflicts with everything. */
5311 if (r2_type == RELOAD_OTHER)
5312 return 1;
5314 /* Otherwise, check conflicts differently for each type. */
5316 switch (r1_type)
5318 case RELOAD_FOR_INPUT:
5319 return (r2_type == RELOAD_FOR_INSN
5320 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5321 || r2_type == RELOAD_FOR_OPADDR_ADDR
5322 || r2_type == RELOAD_FOR_INPUT
5323 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5324 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5325 && r2_opnum > r1_opnum));
5327 case RELOAD_FOR_INPUT_ADDRESS:
5328 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5329 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5331 case RELOAD_FOR_INPADDR_ADDRESS:
5332 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5333 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5335 case RELOAD_FOR_OUTPUT_ADDRESS:
5336 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5337 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5339 case RELOAD_FOR_OUTADDR_ADDRESS:
5340 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5341 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5343 case RELOAD_FOR_OPERAND_ADDRESS:
5344 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5345 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
5346 && (!reloads_unique_chain_p (r1, r2)
5347 || !gen_reload_chain_without_interm_reg_p (r1, r2))));
5349 case RELOAD_FOR_OPADDR_ADDR:
5350 return (r2_type == RELOAD_FOR_INPUT
5351 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5353 case RELOAD_FOR_OUTPUT:
5354 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5355 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5356 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5357 && r2_opnum >= r1_opnum));
5359 case RELOAD_FOR_INSN:
5360 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5361 || r2_type == RELOAD_FOR_INSN
5362 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5364 case RELOAD_FOR_OTHER_ADDRESS:
5365 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5367 case RELOAD_OTHER:
5368 return 1;
5370 default:
5371 gcc_unreachable ();
5375 /* Indexed by reload number, 1 if incoming value
5376 inherited from previous insns. */
5377 static char reload_inherited[MAX_RELOADS];
5379 /* For an inherited reload, this is the insn the reload was inherited from,
5380 if we know it. Otherwise, this is 0. */
5381 static rtx reload_inheritance_insn[MAX_RELOADS];
5383 /* If nonzero, this is a place to get the value of the reload,
5384 rather than using reload_in. */
5385 static rtx reload_override_in[MAX_RELOADS];
5387 /* For each reload, the hard register number of the register used,
5388 or -1 if we did not need a register for this reload. */
5389 static int reload_spill_index[MAX_RELOADS];
5391 /* Index X is the value of rld[X].reg_rtx, adjusted for the input mode. */
5392 static rtx reload_reg_rtx_for_input[MAX_RELOADS];
5394 /* Index X is the value of rld[X].reg_rtx, adjusted for the output mode. */
5395 static rtx reload_reg_rtx_for_output[MAX_RELOADS];
5397 /* Subroutine of free_for_value_p, used to check a single register.
5398 START_REGNO is the starting regno of the full reload register
5399 (possibly comprising multiple hard registers) that we are considering. */
5401 static int
5402 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
5403 enum reload_type type, rtx value, rtx out,
5404 int reloadnum, int ignore_address_reloads)
5406 int time1;
5407 /* Set if we see an input reload that must not share its reload register
5408 with any new earlyclobber, but might otherwise share the reload
5409 register with an output or input-output reload. */
5410 int check_earlyclobber = 0;
5411 int i;
5412 int copy = 0;
5414 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5415 return 0;
5417 if (out == const0_rtx)
5419 copy = 1;
5420 out = NULL_RTX;
5423 /* We use some pseudo 'time' value to check if the lifetimes of the
5424 new register use would overlap with the one of a previous reload
5425 that is not read-only or uses a different value.
5426 The 'time' used doesn't have to be linear in any shape or form, just
5427 monotonic.
5428 Some reload types use different 'buckets' for each operand.
5429 So there are MAX_RECOG_OPERANDS different time values for each
5430 such reload type.
5431 We compute TIME1 as the time when the register for the prospective
5432 new reload ceases to be live, and TIME2 for each existing
5433 reload as the time when that the reload register of that reload
5434 becomes live.
5435 Where there is little to be gained by exact lifetime calculations,
5436 we just make conservative assumptions, i.e. a longer lifetime;
5437 this is done in the 'default:' cases. */
5438 switch (type)
5440 case RELOAD_FOR_OTHER_ADDRESS:
5441 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
5442 time1 = copy ? 0 : 1;
5443 break;
5444 case RELOAD_OTHER:
5445 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5446 break;
5447 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5448 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5449 respectively, to the time values for these, we get distinct time
5450 values. To get distinct time values for each operand, we have to
5451 multiply opnum by at least three. We round that up to four because
5452 multiply by four is often cheaper. */
5453 case RELOAD_FOR_INPADDR_ADDRESS:
5454 time1 = opnum * 4 + 2;
5455 break;
5456 case RELOAD_FOR_INPUT_ADDRESS:
5457 time1 = opnum * 4 + 3;
5458 break;
5459 case RELOAD_FOR_INPUT:
5460 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5461 executes (inclusive). */
5462 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5463 break;
5464 case RELOAD_FOR_OPADDR_ADDR:
5465 /* opnum * 4 + 4
5466 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5467 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5468 break;
5469 case RELOAD_FOR_OPERAND_ADDRESS:
5470 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5471 is executed. */
5472 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5473 break;
5474 case RELOAD_FOR_OUTADDR_ADDRESS:
5475 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5476 break;
5477 case RELOAD_FOR_OUTPUT_ADDRESS:
5478 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5479 break;
5480 default:
5481 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5484 for (i = 0; i < n_reloads; i++)
5486 rtx reg = rld[i].reg_rtx;
5487 if (reg && REG_P (reg)
5488 && ((unsigned) regno - true_regnum (reg)
5489 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5490 && i != reloadnum)
5492 rtx other_input = rld[i].in;
5494 /* If the other reload loads the same input value, that
5495 will not cause a conflict only if it's loading it into
5496 the same register. */
5497 if (true_regnum (reg) != start_regno)
5498 other_input = NULL_RTX;
5499 if (! other_input || ! rtx_equal_p (other_input, value)
5500 || rld[i].out || out)
5502 int time2;
5503 switch (rld[i].when_needed)
5505 case RELOAD_FOR_OTHER_ADDRESS:
5506 time2 = 0;
5507 break;
5508 case RELOAD_FOR_INPADDR_ADDRESS:
5509 /* find_reloads makes sure that a
5510 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5511 by at most one - the first -
5512 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5513 address reload is inherited, the address address reload
5514 goes away, so we can ignore this conflict. */
5515 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5516 && ignore_address_reloads
5517 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5518 Then the address address is still needed to store
5519 back the new address. */
5520 && ! rld[reloadnum].out)
5521 continue;
5522 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5523 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5524 reloads go away. */
5525 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5526 && ignore_address_reloads
5527 /* Unless we are reloading an auto_inc expression. */
5528 && ! rld[reloadnum].out)
5529 continue;
5530 time2 = rld[i].opnum * 4 + 2;
5531 break;
5532 case RELOAD_FOR_INPUT_ADDRESS:
5533 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5534 && ignore_address_reloads
5535 && ! rld[reloadnum].out)
5536 continue;
5537 time2 = rld[i].opnum * 4 + 3;
5538 break;
5539 case RELOAD_FOR_INPUT:
5540 time2 = rld[i].opnum * 4 + 4;
5541 check_earlyclobber = 1;
5542 break;
5543 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5544 == MAX_RECOG_OPERAND * 4 */
5545 case RELOAD_FOR_OPADDR_ADDR:
5546 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5547 && ignore_address_reloads
5548 && ! rld[reloadnum].out)
5549 continue;
5550 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5551 break;
5552 case RELOAD_FOR_OPERAND_ADDRESS:
5553 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5554 check_earlyclobber = 1;
5555 break;
5556 case RELOAD_FOR_INSN:
5557 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5558 break;
5559 case RELOAD_FOR_OUTPUT:
5560 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5561 instruction is executed. */
5562 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5563 break;
5564 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5565 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5566 value. */
5567 case RELOAD_FOR_OUTADDR_ADDRESS:
5568 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5569 && ignore_address_reloads
5570 && ! rld[reloadnum].out)
5571 continue;
5572 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5573 break;
5574 case RELOAD_FOR_OUTPUT_ADDRESS:
5575 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5576 break;
5577 case RELOAD_OTHER:
5578 /* If there is no conflict in the input part, handle this
5579 like an output reload. */
5580 if (! rld[i].in || rtx_equal_p (other_input, value))
5582 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5583 /* Earlyclobbered outputs must conflict with inputs. */
5584 if (earlyclobber_operand_p (rld[i].out))
5585 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5587 break;
5589 time2 = 1;
5590 /* RELOAD_OTHER might be live beyond instruction execution,
5591 but this is not obvious when we set time2 = 1. So check
5592 here if there might be a problem with the new reload
5593 clobbering the register used by the RELOAD_OTHER. */
5594 if (out)
5595 return 0;
5596 break;
5597 default:
5598 return 0;
5600 if ((time1 >= time2
5601 && (! rld[i].in || rld[i].out
5602 || ! rtx_equal_p (other_input, value)))
5603 || (out && rld[reloadnum].out_reg
5604 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
5605 return 0;
5610 /* Earlyclobbered outputs must conflict with inputs. */
5611 if (check_earlyclobber && out && earlyclobber_operand_p (out))
5612 return 0;
5614 return 1;
5617 /* Return 1 if the value in reload reg REGNO, as used by a reload
5618 needed for the part of the insn specified by OPNUM and TYPE,
5619 may be used to load VALUE into it.
5621 MODE is the mode in which the register is used, this is needed to
5622 determine how many hard regs to test.
5624 Other read-only reloads with the same value do not conflict
5625 unless OUT is nonzero and these other reloads have to live while
5626 output reloads live.
5627 If OUT is CONST0_RTX, this is a special case: it means that the
5628 test should not be for using register REGNO as reload register, but
5629 for copying from register REGNO into the reload register.
5631 RELOADNUM is the number of the reload we want to load this value for;
5632 a reload does not conflict with itself.
5634 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
5635 reloads that load an address for the very reload we are considering.
5637 The caller has to make sure that there is no conflict with the return
5638 register. */
5640 static int
5641 free_for_value_p (int regno, enum machine_mode mode, int opnum,
5642 enum reload_type type, rtx value, rtx out, int reloadnum,
5643 int ignore_address_reloads)
5645 int nregs = hard_regno_nregs[regno][mode];
5646 while (nregs-- > 0)
5647 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
5648 value, out, reloadnum,
5649 ignore_address_reloads))
5650 return 0;
5651 return 1;
5654 /* Return nonzero if the rtx X is invariant over the current function. */
5655 /* ??? Actually, the places where we use this expect exactly what is
5656 tested here, and not everything that is function invariant. In
5657 particular, the frame pointer and arg pointer are special cased;
5658 pic_offset_table_rtx is not, and we must not spill these things to
5659 memory. */
5662 function_invariant_p (const_rtx x)
5664 if (CONSTANT_P (x))
5665 return 1;
5666 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
5667 return 1;
5668 if (GET_CODE (x) == PLUS
5669 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
5670 && CONSTANT_P (XEXP (x, 1)))
5671 return 1;
5672 return 0;
5675 /* Determine whether the reload reg X overlaps any rtx'es used for
5676 overriding inheritance. Return nonzero if so. */
5678 static int
5679 conflicts_with_override (rtx x)
5681 int i;
5682 for (i = 0; i < n_reloads; i++)
5683 if (reload_override_in[i]
5684 && reg_overlap_mentioned_p (x, reload_override_in[i]))
5685 return 1;
5686 return 0;
5689 /* Give an error message saying we failed to find a reload for INSN,
5690 and clear out reload R. */
5691 static void
5692 failed_reload (rtx insn, int r)
5694 if (asm_noperands (PATTERN (insn)) < 0)
5695 /* It's the compiler's fault. */
5696 fatal_insn ("could not find a spill register", insn);
5698 /* It's the user's fault; the operand's mode and constraint
5699 don't match. Disable this reload so we don't crash in final. */
5700 error_for_asm (insn,
5701 "%<asm%> operand constraint incompatible with operand size");
5702 rld[r].in = 0;
5703 rld[r].out = 0;
5704 rld[r].reg_rtx = 0;
5705 rld[r].optional = 1;
5706 rld[r].secondary_p = 1;
5709 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
5710 for reload R. If it's valid, get an rtx for it. Return nonzero if
5711 successful. */
5712 static int
5713 set_reload_reg (int i, int r)
5715 int regno;
5716 rtx reg = spill_reg_rtx[i];
5718 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
5719 spill_reg_rtx[i] = reg
5720 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
5722 regno = true_regnum (reg);
5724 /* Detect when the reload reg can't hold the reload mode.
5725 This used to be one `if', but Sequent compiler can't handle that. */
5726 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
5728 enum machine_mode test_mode = VOIDmode;
5729 if (rld[r].in)
5730 test_mode = GET_MODE (rld[r].in);
5731 /* If rld[r].in has VOIDmode, it means we will load it
5732 in whatever mode the reload reg has: to wit, rld[r].mode.
5733 We have already tested that for validity. */
5734 /* Aside from that, we need to test that the expressions
5735 to reload from or into have modes which are valid for this
5736 reload register. Otherwise the reload insns would be invalid. */
5737 if (! (rld[r].in != 0 && test_mode != VOIDmode
5738 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
5739 if (! (rld[r].out != 0
5740 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
5742 /* The reg is OK. */
5743 last_spill_reg = i;
5745 /* Mark as in use for this insn the reload regs we use
5746 for this. */
5747 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
5748 rld[r].when_needed, rld[r].mode);
5750 rld[r].reg_rtx = reg;
5751 reload_spill_index[r] = spill_regs[i];
5752 return 1;
5755 return 0;
5758 /* Find a spill register to use as a reload register for reload R.
5759 LAST_RELOAD is nonzero if this is the last reload for the insn being
5760 processed.
5762 Set rld[R].reg_rtx to the register allocated.
5764 We return 1 if successful, or 0 if we couldn't find a spill reg and
5765 we didn't change anything. */
5767 static int
5768 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
5769 int last_reload)
5771 int i, pass, count;
5773 /* If we put this reload ahead, thinking it is a group,
5774 then insist on finding a group. Otherwise we can grab a
5775 reg that some other reload needs.
5776 (That can happen when we have a 68000 DATA_OR_FP_REG
5777 which is a group of data regs or one fp reg.)
5778 We need not be so restrictive if there are no more reloads
5779 for this insn.
5781 ??? Really it would be nicer to have smarter handling
5782 for that kind of reg class, where a problem like this is normal.
5783 Perhaps those classes should be avoided for reloading
5784 by use of more alternatives. */
5786 int force_group = rld[r].nregs > 1 && ! last_reload;
5788 /* If we want a single register and haven't yet found one,
5789 take any reg in the right class and not in use.
5790 If we want a consecutive group, here is where we look for it.
5792 We use two passes so we can first look for reload regs to
5793 reuse, which are already in use for other reloads in this insn,
5794 and only then use additional registers.
5795 I think that maximizing reuse is needed to make sure we don't
5796 run out of reload regs. Suppose we have three reloads, and
5797 reloads A and B can share regs. These need two regs.
5798 Suppose A and B are given different regs.
5799 That leaves none for C. */
5800 for (pass = 0; pass < 2; pass++)
5802 /* I is the index in spill_regs.
5803 We advance it round-robin between insns to use all spill regs
5804 equally, so that inherited reloads have a chance
5805 of leapfrogging each other. */
5807 i = last_spill_reg;
5809 for (count = 0; count < n_spills; count++)
5811 int rclass = (int) rld[r].rclass;
5812 int regnum;
5814 i++;
5815 if (i >= n_spills)
5816 i -= n_spills;
5817 regnum = spill_regs[i];
5819 if ((reload_reg_free_p (regnum, rld[r].opnum,
5820 rld[r].when_needed)
5821 || (rld[r].in
5822 /* We check reload_reg_used to make sure we
5823 don't clobber the return register. */
5824 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
5825 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
5826 rld[r].when_needed, rld[r].in,
5827 rld[r].out, r, 1)))
5828 && TEST_HARD_REG_BIT (reg_class_contents[rclass], regnum)
5829 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
5830 /* Look first for regs to share, then for unshared. But
5831 don't share regs used for inherited reloads; they are
5832 the ones we want to preserve. */
5833 && (pass
5834 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
5835 regnum)
5836 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
5837 regnum))))
5839 int nr = hard_regno_nregs[regnum][rld[r].mode];
5840 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
5841 (on 68000) got us two FP regs. If NR is 1,
5842 we would reject both of them. */
5843 if (force_group)
5844 nr = rld[r].nregs;
5845 /* If we need only one reg, we have already won. */
5846 if (nr == 1)
5848 /* But reject a single reg if we demand a group. */
5849 if (force_group)
5850 continue;
5851 break;
5853 /* Otherwise check that as many consecutive regs as we need
5854 are available here. */
5855 while (nr > 1)
5857 int regno = regnum + nr - 1;
5858 if (!(TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
5859 && spill_reg_order[regno] >= 0
5860 && reload_reg_free_p (regno, rld[r].opnum,
5861 rld[r].when_needed)))
5862 break;
5863 nr--;
5865 if (nr == 1)
5866 break;
5870 /* If we found something on pass 1, omit pass 2. */
5871 if (count < n_spills)
5872 break;
5875 /* We should have found a spill register by now. */
5876 if (count >= n_spills)
5877 return 0;
5879 /* I is the index in SPILL_REG_RTX of the reload register we are to
5880 allocate. Get an rtx for it and find its register number. */
5882 return set_reload_reg (i, r);
5885 /* Initialize all the tables needed to allocate reload registers.
5886 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
5887 is the array we use to restore the reg_rtx field for every reload. */
5889 static void
5890 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
5892 int i;
5894 for (i = 0; i < n_reloads; i++)
5895 rld[i].reg_rtx = save_reload_reg_rtx[i];
5897 memset (reload_inherited, 0, MAX_RELOADS);
5898 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
5899 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
5901 CLEAR_HARD_REG_SET (reload_reg_used);
5902 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
5903 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
5904 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
5905 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
5906 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
5908 CLEAR_HARD_REG_SET (reg_used_in_insn);
5910 HARD_REG_SET tmp;
5911 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
5912 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5913 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
5914 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
5915 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
5916 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
5919 for (i = 0; i < reload_n_operands; i++)
5921 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
5922 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
5923 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
5924 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
5925 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
5926 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
5929 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
5931 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
5933 for (i = 0; i < n_reloads; i++)
5934 /* If we have already decided to use a certain register,
5935 don't use it in another way. */
5936 if (rld[i].reg_rtx)
5937 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
5938 rld[i].when_needed, rld[i].mode);
5941 /* Assign hard reg targets for the pseudo-registers we must reload
5942 into hard regs for this insn.
5943 Also output the instructions to copy them in and out of the hard regs.
5945 For machines with register classes, we are responsible for
5946 finding a reload reg in the proper class. */
5948 static void
5949 choose_reload_regs (struct insn_chain *chain)
5951 rtx insn = chain->insn;
5952 int i, j;
5953 unsigned int max_group_size = 1;
5954 enum reg_class group_class = NO_REGS;
5955 int pass, win, inheritance;
5957 rtx save_reload_reg_rtx[MAX_RELOADS];
5959 /* In order to be certain of getting the registers we need,
5960 we must sort the reloads into order of increasing register class.
5961 Then our grabbing of reload registers will parallel the process
5962 that provided the reload registers.
5964 Also note whether any of the reloads wants a consecutive group of regs.
5965 If so, record the maximum size of the group desired and what
5966 register class contains all the groups needed by this insn. */
5968 for (j = 0; j < n_reloads; j++)
5970 reload_order[j] = j;
5971 if (rld[j].reg_rtx != NULL_RTX)
5973 gcc_assert (REG_P (rld[j].reg_rtx)
5974 && HARD_REGISTER_P (rld[j].reg_rtx));
5975 reload_spill_index[j] = REGNO (rld[j].reg_rtx);
5977 else
5978 reload_spill_index[j] = -1;
5980 if (rld[j].nregs > 1)
5982 max_group_size = MAX (rld[j].nregs, max_group_size);
5983 group_class
5984 = reg_class_superunion[(int) rld[j].rclass][(int) group_class];
5987 save_reload_reg_rtx[j] = rld[j].reg_rtx;
5990 if (n_reloads > 1)
5991 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
5993 /* If -O, try first with inheritance, then turning it off.
5994 If not -O, don't do inheritance.
5995 Using inheritance when not optimizing leads to paradoxes
5996 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
5997 because one side of the comparison might be inherited. */
5998 win = 0;
5999 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
6001 choose_reload_regs_init (chain, save_reload_reg_rtx);
6003 /* Process the reloads in order of preference just found.
6004 Beyond this point, subregs can be found in reload_reg_rtx.
6006 This used to look for an existing reloaded home for all of the
6007 reloads, and only then perform any new reloads. But that could lose
6008 if the reloads were done out of reg-class order because a later
6009 reload with a looser constraint might have an old home in a register
6010 needed by an earlier reload with a tighter constraint.
6012 To solve this, we make two passes over the reloads, in the order
6013 described above. In the first pass we try to inherit a reload
6014 from a previous insn. If there is a later reload that needs a
6015 class that is a proper subset of the class being processed, we must
6016 also allocate a spill register during the first pass.
6018 Then make a second pass over the reloads to allocate any reloads
6019 that haven't been given registers yet. */
6021 for (j = 0; j < n_reloads; j++)
6023 int r = reload_order[j];
6024 rtx search_equiv = NULL_RTX;
6026 /* Ignore reloads that got marked inoperative. */
6027 if (rld[r].out == 0 && rld[r].in == 0
6028 && ! rld[r].secondary_p)
6029 continue;
6031 /* If find_reloads chose to use reload_in or reload_out as a reload
6032 register, we don't need to chose one. Otherwise, try even if it
6033 found one since we might save an insn if we find the value lying
6034 around.
6035 Try also when reload_in is a pseudo without a hard reg. */
6036 if (rld[r].in != 0 && rld[r].reg_rtx != 0
6037 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
6038 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
6039 && !MEM_P (rld[r].in)
6040 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
6041 continue;
6043 #if 0 /* No longer needed for correct operation.
6044 It might give better code, or might not; worth an experiment? */
6045 /* If this is an optional reload, we can't inherit from earlier insns
6046 until we are sure that any non-optional reloads have been allocated.
6047 The following code takes advantage of the fact that optional reloads
6048 are at the end of reload_order. */
6049 if (rld[r].optional != 0)
6050 for (i = 0; i < j; i++)
6051 if ((rld[reload_order[i]].out != 0
6052 || rld[reload_order[i]].in != 0
6053 || rld[reload_order[i]].secondary_p)
6054 && ! rld[reload_order[i]].optional
6055 && rld[reload_order[i]].reg_rtx == 0)
6056 allocate_reload_reg (chain, reload_order[i], 0);
6057 #endif
6059 /* First see if this pseudo is already available as reloaded
6060 for a previous insn. We cannot try to inherit for reloads
6061 that are smaller than the maximum number of registers needed
6062 for groups unless the register we would allocate cannot be used
6063 for the groups.
6065 We could check here to see if this is a secondary reload for
6066 an object that is already in a register of the desired class.
6067 This would avoid the need for the secondary reload register.
6068 But this is complex because we can't easily determine what
6069 objects might want to be loaded via this reload. So let a
6070 register be allocated here. In `emit_reload_insns' we suppress
6071 one of the loads in the case described above. */
6073 if (inheritance)
6075 int byte = 0;
6076 int regno = -1;
6077 enum machine_mode mode = VOIDmode;
6079 if (rld[r].in == 0)
6081 else if (REG_P (rld[r].in))
6083 regno = REGNO (rld[r].in);
6084 mode = GET_MODE (rld[r].in);
6086 else if (REG_P (rld[r].in_reg))
6088 regno = REGNO (rld[r].in_reg);
6089 mode = GET_MODE (rld[r].in_reg);
6091 else if (GET_CODE (rld[r].in_reg) == SUBREG
6092 && REG_P (SUBREG_REG (rld[r].in_reg)))
6094 regno = REGNO (SUBREG_REG (rld[r].in_reg));
6095 if (regno < FIRST_PSEUDO_REGISTER)
6096 regno = subreg_regno (rld[r].in_reg);
6097 else
6098 byte = SUBREG_BYTE (rld[r].in_reg);
6099 mode = GET_MODE (rld[r].in_reg);
6101 #ifdef AUTO_INC_DEC
6102 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
6103 && REG_P (XEXP (rld[r].in_reg, 0)))
6105 regno = REGNO (XEXP (rld[r].in_reg, 0));
6106 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
6107 rld[r].out = rld[r].in;
6109 #endif
6110 #if 0
6111 /* This won't work, since REGNO can be a pseudo reg number.
6112 Also, it takes much more hair to keep track of all the things
6113 that can invalidate an inherited reload of part of a pseudoreg. */
6114 else if (GET_CODE (rld[r].in) == SUBREG
6115 && REG_P (SUBREG_REG (rld[r].in)))
6116 regno = subreg_regno (rld[r].in);
6117 #endif
6119 if (regno >= 0
6120 && reg_last_reload_reg[regno] != 0
6121 #ifdef CANNOT_CHANGE_MODE_CLASS
6122 /* Verify that the register it's in can be used in
6123 mode MODE. */
6124 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
6125 GET_MODE (reg_last_reload_reg[regno]),
6126 mode)
6127 #endif
6130 enum reg_class rclass = rld[r].rclass, last_class;
6131 rtx last_reg = reg_last_reload_reg[regno];
6132 enum machine_mode need_mode;
6134 i = REGNO (last_reg);
6135 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
6136 last_class = REGNO_REG_CLASS (i);
6138 if (byte == 0)
6139 need_mode = mode;
6140 else
6141 need_mode
6142 = smallest_mode_for_size
6143 (GET_MODE_BITSIZE (mode) + byte * BITS_PER_UNIT,
6144 GET_MODE_CLASS (mode) == MODE_PARTIAL_INT
6145 ? MODE_INT : GET_MODE_CLASS (mode));
6147 if ((GET_MODE_SIZE (GET_MODE (last_reg))
6148 >= GET_MODE_SIZE (need_mode))
6149 && reg_reloaded_contents[i] == regno
6150 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
6151 && HARD_REGNO_MODE_OK (i, rld[r].mode)
6152 && (TEST_HARD_REG_BIT (reg_class_contents[(int) rclass], i)
6153 /* Even if we can't use this register as a reload
6154 register, we might use it for reload_override_in,
6155 if copying it to the desired class is cheap
6156 enough. */
6157 || ((REGISTER_MOVE_COST (mode, last_class, rclass)
6158 < MEMORY_MOVE_COST (mode, rclass, 1))
6159 && (secondary_reload_class (1, rclass, mode,
6160 last_reg)
6161 == NO_REGS)
6162 #ifdef SECONDARY_MEMORY_NEEDED
6163 && ! SECONDARY_MEMORY_NEEDED (last_class, rclass,
6164 mode)
6165 #endif
6168 && (rld[r].nregs == max_group_size
6169 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
6171 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
6172 rld[r].when_needed, rld[r].in,
6173 const0_rtx, r, 1))
6175 /* If a group is needed, verify that all the subsequent
6176 registers still have their values intact. */
6177 int nr = hard_regno_nregs[i][rld[r].mode];
6178 int k;
6180 for (k = 1; k < nr; k++)
6181 if (reg_reloaded_contents[i + k] != regno
6182 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6183 break;
6185 if (k == nr)
6187 int i1;
6188 int bad_for_class;
6190 last_reg = (GET_MODE (last_reg) == mode
6191 ? last_reg : gen_rtx_REG (mode, i));
6193 bad_for_class = 0;
6194 for (k = 0; k < nr; k++)
6195 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6196 i+k);
6198 /* We found a register that contains the
6199 value we need. If this register is the
6200 same as an `earlyclobber' operand of the
6201 current insn, just mark it as a place to
6202 reload from since we can't use it as the
6203 reload register itself. */
6205 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6206 if (reg_overlap_mentioned_for_reload_p
6207 (reg_last_reload_reg[regno],
6208 reload_earlyclobbers[i1]))
6209 break;
6211 if (i1 != n_earlyclobbers
6212 || ! (free_for_value_p (i, rld[r].mode,
6213 rld[r].opnum,
6214 rld[r].when_needed, rld[r].in,
6215 rld[r].out, r, 1))
6216 /* Don't use it if we'd clobber a pseudo reg. */
6217 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6218 && rld[r].out
6219 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6220 /* Don't clobber the frame pointer. */
6221 || (i == HARD_FRAME_POINTER_REGNUM
6222 && frame_pointer_needed
6223 && rld[r].out)
6224 /* Don't really use the inherited spill reg
6225 if we need it wider than we've got it. */
6226 || (GET_MODE_SIZE (rld[r].mode)
6227 > GET_MODE_SIZE (mode))
6228 || bad_for_class
6230 /* If find_reloads chose reload_out as reload
6231 register, stay with it - that leaves the
6232 inherited register for subsequent reloads. */
6233 || (rld[r].out && rld[r].reg_rtx
6234 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6236 if (! rld[r].optional)
6238 reload_override_in[r] = last_reg;
6239 reload_inheritance_insn[r]
6240 = reg_reloaded_insn[i];
6243 else
6245 int k;
6246 /* We can use this as a reload reg. */
6247 /* Mark the register as in use for this part of
6248 the insn. */
6249 mark_reload_reg_in_use (i,
6250 rld[r].opnum,
6251 rld[r].when_needed,
6252 rld[r].mode);
6253 rld[r].reg_rtx = last_reg;
6254 reload_inherited[r] = 1;
6255 reload_inheritance_insn[r]
6256 = reg_reloaded_insn[i];
6257 reload_spill_index[r] = i;
6258 for (k = 0; k < nr; k++)
6259 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6260 i + k);
6267 /* Here's another way to see if the value is already lying around. */
6268 if (inheritance
6269 && rld[r].in != 0
6270 && ! reload_inherited[r]
6271 && rld[r].out == 0
6272 && (CONSTANT_P (rld[r].in)
6273 || GET_CODE (rld[r].in) == PLUS
6274 || REG_P (rld[r].in)
6275 || MEM_P (rld[r].in))
6276 && (rld[r].nregs == max_group_size
6277 || ! reg_classes_intersect_p (rld[r].rclass, group_class)))
6278 search_equiv = rld[r].in;
6279 /* If this is an output reload from a simple move insn, look
6280 if an equivalence for the input is available. */
6281 else if (inheritance && rld[r].in == 0 && rld[r].out != 0)
6283 rtx set = single_set (insn);
6285 if (set
6286 && rtx_equal_p (rld[r].out, SET_DEST (set))
6287 && CONSTANT_P (SET_SRC (set)))
6288 search_equiv = SET_SRC (set);
6291 if (search_equiv)
6293 rtx equiv
6294 = find_equiv_reg (search_equiv, insn, rld[r].rclass,
6295 -1, NULL, 0, rld[r].mode);
6296 int regno = 0;
6298 if (equiv != 0)
6300 if (REG_P (equiv))
6301 regno = REGNO (equiv);
6302 else
6304 /* This must be a SUBREG of a hard register.
6305 Make a new REG since this might be used in an
6306 address and not all machines support SUBREGs
6307 there. */
6308 gcc_assert (GET_CODE (equiv) == SUBREG);
6309 regno = subreg_regno (equiv);
6310 equiv = gen_rtx_REG (rld[r].mode, regno);
6311 /* If we choose EQUIV as the reload register, but the
6312 loop below decides to cancel the inheritance, we'll
6313 end up reloading EQUIV in rld[r].mode, not the mode
6314 it had originally. That isn't safe when EQUIV isn't
6315 available as a spill register since its value might
6316 still be live at this point. */
6317 for (i = regno; i < regno + (int) rld[r].nregs; i++)
6318 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
6319 equiv = 0;
6323 /* If we found a spill reg, reject it unless it is free
6324 and of the desired class. */
6325 if (equiv != 0)
6327 int regs_used = 0;
6328 int bad_for_class = 0;
6329 int max_regno = regno + rld[r].nregs;
6331 for (i = regno; i < max_regno; i++)
6333 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
6335 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6339 if ((regs_used
6340 && ! free_for_value_p (regno, rld[r].mode,
6341 rld[r].opnum, rld[r].when_needed,
6342 rld[r].in, rld[r].out, r, 1))
6343 || bad_for_class)
6344 equiv = 0;
6347 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6348 equiv = 0;
6350 /* We found a register that contains the value we need.
6351 If this register is the same as an `earlyclobber' operand
6352 of the current insn, just mark it as a place to reload from
6353 since we can't use it as the reload register itself. */
6355 if (equiv != 0)
6356 for (i = 0; i < n_earlyclobbers; i++)
6357 if (reg_overlap_mentioned_for_reload_p (equiv,
6358 reload_earlyclobbers[i]))
6360 if (! rld[r].optional)
6361 reload_override_in[r] = equiv;
6362 equiv = 0;
6363 break;
6366 /* If the equiv register we have found is explicitly clobbered
6367 in the current insn, it depends on the reload type if we
6368 can use it, use it for reload_override_in, or not at all.
6369 In particular, we then can't use EQUIV for a
6370 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6372 if (equiv != 0)
6374 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
6375 switch (rld[r].when_needed)
6377 case RELOAD_FOR_OTHER_ADDRESS:
6378 case RELOAD_FOR_INPADDR_ADDRESS:
6379 case RELOAD_FOR_INPUT_ADDRESS:
6380 case RELOAD_FOR_OPADDR_ADDR:
6381 break;
6382 case RELOAD_OTHER:
6383 case RELOAD_FOR_INPUT:
6384 case RELOAD_FOR_OPERAND_ADDRESS:
6385 if (! rld[r].optional)
6386 reload_override_in[r] = equiv;
6387 /* Fall through. */
6388 default:
6389 equiv = 0;
6390 break;
6392 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
6393 switch (rld[r].when_needed)
6395 case RELOAD_FOR_OTHER_ADDRESS:
6396 case RELOAD_FOR_INPADDR_ADDRESS:
6397 case RELOAD_FOR_INPUT_ADDRESS:
6398 case RELOAD_FOR_OPADDR_ADDR:
6399 case RELOAD_FOR_OPERAND_ADDRESS:
6400 case RELOAD_FOR_INPUT:
6401 break;
6402 case RELOAD_OTHER:
6403 if (! rld[r].optional)
6404 reload_override_in[r] = equiv;
6405 /* Fall through. */
6406 default:
6407 equiv = 0;
6408 break;
6412 /* If we found an equivalent reg, say no code need be generated
6413 to load it, and use it as our reload reg. */
6414 if (equiv != 0
6415 && (regno != HARD_FRAME_POINTER_REGNUM
6416 || !frame_pointer_needed))
6418 int nr = hard_regno_nregs[regno][rld[r].mode];
6419 int k;
6420 rld[r].reg_rtx = equiv;
6421 reload_spill_index[r] = regno;
6422 reload_inherited[r] = 1;
6424 /* If reg_reloaded_valid is not set for this register,
6425 there might be a stale spill_reg_store lying around.
6426 We must clear it, since otherwise emit_reload_insns
6427 might delete the store. */
6428 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6429 spill_reg_store[regno] = NULL_RTX;
6430 /* If any of the hard registers in EQUIV are spill
6431 registers, mark them as in use for this insn. */
6432 for (k = 0; k < nr; k++)
6434 i = spill_reg_order[regno + k];
6435 if (i >= 0)
6437 mark_reload_reg_in_use (regno, rld[r].opnum,
6438 rld[r].when_needed,
6439 rld[r].mode);
6440 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6441 regno + k);
6447 /* If we found a register to use already, or if this is an optional
6448 reload, we are done. */
6449 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6450 continue;
6452 #if 0
6453 /* No longer needed for correct operation. Might or might
6454 not give better code on the average. Want to experiment? */
6456 /* See if there is a later reload that has a class different from our
6457 class that intersects our class or that requires less register
6458 than our reload. If so, we must allocate a register to this
6459 reload now, since that reload might inherit a previous reload
6460 and take the only available register in our class. Don't do this
6461 for optional reloads since they will force all previous reloads
6462 to be allocated. Also don't do this for reloads that have been
6463 turned off. */
6465 for (i = j + 1; i < n_reloads; i++)
6467 int s = reload_order[i];
6469 if ((rld[s].in == 0 && rld[s].out == 0
6470 && ! rld[s].secondary_p)
6471 || rld[s].optional)
6472 continue;
6474 if ((rld[s].rclass != rld[r].rclass
6475 && reg_classes_intersect_p (rld[r].rclass,
6476 rld[s].rclass))
6477 || rld[s].nregs < rld[r].nregs)
6478 break;
6481 if (i == n_reloads)
6482 continue;
6484 allocate_reload_reg (chain, r, j == n_reloads - 1);
6485 #endif
6488 /* Now allocate reload registers for anything non-optional that
6489 didn't get one yet. */
6490 for (j = 0; j < n_reloads; j++)
6492 int r = reload_order[j];
6494 /* Ignore reloads that got marked inoperative. */
6495 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6496 continue;
6498 /* Skip reloads that already have a register allocated or are
6499 optional. */
6500 if (rld[r].reg_rtx != 0 || rld[r].optional)
6501 continue;
6503 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6504 break;
6507 /* If that loop got all the way, we have won. */
6508 if (j == n_reloads)
6510 win = 1;
6511 break;
6514 /* Loop around and try without any inheritance. */
6517 if (! win)
6519 /* First undo everything done by the failed attempt
6520 to allocate with inheritance. */
6521 choose_reload_regs_init (chain, save_reload_reg_rtx);
6523 /* Some sanity tests to verify that the reloads found in the first
6524 pass are identical to the ones we have now. */
6525 gcc_assert (chain->n_reloads == n_reloads);
6527 for (i = 0; i < n_reloads; i++)
6529 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6530 continue;
6531 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6532 for (j = 0; j < n_spills; j++)
6533 if (spill_regs[j] == chain->rld[i].regno)
6534 if (! set_reload_reg (j, i))
6535 failed_reload (chain->insn, i);
6539 /* If we thought we could inherit a reload, because it seemed that
6540 nothing else wanted the same reload register earlier in the insn,
6541 verify that assumption, now that all reloads have been assigned.
6542 Likewise for reloads where reload_override_in has been set. */
6544 /* If doing expensive optimizations, do one preliminary pass that doesn't
6545 cancel any inheritance, but removes reloads that have been needed only
6546 for reloads that we know can be inherited. */
6547 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6549 for (j = 0; j < n_reloads; j++)
6551 int r = reload_order[j];
6552 rtx check_reg;
6553 if (reload_inherited[r] && rld[r].reg_rtx)
6554 check_reg = rld[r].reg_rtx;
6555 else if (reload_override_in[r]
6556 && (REG_P (reload_override_in[r])
6557 || GET_CODE (reload_override_in[r]) == SUBREG))
6558 check_reg = reload_override_in[r];
6559 else
6560 continue;
6561 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6562 rld[r].opnum, rld[r].when_needed, rld[r].in,
6563 (reload_inherited[r]
6564 ? rld[r].out : const0_rtx),
6565 r, 1))
6567 if (pass)
6568 continue;
6569 reload_inherited[r] = 0;
6570 reload_override_in[r] = 0;
6572 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6573 reload_override_in, then we do not need its related
6574 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6575 likewise for other reload types.
6576 We handle this by removing a reload when its only replacement
6577 is mentioned in reload_in of the reload we are going to inherit.
6578 A special case are auto_inc expressions; even if the input is
6579 inherited, we still need the address for the output. We can
6580 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6581 If we succeeded removing some reload and we are doing a preliminary
6582 pass just to remove such reloads, make another pass, since the
6583 removal of one reload might allow us to inherit another one. */
6584 else if (rld[r].in
6585 && rld[r].out != rld[r].in
6586 && remove_address_replacements (rld[r].in) && pass)
6587 pass = 2;
6591 /* Now that reload_override_in is known valid,
6592 actually override reload_in. */
6593 for (j = 0; j < n_reloads; j++)
6594 if (reload_override_in[j])
6595 rld[j].in = reload_override_in[j];
6597 /* If this reload won't be done because it has been canceled or is
6598 optional and not inherited, clear reload_reg_rtx so other
6599 routines (such as subst_reloads) don't get confused. */
6600 for (j = 0; j < n_reloads; j++)
6601 if (rld[j].reg_rtx != 0
6602 && ((rld[j].optional && ! reload_inherited[j])
6603 || (rld[j].in == 0 && rld[j].out == 0
6604 && ! rld[j].secondary_p)))
6606 int regno = true_regnum (rld[j].reg_rtx);
6608 if (spill_reg_order[regno] >= 0)
6609 clear_reload_reg_in_use (regno, rld[j].opnum,
6610 rld[j].when_needed, rld[j].mode);
6611 rld[j].reg_rtx = 0;
6612 reload_spill_index[j] = -1;
6615 /* Record which pseudos and which spill regs have output reloads. */
6616 for (j = 0; j < n_reloads; j++)
6618 int r = reload_order[j];
6620 i = reload_spill_index[r];
6622 /* I is nonneg if this reload uses a register.
6623 If rld[r].reg_rtx is 0, this is an optional reload
6624 that we opted to ignore. */
6625 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
6626 && rld[r].reg_rtx != 0)
6628 int nregno = REGNO (rld[r].out_reg);
6629 int nr = 1;
6631 if (nregno < FIRST_PSEUDO_REGISTER)
6632 nr = hard_regno_nregs[nregno][rld[r].mode];
6634 while (--nr >= 0)
6635 SET_REGNO_REG_SET (&reg_has_output_reload,
6636 nregno + nr);
6638 if (i >= 0)
6640 nr = hard_regno_nregs[i][rld[r].mode];
6641 while (--nr >= 0)
6642 SET_HARD_REG_BIT (reg_is_output_reload, i + nr);
6645 gcc_assert (rld[r].when_needed == RELOAD_OTHER
6646 || rld[r].when_needed == RELOAD_FOR_OUTPUT
6647 || rld[r].when_needed == RELOAD_FOR_INSN);
6652 /* Deallocate the reload register for reload R. This is called from
6653 remove_address_replacements. */
6655 void
6656 deallocate_reload_reg (int r)
6658 int regno;
6660 if (! rld[r].reg_rtx)
6661 return;
6662 regno = true_regnum (rld[r].reg_rtx);
6663 rld[r].reg_rtx = 0;
6664 if (spill_reg_order[regno] >= 0)
6665 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
6666 rld[r].mode);
6667 reload_spill_index[r] = -1;
6670 /* If SMALL_REGISTER_CLASSES is nonzero, we may not have merged two
6671 reloads of the same item for fear that we might not have enough reload
6672 registers. However, normally they will get the same reload register
6673 and hence actually need not be loaded twice.
6675 Here we check for the most common case of this phenomenon: when we have
6676 a number of reloads for the same object, each of which were allocated
6677 the same reload_reg_rtx, that reload_reg_rtx is not used for any other
6678 reload, and is not modified in the insn itself. If we find such,
6679 merge all the reloads and set the resulting reload to RELOAD_OTHER.
6680 This will not increase the number of spill registers needed and will
6681 prevent redundant code. */
6683 static void
6684 merge_assigned_reloads (rtx insn)
6686 int i, j;
6688 /* Scan all the reloads looking for ones that only load values and
6689 are not already RELOAD_OTHER and ones whose reload_reg_rtx are
6690 assigned and not modified by INSN. */
6692 for (i = 0; i < n_reloads; i++)
6694 int conflicting_input = 0;
6695 int max_input_address_opnum = -1;
6696 int min_conflicting_input_opnum = MAX_RECOG_OPERANDS;
6698 if (rld[i].in == 0 || rld[i].when_needed == RELOAD_OTHER
6699 || rld[i].out != 0 || rld[i].reg_rtx == 0
6700 || reg_set_p (rld[i].reg_rtx, insn))
6701 continue;
6703 /* Look at all other reloads. Ensure that the only use of this
6704 reload_reg_rtx is in a reload that just loads the same value
6705 as we do. Note that any secondary reloads must be of the identical
6706 class since the values, modes, and result registers are the
6707 same, so we need not do anything with any secondary reloads. */
6709 for (j = 0; j < n_reloads; j++)
6711 if (i == j || rld[j].reg_rtx == 0
6712 || ! reg_overlap_mentioned_p (rld[j].reg_rtx,
6713 rld[i].reg_rtx))
6714 continue;
6716 if (rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6717 && rld[j].opnum > max_input_address_opnum)
6718 max_input_address_opnum = rld[j].opnum;
6720 /* If the reload regs aren't exactly the same (e.g, different modes)
6721 or if the values are different, we can't merge this reload.
6722 But if it is an input reload, we might still merge
6723 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_OTHER_ADDRESS reloads. */
6725 if (! rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6726 || rld[j].out != 0 || rld[j].in == 0
6727 || ! rtx_equal_p (rld[i].in, rld[j].in))
6729 if (rld[j].when_needed != RELOAD_FOR_INPUT
6730 || ((rld[i].when_needed != RELOAD_FOR_INPUT_ADDRESS
6731 || rld[i].opnum > rld[j].opnum)
6732 && rld[i].when_needed != RELOAD_FOR_OTHER_ADDRESS))
6733 break;
6734 conflicting_input = 1;
6735 if (min_conflicting_input_opnum > rld[j].opnum)
6736 min_conflicting_input_opnum = rld[j].opnum;
6740 /* If all is OK, merge the reloads. Only set this to RELOAD_OTHER if
6741 we, in fact, found any matching reloads. */
6743 if (j == n_reloads
6744 && max_input_address_opnum <= min_conflicting_input_opnum)
6746 gcc_assert (rld[i].when_needed != RELOAD_FOR_OUTPUT);
6748 for (j = 0; j < n_reloads; j++)
6749 if (i != j && rld[j].reg_rtx != 0
6750 && rtx_equal_p (rld[i].reg_rtx, rld[j].reg_rtx)
6751 && (! conflicting_input
6752 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6753 || rld[j].when_needed == RELOAD_FOR_OTHER_ADDRESS))
6755 rld[i].when_needed = RELOAD_OTHER;
6756 rld[j].in = 0;
6757 reload_spill_index[j] = -1;
6758 transfer_replacements (i, j);
6761 /* If this is now RELOAD_OTHER, look for any reloads that
6762 load parts of this operand and set them to
6763 RELOAD_FOR_OTHER_ADDRESS if they were for inputs,
6764 RELOAD_OTHER for outputs. Note that this test is
6765 equivalent to looking for reloads for this operand
6766 number.
6768 We must take special care with RELOAD_FOR_OUTPUT_ADDRESS;
6769 it may share registers with a RELOAD_FOR_INPUT, so we can
6770 not change it to RELOAD_FOR_OTHER_ADDRESS. We should
6771 never need to, since we do not modify RELOAD_FOR_OUTPUT.
6773 It is possible that the RELOAD_FOR_OPERAND_ADDRESS
6774 instruction is assigned the same register as the earlier
6775 RELOAD_FOR_OTHER_ADDRESS instruction. Merging these two
6776 instructions will cause the RELOAD_FOR_OTHER_ADDRESS
6777 instruction to be deleted later on. */
6779 if (rld[i].when_needed == RELOAD_OTHER)
6780 for (j = 0; j < n_reloads; j++)
6781 if (rld[j].in != 0
6782 && rld[j].when_needed != RELOAD_OTHER
6783 && rld[j].when_needed != RELOAD_FOR_OTHER_ADDRESS
6784 && rld[j].when_needed != RELOAD_FOR_OUTPUT_ADDRESS
6785 && rld[j].when_needed != RELOAD_FOR_OPERAND_ADDRESS
6786 && (! conflicting_input
6787 || rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6788 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6789 && reg_overlap_mentioned_for_reload_p (rld[j].in,
6790 rld[i].in))
6792 int k;
6794 rld[j].when_needed
6795 = ((rld[j].when_needed == RELOAD_FOR_INPUT_ADDRESS
6796 || rld[j].when_needed == RELOAD_FOR_INPADDR_ADDRESS)
6797 ? RELOAD_FOR_OTHER_ADDRESS : RELOAD_OTHER);
6799 /* Check to see if we accidentally converted two
6800 reloads that use the same reload register with
6801 different inputs to the same type. If so, the
6802 resulting code won't work. */
6803 if (rld[j].reg_rtx)
6804 for (k = 0; k < j; k++)
6805 gcc_assert (rld[k].in == 0 || rld[k].reg_rtx == 0
6806 || rld[k].when_needed != rld[j].when_needed
6807 || !rtx_equal_p (rld[k].reg_rtx,
6808 rld[j].reg_rtx)
6809 || rtx_equal_p (rld[k].in,
6810 rld[j].in));
6816 /* These arrays are filled by emit_reload_insns and its subroutines. */
6817 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
6818 static rtx other_input_address_reload_insns = 0;
6819 static rtx other_input_reload_insns = 0;
6820 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
6821 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6822 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
6823 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
6824 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
6825 static rtx operand_reload_insns = 0;
6826 static rtx other_operand_reload_insns = 0;
6827 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
6829 /* Values to be put in spill_reg_store are put here first. */
6830 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
6831 static HARD_REG_SET reg_reloaded_died;
6833 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
6834 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
6835 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
6836 adjusted register, and return true. Otherwise, return false. */
6837 static bool
6838 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
6839 enum reg_class new_class,
6840 enum machine_mode new_mode)
6843 rtx reg;
6845 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
6847 unsigned regno = REGNO (reg);
6849 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
6850 continue;
6851 if (GET_MODE (reg) != new_mode)
6853 if (!HARD_REGNO_MODE_OK (regno, new_mode))
6854 continue;
6855 if (hard_regno_nregs[regno][new_mode]
6856 > hard_regno_nregs[regno][GET_MODE (reg)])
6857 continue;
6858 reg = reload_adjust_reg_for_mode (reg, new_mode);
6860 *reload_reg = reg;
6861 return true;
6863 return false;
6866 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
6867 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
6868 nonzero, if that is suitable. On success, change *RELOAD_REG to the
6869 adjusted register, and return true. Otherwise, return false. */
6870 static bool
6871 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
6872 enum insn_code icode)
6875 enum reg_class new_class = scratch_reload_class (icode);
6876 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
6878 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
6879 new_class, new_mode);
6882 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
6883 has the number J. OLD contains the value to be used as input. */
6885 static void
6886 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
6887 rtx old, int j)
6889 rtx insn = chain->insn;
6890 rtx reloadreg;
6891 rtx oldequiv_reg = 0;
6892 rtx oldequiv = 0;
6893 int special = 0;
6894 enum machine_mode mode;
6895 rtx *where;
6897 /* delete_output_reload is only invoked properly if old contains
6898 the original pseudo register. Since this is replaced with a
6899 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
6900 find the pseudo in RELOAD_IN_REG. */
6901 if (reload_override_in[j]
6902 && REG_P (rl->in_reg))
6904 oldequiv = old;
6905 old = rl->in_reg;
6907 if (oldequiv == 0)
6908 oldequiv = old;
6909 else if (REG_P (oldequiv))
6910 oldequiv_reg = oldequiv;
6911 else if (GET_CODE (oldequiv) == SUBREG)
6912 oldequiv_reg = SUBREG_REG (oldequiv);
6914 reloadreg = reload_reg_rtx_for_input[j];
6915 mode = GET_MODE (reloadreg);
6917 /* If we are reloading from a register that was recently stored in
6918 with an output-reload, see if we can prove there was
6919 actually no need to store the old value in it. */
6921 if (optimize && REG_P (oldequiv)
6922 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6923 && spill_reg_store[REGNO (oldequiv)]
6924 && REG_P (old)
6925 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
6926 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6927 rl->out_reg)))
6928 delete_output_reload (insn, j, REGNO (oldequiv), reloadreg);
6930 /* Encapsulate OLDEQUIV into the reload mode, then load RELOADREG from
6931 OLDEQUIV. */
6933 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
6934 oldequiv = SUBREG_REG (oldequiv);
6935 if (GET_MODE (oldequiv) != VOIDmode
6936 && mode != GET_MODE (oldequiv))
6937 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
6939 /* Switch to the right place to emit the reload insns. */
6940 switch (rl->when_needed)
6942 case RELOAD_OTHER:
6943 where = &other_input_reload_insns;
6944 break;
6945 case RELOAD_FOR_INPUT:
6946 where = &input_reload_insns[rl->opnum];
6947 break;
6948 case RELOAD_FOR_INPUT_ADDRESS:
6949 where = &input_address_reload_insns[rl->opnum];
6950 break;
6951 case RELOAD_FOR_INPADDR_ADDRESS:
6952 where = &inpaddr_address_reload_insns[rl->opnum];
6953 break;
6954 case RELOAD_FOR_OUTPUT_ADDRESS:
6955 where = &output_address_reload_insns[rl->opnum];
6956 break;
6957 case RELOAD_FOR_OUTADDR_ADDRESS:
6958 where = &outaddr_address_reload_insns[rl->opnum];
6959 break;
6960 case RELOAD_FOR_OPERAND_ADDRESS:
6961 where = &operand_reload_insns;
6962 break;
6963 case RELOAD_FOR_OPADDR_ADDR:
6964 where = &other_operand_reload_insns;
6965 break;
6966 case RELOAD_FOR_OTHER_ADDRESS:
6967 where = &other_input_address_reload_insns;
6968 break;
6969 default:
6970 gcc_unreachable ();
6973 push_to_sequence (*where);
6975 /* Auto-increment addresses must be reloaded in a special way. */
6976 if (rl->out && ! rl->out_reg)
6978 /* We are not going to bother supporting the case where a
6979 incremented register can't be copied directly from
6980 OLDEQUIV since this seems highly unlikely. */
6981 gcc_assert (rl->secondary_in_reload < 0);
6983 if (reload_inherited[j])
6984 oldequiv = reloadreg;
6986 old = XEXP (rl->in_reg, 0);
6988 if (optimize && REG_P (oldequiv)
6989 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
6990 && spill_reg_store[REGNO (oldequiv)]
6991 && REG_P (old)
6992 && (dead_or_set_p (insn,
6993 spill_reg_stored_to[REGNO (oldequiv)])
6994 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
6995 old)))
6996 delete_output_reload (insn, j, REGNO (oldequiv), reloadreg);
6998 /* Prevent normal processing of this reload. */
6999 special = 1;
7000 /* Output a special code sequence for this case. */
7001 new_spill_reg_store[REGNO (reloadreg)]
7002 = inc_for_reload (reloadreg, oldequiv, rl->out,
7003 rl->inc);
7006 /* If we are reloading a pseudo-register that was set by the previous
7007 insn, see if we can get rid of that pseudo-register entirely
7008 by redirecting the previous insn into our reload register. */
7010 else if (optimize && REG_P (old)
7011 && REGNO (old) >= FIRST_PSEUDO_REGISTER
7012 && dead_or_set_p (insn, old)
7013 /* This is unsafe if some other reload
7014 uses the same reg first. */
7015 && ! conflicts_with_override (reloadreg)
7016 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
7017 rl->when_needed, old, rl->out, j, 0))
7019 rtx temp = PREV_INSN (insn);
7020 while (temp && (NOTE_P (temp) || DEBUG_INSN_P (temp)))
7021 temp = PREV_INSN (temp);
7022 if (temp
7023 && NONJUMP_INSN_P (temp)
7024 && GET_CODE (PATTERN (temp)) == SET
7025 && SET_DEST (PATTERN (temp)) == old
7026 /* Make sure we can access insn_operand_constraint. */
7027 && asm_noperands (PATTERN (temp)) < 0
7028 /* This is unsafe if operand occurs more than once in current
7029 insn. Perhaps some occurrences aren't reloaded. */
7030 && count_occurrences (PATTERN (insn), old, 0) == 1)
7032 rtx old = SET_DEST (PATTERN (temp));
7033 /* Store into the reload register instead of the pseudo. */
7034 SET_DEST (PATTERN (temp)) = reloadreg;
7036 /* Verify that resulting insn is valid. */
7037 extract_insn (temp);
7038 if (constrain_operands (1))
7040 /* If the previous insn is an output reload, the source is
7041 a reload register, and its spill_reg_store entry will
7042 contain the previous destination. This is now
7043 invalid. */
7044 if (REG_P (SET_SRC (PATTERN (temp)))
7045 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
7047 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7048 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7051 /* If these are the only uses of the pseudo reg,
7052 pretend for GDB it lives in the reload reg we used. */
7053 if (REG_N_DEATHS (REGNO (old)) == 1
7054 && REG_N_SETS (REGNO (old)) == 1)
7056 reg_renumber[REGNO (old)] = REGNO (reloadreg);
7057 if (ira_conflicts_p)
7058 /* Inform IRA about the change. */
7059 ira_mark_allocation_change (REGNO (old));
7060 alter_reg (REGNO (old), -1, false);
7062 special = 1;
7064 /* Adjust any debug insns between temp and insn. */
7065 while ((temp = NEXT_INSN (temp)) != insn)
7066 if (DEBUG_INSN_P (temp))
7067 replace_rtx (PATTERN (temp), old, reloadreg);
7068 else
7069 gcc_assert (NOTE_P (temp));
7071 else
7073 SET_DEST (PATTERN (temp)) = old;
7078 /* We can't do that, so output an insn to load RELOADREG. */
7080 /* If we have a secondary reload, pick up the secondary register
7081 and icode, if any. If OLDEQUIV and OLD are different or
7082 if this is an in-out reload, recompute whether or not we
7083 still need a secondary register and what the icode should
7084 be. If we still need a secondary register and the class or
7085 icode is different, go back to reloading from OLD if using
7086 OLDEQUIV means that we got the wrong type of register. We
7087 cannot have different class or icode due to an in-out reload
7088 because we don't make such reloads when both the input and
7089 output need secondary reload registers. */
7091 if (! special && rl->secondary_in_reload >= 0)
7093 rtx second_reload_reg = 0;
7094 rtx third_reload_reg = 0;
7095 int secondary_reload = rl->secondary_in_reload;
7096 rtx real_oldequiv = oldequiv;
7097 rtx real_old = old;
7098 rtx tmp;
7099 enum insn_code icode;
7100 enum insn_code tertiary_icode = CODE_FOR_nothing;
7102 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
7103 and similarly for OLD.
7104 See comments in get_secondary_reload in reload.c. */
7105 /* If it is a pseudo that cannot be replaced with its
7106 equivalent MEM, we must fall back to reload_in, which
7107 will have all the necessary substitutions registered.
7108 Likewise for a pseudo that can't be replaced with its
7109 equivalent constant.
7111 Take extra care for subregs of such pseudos. Note that
7112 we cannot use reg_equiv_mem in this case because it is
7113 not in the right mode. */
7115 tmp = oldequiv;
7116 if (GET_CODE (tmp) == SUBREG)
7117 tmp = SUBREG_REG (tmp);
7118 if (REG_P (tmp)
7119 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7120 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
7121 || reg_equiv_constant[REGNO (tmp)] != 0))
7123 if (! reg_equiv_mem[REGNO (tmp)]
7124 || num_not_at_initial_offset
7125 || GET_CODE (oldequiv) == SUBREG)
7126 real_oldequiv = rl->in;
7127 else
7128 real_oldequiv = reg_equiv_mem[REGNO (tmp)];
7131 tmp = old;
7132 if (GET_CODE (tmp) == SUBREG)
7133 tmp = SUBREG_REG (tmp);
7134 if (REG_P (tmp)
7135 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7136 && (reg_equiv_memory_loc[REGNO (tmp)] != 0
7137 || reg_equiv_constant[REGNO (tmp)] != 0))
7139 if (! reg_equiv_mem[REGNO (tmp)]
7140 || num_not_at_initial_offset
7141 || GET_CODE (old) == SUBREG)
7142 real_old = rl->in;
7143 else
7144 real_old = reg_equiv_mem[REGNO (tmp)];
7147 second_reload_reg = rld[secondary_reload].reg_rtx;
7148 if (rld[secondary_reload].secondary_in_reload >= 0)
7150 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
7152 third_reload_reg = rld[tertiary_reload].reg_rtx;
7153 tertiary_icode = rld[secondary_reload].secondary_in_icode;
7154 /* We'd have to add more code for quartary reloads. */
7155 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
7157 icode = rl->secondary_in_icode;
7159 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
7160 || (rl->in != 0 && rl->out != 0))
7162 secondary_reload_info sri, sri2;
7163 enum reg_class new_class, new_t_class;
7165 sri.icode = CODE_FOR_nothing;
7166 sri.prev_sri = NULL;
7167 new_class = targetm.secondary_reload (1, real_oldequiv, rl->rclass,
7168 mode, &sri);
7170 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
7171 second_reload_reg = 0;
7172 else if (new_class == NO_REGS)
7174 if (reload_adjust_reg_for_icode (&second_reload_reg,
7175 third_reload_reg,
7176 (enum insn_code) sri.icode))
7178 icode = (enum insn_code) sri.icode;
7179 third_reload_reg = 0;
7181 else
7183 oldequiv = old;
7184 real_oldequiv = real_old;
7187 else if (sri.icode != CODE_FOR_nothing)
7188 /* We currently lack a way to express this in reloads. */
7189 gcc_unreachable ();
7190 else
7192 sri2.icode = CODE_FOR_nothing;
7193 sri2.prev_sri = &sri;
7194 new_t_class = targetm.secondary_reload (1, real_oldequiv,
7195 new_class, mode, &sri);
7196 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
7198 if (reload_adjust_reg_for_temp (&second_reload_reg,
7199 third_reload_reg,
7200 new_class, mode))
7202 third_reload_reg = 0;
7203 tertiary_icode = (enum insn_code) sri2.icode;
7205 else
7207 oldequiv = old;
7208 real_oldequiv = real_old;
7211 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
7213 rtx intermediate = second_reload_reg;
7215 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7216 new_class, mode)
7217 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
7218 ((enum insn_code)
7219 sri2.icode)))
7221 second_reload_reg = intermediate;
7222 tertiary_icode = (enum insn_code) sri2.icode;
7224 else
7226 oldequiv = old;
7227 real_oldequiv = real_old;
7230 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
7232 rtx intermediate = second_reload_reg;
7234 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7235 new_class, mode)
7236 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
7237 new_t_class, mode))
7239 second_reload_reg = intermediate;
7240 tertiary_icode = (enum insn_code) sri2.icode;
7242 else
7244 oldequiv = old;
7245 real_oldequiv = real_old;
7248 else
7250 /* This could be handled more intelligently too. */
7251 oldequiv = old;
7252 real_oldequiv = real_old;
7257 /* If we still need a secondary reload register, check
7258 to see if it is being used as a scratch or intermediate
7259 register and generate code appropriately. If we need
7260 a scratch register, use REAL_OLDEQUIV since the form of
7261 the insn may depend on the actual address if it is
7262 a MEM. */
7264 if (second_reload_reg)
7266 if (icode != CODE_FOR_nothing)
7268 /* We'd have to add extra code to handle this case. */
7269 gcc_assert (!third_reload_reg);
7271 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7272 second_reload_reg));
7273 special = 1;
7275 else
7277 /* See if we need a scratch register to load the
7278 intermediate register (a tertiary reload). */
7279 if (tertiary_icode != CODE_FOR_nothing)
7281 emit_insn ((GEN_FCN (tertiary_icode)
7282 (second_reload_reg, real_oldequiv,
7283 third_reload_reg)));
7285 else if (third_reload_reg)
7287 gen_reload (third_reload_reg, real_oldequiv,
7288 rl->opnum,
7289 rl->when_needed);
7290 gen_reload (second_reload_reg, third_reload_reg,
7291 rl->opnum,
7292 rl->when_needed);
7294 else
7295 gen_reload (second_reload_reg, real_oldequiv,
7296 rl->opnum,
7297 rl->when_needed);
7299 oldequiv = second_reload_reg;
7304 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7306 rtx real_oldequiv = oldequiv;
7308 if ((REG_P (oldequiv)
7309 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7310 && (reg_equiv_memory_loc[REGNO (oldequiv)] != 0
7311 || reg_equiv_constant[REGNO (oldequiv)] != 0))
7312 || (GET_CODE (oldequiv) == SUBREG
7313 && REG_P (SUBREG_REG (oldequiv))
7314 && (REGNO (SUBREG_REG (oldequiv))
7315 >= FIRST_PSEUDO_REGISTER)
7316 && ((reg_equiv_memory_loc
7317 [REGNO (SUBREG_REG (oldequiv))] != 0)
7318 || (reg_equiv_constant
7319 [REGNO (SUBREG_REG (oldequiv))] != 0)))
7320 || (CONSTANT_P (oldequiv)
7321 && (PREFERRED_RELOAD_CLASS (oldequiv,
7322 REGNO_REG_CLASS (REGNO (reloadreg)))
7323 == NO_REGS)))
7324 real_oldequiv = rl->in;
7325 gen_reload (reloadreg, real_oldequiv, rl->opnum,
7326 rl->when_needed);
7329 if (flag_non_call_exceptions)
7330 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7332 /* End this sequence. */
7333 *where = get_insns ();
7334 end_sequence ();
7336 /* Update reload_override_in so that delete_address_reloads_1
7337 can see the actual register usage. */
7338 if (oldequiv_reg)
7339 reload_override_in[j] = oldequiv;
7342 /* Generate insns to for the output reload RL, which is for the insn described
7343 by CHAIN and has the number J. */
7344 static void
7345 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
7346 int j)
7348 rtx reloadreg;
7349 rtx insn = chain->insn;
7350 int special = 0;
7351 rtx old = rl->out;
7352 enum machine_mode mode;
7353 rtx p;
7354 rtx rl_reg_rtx;
7356 if (rl->when_needed == RELOAD_OTHER)
7357 start_sequence ();
7358 else
7359 push_to_sequence (output_reload_insns[rl->opnum]);
7361 rl_reg_rtx = reload_reg_rtx_for_output[j];
7362 mode = GET_MODE (rl_reg_rtx);
7364 reloadreg = rl_reg_rtx;
7366 /* If we need two reload regs, set RELOADREG to the intermediate
7367 one, since it will be stored into OLD. We might need a secondary
7368 register only for an input reload, so check again here. */
7370 if (rl->secondary_out_reload >= 0)
7372 rtx real_old = old;
7373 int secondary_reload = rl->secondary_out_reload;
7374 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
7376 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
7377 && reg_equiv_mem[REGNO (old)] != 0)
7378 real_old = reg_equiv_mem[REGNO (old)];
7380 if (secondary_reload_class (0, rl->rclass, mode, real_old) != NO_REGS)
7382 rtx second_reloadreg = reloadreg;
7383 reloadreg = rld[secondary_reload].reg_rtx;
7385 /* See if RELOADREG is to be used as a scratch register
7386 or as an intermediate register. */
7387 if (rl->secondary_out_icode != CODE_FOR_nothing)
7389 /* We'd have to add extra code to handle this case. */
7390 gcc_assert (tertiary_reload < 0);
7392 emit_insn ((GEN_FCN (rl->secondary_out_icode)
7393 (real_old, second_reloadreg, reloadreg)));
7394 special = 1;
7396 else
7398 /* See if we need both a scratch and intermediate reload
7399 register. */
7401 enum insn_code tertiary_icode
7402 = rld[secondary_reload].secondary_out_icode;
7404 /* We'd have to add more code for quartary reloads. */
7405 gcc_assert (tertiary_reload < 0
7406 || rld[tertiary_reload].secondary_out_reload < 0);
7408 if (GET_MODE (reloadreg) != mode)
7409 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7411 if (tertiary_icode != CODE_FOR_nothing)
7413 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7414 rtx tem;
7416 /* Copy primary reload reg to secondary reload reg.
7417 (Note that these have been swapped above, then
7418 secondary reload reg to OLD using our insn.) */
7420 /* If REAL_OLD is a paradoxical SUBREG, remove it
7421 and try to put the opposite SUBREG on
7422 RELOADREG. */
7423 if (GET_CODE (real_old) == SUBREG
7424 && (GET_MODE_SIZE (GET_MODE (real_old))
7425 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (real_old))))
7426 && 0 != (tem = gen_lowpart_common
7427 (GET_MODE (SUBREG_REG (real_old)),
7428 reloadreg)))
7429 real_old = SUBREG_REG (real_old), reloadreg = tem;
7431 gen_reload (reloadreg, second_reloadreg,
7432 rl->opnum, rl->when_needed);
7433 emit_insn ((GEN_FCN (tertiary_icode)
7434 (real_old, reloadreg, third_reloadreg)));
7435 special = 1;
7438 else
7440 /* Copy between the reload regs here and then to
7441 OUT later. */
7443 gen_reload (reloadreg, second_reloadreg,
7444 rl->opnum, rl->when_needed);
7445 if (tertiary_reload >= 0)
7447 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7449 gen_reload (third_reloadreg, reloadreg,
7450 rl->opnum, rl->when_needed);
7451 reloadreg = third_reloadreg;
7458 /* Output the last reload insn. */
7459 if (! special)
7461 rtx set;
7463 /* Don't output the last reload if OLD is not the dest of
7464 INSN and is in the src and is clobbered by INSN. */
7465 if (! flag_expensive_optimizations
7466 || !REG_P (old)
7467 || !(set = single_set (insn))
7468 || rtx_equal_p (old, SET_DEST (set))
7469 || !reg_mentioned_p (old, SET_SRC (set))
7470 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7471 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7472 gen_reload (old, reloadreg, rl->opnum,
7473 rl->when_needed);
7476 /* Look at all insns we emitted, just to be safe. */
7477 for (p = get_insns (); p; p = NEXT_INSN (p))
7478 if (INSN_P (p))
7480 rtx pat = PATTERN (p);
7482 /* If this output reload doesn't come from a spill reg,
7483 clear any memory of reloaded copies of the pseudo reg.
7484 If this output reload comes from a spill reg,
7485 reg_has_output_reload will make this do nothing. */
7486 note_stores (pat, forget_old_reloads_1, NULL);
7488 if (reg_mentioned_p (rl_reg_rtx, pat))
7490 rtx set = single_set (insn);
7491 if (reload_spill_index[j] < 0
7492 && set
7493 && SET_SRC (set) == rl_reg_rtx)
7495 int src = REGNO (SET_SRC (set));
7497 reload_spill_index[j] = src;
7498 SET_HARD_REG_BIT (reg_is_output_reload, src);
7499 if (find_regno_note (insn, REG_DEAD, src))
7500 SET_HARD_REG_BIT (reg_reloaded_died, src);
7502 if (HARD_REGISTER_P (rl_reg_rtx))
7504 int s = rl->secondary_out_reload;
7505 set = single_set (p);
7506 /* If this reload copies only to the secondary reload
7507 register, the secondary reload does the actual
7508 store. */
7509 if (s >= 0 && set == NULL_RTX)
7510 /* We can't tell what function the secondary reload
7511 has and where the actual store to the pseudo is
7512 made; leave new_spill_reg_store alone. */
7514 else if (s >= 0
7515 && SET_SRC (set) == rl_reg_rtx
7516 && SET_DEST (set) == rld[s].reg_rtx)
7518 /* Usually the next instruction will be the
7519 secondary reload insn; if we can confirm
7520 that it is, setting new_spill_reg_store to
7521 that insn will allow an extra optimization. */
7522 rtx s_reg = rld[s].reg_rtx;
7523 rtx next = NEXT_INSN (p);
7524 rld[s].out = rl->out;
7525 rld[s].out_reg = rl->out_reg;
7526 set = single_set (next);
7527 if (set && SET_SRC (set) == s_reg
7528 && ! new_spill_reg_store[REGNO (s_reg)])
7530 SET_HARD_REG_BIT (reg_is_output_reload,
7531 REGNO (s_reg));
7532 new_spill_reg_store[REGNO (s_reg)] = next;
7535 else
7536 new_spill_reg_store[REGNO (rl_reg_rtx)] = p;
7541 if (rl->when_needed == RELOAD_OTHER)
7543 emit_insn (other_output_reload_insns[rl->opnum]);
7544 other_output_reload_insns[rl->opnum] = get_insns ();
7546 else
7547 output_reload_insns[rl->opnum] = get_insns ();
7549 if (flag_non_call_exceptions)
7550 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7552 end_sequence ();
7555 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7556 and has the number J. */
7557 static void
7558 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7560 rtx insn = chain->insn;
7561 rtx old = (rl->in && MEM_P (rl->in)
7562 ? rl->in_reg : rl->in);
7563 rtx reg_rtx = rl->reg_rtx;
7565 if (old && reg_rtx)
7567 enum machine_mode mode;
7569 /* Determine the mode to reload in.
7570 This is very tricky because we have three to choose from.
7571 There is the mode the insn operand wants (rl->inmode).
7572 There is the mode of the reload register RELOADREG.
7573 There is the intrinsic mode of the operand, which we could find
7574 by stripping some SUBREGs.
7575 It turns out that RELOADREG's mode is irrelevant:
7576 we can change that arbitrarily.
7578 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
7579 then the reload reg may not support QImode moves, so use SImode.
7580 If foo is in memory due to spilling a pseudo reg, this is safe,
7581 because the QImode value is in the least significant part of a
7582 slot big enough for a SImode. If foo is some other sort of
7583 memory reference, then it is impossible to reload this case,
7584 so previous passes had better make sure this never happens.
7586 Then consider a one-word union which has SImode and one of its
7587 members is a float, being fetched as (SUBREG:SF union:SI).
7588 We must fetch that as SFmode because we could be loading into
7589 a float-only register. In this case OLD's mode is correct.
7591 Consider an immediate integer: it has VOIDmode. Here we need
7592 to get a mode from something else.
7594 In some cases, there is a fourth mode, the operand's
7595 containing mode. If the insn specifies a containing mode for
7596 this operand, it overrides all others.
7598 I am not sure whether the algorithm here is always right,
7599 but it does the right things in those cases. */
7601 mode = GET_MODE (old);
7602 if (mode == VOIDmode)
7603 mode = rl->inmode;
7605 /* We cannot use gen_lowpart_common since it can do the wrong thing
7606 when REG_RTX has a multi-word mode. Note that REG_RTX must
7607 always be a REG here. */
7608 if (GET_MODE (reg_rtx) != mode)
7609 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7611 reload_reg_rtx_for_input[j] = reg_rtx;
7613 if (old != 0
7614 /* AUTO_INC reloads need to be handled even if inherited. We got an
7615 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7616 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7617 && ! rtx_equal_p (reg_rtx, old)
7618 && reg_rtx != 0)
7619 emit_input_reload_insns (chain, rld + j, old, j);
7621 /* When inheriting a wider reload, we have a MEM in rl->in,
7622 e.g. inheriting a SImode output reload for
7623 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7624 if (optimize && reload_inherited[j] && rl->in
7625 && MEM_P (rl->in)
7626 && MEM_P (rl->in_reg)
7627 && reload_spill_index[j] >= 0
7628 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7629 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7631 /* If we are reloading a register that was recently stored in with an
7632 output-reload, see if we can prove there was
7633 actually no need to store the old value in it. */
7635 if (optimize
7636 && (reload_inherited[j] || reload_override_in[j])
7637 && reg_rtx
7638 && REG_P (reg_rtx)
7639 && spill_reg_store[REGNO (reg_rtx)] != 0
7640 #if 0
7641 /* There doesn't seem to be any reason to restrict this to pseudos
7642 and doing so loses in the case where we are copying from a
7643 register of the wrong class. */
7644 && !HARD_REGISTER_P (spill_reg_stored_to[REGNO (reg_rtx)])
7645 #endif
7646 /* The insn might have already some references to stackslots
7647 replaced by MEMs, while reload_out_reg still names the
7648 original pseudo. */
7649 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (reg_rtx)])
7650 || rtx_equal_p (spill_reg_stored_to[REGNO (reg_rtx)], rl->out_reg)))
7651 delete_output_reload (insn, j, REGNO (reg_rtx), reg_rtx);
7654 /* Do output reloading for reload RL, which is for the insn described by
7655 CHAIN and has the number J.
7656 ??? At some point we need to support handling output reloads of
7657 JUMP_INSNs or insns that set cc0. */
7658 static void
7659 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7661 rtx note, old;
7662 rtx insn = chain->insn;
7663 /* If this is an output reload that stores something that is
7664 not loaded in this same reload, see if we can eliminate a previous
7665 store. */
7666 rtx pseudo = rl->out_reg;
7667 rtx reg_rtx = rl->reg_rtx;
7669 if (rl->out && reg_rtx)
7671 enum machine_mode mode;
7673 /* Determine the mode to reload in.
7674 See comments above (for input reloading). */
7675 mode = GET_MODE (rl->out);
7676 if (mode == VOIDmode)
7678 /* VOIDmode should never happen for an output. */
7679 if (asm_noperands (PATTERN (insn)) < 0)
7680 /* It's the compiler's fault. */
7681 fatal_insn ("VOIDmode on an output", insn);
7682 error_for_asm (insn, "output operand is constant in %<asm%>");
7683 /* Prevent crash--use something we know is valid. */
7684 mode = word_mode;
7685 rl->out = gen_rtx_REG (mode, REGNO (reg_rtx));
7687 if (GET_MODE (reg_rtx) != mode)
7688 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7690 reload_reg_rtx_for_output[j] = reg_rtx;
7692 if (pseudo
7693 && optimize
7694 && REG_P (pseudo)
7695 && ! rtx_equal_p (rl->in_reg, pseudo)
7696 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7697 && reg_last_reload_reg[REGNO (pseudo)])
7699 int pseudo_no = REGNO (pseudo);
7700 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7702 /* We don't need to test full validity of last_regno for
7703 inherit here; we only want to know if the store actually
7704 matches the pseudo. */
7705 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7706 && reg_reloaded_contents[last_regno] == pseudo_no
7707 && spill_reg_store[last_regno]
7708 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7709 delete_output_reload (insn, j, last_regno, reg_rtx);
7712 old = rl->out_reg;
7713 if (old == 0
7714 || reg_rtx == 0
7715 || rtx_equal_p (old, reg_rtx))
7716 return;
7718 /* An output operand that dies right away does need a reload,
7719 but need not be copied from it. Show the new location in the
7720 REG_UNUSED note. */
7721 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7722 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7724 XEXP (note, 0) = reg_rtx;
7725 return;
7727 /* Likewise for a SUBREG of an operand that dies. */
7728 else if (GET_CODE (old) == SUBREG
7729 && REG_P (SUBREG_REG (old))
7730 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7731 SUBREG_REG (old))))
7733 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old), reg_rtx);
7734 return;
7736 else if (GET_CODE (old) == SCRATCH)
7737 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7738 but we don't want to make an output reload. */
7739 return;
7741 /* If is a JUMP_INSN, we can't support output reloads yet. */
7742 gcc_assert (NONJUMP_INSN_P (insn));
7744 emit_output_reload_insns (chain, rld + j, j);
7747 /* A reload copies values of MODE from register SRC to register DEST.
7748 Return true if it can be treated for inheritance purposes like a
7749 group of reloads, each one reloading a single hard register. The
7750 caller has already checked that (reg:MODE SRC) and (reg:MODE DEST)
7751 occupy the same number of hard registers. */
7753 static bool
7754 inherit_piecemeal_p (int dest ATTRIBUTE_UNUSED,
7755 int src ATTRIBUTE_UNUSED,
7756 enum machine_mode mode ATTRIBUTE_UNUSED)
7758 #ifdef CANNOT_CHANGE_MODE_CLASS
7759 return (!REG_CANNOT_CHANGE_MODE_P (dest, mode, reg_raw_mode[dest])
7760 && !REG_CANNOT_CHANGE_MODE_P (src, mode, reg_raw_mode[src]));
7761 #else
7762 return true;
7763 #endif
7766 /* Output insns to reload values in and out of the chosen reload regs. */
7768 static void
7769 emit_reload_insns (struct insn_chain *chain)
7771 rtx insn = chain->insn;
7773 int j;
7775 CLEAR_HARD_REG_SET (reg_reloaded_died);
7777 for (j = 0; j < reload_n_operands; j++)
7778 input_reload_insns[j] = input_address_reload_insns[j]
7779 = inpaddr_address_reload_insns[j]
7780 = output_reload_insns[j] = output_address_reload_insns[j]
7781 = outaddr_address_reload_insns[j]
7782 = other_output_reload_insns[j] = 0;
7783 other_input_address_reload_insns = 0;
7784 other_input_reload_insns = 0;
7785 operand_reload_insns = 0;
7786 other_operand_reload_insns = 0;
7788 /* Dump reloads into the dump file. */
7789 if (dump_file)
7791 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
7792 debug_reload_to_stream (dump_file);
7795 /* Now output the instructions to copy the data into and out of the
7796 reload registers. Do these in the order that the reloads were reported,
7797 since reloads of base and index registers precede reloads of operands
7798 and the operands may need the base and index registers reloaded. */
7800 for (j = 0; j < n_reloads; j++)
7802 if (rld[j].reg_rtx && HARD_REGISTER_P (rld[j].reg_rtx))
7804 unsigned int i;
7806 for (i = REGNO (rld[j].reg_rtx); i < END_REGNO (rld[j].reg_rtx); i++)
7807 new_spill_reg_store[i] = 0;
7810 do_input_reload (chain, rld + j, j);
7811 do_output_reload (chain, rld + j, j);
7814 /* Now write all the insns we made for reloads in the order expected by
7815 the allocation functions. Prior to the insn being reloaded, we write
7816 the following reloads:
7818 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
7820 RELOAD_OTHER reloads.
7822 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
7823 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
7824 RELOAD_FOR_INPUT reload for the operand.
7826 RELOAD_FOR_OPADDR_ADDRS reloads.
7828 RELOAD_FOR_OPERAND_ADDRESS reloads.
7830 After the insn being reloaded, we write the following:
7832 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
7833 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
7834 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
7835 reloads for the operand. The RELOAD_OTHER output reloads are
7836 output in descending order by reload number. */
7838 emit_insn_before (other_input_address_reload_insns, insn);
7839 emit_insn_before (other_input_reload_insns, insn);
7841 for (j = 0; j < reload_n_operands; j++)
7843 emit_insn_before (inpaddr_address_reload_insns[j], insn);
7844 emit_insn_before (input_address_reload_insns[j], insn);
7845 emit_insn_before (input_reload_insns[j], insn);
7848 emit_insn_before (other_operand_reload_insns, insn);
7849 emit_insn_before (operand_reload_insns, insn);
7851 for (j = 0; j < reload_n_operands; j++)
7853 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
7854 x = emit_insn_after (output_address_reload_insns[j], x);
7855 x = emit_insn_after (output_reload_insns[j], x);
7856 emit_insn_after (other_output_reload_insns[j], x);
7859 /* For all the spill regs newly reloaded in this instruction,
7860 record what they were reloaded from, so subsequent instructions
7861 can inherit the reloads.
7863 Update spill_reg_store for the reloads of this insn.
7864 Copy the elements that were updated in the loop above. */
7866 for (j = 0; j < n_reloads; j++)
7868 int r = reload_order[j];
7869 int i = reload_spill_index[r];
7871 /* If this is a non-inherited input reload from a pseudo, we must
7872 clear any memory of a previous store to the same pseudo. Only do
7873 something if there will not be an output reload for the pseudo
7874 being reloaded. */
7875 if (rld[r].in_reg != 0
7876 && ! (reload_inherited[r] || reload_override_in[r]))
7878 rtx reg = rld[r].in_reg;
7880 if (GET_CODE (reg) == SUBREG)
7881 reg = SUBREG_REG (reg);
7883 if (REG_P (reg)
7884 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
7885 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
7887 int nregno = REGNO (reg);
7889 if (reg_last_reload_reg[nregno])
7891 int last_regno = REGNO (reg_last_reload_reg[nregno]);
7893 if (reg_reloaded_contents[last_regno] == nregno)
7894 spill_reg_store[last_regno] = 0;
7899 /* I is nonneg if this reload used a register.
7900 If rld[r].reg_rtx is 0, this is an optional reload
7901 that we opted to ignore. */
7903 if (i >= 0 && rld[r].reg_rtx != 0)
7905 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
7906 int k;
7908 /* For a multi register reload, we need to check if all or part
7909 of the value lives to the end. */
7910 for (k = 0; k < nr; k++)
7911 if (reload_reg_reaches_end_p (i + k, rld[r].opnum,
7912 rld[r].when_needed))
7913 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
7915 /* Maybe the spill reg contains a copy of reload_out. */
7916 if (rld[r].out != 0
7917 && (REG_P (rld[r].out)
7918 #ifdef AUTO_INC_DEC
7919 || ! rld[r].out_reg
7920 #endif
7921 || REG_P (rld[r].out_reg)))
7923 rtx reg;
7924 enum machine_mode mode;
7925 int regno, nregs;
7927 reg = reload_reg_rtx_for_output[r];
7928 mode = GET_MODE (reg);
7929 regno = REGNO (reg);
7930 nregs = hard_regno_nregs[regno][mode];
7931 if (reload_regs_reach_end_p (regno, nregs, rld[r].opnum,
7932 rld[r].when_needed))
7934 rtx out = (REG_P (rld[r].out)
7935 ? rld[r].out
7936 : rld[r].out_reg
7937 ? rld[r].out_reg
7938 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
7939 int out_regno = REGNO (out);
7940 int out_nregs = (!HARD_REGISTER_NUM_P (out_regno) ? 1
7941 : hard_regno_nregs[out_regno][mode]);
7942 bool piecemeal;
7944 spill_reg_store[regno] = new_spill_reg_store[regno];
7945 spill_reg_stored_to[regno] = out;
7946 reg_last_reload_reg[out_regno] = reg;
7948 piecemeal = (HARD_REGISTER_NUM_P (out_regno)
7949 && nregs == out_nregs
7950 && inherit_piecemeal_p (out_regno, regno, mode));
7952 /* If OUT_REGNO is a hard register, it may occupy more than
7953 one register. If it does, say what is in the
7954 rest of the registers assuming that both registers
7955 agree on how many words the object takes. If not,
7956 invalidate the subsequent registers. */
7958 if (HARD_REGISTER_NUM_P (out_regno))
7959 for (k = 1; k < out_nregs; k++)
7960 reg_last_reload_reg[out_regno + k]
7961 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
7963 /* Now do the inverse operation. */
7964 for (k = 0; k < nregs; k++)
7966 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
7967 reg_reloaded_contents[regno + k]
7968 = (!HARD_REGISTER_NUM_P (out_regno) || !piecemeal
7969 ? out_regno
7970 : out_regno + k);
7971 reg_reloaded_insn[regno + k] = insn;
7972 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
7973 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
7974 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7975 regno + k);
7976 else
7977 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
7978 regno + k);
7982 /* Maybe the spill reg contains a copy of reload_in. Only do
7983 something if there will not be an output reload for
7984 the register being reloaded. */
7985 else if (rld[r].out_reg == 0
7986 && rld[r].in != 0
7987 && ((REG_P (rld[r].in)
7988 && !HARD_REGISTER_P (rld[r].in)
7989 && !REGNO_REG_SET_P (&reg_has_output_reload,
7990 REGNO (rld[r].in)))
7991 || (REG_P (rld[r].in_reg)
7992 && !REGNO_REG_SET_P (&reg_has_output_reload,
7993 REGNO (rld[r].in_reg))))
7994 && !reg_set_p (reload_reg_rtx_for_input[r], PATTERN (insn)))
7996 rtx reg;
7997 enum machine_mode mode;
7998 int regno, nregs;
8000 reg = reload_reg_rtx_for_input[r];
8001 mode = GET_MODE (reg);
8002 regno = REGNO (reg);
8003 nregs = hard_regno_nregs[regno][mode];
8004 if (reload_regs_reach_end_p (regno, nregs, rld[r].opnum,
8005 rld[r].when_needed))
8007 int in_regno;
8008 int in_nregs;
8009 rtx in;
8010 bool piecemeal;
8012 if (REG_P (rld[r].in)
8013 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
8014 in = rld[r].in;
8015 else if (REG_P (rld[r].in_reg))
8016 in = rld[r].in_reg;
8017 else
8018 in = XEXP (rld[r].in_reg, 0);
8019 in_regno = REGNO (in);
8021 in_nregs = (!HARD_REGISTER_NUM_P (in_regno) ? 1
8022 : hard_regno_nregs[in_regno][mode]);
8024 reg_last_reload_reg[in_regno] = reg;
8026 piecemeal = (HARD_REGISTER_NUM_P (in_regno)
8027 && nregs == in_nregs
8028 && inherit_piecemeal_p (regno, in_regno, mode));
8030 if (HARD_REGISTER_NUM_P (in_regno))
8031 for (k = 1; k < in_nregs; k++)
8032 reg_last_reload_reg[in_regno + k]
8033 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
8035 /* Unless we inherited this reload, show we haven't
8036 recently done a store.
8037 Previous stores of inherited auto_inc expressions
8038 also have to be discarded. */
8039 if (! reload_inherited[r]
8040 || (rld[r].out && ! rld[r].out_reg))
8041 spill_reg_store[regno] = 0;
8043 for (k = 0; k < nregs; k++)
8045 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
8046 reg_reloaded_contents[regno + k]
8047 = (!HARD_REGISTER_NUM_P (in_regno) || !piecemeal
8048 ? in_regno
8049 : in_regno + k);
8050 reg_reloaded_insn[regno + k] = insn;
8051 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
8052 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
8053 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8054 regno + k);
8055 else
8056 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8057 regno + k);
8063 /* The following if-statement was #if 0'd in 1.34 (or before...).
8064 It's reenabled in 1.35 because supposedly nothing else
8065 deals with this problem. */
8067 /* If a register gets output-reloaded from a non-spill register,
8068 that invalidates any previous reloaded copy of it.
8069 But forget_old_reloads_1 won't get to see it, because
8070 it thinks only about the original insn. So invalidate it here.
8071 Also do the same thing for RELOAD_OTHER constraints where the
8072 output is discarded. */
8073 if (i < 0
8074 && ((rld[r].out != 0
8075 && (REG_P (rld[r].out)
8076 || (MEM_P (rld[r].out)
8077 && REG_P (rld[r].out_reg))))
8078 || (rld[r].out == 0 && rld[r].out_reg
8079 && REG_P (rld[r].out_reg))))
8081 rtx out = ((rld[r].out && REG_P (rld[r].out))
8082 ? rld[r].out : rld[r].out_reg);
8083 int out_regno = REGNO (out);
8084 enum machine_mode mode = GET_MODE (out);
8086 /* REG_RTX is now set or clobbered by the main instruction.
8087 As the comment above explains, forget_old_reloads_1 only
8088 sees the original instruction, and there is no guarantee
8089 that the original instruction also clobbered REG_RTX.
8090 For example, if find_reloads sees that the input side of
8091 a matched operand pair dies in this instruction, it may
8092 use the input register as the reload register.
8094 Calling forget_old_reloads_1 is a waste of effort if
8095 REG_RTX is also the output register.
8097 If we know that REG_RTX holds the value of a pseudo
8098 register, the code after the call will record that fact. */
8099 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
8100 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
8102 if (!HARD_REGISTER_NUM_P (out_regno))
8104 rtx src_reg, store_insn = NULL_RTX;
8106 reg_last_reload_reg[out_regno] = 0;
8108 /* If we can find a hard register that is stored, record
8109 the storing insn so that we may delete this insn with
8110 delete_output_reload. */
8111 src_reg = reload_reg_rtx_for_output[r];
8113 /* If this is an optional reload, try to find the source reg
8114 from an input reload. */
8115 if (! src_reg)
8117 rtx set = single_set (insn);
8118 if (set && SET_DEST (set) == rld[r].out)
8120 int k;
8122 src_reg = SET_SRC (set);
8123 store_insn = insn;
8124 for (k = 0; k < n_reloads; k++)
8126 if (rld[k].in == src_reg)
8128 src_reg = reload_reg_rtx_for_input[k];
8129 break;
8134 else
8135 store_insn = new_spill_reg_store[REGNO (src_reg)];
8136 if (src_reg && REG_P (src_reg)
8137 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
8139 int src_regno, src_nregs, k;
8140 rtx note;
8142 gcc_assert (GET_MODE (src_reg) == mode);
8143 src_regno = REGNO (src_reg);
8144 src_nregs = hard_regno_nregs[src_regno][mode];
8145 /* The place where to find a death note varies with
8146 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
8147 necessarily checked exactly in the code that moves
8148 notes, so just check both locations. */
8149 note = find_regno_note (insn, REG_DEAD, src_regno);
8150 if (! note && store_insn)
8151 note = find_regno_note (store_insn, REG_DEAD, src_regno);
8152 for (k = 0; k < src_nregs; k++)
8154 spill_reg_store[src_regno + k] = store_insn;
8155 spill_reg_stored_to[src_regno + k] = out;
8156 reg_reloaded_contents[src_regno + k] = out_regno;
8157 reg_reloaded_insn[src_regno + k] = store_insn;
8158 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + k);
8159 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + k);
8160 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + k,
8161 mode))
8162 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8163 src_regno + k);
8164 else
8165 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8166 src_regno + k);
8167 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + k);
8168 if (note)
8169 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
8170 else
8171 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
8173 reg_last_reload_reg[out_regno] = src_reg;
8174 /* We have to set reg_has_output_reload here, or else
8175 forget_old_reloads_1 will clear reg_last_reload_reg
8176 right away. */
8177 SET_REGNO_REG_SET (&reg_has_output_reload,
8178 out_regno);
8181 else
8183 int k, out_nregs = hard_regno_nregs[out_regno][mode];
8185 for (k = 0; k < out_nregs; k++)
8186 reg_last_reload_reg[out_regno + k] = 0;
8190 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
8193 /* Go through the motions to emit INSN and test if it is strictly valid.
8194 Return the emitted insn if valid, else return NULL. */
8196 static rtx
8197 emit_insn_if_valid_for_reload (rtx insn)
8199 rtx last = get_last_insn ();
8200 int code;
8202 insn = emit_insn (insn);
8203 code = recog_memoized (insn);
8205 if (code >= 0)
8207 extract_insn (insn);
8208 /* We want constrain operands to treat this insn strictly in its
8209 validity determination, i.e., the way it would after reload has
8210 completed. */
8211 if (constrain_operands (1))
8212 return insn;
8215 delete_insns_since (last);
8216 return NULL;
8219 /* Emit code to perform a reload from IN (which may be a reload register) to
8220 OUT (which may also be a reload register). IN or OUT is from operand
8221 OPNUM with reload type TYPE.
8223 Returns first insn emitted. */
8225 static rtx
8226 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
8228 rtx last = get_last_insn ();
8229 rtx tem;
8231 /* If IN is a paradoxical SUBREG, remove it and try to put the
8232 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
8233 if (GET_CODE (in) == SUBREG
8234 && (GET_MODE_SIZE (GET_MODE (in))
8235 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (in))))
8236 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (in)), out)) != 0)
8237 in = SUBREG_REG (in), out = tem;
8238 else if (GET_CODE (out) == SUBREG
8239 && (GET_MODE_SIZE (GET_MODE (out))
8240 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (out))))
8241 && (tem = gen_lowpart_common (GET_MODE (SUBREG_REG (out)), in)) != 0)
8242 out = SUBREG_REG (out), in = tem;
8244 /* How to do this reload can get quite tricky. Normally, we are being
8245 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
8246 register that didn't get a hard register. In that case we can just
8247 call emit_move_insn.
8249 We can also be asked to reload a PLUS that adds a register or a MEM to
8250 another register, constant or MEM. This can occur during frame pointer
8251 elimination and while reloading addresses. This case is handled by
8252 trying to emit a single insn to perform the add. If it is not valid,
8253 we use a two insn sequence.
8255 Or we can be asked to reload an unary operand that was a fragment of
8256 an addressing mode, into a register. If it isn't recognized as-is,
8257 we try making the unop operand and the reload-register the same:
8258 (set reg:X (unop:X expr:Y))
8259 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
8261 Finally, we could be called to handle an 'o' constraint by putting
8262 an address into a register. In that case, we first try to do this
8263 with a named pattern of "reload_load_address". If no such pattern
8264 exists, we just emit a SET insn and hope for the best (it will normally
8265 be valid on machines that use 'o').
8267 This entire process is made complex because reload will never
8268 process the insns we generate here and so we must ensure that
8269 they will fit their constraints and also by the fact that parts of
8270 IN might be being reloaded separately and replaced with spill registers.
8271 Because of this, we are, in some sense, just guessing the right approach
8272 here. The one listed above seems to work.
8274 ??? At some point, this whole thing needs to be rethought. */
8276 if (GET_CODE (in) == PLUS
8277 && (REG_P (XEXP (in, 0))
8278 || GET_CODE (XEXP (in, 0)) == SUBREG
8279 || MEM_P (XEXP (in, 0)))
8280 && (REG_P (XEXP (in, 1))
8281 || GET_CODE (XEXP (in, 1)) == SUBREG
8282 || CONSTANT_P (XEXP (in, 1))
8283 || MEM_P (XEXP (in, 1))))
8285 /* We need to compute the sum of a register or a MEM and another
8286 register, constant, or MEM, and put it into the reload
8287 register. The best possible way of doing this is if the machine
8288 has a three-operand ADD insn that accepts the required operands.
8290 The simplest approach is to try to generate such an insn and see if it
8291 is recognized and matches its constraints. If so, it can be used.
8293 It might be better not to actually emit the insn unless it is valid,
8294 but we need to pass the insn as an operand to `recog' and
8295 `extract_insn' and it is simpler to emit and then delete the insn if
8296 not valid than to dummy things up. */
8298 rtx op0, op1, tem, insn;
8299 int code;
8301 op0 = find_replacement (&XEXP (in, 0));
8302 op1 = find_replacement (&XEXP (in, 1));
8304 /* Since constraint checking is strict, commutativity won't be
8305 checked, so we need to do that here to avoid spurious failure
8306 if the add instruction is two-address and the second operand
8307 of the add is the same as the reload reg, which is frequently
8308 the case. If the insn would be A = B + A, rearrange it so
8309 it will be A = A + B as constrain_operands expects. */
8311 if (REG_P (XEXP (in, 1))
8312 && REGNO (out) == REGNO (XEXP (in, 1)))
8313 tem = op0, op0 = op1, op1 = tem;
8315 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
8316 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
8318 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8319 if (insn)
8320 return insn;
8322 /* If that failed, we must use a conservative two-insn sequence.
8324 Use a move to copy one operand into the reload register. Prefer
8325 to reload a constant, MEM or pseudo since the move patterns can
8326 handle an arbitrary operand. If OP1 is not a constant, MEM or
8327 pseudo and OP1 is not a valid operand for an add instruction, then
8328 reload OP1.
8330 After reloading one of the operands into the reload register, add
8331 the reload register to the output register.
8333 If there is another way to do this for a specific machine, a
8334 DEFINE_PEEPHOLE should be specified that recognizes the sequence
8335 we emit below. */
8337 code = (int) optab_handler (add_optab, GET_MODE (out))->insn_code;
8339 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
8340 || (REG_P (op1)
8341 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
8342 || (code != CODE_FOR_nothing
8343 && ! ((*insn_data[code].operand[2].predicate)
8344 (op1, insn_data[code].operand[2].mode))))
8345 tem = op0, op0 = op1, op1 = tem;
8347 gen_reload (out, op0, opnum, type);
8349 /* If OP0 and OP1 are the same, we can use OUT for OP1.
8350 This fixes a problem on the 32K where the stack pointer cannot
8351 be used as an operand of an add insn. */
8353 if (rtx_equal_p (op0, op1))
8354 op1 = out;
8356 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
8357 if (insn)
8359 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
8360 set_unique_reg_note (insn, REG_EQUIV, in);
8361 return insn;
8364 /* If that failed, copy the address register to the reload register.
8365 Then add the constant to the reload register. */
8367 gcc_assert (!reg_overlap_mentioned_p (out, op0));
8368 gen_reload (out, op1, opnum, type);
8369 insn = emit_insn (gen_add2_insn (out, op0));
8370 set_unique_reg_note (insn, REG_EQUIV, in);
8373 #ifdef SECONDARY_MEMORY_NEEDED
8374 /* If we need a memory location to do the move, do it that way. */
8375 else if ((REG_P (in)
8376 || (GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))))
8377 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
8378 && (REG_P (out)
8379 || (GET_CODE (out) == SUBREG && REG_P (SUBREG_REG (out))))
8380 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
8381 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
8382 REGNO_REG_CLASS (reg_or_subregno (out)),
8383 GET_MODE (out)))
8385 /* Get the memory to use and rewrite both registers to its mode. */
8386 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
8388 if (GET_MODE (loc) != GET_MODE (out))
8389 out = gen_rtx_REG (GET_MODE (loc), REGNO (out));
8391 if (GET_MODE (loc) != GET_MODE (in))
8392 in = gen_rtx_REG (GET_MODE (loc), REGNO (in));
8394 gen_reload (loc, in, opnum, type);
8395 gen_reload (out, loc, opnum, type);
8397 #endif
8398 else if (REG_P (out) && UNARY_P (in))
8400 rtx insn;
8401 rtx op1;
8402 rtx out_moded;
8403 rtx set;
8405 op1 = find_replacement (&XEXP (in, 0));
8406 if (op1 != XEXP (in, 0))
8407 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
8409 /* First, try a plain SET. */
8410 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8411 if (set)
8412 return set;
8414 /* If that failed, move the inner operand to the reload
8415 register, and try the same unop with the inner expression
8416 replaced with the reload register. */
8418 if (GET_MODE (op1) != GET_MODE (out))
8419 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
8420 else
8421 out_moded = out;
8423 gen_reload (out_moded, op1, opnum, type);
8425 insn
8426 = gen_rtx_SET (VOIDmode, out,
8427 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
8428 out_moded));
8429 insn = emit_insn_if_valid_for_reload (insn);
8430 if (insn)
8432 set_unique_reg_note (insn, REG_EQUIV, in);
8433 return insn;
8436 fatal_insn ("Failure trying to reload:", set);
8438 /* If IN is a simple operand, use gen_move_insn. */
8439 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8441 tem = emit_insn (gen_move_insn (out, in));
8442 /* IN may contain a LABEL_REF, if so add a REG_LABEL_OPERAND note. */
8443 mark_jump_label (in, tem, 0);
8446 #ifdef HAVE_reload_load_address
8447 else if (HAVE_reload_load_address)
8448 emit_insn (gen_reload_load_address (out, in));
8449 #endif
8451 /* Otherwise, just write (set OUT IN) and hope for the best. */
8452 else
8453 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8455 /* Return the first insn emitted.
8456 We can not just return get_last_insn, because there may have
8457 been multiple instructions emitted. Also note that gen_move_insn may
8458 emit more than one insn itself, so we can not assume that there is one
8459 insn emitted per emit_insn_before call. */
8461 return last ? NEXT_INSN (last) : get_insns ();
8464 /* Delete a previously made output-reload whose result we now believe
8465 is not needed. First we double-check.
8467 INSN is the insn now being processed.
8468 LAST_RELOAD_REG is the hard register number for which we want to delete
8469 the last output reload.
8470 J is the reload-number that originally used REG. The caller has made
8471 certain that reload J doesn't use REG any longer for input.
8472 NEW_RELOAD_REG is reload register that reload J is using for REG. */
8474 static void
8475 delete_output_reload (rtx insn, int j, int last_reload_reg, rtx new_reload_reg)
8477 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8478 rtx reg = spill_reg_stored_to[last_reload_reg];
8479 int k;
8480 int n_occurrences;
8481 int n_inherited = 0;
8482 rtx i1;
8483 rtx substed;
8485 /* It is possible that this reload has been only used to set another reload
8486 we eliminated earlier and thus deleted this instruction too. */
8487 if (INSN_DELETED_P (output_reload_insn))
8488 return;
8490 /* Get the raw pseudo-register referred to. */
8492 while (GET_CODE (reg) == SUBREG)
8493 reg = SUBREG_REG (reg);
8494 substed = reg_equiv_memory_loc[REGNO (reg)];
8496 /* This is unsafe if the operand occurs more often in the current
8497 insn than it is inherited. */
8498 for (k = n_reloads - 1; k >= 0; k--)
8500 rtx reg2 = rld[k].in;
8501 if (! reg2)
8502 continue;
8503 if (MEM_P (reg2) || reload_override_in[k])
8504 reg2 = rld[k].in_reg;
8505 #ifdef AUTO_INC_DEC
8506 if (rld[k].out && ! rld[k].out_reg)
8507 reg2 = XEXP (rld[k].in_reg, 0);
8508 #endif
8509 while (GET_CODE (reg2) == SUBREG)
8510 reg2 = SUBREG_REG (reg2);
8511 if (rtx_equal_p (reg2, reg))
8513 if (reload_inherited[k] || reload_override_in[k] || k == j)
8514 n_inherited++;
8515 else
8516 return;
8519 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
8520 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
8521 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
8522 reg, 0);
8523 if (substed)
8524 n_occurrences += count_occurrences (PATTERN (insn),
8525 eliminate_regs (substed, VOIDmode,
8526 NULL_RTX), 0);
8527 for (i1 = reg_equiv_alt_mem_list[REGNO (reg)]; i1; i1 = XEXP (i1, 1))
8529 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8530 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8532 if (n_occurrences > n_inherited)
8533 return;
8535 /* If the pseudo-reg we are reloading is no longer referenced
8536 anywhere between the store into it and here,
8537 and we're within the same basic block, then the value can only
8538 pass through the reload reg and end up here.
8539 Otherwise, give up--return. */
8540 for (i1 = NEXT_INSN (output_reload_insn);
8541 i1 != insn; i1 = NEXT_INSN (i1))
8543 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8544 return;
8545 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8546 && reg_mentioned_p (reg, PATTERN (i1)))
8548 /* If this is USE in front of INSN, we only have to check that
8549 there are no more references than accounted for by inheritance. */
8550 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8552 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8553 i1 = NEXT_INSN (i1);
8555 if (n_occurrences <= n_inherited && i1 == insn)
8556 break;
8557 return;
8561 /* We will be deleting the insn. Remove the spill reg information. */
8562 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8564 spill_reg_store[last_reload_reg + k] = 0;
8565 spill_reg_stored_to[last_reload_reg + k] = 0;
8568 /* The caller has already checked that REG dies or is set in INSN.
8569 It has also checked that we are optimizing, and thus some
8570 inaccuracies in the debugging information are acceptable.
8571 So we could just delete output_reload_insn. But in some cases
8572 we can improve the debugging information without sacrificing
8573 optimization - maybe even improving the code: See if the pseudo
8574 reg has been completely replaced with reload regs. If so, delete
8575 the store insn and forget we had a stack slot for the pseudo. */
8576 if (rld[j].out != rld[j].in
8577 && REG_N_DEATHS (REGNO (reg)) == 1
8578 && REG_N_SETS (REGNO (reg)) == 1
8579 && REG_BASIC_BLOCK (REGNO (reg)) >= NUM_FIXED_BLOCKS
8580 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8582 rtx i2;
8584 /* We know that it was used only between here and the beginning of
8585 the current basic block. (We also know that the last use before
8586 INSN was the output reload we are thinking of deleting, but never
8587 mind that.) Search that range; see if any ref remains. */
8588 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8590 rtx set = single_set (i2);
8592 /* Uses which just store in the pseudo don't count,
8593 since if they are the only uses, they are dead. */
8594 if (set != 0 && SET_DEST (set) == reg)
8595 continue;
8596 if (LABEL_P (i2)
8597 || JUMP_P (i2))
8598 break;
8599 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8600 && reg_mentioned_p (reg, PATTERN (i2)))
8602 /* Some other ref remains; just delete the output reload we
8603 know to be dead. */
8604 delete_address_reloads (output_reload_insn, insn);
8605 delete_insn (output_reload_insn);
8606 return;
8610 /* Delete the now-dead stores into this pseudo. Note that this
8611 loop also takes care of deleting output_reload_insn. */
8612 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8614 rtx set = single_set (i2);
8616 if (set != 0 && SET_DEST (set) == reg)
8618 delete_address_reloads (i2, insn);
8619 delete_insn (i2);
8621 if (LABEL_P (i2)
8622 || JUMP_P (i2))
8623 break;
8626 /* For the debugging info, say the pseudo lives in this reload reg. */
8627 reg_renumber[REGNO (reg)] = REGNO (new_reload_reg);
8628 if (ira_conflicts_p)
8629 /* Inform IRA about the change. */
8630 ira_mark_allocation_change (REGNO (reg));
8631 alter_reg (REGNO (reg), -1, false);
8633 else
8635 delete_address_reloads (output_reload_insn, insn);
8636 delete_insn (output_reload_insn);
8640 /* We are going to delete DEAD_INSN. Recursively delete loads of
8641 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8642 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8643 static void
8644 delete_address_reloads (rtx dead_insn, rtx current_insn)
8646 rtx set = single_set (dead_insn);
8647 rtx set2, dst, prev, next;
8648 if (set)
8650 rtx dst = SET_DEST (set);
8651 if (MEM_P (dst))
8652 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8654 /* If we deleted the store from a reloaded post_{in,de}c expression,
8655 we can delete the matching adds. */
8656 prev = PREV_INSN (dead_insn);
8657 next = NEXT_INSN (dead_insn);
8658 if (! prev || ! next)
8659 return;
8660 set = single_set (next);
8661 set2 = single_set (prev);
8662 if (! set || ! set2
8663 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8664 || !CONST_INT_P (XEXP (SET_SRC (set), 1))
8665 || !CONST_INT_P (XEXP (SET_SRC (set2), 1)))
8666 return;
8667 dst = SET_DEST (set);
8668 if (! rtx_equal_p (dst, SET_DEST (set2))
8669 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8670 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8671 || (INTVAL (XEXP (SET_SRC (set), 1))
8672 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8673 return;
8674 delete_related_insns (prev);
8675 delete_related_insns (next);
8678 /* Subfunction of delete_address_reloads: process registers found in X. */
8679 static void
8680 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8682 rtx prev, set, dst, i2;
8683 int i, j;
8684 enum rtx_code code = GET_CODE (x);
8686 if (code != REG)
8688 const char *fmt = GET_RTX_FORMAT (code);
8689 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8691 if (fmt[i] == 'e')
8692 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8693 else if (fmt[i] == 'E')
8695 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8696 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8697 current_insn);
8700 return;
8703 if (spill_reg_order[REGNO (x)] < 0)
8704 return;
8706 /* Scan backwards for the insn that sets x. This might be a way back due
8707 to inheritance. */
8708 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8710 code = GET_CODE (prev);
8711 if (code == CODE_LABEL || code == JUMP_INSN)
8712 return;
8713 if (!INSN_P (prev))
8714 continue;
8715 if (reg_set_p (x, PATTERN (prev)))
8716 break;
8717 if (reg_referenced_p (x, PATTERN (prev)))
8718 return;
8720 if (! prev || INSN_UID (prev) < reload_first_uid)
8721 return;
8722 /* Check that PREV only sets the reload register. */
8723 set = single_set (prev);
8724 if (! set)
8725 return;
8726 dst = SET_DEST (set);
8727 if (!REG_P (dst)
8728 || ! rtx_equal_p (dst, x))
8729 return;
8730 if (! reg_set_p (dst, PATTERN (dead_insn)))
8732 /* Check if DST was used in a later insn -
8733 it might have been inherited. */
8734 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8736 if (LABEL_P (i2))
8737 break;
8738 if (! INSN_P (i2))
8739 continue;
8740 if (reg_referenced_p (dst, PATTERN (i2)))
8742 /* If there is a reference to the register in the current insn,
8743 it might be loaded in a non-inherited reload. If no other
8744 reload uses it, that means the register is set before
8745 referenced. */
8746 if (i2 == current_insn)
8748 for (j = n_reloads - 1; j >= 0; j--)
8749 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8750 || reload_override_in[j] == dst)
8751 return;
8752 for (j = n_reloads - 1; j >= 0; j--)
8753 if (rld[j].in && rld[j].reg_rtx == dst)
8754 break;
8755 if (j >= 0)
8756 break;
8758 return;
8760 if (JUMP_P (i2))
8761 break;
8762 /* If DST is still live at CURRENT_INSN, check if it is used for
8763 any reload. Note that even if CURRENT_INSN sets DST, we still
8764 have to check the reloads. */
8765 if (i2 == current_insn)
8767 for (j = n_reloads - 1; j >= 0; j--)
8768 if ((rld[j].reg_rtx == dst && reload_inherited[j])
8769 || reload_override_in[j] == dst)
8770 return;
8771 /* ??? We can't finish the loop here, because dst might be
8772 allocated to a pseudo in this block if no reload in this
8773 block needs any of the classes containing DST - see
8774 spill_hard_reg. There is no easy way to tell this, so we
8775 have to scan till the end of the basic block. */
8777 if (reg_set_p (dst, PATTERN (i2)))
8778 break;
8781 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
8782 reg_reloaded_contents[REGNO (dst)] = -1;
8783 delete_insn (prev);
8786 /* Output reload-insns to reload VALUE into RELOADREG.
8787 VALUE is an autoincrement or autodecrement RTX whose operand
8788 is a register or memory location;
8789 so reloading involves incrementing that location.
8790 IN is either identical to VALUE, or some cheaper place to reload from.
8792 INC_AMOUNT is the number to increment or decrement by (always positive).
8793 This cannot be deduced from VALUE.
8795 Return the instruction that stores into RELOADREG. */
8797 static rtx
8798 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
8800 /* REG or MEM to be copied and incremented. */
8801 rtx incloc = find_replacement (&XEXP (value, 0));
8802 /* Nonzero if increment after copying. */
8803 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
8804 || GET_CODE (value) == POST_MODIFY);
8805 rtx last;
8806 rtx inc;
8807 rtx add_insn;
8808 int code;
8809 rtx store;
8810 rtx real_in = in == value ? incloc : in;
8812 /* No hard register is equivalent to this register after
8813 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
8814 we could inc/dec that register as well (maybe even using it for
8815 the source), but I'm not sure it's worth worrying about. */
8816 if (REG_P (incloc))
8817 reg_last_reload_reg[REGNO (incloc)] = 0;
8819 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
8821 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
8822 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
8824 else
8826 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
8827 inc_amount = -inc_amount;
8829 inc = GEN_INT (inc_amount);
8832 /* If this is post-increment, first copy the location to the reload reg. */
8833 if (post && real_in != reloadreg)
8834 emit_insn (gen_move_insn (reloadreg, real_in));
8836 if (in == value)
8838 /* See if we can directly increment INCLOC. Use a method similar to
8839 that in gen_reload. */
8841 last = get_last_insn ();
8842 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
8843 gen_rtx_PLUS (GET_MODE (incloc),
8844 incloc, inc)));
8846 code = recog_memoized (add_insn);
8847 if (code >= 0)
8849 extract_insn (add_insn);
8850 if (constrain_operands (1))
8852 /* If this is a pre-increment and we have incremented the value
8853 where it lives, copy the incremented value to RELOADREG to
8854 be used as an address. */
8856 if (! post)
8857 emit_insn (gen_move_insn (reloadreg, incloc));
8859 return add_insn;
8862 delete_insns_since (last);
8865 /* If couldn't do the increment directly, must increment in RELOADREG.
8866 The way we do this depends on whether this is pre- or post-increment.
8867 For pre-increment, copy INCLOC to the reload register, increment it
8868 there, then save back. */
8870 if (! post)
8872 if (in != reloadreg)
8873 emit_insn (gen_move_insn (reloadreg, real_in));
8874 emit_insn (gen_add2_insn (reloadreg, inc));
8875 store = emit_insn (gen_move_insn (incloc, reloadreg));
8877 else
8879 /* Postincrement.
8880 Because this might be a jump insn or a compare, and because RELOADREG
8881 may not be available after the insn in an input reload, we must do
8882 the incrementation before the insn being reloaded for.
8884 We have already copied IN to RELOADREG. Increment the copy in
8885 RELOADREG, save that back, then decrement RELOADREG so it has
8886 the original value. */
8888 emit_insn (gen_add2_insn (reloadreg, inc));
8889 store = emit_insn (gen_move_insn (incloc, reloadreg));
8890 if (CONST_INT_P (inc))
8891 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
8892 else
8893 emit_insn (gen_sub2_insn (reloadreg, inc));
8896 return store;
8899 #ifdef AUTO_INC_DEC
8900 static void
8901 add_auto_inc_notes (rtx insn, rtx x)
8903 enum rtx_code code = GET_CODE (x);
8904 const char *fmt;
8905 int i, j;
8907 if (code == MEM && auto_inc_p (XEXP (x, 0)))
8909 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
8910 return;
8913 /* Scan all the operand sub-expressions. */
8914 fmt = GET_RTX_FORMAT (code);
8915 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8917 if (fmt[i] == 'e')
8918 add_auto_inc_notes (insn, XEXP (x, i));
8919 else if (fmt[i] == 'E')
8920 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8921 add_auto_inc_notes (insn, XVECEXP (x, i, j));
8924 #endif
8926 /* This is used by reload pass, that does emit some instructions after
8927 abnormal calls moving basic block end, but in fact it wants to emit
8928 them on the edge. Looks for abnormal call edges, find backward the
8929 proper call and fix the damage.
8931 Similar handle instructions throwing exceptions internally. */
8932 void
8933 fixup_abnormal_edges (void)
8935 bool inserted = false;
8936 basic_block bb;
8938 FOR_EACH_BB (bb)
8940 edge e;
8941 edge_iterator ei;
8943 /* Look for cases we are interested in - calls or instructions causing
8944 exceptions. */
8945 FOR_EACH_EDGE (e, ei, bb->succs)
8947 if (e->flags & EDGE_ABNORMAL_CALL)
8948 break;
8949 if ((e->flags & (EDGE_ABNORMAL | EDGE_EH))
8950 == (EDGE_ABNORMAL | EDGE_EH))
8951 break;
8953 if (e && !CALL_P (BB_END (bb))
8954 && !can_throw_internal (BB_END (bb)))
8956 rtx insn;
8958 /* Get past the new insns generated. Allow notes, as the insns
8959 may be already deleted. */
8960 insn = BB_END (bb);
8961 while ((NONJUMP_INSN_P (insn) || NOTE_P (insn))
8962 && !can_throw_internal (insn)
8963 && insn != BB_HEAD (bb))
8964 insn = PREV_INSN (insn);
8966 if (CALL_P (insn) || can_throw_internal (insn))
8968 rtx stop, next;
8970 stop = NEXT_INSN (BB_END (bb));
8971 BB_END (bb) = insn;
8972 insn = NEXT_INSN (insn);
8974 FOR_EACH_EDGE (e, ei, bb->succs)
8975 if (e->flags & EDGE_FALLTHRU)
8976 break;
8978 while (insn && insn != stop)
8980 next = NEXT_INSN (insn);
8981 if (INSN_P (insn))
8983 delete_insn (insn);
8985 /* Sometimes there's still the return value USE.
8986 If it's placed after a trapping call (i.e. that
8987 call is the last insn anyway), we have no fallthru
8988 edge. Simply delete this use and don't try to insert
8989 on the non-existent edge. */
8990 if (GET_CODE (PATTERN (insn)) != USE)
8992 /* We're not deleting it, we're moving it. */
8993 INSN_DELETED_P (insn) = 0;
8994 PREV_INSN (insn) = NULL_RTX;
8995 NEXT_INSN (insn) = NULL_RTX;
8997 insert_insn_on_edge (insn, e);
8998 inserted = true;
9001 else if (!BARRIER_P (insn))
9002 set_block_for_insn (insn, NULL);
9003 insn = next;
9007 /* It may be that we don't find any such trapping insn. In this
9008 case we discovered quite late that the insn that had been
9009 marked as can_throw_internal in fact couldn't trap at all.
9010 So we should in fact delete the EH edges out of the block. */
9011 else
9012 purge_dead_edges (bb);
9016 /* We've possibly turned single trapping insn into multiple ones. */
9017 if (flag_non_call_exceptions)
9019 sbitmap blocks;
9020 blocks = sbitmap_alloc (last_basic_block);
9021 sbitmap_ones (blocks);
9022 find_many_sub_basic_blocks (blocks);
9023 sbitmap_free (blocks);
9026 if (inserted)
9027 commit_edge_insertions ();
9029 #ifdef ENABLE_CHECKING
9030 /* Verify that we didn't turn one trapping insn into many, and that
9031 we found and corrected all of the problems wrt fixups on the
9032 fallthru edge. */
9033 verify_flow_info ();
9034 #endif