* cpplib.pot: Regenerate.
[official-gcc.git] / gcc / reload1.c
blob1bcdfad9377e6bb0602b9cac5953c75938628d75
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 2011, 2012 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-error.h"
30 #include "tm_p.h"
31 #include "obstack.h"
32 #include "insn-config.h"
33 #include "ggc.h"
34 #include "flags.h"
35 #include "function.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "addresses.h"
40 #include "basic-block.h"
41 #include "df.h"
42 #include "reload.h"
43 #include "recog.h"
44 #include "except.h"
45 #include "tree.h"
46 #include "ira.h"
47 #include "target.h"
48 #include "emit-rtl.h"
49 #include "dumpfile.h"
51 /* This file contains the reload pass of the compiler, which is
52 run after register allocation has been done. It checks that
53 each insn is valid (operands required to be in registers really
54 are in registers of the proper class) and fixes up invalid ones
55 by copying values temporarily into registers for the insns
56 that need them.
58 The results of register allocation are described by the vector
59 reg_renumber; the insns still contain pseudo regs, but reg_renumber
60 can be used to find which hard reg, if any, a pseudo reg is in.
62 The technique we always use is to free up a few hard regs that are
63 called ``reload regs'', and for each place where a pseudo reg
64 must be in a hard reg, copy it temporarily into one of the reload regs.
66 Reload regs are allocated locally for every instruction that needs
67 reloads. When there are pseudos which are allocated to a register that
68 has been chosen as a reload reg, such pseudos must be ``spilled''.
69 This means that they go to other hard regs, or to stack slots if no other
70 available hard regs can be found. Spilling can invalidate more
71 insns, requiring additional need for reloads, so we must keep checking
72 until the process stabilizes.
74 For machines with different classes of registers, we must keep track
75 of the register class needed for each reload, and make sure that
76 we allocate enough reload registers of each class.
78 The file reload.c contains the code that checks one insn for
79 validity and reports the reloads that it needs. This file
80 is in charge of scanning the entire rtl code, accumulating the
81 reload needs, spilling, assigning reload registers to use for
82 fixing up each insn, and generating the new insns to copy values
83 into the reload registers. */
85 struct target_reload default_target_reload;
86 #if SWITCHABLE_TARGET
87 struct target_reload *this_target_reload = &default_target_reload;
88 #endif
90 #define spill_indirect_levels \
91 (this_target_reload->x_spill_indirect_levels)
93 /* During reload_as_needed, element N contains a REG rtx for the hard reg
94 into which reg N has been reloaded (perhaps for a previous insn). */
95 static rtx *reg_last_reload_reg;
97 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
98 for an output reload that stores into reg N. */
99 static regset_head reg_has_output_reload;
101 /* Indicates which hard regs are reload-registers for an output reload
102 in the current insn. */
103 static HARD_REG_SET reg_is_output_reload;
105 /* Widest width in which each pseudo reg is referred to (via subreg). */
106 static unsigned int *reg_max_ref_width;
108 /* Vector to remember old contents of reg_renumber before spilling. */
109 static short *reg_old_renumber;
111 /* During reload_as_needed, element N contains the last pseudo regno reloaded
112 into hard register N. If that pseudo reg occupied more than one register,
113 reg_reloaded_contents points to that pseudo for each spill register in
114 use; all of these must remain set for an inheritance to occur. */
115 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
117 /* During reload_as_needed, element N contains the insn for which
118 hard register N was last used. Its contents are significant only
119 when reg_reloaded_valid is set for this register. */
120 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
122 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
123 static HARD_REG_SET reg_reloaded_valid;
124 /* Indicate if the register was dead at the end of the reload.
125 This is only valid if reg_reloaded_contents is set and valid. */
126 static HARD_REG_SET reg_reloaded_dead;
128 /* Indicate whether the register's current value is one that is not
129 safe to retain across a call, even for registers that are normally
130 call-saved. This is only meaningful for members of reg_reloaded_valid. */
131 static HARD_REG_SET reg_reloaded_call_part_clobbered;
133 /* Number of spill-regs so far; number of valid elements of spill_regs. */
134 static int n_spills;
136 /* In parallel with spill_regs, contains REG rtx's for those regs.
137 Holds the last rtx used for any given reg, or 0 if it has never
138 been used for spilling yet. This rtx is reused, provided it has
139 the proper mode. */
140 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
142 /* In parallel with spill_regs, contains nonzero for a spill reg
143 that was stored after the last time it was used.
144 The precise value is the insn generated to do the store. */
145 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
147 /* This is the register that was stored with spill_reg_store. This is a
148 copy of reload_out / reload_out_reg when the value was stored; if
149 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
150 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
152 /* This table is the inverse mapping of spill_regs:
153 indexed by hard reg number,
154 it contains the position of that reg in spill_regs,
155 or -1 for something that is not in spill_regs.
157 ?!? This is no longer accurate. */
158 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
160 /* This reg set indicates registers that can't be used as spill registers for
161 the currently processed insn. These are the hard registers which are live
162 during the insn, but not allocated to pseudos, as well as fixed
163 registers. */
164 static HARD_REG_SET bad_spill_regs;
166 /* These are the hard registers that can't be used as spill register for any
167 insn. This includes registers used for user variables and registers that
168 we can't eliminate. A register that appears in this set also can't be used
169 to retry register allocation. */
170 static HARD_REG_SET bad_spill_regs_global;
172 /* Describes order of use of registers for reloading
173 of spilled pseudo-registers. `n_spills' is the number of
174 elements that are actually valid; new ones are added at the end.
176 Both spill_regs and spill_reg_order are used on two occasions:
177 once during find_reload_regs, where they keep track of the spill registers
178 for a single insn, but also during reload_as_needed where they show all
179 the registers ever used by reload. For the latter case, the information
180 is calculated during finish_spills. */
181 static short spill_regs[FIRST_PSEUDO_REGISTER];
183 /* This vector of reg sets indicates, for each pseudo, which hard registers
184 may not be used for retrying global allocation because the register was
185 formerly spilled from one of them. If we allowed reallocating a pseudo to
186 a register that it was already allocated to, reload might not
187 terminate. */
188 static HARD_REG_SET *pseudo_previous_regs;
190 /* This vector of reg sets indicates, for each pseudo, which hard
191 registers may not be used for retrying global allocation because they
192 are used as spill registers during one of the insns in which the
193 pseudo is live. */
194 static HARD_REG_SET *pseudo_forbidden_regs;
196 /* All hard regs that have been used as spill registers for any insn are
197 marked in this set. */
198 static HARD_REG_SET used_spill_regs;
200 /* Index of last register assigned as a spill register. We allocate in
201 a round-robin fashion. */
202 static int last_spill_reg;
204 /* Record the stack slot for each spilled hard register. */
205 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
207 /* Width allocated so far for that stack slot. */
208 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
210 /* Record which pseudos needed to be spilled. */
211 static regset_head spilled_pseudos;
213 /* Record which pseudos changed their allocation in finish_spills. */
214 static regset_head changed_allocation_pseudos;
216 /* Used for communication between order_regs_for_reload and count_pseudo.
217 Used to avoid counting one pseudo twice. */
218 static regset_head pseudos_counted;
220 /* First uid used by insns created by reload in this function.
221 Used in find_equiv_reg. */
222 int reload_first_uid;
224 /* Flag set by local-alloc or global-alloc if anything is live in
225 a call-clobbered reg across calls. */
226 int caller_save_needed;
228 /* Set to 1 while reload_as_needed is operating.
229 Required by some machines to handle any generated moves differently. */
230 int reload_in_progress = 0;
232 /* This obstack is used for allocation of rtl during register elimination.
233 The allocated storage can be freed once find_reloads has processed the
234 insn. */
235 static struct obstack reload_obstack;
237 /* Points to the beginning of the reload_obstack. All insn_chain structures
238 are allocated first. */
239 static char *reload_startobj;
241 /* The point after all insn_chain structures. Used to quickly deallocate
242 memory allocated in copy_reloads during calculate_needs_all_insns. */
243 static char *reload_firstobj;
245 /* This points before all local rtl generated by register elimination.
246 Used to quickly free all memory after processing one insn. */
247 static char *reload_insn_firstobj;
249 /* List of insn_chain instructions, one for every insn that reload needs to
250 examine. */
251 struct insn_chain *reload_insn_chain;
253 /* TRUE if we potentially left dead insns in the insn stream and want to
254 run DCE immediately after reload, FALSE otherwise. */
255 static bool need_dce;
257 /* List of all insns needing reloads. */
258 static struct insn_chain *insns_need_reload;
260 /* This structure is used to record information about register eliminations.
261 Each array entry describes one possible way of eliminating a register
262 in favor of another. If there is more than one way of eliminating a
263 particular register, the most preferred should be specified first. */
265 struct elim_table
267 int from; /* Register number to be eliminated. */
268 int to; /* Register number used as replacement. */
269 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
270 int can_eliminate; /* Nonzero if this elimination can be done. */
271 int can_eliminate_previous; /* Value returned by TARGET_CAN_ELIMINATE
272 target hook in previous scan over insns
273 made by reload. */
274 HOST_WIDE_INT offset; /* Current offset between the two regs. */
275 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
276 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
277 rtx from_rtx; /* REG rtx for the register to be eliminated.
278 We cannot simply compare the number since
279 we might then spuriously replace a hard
280 register corresponding to a pseudo
281 assigned to the reg to be eliminated. */
282 rtx to_rtx; /* REG rtx for the replacement. */
285 static struct elim_table *reg_eliminate = 0;
287 /* This is an intermediate structure to initialize the table. It has
288 exactly the members provided by ELIMINABLE_REGS. */
289 static const struct elim_table_1
291 const int from;
292 const int to;
293 } reg_eliminate_1[] =
295 /* If a set of eliminable registers was specified, define the table from it.
296 Otherwise, default to the normal case of the frame pointer being
297 replaced by the stack pointer. */
299 #ifdef ELIMINABLE_REGS
300 ELIMINABLE_REGS;
301 #else
302 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
303 #endif
305 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
307 /* Record the number of pending eliminations that have an offset not equal
308 to their initial offset. If nonzero, we use a new copy of each
309 replacement result in any insns encountered. */
310 int num_not_at_initial_offset;
312 /* Count the number of registers that we may be able to eliminate. */
313 static int num_eliminable;
314 /* And the number of registers that are equivalent to a constant that
315 can be eliminated to frame_pointer / arg_pointer + constant. */
316 static int num_eliminable_invariants;
318 /* For each label, we record the offset of each elimination. If we reach
319 a label by more than one path and an offset differs, we cannot do the
320 elimination. This information is indexed by the difference of the
321 number of the label and the first label number. We can't offset the
322 pointer itself as this can cause problems on machines with segmented
323 memory. The first table is an array of flags that records whether we
324 have yet encountered a label and the second table is an array of arrays,
325 one entry in the latter array for each elimination. */
327 static int first_label_num;
328 static char *offsets_known_at;
329 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
331 VEC(reg_equivs_t,gc) *reg_equivs;
333 /* Stack of addresses where an rtx has been changed. We can undo the
334 changes by popping items off the stack and restoring the original
335 value at each location.
337 We use this simplistic undo capability rather than copy_rtx as copy_rtx
338 will not make a deep copy of a normally sharable rtx, such as
339 (const (plus (symbol_ref) (const_int))). If such an expression appears
340 as R1 in gen_reload_chain_without_interm_reg_p, then a shared
341 rtx expression would be changed. See PR 42431. */
343 typedef rtx *rtx_p;
344 DEF_VEC_P(rtx_p);
345 DEF_VEC_ALLOC_P(rtx_p,heap);
346 static VEC(rtx_p,heap) *substitute_stack;
348 /* Number of labels in the current function. */
350 static int num_labels;
352 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
353 static void maybe_fix_stack_asms (void);
354 static void copy_reloads (struct insn_chain *);
355 static void calculate_needs_all_insns (int);
356 static int find_reg (struct insn_chain *, int);
357 static void find_reload_regs (struct insn_chain *);
358 static void select_reload_regs (void);
359 static void delete_caller_save_insns (void);
361 static void spill_failure (rtx, enum reg_class);
362 static void count_spilled_pseudo (int, int, int);
363 static void delete_dead_insn (rtx);
364 static void alter_reg (int, int, bool);
365 static void set_label_offsets (rtx, rtx, int);
366 static void check_eliminable_occurrences (rtx);
367 static void elimination_effects (rtx, enum machine_mode);
368 static rtx eliminate_regs_1 (rtx, enum machine_mode, rtx, bool, bool);
369 static int eliminate_regs_in_insn (rtx, int);
370 static void update_eliminable_offsets (void);
371 static void mark_not_eliminable (rtx, const_rtx, void *);
372 static void set_initial_elim_offsets (void);
373 static bool verify_initial_elim_offsets (void);
374 static void set_initial_label_offsets (void);
375 static void set_offsets_for_label (rtx);
376 static void init_eliminable_invariants (rtx, bool);
377 static void init_elim_table (void);
378 static void free_reg_equiv (void);
379 static void update_eliminables (HARD_REG_SET *);
380 static void elimination_costs_in_insn (rtx);
381 static void spill_hard_reg (unsigned int, int);
382 static int finish_spills (int);
383 static void scan_paradoxical_subregs (rtx);
384 static void count_pseudo (int);
385 static void order_regs_for_reload (struct insn_chain *);
386 static void reload_as_needed (int);
387 static void forget_old_reloads_1 (rtx, const_rtx, void *);
388 static void forget_marked_reloads (regset);
389 static int reload_reg_class_lower (const void *, const void *);
390 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
391 enum machine_mode);
392 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
393 enum machine_mode);
394 static int reload_reg_free_p (unsigned int, int, enum reload_type);
395 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
396 rtx, rtx, int, int);
397 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
398 rtx, rtx, int, int);
399 static int allocate_reload_reg (struct insn_chain *, int, int);
400 static int conflicts_with_override (rtx);
401 static void failed_reload (rtx, int);
402 static int set_reload_reg (int, int);
403 static void choose_reload_regs_init (struct insn_chain *, rtx *);
404 static void choose_reload_regs (struct insn_chain *);
405 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
406 rtx, int);
407 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
408 int);
409 static void do_input_reload (struct insn_chain *, struct reload *, int);
410 static void do_output_reload (struct insn_chain *, struct reload *, int);
411 static void emit_reload_insns (struct insn_chain *);
412 static void delete_output_reload (rtx, int, int, rtx);
413 static void delete_address_reloads (rtx, rtx);
414 static void delete_address_reloads_1 (rtx, rtx, rtx);
415 static void inc_for_reload (rtx, rtx, rtx, int);
416 #ifdef AUTO_INC_DEC
417 static void add_auto_inc_notes (rtx, rtx);
418 #endif
419 static void substitute (rtx *, const_rtx, rtx);
420 static bool gen_reload_chain_without_interm_reg_p (int, int);
421 static int reloads_conflict (int, int);
422 static rtx gen_reload (rtx, rtx, int, enum reload_type);
423 static rtx emit_insn_if_valid_for_reload (rtx);
425 /* Initialize the reload pass. This is called at the beginning of compilation
426 and may be called again if the target is reinitialized. */
428 void
429 init_reload (void)
431 int i;
433 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
434 Set spill_indirect_levels to the number of levels such addressing is
435 permitted, zero if it is not permitted at all. */
437 rtx tem
438 = gen_rtx_MEM (Pmode,
439 gen_rtx_PLUS (Pmode,
440 gen_rtx_REG (Pmode,
441 LAST_VIRTUAL_REGISTER + 1),
442 GEN_INT (4)));
443 spill_indirect_levels = 0;
445 while (memory_address_p (QImode, tem))
447 spill_indirect_levels++;
448 tem = gen_rtx_MEM (Pmode, tem);
451 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
453 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
454 indirect_symref_ok = memory_address_p (QImode, tem);
456 /* See if reg+reg is a valid (and offsettable) address. */
458 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
460 tem = gen_rtx_PLUS (Pmode,
461 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
462 gen_rtx_REG (Pmode, i));
464 /* This way, we make sure that reg+reg is an offsettable address. */
465 tem = plus_constant (Pmode, tem, 4);
467 if (memory_address_p (QImode, tem))
469 double_reg_address_ok = 1;
470 break;
474 /* Initialize obstack for our rtl allocation. */
475 gcc_obstack_init (&reload_obstack);
476 reload_startobj = XOBNEWVAR (&reload_obstack, char, 0);
478 INIT_REG_SET (&spilled_pseudos);
479 INIT_REG_SET (&changed_allocation_pseudos);
480 INIT_REG_SET (&pseudos_counted);
483 /* List of insn chains that are currently unused. */
484 static struct insn_chain *unused_insn_chains = 0;
486 /* Allocate an empty insn_chain structure. */
487 struct insn_chain *
488 new_insn_chain (void)
490 struct insn_chain *c;
492 if (unused_insn_chains == 0)
494 c = XOBNEW (&reload_obstack, struct insn_chain);
495 INIT_REG_SET (&c->live_throughout);
496 INIT_REG_SET (&c->dead_or_set);
498 else
500 c = unused_insn_chains;
501 unused_insn_chains = c->next;
503 c->is_caller_save_insn = 0;
504 c->need_operand_change = 0;
505 c->need_reload = 0;
506 c->need_elim = 0;
507 return c;
510 /* Small utility function to set all regs in hard reg set TO which are
511 allocated to pseudos in regset FROM. */
513 void
514 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
516 unsigned int regno;
517 reg_set_iterator rsi;
519 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
521 int r = reg_renumber[regno];
523 if (r < 0)
525 /* reload_combine uses the information from DF_LIVE_IN,
526 which might still contain registers that have not
527 actually been allocated since they have an
528 equivalence. */
529 gcc_assert (ira_conflicts_p || reload_completed);
531 else
532 add_to_hard_reg_set (to, PSEUDO_REGNO_MODE (regno), r);
536 /* Replace all pseudos found in LOC with their corresponding
537 equivalences. */
539 static void
540 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
542 rtx x = *loc;
543 enum rtx_code code;
544 const char *fmt;
545 int i, j;
547 if (! x)
548 return;
550 code = GET_CODE (x);
551 if (code == REG)
553 unsigned int regno = REGNO (x);
555 if (regno < FIRST_PSEUDO_REGISTER)
556 return;
558 x = eliminate_regs_1 (x, mem_mode, usage, true, false);
559 if (x != *loc)
561 *loc = x;
562 replace_pseudos_in (loc, mem_mode, usage);
563 return;
566 if (reg_equiv_constant (regno))
567 *loc = reg_equiv_constant (regno);
568 else if (reg_equiv_invariant (regno))
569 *loc = reg_equiv_invariant (regno);
570 else if (reg_equiv_mem (regno))
571 *loc = reg_equiv_mem (regno);
572 else if (reg_equiv_address (regno))
573 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address (regno));
574 else
576 gcc_assert (!REG_P (regno_reg_rtx[regno])
577 || REGNO (regno_reg_rtx[regno]) != regno);
578 *loc = regno_reg_rtx[regno];
581 return;
583 else if (code == MEM)
585 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
586 return;
589 /* Process each of our operands recursively. */
590 fmt = GET_RTX_FORMAT (code);
591 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
592 if (*fmt == 'e')
593 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
594 else if (*fmt == 'E')
595 for (j = 0; j < XVECLEN (x, i); j++)
596 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
599 /* Determine if the current function has an exception receiver block
600 that reaches the exit block via non-exceptional edges */
602 static bool
603 has_nonexceptional_receiver (void)
605 edge e;
606 edge_iterator ei;
607 basic_block *tos, *worklist, bb;
609 /* If we're not optimizing, then just err on the safe side. */
610 if (!optimize)
611 return true;
613 /* First determine which blocks can reach exit via normal paths. */
614 tos = worklist = XNEWVEC (basic_block, n_basic_blocks + 1);
616 FOR_EACH_BB (bb)
617 bb->flags &= ~BB_REACHABLE;
619 /* Place the exit block on our worklist. */
620 EXIT_BLOCK_PTR->flags |= BB_REACHABLE;
621 *tos++ = EXIT_BLOCK_PTR;
623 /* Iterate: find everything reachable from what we've already seen. */
624 while (tos != worklist)
626 bb = *--tos;
628 FOR_EACH_EDGE (e, ei, bb->preds)
629 if (!(e->flags & EDGE_ABNORMAL))
631 basic_block src = e->src;
633 if (!(src->flags & BB_REACHABLE))
635 src->flags |= BB_REACHABLE;
636 *tos++ = src;
640 free (worklist);
642 /* Now see if there's a reachable block with an exceptional incoming
643 edge. */
644 FOR_EACH_BB (bb)
645 if (bb->flags & BB_REACHABLE && bb_has_abnormal_pred (bb))
646 return true;
648 /* No exceptional block reached exit unexceptionally. */
649 return false;
652 /* Grow (or allocate) the REG_EQUIVS array from its current size (which may be
653 zero elements) to MAX_REG_NUM elements.
655 Initialize all new fields to NULL and update REG_EQUIVS_SIZE. */
656 void
657 grow_reg_equivs (void)
659 int old_size = VEC_length (reg_equivs_t, reg_equivs);
660 int max_regno = max_reg_num ();
661 int i;
662 reg_equivs_t ze;
664 memset (&ze, 0, sizeof (reg_equivs_t));
665 VEC_reserve (reg_equivs_t, gc, reg_equivs, max_regno);
666 for (i = old_size; i < max_regno; i++)
667 VEC_quick_insert (reg_equivs_t, reg_equivs, i, ze);
671 /* Global variables used by reload and its subroutines. */
673 /* The current basic block while in calculate_elim_costs_all_insns. */
674 static basic_block elim_bb;
676 /* Set during calculate_needs if an insn needs register elimination. */
677 static int something_needs_elimination;
678 /* Set during calculate_needs if an insn needs an operand changed. */
679 static int something_needs_operands_changed;
680 /* Set by alter_regs if we spilled a register to the stack. */
681 static bool something_was_spilled;
683 /* Nonzero means we couldn't get enough spill regs. */
684 static int failure;
686 /* Temporary array of pseudo-register number. */
687 static int *temp_pseudo_reg_arr;
689 /* Main entry point for the reload pass.
691 FIRST is the first insn of the function being compiled.
693 GLOBAL nonzero means we were called from global_alloc
694 and should attempt to reallocate any pseudoregs that we
695 displace from hard regs we will use for reloads.
696 If GLOBAL is zero, we do not have enough information to do that,
697 so any pseudo reg that is spilled must go to the stack.
699 Return value is TRUE if reload likely left dead insns in the
700 stream and a DCE pass should be run to elimiante them. Else the
701 return value is FALSE. */
703 bool
704 reload (rtx first, int global)
706 int i, n;
707 rtx insn;
708 struct elim_table *ep;
709 basic_block bb;
710 bool inserted;
712 /* Make sure even insns with volatile mem refs are recognizable. */
713 init_recog ();
715 failure = 0;
717 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
719 /* Make sure that the last insn in the chain
720 is not something that needs reloading. */
721 emit_note (NOTE_INSN_DELETED);
723 /* Enable find_equiv_reg to distinguish insns made by reload. */
724 reload_first_uid = get_max_uid ();
726 #ifdef SECONDARY_MEMORY_NEEDED
727 /* Initialize the secondary memory table. */
728 clear_secondary_mem ();
729 #endif
731 /* We don't have a stack slot for any spill reg yet. */
732 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
733 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
735 /* Initialize the save area information for caller-save, in case some
736 are needed. */
737 init_save_areas ();
739 /* Compute which hard registers are now in use
740 as homes for pseudo registers.
741 This is done here rather than (eg) in global_alloc
742 because this point is reached even if not optimizing. */
743 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
744 mark_home_live (i);
746 /* A function that has a nonlocal label that can reach the exit
747 block via non-exceptional paths must save all call-saved
748 registers. */
749 if (cfun->has_nonlocal_label
750 && has_nonexceptional_receiver ())
751 crtl->saves_all_registers = 1;
753 if (crtl->saves_all_registers)
754 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
755 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
756 df_set_regs_ever_live (i, true);
758 /* Find all the pseudo registers that didn't get hard regs
759 but do have known equivalent constants or memory slots.
760 These include parameters (known equivalent to parameter slots)
761 and cse'd or loop-moved constant memory addresses.
763 Record constant equivalents in reg_equiv_constant
764 so they will be substituted by find_reloads.
765 Record memory equivalents in reg_mem_equiv so they can
766 be substituted eventually by altering the REG-rtx's. */
768 grow_reg_equivs ();
769 reg_old_renumber = XCNEWVEC (short, max_regno);
770 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
771 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
772 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
774 CLEAR_HARD_REG_SET (bad_spill_regs_global);
776 init_eliminable_invariants (first, true);
777 init_elim_table ();
779 /* Alter each pseudo-reg rtx to contain its hard reg number. Assign
780 stack slots to the pseudos that lack hard regs or equivalents.
781 Do not touch virtual registers. */
783 temp_pseudo_reg_arr = XNEWVEC (int, max_regno - LAST_VIRTUAL_REGISTER - 1);
784 for (n = 0, i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
785 temp_pseudo_reg_arr[n++] = i;
787 if (ira_conflicts_p)
788 /* Ask IRA to order pseudo-registers for better stack slot
789 sharing. */
790 ira_sort_regnos_for_alter_reg (temp_pseudo_reg_arr, n, reg_max_ref_width);
792 for (i = 0; i < n; i++)
793 alter_reg (temp_pseudo_reg_arr[i], -1, false);
795 /* If we have some registers we think can be eliminated, scan all insns to
796 see if there is an insn that sets one of these registers to something
797 other than itself plus a constant. If so, the register cannot be
798 eliminated. Doing this scan here eliminates an extra pass through the
799 main reload loop in the most common case where register elimination
800 cannot be done. */
801 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
802 if (INSN_P (insn))
803 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
805 maybe_fix_stack_asms ();
807 insns_need_reload = 0;
808 something_needs_elimination = 0;
810 /* Initialize to -1, which means take the first spill register. */
811 last_spill_reg = -1;
813 /* Spill any hard regs that we know we can't eliminate. */
814 CLEAR_HARD_REG_SET (used_spill_regs);
815 /* There can be multiple ways to eliminate a register;
816 they should be listed adjacently.
817 Elimination for any register fails only if all possible ways fail. */
818 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
820 int from = ep->from;
821 int can_eliminate = 0;
824 can_eliminate |= ep->can_eliminate;
825 ep++;
827 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
828 if (! can_eliminate)
829 spill_hard_reg (from, 1);
832 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
833 if (frame_pointer_needed)
834 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
835 #endif
836 finish_spills (global);
838 /* From now on, we may need to generate moves differently. We may also
839 allow modifications of insns which cause them to not be recognized.
840 Any such modifications will be cleaned up during reload itself. */
841 reload_in_progress = 1;
843 /* This loop scans the entire function each go-round
844 and repeats until one repetition spills no additional hard regs. */
845 for (;;)
847 int something_changed;
848 int did_spill;
849 HOST_WIDE_INT starting_frame_size;
851 starting_frame_size = get_frame_size ();
852 something_was_spilled = false;
854 set_initial_elim_offsets ();
855 set_initial_label_offsets ();
857 /* For each pseudo register that has an equivalent location defined,
858 try to eliminate any eliminable registers (such as the frame pointer)
859 assuming initial offsets for the replacement register, which
860 is the normal case.
862 If the resulting location is directly addressable, substitute
863 the MEM we just got directly for the old REG.
865 If it is not addressable but is a constant or the sum of a hard reg
866 and constant, it is probably not addressable because the constant is
867 out of range, in that case record the address; we will generate
868 hairy code to compute the address in a register each time it is
869 needed. Similarly if it is a hard register, but one that is not
870 valid as an address register.
872 If the location is not addressable, but does not have one of the
873 above forms, assign a stack slot. We have to do this to avoid the
874 potential of producing lots of reloads if, e.g., a location involves
875 a pseudo that didn't get a hard register and has an equivalent memory
876 location that also involves a pseudo that didn't get a hard register.
878 Perhaps at some point we will improve reload_when_needed handling
879 so this problem goes away. But that's very hairy. */
881 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
882 if (reg_renumber[i] < 0 && reg_equiv_memory_loc (i))
884 rtx x = eliminate_regs (reg_equiv_memory_loc (i), VOIDmode,
885 NULL_RTX);
887 if (strict_memory_address_addr_space_p
888 (GET_MODE (regno_reg_rtx[i]), XEXP (x, 0),
889 MEM_ADDR_SPACE (x)))
890 reg_equiv_mem (i) = x, reg_equiv_address (i) = 0;
891 else if (CONSTANT_P (XEXP (x, 0))
892 || (REG_P (XEXP (x, 0))
893 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
894 || (GET_CODE (XEXP (x, 0)) == PLUS
895 && REG_P (XEXP (XEXP (x, 0), 0))
896 && (REGNO (XEXP (XEXP (x, 0), 0))
897 < FIRST_PSEUDO_REGISTER)
898 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
899 reg_equiv_address (i) = XEXP (x, 0), reg_equiv_mem (i) = 0;
900 else
902 /* Make a new stack slot. Then indicate that something
903 changed so we go back and recompute offsets for
904 eliminable registers because the allocation of memory
905 below might change some offset. reg_equiv_{mem,address}
906 will be set up for this pseudo on the next pass around
907 the loop. */
908 reg_equiv_memory_loc (i) = 0;
909 reg_equiv_init (i) = 0;
910 alter_reg (i, -1, true);
914 if (caller_save_needed)
915 setup_save_areas ();
917 /* If we allocated another stack slot, redo elimination bookkeeping. */
918 if (something_was_spilled || starting_frame_size != get_frame_size ())
919 continue;
920 if (starting_frame_size && crtl->stack_alignment_needed)
922 /* If we have a stack frame, we must align it now. The
923 stack size may be a part of the offset computation for
924 register elimination. So if this changes the stack size,
925 then repeat the elimination bookkeeping. We don't
926 realign when there is no stack, as that will cause a
927 stack frame when none is needed should
928 STARTING_FRAME_OFFSET not be already aligned to
929 STACK_BOUNDARY. */
930 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
931 if (starting_frame_size != get_frame_size ())
932 continue;
935 if (caller_save_needed)
937 save_call_clobbered_regs ();
938 /* That might have allocated new insn_chain structures. */
939 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
942 calculate_needs_all_insns (global);
944 if (! ira_conflicts_p)
945 /* Don't do it for IRA. We need this info because we don't
946 change live_throughout and dead_or_set for chains when IRA
947 is used. */
948 CLEAR_REG_SET (&spilled_pseudos);
950 did_spill = 0;
952 something_changed = 0;
954 /* If we allocated any new memory locations, make another pass
955 since it might have changed elimination offsets. */
956 if (something_was_spilled || starting_frame_size != get_frame_size ())
957 something_changed = 1;
959 /* Even if the frame size remained the same, we might still have
960 changed elimination offsets, e.g. if find_reloads called
961 force_const_mem requiring the back end to allocate a constant
962 pool base register that needs to be saved on the stack. */
963 else if (!verify_initial_elim_offsets ())
964 something_changed = 1;
967 HARD_REG_SET to_spill;
968 CLEAR_HARD_REG_SET (to_spill);
969 update_eliminables (&to_spill);
970 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
972 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
973 if (TEST_HARD_REG_BIT (to_spill, i))
975 spill_hard_reg (i, 1);
976 did_spill = 1;
978 /* Regardless of the state of spills, if we previously had
979 a register that we thought we could eliminate, but now can
980 not eliminate, we must run another pass.
982 Consider pseudos which have an entry in reg_equiv_* which
983 reference an eliminable register. We must make another pass
984 to update reg_equiv_* so that we do not substitute in the
985 old value from when we thought the elimination could be
986 performed. */
987 something_changed = 1;
991 select_reload_regs ();
992 if (failure)
993 goto failed;
995 if (insns_need_reload != 0 || did_spill)
996 something_changed |= finish_spills (global);
998 if (! something_changed)
999 break;
1001 if (caller_save_needed)
1002 delete_caller_save_insns ();
1004 obstack_free (&reload_obstack, reload_firstobj);
1007 /* If global-alloc was run, notify it of any register eliminations we have
1008 done. */
1009 if (global)
1010 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1011 if (ep->can_eliminate)
1012 mark_elimination (ep->from, ep->to);
1014 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1015 If that insn didn't set the register (i.e., it copied the register to
1016 memory), just delete that insn instead of the equivalencing insn plus
1017 anything now dead. If we call delete_dead_insn on that insn, we may
1018 delete the insn that actually sets the register if the register dies
1019 there and that is incorrect. */
1021 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1023 if (reg_renumber[i] < 0 && reg_equiv_init (i) != 0)
1025 rtx list;
1026 for (list = reg_equiv_init (i); list; list = XEXP (list, 1))
1028 rtx equiv_insn = XEXP (list, 0);
1030 /* If we already deleted the insn or if it may trap, we can't
1031 delete it. The latter case shouldn't happen, but can
1032 if an insn has a variable address, gets a REG_EH_REGION
1033 note added to it, and then gets converted into a load
1034 from a constant address. */
1035 if (NOTE_P (equiv_insn)
1036 || can_throw_internal (equiv_insn))
1038 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1039 delete_dead_insn (equiv_insn);
1040 else
1041 SET_INSN_DELETED (equiv_insn);
1046 /* Use the reload registers where necessary
1047 by generating move instructions to move the must-be-register
1048 values into or out of the reload registers. */
1050 if (insns_need_reload != 0 || something_needs_elimination
1051 || something_needs_operands_changed)
1053 HOST_WIDE_INT old_frame_size = get_frame_size ();
1055 reload_as_needed (global);
1057 gcc_assert (old_frame_size == get_frame_size ());
1059 gcc_assert (verify_initial_elim_offsets ());
1062 /* If we were able to eliminate the frame pointer, show that it is no
1063 longer live at the start of any basic block. If it ls live by
1064 virtue of being in a pseudo, that pseudo will be marked live
1065 and hence the frame pointer will be known to be live via that
1066 pseudo. */
1068 if (! frame_pointer_needed)
1069 FOR_EACH_BB (bb)
1070 bitmap_clear_bit (df_get_live_in (bb), HARD_FRAME_POINTER_REGNUM);
1072 /* Come here (with failure set nonzero) if we can't get enough spill
1073 regs. */
1074 failed:
1076 CLEAR_REG_SET (&changed_allocation_pseudos);
1077 CLEAR_REG_SET (&spilled_pseudos);
1078 reload_in_progress = 0;
1080 /* Now eliminate all pseudo regs by modifying them into
1081 their equivalent memory references.
1082 The REG-rtx's for the pseudos are modified in place,
1083 so all insns that used to refer to them now refer to memory.
1085 For a reg that has a reg_equiv_address, all those insns
1086 were changed by reloading so that no insns refer to it any longer;
1087 but the DECL_RTL of a variable decl may refer to it,
1088 and if so this causes the debugging info to mention the variable. */
1090 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1092 rtx addr = 0;
1094 if (reg_equiv_mem (i))
1095 addr = XEXP (reg_equiv_mem (i), 0);
1097 if (reg_equiv_address (i))
1098 addr = reg_equiv_address (i);
1100 if (addr)
1102 if (reg_renumber[i] < 0)
1104 rtx reg = regno_reg_rtx[i];
1106 REG_USERVAR_P (reg) = 0;
1107 PUT_CODE (reg, MEM);
1108 XEXP (reg, 0) = addr;
1109 if (reg_equiv_memory_loc (i))
1110 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc (i));
1111 else
1112 MEM_ATTRS (reg) = 0;
1113 MEM_NOTRAP_P (reg) = 1;
1115 else if (reg_equiv_mem (i))
1116 XEXP (reg_equiv_mem (i), 0) = addr;
1119 /* We don't want complex addressing modes in debug insns
1120 if simpler ones will do, so delegitimize equivalences
1121 in debug insns. */
1122 if (MAY_HAVE_DEBUG_INSNS && reg_renumber[i] < 0)
1124 rtx reg = regno_reg_rtx[i];
1125 rtx equiv = 0;
1126 df_ref use, next;
1128 if (reg_equiv_constant (i))
1129 equiv = reg_equiv_constant (i);
1130 else if (reg_equiv_invariant (i))
1131 equiv = reg_equiv_invariant (i);
1132 else if (reg && MEM_P (reg))
1133 equiv = targetm.delegitimize_address (reg);
1134 else if (reg && REG_P (reg) && (int)REGNO (reg) != i)
1135 equiv = reg;
1137 if (equiv == reg)
1138 continue;
1140 for (use = DF_REG_USE_CHAIN (i); use; use = next)
1142 insn = DF_REF_INSN (use);
1144 /* Make sure the next ref is for a different instruction,
1145 so that we're not affected by the rescan. */
1146 next = DF_REF_NEXT_REG (use);
1147 while (next && DF_REF_INSN (next) == insn)
1148 next = DF_REF_NEXT_REG (next);
1150 if (DEBUG_INSN_P (insn))
1152 if (!equiv)
1154 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
1155 df_insn_rescan_debug_internal (insn);
1157 else
1158 INSN_VAR_LOCATION_LOC (insn)
1159 = simplify_replace_rtx (INSN_VAR_LOCATION_LOC (insn),
1160 reg, equiv);
1166 /* We must set reload_completed now since the cleanup_subreg_operands call
1167 below will re-recognize each insn and reload may have generated insns
1168 which are only valid during and after reload. */
1169 reload_completed = 1;
1171 /* Make a pass over all the insns and delete all USEs which we inserted
1172 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1173 notes. Delete all CLOBBER insns, except those that refer to the return
1174 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1175 from misarranging variable-array code, and simplify (subreg (reg))
1176 operands. Strip and regenerate REG_INC notes that may have been moved
1177 around. */
1179 for (insn = first; insn; insn = NEXT_INSN (insn))
1180 if (INSN_P (insn))
1182 rtx *pnote;
1184 if (CALL_P (insn))
1185 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1186 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1188 if ((GET_CODE (PATTERN (insn)) == USE
1189 /* We mark with QImode USEs introduced by reload itself. */
1190 && (GET_MODE (insn) == QImode
1191 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1192 || (GET_CODE (PATTERN (insn)) == CLOBBER
1193 && (!MEM_P (XEXP (PATTERN (insn), 0))
1194 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1195 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1196 && XEXP (XEXP (PATTERN (insn), 0), 0)
1197 != stack_pointer_rtx))
1198 && (!REG_P (XEXP (PATTERN (insn), 0))
1199 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1201 delete_insn (insn);
1202 continue;
1205 /* Some CLOBBERs may survive until here and still reference unassigned
1206 pseudos with const equivalent, which may in turn cause ICE in later
1207 passes if the reference remains in place. */
1208 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1209 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1210 VOIDmode, PATTERN (insn));
1212 /* Discard obvious no-ops, even without -O. This optimization
1213 is fast and doesn't interfere with debugging. */
1214 if (NONJUMP_INSN_P (insn)
1215 && GET_CODE (PATTERN (insn)) == SET
1216 && REG_P (SET_SRC (PATTERN (insn)))
1217 && REG_P (SET_DEST (PATTERN (insn)))
1218 && (REGNO (SET_SRC (PATTERN (insn)))
1219 == REGNO (SET_DEST (PATTERN (insn)))))
1221 delete_insn (insn);
1222 continue;
1225 pnote = &REG_NOTES (insn);
1226 while (*pnote != 0)
1228 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1229 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1230 || REG_NOTE_KIND (*pnote) == REG_INC)
1231 *pnote = XEXP (*pnote, 1);
1232 else
1233 pnote = &XEXP (*pnote, 1);
1236 #ifdef AUTO_INC_DEC
1237 add_auto_inc_notes (insn, PATTERN (insn));
1238 #endif
1240 /* Simplify (subreg (reg)) if it appears as an operand. */
1241 cleanup_subreg_operands (insn);
1243 /* Clean up invalid ASMs so that they don't confuse later passes.
1244 See PR 21299. */
1245 if (asm_noperands (PATTERN (insn)) >= 0)
1247 extract_insn (insn);
1248 if (!constrain_operands (1))
1250 error_for_asm (insn,
1251 "%<asm%> operand has impossible constraints");
1252 delete_insn (insn);
1253 continue;
1258 /* If we are doing generic stack checking, give a warning if this
1259 function's frame size is larger than we expect. */
1260 if (flag_stack_check == GENERIC_STACK_CHECK)
1262 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1263 static int verbose_warned = 0;
1265 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1266 if (df_regs_ever_live_p (i) && ! fixed_regs[i] && call_used_regs[i])
1267 size += UNITS_PER_WORD;
1269 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1271 warning (0, "frame size too large for reliable stack checking");
1272 if (! verbose_warned)
1274 warning (0, "try reducing the number of local variables");
1275 verbose_warned = 1;
1280 free (temp_pseudo_reg_arr);
1282 /* Indicate that we no longer have known memory locations or constants. */
1283 free_reg_equiv ();
1285 free (reg_max_ref_width);
1286 free (reg_old_renumber);
1287 free (pseudo_previous_regs);
1288 free (pseudo_forbidden_regs);
1290 CLEAR_HARD_REG_SET (used_spill_regs);
1291 for (i = 0; i < n_spills; i++)
1292 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1294 /* Free all the insn_chain structures at once. */
1295 obstack_free (&reload_obstack, reload_startobj);
1296 unused_insn_chains = 0;
1298 inserted = fixup_abnormal_edges ();
1300 /* We've possibly turned single trapping insn into multiple ones. */
1301 if (cfun->can_throw_non_call_exceptions)
1303 sbitmap blocks;
1304 blocks = sbitmap_alloc (last_basic_block);
1305 sbitmap_ones (blocks);
1306 find_many_sub_basic_blocks (blocks);
1307 sbitmap_free (blocks);
1310 if (inserted)
1311 commit_edge_insertions ();
1313 /* Replacing pseudos with their memory equivalents might have
1314 created shared rtx. Subsequent passes would get confused
1315 by this, so unshare everything here. */
1316 unshare_all_rtl_again (first);
1318 #ifdef STACK_BOUNDARY
1319 /* init_emit has set the alignment of the hard frame pointer
1320 to STACK_BOUNDARY. It is very likely no longer valid if
1321 the hard frame pointer was used for register allocation. */
1322 if (!frame_pointer_needed)
1323 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1324 #endif
1326 VEC_free (rtx_p, heap, substitute_stack);
1328 gcc_assert (bitmap_empty_p (&spilled_pseudos));
1330 reload_completed = !failure;
1332 return need_dce;
1335 /* Yet another special case. Unfortunately, reg-stack forces people to
1336 write incorrect clobbers in asm statements. These clobbers must not
1337 cause the register to appear in bad_spill_regs, otherwise we'll call
1338 fatal_insn later. We clear the corresponding regnos in the live
1339 register sets to avoid this.
1340 The whole thing is rather sick, I'm afraid. */
1342 static void
1343 maybe_fix_stack_asms (void)
1345 #ifdef STACK_REGS
1346 const char *constraints[MAX_RECOG_OPERANDS];
1347 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1348 struct insn_chain *chain;
1350 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1352 int i, noperands;
1353 HARD_REG_SET clobbered, allowed;
1354 rtx pat;
1356 if (! INSN_P (chain->insn)
1357 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1358 continue;
1359 pat = PATTERN (chain->insn);
1360 if (GET_CODE (pat) != PARALLEL)
1361 continue;
1363 CLEAR_HARD_REG_SET (clobbered);
1364 CLEAR_HARD_REG_SET (allowed);
1366 /* First, make a mask of all stack regs that are clobbered. */
1367 for (i = 0; i < XVECLEN (pat, 0); i++)
1369 rtx t = XVECEXP (pat, 0, i);
1370 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1371 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1374 /* Get the operand values and constraints out of the insn. */
1375 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1376 constraints, operand_mode, NULL);
1378 /* For every operand, see what registers are allowed. */
1379 for (i = 0; i < noperands; i++)
1381 const char *p = constraints[i];
1382 /* For every alternative, we compute the class of registers allowed
1383 for reloading in CLS, and merge its contents into the reg set
1384 ALLOWED. */
1385 int cls = (int) NO_REGS;
1387 for (;;)
1389 char c = *p;
1391 if (c == '\0' || c == ',' || c == '#')
1393 /* End of one alternative - mark the regs in the current
1394 class, and reset the class. */
1395 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1396 cls = NO_REGS;
1397 p++;
1398 if (c == '#')
1399 do {
1400 c = *p++;
1401 } while (c != '\0' && c != ',');
1402 if (c == '\0')
1403 break;
1404 continue;
1407 switch (c)
1409 case '=': case '+': case '*': case '%': case '?': case '!':
1410 case '0': case '1': case '2': case '3': case '4': case '<':
1411 case '>': case 'V': case 'o': case '&': case 'E': case 'F':
1412 case 's': case 'i': case 'n': case 'X': case 'I': case 'J':
1413 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P':
1414 case TARGET_MEM_CONSTRAINT:
1415 break;
1417 case 'p':
1418 cls = (int) reg_class_subunion[cls]
1419 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1420 ADDRESS, SCRATCH)];
1421 break;
1423 case 'g':
1424 case 'r':
1425 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1426 break;
1428 default:
1429 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1430 cls = (int) reg_class_subunion[cls]
1431 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1432 ADDRESS, SCRATCH)];
1433 else
1434 cls = (int) reg_class_subunion[cls]
1435 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1437 p += CONSTRAINT_LEN (c, p);
1440 /* Those of the registers which are clobbered, but allowed by the
1441 constraints, must be usable as reload registers. So clear them
1442 out of the life information. */
1443 AND_HARD_REG_SET (allowed, clobbered);
1444 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1445 if (TEST_HARD_REG_BIT (allowed, i))
1447 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1448 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1452 #endif
1455 /* Copy the global variables n_reloads and rld into the corresponding elts
1456 of CHAIN. */
1457 static void
1458 copy_reloads (struct insn_chain *chain)
1460 chain->n_reloads = n_reloads;
1461 chain->rld = XOBNEWVEC (&reload_obstack, struct reload, n_reloads);
1462 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1463 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1466 /* Walk the chain of insns, and determine for each whether it needs reloads
1467 and/or eliminations. Build the corresponding insns_need_reload list, and
1468 set something_needs_elimination as appropriate. */
1469 static void
1470 calculate_needs_all_insns (int global)
1472 struct insn_chain **pprev_reload = &insns_need_reload;
1473 struct insn_chain *chain, *next = 0;
1475 something_needs_elimination = 0;
1477 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1478 for (chain = reload_insn_chain; chain != 0; chain = next)
1480 rtx insn = chain->insn;
1482 next = chain->next;
1484 /* Clear out the shortcuts. */
1485 chain->n_reloads = 0;
1486 chain->need_elim = 0;
1487 chain->need_reload = 0;
1488 chain->need_operand_change = 0;
1490 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1491 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1492 what effects this has on the known offsets at labels. */
1494 if (LABEL_P (insn) || JUMP_P (insn)
1495 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1496 set_label_offsets (insn, insn, 0);
1498 if (INSN_P (insn))
1500 rtx old_body = PATTERN (insn);
1501 int old_code = INSN_CODE (insn);
1502 rtx old_notes = REG_NOTES (insn);
1503 int did_elimination = 0;
1504 int operands_changed = 0;
1505 rtx set = single_set (insn);
1507 /* Skip insns that only set an equivalence. */
1508 if (set && REG_P (SET_DEST (set))
1509 && reg_renumber[REGNO (SET_DEST (set))] < 0
1510 && (reg_equiv_constant (REGNO (SET_DEST (set)))
1511 || (reg_equiv_invariant (REGNO (SET_DEST (set)))))
1512 && reg_equiv_init (REGNO (SET_DEST (set))))
1513 continue;
1515 /* If needed, eliminate any eliminable registers. */
1516 if (num_eliminable || num_eliminable_invariants)
1517 did_elimination = eliminate_regs_in_insn (insn, 0);
1519 /* Analyze the instruction. */
1520 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1521 global, spill_reg_order);
1523 /* If a no-op set needs more than one reload, this is likely
1524 to be something that needs input address reloads. We
1525 can't get rid of this cleanly later, and it is of no use
1526 anyway, so discard it now.
1527 We only do this when expensive_optimizations is enabled,
1528 since this complements reload inheritance / output
1529 reload deletion, and it can make debugging harder. */
1530 if (flag_expensive_optimizations && n_reloads > 1)
1532 rtx set = single_set (insn);
1533 if (set
1535 ((SET_SRC (set) == SET_DEST (set)
1536 && REG_P (SET_SRC (set))
1537 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1538 || (REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
1539 && reg_renumber[REGNO (SET_SRC (set))] < 0
1540 && reg_renumber[REGNO (SET_DEST (set))] < 0
1541 && reg_equiv_memory_loc (REGNO (SET_SRC (set))) != NULL
1542 && reg_equiv_memory_loc (REGNO (SET_DEST (set))) != NULL
1543 && rtx_equal_p (reg_equiv_memory_loc (REGNO (SET_SRC (set))),
1544 reg_equiv_memory_loc (REGNO (SET_DEST (set)))))))
1546 if (ira_conflicts_p)
1547 /* Inform IRA about the insn deletion. */
1548 ira_mark_memory_move_deletion (REGNO (SET_DEST (set)),
1549 REGNO (SET_SRC (set)));
1550 delete_insn (insn);
1551 /* Delete it from the reload chain. */
1552 if (chain->prev)
1553 chain->prev->next = next;
1554 else
1555 reload_insn_chain = next;
1556 if (next)
1557 next->prev = chain->prev;
1558 chain->next = unused_insn_chains;
1559 unused_insn_chains = chain;
1560 continue;
1563 if (num_eliminable)
1564 update_eliminable_offsets ();
1566 /* Remember for later shortcuts which insns had any reloads or
1567 register eliminations. */
1568 chain->need_elim = did_elimination;
1569 chain->need_reload = n_reloads > 0;
1570 chain->need_operand_change = operands_changed;
1572 /* Discard any register replacements done. */
1573 if (did_elimination)
1575 obstack_free (&reload_obstack, reload_insn_firstobj);
1576 PATTERN (insn) = old_body;
1577 INSN_CODE (insn) = old_code;
1578 REG_NOTES (insn) = old_notes;
1579 something_needs_elimination = 1;
1582 something_needs_operands_changed |= operands_changed;
1584 if (n_reloads != 0)
1586 copy_reloads (chain);
1587 *pprev_reload = chain;
1588 pprev_reload = &chain->next_need_reload;
1592 *pprev_reload = 0;
1595 /* This function is called from the register allocator to set up estimates
1596 for the cost of eliminating pseudos which have REG_EQUIV equivalences to
1597 an invariant. The structure is similar to calculate_needs_all_insns. */
1599 void
1600 calculate_elim_costs_all_insns (void)
1602 int *reg_equiv_init_cost;
1603 basic_block bb;
1604 int i;
1606 reg_equiv_init_cost = XCNEWVEC (int, max_regno);
1607 init_elim_table ();
1608 init_eliminable_invariants (get_insns (), false);
1610 set_initial_elim_offsets ();
1611 set_initial_label_offsets ();
1613 FOR_EACH_BB (bb)
1615 rtx insn;
1616 elim_bb = bb;
1618 FOR_BB_INSNS (bb, insn)
1620 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1621 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1622 what effects this has on the known offsets at labels. */
1624 if (LABEL_P (insn) || JUMP_P (insn)
1625 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1626 set_label_offsets (insn, insn, 0);
1628 if (INSN_P (insn))
1630 rtx set = single_set (insn);
1632 /* Skip insns that only set an equivalence. */
1633 if (set && REG_P (SET_DEST (set))
1634 && reg_renumber[REGNO (SET_DEST (set))] < 0
1635 && (reg_equiv_constant (REGNO (SET_DEST (set)))
1636 || reg_equiv_invariant (REGNO (SET_DEST (set)))))
1638 unsigned regno = REGNO (SET_DEST (set));
1639 rtx init = reg_equiv_init (regno);
1640 if (init)
1642 rtx t = eliminate_regs_1 (SET_SRC (set), VOIDmode, insn,
1643 false, true);
1644 int cost = set_src_cost (t, optimize_bb_for_speed_p (bb));
1645 int freq = REG_FREQ_FROM_BB (bb);
1647 reg_equiv_init_cost[regno] = cost * freq;
1648 continue;
1651 /* If needed, eliminate any eliminable registers. */
1652 if (num_eliminable || num_eliminable_invariants)
1653 elimination_costs_in_insn (insn);
1655 if (num_eliminable)
1656 update_eliminable_offsets ();
1660 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1662 if (reg_equiv_invariant (i))
1664 if (reg_equiv_init (i))
1666 int cost = reg_equiv_init_cost[i];
1667 if (dump_file)
1668 fprintf (dump_file,
1669 "Reg %d has equivalence, initial gains %d\n", i, cost);
1670 if (cost != 0)
1671 ira_adjust_equiv_reg_cost (i, cost);
1673 else
1675 if (dump_file)
1676 fprintf (dump_file,
1677 "Reg %d had equivalence, but can't be eliminated\n",
1679 ira_adjust_equiv_reg_cost (i, 0);
1684 free (reg_equiv_init_cost);
1685 free (offsets_known_at);
1686 free (offsets_at);
1687 offsets_at = NULL;
1688 offsets_known_at = NULL;
1691 /* Comparison function for qsort to decide which of two reloads
1692 should be handled first. *P1 and *P2 are the reload numbers. */
1694 static int
1695 reload_reg_class_lower (const void *r1p, const void *r2p)
1697 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1698 int t;
1700 /* Consider required reloads before optional ones. */
1701 t = rld[r1].optional - rld[r2].optional;
1702 if (t != 0)
1703 return t;
1705 /* Count all solitary classes before non-solitary ones. */
1706 t = ((reg_class_size[(int) rld[r2].rclass] == 1)
1707 - (reg_class_size[(int) rld[r1].rclass] == 1));
1708 if (t != 0)
1709 return t;
1711 /* Aside from solitaires, consider all multi-reg groups first. */
1712 t = rld[r2].nregs - rld[r1].nregs;
1713 if (t != 0)
1714 return t;
1716 /* Consider reloads in order of increasing reg-class number. */
1717 t = (int) rld[r1].rclass - (int) rld[r2].rclass;
1718 if (t != 0)
1719 return t;
1721 /* If reloads are equally urgent, sort by reload number,
1722 so that the results of qsort leave nothing to chance. */
1723 return r1 - r2;
1726 /* The cost of spilling each hard reg. */
1727 static int spill_cost[FIRST_PSEUDO_REGISTER];
1729 /* When spilling multiple hard registers, we use SPILL_COST for the first
1730 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1731 only the first hard reg for a multi-reg pseudo. */
1732 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1734 /* Map of hard regno to pseudo regno currently occupying the hard
1735 reg. */
1736 static int hard_regno_to_pseudo_regno[FIRST_PSEUDO_REGISTER];
1738 /* Update the spill cost arrays, considering that pseudo REG is live. */
1740 static void
1741 count_pseudo (int reg)
1743 int freq = REG_FREQ (reg);
1744 int r = reg_renumber[reg];
1745 int nregs;
1747 /* Ignore spilled pseudo-registers which can be here only if IRA is used. */
1748 if (ira_conflicts_p && r < 0)
1749 return;
1751 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1752 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1753 return;
1755 SET_REGNO_REG_SET (&pseudos_counted, reg);
1757 gcc_assert (r >= 0);
1759 spill_add_cost[r] += freq;
1760 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1761 while (nregs-- > 0)
1763 hard_regno_to_pseudo_regno[r + nregs] = reg;
1764 spill_cost[r + nregs] += freq;
1768 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1769 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1771 static void
1772 order_regs_for_reload (struct insn_chain *chain)
1774 unsigned i;
1775 HARD_REG_SET used_by_pseudos;
1776 HARD_REG_SET used_by_pseudos2;
1777 reg_set_iterator rsi;
1779 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1781 memset (spill_cost, 0, sizeof spill_cost);
1782 memset (spill_add_cost, 0, sizeof spill_add_cost);
1783 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1784 hard_regno_to_pseudo_regno[i] = -1;
1786 /* Count number of uses of each hard reg by pseudo regs allocated to it
1787 and then order them by decreasing use. First exclude hard registers
1788 that are live in or across this insn. */
1790 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1791 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1792 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1793 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1795 /* Now find out which pseudos are allocated to it, and update
1796 hard_reg_n_uses. */
1797 CLEAR_REG_SET (&pseudos_counted);
1799 EXECUTE_IF_SET_IN_REG_SET
1800 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1802 count_pseudo (i);
1804 EXECUTE_IF_SET_IN_REG_SET
1805 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1807 count_pseudo (i);
1809 CLEAR_REG_SET (&pseudos_counted);
1812 /* Vector of reload-numbers showing the order in which the reloads should
1813 be processed. */
1814 static short reload_order[MAX_RELOADS];
1816 /* This is used to keep track of the spill regs used in one insn. */
1817 static HARD_REG_SET used_spill_regs_local;
1819 /* We decided to spill hard register SPILLED, which has a size of
1820 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1821 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1822 update SPILL_COST/SPILL_ADD_COST. */
1824 static void
1825 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1827 int freq = REG_FREQ (reg);
1828 int r = reg_renumber[reg];
1829 int nregs;
1831 /* Ignore spilled pseudo-registers which can be here only if IRA is used. */
1832 if (ira_conflicts_p && r < 0)
1833 return;
1835 gcc_assert (r >= 0);
1837 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1839 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1840 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1841 return;
1843 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1845 spill_add_cost[r] -= freq;
1846 while (nregs-- > 0)
1848 hard_regno_to_pseudo_regno[r + nregs] = -1;
1849 spill_cost[r + nregs] -= freq;
1853 /* Find reload register to use for reload number ORDER. */
1855 static int
1856 find_reg (struct insn_chain *chain, int order)
1858 int rnum = reload_order[order];
1859 struct reload *rl = rld + rnum;
1860 int best_cost = INT_MAX;
1861 int best_reg = -1;
1862 unsigned int i, j, n;
1863 int k;
1864 HARD_REG_SET not_usable;
1865 HARD_REG_SET used_by_other_reload;
1866 reg_set_iterator rsi;
1867 static int regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1868 static int best_regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1870 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1871 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1872 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->rclass]);
1874 CLEAR_HARD_REG_SET (used_by_other_reload);
1875 for (k = 0; k < order; k++)
1877 int other = reload_order[k];
1879 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1880 for (j = 0; j < rld[other].nregs; j++)
1881 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1884 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1886 #ifdef REG_ALLOC_ORDER
1887 unsigned int regno = reg_alloc_order[i];
1888 #else
1889 unsigned int regno = i;
1890 #endif
1892 if (! TEST_HARD_REG_BIT (not_usable, regno)
1893 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1894 && HARD_REGNO_MODE_OK (regno, rl->mode))
1896 int this_cost = spill_cost[regno];
1897 int ok = 1;
1898 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1900 for (j = 1; j < this_nregs; j++)
1902 this_cost += spill_add_cost[regno + j];
1903 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1904 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1905 ok = 0;
1907 if (! ok)
1908 continue;
1910 if (ira_conflicts_p)
1912 /* Ask IRA to find a better pseudo-register for
1913 spilling. */
1914 for (n = j = 0; j < this_nregs; j++)
1916 int r = hard_regno_to_pseudo_regno[regno + j];
1918 if (r < 0)
1919 continue;
1920 if (n == 0 || regno_pseudo_regs[n - 1] != r)
1921 regno_pseudo_regs[n++] = r;
1923 regno_pseudo_regs[n++] = -1;
1924 if (best_reg < 0
1925 || ira_better_spill_reload_regno_p (regno_pseudo_regs,
1926 best_regno_pseudo_regs,
1927 rl->in, rl->out,
1928 chain->insn))
1930 best_reg = regno;
1931 for (j = 0;; j++)
1933 best_regno_pseudo_regs[j] = regno_pseudo_regs[j];
1934 if (regno_pseudo_regs[j] < 0)
1935 break;
1938 continue;
1941 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1942 this_cost--;
1943 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1944 this_cost--;
1945 if (this_cost < best_cost
1946 /* Among registers with equal cost, prefer caller-saved ones, or
1947 use REG_ALLOC_ORDER if it is defined. */
1948 || (this_cost == best_cost
1949 #ifdef REG_ALLOC_ORDER
1950 && (inv_reg_alloc_order[regno]
1951 < inv_reg_alloc_order[best_reg])
1952 #else
1953 && call_used_regs[regno]
1954 && ! call_used_regs[best_reg]
1955 #endif
1958 best_reg = regno;
1959 best_cost = this_cost;
1963 if (best_reg == -1)
1964 return 0;
1966 if (dump_file)
1967 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1969 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1970 rl->regno = best_reg;
1972 EXECUTE_IF_SET_IN_REG_SET
1973 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1975 count_spilled_pseudo (best_reg, rl->nregs, j);
1978 EXECUTE_IF_SET_IN_REG_SET
1979 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1981 count_spilled_pseudo (best_reg, rl->nregs, j);
1984 for (i = 0; i < rl->nregs; i++)
1986 gcc_assert (spill_cost[best_reg + i] == 0);
1987 gcc_assert (spill_add_cost[best_reg + i] == 0);
1988 gcc_assert (hard_regno_to_pseudo_regno[best_reg + i] == -1);
1989 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1991 return 1;
1994 /* Find more reload regs to satisfy the remaining need of an insn, which
1995 is given by CHAIN.
1996 Do it by ascending class number, since otherwise a reg
1997 might be spilled for a big class and might fail to count
1998 for a smaller class even though it belongs to that class. */
2000 static void
2001 find_reload_regs (struct insn_chain *chain)
2003 int i;
2005 /* In order to be certain of getting the registers we need,
2006 we must sort the reloads into order of increasing register class.
2007 Then our grabbing of reload registers will parallel the process
2008 that provided the reload registers. */
2009 for (i = 0; i < chain->n_reloads; i++)
2011 /* Show whether this reload already has a hard reg. */
2012 if (chain->rld[i].reg_rtx)
2014 int regno = REGNO (chain->rld[i].reg_rtx);
2015 chain->rld[i].regno = regno;
2016 chain->rld[i].nregs
2017 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
2019 else
2020 chain->rld[i].regno = -1;
2021 reload_order[i] = i;
2024 n_reloads = chain->n_reloads;
2025 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
2027 CLEAR_HARD_REG_SET (used_spill_regs_local);
2029 if (dump_file)
2030 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
2032 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
2034 /* Compute the order of preference for hard registers to spill. */
2036 order_regs_for_reload (chain);
2038 for (i = 0; i < n_reloads; i++)
2040 int r = reload_order[i];
2042 /* Ignore reloads that got marked inoperative. */
2043 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
2044 && ! rld[r].optional
2045 && rld[r].regno == -1)
2046 if (! find_reg (chain, i))
2048 if (dump_file)
2049 fprintf (dump_file, "reload failure for reload %d\n", r);
2050 spill_failure (chain->insn, rld[r].rclass);
2051 failure = 1;
2052 return;
2056 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
2057 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
2059 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
2062 static void
2063 select_reload_regs (void)
2065 struct insn_chain *chain;
2067 /* Try to satisfy the needs for each insn. */
2068 for (chain = insns_need_reload; chain != 0;
2069 chain = chain->next_need_reload)
2070 find_reload_regs (chain);
2073 /* Delete all insns that were inserted by emit_caller_save_insns during
2074 this iteration. */
2075 static void
2076 delete_caller_save_insns (void)
2078 struct insn_chain *c = reload_insn_chain;
2080 while (c != 0)
2082 while (c != 0 && c->is_caller_save_insn)
2084 struct insn_chain *next = c->next;
2085 rtx insn = c->insn;
2087 if (c == reload_insn_chain)
2088 reload_insn_chain = next;
2089 delete_insn (insn);
2091 if (next)
2092 next->prev = c->prev;
2093 if (c->prev)
2094 c->prev->next = next;
2095 c->next = unused_insn_chains;
2096 unused_insn_chains = c;
2097 c = next;
2099 if (c != 0)
2100 c = c->next;
2104 /* Handle the failure to find a register to spill.
2105 INSN should be one of the insns which needed this particular spill reg. */
2107 static void
2108 spill_failure (rtx insn, enum reg_class rclass)
2110 if (asm_noperands (PATTERN (insn)) >= 0)
2111 error_for_asm (insn, "can%'t find a register in class %qs while "
2112 "reloading %<asm%>",
2113 reg_class_names[rclass]);
2114 else
2116 error ("unable to find a register to spill in class %qs",
2117 reg_class_names[rclass]);
2119 if (dump_file)
2121 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2122 debug_reload_to_stream (dump_file);
2124 fatal_insn ("this is the insn:", insn);
2128 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2129 data that is dead in INSN. */
2131 static void
2132 delete_dead_insn (rtx insn)
2134 rtx prev = prev_active_insn (insn);
2135 rtx prev_dest;
2137 /* If the previous insn sets a register that dies in our insn make
2138 a note that we want to run DCE immediately after reload.
2140 We used to delete the previous insn & recurse, but that's wrong for
2141 block local equivalences. Instead of trying to figure out the exact
2142 circumstances where we can delete the potentially dead insns, just
2143 let DCE do the job. */
2144 if (prev && GET_CODE (PATTERN (prev)) == SET
2145 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2146 && reg_mentioned_p (prev_dest, PATTERN (insn))
2147 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2148 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2149 need_dce = 1;
2151 SET_INSN_DELETED (insn);
2154 /* Modify the home of pseudo-reg I.
2155 The new home is present in reg_renumber[I].
2157 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2158 or it may be -1, meaning there is none or it is not relevant.
2159 This is used so that all pseudos spilled from a given hard reg
2160 can share one stack slot. */
2162 static void
2163 alter_reg (int i, int from_reg, bool dont_share_p)
2165 /* When outputting an inline function, this can happen
2166 for a reg that isn't actually used. */
2167 if (regno_reg_rtx[i] == 0)
2168 return;
2170 /* If the reg got changed to a MEM at rtl-generation time,
2171 ignore it. */
2172 if (!REG_P (regno_reg_rtx[i]))
2173 return;
2175 /* Modify the reg-rtx to contain the new hard reg
2176 number or else to contain its pseudo reg number. */
2177 SET_REGNO (regno_reg_rtx[i],
2178 reg_renumber[i] >= 0 ? reg_renumber[i] : i);
2180 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2181 allocate a stack slot for it. */
2183 if (reg_renumber[i] < 0
2184 && REG_N_REFS (i) > 0
2185 && reg_equiv_constant (i) == 0
2186 && (reg_equiv_invariant (i) == 0
2187 || reg_equiv_init (i) == 0)
2188 && reg_equiv_memory_loc (i) == 0)
2190 rtx x = NULL_RTX;
2191 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2192 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2193 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2194 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2195 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2196 int adjust = 0;
2198 something_was_spilled = true;
2200 if (ira_conflicts_p)
2202 /* Mark the spill for IRA. */
2203 SET_REGNO_REG_SET (&spilled_pseudos, i);
2204 if (!dont_share_p)
2205 x = ira_reuse_stack_slot (i, inherent_size, total_size);
2208 if (x)
2211 /* Each pseudo reg has an inherent size which comes from its own mode,
2212 and a total size which provides room for paradoxical subregs
2213 which refer to the pseudo reg in wider modes.
2215 We can use a slot already allocated if it provides both
2216 enough inherent space and enough total space.
2217 Otherwise, we allocate a new slot, making sure that it has no less
2218 inherent space, and no less total space, then the previous slot. */
2219 else if (from_reg == -1 || (!dont_share_p && ira_conflicts_p))
2221 rtx stack_slot;
2223 /* No known place to spill from => no slot to reuse. */
2224 x = assign_stack_local (mode, total_size,
2225 min_align > inherent_align
2226 || total_size > inherent_size ? -1 : 0);
2228 stack_slot = x;
2230 /* Cancel the big-endian correction done in assign_stack_local.
2231 Get the address of the beginning of the slot. This is so we
2232 can do a big-endian correction unconditionally below. */
2233 if (BYTES_BIG_ENDIAN)
2235 adjust = inherent_size - total_size;
2236 if (adjust)
2237 stack_slot
2238 = adjust_address_nv (x, mode_for_size (total_size
2239 * BITS_PER_UNIT,
2240 MODE_INT, 1),
2241 adjust);
2244 if (! dont_share_p && ira_conflicts_p)
2245 /* Inform IRA about allocation a new stack slot. */
2246 ira_mark_new_stack_slot (stack_slot, i, total_size);
2249 /* Reuse a stack slot if possible. */
2250 else if (spill_stack_slot[from_reg] != 0
2251 && spill_stack_slot_width[from_reg] >= total_size
2252 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2253 >= inherent_size)
2254 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2255 x = spill_stack_slot[from_reg];
2257 /* Allocate a bigger slot. */
2258 else
2260 /* Compute maximum size needed, both for inherent size
2261 and for total size. */
2262 rtx stack_slot;
2264 if (spill_stack_slot[from_reg])
2266 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2267 > inherent_size)
2268 mode = GET_MODE (spill_stack_slot[from_reg]);
2269 if (spill_stack_slot_width[from_reg] > total_size)
2270 total_size = spill_stack_slot_width[from_reg];
2271 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2272 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2275 /* Make a slot with that size. */
2276 x = assign_stack_local (mode, total_size,
2277 min_align > inherent_align
2278 || total_size > inherent_size ? -1 : 0);
2279 stack_slot = x;
2281 /* Cancel the big-endian correction done in assign_stack_local.
2282 Get the address of the beginning of the slot. This is so we
2283 can do a big-endian correction unconditionally below. */
2284 if (BYTES_BIG_ENDIAN)
2286 adjust = GET_MODE_SIZE (mode) - total_size;
2287 if (adjust)
2288 stack_slot
2289 = adjust_address_nv (x, mode_for_size (total_size
2290 * BITS_PER_UNIT,
2291 MODE_INT, 1),
2292 adjust);
2295 spill_stack_slot[from_reg] = stack_slot;
2296 spill_stack_slot_width[from_reg] = total_size;
2299 /* On a big endian machine, the "address" of the slot
2300 is the address of the low part that fits its inherent mode. */
2301 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2302 adjust += (total_size - inherent_size);
2304 /* If we have any adjustment to make, or if the stack slot is the
2305 wrong mode, make a new stack slot. */
2306 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2308 /* Set all of the memory attributes as appropriate for a spill. */
2309 set_mem_attrs_for_spill (x);
2311 /* Save the stack slot for later. */
2312 reg_equiv_memory_loc (i) = x;
2316 /* Mark the slots in regs_ever_live for the hard regs used by
2317 pseudo-reg number REGNO, accessed in MODE. */
2319 static void
2320 mark_home_live_1 (int regno, enum machine_mode mode)
2322 int i, lim;
2324 i = reg_renumber[regno];
2325 if (i < 0)
2326 return;
2327 lim = end_hard_regno (mode, i);
2328 while (i < lim)
2329 df_set_regs_ever_live(i++, true);
2332 /* Mark the slots in regs_ever_live for the hard regs
2333 used by pseudo-reg number REGNO. */
2335 void
2336 mark_home_live (int regno)
2338 if (reg_renumber[regno] >= 0)
2339 mark_home_live_1 (regno, PSEUDO_REGNO_MODE (regno));
2342 /* This function handles the tracking of elimination offsets around branches.
2344 X is a piece of RTL being scanned.
2346 INSN is the insn that it came from, if any.
2348 INITIAL_P is nonzero if we are to set the offset to be the initial
2349 offset and zero if we are setting the offset of the label to be the
2350 current offset. */
2352 static void
2353 set_label_offsets (rtx x, rtx insn, int initial_p)
2355 enum rtx_code code = GET_CODE (x);
2356 rtx tem;
2357 unsigned int i;
2358 struct elim_table *p;
2360 switch (code)
2362 case LABEL_REF:
2363 if (LABEL_REF_NONLOCAL_P (x))
2364 return;
2366 x = XEXP (x, 0);
2368 /* ... fall through ... */
2370 case CODE_LABEL:
2371 /* If we know nothing about this label, set the desired offsets. Note
2372 that this sets the offset at a label to be the offset before a label
2373 if we don't know anything about the label. This is not correct for
2374 the label after a BARRIER, but is the best guess we can make. If
2375 we guessed wrong, we will suppress an elimination that might have
2376 been possible had we been able to guess correctly. */
2378 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2380 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2381 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2382 = (initial_p ? reg_eliminate[i].initial_offset
2383 : reg_eliminate[i].offset);
2384 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2387 /* Otherwise, if this is the definition of a label and it is
2388 preceded by a BARRIER, set our offsets to the known offset of
2389 that label. */
2391 else if (x == insn
2392 && (tem = prev_nonnote_insn (insn)) != 0
2393 && BARRIER_P (tem))
2394 set_offsets_for_label (insn);
2395 else
2396 /* If neither of the above cases is true, compare each offset
2397 with those previously recorded and suppress any eliminations
2398 where the offsets disagree. */
2400 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2401 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2402 != (initial_p ? reg_eliminate[i].initial_offset
2403 : reg_eliminate[i].offset))
2404 reg_eliminate[i].can_eliminate = 0;
2406 return;
2408 case JUMP_INSN:
2409 set_label_offsets (PATTERN (insn), insn, initial_p);
2411 /* ... fall through ... */
2413 case INSN:
2414 case CALL_INSN:
2415 /* Any labels mentioned in REG_LABEL_OPERAND notes can be branched
2416 to indirectly and hence must have all eliminations at their
2417 initial offsets. */
2418 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2419 if (REG_NOTE_KIND (tem) == REG_LABEL_OPERAND)
2420 set_label_offsets (XEXP (tem, 0), insn, 1);
2421 return;
2423 case PARALLEL:
2424 case ADDR_VEC:
2425 case ADDR_DIFF_VEC:
2426 /* Each of the labels in the parallel or address vector must be
2427 at their initial offsets. We want the first field for PARALLEL
2428 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2430 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2431 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2432 insn, initial_p);
2433 return;
2435 case SET:
2436 /* We only care about setting PC. If the source is not RETURN,
2437 IF_THEN_ELSE, or a label, disable any eliminations not at
2438 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2439 isn't one of those possibilities. For branches to a label,
2440 call ourselves recursively.
2442 Note that this can disable elimination unnecessarily when we have
2443 a non-local goto since it will look like a non-constant jump to
2444 someplace in the current function. This isn't a significant
2445 problem since such jumps will normally be when all elimination
2446 pairs are back to their initial offsets. */
2448 if (SET_DEST (x) != pc_rtx)
2449 return;
2451 switch (GET_CODE (SET_SRC (x)))
2453 case PC:
2454 case RETURN:
2455 return;
2457 case LABEL_REF:
2458 set_label_offsets (SET_SRC (x), insn, initial_p);
2459 return;
2461 case IF_THEN_ELSE:
2462 tem = XEXP (SET_SRC (x), 1);
2463 if (GET_CODE (tem) == LABEL_REF)
2464 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2465 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2466 break;
2468 tem = XEXP (SET_SRC (x), 2);
2469 if (GET_CODE (tem) == LABEL_REF)
2470 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2471 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2472 break;
2473 return;
2475 default:
2476 break;
2479 /* If we reach here, all eliminations must be at their initial
2480 offset because we are doing a jump to a variable address. */
2481 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2482 if (p->offset != p->initial_offset)
2483 p->can_eliminate = 0;
2484 break;
2486 default:
2487 break;
2491 /* Called through for_each_rtx, this function examines every reg that occurs
2492 in PX and adjusts the costs for its elimination which are gathered by IRA.
2493 DATA is the insn in which PX occurs. We do not recurse into MEM
2494 expressions. */
2496 static int
2497 note_reg_elim_costly (rtx *px, void *data)
2499 rtx insn = (rtx)data;
2500 rtx x = *px;
2502 if (MEM_P (x))
2503 return -1;
2505 if (REG_P (x)
2506 && REGNO (x) >= FIRST_PSEUDO_REGISTER
2507 && reg_equiv_init (REGNO (x))
2508 && reg_equiv_invariant (REGNO (x)))
2510 rtx t = reg_equiv_invariant (REGNO (x));
2511 rtx new_rtx = eliminate_regs_1 (t, Pmode, insn, true, true);
2512 int cost = set_src_cost (new_rtx, optimize_bb_for_speed_p (elim_bb));
2513 int freq = REG_FREQ_FROM_BB (elim_bb);
2515 if (cost != 0)
2516 ira_adjust_equiv_reg_cost (REGNO (x), -cost * freq);
2518 return 0;
2521 /* Scan X and replace any eliminable registers (such as fp) with a
2522 replacement (such as sp), plus an offset.
2524 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2525 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2526 MEM, we are allowed to replace a sum of a register and the constant zero
2527 with the register, which we cannot do outside a MEM. In addition, we need
2528 to record the fact that a register is referenced outside a MEM.
2530 If INSN is an insn, it is the insn containing X. If we replace a REG
2531 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2532 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2533 the REG is being modified.
2535 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2536 That's used when we eliminate in expressions stored in notes.
2537 This means, do not set ref_outside_mem even if the reference
2538 is outside of MEMs.
2540 If FOR_COSTS is true, we are being called before reload in order to
2541 estimate the costs of keeping registers with an equivalence unallocated.
2543 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2544 replacements done assuming all offsets are at their initial values. If
2545 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2546 encounter, return the actual location so that find_reloads will do
2547 the proper thing. */
2549 static rtx
2550 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2551 bool may_use_invariant, bool for_costs)
2553 enum rtx_code code = GET_CODE (x);
2554 struct elim_table *ep;
2555 int regno;
2556 rtx new_rtx;
2557 int i, j;
2558 const char *fmt;
2559 int copied = 0;
2561 if (! current_function_decl)
2562 return x;
2564 switch (code)
2566 CASE_CONST_ANY:
2567 case CONST:
2568 case SYMBOL_REF:
2569 case CODE_LABEL:
2570 case PC:
2571 case CC0:
2572 case ASM_INPUT:
2573 case ADDR_VEC:
2574 case ADDR_DIFF_VEC:
2575 case RETURN:
2576 return x;
2578 case REG:
2579 regno = REGNO (x);
2581 /* First handle the case where we encounter a bare register that
2582 is eliminable. Replace it with a PLUS. */
2583 if (regno < FIRST_PSEUDO_REGISTER)
2585 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2586 ep++)
2587 if (ep->from_rtx == x && ep->can_eliminate)
2588 return plus_constant (Pmode, ep->to_rtx, ep->previous_offset);
2591 else if (reg_renumber && reg_renumber[regno] < 0
2592 && reg_equivs
2593 && reg_equiv_invariant (regno))
2595 if (may_use_invariant || (insn && DEBUG_INSN_P (insn)))
2596 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant (regno)),
2597 mem_mode, insn, true, for_costs);
2598 /* There exists at least one use of REGNO that cannot be
2599 eliminated. Prevent the defining insn from being deleted. */
2600 reg_equiv_init (regno) = NULL_RTX;
2601 if (!for_costs)
2602 alter_reg (regno, -1, true);
2604 return x;
2606 /* You might think handling MINUS in a manner similar to PLUS is a
2607 good idea. It is not. It has been tried multiple times and every
2608 time the change has had to have been reverted.
2610 Other parts of reload know a PLUS is special (gen_reload for example)
2611 and require special code to handle code a reloaded PLUS operand.
2613 Also consider backends where the flags register is clobbered by a
2614 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2615 lea instruction comes to mind). If we try to reload a MINUS, we
2616 may kill the flags register that was holding a useful value.
2618 So, please before trying to handle MINUS, consider reload as a
2619 whole instead of this little section as well as the backend issues. */
2620 case PLUS:
2621 /* If this is the sum of an eliminable register and a constant, rework
2622 the sum. */
2623 if (REG_P (XEXP (x, 0))
2624 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2625 && CONSTANT_P (XEXP (x, 1)))
2627 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2628 ep++)
2629 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2631 /* The only time we want to replace a PLUS with a REG (this
2632 occurs when the constant operand of the PLUS is the negative
2633 of the offset) is when we are inside a MEM. We won't want
2634 to do so at other times because that would change the
2635 structure of the insn in a way that reload can't handle.
2636 We special-case the commonest situation in
2637 eliminate_regs_in_insn, so just replace a PLUS with a
2638 PLUS here, unless inside a MEM. */
2639 if (mem_mode != 0 && CONST_INT_P (XEXP (x, 1))
2640 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2641 return ep->to_rtx;
2642 else
2643 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2644 plus_constant (Pmode, XEXP (x, 1),
2645 ep->previous_offset));
2648 /* If the register is not eliminable, we are done since the other
2649 operand is a constant. */
2650 return x;
2653 /* If this is part of an address, we want to bring any constant to the
2654 outermost PLUS. We will do this by doing register replacement in
2655 our operands and seeing if a constant shows up in one of them.
2657 Note that there is no risk of modifying the structure of the insn,
2658 since we only get called for its operands, thus we are either
2659 modifying the address inside a MEM, or something like an address
2660 operand of a load-address insn. */
2663 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true,
2664 for_costs);
2665 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2666 for_costs);
2668 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2670 /* If one side is a PLUS and the other side is a pseudo that
2671 didn't get a hard register but has a reg_equiv_constant,
2672 we must replace the constant here since it may no longer
2673 be in the position of any operand. */
2674 if (GET_CODE (new0) == PLUS && REG_P (new1)
2675 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2676 && reg_renumber[REGNO (new1)] < 0
2677 && reg_equivs
2678 && reg_equiv_constant (REGNO (new1)) != 0)
2679 new1 = reg_equiv_constant (REGNO (new1));
2680 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2681 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2682 && reg_renumber[REGNO (new0)] < 0
2683 && reg_equiv_constant (REGNO (new0)) != 0)
2684 new0 = reg_equiv_constant (REGNO (new0));
2686 new_rtx = form_sum (GET_MODE (x), new0, new1);
2688 /* As above, if we are not inside a MEM we do not want to
2689 turn a PLUS into something else. We might try to do so here
2690 for an addition of 0 if we aren't optimizing. */
2691 if (! mem_mode && GET_CODE (new_rtx) != PLUS)
2692 return gen_rtx_PLUS (GET_MODE (x), new_rtx, const0_rtx);
2693 else
2694 return new_rtx;
2697 return x;
2699 case MULT:
2700 /* If this is the product of an eliminable register and a
2701 constant, apply the distribute law and move the constant out
2702 so that we have (plus (mult ..) ..). This is needed in order
2703 to keep load-address insns valid. This case is pathological.
2704 We ignore the possibility of overflow here. */
2705 if (REG_P (XEXP (x, 0))
2706 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2707 && CONST_INT_P (XEXP (x, 1)))
2708 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2709 ep++)
2710 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2712 if (! mem_mode
2713 /* Refs inside notes or in DEBUG_INSNs don't count for
2714 this purpose. */
2715 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2716 || GET_CODE (insn) == INSN_LIST
2717 || DEBUG_INSN_P (insn))))
2718 ep->ref_outside_mem = 1;
2720 return
2721 plus_constant (Pmode,
2722 gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2723 ep->previous_offset * INTVAL (XEXP (x, 1)));
2726 /* ... fall through ... */
2728 case CALL:
2729 case COMPARE:
2730 /* See comments before PLUS about handling MINUS. */
2731 case MINUS:
2732 case DIV: case UDIV:
2733 case MOD: case UMOD:
2734 case AND: case IOR: case XOR:
2735 case ROTATERT: case ROTATE:
2736 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2737 case NE: case EQ:
2738 case GE: case GT: case GEU: case GTU:
2739 case LE: case LT: case LEU: case LTU:
2741 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2742 for_costs);
2743 rtx new1 = XEXP (x, 1)
2744 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false,
2745 for_costs) : 0;
2747 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2748 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2750 return x;
2752 case EXPR_LIST:
2753 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2754 if (XEXP (x, 0))
2756 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true,
2757 for_costs);
2758 if (new_rtx != XEXP (x, 0))
2760 /* If this is a REG_DEAD note, it is not valid anymore.
2761 Using the eliminated version could result in creating a
2762 REG_DEAD note for the stack or frame pointer. */
2763 if (REG_NOTE_KIND (x) == REG_DEAD)
2764 return (XEXP (x, 1)
2765 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2766 for_costs)
2767 : NULL_RTX);
2769 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
2773 /* ... fall through ... */
2775 case INSN_LIST:
2776 /* Now do eliminations in the rest of the chain. If this was
2777 an EXPR_LIST, this might result in allocating more memory than is
2778 strictly needed, but it simplifies the code. */
2779 if (XEXP (x, 1))
2781 new_rtx = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2782 for_costs);
2783 if (new_rtx != XEXP (x, 1))
2784 return
2785 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new_rtx);
2787 return x;
2789 case PRE_INC:
2790 case POST_INC:
2791 case PRE_DEC:
2792 case POST_DEC:
2793 /* We do not support elimination of a register that is modified.
2794 elimination_effects has already make sure that this does not
2795 happen. */
2796 return x;
2798 case PRE_MODIFY:
2799 case POST_MODIFY:
2800 /* We do not support elimination of a register that is modified.
2801 elimination_effects has already make sure that this does not
2802 happen. The only remaining case we need to consider here is
2803 that the increment value may be an eliminable register. */
2804 if (GET_CODE (XEXP (x, 1)) == PLUS
2805 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2807 rtx new_rtx = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2808 insn, true, for_costs);
2810 if (new_rtx != XEXP (XEXP (x, 1), 1))
2811 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2812 gen_rtx_PLUS (GET_MODE (x),
2813 XEXP (x, 0), new_rtx));
2815 return x;
2817 case STRICT_LOW_PART:
2818 case NEG: case NOT:
2819 case SIGN_EXTEND: case ZERO_EXTEND:
2820 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2821 case FLOAT: case FIX:
2822 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2823 case ABS:
2824 case SQRT:
2825 case FFS:
2826 case CLZ:
2827 case CTZ:
2828 case POPCOUNT:
2829 case PARITY:
2830 case BSWAP:
2831 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2832 for_costs);
2833 if (new_rtx != XEXP (x, 0))
2834 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
2835 return x;
2837 case SUBREG:
2838 /* Similar to above processing, but preserve SUBREG_BYTE.
2839 Convert (subreg (mem)) to (mem) if not paradoxical.
2840 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2841 pseudo didn't get a hard reg, we must replace this with the
2842 eliminated version of the memory location because push_reload
2843 may do the replacement in certain circumstances. */
2844 if (REG_P (SUBREG_REG (x))
2845 && !paradoxical_subreg_p (x)
2846 && reg_equivs
2847 && reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
2849 new_rtx = SUBREG_REG (x);
2851 else
2852 new_rtx = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false, for_costs);
2854 if (new_rtx != SUBREG_REG (x))
2856 int x_size = GET_MODE_SIZE (GET_MODE (x));
2857 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
2859 if (MEM_P (new_rtx)
2860 && ((x_size < new_size
2861 #ifdef WORD_REGISTER_OPERATIONS
2862 /* On these machines, combine can create rtl of the form
2863 (set (subreg:m1 (reg:m2 R) 0) ...)
2864 where m1 < m2, and expects something interesting to
2865 happen to the entire word. Moreover, it will use the
2866 (reg:m2 R) later, expecting all bits to be preserved.
2867 So if the number of words is the same, preserve the
2868 subreg so that push_reload can see it. */
2869 && ! ((x_size - 1) / UNITS_PER_WORD
2870 == (new_size -1 ) / UNITS_PER_WORD)
2871 #endif
2873 || x_size == new_size)
2875 return adjust_address_nv (new_rtx, GET_MODE (x), SUBREG_BYTE (x));
2876 else
2877 return gen_rtx_SUBREG (GET_MODE (x), new_rtx, SUBREG_BYTE (x));
2880 return x;
2882 case MEM:
2883 /* Our only special processing is to pass the mode of the MEM to our
2884 recursive call and copy the flags. While we are here, handle this
2885 case more efficiently. */
2887 new_rtx = eliminate_regs_1 (XEXP (x, 0), GET_MODE (x), insn, true,
2888 for_costs);
2889 if (for_costs
2890 && memory_address_p (GET_MODE (x), XEXP (x, 0))
2891 && !memory_address_p (GET_MODE (x), new_rtx))
2892 for_each_rtx (&XEXP (x, 0), note_reg_elim_costly, insn);
2894 return replace_equiv_address_nv (x, new_rtx);
2896 case USE:
2897 /* Handle insn_list USE that a call to a pure function may generate. */
2898 new_rtx = eliminate_regs_1 (XEXP (x, 0), VOIDmode, insn, false,
2899 for_costs);
2900 if (new_rtx != XEXP (x, 0))
2901 return gen_rtx_USE (GET_MODE (x), new_rtx);
2902 return x;
2904 case CLOBBER:
2905 case ASM_OPERANDS:
2906 gcc_assert (insn && DEBUG_INSN_P (insn));
2907 break;
2909 case SET:
2910 gcc_unreachable ();
2912 default:
2913 break;
2916 /* Process each of our operands recursively. If any have changed, make a
2917 copy of the rtx. */
2918 fmt = GET_RTX_FORMAT (code);
2919 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2921 if (*fmt == 'e')
2923 new_rtx = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false,
2924 for_costs);
2925 if (new_rtx != XEXP (x, i) && ! copied)
2927 x = shallow_copy_rtx (x);
2928 copied = 1;
2930 XEXP (x, i) = new_rtx;
2932 else if (*fmt == 'E')
2934 int copied_vec = 0;
2935 for (j = 0; j < XVECLEN (x, i); j++)
2937 new_rtx = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false,
2938 for_costs);
2939 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
2941 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2942 XVEC (x, i)->elem);
2943 if (! copied)
2945 x = shallow_copy_rtx (x);
2946 copied = 1;
2948 XVEC (x, i) = new_v;
2949 copied_vec = 1;
2951 XVECEXP (x, i, j) = new_rtx;
2956 return x;
2960 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2962 return eliminate_regs_1 (x, mem_mode, insn, false, false);
2965 /* Scan rtx X for modifications of elimination target registers. Update
2966 the table of eliminables to reflect the changed state. MEM_MODE is
2967 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2969 static void
2970 elimination_effects (rtx x, enum machine_mode mem_mode)
2972 enum rtx_code code = GET_CODE (x);
2973 struct elim_table *ep;
2974 int regno;
2975 int i, j;
2976 const char *fmt;
2978 switch (code)
2980 CASE_CONST_ANY:
2981 case CONST:
2982 case SYMBOL_REF:
2983 case CODE_LABEL:
2984 case PC:
2985 case CC0:
2986 case ASM_INPUT:
2987 case ADDR_VEC:
2988 case ADDR_DIFF_VEC:
2989 case RETURN:
2990 return;
2992 case REG:
2993 regno = REGNO (x);
2995 /* First handle the case where we encounter a bare register that
2996 is eliminable. Replace it with a PLUS. */
2997 if (regno < FIRST_PSEUDO_REGISTER)
2999 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3000 ep++)
3001 if (ep->from_rtx == x && ep->can_eliminate)
3003 if (! mem_mode)
3004 ep->ref_outside_mem = 1;
3005 return;
3009 else if (reg_renumber[regno] < 0
3010 && reg_equivs != 0
3011 && reg_equiv_constant (regno)
3012 && ! function_invariant_p (reg_equiv_constant (regno)))
3013 elimination_effects (reg_equiv_constant (regno), mem_mode);
3014 return;
3016 case PRE_INC:
3017 case POST_INC:
3018 case PRE_DEC:
3019 case POST_DEC:
3020 case POST_MODIFY:
3021 case PRE_MODIFY:
3022 /* If we modify the source of an elimination rule, disable it. */
3023 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3024 if (ep->from_rtx == XEXP (x, 0))
3025 ep->can_eliminate = 0;
3027 /* If we modify the target of an elimination rule by adding a constant,
3028 update its offset. If we modify the target in any other way, we'll
3029 have to disable the rule as well. */
3030 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3031 if (ep->to_rtx == XEXP (x, 0))
3033 int size = GET_MODE_SIZE (mem_mode);
3035 /* If more bytes than MEM_MODE are pushed, account for them. */
3036 #ifdef PUSH_ROUNDING
3037 if (ep->to_rtx == stack_pointer_rtx)
3038 size = PUSH_ROUNDING (size);
3039 #endif
3040 if (code == PRE_DEC || code == POST_DEC)
3041 ep->offset += size;
3042 else if (code == PRE_INC || code == POST_INC)
3043 ep->offset -= size;
3044 else if (code == PRE_MODIFY || code == POST_MODIFY)
3046 if (GET_CODE (XEXP (x, 1)) == PLUS
3047 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
3048 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
3049 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
3050 else
3051 ep->can_eliminate = 0;
3055 /* These two aren't unary operators. */
3056 if (code == POST_MODIFY || code == PRE_MODIFY)
3057 break;
3059 /* Fall through to generic unary operation case. */
3060 case STRICT_LOW_PART:
3061 case NEG: case NOT:
3062 case SIGN_EXTEND: case ZERO_EXTEND:
3063 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3064 case FLOAT: case FIX:
3065 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3066 case ABS:
3067 case SQRT:
3068 case FFS:
3069 case CLZ:
3070 case CTZ:
3071 case POPCOUNT:
3072 case PARITY:
3073 case BSWAP:
3074 elimination_effects (XEXP (x, 0), mem_mode);
3075 return;
3077 case SUBREG:
3078 if (REG_P (SUBREG_REG (x))
3079 && (GET_MODE_SIZE (GET_MODE (x))
3080 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3081 && reg_equivs != 0
3082 && reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
3083 return;
3085 elimination_effects (SUBREG_REG (x), mem_mode);
3086 return;
3088 case USE:
3089 /* If using a register that is the source of an eliminate we still
3090 think can be performed, note it cannot be performed since we don't
3091 know how this register is used. */
3092 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3093 if (ep->from_rtx == XEXP (x, 0))
3094 ep->can_eliminate = 0;
3096 elimination_effects (XEXP (x, 0), mem_mode);
3097 return;
3099 case CLOBBER:
3100 /* If clobbering a register that is the replacement register for an
3101 elimination we still think can be performed, note that it cannot
3102 be performed. Otherwise, we need not be concerned about it. */
3103 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3104 if (ep->to_rtx == XEXP (x, 0))
3105 ep->can_eliminate = 0;
3107 elimination_effects (XEXP (x, 0), mem_mode);
3108 return;
3110 case SET:
3111 /* Check for setting a register that we know about. */
3112 if (REG_P (SET_DEST (x)))
3114 /* See if this is setting the replacement register for an
3115 elimination.
3117 If DEST is the hard frame pointer, we do nothing because we
3118 assume that all assignments to the frame pointer are for
3119 non-local gotos and are being done at a time when they are valid
3120 and do not disturb anything else. Some machines want to
3121 eliminate a fake argument pointer (or even a fake frame pointer)
3122 with either the real frame or the stack pointer. Assignments to
3123 the hard frame pointer must not prevent this elimination. */
3125 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3126 ep++)
3127 if (ep->to_rtx == SET_DEST (x)
3128 && SET_DEST (x) != hard_frame_pointer_rtx)
3130 /* If it is being incremented, adjust the offset. Otherwise,
3131 this elimination can't be done. */
3132 rtx src = SET_SRC (x);
3134 if (GET_CODE (src) == PLUS
3135 && XEXP (src, 0) == SET_DEST (x)
3136 && CONST_INT_P (XEXP (src, 1)))
3137 ep->offset -= INTVAL (XEXP (src, 1));
3138 else
3139 ep->can_eliminate = 0;
3143 elimination_effects (SET_DEST (x), VOIDmode);
3144 elimination_effects (SET_SRC (x), VOIDmode);
3145 return;
3147 case MEM:
3148 /* Our only special processing is to pass the mode of the MEM to our
3149 recursive call. */
3150 elimination_effects (XEXP (x, 0), GET_MODE (x));
3151 return;
3153 default:
3154 break;
3157 fmt = GET_RTX_FORMAT (code);
3158 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3160 if (*fmt == 'e')
3161 elimination_effects (XEXP (x, i), mem_mode);
3162 else if (*fmt == 'E')
3163 for (j = 0; j < XVECLEN (x, i); j++)
3164 elimination_effects (XVECEXP (x, i, j), mem_mode);
3168 /* Descend through rtx X and verify that no references to eliminable registers
3169 remain. If any do remain, mark the involved register as not
3170 eliminable. */
3172 static void
3173 check_eliminable_occurrences (rtx x)
3175 const char *fmt;
3176 int i;
3177 enum rtx_code code;
3179 if (x == 0)
3180 return;
3182 code = GET_CODE (x);
3184 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3186 struct elim_table *ep;
3188 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3189 if (ep->from_rtx == x)
3190 ep->can_eliminate = 0;
3191 return;
3194 fmt = GET_RTX_FORMAT (code);
3195 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3197 if (*fmt == 'e')
3198 check_eliminable_occurrences (XEXP (x, i));
3199 else if (*fmt == 'E')
3201 int j;
3202 for (j = 0; j < XVECLEN (x, i); j++)
3203 check_eliminable_occurrences (XVECEXP (x, i, j));
3208 /* Scan INSN and eliminate all eliminable registers in it.
3210 If REPLACE is nonzero, do the replacement destructively. Also
3211 delete the insn as dead it if it is setting an eliminable register.
3213 If REPLACE is zero, do all our allocations in reload_obstack.
3215 If no eliminations were done and this insn doesn't require any elimination
3216 processing (these are not identical conditions: it might be updating sp,
3217 but not referencing fp; this needs to be seen during reload_as_needed so
3218 that the offset between fp and sp can be taken into consideration), zero
3219 is returned. Otherwise, 1 is returned. */
3221 static int
3222 eliminate_regs_in_insn (rtx insn, int replace)
3224 int icode = recog_memoized (insn);
3225 rtx old_body = PATTERN (insn);
3226 int insn_is_asm = asm_noperands (old_body) >= 0;
3227 rtx old_set = single_set (insn);
3228 rtx new_body;
3229 int val = 0;
3230 int i;
3231 rtx substed_operand[MAX_RECOG_OPERANDS];
3232 rtx orig_operand[MAX_RECOG_OPERANDS];
3233 struct elim_table *ep;
3234 rtx plus_src, plus_cst_src;
3236 if (! insn_is_asm && icode < 0)
3238 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3239 || GET_CODE (PATTERN (insn)) == CLOBBER
3240 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3241 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3242 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3243 || DEBUG_INSN_P (insn));
3244 if (DEBUG_INSN_P (insn))
3245 INSN_VAR_LOCATION_LOC (insn)
3246 = eliminate_regs (INSN_VAR_LOCATION_LOC (insn), VOIDmode, insn);
3247 return 0;
3250 if (old_set != 0 && REG_P (SET_DEST (old_set))
3251 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3253 /* Check for setting an eliminable register. */
3254 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3255 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3257 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3258 /* If this is setting the frame pointer register to the
3259 hardware frame pointer register and this is an elimination
3260 that will be done (tested above), this insn is really
3261 adjusting the frame pointer downward to compensate for
3262 the adjustment done before a nonlocal goto. */
3263 if (ep->from == FRAME_POINTER_REGNUM
3264 && ep->to == HARD_FRAME_POINTER_REGNUM)
3266 rtx base = SET_SRC (old_set);
3267 rtx base_insn = insn;
3268 HOST_WIDE_INT offset = 0;
3270 while (base != ep->to_rtx)
3272 rtx prev_insn, prev_set;
3274 if (GET_CODE (base) == PLUS
3275 && CONST_INT_P (XEXP (base, 1)))
3277 offset += INTVAL (XEXP (base, 1));
3278 base = XEXP (base, 0);
3280 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3281 && (prev_set = single_set (prev_insn)) != 0
3282 && rtx_equal_p (SET_DEST (prev_set), base))
3284 base = SET_SRC (prev_set);
3285 base_insn = prev_insn;
3287 else
3288 break;
3291 if (base == ep->to_rtx)
3293 rtx src = plus_constant (Pmode, ep->to_rtx,
3294 offset - ep->offset);
3296 new_body = old_body;
3297 if (! replace)
3299 new_body = copy_insn (old_body);
3300 if (REG_NOTES (insn))
3301 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3303 PATTERN (insn) = new_body;
3304 old_set = single_set (insn);
3306 /* First see if this insn remains valid when we
3307 make the change. If not, keep the INSN_CODE
3308 the same and let reload fit it up. */
3309 validate_change (insn, &SET_SRC (old_set), src, 1);
3310 validate_change (insn, &SET_DEST (old_set),
3311 ep->to_rtx, 1);
3312 if (! apply_change_group ())
3314 SET_SRC (old_set) = src;
3315 SET_DEST (old_set) = ep->to_rtx;
3318 val = 1;
3319 goto done;
3322 #endif
3324 /* In this case this insn isn't serving a useful purpose. We
3325 will delete it in reload_as_needed once we know that this
3326 elimination is, in fact, being done.
3328 If REPLACE isn't set, we can't delete this insn, but needn't
3329 process it since it won't be used unless something changes. */
3330 if (replace)
3332 delete_dead_insn (insn);
3333 return 1;
3335 val = 1;
3336 goto done;
3340 /* We allow one special case which happens to work on all machines we
3341 currently support: a single set with the source or a REG_EQUAL
3342 note being a PLUS of an eliminable register and a constant. */
3343 plus_src = plus_cst_src = 0;
3344 if (old_set && REG_P (SET_DEST (old_set)))
3346 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3347 plus_src = SET_SRC (old_set);
3348 /* First see if the source is of the form (plus (...) CST). */
3349 if (plus_src
3350 && CONST_INT_P (XEXP (plus_src, 1)))
3351 plus_cst_src = plus_src;
3352 else if (REG_P (SET_SRC (old_set))
3353 || plus_src)
3355 /* Otherwise, see if we have a REG_EQUAL note of the form
3356 (plus (...) CST). */
3357 rtx links;
3358 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3360 if ((REG_NOTE_KIND (links) == REG_EQUAL
3361 || REG_NOTE_KIND (links) == REG_EQUIV)
3362 && GET_CODE (XEXP (links, 0)) == PLUS
3363 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3365 plus_cst_src = XEXP (links, 0);
3366 break;
3371 /* Check that the first operand of the PLUS is a hard reg or
3372 the lowpart subreg of one. */
3373 if (plus_cst_src)
3375 rtx reg = XEXP (plus_cst_src, 0);
3376 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3377 reg = SUBREG_REG (reg);
3379 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3380 plus_cst_src = 0;
3383 if (plus_cst_src)
3385 rtx reg = XEXP (plus_cst_src, 0);
3386 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3388 if (GET_CODE (reg) == SUBREG)
3389 reg = SUBREG_REG (reg);
3391 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3392 if (ep->from_rtx == reg && ep->can_eliminate)
3394 rtx to_rtx = ep->to_rtx;
3395 offset += ep->offset;
3396 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
3398 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3399 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3400 to_rtx);
3401 /* If we have a nonzero offset, and the source is already
3402 a simple REG, the following transformation would
3403 increase the cost of the insn by replacing a simple REG
3404 with (plus (reg sp) CST). So try only when we already
3405 had a PLUS before. */
3406 if (offset == 0 || plus_src)
3408 rtx new_src = plus_constant (GET_MODE (to_rtx),
3409 to_rtx, offset);
3411 new_body = old_body;
3412 if (! replace)
3414 new_body = copy_insn (old_body);
3415 if (REG_NOTES (insn))
3416 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3418 PATTERN (insn) = new_body;
3419 old_set = single_set (insn);
3421 /* First see if this insn remains valid when we make the
3422 change. If not, try to replace the whole pattern with
3423 a simple set (this may help if the original insn was a
3424 PARALLEL that was only recognized as single_set due to
3425 REG_UNUSED notes). If this isn't valid either, keep
3426 the INSN_CODE the same and let reload fix it up. */
3427 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3429 rtx new_pat = gen_rtx_SET (VOIDmode,
3430 SET_DEST (old_set), new_src);
3432 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3433 SET_SRC (old_set) = new_src;
3436 else
3437 break;
3439 val = 1;
3440 /* This can't have an effect on elimination offsets, so skip right
3441 to the end. */
3442 goto done;
3446 /* Determine the effects of this insn on elimination offsets. */
3447 elimination_effects (old_body, VOIDmode);
3449 /* Eliminate all eliminable registers occurring in operands that
3450 can be handled by reload. */
3451 extract_insn (insn);
3452 for (i = 0; i < recog_data.n_operands; i++)
3454 orig_operand[i] = recog_data.operand[i];
3455 substed_operand[i] = recog_data.operand[i];
3457 /* For an asm statement, every operand is eliminable. */
3458 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3460 bool is_set_src, in_plus;
3462 /* Check for setting a register that we know about. */
3463 if (recog_data.operand_type[i] != OP_IN
3464 && REG_P (orig_operand[i]))
3466 /* If we are assigning to a register that can be eliminated, it
3467 must be as part of a PARALLEL, since the code above handles
3468 single SETs. We must indicate that we can no longer
3469 eliminate this reg. */
3470 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3471 ep++)
3472 if (ep->from_rtx == orig_operand[i])
3473 ep->can_eliminate = 0;
3476 /* Companion to the above plus substitution, we can allow
3477 invariants as the source of a plain move. */
3478 is_set_src = false;
3479 if (old_set
3480 && recog_data.operand_loc[i] == &SET_SRC (old_set))
3481 is_set_src = true;
3482 in_plus = false;
3483 if (plus_src
3484 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3485 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3486 in_plus = true;
3488 substed_operand[i]
3489 = eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3490 replace ? insn : NULL_RTX,
3491 is_set_src || in_plus, false);
3492 if (substed_operand[i] != orig_operand[i])
3493 val = 1;
3494 /* Terminate the search in check_eliminable_occurrences at
3495 this point. */
3496 *recog_data.operand_loc[i] = 0;
3498 /* If an output operand changed from a REG to a MEM and INSN is an
3499 insn, write a CLOBBER insn. */
3500 if (recog_data.operand_type[i] != OP_IN
3501 && REG_P (orig_operand[i])
3502 && MEM_P (substed_operand[i])
3503 && replace)
3504 emit_insn_after (gen_clobber (orig_operand[i]), insn);
3508 for (i = 0; i < recog_data.n_dups; i++)
3509 *recog_data.dup_loc[i]
3510 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3512 /* If any eliminable remain, they aren't eliminable anymore. */
3513 check_eliminable_occurrences (old_body);
3515 /* Substitute the operands; the new values are in the substed_operand
3516 array. */
3517 for (i = 0; i < recog_data.n_operands; i++)
3518 *recog_data.operand_loc[i] = substed_operand[i];
3519 for (i = 0; i < recog_data.n_dups; i++)
3520 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3522 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3523 re-recognize the insn. We do this in case we had a simple addition
3524 but now can do this as a load-address. This saves an insn in this
3525 common case.
3526 If re-recognition fails, the old insn code number will still be used,
3527 and some register operands may have changed into PLUS expressions.
3528 These will be handled by find_reloads by loading them into a register
3529 again. */
3531 if (val)
3533 /* If we aren't replacing things permanently and we changed something,
3534 make another copy to ensure that all the RTL is new. Otherwise
3535 things can go wrong if find_reload swaps commutative operands
3536 and one is inside RTL that has been copied while the other is not. */
3537 new_body = old_body;
3538 if (! replace)
3540 new_body = copy_insn (old_body);
3541 if (REG_NOTES (insn))
3542 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3544 PATTERN (insn) = new_body;
3546 /* If we had a move insn but now we don't, rerecognize it. This will
3547 cause spurious re-recognition if the old move had a PARALLEL since
3548 the new one still will, but we can't call single_set without
3549 having put NEW_BODY into the insn and the re-recognition won't
3550 hurt in this rare case. */
3551 /* ??? Why this huge if statement - why don't we just rerecognize the
3552 thing always? */
3553 if (! insn_is_asm
3554 && old_set != 0
3555 && ((REG_P (SET_SRC (old_set))
3556 && (GET_CODE (new_body) != SET
3557 || !REG_P (SET_SRC (new_body))))
3558 /* If this was a load from or store to memory, compare
3559 the MEM in recog_data.operand to the one in the insn.
3560 If they are not equal, then rerecognize the insn. */
3561 || (old_set != 0
3562 && ((MEM_P (SET_SRC (old_set))
3563 && SET_SRC (old_set) != recog_data.operand[1])
3564 || (MEM_P (SET_DEST (old_set))
3565 && SET_DEST (old_set) != recog_data.operand[0])))
3566 /* If this was an add insn before, rerecognize. */
3567 || GET_CODE (SET_SRC (old_set)) == PLUS))
3569 int new_icode = recog (PATTERN (insn), insn, 0);
3570 if (new_icode >= 0)
3571 INSN_CODE (insn) = new_icode;
3575 /* Restore the old body. If there were any changes to it, we made a copy
3576 of it while the changes were still in place, so we'll correctly return
3577 a modified insn below. */
3578 if (! replace)
3580 /* Restore the old body. */
3581 for (i = 0; i < recog_data.n_operands; i++)
3582 /* Restoring a top-level match_parallel would clobber the new_body
3583 we installed in the insn. */
3584 if (recog_data.operand_loc[i] != &PATTERN (insn))
3585 *recog_data.operand_loc[i] = orig_operand[i];
3586 for (i = 0; i < recog_data.n_dups; i++)
3587 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3590 /* Update all elimination pairs to reflect the status after the current
3591 insn. The changes we make were determined by the earlier call to
3592 elimination_effects.
3594 We also detect cases where register elimination cannot be done,
3595 namely, if a register would be both changed and referenced outside a MEM
3596 in the resulting insn since such an insn is often undefined and, even if
3597 not, we cannot know what meaning will be given to it. Note that it is
3598 valid to have a register used in an address in an insn that changes it
3599 (presumably with a pre- or post-increment or decrement).
3601 If anything changes, return nonzero. */
3603 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3605 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3606 ep->can_eliminate = 0;
3608 ep->ref_outside_mem = 0;
3610 if (ep->previous_offset != ep->offset)
3611 val = 1;
3614 done:
3615 /* If we changed something, perform elimination in REG_NOTES. This is
3616 needed even when REPLACE is zero because a REG_DEAD note might refer
3617 to a register that we eliminate and could cause a different number
3618 of spill registers to be needed in the final reload pass than in
3619 the pre-passes. */
3620 if (val && REG_NOTES (insn) != 0)
3621 REG_NOTES (insn)
3622 = eliminate_regs_1 (REG_NOTES (insn), VOIDmode, REG_NOTES (insn), true,
3623 false);
3625 return val;
3628 /* Like eliminate_regs_in_insn, but only estimate costs for the use of the
3629 register allocator. INSN is the instruction we need to examine, we perform
3630 eliminations in its operands and record cases where eliminating a reg with
3631 an invariant equivalence would add extra cost. */
3633 static void
3634 elimination_costs_in_insn (rtx insn)
3636 int icode = recog_memoized (insn);
3637 rtx old_body = PATTERN (insn);
3638 int insn_is_asm = asm_noperands (old_body) >= 0;
3639 rtx old_set = single_set (insn);
3640 int i;
3641 rtx orig_operand[MAX_RECOG_OPERANDS];
3642 rtx orig_dup[MAX_RECOG_OPERANDS];
3643 struct elim_table *ep;
3644 rtx plus_src, plus_cst_src;
3645 bool sets_reg_p;
3647 if (! insn_is_asm && icode < 0)
3649 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3650 || GET_CODE (PATTERN (insn)) == CLOBBER
3651 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3652 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3653 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3654 || DEBUG_INSN_P (insn));
3655 return;
3658 if (old_set != 0 && REG_P (SET_DEST (old_set))
3659 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3661 /* Check for setting an eliminable register. */
3662 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3663 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3664 return;
3667 /* We allow one special case which happens to work on all machines we
3668 currently support: a single set with the source or a REG_EQUAL
3669 note being a PLUS of an eliminable register and a constant. */
3670 plus_src = plus_cst_src = 0;
3671 sets_reg_p = false;
3672 if (old_set && REG_P (SET_DEST (old_set)))
3674 sets_reg_p = true;
3675 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3676 plus_src = SET_SRC (old_set);
3677 /* First see if the source is of the form (plus (...) CST). */
3678 if (plus_src
3679 && CONST_INT_P (XEXP (plus_src, 1)))
3680 plus_cst_src = plus_src;
3681 else if (REG_P (SET_SRC (old_set))
3682 || plus_src)
3684 /* Otherwise, see if we have a REG_EQUAL note of the form
3685 (plus (...) CST). */
3686 rtx links;
3687 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3689 if ((REG_NOTE_KIND (links) == REG_EQUAL
3690 || REG_NOTE_KIND (links) == REG_EQUIV)
3691 && GET_CODE (XEXP (links, 0)) == PLUS
3692 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3694 plus_cst_src = XEXP (links, 0);
3695 break;
3701 /* Determine the effects of this insn on elimination offsets. */
3702 elimination_effects (old_body, VOIDmode);
3704 /* Eliminate all eliminable registers occurring in operands that
3705 can be handled by reload. */
3706 extract_insn (insn);
3707 for (i = 0; i < recog_data.n_dups; i++)
3708 orig_dup[i] = *recog_data.dup_loc[i];
3710 for (i = 0; i < recog_data.n_operands; i++)
3712 orig_operand[i] = recog_data.operand[i];
3714 /* For an asm statement, every operand is eliminable. */
3715 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3717 bool is_set_src, in_plus;
3719 /* Check for setting a register that we know about. */
3720 if (recog_data.operand_type[i] != OP_IN
3721 && REG_P (orig_operand[i]))
3723 /* If we are assigning to a register that can be eliminated, it
3724 must be as part of a PARALLEL, since the code above handles
3725 single SETs. We must indicate that we can no longer
3726 eliminate this reg. */
3727 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3728 ep++)
3729 if (ep->from_rtx == orig_operand[i])
3730 ep->can_eliminate = 0;
3733 /* Companion to the above plus substitution, we can allow
3734 invariants as the source of a plain move. */
3735 is_set_src = false;
3736 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3737 is_set_src = true;
3738 if (is_set_src && !sets_reg_p)
3739 note_reg_elim_costly (&SET_SRC (old_set), insn);
3740 in_plus = false;
3741 if (plus_src && sets_reg_p
3742 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3743 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3744 in_plus = true;
3746 eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3747 NULL_RTX,
3748 is_set_src || in_plus, true);
3749 /* Terminate the search in check_eliminable_occurrences at
3750 this point. */
3751 *recog_data.operand_loc[i] = 0;
3755 for (i = 0; i < recog_data.n_dups; i++)
3756 *recog_data.dup_loc[i]
3757 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3759 /* If any eliminable remain, they aren't eliminable anymore. */
3760 check_eliminable_occurrences (old_body);
3762 /* Restore the old body. */
3763 for (i = 0; i < recog_data.n_operands; i++)
3764 *recog_data.operand_loc[i] = orig_operand[i];
3765 for (i = 0; i < recog_data.n_dups; i++)
3766 *recog_data.dup_loc[i] = orig_dup[i];
3768 /* Update all elimination pairs to reflect the status after the current
3769 insn. The changes we make were determined by the earlier call to
3770 elimination_effects. */
3772 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3774 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3775 ep->can_eliminate = 0;
3777 ep->ref_outside_mem = 0;
3780 return;
3783 /* Loop through all elimination pairs.
3784 Recalculate the number not at initial offset.
3786 Compute the maximum offset (minimum offset if the stack does not
3787 grow downward) for each elimination pair. */
3789 static void
3790 update_eliminable_offsets (void)
3792 struct elim_table *ep;
3794 num_not_at_initial_offset = 0;
3795 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3797 ep->previous_offset = ep->offset;
3798 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3799 num_not_at_initial_offset++;
3803 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3804 replacement we currently believe is valid, mark it as not eliminable if X
3805 modifies DEST in any way other than by adding a constant integer to it.
3807 If DEST is the frame pointer, we do nothing because we assume that
3808 all assignments to the hard frame pointer are nonlocal gotos and are being
3809 done at a time when they are valid and do not disturb anything else.
3810 Some machines want to eliminate a fake argument pointer with either the
3811 frame or stack pointer. Assignments to the hard frame pointer must not
3812 prevent this elimination.
3814 Called via note_stores from reload before starting its passes to scan
3815 the insns of the function. */
3817 static void
3818 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3820 unsigned int i;
3822 /* A SUBREG of a hard register here is just changing its mode. We should
3823 not see a SUBREG of an eliminable hard register, but check just in
3824 case. */
3825 if (GET_CODE (dest) == SUBREG)
3826 dest = SUBREG_REG (dest);
3828 if (dest == hard_frame_pointer_rtx)
3829 return;
3831 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3832 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3833 && (GET_CODE (x) != SET
3834 || GET_CODE (SET_SRC (x)) != PLUS
3835 || XEXP (SET_SRC (x), 0) != dest
3836 || !CONST_INT_P (XEXP (SET_SRC (x), 1))))
3838 reg_eliminate[i].can_eliminate_previous
3839 = reg_eliminate[i].can_eliminate = 0;
3840 num_eliminable--;
3844 /* Verify that the initial elimination offsets did not change since the
3845 last call to set_initial_elim_offsets. This is used to catch cases
3846 where something illegal happened during reload_as_needed that could
3847 cause incorrect code to be generated if we did not check for it. */
3849 static bool
3850 verify_initial_elim_offsets (void)
3852 HOST_WIDE_INT t;
3854 if (!num_eliminable)
3855 return true;
3857 #ifdef ELIMINABLE_REGS
3859 struct elim_table *ep;
3861 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3863 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3864 if (t != ep->initial_offset)
3865 return false;
3868 #else
3869 INITIAL_FRAME_POINTER_OFFSET (t);
3870 if (t != reg_eliminate[0].initial_offset)
3871 return false;
3872 #endif
3874 return true;
3877 /* Reset all offsets on eliminable registers to their initial values. */
3879 static void
3880 set_initial_elim_offsets (void)
3882 struct elim_table *ep = reg_eliminate;
3884 #ifdef ELIMINABLE_REGS
3885 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3887 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3888 ep->previous_offset = ep->offset = ep->initial_offset;
3890 #else
3891 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3892 ep->previous_offset = ep->offset = ep->initial_offset;
3893 #endif
3895 num_not_at_initial_offset = 0;
3898 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3900 static void
3901 set_initial_eh_label_offset (rtx label)
3903 set_label_offsets (label, NULL_RTX, 1);
3906 /* Initialize the known label offsets.
3907 Set a known offset for each forced label to be at the initial offset
3908 of each elimination. We do this because we assume that all
3909 computed jumps occur from a location where each elimination is
3910 at its initial offset.
3911 For all other labels, show that we don't know the offsets. */
3913 static void
3914 set_initial_label_offsets (void)
3916 rtx x;
3917 memset (offsets_known_at, 0, num_labels);
3919 for (x = forced_labels; x; x = XEXP (x, 1))
3920 if (XEXP (x, 0))
3921 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3923 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
3924 if (XEXP (x, 0))
3925 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3927 for_each_eh_label (set_initial_eh_label_offset);
3930 /* Set all elimination offsets to the known values for the code label given
3931 by INSN. */
3933 static void
3934 set_offsets_for_label (rtx insn)
3936 unsigned int i;
3937 int label_nr = CODE_LABEL_NUMBER (insn);
3938 struct elim_table *ep;
3940 num_not_at_initial_offset = 0;
3941 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3943 ep->offset = ep->previous_offset
3944 = offsets_at[label_nr - first_label_num][i];
3945 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3946 num_not_at_initial_offset++;
3950 /* See if anything that happened changes which eliminations are valid.
3951 For example, on the SPARC, whether or not the frame pointer can
3952 be eliminated can depend on what registers have been used. We need
3953 not check some conditions again (such as flag_omit_frame_pointer)
3954 since they can't have changed. */
3956 static void
3957 update_eliminables (HARD_REG_SET *pset)
3959 int previous_frame_pointer_needed = frame_pointer_needed;
3960 struct elim_table *ep;
3962 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3963 if ((ep->from == HARD_FRAME_POINTER_REGNUM
3964 && targetm.frame_pointer_required ())
3965 #ifdef ELIMINABLE_REGS
3966 || ! targetm.can_eliminate (ep->from, ep->to)
3967 #endif
3969 ep->can_eliminate = 0;
3971 /* Look for the case where we have discovered that we can't replace
3972 register A with register B and that means that we will now be
3973 trying to replace register A with register C. This means we can
3974 no longer replace register C with register B and we need to disable
3975 such an elimination, if it exists. This occurs often with A == ap,
3976 B == sp, and C == fp. */
3978 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3980 struct elim_table *op;
3981 int new_to = -1;
3983 if (! ep->can_eliminate && ep->can_eliminate_previous)
3985 /* Find the current elimination for ep->from, if there is a
3986 new one. */
3987 for (op = reg_eliminate;
3988 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3989 if (op->from == ep->from && op->can_eliminate)
3991 new_to = op->to;
3992 break;
3995 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
3996 disable it. */
3997 for (op = reg_eliminate;
3998 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3999 if (op->from == new_to && op->to == ep->to)
4000 op->can_eliminate = 0;
4004 /* See if any registers that we thought we could eliminate the previous
4005 time are no longer eliminable. If so, something has changed and we
4006 must spill the register. Also, recompute the number of eliminable
4007 registers and see if the frame pointer is needed; it is if there is
4008 no elimination of the frame pointer that we can perform. */
4010 frame_pointer_needed = 1;
4011 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4013 if (ep->can_eliminate
4014 && ep->from == FRAME_POINTER_REGNUM
4015 && ep->to != HARD_FRAME_POINTER_REGNUM
4016 && (! SUPPORTS_STACK_ALIGNMENT
4017 || ! crtl->stack_realign_needed))
4018 frame_pointer_needed = 0;
4020 if (! ep->can_eliminate && ep->can_eliminate_previous)
4022 ep->can_eliminate_previous = 0;
4023 SET_HARD_REG_BIT (*pset, ep->from);
4024 num_eliminable--;
4028 /* If we didn't need a frame pointer last time, but we do now, spill
4029 the hard frame pointer. */
4030 if (frame_pointer_needed && ! previous_frame_pointer_needed)
4031 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
4034 /* Return true if X is used as the target register of an elimination. */
4036 bool
4037 elimination_target_reg_p (rtx x)
4039 struct elim_table *ep;
4041 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4042 if (ep->to_rtx == x && ep->can_eliminate)
4043 return true;
4045 return false;
4048 /* Initialize the table of registers to eliminate.
4049 Pre-condition: global flag frame_pointer_needed has been set before
4050 calling this function. */
4052 static void
4053 init_elim_table (void)
4055 struct elim_table *ep;
4056 #ifdef ELIMINABLE_REGS
4057 const struct elim_table_1 *ep1;
4058 #endif
4060 if (!reg_eliminate)
4061 reg_eliminate = XCNEWVEC (struct elim_table, NUM_ELIMINABLE_REGS);
4063 num_eliminable = 0;
4065 #ifdef ELIMINABLE_REGS
4066 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
4067 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
4069 ep->from = ep1->from;
4070 ep->to = ep1->to;
4071 ep->can_eliminate = ep->can_eliminate_previous
4072 = (targetm.can_eliminate (ep->from, ep->to)
4073 && ! (ep->to == STACK_POINTER_REGNUM
4074 && frame_pointer_needed
4075 && (! SUPPORTS_STACK_ALIGNMENT
4076 || ! stack_realign_fp)));
4078 #else
4079 reg_eliminate[0].from = reg_eliminate_1[0].from;
4080 reg_eliminate[0].to = reg_eliminate_1[0].to;
4081 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
4082 = ! frame_pointer_needed;
4083 #endif
4085 /* Count the number of eliminable registers and build the FROM and TO
4086 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
4087 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
4088 We depend on this. */
4089 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4091 num_eliminable += ep->can_eliminate;
4092 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
4093 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
4097 /* Find all the pseudo registers that didn't get hard regs
4098 but do have known equivalent constants or memory slots.
4099 These include parameters (known equivalent to parameter slots)
4100 and cse'd or loop-moved constant memory addresses.
4102 Record constant equivalents in reg_equiv_constant
4103 so they will be substituted by find_reloads.
4104 Record memory equivalents in reg_mem_equiv so they can
4105 be substituted eventually by altering the REG-rtx's. */
4107 static void
4108 init_eliminable_invariants (rtx first, bool do_subregs)
4110 int i;
4111 rtx insn;
4113 grow_reg_equivs ();
4114 if (do_subregs)
4115 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
4116 else
4117 reg_max_ref_width = NULL;
4119 num_eliminable_invariants = 0;
4121 first_label_num = get_first_label_num ();
4122 num_labels = max_label_num () - first_label_num;
4124 /* Allocate the tables used to store offset information at labels. */
4125 offsets_known_at = XNEWVEC (char, num_labels);
4126 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
4128 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
4129 to. If DO_SUBREGS is true, also find all paradoxical subregs and
4130 find largest such for each pseudo. FIRST is the head of the insn
4131 list. */
4133 for (insn = first; insn; insn = NEXT_INSN (insn))
4135 rtx set = single_set (insn);
4137 /* We may introduce USEs that we want to remove at the end, so
4138 we'll mark them with QImode. Make sure there are no
4139 previously-marked insns left by say regmove. */
4140 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
4141 && GET_MODE (insn) != VOIDmode)
4142 PUT_MODE (insn, VOIDmode);
4144 if (do_subregs && NONDEBUG_INSN_P (insn))
4145 scan_paradoxical_subregs (PATTERN (insn));
4147 if (set != 0 && REG_P (SET_DEST (set)))
4149 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
4150 rtx x;
4152 if (! note)
4153 continue;
4155 i = REGNO (SET_DEST (set));
4156 x = XEXP (note, 0);
4158 if (i <= LAST_VIRTUAL_REGISTER)
4159 continue;
4161 /* If flag_pic and we have constant, verify it's legitimate. */
4162 if (!CONSTANT_P (x)
4163 || !flag_pic || LEGITIMATE_PIC_OPERAND_P (x))
4165 /* It can happen that a REG_EQUIV note contains a MEM
4166 that is not a legitimate memory operand. As later
4167 stages of reload assume that all addresses found
4168 in the reg_equiv_* arrays were originally legitimate,
4169 we ignore such REG_EQUIV notes. */
4170 if (memory_operand (x, VOIDmode))
4172 /* Always unshare the equivalence, so we can
4173 substitute into this insn without touching the
4174 equivalence. */
4175 reg_equiv_memory_loc (i) = copy_rtx (x);
4177 else if (function_invariant_p (x))
4179 enum machine_mode mode;
4181 mode = GET_MODE (SET_DEST (set));
4182 if (GET_CODE (x) == PLUS)
4184 /* This is PLUS of frame pointer and a constant,
4185 and might be shared. Unshare it. */
4186 reg_equiv_invariant (i) = copy_rtx (x);
4187 num_eliminable_invariants++;
4189 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
4191 reg_equiv_invariant (i) = x;
4192 num_eliminable_invariants++;
4194 else if (targetm.legitimate_constant_p (mode, x))
4195 reg_equiv_constant (i) = x;
4196 else
4198 reg_equiv_memory_loc (i) = force_const_mem (mode, x);
4199 if (! reg_equiv_memory_loc (i))
4200 reg_equiv_init (i) = NULL_RTX;
4203 else
4205 reg_equiv_init (i) = NULL_RTX;
4206 continue;
4209 else
4210 reg_equiv_init (i) = NULL_RTX;
4214 if (dump_file)
4215 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4216 if (reg_equiv_init (i))
4218 fprintf (dump_file, "init_insns for %u: ", i);
4219 print_inline_rtx (dump_file, reg_equiv_init (i), 20);
4220 fprintf (dump_file, "\n");
4224 /* Indicate that we no longer have known memory locations or constants.
4225 Free all data involved in tracking these. */
4227 static void
4228 free_reg_equiv (void)
4230 int i;
4233 free (offsets_known_at);
4234 free (offsets_at);
4235 offsets_at = 0;
4236 offsets_known_at = 0;
4238 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4239 if (reg_equiv_alt_mem_list (i))
4240 free_EXPR_LIST_list (&reg_equiv_alt_mem_list (i));
4241 VEC_free (reg_equivs_t, gc, reg_equivs);
4242 reg_equivs = NULL;
4246 /* Kick all pseudos out of hard register REGNO.
4248 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
4249 because we found we can't eliminate some register. In the case, no pseudos
4250 are allowed to be in the register, even if they are only in a block that
4251 doesn't require spill registers, unlike the case when we are spilling this
4252 hard reg to produce another spill register.
4254 Return nonzero if any pseudos needed to be kicked out. */
4256 static void
4257 spill_hard_reg (unsigned int regno, int cant_eliminate)
4259 int i;
4261 if (cant_eliminate)
4263 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
4264 df_set_regs_ever_live (regno, true);
4267 /* Spill every pseudo reg that was allocated to this reg
4268 or to something that overlaps this reg. */
4270 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4271 if (reg_renumber[i] >= 0
4272 && (unsigned int) reg_renumber[i] <= regno
4273 && end_hard_regno (PSEUDO_REGNO_MODE (i), reg_renumber[i]) > regno)
4274 SET_REGNO_REG_SET (&spilled_pseudos, i);
4277 /* After find_reload_regs has been run for all insn that need reloads,
4278 and/or spill_hard_regs was called, this function is used to actually
4279 spill pseudo registers and try to reallocate them. It also sets up the
4280 spill_regs array for use by choose_reload_regs. */
4282 static int
4283 finish_spills (int global)
4285 struct insn_chain *chain;
4286 int something_changed = 0;
4287 unsigned i;
4288 reg_set_iterator rsi;
4290 /* Build the spill_regs array for the function. */
4291 /* If there are some registers still to eliminate and one of the spill regs
4292 wasn't ever used before, additional stack space may have to be
4293 allocated to store this register. Thus, we may have changed the offset
4294 between the stack and frame pointers, so mark that something has changed.
4296 One might think that we need only set VAL to 1 if this is a call-used
4297 register. However, the set of registers that must be saved by the
4298 prologue is not identical to the call-used set. For example, the
4299 register used by the call insn for the return PC is a call-used register,
4300 but must be saved by the prologue. */
4302 n_spills = 0;
4303 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4304 if (TEST_HARD_REG_BIT (used_spill_regs, i))
4306 spill_reg_order[i] = n_spills;
4307 spill_regs[n_spills++] = i;
4308 if (num_eliminable && ! df_regs_ever_live_p (i))
4309 something_changed = 1;
4310 df_set_regs_ever_live (i, true);
4312 else
4313 spill_reg_order[i] = -1;
4315 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
4316 if (! ira_conflicts_p || reg_renumber[i] >= 0)
4318 /* Record the current hard register the pseudo is allocated to
4319 in pseudo_previous_regs so we avoid reallocating it to the
4320 same hard reg in a later pass. */
4321 gcc_assert (reg_renumber[i] >= 0);
4323 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
4324 /* Mark it as no longer having a hard register home. */
4325 reg_renumber[i] = -1;
4326 if (ira_conflicts_p)
4327 /* Inform IRA about the change. */
4328 ira_mark_allocation_change (i);
4329 /* We will need to scan everything again. */
4330 something_changed = 1;
4333 /* Retry global register allocation if possible. */
4334 if (global && ira_conflicts_p)
4336 unsigned int n;
4338 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
4339 /* For every insn that needs reloads, set the registers used as spill
4340 regs in pseudo_forbidden_regs for every pseudo live across the
4341 insn. */
4342 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
4344 EXECUTE_IF_SET_IN_REG_SET
4345 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
4347 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4348 chain->used_spill_regs);
4350 EXECUTE_IF_SET_IN_REG_SET
4351 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
4353 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4354 chain->used_spill_regs);
4358 /* Retry allocating the pseudos spilled in IRA and the
4359 reload. For each reg, merge the various reg sets that
4360 indicate which hard regs can't be used, and call
4361 ira_reassign_pseudos. */
4362 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
4363 if (reg_old_renumber[i] != reg_renumber[i])
4365 if (reg_renumber[i] < 0)
4366 temp_pseudo_reg_arr[n++] = i;
4367 else
4368 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
4370 if (ira_reassign_pseudos (temp_pseudo_reg_arr, n,
4371 bad_spill_regs_global,
4372 pseudo_forbidden_regs, pseudo_previous_regs,
4373 &spilled_pseudos))
4374 something_changed = 1;
4376 /* Fix up the register information in the insn chain.
4377 This involves deleting those of the spilled pseudos which did not get
4378 a new hard register home from the live_{before,after} sets. */
4379 for (chain = reload_insn_chain; chain; chain = chain->next)
4381 HARD_REG_SET used_by_pseudos;
4382 HARD_REG_SET used_by_pseudos2;
4384 if (! ira_conflicts_p)
4386 /* Don't do it for IRA because IRA and the reload still can
4387 assign hard registers to the spilled pseudos on next
4388 reload iterations. */
4389 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
4390 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
4392 /* Mark any unallocated hard regs as available for spills. That
4393 makes inheritance work somewhat better. */
4394 if (chain->need_reload)
4396 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
4397 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
4398 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4400 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
4401 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
4402 /* Value of chain->used_spill_regs from previous iteration
4403 may be not included in the value calculated here because
4404 of possible removing caller-saves insns (see function
4405 delete_caller_save_insns. */
4406 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4407 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4411 CLEAR_REG_SET (&changed_allocation_pseudos);
4412 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4413 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
4415 int regno = reg_renumber[i];
4416 if (reg_old_renumber[i] == regno)
4417 continue;
4419 SET_REGNO_REG_SET (&changed_allocation_pseudos, i);
4421 alter_reg (i, reg_old_renumber[i], false);
4422 reg_old_renumber[i] = regno;
4423 if (dump_file)
4425 if (regno == -1)
4426 fprintf (dump_file, " Register %d now on stack.\n\n", i);
4427 else
4428 fprintf (dump_file, " Register %d now in %d.\n\n",
4429 i, reg_renumber[i]);
4433 return something_changed;
4436 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
4438 static void
4439 scan_paradoxical_subregs (rtx x)
4441 int i;
4442 const char *fmt;
4443 enum rtx_code code = GET_CODE (x);
4445 switch (code)
4447 case REG:
4448 case CONST:
4449 case SYMBOL_REF:
4450 case LABEL_REF:
4451 CASE_CONST_ANY:
4452 case CC0:
4453 case PC:
4454 case USE:
4455 case CLOBBER:
4456 return;
4458 case SUBREG:
4459 if (REG_P (SUBREG_REG (x))
4460 && (GET_MODE_SIZE (GET_MODE (x))
4461 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
4463 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4464 = GET_MODE_SIZE (GET_MODE (x));
4465 mark_home_live_1 (REGNO (SUBREG_REG (x)), GET_MODE (x));
4467 return;
4469 default:
4470 break;
4473 fmt = GET_RTX_FORMAT (code);
4474 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4476 if (fmt[i] == 'e')
4477 scan_paradoxical_subregs (XEXP (x, i));
4478 else if (fmt[i] == 'E')
4480 int j;
4481 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4482 scan_paradoxical_subregs (XVECEXP (x, i, j));
4487 /* *OP_PTR and *OTHER_PTR are two operands to a conceptual reload.
4488 If *OP_PTR is a paradoxical subreg, try to remove that subreg
4489 and apply the corresponding narrowing subreg to *OTHER_PTR.
4490 Return true if the operands were changed, false otherwise. */
4492 static bool
4493 strip_paradoxical_subreg (rtx *op_ptr, rtx *other_ptr)
4495 rtx op, inner, other, tem;
4497 op = *op_ptr;
4498 if (!paradoxical_subreg_p (op))
4499 return false;
4500 inner = SUBREG_REG (op);
4502 other = *other_ptr;
4503 tem = gen_lowpart_common (GET_MODE (inner), other);
4504 if (!tem)
4505 return false;
4507 /* If the lowpart operation turned a hard register into a subreg,
4508 rather than simplifying it to another hard register, then the
4509 mode change cannot be properly represented. For example, OTHER
4510 might be valid in its current mode, but not in the new one. */
4511 if (GET_CODE (tem) == SUBREG
4512 && REG_P (other)
4513 && HARD_REGISTER_P (other))
4514 return false;
4516 *op_ptr = inner;
4517 *other_ptr = tem;
4518 return true;
4521 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
4522 examine all of the reload insns between PREV and NEXT exclusive, and
4523 annotate all that may trap. */
4525 static void
4526 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
4528 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4529 if (note == NULL)
4530 return;
4531 if (!insn_could_throw_p (insn))
4532 remove_note (insn, note);
4533 copy_reg_eh_region_note_forward (note, NEXT_INSN (prev), next);
4536 /* Reload pseudo-registers into hard regs around each insn as needed.
4537 Additional register load insns are output before the insn that needs it
4538 and perhaps store insns after insns that modify the reloaded pseudo reg.
4540 reg_last_reload_reg and reg_reloaded_contents keep track of
4541 which registers are already available in reload registers.
4542 We update these for the reloads that we perform,
4543 as the insns are scanned. */
4545 static void
4546 reload_as_needed (int live_known)
4548 struct insn_chain *chain;
4549 #if defined (AUTO_INC_DEC)
4550 int i;
4551 #endif
4552 rtx x, marker;
4554 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
4555 memset (spill_reg_store, 0, sizeof spill_reg_store);
4556 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
4557 INIT_REG_SET (&reg_has_output_reload);
4558 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4559 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
4561 set_initial_elim_offsets ();
4563 /* Generate a marker insn that we will move around. */
4564 marker = emit_note (NOTE_INSN_DELETED);
4565 unlink_insn_chain (marker, marker);
4567 for (chain = reload_insn_chain; chain; chain = chain->next)
4569 rtx prev = 0;
4570 rtx insn = chain->insn;
4571 rtx old_next = NEXT_INSN (insn);
4572 #ifdef AUTO_INC_DEC
4573 rtx old_prev = PREV_INSN (insn);
4574 #endif
4576 /* If we pass a label, copy the offsets from the label information
4577 into the current offsets of each elimination. */
4578 if (LABEL_P (insn))
4579 set_offsets_for_label (insn);
4581 else if (INSN_P (insn))
4583 regset_head regs_to_forget;
4584 INIT_REG_SET (&regs_to_forget);
4585 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
4587 /* If this is a USE and CLOBBER of a MEM, ensure that any
4588 references to eliminable registers have been removed. */
4590 if ((GET_CODE (PATTERN (insn)) == USE
4591 || GET_CODE (PATTERN (insn)) == CLOBBER)
4592 && MEM_P (XEXP (PATTERN (insn), 0)))
4593 XEXP (XEXP (PATTERN (insn), 0), 0)
4594 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4595 GET_MODE (XEXP (PATTERN (insn), 0)),
4596 NULL_RTX);
4598 /* If we need to do register elimination processing, do so.
4599 This might delete the insn, in which case we are done. */
4600 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4602 eliminate_regs_in_insn (insn, 1);
4603 if (NOTE_P (insn))
4605 update_eliminable_offsets ();
4606 CLEAR_REG_SET (&regs_to_forget);
4607 continue;
4611 /* If need_elim is nonzero but need_reload is zero, one might think
4612 that we could simply set n_reloads to 0. However, find_reloads
4613 could have done some manipulation of the insn (such as swapping
4614 commutative operands), and these manipulations are lost during
4615 the first pass for every insn that needs register elimination.
4616 So the actions of find_reloads must be redone here. */
4618 if (! chain->need_elim && ! chain->need_reload
4619 && ! chain->need_operand_change)
4620 n_reloads = 0;
4621 /* First find the pseudo regs that must be reloaded for this insn.
4622 This info is returned in the tables reload_... (see reload.h).
4623 Also modify the body of INSN by substituting RELOAD
4624 rtx's for those pseudo regs. */
4625 else
4627 CLEAR_REG_SET (&reg_has_output_reload);
4628 CLEAR_HARD_REG_SET (reg_is_output_reload);
4630 find_reloads (insn, 1, spill_indirect_levels, live_known,
4631 spill_reg_order);
4634 if (n_reloads > 0)
4636 rtx next = NEXT_INSN (insn);
4637 rtx p;
4639 /* ??? PREV can get deleted by reload inheritance.
4640 Work around this by emitting a marker note. */
4641 prev = PREV_INSN (insn);
4642 reorder_insns_nobb (marker, marker, prev);
4644 /* Now compute which reload regs to reload them into. Perhaps
4645 reusing reload regs from previous insns, or else output
4646 load insns to reload them. Maybe output store insns too.
4647 Record the choices of reload reg in reload_reg_rtx. */
4648 choose_reload_regs (chain);
4650 /* Generate the insns to reload operands into or out of
4651 their reload regs. */
4652 emit_reload_insns (chain);
4654 /* Substitute the chosen reload regs from reload_reg_rtx
4655 into the insn's body (or perhaps into the bodies of other
4656 load and store insn that we just made for reloading
4657 and that we moved the structure into). */
4658 subst_reloads (insn);
4660 prev = PREV_INSN (marker);
4661 unlink_insn_chain (marker, marker);
4663 /* Adjust the exception region notes for loads and stores. */
4664 if (cfun->can_throw_non_call_exceptions && !CALL_P (insn))
4665 fixup_eh_region_note (insn, prev, next);
4667 /* Adjust the location of REG_ARGS_SIZE. */
4668 p = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4669 if (p)
4671 remove_note (insn, p);
4672 fixup_args_size_notes (prev, PREV_INSN (next),
4673 INTVAL (XEXP (p, 0)));
4676 /* If this was an ASM, make sure that all the reload insns
4677 we have generated are valid. If not, give an error
4678 and delete them. */
4679 if (asm_noperands (PATTERN (insn)) >= 0)
4680 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4681 if (p != insn && INSN_P (p)
4682 && GET_CODE (PATTERN (p)) != USE
4683 && (recog_memoized (p) < 0
4684 || (extract_insn (p), ! constrain_operands (1))))
4686 error_for_asm (insn,
4687 "%<asm%> operand requires "
4688 "impossible reload");
4689 delete_insn (p);
4693 if (num_eliminable && chain->need_elim)
4694 update_eliminable_offsets ();
4696 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4697 is no longer validly lying around to save a future reload.
4698 Note that this does not detect pseudos that were reloaded
4699 for this insn in order to be stored in
4700 (obeying register constraints). That is correct; such reload
4701 registers ARE still valid. */
4702 forget_marked_reloads (&regs_to_forget);
4703 CLEAR_REG_SET (&regs_to_forget);
4705 /* There may have been CLOBBER insns placed after INSN. So scan
4706 between INSN and NEXT and use them to forget old reloads. */
4707 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4708 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4709 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4711 #ifdef AUTO_INC_DEC
4712 /* Likewise for regs altered by auto-increment in this insn.
4713 REG_INC notes have been changed by reloading:
4714 find_reloads_address_1 records substitutions for them,
4715 which have been performed by subst_reloads above. */
4716 for (i = n_reloads - 1; i >= 0; i--)
4718 rtx in_reg = rld[i].in_reg;
4719 if (in_reg)
4721 enum rtx_code code = GET_CODE (in_reg);
4722 /* PRE_INC / PRE_DEC will have the reload register ending up
4723 with the same value as the stack slot, but that doesn't
4724 hold true for POST_INC / POST_DEC. Either we have to
4725 convert the memory access to a true POST_INC / POST_DEC,
4726 or we can't use the reload register for inheritance. */
4727 if ((code == POST_INC || code == POST_DEC)
4728 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4729 REGNO (rld[i].reg_rtx))
4730 /* Make sure it is the inc/dec pseudo, and not
4731 some other (e.g. output operand) pseudo. */
4732 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4733 == REGNO (XEXP (in_reg, 0))))
4736 rtx reload_reg = rld[i].reg_rtx;
4737 enum machine_mode mode = GET_MODE (reload_reg);
4738 int n = 0;
4739 rtx p;
4741 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4743 /* We really want to ignore REG_INC notes here, so
4744 use PATTERN (p) as argument to reg_set_p . */
4745 if (reg_set_p (reload_reg, PATTERN (p)))
4746 break;
4747 n = count_occurrences (PATTERN (p), reload_reg, 0);
4748 if (! n)
4749 continue;
4750 if (n == 1)
4752 rtx replace_reg
4753 = gen_rtx_fmt_e (code, mode, reload_reg);
4755 validate_replace_rtx_group (reload_reg,
4756 replace_reg, p);
4757 n = verify_changes (0);
4759 /* We must also verify that the constraints
4760 are met after the replacement. Make sure
4761 extract_insn is only called for an insn
4762 where the replacements were found to be
4763 valid so far. */
4764 if (n)
4766 extract_insn (p);
4767 n = constrain_operands (1);
4770 /* If the constraints were not met, then
4771 undo the replacement, else confirm it. */
4772 if (!n)
4773 cancel_changes (0);
4774 else
4775 confirm_change_group ();
4777 break;
4779 if (n == 1)
4781 add_reg_note (p, REG_INC, reload_reg);
4782 /* Mark this as having an output reload so that the
4783 REG_INC processing code below won't invalidate
4784 the reload for inheritance. */
4785 SET_HARD_REG_BIT (reg_is_output_reload,
4786 REGNO (reload_reg));
4787 SET_REGNO_REG_SET (&reg_has_output_reload,
4788 REGNO (XEXP (in_reg, 0)));
4790 else
4791 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4792 NULL);
4794 else if ((code == PRE_INC || code == PRE_DEC)
4795 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4796 REGNO (rld[i].reg_rtx))
4797 /* Make sure it is the inc/dec pseudo, and not
4798 some other (e.g. output operand) pseudo. */
4799 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4800 == REGNO (XEXP (in_reg, 0))))
4802 SET_HARD_REG_BIT (reg_is_output_reload,
4803 REGNO (rld[i].reg_rtx));
4804 SET_REGNO_REG_SET (&reg_has_output_reload,
4805 REGNO (XEXP (in_reg, 0)));
4807 else if (code == PRE_INC || code == PRE_DEC
4808 || code == POST_INC || code == POST_DEC)
4810 int in_regno = REGNO (XEXP (in_reg, 0));
4812 if (reg_last_reload_reg[in_regno] != NULL_RTX)
4814 int in_hard_regno;
4815 bool forget_p = true;
4817 in_hard_regno = REGNO (reg_last_reload_reg[in_regno]);
4818 if (TEST_HARD_REG_BIT (reg_reloaded_valid,
4819 in_hard_regno))
4821 for (x = old_prev ? NEXT_INSN (old_prev) : insn;
4822 x != old_next;
4823 x = NEXT_INSN (x))
4824 if (x == reg_reloaded_insn[in_hard_regno])
4826 forget_p = false;
4827 break;
4830 /* If for some reasons, we didn't set up
4831 reg_last_reload_reg in this insn,
4832 invalidate inheritance from previous
4833 insns for the incremented/decremented
4834 register. Such registers will be not in
4835 reg_has_output_reload. Invalidate it
4836 also if the corresponding element in
4837 reg_reloaded_insn is also
4838 invalidated. */
4839 if (forget_p)
4840 forget_old_reloads_1 (XEXP (in_reg, 0),
4841 NULL_RTX, NULL);
4846 /* If a pseudo that got a hard register is auto-incremented,
4847 we must purge records of copying it into pseudos without
4848 hard registers. */
4849 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4850 if (REG_NOTE_KIND (x) == REG_INC)
4852 /* See if this pseudo reg was reloaded in this insn.
4853 If so, its last-reload info is still valid
4854 because it is based on this insn's reload. */
4855 for (i = 0; i < n_reloads; i++)
4856 if (rld[i].out == XEXP (x, 0))
4857 break;
4859 if (i == n_reloads)
4860 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4862 #endif
4864 /* A reload reg's contents are unknown after a label. */
4865 if (LABEL_P (insn))
4866 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4868 /* Don't assume a reload reg is still good after a call insn
4869 if it is a call-used reg, or if it contains a value that will
4870 be partially clobbered by the call. */
4871 else if (CALL_P (insn))
4873 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4874 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4876 /* If this is a call to a setjmp-type function, we must not
4877 reuse any reload reg contents across the call; that will
4878 just be clobbered by other uses of the register in later
4879 code, before the longjmp. */
4880 if (find_reg_note (insn, REG_SETJMP, NULL_RTX))
4881 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4885 /* Clean up. */
4886 free (reg_last_reload_reg);
4887 CLEAR_REG_SET (&reg_has_output_reload);
4890 /* Discard all record of any value reloaded from X,
4891 or reloaded in X from someplace else;
4892 unless X is an output reload reg of the current insn.
4894 X may be a hard reg (the reload reg)
4895 or it may be a pseudo reg that was reloaded from.
4897 When DATA is non-NULL just mark the registers in regset
4898 to be forgotten later. */
4900 static void
4901 forget_old_reloads_1 (rtx x, const_rtx ignored ATTRIBUTE_UNUSED,
4902 void *data)
4904 unsigned int regno;
4905 unsigned int nr;
4906 regset regs = (regset) data;
4908 /* note_stores does give us subregs of hard regs,
4909 subreg_regno_offset requires a hard reg. */
4910 while (GET_CODE (x) == SUBREG)
4912 /* We ignore the subreg offset when calculating the regno,
4913 because we are using the entire underlying hard register
4914 below. */
4915 x = SUBREG_REG (x);
4918 if (!REG_P (x))
4919 return;
4921 regno = REGNO (x);
4923 if (regno >= FIRST_PSEUDO_REGISTER)
4924 nr = 1;
4925 else
4927 unsigned int i;
4929 nr = hard_regno_nregs[regno][GET_MODE (x)];
4930 /* Storing into a spilled-reg invalidates its contents.
4931 This can happen if a block-local pseudo is allocated to that reg
4932 and it wasn't spilled because this block's total need is 0.
4933 Then some insn might have an optional reload and use this reg. */
4934 if (!regs)
4935 for (i = 0; i < nr; i++)
4936 /* But don't do this if the reg actually serves as an output
4937 reload reg in the current instruction. */
4938 if (n_reloads == 0
4939 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4941 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4942 spill_reg_store[regno + i] = 0;
4946 if (regs)
4947 while (nr-- > 0)
4948 SET_REGNO_REG_SET (regs, regno + nr);
4949 else
4951 /* Since value of X has changed,
4952 forget any value previously copied from it. */
4954 while (nr-- > 0)
4955 /* But don't forget a copy if this is the output reload
4956 that establishes the copy's validity. */
4957 if (n_reloads == 0
4958 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4959 reg_last_reload_reg[regno + nr] = 0;
4963 /* Forget the reloads marked in regset by previous function. */
4964 static void
4965 forget_marked_reloads (regset regs)
4967 unsigned int reg;
4968 reg_set_iterator rsi;
4969 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4971 if (reg < FIRST_PSEUDO_REGISTER
4972 /* But don't do this if the reg actually serves as an output
4973 reload reg in the current instruction. */
4974 && (n_reloads == 0
4975 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4977 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4978 spill_reg_store[reg] = 0;
4980 if (n_reloads == 0
4981 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4982 reg_last_reload_reg[reg] = 0;
4986 /* The following HARD_REG_SETs indicate when each hard register is
4987 used for a reload of various parts of the current insn. */
4989 /* If reg is unavailable for all reloads. */
4990 static HARD_REG_SET reload_reg_unavailable;
4991 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
4992 static HARD_REG_SET reload_reg_used;
4993 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
4994 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
4995 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
4996 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
4997 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
4998 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
4999 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
5000 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
5001 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
5002 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5003 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
5004 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
5005 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
5006 static HARD_REG_SET reload_reg_used_in_op_addr;
5007 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
5008 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
5009 /* If reg is in use for a RELOAD_FOR_INSN reload. */
5010 static HARD_REG_SET reload_reg_used_in_insn;
5011 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
5012 static HARD_REG_SET reload_reg_used_in_other_addr;
5014 /* If reg is in use as a reload reg for any sort of reload. */
5015 static HARD_REG_SET reload_reg_used_at_all;
5017 /* If reg is use as an inherited reload. We just mark the first register
5018 in the group. */
5019 static HARD_REG_SET reload_reg_used_for_inherit;
5021 /* Records which hard regs are used in any way, either as explicit use or
5022 by being allocated to a pseudo during any point of the current insn. */
5023 static HARD_REG_SET reg_used_in_insn;
5025 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
5026 TYPE. MODE is used to indicate how many consecutive regs are
5027 actually used. */
5029 static void
5030 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
5031 enum machine_mode mode)
5033 switch (type)
5035 case RELOAD_OTHER:
5036 add_to_hard_reg_set (&reload_reg_used, mode, regno);
5037 break;
5039 case RELOAD_FOR_INPUT_ADDRESS:
5040 add_to_hard_reg_set (&reload_reg_used_in_input_addr[opnum], mode, regno);
5041 break;
5043 case RELOAD_FOR_INPADDR_ADDRESS:
5044 add_to_hard_reg_set (&reload_reg_used_in_inpaddr_addr[opnum], mode, regno);
5045 break;
5047 case RELOAD_FOR_OUTPUT_ADDRESS:
5048 add_to_hard_reg_set (&reload_reg_used_in_output_addr[opnum], mode, regno);
5049 break;
5051 case RELOAD_FOR_OUTADDR_ADDRESS:
5052 add_to_hard_reg_set (&reload_reg_used_in_outaddr_addr[opnum], mode, regno);
5053 break;
5055 case RELOAD_FOR_OPERAND_ADDRESS:
5056 add_to_hard_reg_set (&reload_reg_used_in_op_addr, mode, regno);
5057 break;
5059 case RELOAD_FOR_OPADDR_ADDR:
5060 add_to_hard_reg_set (&reload_reg_used_in_op_addr_reload, mode, regno);
5061 break;
5063 case RELOAD_FOR_OTHER_ADDRESS:
5064 add_to_hard_reg_set (&reload_reg_used_in_other_addr, mode, regno);
5065 break;
5067 case RELOAD_FOR_INPUT:
5068 add_to_hard_reg_set (&reload_reg_used_in_input[opnum], mode, regno);
5069 break;
5071 case RELOAD_FOR_OUTPUT:
5072 add_to_hard_reg_set (&reload_reg_used_in_output[opnum], mode, regno);
5073 break;
5075 case RELOAD_FOR_INSN:
5076 add_to_hard_reg_set (&reload_reg_used_in_insn, mode, regno);
5077 break;
5080 add_to_hard_reg_set (&reload_reg_used_at_all, mode, regno);
5083 /* Similarly, but show REGNO is no longer in use for a reload. */
5085 static void
5086 clear_reload_reg_in_use (unsigned int regno, int opnum,
5087 enum reload_type type, enum machine_mode mode)
5089 unsigned int nregs = hard_regno_nregs[regno][mode];
5090 unsigned int start_regno, end_regno, r;
5091 int i;
5092 /* A complication is that for some reload types, inheritance might
5093 allow multiple reloads of the same types to share a reload register.
5094 We set check_opnum if we have to check only reloads with the same
5095 operand number, and check_any if we have to check all reloads. */
5096 int check_opnum = 0;
5097 int check_any = 0;
5098 HARD_REG_SET *used_in_set;
5100 switch (type)
5102 case RELOAD_OTHER:
5103 used_in_set = &reload_reg_used;
5104 break;
5106 case RELOAD_FOR_INPUT_ADDRESS:
5107 used_in_set = &reload_reg_used_in_input_addr[opnum];
5108 break;
5110 case RELOAD_FOR_INPADDR_ADDRESS:
5111 check_opnum = 1;
5112 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
5113 break;
5115 case RELOAD_FOR_OUTPUT_ADDRESS:
5116 used_in_set = &reload_reg_used_in_output_addr[opnum];
5117 break;
5119 case RELOAD_FOR_OUTADDR_ADDRESS:
5120 check_opnum = 1;
5121 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
5122 break;
5124 case RELOAD_FOR_OPERAND_ADDRESS:
5125 used_in_set = &reload_reg_used_in_op_addr;
5126 break;
5128 case RELOAD_FOR_OPADDR_ADDR:
5129 check_any = 1;
5130 used_in_set = &reload_reg_used_in_op_addr_reload;
5131 break;
5133 case RELOAD_FOR_OTHER_ADDRESS:
5134 used_in_set = &reload_reg_used_in_other_addr;
5135 check_any = 1;
5136 break;
5138 case RELOAD_FOR_INPUT:
5139 used_in_set = &reload_reg_used_in_input[opnum];
5140 break;
5142 case RELOAD_FOR_OUTPUT:
5143 used_in_set = &reload_reg_used_in_output[opnum];
5144 break;
5146 case RELOAD_FOR_INSN:
5147 used_in_set = &reload_reg_used_in_insn;
5148 break;
5149 default:
5150 gcc_unreachable ();
5152 /* We resolve conflicts with remaining reloads of the same type by
5153 excluding the intervals of reload registers by them from the
5154 interval of freed reload registers. Since we only keep track of
5155 one set of interval bounds, we might have to exclude somewhat
5156 more than what would be necessary if we used a HARD_REG_SET here.
5157 But this should only happen very infrequently, so there should
5158 be no reason to worry about it. */
5160 start_regno = regno;
5161 end_regno = regno + nregs;
5162 if (check_opnum || check_any)
5164 for (i = n_reloads - 1; i >= 0; i--)
5166 if (rld[i].when_needed == type
5167 && (check_any || rld[i].opnum == opnum)
5168 && rld[i].reg_rtx)
5170 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
5171 unsigned int conflict_end
5172 = end_hard_regno (rld[i].mode, conflict_start);
5174 /* If there is an overlap with the first to-be-freed register,
5175 adjust the interval start. */
5176 if (conflict_start <= start_regno && conflict_end > start_regno)
5177 start_regno = conflict_end;
5178 /* Otherwise, if there is a conflict with one of the other
5179 to-be-freed registers, adjust the interval end. */
5180 if (conflict_start > start_regno && conflict_start < end_regno)
5181 end_regno = conflict_start;
5186 for (r = start_regno; r < end_regno; r++)
5187 CLEAR_HARD_REG_BIT (*used_in_set, r);
5190 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
5191 specified by OPNUM and TYPE. */
5193 static int
5194 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
5196 int i;
5198 /* In use for a RELOAD_OTHER means it's not available for anything. */
5199 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
5200 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5201 return 0;
5203 switch (type)
5205 case RELOAD_OTHER:
5206 /* In use for anything means we can't use it for RELOAD_OTHER. */
5207 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
5208 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5209 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
5210 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5211 return 0;
5213 for (i = 0; i < reload_n_operands; i++)
5214 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5215 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5216 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5217 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5218 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5219 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5220 return 0;
5222 return 1;
5224 case RELOAD_FOR_INPUT:
5225 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5226 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
5227 return 0;
5229 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5230 return 0;
5232 /* If it is used for some other input, can't use it. */
5233 for (i = 0; i < reload_n_operands; i++)
5234 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5235 return 0;
5237 /* If it is used in a later operand's address, can't use it. */
5238 for (i = opnum + 1; i < reload_n_operands; i++)
5239 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5240 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5241 return 0;
5243 return 1;
5245 case RELOAD_FOR_INPUT_ADDRESS:
5246 /* Can't use a register if it is used for an input address for this
5247 operand or used as an input in an earlier one. */
5248 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
5249 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
5250 return 0;
5252 for (i = 0; i < opnum; i++)
5253 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5254 return 0;
5256 return 1;
5258 case RELOAD_FOR_INPADDR_ADDRESS:
5259 /* Can't use a register if it is used for an input address
5260 for this operand or used as an input in an earlier
5261 one. */
5262 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
5263 return 0;
5265 for (i = 0; i < opnum; i++)
5266 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5267 return 0;
5269 return 1;
5271 case RELOAD_FOR_OUTPUT_ADDRESS:
5272 /* Can't use a register if it is used for an output address for this
5273 operand or used as an output in this or a later operand. Note
5274 that multiple output operands are emitted in reverse order, so
5275 the conflicting ones are those with lower indices. */
5276 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
5277 return 0;
5279 for (i = 0; i <= opnum; i++)
5280 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5281 return 0;
5283 return 1;
5285 case RELOAD_FOR_OUTADDR_ADDRESS:
5286 /* Can't use a register if it is used for an output address
5287 for this operand or used as an output in this or a
5288 later operand. Note that multiple output operands are
5289 emitted in reverse order, so the conflicting ones are
5290 those with lower indices. */
5291 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
5292 return 0;
5294 for (i = 0; i <= opnum; i++)
5295 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5296 return 0;
5298 return 1;
5300 case RELOAD_FOR_OPERAND_ADDRESS:
5301 for (i = 0; i < reload_n_operands; i++)
5302 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5303 return 0;
5305 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5306 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5308 case RELOAD_FOR_OPADDR_ADDR:
5309 for (i = 0; i < reload_n_operands; i++)
5310 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5311 return 0;
5313 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
5315 case RELOAD_FOR_OUTPUT:
5316 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
5317 outputs, or an operand address for this or an earlier output.
5318 Note that multiple output operands are emitted in reverse order,
5319 so the conflicting ones are those with higher indices. */
5320 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5321 return 0;
5323 for (i = 0; i < reload_n_operands; i++)
5324 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5325 return 0;
5327 for (i = opnum; i < reload_n_operands; i++)
5328 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5329 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5330 return 0;
5332 return 1;
5334 case RELOAD_FOR_INSN:
5335 for (i = 0; i < reload_n_operands; i++)
5336 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5337 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5338 return 0;
5340 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5341 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5343 case RELOAD_FOR_OTHER_ADDRESS:
5344 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
5346 default:
5347 gcc_unreachable ();
5351 /* Return 1 if the value in reload reg REGNO, as used by the reload with
5352 the number RELOADNUM, is still available in REGNO at the end of the insn.
5354 We can assume that the reload reg was already tested for availability
5355 at the time it is needed, and we should not check this again,
5356 in case the reg has already been marked in use. */
5358 static int
5359 reload_reg_reaches_end_p (unsigned int regno, int reloadnum)
5361 int opnum = rld[reloadnum].opnum;
5362 enum reload_type type = rld[reloadnum].when_needed;
5363 int i;
5365 /* See if there is a reload with the same type for this operand, using
5366 the same register. This case is not handled by the code below. */
5367 for (i = reloadnum + 1; i < n_reloads; i++)
5369 rtx reg;
5370 int nregs;
5372 if (rld[i].opnum != opnum || rld[i].when_needed != type)
5373 continue;
5374 reg = rld[i].reg_rtx;
5375 if (reg == NULL_RTX)
5376 continue;
5377 nregs = hard_regno_nregs[REGNO (reg)][GET_MODE (reg)];
5378 if (regno >= REGNO (reg) && regno < REGNO (reg) + nregs)
5379 return 0;
5382 switch (type)
5384 case RELOAD_OTHER:
5385 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
5386 its value must reach the end. */
5387 return 1;
5389 /* If this use is for part of the insn,
5390 its value reaches if no subsequent part uses the same register.
5391 Just like the above function, don't try to do this with lots
5392 of fallthroughs. */
5394 case RELOAD_FOR_OTHER_ADDRESS:
5395 /* Here we check for everything else, since these don't conflict
5396 with anything else and everything comes later. */
5398 for (i = 0; i < reload_n_operands; i++)
5399 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5400 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5401 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5402 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5403 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5404 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5405 return 0;
5407 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5408 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
5409 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5410 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5412 case RELOAD_FOR_INPUT_ADDRESS:
5413 case RELOAD_FOR_INPADDR_ADDRESS:
5414 /* Similar, except that we check only for this and subsequent inputs
5415 and the address of only subsequent inputs and we do not need
5416 to check for RELOAD_OTHER objects since they are known not to
5417 conflict. */
5419 for (i = opnum; i < reload_n_operands; i++)
5420 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5421 return 0;
5423 /* Reload register of reload with type RELOAD_FOR_INPADDR_ADDRESS
5424 could be killed if the register is also used by reload with type
5425 RELOAD_FOR_INPUT_ADDRESS, so check it. */
5426 if (type == RELOAD_FOR_INPADDR_ADDRESS
5427 && TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno))
5428 return 0;
5430 for (i = opnum + 1; i < reload_n_operands; i++)
5431 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5432 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5433 return 0;
5435 for (i = 0; i < reload_n_operands; i++)
5436 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5437 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5438 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5439 return 0;
5441 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5442 return 0;
5444 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5445 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5446 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5448 case RELOAD_FOR_INPUT:
5449 /* Similar to input address, except we start at the next operand for
5450 both input and input address and we do not check for
5451 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5452 would conflict. */
5454 for (i = opnum + 1; i < reload_n_operands; i++)
5455 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5456 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5457 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5458 return 0;
5460 /* ... fall through ... */
5462 case RELOAD_FOR_OPERAND_ADDRESS:
5463 /* Check outputs and their addresses. */
5465 for (i = 0; i < reload_n_operands; i++)
5466 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5467 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5468 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5469 return 0;
5471 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
5473 case RELOAD_FOR_OPADDR_ADDR:
5474 for (i = 0; i < reload_n_operands; i++)
5475 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5476 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5477 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5478 return 0;
5480 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5481 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5482 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5484 case RELOAD_FOR_INSN:
5485 /* These conflict with other outputs with RELOAD_OTHER. So
5486 we need only check for output addresses. */
5488 opnum = reload_n_operands;
5490 /* ... fall through ... */
5492 case RELOAD_FOR_OUTPUT:
5493 case RELOAD_FOR_OUTPUT_ADDRESS:
5494 case RELOAD_FOR_OUTADDR_ADDRESS:
5495 /* We already know these can't conflict with a later output. So the
5496 only thing to check are later output addresses.
5497 Note that multiple output operands are emitted in reverse order,
5498 so the conflicting ones are those with lower indices. */
5499 for (i = 0; i < opnum; i++)
5500 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5501 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5502 return 0;
5504 /* Reload register of reload with type RELOAD_FOR_OUTADDR_ADDRESS
5505 could be killed if the register is also used by reload with type
5506 RELOAD_FOR_OUTPUT_ADDRESS, so check it. */
5507 if (type == RELOAD_FOR_OUTADDR_ADDRESS
5508 && TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
5509 return 0;
5511 return 1;
5513 default:
5514 gcc_unreachable ();
5518 /* Like reload_reg_reaches_end_p, but check that the condition holds for
5519 every register in REG. */
5521 static bool
5522 reload_reg_rtx_reaches_end_p (rtx reg, int reloadnum)
5524 unsigned int i;
5526 for (i = REGNO (reg); i < END_REGNO (reg); i++)
5527 if (!reload_reg_reaches_end_p (i, reloadnum))
5528 return false;
5529 return true;
5533 /* Returns whether R1 and R2 are uniquely chained: the value of one
5534 is used by the other, and that value is not used by any other
5535 reload for this insn. This is used to partially undo the decision
5536 made in find_reloads when in the case of multiple
5537 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
5538 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
5539 reloads. This code tries to avoid the conflict created by that
5540 change. It might be cleaner to explicitly keep track of which
5541 RELOAD_FOR_OPADDR_ADDR reload is associated with which
5542 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
5543 this after the fact. */
5544 static bool
5545 reloads_unique_chain_p (int r1, int r2)
5547 int i;
5549 /* We only check input reloads. */
5550 if (! rld[r1].in || ! rld[r2].in)
5551 return false;
5553 /* Avoid anything with output reloads. */
5554 if (rld[r1].out || rld[r2].out)
5555 return false;
5557 /* "chained" means one reload is a component of the other reload,
5558 not the same as the other reload. */
5559 if (rld[r1].opnum != rld[r2].opnum
5560 || rtx_equal_p (rld[r1].in, rld[r2].in)
5561 || rld[r1].optional || rld[r2].optional
5562 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
5563 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
5564 return false;
5566 for (i = 0; i < n_reloads; i ++)
5567 /* Look for input reloads that aren't our two */
5568 if (i != r1 && i != r2 && rld[i].in)
5570 /* If our reload is mentioned at all, it isn't a simple chain. */
5571 if (reg_mentioned_p (rld[r1].in, rld[i].in))
5572 return false;
5574 return true;
5577 /* The recursive function change all occurrences of WHAT in *WHERE
5578 to REPL. */
5579 static void
5580 substitute (rtx *where, const_rtx what, rtx repl)
5582 const char *fmt;
5583 int i;
5584 enum rtx_code code;
5586 if (*where == 0)
5587 return;
5589 if (*where == what || rtx_equal_p (*where, what))
5591 /* Record the location of the changed rtx. */
5592 VEC_safe_push (rtx_p, heap, substitute_stack, where);
5593 *where = repl;
5594 return;
5597 code = GET_CODE (*where);
5598 fmt = GET_RTX_FORMAT (code);
5599 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5601 if (fmt[i] == 'E')
5603 int j;
5605 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
5606 substitute (&XVECEXP (*where, i, j), what, repl);
5608 else if (fmt[i] == 'e')
5609 substitute (&XEXP (*where, i), what, repl);
5613 /* The function returns TRUE if chain of reload R1 and R2 (in any
5614 order) can be evaluated without usage of intermediate register for
5615 the reload containing another reload. It is important to see
5616 gen_reload to understand what the function is trying to do. As an
5617 example, let us have reload chain
5619 r2: const
5620 r1: <something> + const
5622 and reload R2 got reload reg HR. The function returns true if
5623 there is a correct insn HR = HR + <something>. Otherwise,
5624 gen_reload will use intermediate register (and this is the reload
5625 reg for R1) to reload <something>.
5627 We need this function to find a conflict for chain reloads. In our
5628 example, if HR = HR + <something> is incorrect insn, then we cannot
5629 use HR as a reload register for R2. If we do use it then we get a
5630 wrong code:
5632 HR = const
5633 HR = <something>
5634 HR = HR + HR
5637 static bool
5638 gen_reload_chain_without_interm_reg_p (int r1, int r2)
5640 /* Assume other cases in gen_reload are not possible for
5641 chain reloads or do need an intermediate hard registers. */
5642 bool result = true;
5643 int regno, n, code;
5644 rtx out, in, insn;
5645 rtx last = get_last_insn ();
5647 /* Make r2 a component of r1. */
5648 if (reg_mentioned_p (rld[r1].in, rld[r2].in))
5650 n = r1;
5651 r1 = r2;
5652 r2 = n;
5654 gcc_assert (reg_mentioned_p (rld[r2].in, rld[r1].in));
5655 regno = rld[r1].regno >= 0 ? rld[r1].regno : rld[r2].regno;
5656 gcc_assert (regno >= 0);
5657 out = gen_rtx_REG (rld[r1].mode, regno);
5658 in = rld[r1].in;
5659 substitute (&in, rld[r2].in, gen_rtx_REG (rld[r2].mode, regno));
5661 /* If IN is a paradoxical SUBREG, remove it and try to put the
5662 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
5663 strip_paradoxical_subreg (&in, &out);
5665 if (GET_CODE (in) == PLUS
5666 && (REG_P (XEXP (in, 0))
5667 || GET_CODE (XEXP (in, 0)) == SUBREG
5668 || MEM_P (XEXP (in, 0)))
5669 && (REG_P (XEXP (in, 1))
5670 || GET_CODE (XEXP (in, 1)) == SUBREG
5671 || CONSTANT_P (XEXP (in, 1))
5672 || MEM_P (XEXP (in, 1))))
5674 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
5675 code = recog_memoized (insn);
5676 result = false;
5678 if (code >= 0)
5680 extract_insn (insn);
5681 /* We want constrain operands to treat this insn strictly in
5682 its validity determination, i.e., the way it would after
5683 reload has completed. */
5684 result = constrain_operands (1);
5687 delete_insns_since (last);
5690 /* Restore the original value at each changed address within R1. */
5691 while (!VEC_empty (rtx_p, substitute_stack))
5693 rtx *where = VEC_pop (rtx_p, substitute_stack);
5694 *where = rld[r2].in;
5697 return result;
5700 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5701 Return 0 otherwise.
5703 This function uses the same algorithm as reload_reg_free_p above. */
5705 static int
5706 reloads_conflict (int r1, int r2)
5708 enum reload_type r1_type = rld[r1].when_needed;
5709 enum reload_type r2_type = rld[r2].when_needed;
5710 int r1_opnum = rld[r1].opnum;
5711 int r2_opnum = rld[r2].opnum;
5713 /* RELOAD_OTHER conflicts with everything. */
5714 if (r2_type == RELOAD_OTHER)
5715 return 1;
5717 /* Otherwise, check conflicts differently for each type. */
5719 switch (r1_type)
5721 case RELOAD_FOR_INPUT:
5722 return (r2_type == RELOAD_FOR_INSN
5723 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5724 || r2_type == RELOAD_FOR_OPADDR_ADDR
5725 || r2_type == RELOAD_FOR_INPUT
5726 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5727 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5728 && r2_opnum > r1_opnum));
5730 case RELOAD_FOR_INPUT_ADDRESS:
5731 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5732 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5734 case RELOAD_FOR_INPADDR_ADDRESS:
5735 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5736 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5738 case RELOAD_FOR_OUTPUT_ADDRESS:
5739 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5740 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5742 case RELOAD_FOR_OUTADDR_ADDRESS:
5743 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5744 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5746 case RELOAD_FOR_OPERAND_ADDRESS:
5747 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5748 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
5749 && (!reloads_unique_chain_p (r1, r2)
5750 || !gen_reload_chain_without_interm_reg_p (r1, r2))));
5752 case RELOAD_FOR_OPADDR_ADDR:
5753 return (r2_type == RELOAD_FOR_INPUT
5754 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5756 case RELOAD_FOR_OUTPUT:
5757 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5758 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5759 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5760 && r2_opnum >= r1_opnum));
5762 case RELOAD_FOR_INSN:
5763 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5764 || r2_type == RELOAD_FOR_INSN
5765 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5767 case RELOAD_FOR_OTHER_ADDRESS:
5768 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5770 case RELOAD_OTHER:
5771 return 1;
5773 default:
5774 gcc_unreachable ();
5778 /* Indexed by reload number, 1 if incoming value
5779 inherited from previous insns. */
5780 static char reload_inherited[MAX_RELOADS];
5782 /* For an inherited reload, this is the insn the reload was inherited from,
5783 if we know it. Otherwise, this is 0. */
5784 static rtx reload_inheritance_insn[MAX_RELOADS];
5786 /* If nonzero, this is a place to get the value of the reload,
5787 rather than using reload_in. */
5788 static rtx reload_override_in[MAX_RELOADS];
5790 /* For each reload, the hard register number of the register used,
5791 or -1 if we did not need a register for this reload. */
5792 static int reload_spill_index[MAX_RELOADS];
5794 /* Index X is the value of rld[X].reg_rtx, adjusted for the input mode. */
5795 static rtx reload_reg_rtx_for_input[MAX_RELOADS];
5797 /* Index X is the value of rld[X].reg_rtx, adjusted for the output mode. */
5798 static rtx reload_reg_rtx_for_output[MAX_RELOADS];
5800 /* Subroutine of free_for_value_p, used to check a single register.
5801 START_REGNO is the starting regno of the full reload register
5802 (possibly comprising multiple hard registers) that we are considering. */
5804 static int
5805 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
5806 enum reload_type type, rtx value, rtx out,
5807 int reloadnum, int ignore_address_reloads)
5809 int time1;
5810 /* Set if we see an input reload that must not share its reload register
5811 with any new earlyclobber, but might otherwise share the reload
5812 register with an output or input-output reload. */
5813 int check_earlyclobber = 0;
5814 int i;
5815 int copy = 0;
5817 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5818 return 0;
5820 if (out == const0_rtx)
5822 copy = 1;
5823 out = NULL_RTX;
5826 /* We use some pseudo 'time' value to check if the lifetimes of the
5827 new register use would overlap with the one of a previous reload
5828 that is not read-only or uses a different value.
5829 The 'time' used doesn't have to be linear in any shape or form, just
5830 monotonic.
5831 Some reload types use different 'buckets' for each operand.
5832 So there are MAX_RECOG_OPERANDS different time values for each
5833 such reload type.
5834 We compute TIME1 as the time when the register for the prospective
5835 new reload ceases to be live, and TIME2 for each existing
5836 reload as the time when that the reload register of that reload
5837 becomes live.
5838 Where there is little to be gained by exact lifetime calculations,
5839 we just make conservative assumptions, i.e. a longer lifetime;
5840 this is done in the 'default:' cases. */
5841 switch (type)
5843 case RELOAD_FOR_OTHER_ADDRESS:
5844 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
5845 time1 = copy ? 0 : 1;
5846 break;
5847 case RELOAD_OTHER:
5848 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5849 break;
5850 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5851 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5852 respectively, to the time values for these, we get distinct time
5853 values. To get distinct time values for each operand, we have to
5854 multiply opnum by at least three. We round that up to four because
5855 multiply by four is often cheaper. */
5856 case RELOAD_FOR_INPADDR_ADDRESS:
5857 time1 = opnum * 4 + 2;
5858 break;
5859 case RELOAD_FOR_INPUT_ADDRESS:
5860 time1 = opnum * 4 + 3;
5861 break;
5862 case RELOAD_FOR_INPUT:
5863 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5864 executes (inclusive). */
5865 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5866 break;
5867 case RELOAD_FOR_OPADDR_ADDR:
5868 /* opnum * 4 + 4
5869 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5870 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5871 break;
5872 case RELOAD_FOR_OPERAND_ADDRESS:
5873 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5874 is executed. */
5875 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5876 break;
5877 case RELOAD_FOR_OUTADDR_ADDRESS:
5878 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5879 break;
5880 case RELOAD_FOR_OUTPUT_ADDRESS:
5881 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5882 break;
5883 default:
5884 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5887 for (i = 0; i < n_reloads; i++)
5889 rtx reg = rld[i].reg_rtx;
5890 if (reg && REG_P (reg)
5891 && ((unsigned) regno - true_regnum (reg)
5892 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5893 && i != reloadnum)
5895 rtx other_input = rld[i].in;
5897 /* If the other reload loads the same input value, that
5898 will not cause a conflict only if it's loading it into
5899 the same register. */
5900 if (true_regnum (reg) != start_regno)
5901 other_input = NULL_RTX;
5902 if (! other_input || ! rtx_equal_p (other_input, value)
5903 || rld[i].out || out)
5905 int time2;
5906 switch (rld[i].when_needed)
5908 case RELOAD_FOR_OTHER_ADDRESS:
5909 time2 = 0;
5910 break;
5911 case RELOAD_FOR_INPADDR_ADDRESS:
5912 /* find_reloads makes sure that a
5913 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5914 by at most one - the first -
5915 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5916 address reload is inherited, the address address reload
5917 goes away, so we can ignore this conflict. */
5918 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5919 && ignore_address_reloads
5920 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5921 Then the address address is still needed to store
5922 back the new address. */
5923 && ! rld[reloadnum].out)
5924 continue;
5925 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5926 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5927 reloads go away. */
5928 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5929 && ignore_address_reloads
5930 /* Unless we are reloading an auto_inc expression. */
5931 && ! rld[reloadnum].out)
5932 continue;
5933 time2 = rld[i].opnum * 4 + 2;
5934 break;
5935 case RELOAD_FOR_INPUT_ADDRESS:
5936 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5937 && ignore_address_reloads
5938 && ! rld[reloadnum].out)
5939 continue;
5940 time2 = rld[i].opnum * 4 + 3;
5941 break;
5942 case RELOAD_FOR_INPUT:
5943 time2 = rld[i].opnum * 4 + 4;
5944 check_earlyclobber = 1;
5945 break;
5946 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5947 == MAX_RECOG_OPERAND * 4 */
5948 case RELOAD_FOR_OPADDR_ADDR:
5949 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5950 && ignore_address_reloads
5951 && ! rld[reloadnum].out)
5952 continue;
5953 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5954 break;
5955 case RELOAD_FOR_OPERAND_ADDRESS:
5956 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5957 check_earlyclobber = 1;
5958 break;
5959 case RELOAD_FOR_INSN:
5960 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5961 break;
5962 case RELOAD_FOR_OUTPUT:
5963 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5964 instruction is executed. */
5965 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5966 break;
5967 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5968 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5969 value. */
5970 case RELOAD_FOR_OUTADDR_ADDRESS:
5971 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5972 && ignore_address_reloads
5973 && ! rld[reloadnum].out)
5974 continue;
5975 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5976 break;
5977 case RELOAD_FOR_OUTPUT_ADDRESS:
5978 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5979 break;
5980 case RELOAD_OTHER:
5981 /* If there is no conflict in the input part, handle this
5982 like an output reload. */
5983 if (! rld[i].in || rtx_equal_p (other_input, value))
5985 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5986 /* Earlyclobbered outputs must conflict with inputs. */
5987 if (earlyclobber_operand_p (rld[i].out))
5988 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5990 break;
5992 time2 = 1;
5993 /* RELOAD_OTHER might be live beyond instruction execution,
5994 but this is not obvious when we set time2 = 1. So check
5995 here if there might be a problem with the new reload
5996 clobbering the register used by the RELOAD_OTHER. */
5997 if (out)
5998 return 0;
5999 break;
6000 default:
6001 return 0;
6003 if ((time1 >= time2
6004 && (! rld[i].in || rld[i].out
6005 || ! rtx_equal_p (other_input, value)))
6006 || (out && rld[reloadnum].out_reg
6007 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
6008 return 0;
6013 /* Earlyclobbered outputs must conflict with inputs. */
6014 if (check_earlyclobber && out && earlyclobber_operand_p (out))
6015 return 0;
6017 return 1;
6020 /* Return 1 if the value in reload reg REGNO, as used by a reload
6021 needed for the part of the insn specified by OPNUM and TYPE,
6022 may be used to load VALUE into it.
6024 MODE is the mode in which the register is used, this is needed to
6025 determine how many hard regs to test.
6027 Other read-only reloads with the same value do not conflict
6028 unless OUT is nonzero and these other reloads have to live while
6029 output reloads live.
6030 If OUT is CONST0_RTX, this is a special case: it means that the
6031 test should not be for using register REGNO as reload register, but
6032 for copying from register REGNO into the reload register.
6034 RELOADNUM is the number of the reload we want to load this value for;
6035 a reload does not conflict with itself.
6037 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
6038 reloads that load an address for the very reload we are considering.
6040 The caller has to make sure that there is no conflict with the return
6041 register. */
6043 static int
6044 free_for_value_p (int regno, enum machine_mode mode, int opnum,
6045 enum reload_type type, rtx value, rtx out, int reloadnum,
6046 int ignore_address_reloads)
6048 int nregs = hard_regno_nregs[regno][mode];
6049 while (nregs-- > 0)
6050 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
6051 value, out, reloadnum,
6052 ignore_address_reloads))
6053 return 0;
6054 return 1;
6057 /* Return nonzero if the rtx X is invariant over the current function. */
6058 /* ??? Actually, the places where we use this expect exactly what is
6059 tested here, and not everything that is function invariant. In
6060 particular, the frame pointer and arg pointer are special cased;
6061 pic_offset_table_rtx is not, and we must not spill these things to
6062 memory. */
6065 function_invariant_p (const_rtx x)
6067 if (CONSTANT_P (x))
6068 return 1;
6069 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
6070 return 1;
6071 if (GET_CODE (x) == PLUS
6072 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
6073 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6074 return 1;
6075 return 0;
6078 /* Determine whether the reload reg X overlaps any rtx'es used for
6079 overriding inheritance. Return nonzero if so. */
6081 static int
6082 conflicts_with_override (rtx x)
6084 int i;
6085 for (i = 0; i < n_reloads; i++)
6086 if (reload_override_in[i]
6087 && reg_overlap_mentioned_p (x, reload_override_in[i]))
6088 return 1;
6089 return 0;
6092 /* Give an error message saying we failed to find a reload for INSN,
6093 and clear out reload R. */
6094 static void
6095 failed_reload (rtx insn, int r)
6097 if (asm_noperands (PATTERN (insn)) < 0)
6098 /* It's the compiler's fault. */
6099 fatal_insn ("could not find a spill register", insn);
6101 /* It's the user's fault; the operand's mode and constraint
6102 don't match. Disable this reload so we don't crash in final. */
6103 error_for_asm (insn,
6104 "%<asm%> operand constraint incompatible with operand size");
6105 rld[r].in = 0;
6106 rld[r].out = 0;
6107 rld[r].reg_rtx = 0;
6108 rld[r].optional = 1;
6109 rld[r].secondary_p = 1;
6112 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
6113 for reload R. If it's valid, get an rtx for it. Return nonzero if
6114 successful. */
6115 static int
6116 set_reload_reg (int i, int r)
6118 /* regno is 'set but not used' if HARD_REGNO_MODE_OK doesn't use its first
6119 parameter. */
6120 int regno ATTRIBUTE_UNUSED;
6121 rtx reg = spill_reg_rtx[i];
6123 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
6124 spill_reg_rtx[i] = reg
6125 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
6127 regno = true_regnum (reg);
6129 /* Detect when the reload reg can't hold the reload mode.
6130 This used to be one `if', but Sequent compiler can't handle that. */
6131 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
6133 enum machine_mode test_mode = VOIDmode;
6134 if (rld[r].in)
6135 test_mode = GET_MODE (rld[r].in);
6136 /* If rld[r].in has VOIDmode, it means we will load it
6137 in whatever mode the reload reg has: to wit, rld[r].mode.
6138 We have already tested that for validity. */
6139 /* Aside from that, we need to test that the expressions
6140 to reload from or into have modes which are valid for this
6141 reload register. Otherwise the reload insns would be invalid. */
6142 if (! (rld[r].in != 0 && test_mode != VOIDmode
6143 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
6144 if (! (rld[r].out != 0
6145 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
6147 /* The reg is OK. */
6148 last_spill_reg = i;
6150 /* Mark as in use for this insn the reload regs we use
6151 for this. */
6152 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
6153 rld[r].when_needed, rld[r].mode);
6155 rld[r].reg_rtx = reg;
6156 reload_spill_index[r] = spill_regs[i];
6157 return 1;
6160 return 0;
6163 /* Find a spill register to use as a reload register for reload R.
6164 LAST_RELOAD is nonzero if this is the last reload for the insn being
6165 processed.
6167 Set rld[R].reg_rtx to the register allocated.
6169 We return 1 if successful, or 0 if we couldn't find a spill reg and
6170 we didn't change anything. */
6172 static int
6173 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
6174 int last_reload)
6176 int i, pass, count;
6178 /* If we put this reload ahead, thinking it is a group,
6179 then insist on finding a group. Otherwise we can grab a
6180 reg that some other reload needs.
6181 (That can happen when we have a 68000 DATA_OR_FP_REG
6182 which is a group of data regs or one fp reg.)
6183 We need not be so restrictive if there are no more reloads
6184 for this insn.
6186 ??? Really it would be nicer to have smarter handling
6187 for that kind of reg class, where a problem like this is normal.
6188 Perhaps those classes should be avoided for reloading
6189 by use of more alternatives. */
6191 int force_group = rld[r].nregs > 1 && ! last_reload;
6193 /* If we want a single register and haven't yet found one,
6194 take any reg in the right class and not in use.
6195 If we want a consecutive group, here is where we look for it.
6197 We use three passes so we can first look for reload regs to
6198 reuse, which are already in use for other reloads in this insn,
6199 and only then use additional registers which are not "bad", then
6200 finally any register.
6202 I think that maximizing reuse is needed to make sure we don't
6203 run out of reload regs. Suppose we have three reloads, and
6204 reloads A and B can share regs. These need two regs.
6205 Suppose A and B are given different regs.
6206 That leaves none for C. */
6207 for (pass = 0; pass < 3; pass++)
6209 /* I is the index in spill_regs.
6210 We advance it round-robin between insns to use all spill regs
6211 equally, so that inherited reloads have a chance
6212 of leapfrogging each other. */
6214 i = last_spill_reg;
6216 for (count = 0; count < n_spills; count++)
6218 int rclass = (int) rld[r].rclass;
6219 int regnum;
6221 i++;
6222 if (i >= n_spills)
6223 i -= n_spills;
6224 regnum = spill_regs[i];
6226 if ((reload_reg_free_p (regnum, rld[r].opnum,
6227 rld[r].when_needed)
6228 || (rld[r].in
6229 /* We check reload_reg_used to make sure we
6230 don't clobber the return register. */
6231 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
6232 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
6233 rld[r].when_needed, rld[r].in,
6234 rld[r].out, r, 1)))
6235 && TEST_HARD_REG_BIT (reg_class_contents[rclass], regnum)
6236 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
6237 /* Look first for regs to share, then for unshared. But
6238 don't share regs used for inherited reloads; they are
6239 the ones we want to preserve. */
6240 && (pass
6241 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
6242 regnum)
6243 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
6244 regnum))))
6246 int nr = hard_regno_nregs[regnum][rld[r].mode];
6248 /* During the second pass we want to avoid reload registers
6249 which are "bad" for this reload. */
6250 if (pass == 1
6251 && ira_bad_reload_regno (regnum, rld[r].in, rld[r].out))
6252 continue;
6254 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
6255 (on 68000) got us two FP regs. If NR is 1,
6256 we would reject both of them. */
6257 if (force_group)
6258 nr = rld[r].nregs;
6259 /* If we need only one reg, we have already won. */
6260 if (nr == 1)
6262 /* But reject a single reg if we demand a group. */
6263 if (force_group)
6264 continue;
6265 break;
6267 /* Otherwise check that as many consecutive regs as we need
6268 are available here. */
6269 while (nr > 1)
6271 int regno = regnum + nr - 1;
6272 if (!(TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
6273 && spill_reg_order[regno] >= 0
6274 && reload_reg_free_p (regno, rld[r].opnum,
6275 rld[r].when_needed)))
6276 break;
6277 nr--;
6279 if (nr == 1)
6280 break;
6284 /* If we found something on the current pass, omit later passes. */
6285 if (count < n_spills)
6286 break;
6289 /* We should have found a spill register by now. */
6290 if (count >= n_spills)
6291 return 0;
6293 /* I is the index in SPILL_REG_RTX of the reload register we are to
6294 allocate. Get an rtx for it and find its register number. */
6296 return set_reload_reg (i, r);
6299 /* Initialize all the tables needed to allocate reload registers.
6300 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
6301 is the array we use to restore the reg_rtx field for every reload. */
6303 static void
6304 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
6306 int i;
6308 for (i = 0; i < n_reloads; i++)
6309 rld[i].reg_rtx = save_reload_reg_rtx[i];
6311 memset (reload_inherited, 0, MAX_RELOADS);
6312 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
6313 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
6315 CLEAR_HARD_REG_SET (reload_reg_used);
6316 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
6317 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
6318 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
6319 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
6320 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
6322 CLEAR_HARD_REG_SET (reg_used_in_insn);
6324 HARD_REG_SET tmp;
6325 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
6326 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
6327 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
6328 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
6329 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
6330 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
6333 for (i = 0; i < reload_n_operands; i++)
6335 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
6336 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
6337 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
6338 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
6339 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
6340 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
6343 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
6345 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
6347 for (i = 0; i < n_reloads; i++)
6348 /* If we have already decided to use a certain register,
6349 don't use it in another way. */
6350 if (rld[i].reg_rtx)
6351 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
6352 rld[i].when_needed, rld[i].mode);
6355 /* Assign hard reg targets for the pseudo-registers we must reload
6356 into hard regs for this insn.
6357 Also output the instructions to copy them in and out of the hard regs.
6359 For machines with register classes, we are responsible for
6360 finding a reload reg in the proper class. */
6362 static void
6363 choose_reload_regs (struct insn_chain *chain)
6365 rtx insn = chain->insn;
6366 int i, j;
6367 unsigned int max_group_size = 1;
6368 enum reg_class group_class = NO_REGS;
6369 int pass, win, inheritance;
6371 rtx save_reload_reg_rtx[MAX_RELOADS];
6373 /* In order to be certain of getting the registers we need,
6374 we must sort the reloads into order of increasing register class.
6375 Then our grabbing of reload registers will parallel the process
6376 that provided the reload registers.
6378 Also note whether any of the reloads wants a consecutive group of regs.
6379 If so, record the maximum size of the group desired and what
6380 register class contains all the groups needed by this insn. */
6382 for (j = 0; j < n_reloads; j++)
6384 reload_order[j] = j;
6385 if (rld[j].reg_rtx != NULL_RTX)
6387 gcc_assert (REG_P (rld[j].reg_rtx)
6388 && HARD_REGISTER_P (rld[j].reg_rtx));
6389 reload_spill_index[j] = REGNO (rld[j].reg_rtx);
6391 else
6392 reload_spill_index[j] = -1;
6394 if (rld[j].nregs > 1)
6396 max_group_size = MAX (rld[j].nregs, max_group_size);
6397 group_class
6398 = reg_class_superunion[(int) rld[j].rclass][(int) group_class];
6401 save_reload_reg_rtx[j] = rld[j].reg_rtx;
6404 if (n_reloads > 1)
6405 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
6407 /* If -O, try first with inheritance, then turning it off.
6408 If not -O, don't do inheritance.
6409 Using inheritance when not optimizing leads to paradoxes
6410 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
6411 because one side of the comparison might be inherited. */
6412 win = 0;
6413 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
6415 choose_reload_regs_init (chain, save_reload_reg_rtx);
6417 /* Process the reloads in order of preference just found.
6418 Beyond this point, subregs can be found in reload_reg_rtx.
6420 This used to look for an existing reloaded home for all of the
6421 reloads, and only then perform any new reloads. But that could lose
6422 if the reloads were done out of reg-class order because a later
6423 reload with a looser constraint might have an old home in a register
6424 needed by an earlier reload with a tighter constraint.
6426 To solve this, we make two passes over the reloads, in the order
6427 described above. In the first pass we try to inherit a reload
6428 from a previous insn. If there is a later reload that needs a
6429 class that is a proper subset of the class being processed, we must
6430 also allocate a spill register during the first pass.
6432 Then make a second pass over the reloads to allocate any reloads
6433 that haven't been given registers yet. */
6435 for (j = 0; j < n_reloads; j++)
6437 int r = reload_order[j];
6438 rtx search_equiv = NULL_RTX;
6440 /* Ignore reloads that got marked inoperative. */
6441 if (rld[r].out == 0 && rld[r].in == 0
6442 && ! rld[r].secondary_p)
6443 continue;
6445 /* If find_reloads chose to use reload_in or reload_out as a reload
6446 register, we don't need to chose one. Otherwise, try even if it
6447 found one since we might save an insn if we find the value lying
6448 around.
6449 Try also when reload_in is a pseudo without a hard reg. */
6450 if (rld[r].in != 0 && rld[r].reg_rtx != 0
6451 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
6452 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
6453 && !MEM_P (rld[r].in)
6454 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
6455 continue;
6457 #if 0 /* No longer needed for correct operation.
6458 It might give better code, or might not; worth an experiment? */
6459 /* If this is an optional reload, we can't inherit from earlier insns
6460 until we are sure that any non-optional reloads have been allocated.
6461 The following code takes advantage of the fact that optional reloads
6462 are at the end of reload_order. */
6463 if (rld[r].optional != 0)
6464 for (i = 0; i < j; i++)
6465 if ((rld[reload_order[i]].out != 0
6466 || rld[reload_order[i]].in != 0
6467 || rld[reload_order[i]].secondary_p)
6468 && ! rld[reload_order[i]].optional
6469 && rld[reload_order[i]].reg_rtx == 0)
6470 allocate_reload_reg (chain, reload_order[i], 0);
6471 #endif
6473 /* First see if this pseudo is already available as reloaded
6474 for a previous insn. We cannot try to inherit for reloads
6475 that are smaller than the maximum number of registers needed
6476 for groups unless the register we would allocate cannot be used
6477 for the groups.
6479 We could check here to see if this is a secondary reload for
6480 an object that is already in a register of the desired class.
6481 This would avoid the need for the secondary reload register.
6482 But this is complex because we can't easily determine what
6483 objects might want to be loaded via this reload. So let a
6484 register be allocated here. In `emit_reload_insns' we suppress
6485 one of the loads in the case described above. */
6487 if (inheritance)
6489 int byte = 0;
6490 int regno = -1;
6491 enum machine_mode mode = VOIDmode;
6493 if (rld[r].in == 0)
6495 else if (REG_P (rld[r].in))
6497 regno = REGNO (rld[r].in);
6498 mode = GET_MODE (rld[r].in);
6500 else if (REG_P (rld[r].in_reg))
6502 regno = REGNO (rld[r].in_reg);
6503 mode = GET_MODE (rld[r].in_reg);
6505 else if (GET_CODE (rld[r].in_reg) == SUBREG
6506 && REG_P (SUBREG_REG (rld[r].in_reg)))
6508 regno = REGNO (SUBREG_REG (rld[r].in_reg));
6509 if (regno < FIRST_PSEUDO_REGISTER)
6510 regno = subreg_regno (rld[r].in_reg);
6511 else
6512 byte = SUBREG_BYTE (rld[r].in_reg);
6513 mode = GET_MODE (rld[r].in_reg);
6515 #ifdef AUTO_INC_DEC
6516 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
6517 && REG_P (XEXP (rld[r].in_reg, 0)))
6519 regno = REGNO (XEXP (rld[r].in_reg, 0));
6520 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
6521 rld[r].out = rld[r].in;
6523 #endif
6524 #if 0
6525 /* This won't work, since REGNO can be a pseudo reg number.
6526 Also, it takes much more hair to keep track of all the things
6527 that can invalidate an inherited reload of part of a pseudoreg. */
6528 else if (GET_CODE (rld[r].in) == SUBREG
6529 && REG_P (SUBREG_REG (rld[r].in)))
6530 regno = subreg_regno (rld[r].in);
6531 #endif
6533 if (regno >= 0
6534 && reg_last_reload_reg[regno] != 0
6535 && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
6536 >= GET_MODE_SIZE (mode) + byte)
6537 #ifdef CANNOT_CHANGE_MODE_CLASS
6538 /* Verify that the register it's in can be used in
6539 mode MODE. */
6540 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
6541 GET_MODE (reg_last_reload_reg[regno]),
6542 mode)
6543 #endif
6546 enum reg_class rclass = rld[r].rclass, last_class;
6547 rtx last_reg = reg_last_reload_reg[regno];
6549 i = REGNO (last_reg);
6550 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
6551 last_class = REGNO_REG_CLASS (i);
6553 if (reg_reloaded_contents[i] == regno
6554 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
6555 && HARD_REGNO_MODE_OK (i, rld[r].mode)
6556 && (TEST_HARD_REG_BIT (reg_class_contents[(int) rclass], i)
6557 /* Even if we can't use this register as a reload
6558 register, we might use it for reload_override_in,
6559 if copying it to the desired class is cheap
6560 enough. */
6561 || ((register_move_cost (mode, last_class, rclass)
6562 < memory_move_cost (mode, rclass, true))
6563 && (secondary_reload_class (1, rclass, mode,
6564 last_reg)
6565 == NO_REGS)
6566 #ifdef SECONDARY_MEMORY_NEEDED
6567 && ! SECONDARY_MEMORY_NEEDED (last_class, rclass,
6568 mode)
6569 #endif
6572 && (rld[r].nregs == max_group_size
6573 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
6575 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
6576 rld[r].when_needed, rld[r].in,
6577 const0_rtx, r, 1))
6579 /* If a group is needed, verify that all the subsequent
6580 registers still have their values intact. */
6581 int nr = hard_regno_nregs[i][rld[r].mode];
6582 int k;
6584 for (k = 1; k < nr; k++)
6585 if (reg_reloaded_contents[i + k] != regno
6586 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6587 break;
6589 if (k == nr)
6591 int i1;
6592 int bad_for_class;
6594 last_reg = (GET_MODE (last_reg) == mode
6595 ? last_reg : gen_rtx_REG (mode, i));
6597 bad_for_class = 0;
6598 for (k = 0; k < nr; k++)
6599 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6600 i+k);
6602 /* We found a register that contains the
6603 value we need. If this register is the
6604 same as an `earlyclobber' operand of the
6605 current insn, just mark it as a place to
6606 reload from since we can't use it as the
6607 reload register itself. */
6609 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6610 if (reg_overlap_mentioned_for_reload_p
6611 (reg_last_reload_reg[regno],
6612 reload_earlyclobbers[i1]))
6613 break;
6615 if (i1 != n_earlyclobbers
6616 || ! (free_for_value_p (i, rld[r].mode,
6617 rld[r].opnum,
6618 rld[r].when_needed, rld[r].in,
6619 rld[r].out, r, 1))
6620 /* Don't use it if we'd clobber a pseudo reg. */
6621 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6622 && rld[r].out
6623 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6624 /* Don't clobber the frame pointer. */
6625 || (i == HARD_FRAME_POINTER_REGNUM
6626 && frame_pointer_needed
6627 && rld[r].out)
6628 /* Don't really use the inherited spill reg
6629 if we need it wider than we've got it. */
6630 || (GET_MODE_SIZE (rld[r].mode)
6631 > GET_MODE_SIZE (mode))
6632 || bad_for_class
6634 /* If find_reloads chose reload_out as reload
6635 register, stay with it - that leaves the
6636 inherited register for subsequent reloads. */
6637 || (rld[r].out && rld[r].reg_rtx
6638 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6640 if (! rld[r].optional)
6642 reload_override_in[r] = last_reg;
6643 reload_inheritance_insn[r]
6644 = reg_reloaded_insn[i];
6647 else
6649 int k;
6650 /* We can use this as a reload reg. */
6651 /* Mark the register as in use for this part of
6652 the insn. */
6653 mark_reload_reg_in_use (i,
6654 rld[r].opnum,
6655 rld[r].when_needed,
6656 rld[r].mode);
6657 rld[r].reg_rtx = last_reg;
6658 reload_inherited[r] = 1;
6659 reload_inheritance_insn[r]
6660 = reg_reloaded_insn[i];
6661 reload_spill_index[r] = i;
6662 for (k = 0; k < nr; k++)
6663 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6664 i + k);
6671 /* Here's another way to see if the value is already lying around. */
6672 if (inheritance
6673 && rld[r].in != 0
6674 && ! reload_inherited[r]
6675 && rld[r].out == 0
6676 && (CONSTANT_P (rld[r].in)
6677 || GET_CODE (rld[r].in) == PLUS
6678 || REG_P (rld[r].in)
6679 || MEM_P (rld[r].in))
6680 && (rld[r].nregs == max_group_size
6681 || ! reg_classes_intersect_p (rld[r].rclass, group_class)))
6682 search_equiv = rld[r].in;
6684 if (search_equiv)
6686 rtx equiv
6687 = find_equiv_reg (search_equiv, insn, rld[r].rclass,
6688 -1, NULL, 0, rld[r].mode);
6689 int regno = 0;
6691 if (equiv != 0)
6693 if (REG_P (equiv))
6694 regno = REGNO (equiv);
6695 else
6697 /* This must be a SUBREG of a hard register.
6698 Make a new REG since this might be used in an
6699 address and not all machines support SUBREGs
6700 there. */
6701 gcc_assert (GET_CODE (equiv) == SUBREG);
6702 regno = subreg_regno (equiv);
6703 equiv = gen_rtx_REG (rld[r].mode, regno);
6704 /* If we choose EQUIV as the reload register, but the
6705 loop below decides to cancel the inheritance, we'll
6706 end up reloading EQUIV in rld[r].mode, not the mode
6707 it had originally. That isn't safe when EQUIV isn't
6708 available as a spill register since its value might
6709 still be live at this point. */
6710 for (i = regno; i < regno + (int) rld[r].nregs; i++)
6711 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
6712 equiv = 0;
6716 /* If we found a spill reg, reject it unless it is free
6717 and of the desired class. */
6718 if (equiv != 0)
6720 int regs_used = 0;
6721 int bad_for_class = 0;
6722 int max_regno = regno + rld[r].nregs;
6724 for (i = regno; i < max_regno; i++)
6726 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
6728 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6732 if ((regs_used
6733 && ! free_for_value_p (regno, rld[r].mode,
6734 rld[r].opnum, rld[r].when_needed,
6735 rld[r].in, rld[r].out, r, 1))
6736 || bad_for_class)
6737 equiv = 0;
6740 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6741 equiv = 0;
6743 /* We found a register that contains the value we need.
6744 If this register is the same as an `earlyclobber' operand
6745 of the current insn, just mark it as a place to reload from
6746 since we can't use it as the reload register itself. */
6748 if (equiv != 0)
6749 for (i = 0; i < n_earlyclobbers; i++)
6750 if (reg_overlap_mentioned_for_reload_p (equiv,
6751 reload_earlyclobbers[i]))
6753 if (! rld[r].optional)
6754 reload_override_in[r] = equiv;
6755 equiv = 0;
6756 break;
6759 /* If the equiv register we have found is explicitly clobbered
6760 in the current insn, it depends on the reload type if we
6761 can use it, use it for reload_override_in, or not at all.
6762 In particular, we then can't use EQUIV for a
6763 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6765 if (equiv != 0)
6767 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
6768 switch (rld[r].when_needed)
6770 case RELOAD_FOR_OTHER_ADDRESS:
6771 case RELOAD_FOR_INPADDR_ADDRESS:
6772 case RELOAD_FOR_INPUT_ADDRESS:
6773 case RELOAD_FOR_OPADDR_ADDR:
6774 break;
6775 case RELOAD_OTHER:
6776 case RELOAD_FOR_INPUT:
6777 case RELOAD_FOR_OPERAND_ADDRESS:
6778 if (! rld[r].optional)
6779 reload_override_in[r] = equiv;
6780 /* Fall through. */
6781 default:
6782 equiv = 0;
6783 break;
6785 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
6786 switch (rld[r].when_needed)
6788 case RELOAD_FOR_OTHER_ADDRESS:
6789 case RELOAD_FOR_INPADDR_ADDRESS:
6790 case RELOAD_FOR_INPUT_ADDRESS:
6791 case RELOAD_FOR_OPADDR_ADDR:
6792 case RELOAD_FOR_OPERAND_ADDRESS:
6793 case RELOAD_FOR_INPUT:
6794 break;
6795 case RELOAD_OTHER:
6796 if (! rld[r].optional)
6797 reload_override_in[r] = equiv;
6798 /* Fall through. */
6799 default:
6800 equiv = 0;
6801 break;
6805 /* If we found an equivalent reg, say no code need be generated
6806 to load it, and use it as our reload reg. */
6807 if (equiv != 0
6808 && (regno != HARD_FRAME_POINTER_REGNUM
6809 || !frame_pointer_needed))
6811 int nr = hard_regno_nregs[regno][rld[r].mode];
6812 int k;
6813 rld[r].reg_rtx = equiv;
6814 reload_spill_index[r] = regno;
6815 reload_inherited[r] = 1;
6817 /* If reg_reloaded_valid is not set for this register,
6818 there might be a stale spill_reg_store lying around.
6819 We must clear it, since otherwise emit_reload_insns
6820 might delete the store. */
6821 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6822 spill_reg_store[regno] = NULL_RTX;
6823 /* If any of the hard registers in EQUIV are spill
6824 registers, mark them as in use for this insn. */
6825 for (k = 0; k < nr; k++)
6827 i = spill_reg_order[regno + k];
6828 if (i >= 0)
6830 mark_reload_reg_in_use (regno, rld[r].opnum,
6831 rld[r].when_needed,
6832 rld[r].mode);
6833 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6834 regno + k);
6840 /* If we found a register to use already, or if this is an optional
6841 reload, we are done. */
6842 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6843 continue;
6845 #if 0
6846 /* No longer needed for correct operation. Might or might
6847 not give better code on the average. Want to experiment? */
6849 /* See if there is a later reload that has a class different from our
6850 class that intersects our class or that requires less register
6851 than our reload. If so, we must allocate a register to this
6852 reload now, since that reload might inherit a previous reload
6853 and take the only available register in our class. Don't do this
6854 for optional reloads since they will force all previous reloads
6855 to be allocated. Also don't do this for reloads that have been
6856 turned off. */
6858 for (i = j + 1; i < n_reloads; i++)
6860 int s = reload_order[i];
6862 if ((rld[s].in == 0 && rld[s].out == 0
6863 && ! rld[s].secondary_p)
6864 || rld[s].optional)
6865 continue;
6867 if ((rld[s].rclass != rld[r].rclass
6868 && reg_classes_intersect_p (rld[r].rclass,
6869 rld[s].rclass))
6870 || rld[s].nregs < rld[r].nregs)
6871 break;
6874 if (i == n_reloads)
6875 continue;
6877 allocate_reload_reg (chain, r, j == n_reloads - 1);
6878 #endif
6881 /* Now allocate reload registers for anything non-optional that
6882 didn't get one yet. */
6883 for (j = 0; j < n_reloads; j++)
6885 int r = reload_order[j];
6887 /* Ignore reloads that got marked inoperative. */
6888 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6889 continue;
6891 /* Skip reloads that already have a register allocated or are
6892 optional. */
6893 if (rld[r].reg_rtx != 0 || rld[r].optional)
6894 continue;
6896 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6897 break;
6900 /* If that loop got all the way, we have won. */
6901 if (j == n_reloads)
6903 win = 1;
6904 break;
6907 /* Loop around and try without any inheritance. */
6910 if (! win)
6912 /* First undo everything done by the failed attempt
6913 to allocate with inheritance. */
6914 choose_reload_regs_init (chain, save_reload_reg_rtx);
6916 /* Some sanity tests to verify that the reloads found in the first
6917 pass are identical to the ones we have now. */
6918 gcc_assert (chain->n_reloads == n_reloads);
6920 for (i = 0; i < n_reloads; i++)
6922 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6923 continue;
6924 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6925 for (j = 0; j < n_spills; j++)
6926 if (spill_regs[j] == chain->rld[i].regno)
6927 if (! set_reload_reg (j, i))
6928 failed_reload (chain->insn, i);
6932 /* If we thought we could inherit a reload, because it seemed that
6933 nothing else wanted the same reload register earlier in the insn,
6934 verify that assumption, now that all reloads have been assigned.
6935 Likewise for reloads where reload_override_in has been set. */
6937 /* If doing expensive optimizations, do one preliminary pass that doesn't
6938 cancel any inheritance, but removes reloads that have been needed only
6939 for reloads that we know can be inherited. */
6940 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6942 for (j = 0; j < n_reloads; j++)
6944 int r = reload_order[j];
6945 rtx check_reg;
6946 if (reload_inherited[r] && rld[r].reg_rtx)
6947 check_reg = rld[r].reg_rtx;
6948 else if (reload_override_in[r]
6949 && (REG_P (reload_override_in[r])
6950 || GET_CODE (reload_override_in[r]) == SUBREG))
6951 check_reg = reload_override_in[r];
6952 else
6953 continue;
6954 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6955 rld[r].opnum, rld[r].when_needed, rld[r].in,
6956 (reload_inherited[r]
6957 ? rld[r].out : const0_rtx),
6958 r, 1))
6960 if (pass)
6961 continue;
6962 reload_inherited[r] = 0;
6963 reload_override_in[r] = 0;
6965 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6966 reload_override_in, then we do not need its related
6967 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6968 likewise for other reload types.
6969 We handle this by removing a reload when its only replacement
6970 is mentioned in reload_in of the reload we are going to inherit.
6971 A special case are auto_inc expressions; even if the input is
6972 inherited, we still need the address for the output. We can
6973 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6974 If we succeeded removing some reload and we are doing a preliminary
6975 pass just to remove such reloads, make another pass, since the
6976 removal of one reload might allow us to inherit another one. */
6977 else if (rld[r].in
6978 && rld[r].out != rld[r].in
6979 && remove_address_replacements (rld[r].in) && pass)
6980 pass = 2;
6984 /* Now that reload_override_in is known valid,
6985 actually override reload_in. */
6986 for (j = 0; j < n_reloads; j++)
6987 if (reload_override_in[j])
6988 rld[j].in = reload_override_in[j];
6990 /* If this reload won't be done because it has been canceled or is
6991 optional and not inherited, clear reload_reg_rtx so other
6992 routines (such as subst_reloads) don't get confused. */
6993 for (j = 0; j < n_reloads; j++)
6994 if (rld[j].reg_rtx != 0
6995 && ((rld[j].optional && ! reload_inherited[j])
6996 || (rld[j].in == 0 && rld[j].out == 0
6997 && ! rld[j].secondary_p)))
6999 int regno = true_regnum (rld[j].reg_rtx);
7001 if (spill_reg_order[regno] >= 0)
7002 clear_reload_reg_in_use (regno, rld[j].opnum,
7003 rld[j].when_needed, rld[j].mode);
7004 rld[j].reg_rtx = 0;
7005 reload_spill_index[j] = -1;
7008 /* Record which pseudos and which spill regs have output reloads. */
7009 for (j = 0; j < n_reloads; j++)
7011 int r = reload_order[j];
7013 i = reload_spill_index[r];
7015 /* I is nonneg if this reload uses a register.
7016 If rld[r].reg_rtx is 0, this is an optional reload
7017 that we opted to ignore. */
7018 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
7019 && rld[r].reg_rtx != 0)
7021 int nregno = REGNO (rld[r].out_reg);
7022 int nr = 1;
7024 if (nregno < FIRST_PSEUDO_REGISTER)
7025 nr = hard_regno_nregs[nregno][rld[r].mode];
7027 while (--nr >= 0)
7028 SET_REGNO_REG_SET (&reg_has_output_reload,
7029 nregno + nr);
7031 if (i >= 0)
7032 add_to_hard_reg_set (&reg_is_output_reload, rld[r].mode, i);
7034 gcc_assert (rld[r].when_needed == RELOAD_OTHER
7035 || rld[r].when_needed == RELOAD_FOR_OUTPUT
7036 || rld[r].when_needed == RELOAD_FOR_INSN);
7041 /* Deallocate the reload register for reload R. This is called from
7042 remove_address_replacements. */
7044 void
7045 deallocate_reload_reg (int r)
7047 int regno;
7049 if (! rld[r].reg_rtx)
7050 return;
7051 regno = true_regnum (rld[r].reg_rtx);
7052 rld[r].reg_rtx = 0;
7053 if (spill_reg_order[regno] >= 0)
7054 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
7055 rld[r].mode);
7056 reload_spill_index[r] = -1;
7059 /* These arrays are filled by emit_reload_insns and its subroutines. */
7060 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
7061 static rtx other_input_address_reload_insns = 0;
7062 static rtx other_input_reload_insns = 0;
7063 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
7064 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
7065 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
7066 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
7067 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
7068 static rtx operand_reload_insns = 0;
7069 static rtx other_operand_reload_insns = 0;
7070 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
7072 /* Values to be put in spill_reg_store are put here first. Instructions
7073 must only be placed here if the associated reload register reaches
7074 the end of the instruction's reload sequence. */
7075 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
7076 static HARD_REG_SET reg_reloaded_died;
7078 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
7079 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
7080 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
7081 adjusted register, and return true. Otherwise, return false. */
7082 static bool
7083 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
7084 enum reg_class new_class,
7085 enum machine_mode new_mode)
7088 rtx reg;
7090 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
7092 unsigned regno = REGNO (reg);
7094 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
7095 continue;
7096 if (GET_MODE (reg) != new_mode)
7098 if (!HARD_REGNO_MODE_OK (regno, new_mode))
7099 continue;
7100 if (hard_regno_nregs[regno][new_mode]
7101 > hard_regno_nregs[regno][GET_MODE (reg)])
7102 continue;
7103 reg = reload_adjust_reg_for_mode (reg, new_mode);
7105 *reload_reg = reg;
7106 return true;
7108 return false;
7111 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
7112 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
7113 nonzero, if that is suitable. On success, change *RELOAD_REG to the
7114 adjusted register, and return true. Otherwise, return false. */
7115 static bool
7116 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
7117 enum insn_code icode)
7120 enum reg_class new_class = scratch_reload_class (icode);
7121 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
7123 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
7124 new_class, new_mode);
7127 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
7128 has the number J. OLD contains the value to be used as input. */
7130 static void
7131 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
7132 rtx old, int j)
7134 rtx insn = chain->insn;
7135 rtx reloadreg;
7136 rtx oldequiv_reg = 0;
7137 rtx oldequiv = 0;
7138 int special = 0;
7139 enum machine_mode mode;
7140 rtx *where;
7142 /* delete_output_reload is only invoked properly if old contains
7143 the original pseudo register. Since this is replaced with a
7144 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
7145 find the pseudo in RELOAD_IN_REG. */
7146 if (reload_override_in[j]
7147 && REG_P (rl->in_reg))
7149 oldequiv = old;
7150 old = rl->in_reg;
7152 if (oldequiv == 0)
7153 oldequiv = old;
7154 else if (REG_P (oldequiv))
7155 oldequiv_reg = oldequiv;
7156 else if (GET_CODE (oldequiv) == SUBREG)
7157 oldequiv_reg = SUBREG_REG (oldequiv);
7159 reloadreg = reload_reg_rtx_for_input[j];
7160 mode = GET_MODE (reloadreg);
7162 /* If we are reloading from a register that was recently stored in
7163 with an output-reload, see if we can prove there was
7164 actually no need to store the old value in it. */
7166 if (optimize && REG_P (oldequiv)
7167 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
7168 && spill_reg_store[REGNO (oldequiv)]
7169 && REG_P (old)
7170 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
7171 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
7172 rl->out_reg)))
7173 delete_output_reload (insn, j, REGNO (oldequiv), reloadreg);
7175 /* Encapsulate OLDEQUIV into the reload mode, then load RELOADREG from
7176 OLDEQUIV. */
7178 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
7179 oldequiv = SUBREG_REG (oldequiv);
7180 if (GET_MODE (oldequiv) != VOIDmode
7181 && mode != GET_MODE (oldequiv))
7182 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
7184 /* Switch to the right place to emit the reload insns. */
7185 switch (rl->when_needed)
7187 case RELOAD_OTHER:
7188 where = &other_input_reload_insns;
7189 break;
7190 case RELOAD_FOR_INPUT:
7191 where = &input_reload_insns[rl->opnum];
7192 break;
7193 case RELOAD_FOR_INPUT_ADDRESS:
7194 where = &input_address_reload_insns[rl->opnum];
7195 break;
7196 case RELOAD_FOR_INPADDR_ADDRESS:
7197 where = &inpaddr_address_reload_insns[rl->opnum];
7198 break;
7199 case RELOAD_FOR_OUTPUT_ADDRESS:
7200 where = &output_address_reload_insns[rl->opnum];
7201 break;
7202 case RELOAD_FOR_OUTADDR_ADDRESS:
7203 where = &outaddr_address_reload_insns[rl->opnum];
7204 break;
7205 case RELOAD_FOR_OPERAND_ADDRESS:
7206 where = &operand_reload_insns;
7207 break;
7208 case RELOAD_FOR_OPADDR_ADDR:
7209 where = &other_operand_reload_insns;
7210 break;
7211 case RELOAD_FOR_OTHER_ADDRESS:
7212 where = &other_input_address_reload_insns;
7213 break;
7214 default:
7215 gcc_unreachable ();
7218 push_to_sequence (*where);
7220 /* Auto-increment addresses must be reloaded in a special way. */
7221 if (rl->out && ! rl->out_reg)
7223 /* We are not going to bother supporting the case where a
7224 incremented register can't be copied directly from
7225 OLDEQUIV since this seems highly unlikely. */
7226 gcc_assert (rl->secondary_in_reload < 0);
7228 if (reload_inherited[j])
7229 oldequiv = reloadreg;
7231 old = XEXP (rl->in_reg, 0);
7233 /* Prevent normal processing of this reload. */
7234 special = 1;
7235 /* Output a special code sequence for this case. */
7236 inc_for_reload (reloadreg, oldequiv, rl->out, rl->inc);
7239 /* If we are reloading a pseudo-register that was set by the previous
7240 insn, see if we can get rid of that pseudo-register entirely
7241 by redirecting the previous insn into our reload register. */
7243 else if (optimize && REG_P (old)
7244 && REGNO (old) >= FIRST_PSEUDO_REGISTER
7245 && dead_or_set_p (insn, old)
7246 /* This is unsafe if some other reload
7247 uses the same reg first. */
7248 && ! conflicts_with_override (reloadreg)
7249 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
7250 rl->when_needed, old, rl->out, j, 0))
7252 rtx temp = PREV_INSN (insn);
7253 while (temp && (NOTE_P (temp) || DEBUG_INSN_P (temp)))
7254 temp = PREV_INSN (temp);
7255 if (temp
7256 && NONJUMP_INSN_P (temp)
7257 && GET_CODE (PATTERN (temp)) == SET
7258 && SET_DEST (PATTERN (temp)) == old
7259 /* Make sure we can access insn_operand_constraint. */
7260 && asm_noperands (PATTERN (temp)) < 0
7261 /* This is unsafe if operand occurs more than once in current
7262 insn. Perhaps some occurrences aren't reloaded. */
7263 && count_occurrences (PATTERN (insn), old, 0) == 1)
7265 rtx old = SET_DEST (PATTERN (temp));
7266 /* Store into the reload register instead of the pseudo. */
7267 SET_DEST (PATTERN (temp)) = reloadreg;
7269 /* Verify that resulting insn is valid. */
7270 extract_insn (temp);
7271 if (constrain_operands (1))
7273 /* If the previous insn is an output reload, the source is
7274 a reload register, and its spill_reg_store entry will
7275 contain the previous destination. This is now
7276 invalid. */
7277 if (REG_P (SET_SRC (PATTERN (temp)))
7278 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
7280 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7281 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7284 /* If these are the only uses of the pseudo reg,
7285 pretend for GDB it lives in the reload reg we used. */
7286 if (REG_N_DEATHS (REGNO (old)) == 1
7287 && REG_N_SETS (REGNO (old)) == 1)
7289 reg_renumber[REGNO (old)] = REGNO (reloadreg);
7290 if (ira_conflicts_p)
7291 /* Inform IRA about the change. */
7292 ira_mark_allocation_change (REGNO (old));
7293 alter_reg (REGNO (old), -1, false);
7295 special = 1;
7297 /* Adjust any debug insns between temp and insn. */
7298 while ((temp = NEXT_INSN (temp)) != insn)
7299 if (DEBUG_INSN_P (temp))
7300 replace_rtx (PATTERN (temp), old, reloadreg);
7301 else
7302 gcc_assert (NOTE_P (temp));
7304 else
7306 SET_DEST (PATTERN (temp)) = old;
7311 /* We can't do that, so output an insn to load RELOADREG. */
7313 /* If we have a secondary reload, pick up the secondary register
7314 and icode, if any. If OLDEQUIV and OLD are different or
7315 if this is an in-out reload, recompute whether or not we
7316 still need a secondary register and what the icode should
7317 be. If we still need a secondary register and the class or
7318 icode is different, go back to reloading from OLD if using
7319 OLDEQUIV means that we got the wrong type of register. We
7320 cannot have different class or icode due to an in-out reload
7321 because we don't make such reloads when both the input and
7322 output need secondary reload registers. */
7324 if (! special && rl->secondary_in_reload >= 0)
7326 rtx second_reload_reg = 0;
7327 rtx third_reload_reg = 0;
7328 int secondary_reload = rl->secondary_in_reload;
7329 rtx real_oldequiv = oldequiv;
7330 rtx real_old = old;
7331 rtx tmp;
7332 enum insn_code icode;
7333 enum insn_code tertiary_icode = CODE_FOR_nothing;
7335 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
7336 and similarly for OLD.
7337 See comments in get_secondary_reload in reload.c. */
7338 /* If it is a pseudo that cannot be replaced with its
7339 equivalent MEM, we must fall back to reload_in, which
7340 will have all the necessary substitutions registered.
7341 Likewise for a pseudo that can't be replaced with its
7342 equivalent constant.
7344 Take extra care for subregs of such pseudos. Note that
7345 we cannot use reg_equiv_mem in this case because it is
7346 not in the right mode. */
7348 tmp = oldequiv;
7349 if (GET_CODE (tmp) == SUBREG)
7350 tmp = SUBREG_REG (tmp);
7351 if (REG_P (tmp)
7352 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7353 && (reg_equiv_memory_loc (REGNO (tmp)) != 0
7354 || reg_equiv_constant (REGNO (tmp)) != 0))
7356 if (! reg_equiv_mem (REGNO (tmp))
7357 || num_not_at_initial_offset
7358 || GET_CODE (oldequiv) == SUBREG)
7359 real_oldequiv = rl->in;
7360 else
7361 real_oldequiv = reg_equiv_mem (REGNO (tmp));
7364 tmp = old;
7365 if (GET_CODE (tmp) == SUBREG)
7366 tmp = SUBREG_REG (tmp);
7367 if (REG_P (tmp)
7368 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7369 && (reg_equiv_memory_loc (REGNO (tmp)) != 0
7370 || reg_equiv_constant (REGNO (tmp)) != 0))
7372 if (! reg_equiv_mem (REGNO (tmp))
7373 || num_not_at_initial_offset
7374 || GET_CODE (old) == SUBREG)
7375 real_old = rl->in;
7376 else
7377 real_old = reg_equiv_mem (REGNO (tmp));
7380 second_reload_reg = rld[secondary_reload].reg_rtx;
7381 if (rld[secondary_reload].secondary_in_reload >= 0)
7383 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
7385 third_reload_reg = rld[tertiary_reload].reg_rtx;
7386 tertiary_icode = rld[secondary_reload].secondary_in_icode;
7387 /* We'd have to add more code for quartary reloads. */
7388 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
7390 icode = rl->secondary_in_icode;
7392 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
7393 || (rl->in != 0 && rl->out != 0))
7395 secondary_reload_info sri, sri2;
7396 enum reg_class new_class, new_t_class;
7398 sri.icode = CODE_FOR_nothing;
7399 sri.prev_sri = NULL;
7400 new_class
7401 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7402 rl->rclass, mode,
7403 &sri);
7405 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
7406 second_reload_reg = 0;
7407 else if (new_class == NO_REGS)
7409 if (reload_adjust_reg_for_icode (&second_reload_reg,
7410 third_reload_reg,
7411 (enum insn_code) sri.icode))
7413 icode = (enum insn_code) sri.icode;
7414 third_reload_reg = 0;
7416 else
7418 oldequiv = old;
7419 real_oldequiv = real_old;
7422 else if (sri.icode != CODE_FOR_nothing)
7423 /* We currently lack a way to express this in reloads. */
7424 gcc_unreachable ();
7425 else
7427 sri2.icode = CODE_FOR_nothing;
7428 sri2.prev_sri = &sri;
7429 new_t_class
7430 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7431 new_class, mode,
7432 &sri);
7433 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
7435 if (reload_adjust_reg_for_temp (&second_reload_reg,
7436 third_reload_reg,
7437 new_class, mode))
7439 third_reload_reg = 0;
7440 tertiary_icode = (enum insn_code) sri2.icode;
7442 else
7444 oldequiv = old;
7445 real_oldequiv = real_old;
7448 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
7450 rtx intermediate = second_reload_reg;
7452 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7453 new_class, mode)
7454 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
7455 ((enum insn_code)
7456 sri2.icode)))
7458 second_reload_reg = intermediate;
7459 tertiary_icode = (enum insn_code) sri2.icode;
7461 else
7463 oldequiv = old;
7464 real_oldequiv = real_old;
7467 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
7469 rtx intermediate = second_reload_reg;
7471 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7472 new_class, mode)
7473 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
7474 new_t_class, mode))
7476 second_reload_reg = intermediate;
7477 tertiary_icode = (enum insn_code) sri2.icode;
7479 else
7481 oldequiv = old;
7482 real_oldequiv = real_old;
7485 else
7487 /* This could be handled more intelligently too. */
7488 oldequiv = old;
7489 real_oldequiv = real_old;
7494 /* If we still need a secondary reload register, check
7495 to see if it is being used as a scratch or intermediate
7496 register and generate code appropriately. If we need
7497 a scratch register, use REAL_OLDEQUIV since the form of
7498 the insn may depend on the actual address if it is
7499 a MEM. */
7501 if (second_reload_reg)
7503 if (icode != CODE_FOR_nothing)
7505 /* We'd have to add extra code to handle this case. */
7506 gcc_assert (!third_reload_reg);
7508 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7509 second_reload_reg));
7510 special = 1;
7512 else
7514 /* See if we need a scratch register to load the
7515 intermediate register (a tertiary reload). */
7516 if (tertiary_icode != CODE_FOR_nothing)
7518 emit_insn ((GEN_FCN (tertiary_icode)
7519 (second_reload_reg, real_oldequiv,
7520 third_reload_reg)));
7522 else if (third_reload_reg)
7524 gen_reload (third_reload_reg, real_oldequiv,
7525 rl->opnum,
7526 rl->when_needed);
7527 gen_reload (second_reload_reg, third_reload_reg,
7528 rl->opnum,
7529 rl->when_needed);
7531 else
7532 gen_reload (second_reload_reg, real_oldequiv,
7533 rl->opnum,
7534 rl->when_needed);
7536 oldequiv = second_reload_reg;
7541 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7543 rtx real_oldequiv = oldequiv;
7545 if ((REG_P (oldequiv)
7546 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7547 && (reg_equiv_memory_loc (REGNO (oldequiv)) != 0
7548 || reg_equiv_constant (REGNO (oldequiv)) != 0))
7549 || (GET_CODE (oldequiv) == SUBREG
7550 && REG_P (SUBREG_REG (oldequiv))
7551 && (REGNO (SUBREG_REG (oldequiv))
7552 >= FIRST_PSEUDO_REGISTER)
7553 && ((reg_equiv_memory_loc (REGNO (SUBREG_REG (oldequiv))) != 0)
7554 || (reg_equiv_constant (REGNO (SUBREG_REG (oldequiv))) != 0)))
7555 || (CONSTANT_P (oldequiv)
7556 && (targetm.preferred_reload_class (oldequiv,
7557 REGNO_REG_CLASS (REGNO (reloadreg)))
7558 == NO_REGS)))
7559 real_oldequiv = rl->in;
7560 gen_reload (reloadreg, real_oldequiv, rl->opnum,
7561 rl->when_needed);
7564 if (cfun->can_throw_non_call_exceptions)
7565 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7567 /* End this sequence. */
7568 *where = get_insns ();
7569 end_sequence ();
7571 /* Update reload_override_in so that delete_address_reloads_1
7572 can see the actual register usage. */
7573 if (oldequiv_reg)
7574 reload_override_in[j] = oldequiv;
7577 /* Generate insns to for the output reload RL, which is for the insn described
7578 by CHAIN and has the number J. */
7579 static void
7580 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
7581 int j)
7583 rtx reloadreg;
7584 rtx insn = chain->insn;
7585 int special = 0;
7586 rtx old = rl->out;
7587 enum machine_mode mode;
7588 rtx p;
7589 rtx rl_reg_rtx;
7591 if (rl->when_needed == RELOAD_OTHER)
7592 start_sequence ();
7593 else
7594 push_to_sequence (output_reload_insns[rl->opnum]);
7596 rl_reg_rtx = reload_reg_rtx_for_output[j];
7597 mode = GET_MODE (rl_reg_rtx);
7599 reloadreg = rl_reg_rtx;
7601 /* If we need two reload regs, set RELOADREG to the intermediate
7602 one, since it will be stored into OLD. We might need a secondary
7603 register only for an input reload, so check again here. */
7605 if (rl->secondary_out_reload >= 0)
7607 rtx real_old = old;
7608 int secondary_reload = rl->secondary_out_reload;
7609 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
7611 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
7612 && reg_equiv_mem (REGNO (old)) != 0)
7613 real_old = reg_equiv_mem (REGNO (old));
7615 if (secondary_reload_class (0, rl->rclass, mode, real_old) != NO_REGS)
7617 rtx second_reloadreg = reloadreg;
7618 reloadreg = rld[secondary_reload].reg_rtx;
7620 /* See if RELOADREG is to be used as a scratch register
7621 or as an intermediate register. */
7622 if (rl->secondary_out_icode != CODE_FOR_nothing)
7624 /* We'd have to add extra code to handle this case. */
7625 gcc_assert (tertiary_reload < 0);
7627 emit_insn ((GEN_FCN (rl->secondary_out_icode)
7628 (real_old, second_reloadreg, reloadreg)));
7629 special = 1;
7631 else
7633 /* See if we need both a scratch and intermediate reload
7634 register. */
7636 enum insn_code tertiary_icode
7637 = rld[secondary_reload].secondary_out_icode;
7639 /* We'd have to add more code for quartary reloads. */
7640 gcc_assert (tertiary_reload < 0
7641 || rld[tertiary_reload].secondary_out_reload < 0);
7643 if (GET_MODE (reloadreg) != mode)
7644 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7646 if (tertiary_icode != CODE_FOR_nothing)
7648 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7650 /* Copy primary reload reg to secondary reload reg.
7651 (Note that these have been swapped above, then
7652 secondary reload reg to OLD using our insn.) */
7654 /* If REAL_OLD is a paradoxical SUBREG, remove it
7655 and try to put the opposite SUBREG on
7656 RELOADREG. */
7657 strip_paradoxical_subreg (&real_old, &reloadreg);
7659 gen_reload (reloadreg, second_reloadreg,
7660 rl->opnum, rl->when_needed);
7661 emit_insn ((GEN_FCN (tertiary_icode)
7662 (real_old, reloadreg, third_reloadreg)));
7663 special = 1;
7666 else
7668 /* Copy between the reload regs here and then to
7669 OUT later. */
7671 gen_reload (reloadreg, second_reloadreg,
7672 rl->opnum, rl->when_needed);
7673 if (tertiary_reload >= 0)
7675 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7677 gen_reload (third_reloadreg, reloadreg,
7678 rl->opnum, rl->when_needed);
7679 reloadreg = third_reloadreg;
7686 /* Output the last reload insn. */
7687 if (! special)
7689 rtx set;
7691 /* Don't output the last reload if OLD is not the dest of
7692 INSN and is in the src and is clobbered by INSN. */
7693 if (! flag_expensive_optimizations
7694 || !REG_P (old)
7695 || !(set = single_set (insn))
7696 || rtx_equal_p (old, SET_DEST (set))
7697 || !reg_mentioned_p (old, SET_SRC (set))
7698 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7699 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7700 gen_reload (old, reloadreg, rl->opnum,
7701 rl->when_needed);
7704 /* Look at all insns we emitted, just to be safe. */
7705 for (p = get_insns (); p; p = NEXT_INSN (p))
7706 if (INSN_P (p))
7708 rtx pat = PATTERN (p);
7710 /* If this output reload doesn't come from a spill reg,
7711 clear any memory of reloaded copies of the pseudo reg.
7712 If this output reload comes from a spill reg,
7713 reg_has_output_reload will make this do nothing. */
7714 note_stores (pat, forget_old_reloads_1, NULL);
7716 if (reg_mentioned_p (rl_reg_rtx, pat))
7718 rtx set = single_set (insn);
7719 if (reload_spill_index[j] < 0
7720 && set
7721 && SET_SRC (set) == rl_reg_rtx)
7723 int src = REGNO (SET_SRC (set));
7725 reload_spill_index[j] = src;
7726 SET_HARD_REG_BIT (reg_is_output_reload, src);
7727 if (find_regno_note (insn, REG_DEAD, src))
7728 SET_HARD_REG_BIT (reg_reloaded_died, src);
7730 if (HARD_REGISTER_P (rl_reg_rtx))
7732 int s = rl->secondary_out_reload;
7733 set = single_set (p);
7734 /* If this reload copies only to the secondary reload
7735 register, the secondary reload does the actual
7736 store. */
7737 if (s >= 0 && set == NULL_RTX)
7738 /* We can't tell what function the secondary reload
7739 has and where the actual store to the pseudo is
7740 made; leave new_spill_reg_store alone. */
7742 else if (s >= 0
7743 && SET_SRC (set) == rl_reg_rtx
7744 && SET_DEST (set) == rld[s].reg_rtx)
7746 /* Usually the next instruction will be the
7747 secondary reload insn; if we can confirm
7748 that it is, setting new_spill_reg_store to
7749 that insn will allow an extra optimization. */
7750 rtx s_reg = rld[s].reg_rtx;
7751 rtx next = NEXT_INSN (p);
7752 rld[s].out = rl->out;
7753 rld[s].out_reg = rl->out_reg;
7754 set = single_set (next);
7755 if (set && SET_SRC (set) == s_reg
7756 && reload_reg_rtx_reaches_end_p (s_reg, s))
7758 SET_HARD_REG_BIT (reg_is_output_reload,
7759 REGNO (s_reg));
7760 new_spill_reg_store[REGNO (s_reg)] = next;
7763 else if (reload_reg_rtx_reaches_end_p (rl_reg_rtx, j))
7764 new_spill_reg_store[REGNO (rl_reg_rtx)] = p;
7769 if (rl->when_needed == RELOAD_OTHER)
7771 emit_insn (other_output_reload_insns[rl->opnum]);
7772 other_output_reload_insns[rl->opnum] = get_insns ();
7774 else
7775 output_reload_insns[rl->opnum] = get_insns ();
7777 if (cfun->can_throw_non_call_exceptions)
7778 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7780 end_sequence ();
7783 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7784 and has the number J. */
7785 static void
7786 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7788 rtx insn = chain->insn;
7789 rtx old = (rl->in && MEM_P (rl->in)
7790 ? rl->in_reg : rl->in);
7791 rtx reg_rtx = rl->reg_rtx;
7793 if (old && reg_rtx)
7795 enum machine_mode mode;
7797 /* Determine the mode to reload in.
7798 This is very tricky because we have three to choose from.
7799 There is the mode the insn operand wants (rl->inmode).
7800 There is the mode of the reload register RELOADREG.
7801 There is the intrinsic mode of the operand, which we could find
7802 by stripping some SUBREGs.
7803 It turns out that RELOADREG's mode is irrelevant:
7804 we can change that arbitrarily.
7806 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
7807 then the reload reg may not support QImode moves, so use SImode.
7808 If foo is in memory due to spilling a pseudo reg, this is safe,
7809 because the QImode value is in the least significant part of a
7810 slot big enough for a SImode. If foo is some other sort of
7811 memory reference, then it is impossible to reload this case,
7812 so previous passes had better make sure this never happens.
7814 Then consider a one-word union which has SImode and one of its
7815 members is a float, being fetched as (SUBREG:SF union:SI).
7816 We must fetch that as SFmode because we could be loading into
7817 a float-only register. In this case OLD's mode is correct.
7819 Consider an immediate integer: it has VOIDmode. Here we need
7820 to get a mode from something else.
7822 In some cases, there is a fourth mode, the operand's
7823 containing mode. If the insn specifies a containing mode for
7824 this operand, it overrides all others.
7826 I am not sure whether the algorithm here is always right,
7827 but it does the right things in those cases. */
7829 mode = GET_MODE (old);
7830 if (mode == VOIDmode)
7831 mode = rl->inmode;
7833 /* We cannot use gen_lowpart_common since it can do the wrong thing
7834 when REG_RTX has a multi-word mode. Note that REG_RTX must
7835 always be a REG here. */
7836 if (GET_MODE (reg_rtx) != mode)
7837 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7839 reload_reg_rtx_for_input[j] = reg_rtx;
7841 if (old != 0
7842 /* AUTO_INC reloads need to be handled even if inherited. We got an
7843 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7844 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7845 && ! rtx_equal_p (reg_rtx, old)
7846 && reg_rtx != 0)
7847 emit_input_reload_insns (chain, rld + j, old, j);
7849 /* When inheriting a wider reload, we have a MEM in rl->in,
7850 e.g. inheriting a SImode output reload for
7851 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7852 if (optimize && reload_inherited[j] && rl->in
7853 && MEM_P (rl->in)
7854 && MEM_P (rl->in_reg)
7855 && reload_spill_index[j] >= 0
7856 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7857 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7859 /* If we are reloading a register that was recently stored in with an
7860 output-reload, see if we can prove there was
7861 actually no need to store the old value in it. */
7863 if (optimize
7864 && (reload_inherited[j] || reload_override_in[j])
7865 && reg_rtx
7866 && REG_P (reg_rtx)
7867 && spill_reg_store[REGNO (reg_rtx)] != 0
7868 #if 0
7869 /* There doesn't seem to be any reason to restrict this to pseudos
7870 and doing so loses in the case where we are copying from a
7871 register of the wrong class. */
7872 && !HARD_REGISTER_P (spill_reg_stored_to[REGNO (reg_rtx)])
7873 #endif
7874 /* The insn might have already some references to stackslots
7875 replaced by MEMs, while reload_out_reg still names the
7876 original pseudo. */
7877 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (reg_rtx)])
7878 || rtx_equal_p (spill_reg_stored_to[REGNO (reg_rtx)], rl->out_reg)))
7879 delete_output_reload (insn, j, REGNO (reg_rtx), reg_rtx);
7882 /* Do output reloading for reload RL, which is for the insn described by
7883 CHAIN and has the number J.
7884 ??? At some point we need to support handling output reloads of
7885 JUMP_INSNs or insns that set cc0. */
7886 static void
7887 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7889 rtx note, old;
7890 rtx insn = chain->insn;
7891 /* If this is an output reload that stores something that is
7892 not loaded in this same reload, see if we can eliminate a previous
7893 store. */
7894 rtx pseudo = rl->out_reg;
7895 rtx reg_rtx = rl->reg_rtx;
7897 if (rl->out && reg_rtx)
7899 enum machine_mode mode;
7901 /* Determine the mode to reload in.
7902 See comments above (for input reloading). */
7903 mode = GET_MODE (rl->out);
7904 if (mode == VOIDmode)
7906 /* VOIDmode should never happen for an output. */
7907 if (asm_noperands (PATTERN (insn)) < 0)
7908 /* It's the compiler's fault. */
7909 fatal_insn ("VOIDmode on an output", insn);
7910 error_for_asm (insn, "output operand is constant in %<asm%>");
7911 /* Prevent crash--use something we know is valid. */
7912 mode = word_mode;
7913 rl->out = gen_rtx_REG (mode, REGNO (reg_rtx));
7915 if (GET_MODE (reg_rtx) != mode)
7916 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7918 reload_reg_rtx_for_output[j] = reg_rtx;
7920 if (pseudo
7921 && optimize
7922 && REG_P (pseudo)
7923 && ! rtx_equal_p (rl->in_reg, pseudo)
7924 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7925 && reg_last_reload_reg[REGNO (pseudo)])
7927 int pseudo_no = REGNO (pseudo);
7928 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7930 /* We don't need to test full validity of last_regno for
7931 inherit here; we only want to know if the store actually
7932 matches the pseudo. */
7933 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7934 && reg_reloaded_contents[last_regno] == pseudo_no
7935 && spill_reg_store[last_regno]
7936 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7937 delete_output_reload (insn, j, last_regno, reg_rtx);
7940 old = rl->out_reg;
7941 if (old == 0
7942 || reg_rtx == 0
7943 || rtx_equal_p (old, reg_rtx))
7944 return;
7946 /* An output operand that dies right away does need a reload,
7947 but need not be copied from it. Show the new location in the
7948 REG_UNUSED note. */
7949 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7950 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7952 XEXP (note, 0) = reg_rtx;
7953 return;
7955 /* Likewise for a SUBREG of an operand that dies. */
7956 else if (GET_CODE (old) == SUBREG
7957 && REG_P (SUBREG_REG (old))
7958 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7959 SUBREG_REG (old))))
7961 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old), reg_rtx);
7962 return;
7964 else if (GET_CODE (old) == SCRATCH)
7965 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7966 but we don't want to make an output reload. */
7967 return;
7969 /* If is a JUMP_INSN, we can't support output reloads yet. */
7970 gcc_assert (NONJUMP_INSN_P (insn));
7972 emit_output_reload_insns (chain, rld + j, j);
7975 /* A reload copies values of MODE from register SRC to register DEST.
7976 Return true if it can be treated for inheritance purposes like a
7977 group of reloads, each one reloading a single hard register. The
7978 caller has already checked that (reg:MODE SRC) and (reg:MODE DEST)
7979 occupy the same number of hard registers. */
7981 static bool
7982 inherit_piecemeal_p (int dest ATTRIBUTE_UNUSED,
7983 int src ATTRIBUTE_UNUSED,
7984 enum machine_mode mode ATTRIBUTE_UNUSED)
7986 #ifdef CANNOT_CHANGE_MODE_CLASS
7987 return (!REG_CANNOT_CHANGE_MODE_P (dest, mode, reg_raw_mode[dest])
7988 && !REG_CANNOT_CHANGE_MODE_P (src, mode, reg_raw_mode[src]));
7989 #else
7990 return true;
7991 #endif
7994 /* Output insns to reload values in and out of the chosen reload regs. */
7996 static void
7997 emit_reload_insns (struct insn_chain *chain)
7999 rtx insn = chain->insn;
8001 int j;
8003 CLEAR_HARD_REG_SET (reg_reloaded_died);
8005 for (j = 0; j < reload_n_operands; j++)
8006 input_reload_insns[j] = input_address_reload_insns[j]
8007 = inpaddr_address_reload_insns[j]
8008 = output_reload_insns[j] = output_address_reload_insns[j]
8009 = outaddr_address_reload_insns[j]
8010 = other_output_reload_insns[j] = 0;
8011 other_input_address_reload_insns = 0;
8012 other_input_reload_insns = 0;
8013 operand_reload_insns = 0;
8014 other_operand_reload_insns = 0;
8016 /* Dump reloads into the dump file. */
8017 if (dump_file)
8019 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
8020 debug_reload_to_stream (dump_file);
8023 for (j = 0; j < n_reloads; j++)
8024 if (rld[j].reg_rtx && HARD_REGISTER_P (rld[j].reg_rtx))
8026 unsigned int i;
8028 for (i = REGNO (rld[j].reg_rtx); i < END_REGNO (rld[j].reg_rtx); i++)
8029 new_spill_reg_store[i] = 0;
8032 /* Now output the instructions to copy the data into and out of the
8033 reload registers. Do these in the order that the reloads were reported,
8034 since reloads of base and index registers precede reloads of operands
8035 and the operands may need the base and index registers reloaded. */
8037 for (j = 0; j < n_reloads; j++)
8039 do_input_reload (chain, rld + j, j);
8040 do_output_reload (chain, rld + j, j);
8043 /* Now write all the insns we made for reloads in the order expected by
8044 the allocation functions. Prior to the insn being reloaded, we write
8045 the following reloads:
8047 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
8049 RELOAD_OTHER reloads.
8051 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
8052 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
8053 RELOAD_FOR_INPUT reload for the operand.
8055 RELOAD_FOR_OPADDR_ADDRS reloads.
8057 RELOAD_FOR_OPERAND_ADDRESS reloads.
8059 After the insn being reloaded, we write the following:
8061 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
8062 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
8063 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
8064 reloads for the operand. The RELOAD_OTHER output reloads are
8065 output in descending order by reload number. */
8067 emit_insn_before (other_input_address_reload_insns, insn);
8068 emit_insn_before (other_input_reload_insns, insn);
8070 for (j = 0; j < reload_n_operands; j++)
8072 emit_insn_before (inpaddr_address_reload_insns[j], insn);
8073 emit_insn_before (input_address_reload_insns[j], insn);
8074 emit_insn_before (input_reload_insns[j], insn);
8077 emit_insn_before (other_operand_reload_insns, insn);
8078 emit_insn_before (operand_reload_insns, insn);
8080 for (j = 0; j < reload_n_operands; j++)
8082 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
8083 x = emit_insn_after (output_address_reload_insns[j], x);
8084 x = emit_insn_after (output_reload_insns[j], x);
8085 emit_insn_after (other_output_reload_insns[j], x);
8088 /* For all the spill regs newly reloaded in this instruction,
8089 record what they were reloaded from, so subsequent instructions
8090 can inherit the reloads.
8092 Update spill_reg_store for the reloads of this insn.
8093 Copy the elements that were updated in the loop above. */
8095 for (j = 0; j < n_reloads; j++)
8097 int r = reload_order[j];
8098 int i = reload_spill_index[r];
8100 /* If this is a non-inherited input reload from a pseudo, we must
8101 clear any memory of a previous store to the same pseudo. Only do
8102 something if there will not be an output reload for the pseudo
8103 being reloaded. */
8104 if (rld[r].in_reg != 0
8105 && ! (reload_inherited[r] || reload_override_in[r]))
8107 rtx reg = rld[r].in_reg;
8109 if (GET_CODE (reg) == SUBREG)
8110 reg = SUBREG_REG (reg);
8112 if (REG_P (reg)
8113 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
8114 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
8116 int nregno = REGNO (reg);
8118 if (reg_last_reload_reg[nregno])
8120 int last_regno = REGNO (reg_last_reload_reg[nregno]);
8122 if (reg_reloaded_contents[last_regno] == nregno)
8123 spill_reg_store[last_regno] = 0;
8128 /* I is nonneg if this reload used a register.
8129 If rld[r].reg_rtx is 0, this is an optional reload
8130 that we opted to ignore. */
8132 if (i >= 0 && rld[r].reg_rtx != 0)
8134 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
8135 int k;
8137 /* For a multi register reload, we need to check if all or part
8138 of the value lives to the end. */
8139 for (k = 0; k < nr; k++)
8140 if (reload_reg_reaches_end_p (i + k, r))
8141 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
8143 /* Maybe the spill reg contains a copy of reload_out. */
8144 if (rld[r].out != 0
8145 && (REG_P (rld[r].out)
8146 || (rld[r].out_reg
8147 ? REG_P (rld[r].out_reg)
8148 /* The reload value is an auto-modification of
8149 some kind. For PRE_INC, POST_INC, PRE_DEC
8150 and POST_DEC, we record an equivalence
8151 between the reload register and the operand
8152 on the optimistic assumption that we can make
8153 the equivalence hold. reload_as_needed must
8154 then either make it hold or invalidate the
8155 equivalence.
8157 PRE_MODIFY and POST_MODIFY addresses are reloaded
8158 somewhat differently, and allowing them here leads
8159 to problems. */
8160 : (GET_CODE (rld[r].out) != POST_MODIFY
8161 && GET_CODE (rld[r].out) != PRE_MODIFY))))
8163 rtx reg;
8165 reg = reload_reg_rtx_for_output[r];
8166 if (reload_reg_rtx_reaches_end_p (reg, r))
8168 enum machine_mode mode = GET_MODE (reg);
8169 int regno = REGNO (reg);
8170 int nregs = hard_regno_nregs[regno][mode];
8171 rtx out = (REG_P (rld[r].out)
8172 ? rld[r].out
8173 : rld[r].out_reg
8174 ? rld[r].out_reg
8175 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
8176 int out_regno = REGNO (out);
8177 int out_nregs = (!HARD_REGISTER_NUM_P (out_regno) ? 1
8178 : hard_regno_nregs[out_regno][mode]);
8179 bool piecemeal;
8181 spill_reg_store[regno] = new_spill_reg_store[regno];
8182 spill_reg_stored_to[regno] = out;
8183 reg_last_reload_reg[out_regno] = reg;
8185 piecemeal = (HARD_REGISTER_NUM_P (out_regno)
8186 && nregs == out_nregs
8187 && inherit_piecemeal_p (out_regno, regno, mode));
8189 /* If OUT_REGNO is a hard register, it may occupy more than
8190 one register. If it does, say what is in the
8191 rest of the registers assuming that both registers
8192 agree on how many words the object takes. If not,
8193 invalidate the subsequent registers. */
8195 if (HARD_REGISTER_NUM_P (out_regno))
8196 for (k = 1; k < out_nregs; k++)
8197 reg_last_reload_reg[out_regno + k]
8198 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
8200 /* Now do the inverse operation. */
8201 for (k = 0; k < nregs; k++)
8203 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
8204 reg_reloaded_contents[regno + k]
8205 = (!HARD_REGISTER_NUM_P (out_regno) || !piecemeal
8206 ? out_regno
8207 : out_regno + k);
8208 reg_reloaded_insn[regno + k] = insn;
8209 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
8210 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
8211 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8212 regno + k);
8213 else
8214 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8215 regno + k);
8219 /* Maybe the spill reg contains a copy of reload_in. Only do
8220 something if there will not be an output reload for
8221 the register being reloaded. */
8222 else if (rld[r].out_reg == 0
8223 && rld[r].in != 0
8224 && ((REG_P (rld[r].in)
8225 && !HARD_REGISTER_P (rld[r].in)
8226 && !REGNO_REG_SET_P (&reg_has_output_reload,
8227 REGNO (rld[r].in)))
8228 || (REG_P (rld[r].in_reg)
8229 && !REGNO_REG_SET_P (&reg_has_output_reload,
8230 REGNO (rld[r].in_reg))))
8231 && !reg_set_p (reload_reg_rtx_for_input[r], PATTERN (insn)))
8233 rtx reg;
8235 reg = reload_reg_rtx_for_input[r];
8236 if (reload_reg_rtx_reaches_end_p (reg, r))
8238 enum machine_mode mode;
8239 int regno;
8240 int nregs;
8241 int in_regno;
8242 int in_nregs;
8243 rtx in;
8244 bool piecemeal;
8246 mode = GET_MODE (reg);
8247 regno = REGNO (reg);
8248 nregs = hard_regno_nregs[regno][mode];
8249 if (REG_P (rld[r].in)
8250 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
8251 in = rld[r].in;
8252 else if (REG_P (rld[r].in_reg))
8253 in = rld[r].in_reg;
8254 else
8255 in = XEXP (rld[r].in_reg, 0);
8256 in_regno = REGNO (in);
8258 in_nregs = (!HARD_REGISTER_NUM_P (in_regno) ? 1
8259 : hard_regno_nregs[in_regno][mode]);
8261 reg_last_reload_reg[in_regno] = reg;
8263 piecemeal = (HARD_REGISTER_NUM_P (in_regno)
8264 && nregs == in_nregs
8265 && inherit_piecemeal_p (regno, in_regno, mode));
8267 if (HARD_REGISTER_NUM_P (in_regno))
8268 for (k = 1; k < in_nregs; k++)
8269 reg_last_reload_reg[in_regno + k]
8270 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
8272 /* Unless we inherited this reload, show we haven't
8273 recently done a store.
8274 Previous stores of inherited auto_inc expressions
8275 also have to be discarded. */
8276 if (! reload_inherited[r]
8277 || (rld[r].out && ! rld[r].out_reg))
8278 spill_reg_store[regno] = 0;
8280 for (k = 0; k < nregs; k++)
8282 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
8283 reg_reloaded_contents[regno + k]
8284 = (!HARD_REGISTER_NUM_P (in_regno) || !piecemeal
8285 ? in_regno
8286 : in_regno + k);
8287 reg_reloaded_insn[regno + k] = insn;
8288 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
8289 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
8290 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8291 regno + k);
8292 else
8293 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8294 regno + k);
8300 /* The following if-statement was #if 0'd in 1.34 (or before...).
8301 It's reenabled in 1.35 because supposedly nothing else
8302 deals with this problem. */
8304 /* If a register gets output-reloaded from a non-spill register,
8305 that invalidates any previous reloaded copy of it.
8306 But forget_old_reloads_1 won't get to see it, because
8307 it thinks only about the original insn. So invalidate it here.
8308 Also do the same thing for RELOAD_OTHER constraints where the
8309 output is discarded. */
8310 if (i < 0
8311 && ((rld[r].out != 0
8312 && (REG_P (rld[r].out)
8313 || (MEM_P (rld[r].out)
8314 && REG_P (rld[r].out_reg))))
8315 || (rld[r].out == 0 && rld[r].out_reg
8316 && REG_P (rld[r].out_reg))))
8318 rtx out = ((rld[r].out && REG_P (rld[r].out))
8319 ? rld[r].out : rld[r].out_reg);
8320 int out_regno = REGNO (out);
8321 enum machine_mode mode = GET_MODE (out);
8323 /* REG_RTX is now set or clobbered by the main instruction.
8324 As the comment above explains, forget_old_reloads_1 only
8325 sees the original instruction, and there is no guarantee
8326 that the original instruction also clobbered REG_RTX.
8327 For example, if find_reloads sees that the input side of
8328 a matched operand pair dies in this instruction, it may
8329 use the input register as the reload register.
8331 Calling forget_old_reloads_1 is a waste of effort if
8332 REG_RTX is also the output register.
8334 If we know that REG_RTX holds the value of a pseudo
8335 register, the code after the call will record that fact. */
8336 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
8337 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
8339 if (!HARD_REGISTER_NUM_P (out_regno))
8341 rtx src_reg, store_insn = NULL_RTX;
8343 reg_last_reload_reg[out_regno] = 0;
8345 /* If we can find a hard register that is stored, record
8346 the storing insn so that we may delete this insn with
8347 delete_output_reload. */
8348 src_reg = reload_reg_rtx_for_output[r];
8350 if (src_reg)
8352 if (reload_reg_rtx_reaches_end_p (src_reg, r))
8353 store_insn = new_spill_reg_store[REGNO (src_reg)];
8354 else
8355 src_reg = NULL_RTX;
8357 else
8359 /* If this is an optional reload, try to find the
8360 source reg from an input reload. */
8361 rtx set = single_set (insn);
8362 if (set && SET_DEST (set) == rld[r].out)
8364 int k;
8366 src_reg = SET_SRC (set);
8367 store_insn = insn;
8368 for (k = 0; k < n_reloads; k++)
8370 if (rld[k].in == src_reg)
8372 src_reg = reload_reg_rtx_for_input[k];
8373 break;
8378 if (src_reg && REG_P (src_reg)
8379 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
8381 int src_regno, src_nregs, k;
8382 rtx note;
8384 gcc_assert (GET_MODE (src_reg) == mode);
8385 src_regno = REGNO (src_reg);
8386 src_nregs = hard_regno_nregs[src_regno][mode];
8387 /* The place where to find a death note varies with
8388 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
8389 necessarily checked exactly in the code that moves
8390 notes, so just check both locations. */
8391 note = find_regno_note (insn, REG_DEAD, src_regno);
8392 if (! note && store_insn)
8393 note = find_regno_note (store_insn, REG_DEAD, src_regno);
8394 for (k = 0; k < src_nregs; k++)
8396 spill_reg_store[src_regno + k] = store_insn;
8397 spill_reg_stored_to[src_regno + k] = out;
8398 reg_reloaded_contents[src_regno + k] = out_regno;
8399 reg_reloaded_insn[src_regno + k] = store_insn;
8400 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + k);
8401 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + k);
8402 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + k,
8403 mode))
8404 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8405 src_regno + k);
8406 else
8407 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8408 src_regno + k);
8409 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + k);
8410 if (note)
8411 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
8412 else
8413 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
8415 reg_last_reload_reg[out_regno] = src_reg;
8416 /* We have to set reg_has_output_reload here, or else
8417 forget_old_reloads_1 will clear reg_last_reload_reg
8418 right away. */
8419 SET_REGNO_REG_SET (&reg_has_output_reload,
8420 out_regno);
8423 else
8425 int k, out_nregs = hard_regno_nregs[out_regno][mode];
8427 for (k = 0; k < out_nregs; k++)
8428 reg_last_reload_reg[out_regno + k] = 0;
8432 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
8435 /* Go through the motions to emit INSN and test if it is strictly valid.
8436 Return the emitted insn if valid, else return NULL. */
8438 static rtx
8439 emit_insn_if_valid_for_reload (rtx insn)
8441 rtx last = get_last_insn ();
8442 int code;
8444 insn = emit_insn (insn);
8445 code = recog_memoized (insn);
8447 if (code >= 0)
8449 extract_insn (insn);
8450 /* We want constrain operands to treat this insn strictly in its
8451 validity determination, i.e., the way it would after reload has
8452 completed. */
8453 if (constrain_operands (1))
8454 return insn;
8457 delete_insns_since (last);
8458 return NULL;
8461 #ifdef SECONDARY_MEMORY_NEEDED
8462 /* If X is not a subreg, return it unmodified. If it is a subreg,
8463 look up whether we made a replacement for the SUBREG_REG. Return
8464 either the replacement or the SUBREG_REG. */
8466 static rtx
8467 replaced_subreg (rtx x)
8469 if (GET_CODE (x) == SUBREG)
8470 return find_replacement (&SUBREG_REG (x));
8471 return x;
8473 #endif
8475 /* Emit code to perform a reload from IN (which may be a reload register) to
8476 OUT (which may also be a reload register). IN or OUT is from operand
8477 OPNUM with reload type TYPE.
8479 Returns first insn emitted. */
8481 static rtx
8482 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
8484 rtx last = get_last_insn ();
8485 rtx tem;
8486 #ifdef SECONDARY_MEMORY_NEEDED
8487 rtx tem1, tem2;
8488 #endif
8490 /* If IN is a paradoxical SUBREG, remove it and try to put the
8491 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
8492 if (!strip_paradoxical_subreg (&in, &out))
8493 strip_paradoxical_subreg (&out, &in);
8495 /* How to do this reload can get quite tricky. Normally, we are being
8496 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
8497 register that didn't get a hard register. In that case we can just
8498 call emit_move_insn.
8500 We can also be asked to reload a PLUS that adds a register or a MEM to
8501 another register, constant or MEM. This can occur during frame pointer
8502 elimination and while reloading addresses. This case is handled by
8503 trying to emit a single insn to perform the add. If it is not valid,
8504 we use a two insn sequence.
8506 Or we can be asked to reload an unary operand that was a fragment of
8507 an addressing mode, into a register. If it isn't recognized as-is,
8508 we try making the unop operand and the reload-register the same:
8509 (set reg:X (unop:X expr:Y))
8510 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
8512 Finally, we could be called to handle an 'o' constraint by putting
8513 an address into a register. In that case, we first try to do this
8514 with a named pattern of "reload_load_address". If no such pattern
8515 exists, we just emit a SET insn and hope for the best (it will normally
8516 be valid on machines that use 'o').
8518 This entire process is made complex because reload will never
8519 process the insns we generate here and so we must ensure that
8520 they will fit their constraints and also by the fact that parts of
8521 IN might be being reloaded separately and replaced with spill registers.
8522 Because of this, we are, in some sense, just guessing the right approach
8523 here. The one listed above seems to work.
8525 ??? At some point, this whole thing needs to be rethought. */
8527 if (GET_CODE (in) == PLUS
8528 && (REG_P (XEXP (in, 0))
8529 || GET_CODE (XEXP (in, 0)) == SUBREG
8530 || MEM_P (XEXP (in, 0)))
8531 && (REG_P (XEXP (in, 1))
8532 || GET_CODE (XEXP (in, 1)) == SUBREG
8533 || CONSTANT_P (XEXP (in, 1))
8534 || MEM_P (XEXP (in, 1))))
8536 /* We need to compute the sum of a register or a MEM and another
8537 register, constant, or MEM, and put it into the reload
8538 register. The best possible way of doing this is if the machine
8539 has a three-operand ADD insn that accepts the required operands.
8541 The simplest approach is to try to generate such an insn and see if it
8542 is recognized and matches its constraints. If so, it can be used.
8544 It might be better not to actually emit the insn unless it is valid,
8545 but we need to pass the insn as an operand to `recog' and
8546 `extract_insn' and it is simpler to emit and then delete the insn if
8547 not valid than to dummy things up. */
8549 rtx op0, op1, tem, insn;
8550 enum insn_code code;
8552 op0 = find_replacement (&XEXP (in, 0));
8553 op1 = find_replacement (&XEXP (in, 1));
8555 /* Since constraint checking is strict, commutativity won't be
8556 checked, so we need to do that here to avoid spurious failure
8557 if the add instruction is two-address and the second operand
8558 of the add is the same as the reload reg, which is frequently
8559 the case. If the insn would be A = B + A, rearrange it so
8560 it will be A = A + B as constrain_operands expects. */
8562 if (REG_P (XEXP (in, 1))
8563 && REGNO (out) == REGNO (XEXP (in, 1)))
8564 tem = op0, op0 = op1, op1 = tem;
8566 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
8567 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
8569 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8570 if (insn)
8571 return insn;
8573 /* If that failed, we must use a conservative two-insn sequence.
8575 Use a move to copy one operand into the reload register. Prefer
8576 to reload a constant, MEM or pseudo since the move patterns can
8577 handle an arbitrary operand. If OP1 is not a constant, MEM or
8578 pseudo and OP1 is not a valid operand for an add instruction, then
8579 reload OP1.
8581 After reloading one of the operands into the reload register, add
8582 the reload register to the output register.
8584 If there is another way to do this for a specific machine, a
8585 DEFINE_PEEPHOLE should be specified that recognizes the sequence
8586 we emit below. */
8588 code = optab_handler (add_optab, GET_MODE (out));
8590 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
8591 || (REG_P (op1)
8592 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
8593 || (code != CODE_FOR_nothing
8594 && !insn_operand_matches (code, 2, op1)))
8595 tem = op0, op0 = op1, op1 = tem;
8597 gen_reload (out, op0, opnum, type);
8599 /* If OP0 and OP1 are the same, we can use OUT for OP1.
8600 This fixes a problem on the 32K where the stack pointer cannot
8601 be used as an operand of an add insn. */
8603 if (rtx_equal_p (op0, op1))
8604 op1 = out;
8606 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
8607 if (insn)
8609 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
8610 set_dst_reg_note (insn, REG_EQUIV, in, out);
8611 return insn;
8614 /* If that failed, copy the address register to the reload register.
8615 Then add the constant to the reload register. */
8617 gcc_assert (!reg_overlap_mentioned_p (out, op0));
8618 gen_reload (out, op1, opnum, type);
8619 insn = emit_insn (gen_add2_insn (out, op0));
8620 set_dst_reg_note (insn, REG_EQUIV, in, out);
8623 #ifdef SECONDARY_MEMORY_NEEDED
8624 /* If we need a memory location to do the move, do it that way. */
8625 else if ((tem1 = replaced_subreg (in), tem2 = replaced_subreg (out),
8626 (REG_P (tem1) && REG_P (tem2)))
8627 && REGNO (tem1) < FIRST_PSEUDO_REGISTER
8628 && REGNO (tem2) < FIRST_PSEUDO_REGISTER
8629 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (tem1)),
8630 REGNO_REG_CLASS (REGNO (tem2)),
8631 GET_MODE (out)))
8633 /* Get the memory to use and rewrite both registers to its mode. */
8634 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
8636 if (GET_MODE (loc) != GET_MODE (out))
8637 out = gen_rtx_REG (GET_MODE (loc), reg_or_subregno (out));
8639 if (GET_MODE (loc) != GET_MODE (in))
8640 in = gen_rtx_REG (GET_MODE (loc), reg_or_subregno (in));
8642 gen_reload (loc, in, opnum, type);
8643 gen_reload (out, loc, opnum, type);
8645 #endif
8646 else if (REG_P (out) && UNARY_P (in))
8648 rtx insn;
8649 rtx op1;
8650 rtx out_moded;
8651 rtx set;
8653 op1 = find_replacement (&XEXP (in, 0));
8654 if (op1 != XEXP (in, 0))
8655 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
8657 /* First, try a plain SET. */
8658 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8659 if (set)
8660 return set;
8662 /* If that failed, move the inner operand to the reload
8663 register, and try the same unop with the inner expression
8664 replaced with the reload register. */
8666 if (GET_MODE (op1) != GET_MODE (out))
8667 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
8668 else
8669 out_moded = out;
8671 gen_reload (out_moded, op1, opnum, type);
8673 insn
8674 = gen_rtx_SET (VOIDmode, out,
8675 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
8676 out_moded));
8677 insn = emit_insn_if_valid_for_reload (insn);
8678 if (insn)
8680 set_unique_reg_note (insn, REG_EQUIV, in);
8681 return insn;
8684 fatal_insn ("failure trying to reload:", set);
8686 /* If IN is a simple operand, use gen_move_insn. */
8687 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8689 tem = emit_insn (gen_move_insn (out, in));
8690 /* IN may contain a LABEL_REF, if so add a REG_LABEL_OPERAND note. */
8691 mark_jump_label (in, tem, 0);
8694 #ifdef HAVE_reload_load_address
8695 else if (HAVE_reload_load_address)
8696 emit_insn (gen_reload_load_address (out, in));
8697 #endif
8699 /* Otherwise, just write (set OUT IN) and hope for the best. */
8700 else
8701 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8703 /* Return the first insn emitted.
8704 We can not just return get_last_insn, because there may have
8705 been multiple instructions emitted. Also note that gen_move_insn may
8706 emit more than one insn itself, so we can not assume that there is one
8707 insn emitted per emit_insn_before call. */
8709 return last ? NEXT_INSN (last) : get_insns ();
8712 /* Delete a previously made output-reload whose result we now believe
8713 is not needed. First we double-check.
8715 INSN is the insn now being processed.
8716 LAST_RELOAD_REG is the hard register number for which we want to delete
8717 the last output reload.
8718 J is the reload-number that originally used REG. The caller has made
8719 certain that reload J doesn't use REG any longer for input.
8720 NEW_RELOAD_REG is reload register that reload J is using for REG. */
8722 static void
8723 delete_output_reload (rtx insn, int j, int last_reload_reg, rtx new_reload_reg)
8725 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8726 rtx reg = spill_reg_stored_to[last_reload_reg];
8727 int k;
8728 int n_occurrences;
8729 int n_inherited = 0;
8730 rtx i1;
8731 rtx substed;
8732 unsigned regno;
8733 int nregs;
8735 /* It is possible that this reload has been only used to set another reload
8736 we eliminated earlier and thus deleted this instruction too. */
8737 if (INSN_DELETED_P (output_reload_insn))
8738 return;
8740 /* Get the raw pseudo-register referred to. */
8742 while (GET_CODE (reg) == SUBREG)
8743 reg = SUBREG_REG (reg);
8744 substed = reg_equiv_memory_loc (REGNO (reg));
8746 /* This is unsafe if the operand occurs more often in the current
8747 insn than it is inherited. */
8748 for (k = n_reloads - 1; k >= 0; k--)
8750 rtx reg2 = rld[k].in;
8751 if (! reg2)
8752 continue;
8753 if (MEM_P (reg2) || reload_override_in[k])
8754 reg2 = rld[k].in_reg;
8755 #ifdef AUTO_INC_DEC
8756 if (rld[k].out && ! rld[k].out_reg)
8757 reg2 = XEXP (rld[k].in_reg, 0);
8758 #endif
8759 while (GET_CODE (reg2) == SUBREG)
8760 reg2 = SUBREG_REG (reg2);
8761 if (rtx_equal_p (reg2, reg))
8763 if (reload_inherited[k] || reload_override_in[k] || k == j)
8764 n_inherited++;
8765 else
8766 return;
8769 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
8770 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
8771 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
8772 reg, 0);
8773 if (substed)
8774 n_occurrences += count_occurrences (PATTERN (insn),
8775 eliminate_regs (substed, VOIDmode,
8776 NULL_RTX), 0);
8777 for (i1 = reg_equiv_alt_mem_list (REGNO (reg)); i1; i1 = XEXP (i1, 1))
8779 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8780 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8782 if (n_occurrences > n_inherited)
8783 return;
8785 regno = REGNO (reg);
8786 if (regno >= FIRST_PSEUDO_REGISTER)
8787 nregs = 1;
8788 else
8789 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
8791 /* If the pseudo-reg we are reloading is no longer referenced
8792 anywhere between the store into it and here,
8793 and we're within the same basic block, then the value can only
8794 pass through the reload reg and end up here.
8795 Otherwise, give up--return. */
8796 for (i1 = NEXT_INSN (output_reload_insn);
8797 i1 != insn; i1 = NEXT_INSN (i1))
8799 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8800 return;
8801 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8802 && refers_to_regno_p (regno, regno + nregs, PATTERN (i1), NULL))
8804 /* If this is USE in front of INSN, we only have to check that
8805 there are no more references than accounted for by inheritance. */
8806 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8808 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8809 i1 = NEXT_INSN (i1);
8811 if (n_occurrences <= n_inherited && i1 == insn)
8812 break;
8813 return;
8817 /* We will be deleting the insn. Remove the spill reg information. */
8818 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8820 spill_reg_store[last_reload_reg + k] = 0;
8821 spill_reg_stored_to[last_reload_reg + k] = 0;
8824 /* The caller has already checked that REG dies or is set in INSN.
8825 It has also checked that we are optimizing, and thus some
8826 inaccuracies in the debugging information are acceptable.
8827 So we could just delete output_reload_insn. But in some cases
8828 we can improve the debugging information without sacrificing
8829 optimization - maybe even improving the code: See if the pseudo
8830 reg has been completely replaced with reload regs. If so, delete
8831 the store insn and forget we had a stack slot for the pseudo. */
8832 if (rld[j].out != rld[j].in
8833 && REG_N_DEATHS (REGNO (reg)) == 1
8834 && REG_N_SETS (REGNO (reg)) == 1
8835 && REG_BASIC_BLOCK (REGNO (reg)) >= NUM_FIXED_BLOCKS
8836 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8838 rtx i2;
8840 /* We know that it was used only between here and the beginning of
8841 the current basic block. (We also know that the last use before
8842 INSN was the output reload we are thinking of deleting, but never
8843 mind that.) Search that range; see if any ref remains. */
8844 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8846 rtx set = single_set (i2);
8848 /* Uses which just store in the pseudo don't count,
8849 since if they are the only uses, they are dead. */
8850 if (set != 0 && SET_DEST (set) == reg)
8851 continue;
8852 if (LABEL_P (i2)
8853 || JUMP_P (i2))
8854 break;
8855 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8856 && reg_mentioned_p (reg, PATTERN (i2)))
8858 /* Some other ref remains; just delete the output reload we
8859 know to be dead. */
8860 delete_address_reloads (output_reload_insn, insn);
8861 delete_insn (output_reload_insn);
8862 return;
8866 /* Delete the now-dead stores into this pseudo. Note that this
8867 loop also takes care of deleting output_reload_insn. */
8868 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8870 rtx set = single_set (i2);
8872 if (set != 0 && SET_DEST (set) == reg)
8874 delete_address_reloads (i2, insn);
8875 delete_insn (i2);
8877 if (LABEL_P (i2)
8878 || JUMP_P (i2))
8879 break;
8882 /* For the debugging info, say the pseudo lives in this reload reg. */
8883 reg_renumber[REGNO (reg)] = REGNO (new_reload_reg);
8884 if (ira_conflicts_p)
8885 /* Inform IRA about the change. */
8886 ira_mark_allocation_change (REGNO (reg));
8887 alter_reg (REGNO (reg), -1, false);
8889 else
8891 delete_address_reloads (output_reload_insn, insn);
8892 delete_insn (output_reload_insn);
8896 /* We are going to delete DEAD_INSN. Recursively delete loads of
8897 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8898 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8899 static void
8900 delete_address_reloads (rtx dead_insn, rtx current_insn)
8902 rtx set = single_set (dead_insn);
8903 rtx set2, dst, prev, next;
8904 if (set)
8906 rtx dst = SET_DEST (set);
8907 if (MEM_P (dst))
8908 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8910 /* If we deleted the store from a reloaded post_{in,de}c expression,
8911 we can delete the matching adds. */
8912 prev = PREV_INSN (dead_insn);
8913 next = NEXT_INSN (dead_insn);
8914 if (! prev || ! next)
8915 return;
8916 set = single_set (next);
8917 set2 = single_set (prev);
8918 if (! set || ! set2
8919 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8920 || !CONST_INT_P (XEXP (SET_SRC (set), 1))
8921 || !CONST_INT_P (XEXP (SET_SRC (set2), 1)))
8922 return;
8923 dst = SET_DEST (set);
8924 if (! rtx_equal_p (dst, SET_DEST (set2))
8925 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8926 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8927 || (INTVAL (XEXP (SET_SRC (set), 1))
8928 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8929 return;
8930 delete_related_insns (prev);
8931 delete_related_insns (next);
8934 /* Subfunction of delete_address_reloads: process registers found in X. */
8935 static void
8936 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8938 rtx prev, set, dst, i2;
8939 int i, j;
8940 enum rtx_code code = GET_CODE (x);
8942 if (code != REG)
8944 const char *fmt = GET_RTX_FORMAT (code);
8945 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8947 if (fmt[i] == 'e')
8948 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8949 else if (fmt[i] == 'E')
8951 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8952 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8953 current_insn);
8956 return;
8959 if (spill_reg_order[REGNO (x)] < 0)
8960 return;
8962 /* Scan backwards for the insn that sets x. This might be a way back due
8963 to inheritance. */
8964 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8966 code = GET_CODE (prev);
8967 if (code == CODE_LABEL || code == JUMP_INSN)
8968 return;
8969 if (!INSN_P (prev))
8970 continue;
8971 if (reg_set_p (x, PATTERN (prev)))
8972 break;
8973 if (reg_referenced_p (x, PATTERN (prev)))
8974 return;
8976 if (! prev || INSN_UID (prev) < reload_first_uid)
8977 return;
8978 /* Check that PREV only sets the reload register. */
8979 set = single_set (prev);
8980 if (! set)
8981 return;
8982 dst = SET_DEST (set);
8983 if (!REG_P (dst)
8984 || ! rtx_equal_p (dst, x))
8985 return;
8986 if (! reg_set_p (dst, PATTERN (dead_insn)))
8988 /* Check if DST was used in a later insn -
8989 it might have been inherited. */
8990 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8992 if (LABEL_P (i2))
8993 break;
8994 if (! INSN_P (i2))
8995 continue;
8996 if (reg_referenced_p (dst, PATTERN (i2)))
8998 /* If there is a reference to the register in the current insn,
8999 it might be loaded in a non-inherited reload. If no other
9000 reload uses it, that means the register is set before
9001 referenced. */
9002 if (i2 == current_insn)
9004 for (j = n_reloads - 1; j >= 0; j--)
9005 if ((rld[j].reg_rtx == dst && reload_inherited[j])
9006 || reload_override_in[j] == dst)
9007 return;
9008 for (j = n_reloads - 1; j >= 0; j--)
9009 if (rld[j].in && rld[j].reg_rtx == dst)
9010 break;
9011 if (j >= 0)
9012 break;
9014 return;
9016 if (JUMP_P (i2))
9017 break;
9018 /* If DST is still live at CURRENT_INSN, check if it is used for
9019 any reload. Note that even if CURRENT_INSN sets DST, we still
9020 have to check the reloads. */
9021 if (i2 == current_insn)
9023 for (j = n_reloads - 1; j >= 0; j--)
9024 if ((rld[j].reg_rtx == dst && reload_inherited[j])
9025 || reload_override_in[j] == dst)
9026 return;
9027 /* ??? We can't finish the loop here, because dst might be
9028 allocated to a pseudo in this block if no reload in this
9029 block needs any of the classes containing DST - see
9030 spill_hard_reg. There is no easy way to tell this, so we
9031 have to scan till the end of the basic block. */
9033 if (reg_set_p (dst, PATTERN (i2)))
9034 break;
9037 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
9038 reg_reloaded_contents[REGNO (dst)] = -1;
9039 delete_insn (prev);
9042 /* Output reload-insns to reload VALUE into RELOADREG.
9043 VALUE is an autoincrement or autodecrement RTX whose operand
9044 is a register or memory location;
9045 so reloading involves incrementing that location.
9046 IN is either identical to VALUE, or some cheaper place to reload from.
9048 INC_AMOUNT is the number to increment or decrement by (always positive).
9049 This cannot be deduced from VALUE. */
9051 static void
9052 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
9054 /* REG or MEM to be copied and incremented. */
9055 rtx incloc = find_replacement (&XEXP (value, 0));
9056 /* Nonzero if increment after copying. */
9057 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
9058 || GET_CODE (value) == POST_MODIFY);
9059 rtx last;
9060 rtx inc;
9061 rtx add_insn;
9062 int code;
9063 rtx real_in = in == value ? incloc : in;
9065 /* No hard register is equivalent to this register after
9066 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
9067 we could inc/dec that register as well (maybe even using it for
9068 the source), but I'm not sure it's worth worrying about. */
9069 if (REG_P (incloc))
9070 reg_last_reload_reg[REGNO (incloc)] = 0;
9072 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
9074 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
9075 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
9077 else
9079 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
9080 inc_amount = -inc_amount;
9082 inc = GEN_INT (inc_amount);
9085 /* If this is post-increment, first copy the location to the reload reg. */
9086 if (post && real_in != reloadreg)
9087 emit_insn (gen_move_insn (reloadreg, real_in));
9089 if (in == value)
9091 /* See if we can directly increment INCLOC. Use a method similar to
9092 that in gen_reload. */
9094 last = get_last_insn ();
9095 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
9096 gen_rtx_PLUS (GET_MODE (incloc),
9097 incloc, inc)));
9099 code = recog_memoized (add_insn);
9100 if (code >= 0)
9102 extract_insn (add_insn);
9103 if (constrain_operands (1))
9105 /* If this is a pre-increment and we have incremented the value
9106 where it lives, copy the incremented value to RELOADREG to
9107 be used as an address. */
9109 if (! post)
9110 emit_insn (gen_move_insn (reloadreg, incloc));
9111 return;
9114 delete_insns_since (last);
9117 /* If couldn't do the increment directly, must increment in RELOADREG.
9118 The way we do this depends on whether this is pre- or post-increment.
9119 For pre-increment, copy INCLOC to the reload register, increment it
9120 there, then save back. */
9122 if (! post)
9124 if (in != reloadreg)
9125 emit_insn (gen_move_insn (reloadreg, real_in));
9126 emit_insn (gen_add2_insn (reloadreg, inc));
9127 emit_insn (gen_move_insn (incloc, reloadreg));
9129 else
9131 /* Postincrement.
9132 Because this might be a jump insn or a compare, and because RELOADREG
9133 may not be available after the insn in an input reload, we must do
9134 the incrementation before the insn being reloaded for.
9136 We have already copied IN to RELOADREG. Increment the copy in
9137 RELOADREG, save that back, then decrement RELOADREG so it has
9138 the original value. */
9140 emit_insn (gen_add2_insn (reloadreg, inc));
9141 emit_insn (gen_move_insn (incloc, reloadreg));
9142 if (CONST_INT_P (inc))
9143 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
9144 else
9145 emit_insn (gen_sub2_insn (reloadreg, inc));
9149 #ifdef AUTO_INC_DEC
9150 static void
9151 add_auto_inc_notes (rtx insn, rtx x)
9153 enum rtx_code code = GET_CODE (x);
9154 const char *fmt;
9155 int i, j;
9157 if (code == MEM && auto_inc_p (XEXP (x, 0)))
9159 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
9160 return;
9163 /* Scan all the operand sub-expressions. */
9164 fmt = GET_RTX_FORMAT (code);
9165 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9167 if (fmt[i] == 'e')
9168 add_auto_inc_notes (insn, XEXP (x, i));
9169 else if (fmt[i] == 'E')
9170 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9171 add_auto_inc_notes (insn, XVECEXP (x, i, j));
9174 #endif