PR c++/53858
[official-gcc.git] / gcc / reload1.c
blob00980ce8786ff87cb2451ad05f6c70f5e106e4b3
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"
50 /* This file contains the reload pass of the compiler, which is
51 run after register allocation has been done. It checks that
52 each insn is valid (operands required to be in registers really
53 are in registers of the proper class) and fixes up invalid ones
54 by copying values temporarily into registers for the insns
55 that need them.
57 The results of register allocation are described by the vector
58 reg_renumber; the insns still contain pseudo regs, but reg_renumber
59 can be used to find which hard reg, if any, a pseudo reg is in.
61 The technique we always use is to free up a few hard regs that are
62 called ``reload regs'', and for each place where a pseudo reg
63 must be in a hard reg, copy it temporarily into one of the reload regs.
65 Reload regs are allocated locally for every instruction that needs
66 reloads. When there are pseudos which are allocated to a register that
67 has been chosen as a reload reg, such pseudos must be ``spilled''.
68 This means that they go to other hard regs, or to stack slots if no other
69 available hard regs can be found. Spilling can invalidate more
70 insns, requiring additional need for reloads, so we must keep checking
71 until the process stabilizes.
73 For machines with different classes of registers, we must keep track
74 of the register class needed for each reload, and make sure that
75 we allocate enough reload registers of each class.
77 The file reload.c contains the code that checks one insn for
78 validity and reports the reloads that it needs. This file
79 is in charge of scanning the entire rtl code, accumulating the
80 reload needs, spilling, assigning reload registers to use for
81 fixing up each insn, and generating the new insns to copy values
82 into the reload registers. */
84 struct target_reload default_target_reload;
85 #if SWITCHABLE_TARGET
86 struct target_reload *this_target_reload = &default_target_reload;
87 #endif
89 #define spill_indirect_levels \
90 (this_target_reload->x_spill_indirect_levels)
92 /* During reload_as_needed, element N contains a REG rtx for the hard reg
93 into which reg N has been reloaded (perhaps for a previous insn). */
94 static rtx *reg_last_reload_reg;
96 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
97 for an output reload that stores into reg N. */
98 static regset_head reg_has_output_reload;
100 /* Indicates which hard regs are reload-registers for an output reload
101 in the current insn. */
102 static HARD_REG_SET reg_is_output_reload;
104 /* Widest width in which each pseudo reg is referred to (via subreg). */
105 static unsigned int *reg_max_ref_width;
107 /* Vector to remember old contents of reg_renumber before spilling. */
108 static short *reg_old_renumber;
110 /* During reload_as_needed, element N contains the last pseudo regno reloaded
111 into hard register N. If that pseudo reg occupied more than one register,
112 reg_reloaded_contents points to that pseudo for each spill register in
113 use; all of these must remain set for an inheritance to occur. */
114 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
116 /* During reload_as_needed, element N contains the insn for which
117 hard register N was last used. Its contents are significant only
118 when reg_reloaded_valid is set for this register. */
119 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
121 /* Indicate if reg_reloaded_insn / reg_reloaded_contents is valid. */
122 static HARD_REG_SET reg_reloaded_valid;
123 /* Indicate if the register was dead at the end of the reload.
124 This is only valid if reg_reloaded_contents is set and valid. */
125 static HARD_REG_SET reg_reloaded_dead;
127 /* Indicate whether the register's current value is one that is not
128 safe to retain across a call, even for registers that are normally
129 call-saved. This is only meaningful for members of reg_reloaded_valid. */
130 static HARD_REG_SET reg_reloaded_call_part_clobbered;
132 /* Number of spill-regs so far; number of valid elements of spill_regs. */
133 static int n_spills;
135 /* In parallel with spill_regs, contains REG rtx's for those regs.
136 Holds the last rtx used for any given reg, or 0 if it has never
137 been used for spilling yet. This rtx is reused, provided it has
138 the proper mode. */
139 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
141 /* In parallel with spill_regs, contains nonzero for a spill reg
142 that was stored after the last time it was used.
143 The precise value is the insn generated to do the store. */
144 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
146 /* This is the register that was stored with spill_reg_store. This is a
147 copy of reload_out / reload_out_reg when the value was stored; if
148 reload_out is a MEM, spill_reg_stored_to will be set to reload_out_reg. */
149 static rtx spill_reg_stored_to[FIRST_PSEUDO_REGISTER];
151 /* This table is the inverse mapping of spill_regs:
152 indexed by hard reg number,
153 it contains the position of that reg in spill_regs,
154 or -1 for something that is not in spill_regs.
156 ?!? This is no longer accurate. */
157 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
159 /* This reg set indicates registers that can't be used as spill registers for
160 the currently processed insn. These are the hard registers which are live
161 during the insn, but not allocated to pseudos, as well as fixed
162 registers. */
163 static HARD_REG_SET bad_spill_regs;
165 /* These are the hard registers that can't be used as spill register for any
166 insn. This includes registers used for user variables and registers that
167 we can't eliminate. A register that appears in this set also can't be used
168 to retry register allocation. */
169 static HARD_REG_SET bad_spill_regs_global;
171 /* Describes order of use of registers for reloading
172 of spilled pseudo-registers. `n_spills' is the number of
173 elements that are actually valid; new ones are added at the end.
175 Both spill_regs and spill_reg_order are used on two occasions:
176 once during find_reload_regs, where they keep track of the spill registers
177 for a single insn, but also during reload_as_needed where they show all
178 the registers ever used by reload. For the latter case, the information
179 is calculated during finish_spills. */
180 static short spill_regs[FIRST_PSEUDO_REGISTER];
182 /* This vector of reg sets indicates, for each pseudo, which hard registers
183 may not be used for retrying global allocation because the register was
184 formerly spilled from one of them. If we allowed reallocating a pseudo to
185 a register that it was already allocated to, reload might not
186 terminate. */
187 static HARD_REG_SET *pseudo_previous_regs;
189 /* This vector of reg sets indicates, for each pseudo, which hard
190 registers may not be used for retrying global allocation because they
191 are used as spill registers during one of the insns in which the
192 pseudo is live. */
193 static HARD_REG_SET *pseudo_forbidden_regs;
195 /* All hard regs that have been used as spill registers for any insn are
196 marked in this set. */
197 static HARD_REG_SET used_spill_regs;
199 /* Index of last register assigned as a spill register. We allocate in
200 a round-robin fashion. */
201 static int last_spill_reg;
203 /* Record the stack slot for each spilled hard register. */
204 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
206 /* Width allocated so far for that stack slot. */
207 static unsigned int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
209 /* Record which pseudos needed to be spilled. */
210 static regset_head spilled_pseudos;
212 /* Record which pseudos changed their allocation in finish_spills. */
213 static regset_head changed_allocation_pseudos;
215 /* Used for communication between order_regs_for_reload and count_pseudo.
216 Used to avoid counting one pseudo twice. */
217 static regset_head pseudos_counted;
219 /* First uid used by insns created by reload in this function.
220 Used in find_equiv_reg. */
221 int reload_first_uid;
223 /* Flag set by local-alloc or global-alloc if anything is live in
224 a call-clobbered reg across calls. */
225 int caller_save_needed;
227 /* Set to 1 while reload_as_needed is operating.
228 Required by some machines to handle any generated moves differently. */
229 int reload_in_progress = 0;
231 /* This obstack is used for allocation of rtl during register elimination.
232 The allocated storage can be freed once find_reloads has processed the
233 insn. */
234 static struct obstack reload_obstack;
236 /* Points to the beginning of the reload_obstack. All insn_chain structures
237 are allocated first. */
238 static char *reload_startobj;
240 /* The point after all insn_chain structures. Used to quickly deallocate
241 memory allocated in copy_reloads during calculate_needs_all_insns. */
242 static char *reload_firstobj;
244 /* This points before all local rtl generated by register elimination.
245 Used to quickly free all memory after processing one insn. */
246 static char *reload_insn_firstobj;
248 /* List of insn_chain instructions, one for every insn that reload needs to
249 examine. */
250 struct insn_chain *reload_insn_chain;
252 /* TRUE if we potentially left dead insns in the insn stream and want to
253 run DCE immediately after reload, FALSE otherwise. */
254 static bool need_dce;
256 /* List of all insns needing reloads. */
257 static struct insn_chain *insns_need_reload;
259 /* This structure is used to record information about register eliminations.
260 Each array entry describes one possible way of eliminating a register
261 in favor of another. If there is more than one way of eliminating a
262 particular register, the most preferred should be specified first. */
264 struct elim_table
266 int from; /* Register number to be eliminated. */
267 int to; /* Register number used as replacement. */
268 HOST_WIDE_INT initial_offset; /* Initial difference between values. */
269 int can_eliminate; /* Nonzero if this elimination can be done. */
270 int can_eliminate_previous; /* Value returned by TARGET_CAN_ELIMINATE
271 target hook in previous scan over insns
272 made by reload. */
273 HOST_WIDE_INT offset; /* Current offset between the two regs. */
274 HOST_WIDE_INT previous_offset;/* Offset at end of previous insn. */
275 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
276 rtx from_rtx; /* REG rtx for the register to be eliminated.
277 We cannot simply compare the number since
278 we might then spuriously replace a hard
279 register corresponding to a pseudo
280 assigned to the reg to be eliminated. */
281 rtx to_rtx; /* REG rtx for the replacement. */
284 static struct elim_table *reg_eliminate = 0;
286 /* This is an intermediate structure to initialize the table. It has
287 exactly the members provided by ELIMINABLE_REGS. */
288 static const struct elim_table_1
290 const int from;
291 const int to;
292 } reg_eliminate_1[] =
294 /* If a set of eliminable registers was specified, define the table from it.
295 Otherwise, default to the normal case of the frame pointer being
296 replaced by the stack pointer. */
298 #ifdef ELIMINABLE_REGS
299 ELIMINABLE_REGS;
300 #else
301 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
302 #endif
304 #define NUM_ELIMINABLE_REGS ARRAY_SIZE (reg_eliminate_1)
306 /* Record the number of pending eliminations that have an offset not equal
307 to their initial offset. If nonzero, we use a new copy of each
308 replacement result in any insns encountered. */
309 int num_not_at_initial_offset;
311 /* Count the number of registers that we may be able to eliminate. */
312 static int num_eliminable;
313 /* And the number of registers that are equivalent to a constant that
314 can be eliminated to frame_pointer / arg_pointer + constant. */
315 static int num_eliminable_invariants;
317 /* For each label, we record the offset of each elimination. If we reach
318 a label by more than one path and an offset differs, we cannot do the
319 elimination. This information is indexed by the difference of the
320 number of the label and the first label number. We can't offset the
321 pointer itself as this can cause problems on machines with segmented
322 memory. The first table is an array of flags that records whether we
323 have yet encountered a label and the second table is an array of arrays,
324 one entry in the latter array for each elimination. */
326 static int first_label_num;
327 static char *offsets_known_at;
328 static HOST_WIDE_INT (*offsets_at)[NUM_ELIMINABLE_REGS];
330 VEC(reg_equivs_t,gc) *reg_equivs;
332 /* Stack of addresses where an rtx has been changed. We can undo the
333 changes by popping items off the stack and restoring the original
334 value at each location.
336 We use this simplistic undo capability rather than copy_rtx as copy_rtx
337 will not make a deep copy of a normally sharable rtx, such as
338 (const (plus (symbol_ref) (const_int))). If such an expression appears
339 as R1 in gen_reload_chain_without_interm_reg_p, then a shared
340 rtx expression would be changed. See PR 42431. */
342 typedef rtx *rtx_p;
343 DEF_VEC_P(rtx_p);
344 DEF_VEC_ALLOC_P(rtx_p,heap);
345 static VEC(rtx_p,heap) *substitute_stack;
347 /* Number of labels in the current function. */
349 static int num_labels;
351 static void replace_pseudos_in (rtx *, enum machine_mode, rtx);
352 static void maybe_fix_stack_asms (void);
353 static void copy_reloads (struct insn_chain *);
354 static void calculate_needs_all_insns (int);
355 static int find_reg (struct insn_chain *, int);
356 static void find_reload_regs (struct insn_chain *);
357 static void select_reload_regs (void);
358 static void delete_caller_save_insns (void);
360 static void spill_failure (rtx, enum reg_class);
361 static void count_spilled_pseudo (int, int, int);
362 static void delete_dead_insn (rtx);
363 static void alter_reg (int, int, bool);
364 static void set_label_offsets (rtx, rtx, int);
365 static void check_eliminable_occurrences (rtx);
366 static void elimination_effects (rtx, enum machine_mode);
367 static rtx eliminate_regs_1 (rtx, enum machine_mode, rtx, bool, bool);
368 static int eliminate_regs_in_insn (rtx, int);
369 static void update_eliminable_offsets (void);
370 static void mark_not_eliminable (rtx, const_rtx, void *);
371 static void set_initial_elim_offsets (void);
372 static bool verify_initial_elim_offsets (void);
373 static void set_initial_label_offsets (void);
374 static void set_offsets_for_label (rtx);
375 static void init_eliminable_invariants (rtx, bool);
376 static void init_elim_table (void);
377 static void free_reg_equiv (void);
378 static void update_eliminables (HARD_REG_SET *);
379 static void elimination_costs_in_insn (rtx);
380 static void spill_hard_reg (unsigned int, int);
381 static int finish_spills (int);
382 static void scan_paradoxical_subregs (rtx);
383 static void count_pseudo (int);
384 static void order_regs_for_reload (struct insn_chain *);
385 static void reload_as_needed (int);
386 static void forget_old_reloads_1 (rtx, const_rtx, void *);
387 static void forget_marked_reloads (regset);
388 static int reload_reg_class_lower (const void *, const void *);
389 static void mark_reload_reg_in_use (unsigned int, int, enum reload_type,
390 enum machine_mode);
391 static void clear_reload_reg_in_use (unsigned int, int, enum reload_type,
392 enum machine_mode);
393 static int reload_reg_free_p (unsigned int, int, enum reload_type);
394 static int reload_reg_free_for_value_p (int, int, int, enum reload_type,
395 rtx, rtx, int, int);
396 static int free_for_value_p (int, enum machine_mode, int, enum reload_type,
397 rtx, rtx, int, int);
398 static int allocate_reload_reg (struct insn_chain *, int, int);
399 static int conflicts_with_override (rtx);
400 static void failed_reload (rtx, int);
401 static int set_reload_reg (int, int);
402 static void choose_reload_regs_init (struct insn_chain *, rtx *);
403 static void choose_reload_regs (struct insn_chain *);
404 static void emit_input_reload_insns (struct insn_chain *, struct reload *,
405 rtx, int);
406 static void emit_output_reload_insns (struct insn_chain *, struct reload *,
407 int);
408 static void do_input_reload (struct insn_chain *, struct reload *, int);
409 static void do_output_reload (struct insn_chain *, struct reload *, int);
410 static void emit_reload_insns (struct insn_chain *);
411 static void delete_output_reload (rtx, int, int, rtx);
412 static void delete_address_reloads (rtx, rtx);
413 static void delete_address_reloads_1 (rtx, rtx, rtx);
414 static void inc_for_reload (rtx, rtx, rtx, int);
415 #ifdef AUTO_INC_DEC
416 static void add_auto_inc_notes (rtx, rtx);
417 #endif
418 static void substitute (rtx *, const_rtx, rtx);
419 static bool gen_reload_chain_without_interm_reg_p (int, int);
420 static int reloads_conflict (int, int);
421 static rtx gen_reload (rtx, rtx, int, enum reload_type);
422 static rtx emit_insn_if_valid_for_reload (rtx);
424 /* Initialize the reload pass. This is called at the beginning of compilation
425 and may be called again if the target is reinitialized. */
427 void
428 init_reload (void)
430 int i;
432 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
433 Set spill_indirect_levels to the number of levels such addressing is
434 permitted, zero if it is not permitted at all. */
436 rtx tem
437 = gen_rtx_MEM (Pmode,
438 gen_rtx_PLUS (Pmode,
439 gen_rtx_REG (Pmode,
440 LAST_VIRTUAL_REGISTER + 1),
441 GEN_INT (4)));
442 spill_indirect_levels = 0;
444 while (memory_address_p (QImode, tem))
446 spill_indirect_levels++;
447 tem = gen_rtx_MEM (Pmode, tem);
450 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
452 tem = gen_rtx_MEM (Pmode, gen_rtx_SYMBOL_REF (Pmode, "foo"));
453 indirect_symref_ok = memory_address_p (QImode, tem);
455 /* See if reg+reg is a valid (and offsettable) address. */
457 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
459 tem = gen_rtx_PLUS (Pmode,
460 gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM),
461 gen_rtx_REG (Pmode, i));
463 /* This way, we make sure that reg+reg is an offsettable address. */
464 tem = plus_constant (Pmode, tem, 4);
466 if (memory_address_p (QImode, tem))
468 double_reg_address_ok = 1;
469 break;
473 /* Initialize obstack for our rtl allocation. */
474 gcc_obstack_init (&reload_obstack);
475 reload_startobj = XOBNEWVAR (&reload_obstack, char, 0);
477 INIT_REG_SET (&spilled_pseudos);
478 INIT_REG_SET (&changed_allocation_pseudos);
479 INIT_REG_SET (&pseudos_counted);
482 /* List of insn chains that are currently unused. */
483 static struct insn_chain *unused_insn_chains = 0;
485 /* Allocate an empty insn_chain structure. */
486 struct insn_chain *
487 new_insn_chain (void)
489 struct insn_chain *c;
491 if (unused_insn_chains == 0)
493 c = XOBNEW (&reload_obstack, struct insn_chain);
494 INIT_REG_SET (&c->live_throughout);
495 INIT_REG_SET (&c->dead_or_set);
497 else
499 c = unused_insn_chains;
500 unused_insn_chains = c->next;
502 c->is_caller_save_insn = 0;
503 c->need_operand_change = 0;
504 c->need_reload = 0;
505 c->need_elim = 0;
506 return c;
509 /* Small utility function to set all regs in hard reg set TO which are
510 allocated to pseudos in regset FROM. */
512 void
513 compute_use_by_pseudos (HARD_REG_SET *to, regset from)
515 unsigned int regno;
516 reg_set_iterator rsi;
518 EXECUTE_IF_SET_IN_REG_SET (from, FIRST_PSEUDO_REGISTER, regno, rsi)
520 int r = reg_renumber[regno];
522 if (r < 0)
524 /* reload_combine uses the information from DF_LIVE_IN,
525 which might still contain registers that have not
526 actually been allocated since they have an
527 equivalence. */
528 gcc_assert (ira_conflicts_p || reload_completed);
530 else
531 add_to_hard_reg_set (to, PSEUDO_REGNO_MODE (regno), r);
535 /* Replace all pseudos found in LOC with their corresponding
536 equivalences. */
538 static void
539 replace_pseudos_in (rtx *loc, enum machine_mode mem_mode, rtx usage)
541 rtx x = *loc;
542 enum rtx_code code;
543 const char *fmt;
544 int i, j;
546 if (! x)
547 return;
549 code = GET_CODE (x);
550 if (code == REG)
552 unsigned int regno = REGNO (x);
554 if (regno < FIRST_PSEUDO_REGISTER)
555 return;
557 x = eliminate_regs_1 (x, mem_mode, usage, true, false);
558 if (x != *loc)
560 *loc = x;
561 replace_pseudos_in (loc, mem_mode, usage);
562 return;
565 if (reg_equiv_constant (regno))
566 *loc = reg_equiv_constant (regno);
567 else if (reg_equiv_invariant (regno))
568 *loc = reg_equiv_invariant (regno);
569 else if (reg_equiv_mem (regno))
570 *loc = reg_equiv_mem (regno);
571 else if (reg_equiv_address (regno))
572 *loc = gen_rtx_MEM (GET_MODE (x), reg_equiv_address (regno));
573 else
575 gcc_assert (!REG_P (regno_reg_rtx[regno])
576 || REGNO (regno_reg_rtx[regno]) != regno);
577 *loc = regno_reg_rtx[regno];
580 return;
582 else if (code == MEM)
584 replace_pseudos_in (& XEXP (x, 0), GET_MODE (x), usage);
585 return;
588 /* Process each of our operands recursively. */
589 fmt = GET_RTX_FORMAT (code);
590 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
591 if (*fmt == 'e')
592 replace_pseudos_in (&XEXP (x, i), mem_mode, usage);
593 else if (*fmt == 'E')
594 for (j = 0; j < XVECLEN (x, i); j++)
595 replace_pseudos_in (& XVECEXP (x, i, j), mem_mode, usage);
598 /* Determine if the current function has an exception receiver block
599 that reaches the exit block via non-exceptional edges */
601 static bool
602 has_nonexceptional_receiver (void)
604 edge e;
605 edge_iterator ei;
606 basic_block *tos, *worklist, bb;
608 /* If we're not optimizing, then just err on the safe side. */
609 if (!optimize)
610 return true;
612 /* First determine which blocks can reach exit via normal paths. */
613 tos = worklist = XNEWVEC (basic_block, n_basic_blocks + 1);
615 FOR_EACH_BB (bb)
616 bb->flags &= ~BB_REACHABLE;
618 /* Place the exit block on our worklist. */
619 EXIT_BLOCK_PTR->flags |= BB_REACHABLE;
620 *tos++ = EXIT_BLOCK_PTR;
622 /* Iterate: find everything reachable from what we've already seen. */
623 while (tos != worklist)
625 bb = *--tos;
627 FOR_EACH_EDGE (e, ei, bb->preds)
628 if (!(e->flags & EDGE_ABNORMAL))
630 basic_block src = e->src;
632 if (!(src->flags & BB_REACHABLE))
634 src->flags |= BB_REACHABLE;
635 *tos++ = src;
639 free (worklist);
641 /* Now see if there's a reachable block with an exceptional incoming
642 edge. */
643 FOR_EACH_BB (bb)
644 if (bb->flags & BB_REACHABLE && bb_has_abnormal_pred (bb))
645 return true;
647 /* No exceptional block reached exit unexceptionally. */
648 return false;
651 /* Grow (or allocate) the REG_EQUIVS array from its current size (which may be
652 zero elements) to MAX_REG_NUM elements.
654 Initialize all new fields to NULL and update REG_EQUIVS_SIZE. */
655 void
656 grow_reg_equivs (void)
658 int old_size = VEC_length (reg_equivs_t, reg_equivs);
659 int max_regno = max_reg_num ();
660 int i;
662 VEC_reserve (reg_equivs_t, gc, reg_equivs, max_regno);
663 for (i = old_size; i < max_regno; i++)
665 VEC_quick_insert (reg_equivs_t, reg_equivs, i, 0);
666 memset (VEC_index (reg_equivs_t, reg_equivs, i), 0, sizeof (reg_equivs_t));
672 /* Global variables used by reload and its subroutines. */
674 /* The current basic block while in calculate_elim_costs_all_insns. */
675 static basic_block elim_bb;
677 /* Set during calculate_needs if an insn needs register elimination. */
678 static int something_needs_elimination;
679 /* Set during calculate_needs if an insn needs an operand changed. */
680 static int something_needs_operands_changed;
681 /* Set by alter_regs if we spilled a register to the stack. */
682 static bool something_was_spilled;
684 /* Nonzero means we couldn't get enough spill regs. */
685 static int failure;
687 /* Temporary array of pseudo-register number. */
688 static int *temp_pseudo_reg_arr;
690 /* Main entry point for the reload pass.
692 FIRST is the first insn of the function being compiled.
694 GLOBAL nonzero means we were called from global_alloc
695 and should attempt to reallocate any pseudoregs that we
696 displace from hard regs we will use for reloads.
697 If GLOBAL is zero, we do not have enough information to do that,
698 so any pseudo reg that is spilled must go to the stack.
700 Return value is TRUE if reload likely left dead insns in the
701 stream and a DCE pass should be run to elimiante them. Else the
702 return value is FALSE. */
704 bool
705 reload (rtx first, int global)
707 int i, n;
708 rtx insn;
709 struct elim_table *ep;
710 basic_block bb;
711 bool inserted;
713 /* Make sure even insns with volatile mem refs are recognizable. */
714 init_recog ();
716 failure = 0;
718 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
720 /* Make sure that the last insn in the chain
721 is not something that needs reloading. */
722 emit_note (NOTE_INSN_DELETED);
724 /* Enable find_equiv_reg to distinguish insns made by reload. */
725 reload_first_uid = get_max_uid ();
727 #ifdef SECONDARY_MEMORY_NEEDED
728 /* Initialize the secondary memory table. */
729 clear_secondary_mem ();
730 #endif
732 /* We don't have a stack slot for any spill reg yet. */
733 memset (spill_stack_slot, 0, sizeof spill_stack_slot);
734 memset (spill_stack_slot_width, 0, sizeof spill_stack_slot_width);
736 /* Initialize the save area information for caller-save, in case some
737 are needed. */
738 init_save_areas ();
740 /* Compute which hard registers are now in use
741 as homes for pseudo registers.
742 This is done here rather than (eg) in global_alloc
743 because this point is reached even if not optimizing. */
744 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
745 mark_home_live (i);
747 /* A function that has a nonlocal label that can reach the exit
748 block via non-exceptional paths must save all call-saved
749 registers. */
750 if (cfun->has_nonlocal_label
751 && has_nonexceptional_receiver ())
752 crtl->saves_all_registers = 1;
754 if (crtl->saves_all_registers)
755 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
756 if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
757 df_set_regs_ever_live (i, true);
759 /* Find all the pseudo registers that didn't get hard regs
760 but do have known equivalent constants or memory slots.
761 These include parameters (known equivalent to parameter slots)
762 and cse'd or loop-moved constant memory addresses.
764 Record constant equivalents in reg_equiv_constant
765 so they will be substituted by find_reloads.
766 Record memory equivalents in reg_mem_equiv so they can
767 be substituted eventually by altering the REG-rtx's. */
769 grow_reg_equivs ();
770 reg_old_renumber = XCNEWVEC (short, max_regno);
771 memcpy (reg_old_renumber, reg_renumber, max_regno * sizeof (short));
772 pseudo_forbidden_regs = XNEWVEC (HARD_REG_SET, max_regno);
773 pseudo_previous_regs = XCNEWVEC (HARD_REG_SET, max_regno);
775 CLEAR_HARD_REG_SET (bad_spill_regs_global);
777 init_eliminable_invariants (first, true);
778 init_elim_table ();
780 /* Alter each pseudo-reg rtx to contain its hard reg number. Assign
781 stack slots to the pseudos that lack hard regs or equivalents.
782 Do not touch virtual registers. */
784 temp_pseudo_reg_arr = XNEWVEC (int, max_regno - LAST_VIRTUAL_REGISTER - 1);
785 for (n = 0, i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
786 temp_pseudo_reg_arr[n++] = i;
788 if (ira_conflicts_p)
789 /* Ask IRA to order pseudo-registers for better stack slot
790 sharing. */
791 ira_sort_regnos_for_alter_reg (temp_pseudo_reg_arr, n, reg_max_ref_width);
793 for (i = 0; i < n; i++)
794 alter_reg (temp_pseudo_reg_arr[i], -1, false);
796 /* If we have some registers we think can be eliminated, scan all insns to
797 see if there is an insn that sets one of these registers to something
798 other than itself plus a constant. If so, the register cannot be
799 eliminated. Doing this scan here eliminates an extra pass through the
800 main reload loop in the most common case where register elimination
801 cannot be done. */
802 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
803 if (INSN_P (insn))
804 note_stores (PATTERN (insn), mark_not_eliminable, NULL);
806 maybe_fix_stack_asms ();
808 insns_need_reload = 0;
809 something_needs_elimination = 0;
811 /* Initialize to -1, which means take the first spill register. */
812 last_spill_reg = -1;
814 /* Spill any hard regs that we know we can't eliminate. */
815 CLEAR_HARD_REG_SET (used_spill_regs);
816 /* There can be multiple ways to eliminate a register;
817 they should be listed adjacently.
818 Elimination for any register fails only if all possible ways fail. */
819 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; )
821 int from = ep->from;
822 int can_eliminate = 0;
825 can_eliminate |= ep->can_eliminate;
826 ep++;
828 while (ep < &reg_eliminate[NUM_ELIMINABLE_REGS] && ep->from == from);
829 if (! can_eliminate)
830 spill_hard_reg (from, 1);
833 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
834 if (frame_pointer_needed)
835 spill_hard_reg (HARD_FRAME_POINTER_REGNUM, 1);
836 #endif
837 finish_spills (global);
839 /* From now on, we may need to generate moves differently. We may also
840 allow modifications of insns which cause them to not be recognized.
841 Any such modifications will be cleaned up during reload itself. */
842 reload_in_progress = 1;
844 /* This loop scans the entire function each go-round
845 and repeats until one repetition spills no additional hard regs. */
846 for (;;)
848 int something_changed;
849 int did_spill;
850 HOST_WIDE_INT starting_frame_size;
852 starting_frame_size = get_frame_size ();
853 something_was_spilled = false;
855 set_initial_elim_offsets ();
856 set_initial_label_offsets ();
858 /* For each pseudo register that has an equivalent location defined,
859 try to eliminate any eliminable registers (such as the frame pointer)
860 assuming initial offsets for the replacement register, which
861 is the normal case.
863 If the resulting location is directly addressable, substitute
864 the MEM we just got directly for the old REG.
866 If it is not addressable but is a constant or the sum of a hard reg
867 and constant, it is probably not addressable because the constant is
868 out of range, in that case record the address; we will generate
869 hairy code to compute the address in a register each time it is
870 needed. Similarly if it is a hard register, but one that is not
871 valid as an address register.
873 If the location is not addressable, but does not have one of the
874 above forms, assign a stack slot. We have to do this to avoid the
875 potential of producing lots of reloads if, e.g., a location involves
876 a pseudo that didn't get a hard register and has an equivalent memory
877 location that also involves a pseudo that didn't get a hard register.
879 Perhaps at some point we will improve reload_when_needed handling
880 so this problem goes away. But that's very hairy. */
882 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
883 if (reg_renumber[i] < 0 && reg_equiv_memory_loc (i))
885 rtx x = eliminate_regs (reg_equiv_memory_loc (i), VOIDmode,
886 NULL_RTX);
888 if (strict_memory_address_addr_space_p
889 (GET_MODE (regno_reg_rtx[i]), XEXP (x, 0),
890 MEM_ADDR_SPACE (x)))
891 reg_equiv_mem (i) = x, reg_equiv_address (i) = 0;
892 else if (CONSTANT_P (XEXP (x, 0))
893 || (REG_P (XEXP (x, 0))
894 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
895 || (GET_CODE (XEXP (x, 0)) == PLUS
896 && REG_P (XEXP (XEXP (x, 0), 0))
897 && (REGNO (XEXP (XEXP (x, 0), 0))
898 < FIRST_PSEUDO_REGISTER)
899 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
900 reg_equiv_address (i) = XEXP (x, 0), reg_equiv_mem (i) = 0;
901 else
903 /* Make a new stack slot. Then indicate that something
904 changed so we go back and recompute offsets for
905 eliminable registers because the allocation of memory
906 below might change some offset. reg_equiv_{mem,address}
907 will be set up for this pseudo on the next pass around
908 the loop. */
909 reg_equiv_memory_loc (i) = 0;
910 reg_equiv_init (i) = 0;
911 alter_reg (i, -1, true);
915 if (caller_save_needed)
916 setup_save_areas ();
918 /* If we allocated another stack slot, redo elimination bookkeeping. */
919 if (something_was_spilled || starting_frame_size != get_frame_size ())
920 continue;
921 if (starting_frame_size && crtl->stack_alignment_needed)
923 /* If we have a stack frame, we must align it now. The
924 stack size may be a part of the offset computation for
925 register elimination. So if this changes the stack size,
926 then repeat the elimination bookkeeping. We don't
927 realign when there is no stack, as that will cause a
928 stack frame when none is needed should
929 STARTING_FRAME_OFFSET not be already aligned to
930 STACK_BOUNDARY. */
931 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
932 if (starting_frame_size != get_frame_size ())
933 continue;
936 if (caller_save_needed)
938 save_call_clobbered_regs ();
939 /* That might have allocated new insn_chain structures. */
940 reload_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
943 calculate_needs_all_insns (global);
945 if (! ira_conflicts_p)
946 /* Don't do it for IRA. We need this info because we don't
947 change live_throughout and dead_or_set for chains when IRA
948 is used. */
949 CLEAR_REG_SET (&spilled_pseudos);
951 did_spill = 0;
953 something_changed = 0;
955 /* If we allocated any new memory locations, make another pass
956 since it might have changed elimination offsets. */
957 if (something_was_spilled || starting_frame_size != get_frame_size ())
958 something_changed = 1;
960 /* Even if the frame size remained the same, we might still have
961 changed elimination offsets, e.g. if find_reloads called
962 force_const_mem requiring the back end to allocate a constant
963 pool base register that needs to be saved on the stack. */
964 else if (!verify_initial_elim_offsets ())
965 something_changed = 1;
968 HARD_REG_SET to_spill;
969 CLEAR_HARD_REG_SET (to_spill);
970 update_eliminables (&to_spill);
971 AND_COMPL_HARD_REG_SET (used_spill_regs, to_spill);
973 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
974 if (TEST_HARD_REG_BIT (to_spill, i))
976 spill_hard_reg (i, 1);
977 did_spill = 1;
979 /* Regardless of the state of spills, if we previously had
980 a register that we thought we could eliminate, but now can
981 not eliminate, we must run another pass.
983 Consider pseudos which have an entry in reg_equiv_* which
984 reference an eliminable register. We must make another pass
985 to update reg_equiv_* so that we do not substitute in the
986 old value from when we thought the elimination could be
987 performed. */
988 something_changed = 1;
992 select_reload_regs ();
993 if (failure)
994 goto failed;
996 if (insns_need_reload != 0 || did_spill)
997 something_changed |= finish_spills (global);
999 if (! something_changed)
1000 break;
1002 if (caller_save_needed)
1003 delete_caller_save_insns ();
1005 obstack_free (&reload_obstack, reload_firstobj);
1008 /* If global-alloc was run, notify it of any register eliminations we have
1009 done. */
1010 if (global)
1011 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1012 if (ep->can_eliminate)
1013 mark_elimination (ep->from, ep->to);
1015 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1016 If that insn didn't set the register (i.e., it copied the register to
1017 memory), just delete that insn instead of the equivalencing insn plus
1018 anything now dead. If we call delete_dead_insn on that insn, we may
1019 delete the insn that actually sets the register if the register dies
1020 there and that is incorrect. */
1022 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1024 if (reg_renumber[i] < 0 && reg_equiv_init (i) != 0)
1026 rtx list;
1027 for (list = reg_equiv_init (i); list; list = XEXP (list, 1))
1029 rtx equiv_insn = XEXP (list, 0);
1031 /* If we already deleted the insn or if it may trap, we can't
1032 delete it. The latter case shouldn't happen, but can
1033 if an insn has a variable address, gets a REG_EH_REGION
1034 note added to it, and then gets converted into a load
1035 from a constant address. */
1036 if (NOTE_P (equiv_insn)
1037 || can_throw_internal (equiv_insn))
1039 else if (reg_set_p (regno_reg_rtx[i], PATTERN (equiv_insn)))
1040 delete_dead_insn (equiv_insn);
1041 else
1042 SET_INSN_DELETED (equiv_insn);
1047 /* Use the reload registers where necessary
1048 by generating move instructions to move the must-be-register
1049 values into or out of the reload registers. */
1051 if (insns_need_reload != 0 || something_needs_elimination
1052 || something_needs_operands_changed)
1054 HOST_WIDE_INT old_frame_size = get_frame_size ();
1056 reload_as_needed (global);
1058 gcc_assert (old_frame_size == get_frame_size ());
1060 gcc_assert (verify_initial_elim_offsets ());
1063 /* If we were able to eliminate the frame pointer, show that it is no
1064 longer live at the start of any basic block. If it ls live by
1065 virtue of being in a pseudo, that pseudo will be marked live
1066 and hence the frame pointer will be known to be live via that
1067 pseudo. */
1069 if (! frame_pointer_needed)
1070 FOR_EACH_BB (bb)
1071 bitmap_clear_bit (df_get_live_in (bb), HARD_FRAME_POINTER_REGNUM);
1073 /* Come here (with failure set nonzero) if we can't get enough spill
1074 regs. */
1075 failed:
1077 CLEAR_REG_SET (&changed_allocation_pseudos);
1078 CLEAR_REG_SET (&spilled_pseudos);
1079 reload_in_progress = 0;
1081 /* Now eliminate all pseudo regs by modifying them into
1082 their equivalent memory references.
1083 The REG-rtx's for the pseudos are modified in place,
1084 so all insns that used to refer to them now refer to memory.
1086 For a reg that has a reg_equiv_address, all those insns
1087 were changed by reloading so that no insns refer to it any longer;
1088 but the DECL_RTL of a variable decl may refer to it,
1089 and if so this causes the debugging info to mention the variable. */
1091 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1093 rtx addr = 0;
1095 if (reg_equiv_mem (i))
1096 addr = XEXP (reg_equiv_mem (i), 0);
1098 if (reg_equiv_address (i))
1099 addr = reg_equiv_address (i);
1101 if (addr)
1103 if (reg_renumber[i] < 0)
1105 rtx reg = regno_reg_rtx[i];
1107 REG_USERVAR_P (reg) = 0;
1108 PUT_CODE (reg, MEM);
1109 XEXP (reg, 0) = addr;
1110 if (reg_equiv_memory_loc (i))
1111 MEM_COPY_ATTRIBUTES (reg, reg_equiv_memory_loc (i));
1112 else
1113 MEM_ATTRS (reg) = 0;
1114 MEM_NOTRAP_P (reg) = 1;
1116 else if (reg_equiv_mem (i))
1117 XEXP (reg_equiv_mem (i), 0) = addr;
1120 /* We don't want complex addressing modes in debug insns
1121 if simpler ones will do, so delegitimize equivalences
1122 in debug insns. */
1123 if (MAY_HAVE_DEBUG_INSNS && reg_renumber[i] < 0)
1125 rtx reg = regno_reg_rtx[i];
1126 rtx equiv = 0;
1127 df_ref use, next;
1129 if (reg_equiv_constant (i))
1130 equiv = reg_equiv_constant (i);
1131 else if (reg_equiv_invariant (i))
1132 equiv = reg_equiv_invariant (i);
1133 else if (reg && MEM_P (reg))
1134 equiv = targetm.delegitimize_address (reg);
1135 else if (reg && REG_P (reg) && (int)REGNO (reg) != i)
1136 equiv = reg;
1138 if (equiv == reg)
1139 continue;
1141 for (use = DF_REG_USE_CHAIN (i); use; use = next)
1143 insn = DF_REF_INSN (use);
1145 /* Make sure the next ref is for a different instruction,
1146 so that we're not affected by the rescan. */
1147 next = DF_REF_NEXT_REG (use);
1148 while (next && DF_REF_INSN (next) == insn)
1149 next = DF_REF_NEXT_REG (next);
1151 if (DEBUG_INSN_P (insn))
1153 if (!equiv)
1155 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
1156 df_insn_rescan_debug_internal (insn);
1158 else
1159 INSN_VAR_LOCATION_LOC (insn)
1160 = simplify_replace_rtx (INSN_VAR_LOCATION_LOC (insn),
1161 reg, equiv);
1167 /* We must set reload_completed now since the cleanup_subreg_operands call
1168 below will re-recognize each insn and reload may have generated insns
1169 which are only valid during and after reload. */
1170 reload_completed = 1;
1172 /* Make a pass over all the insns and delete all USEs which we inserted
1173 only to tag a REG_EQUAL note on them. Remove all REG_DEAD and REG_UNUSED
1174 notes. Delete all CLOBBER insns, except those that refer to the return
1175 value and the special mem:BLK CLOBBERs added to prevent the scheduler
1176 from misarranging variable-array code, and simplify (subreg (reg))
1177 operands. Strip and regenerate REG_INC notes that may have been moved
1178 around. */
1180 for (insn = first; insn; insn = NEXT_INSN (insn))
1181 if (INSN_P (insn))
1183 rtx *pnote;
1185 if (CALL_P (insn))
1186 replace_pseudos_in (& CALL_INSN_FUNCTION_USAGE (insn),
1187 VOIDmode, CALL_INSN_FUNCTION_USAGE (insn));
1189 if ((GET_CODE (PATTERN (insn)) == USE
1190 /* We mark with QImode USEs introduced by reload itself. */
1191 && (GET_MODE (insn) == QImode
1192 || find_reg_note (insn, REG_EQUAL, NULL_RTX)))
1193 || (GET_CODE (PATTERN (insn)) == CLOBBER
1194 && (!MEM_P (XEXP (PATTERN (insn), 0))
1195 || GET_MODE (XEXP (PATTERN (insn), 0)) != BLKmode
1196 || (GET_CODE (XEXP (XEXP (PATTERN (insn), 0), 0)) != SCRATCH
1197 && XEXP (XEXP (PATTERN (insn), 0), 0)
1198 != stack_pointer_rtx))
1199 && (!REG_P (XEXP (PATTERN (insn), 0))
1200 || ! REG_FUNCTION_VALUE_P (XEXP (PATTERN (insn), 0)))))
1202 delete_insn (insn);
1203 continue;
1206 /* Some CLOBBERs may survive until here and still reference unassigned
1207 pseudos with const equivalent, which may in turn cause ICE in later
1208 passes if the reference remains in place. */
1209 if (GET_CODE (PATTERN (insn)) == CLOBBER)
1210 replace_pseudos_in (& XEXP (PATTERN (insn), 0),
1211 VOIDmode, PATTERN (insn));
1213 /* Discard obvious no-ops, even without -O. This optimization
1214 is fast and doesn't interfere with debugging. */
1215 if (NONJUMP_INSN_P (insn)
1216 && GET_CODE (PATTERN (insn)) == SET
1217 && REG_P (SET_SRC (PATTERN (insn)))
1218 && REG_P (SET_DEST (PATTERN (insn)))
1219 && (REGNO (SET_SRC (PATTERN (insn)))
1220 == REGNO (SET_DEST (PATTERN (insn)))))
1222 delete_insn (insn);
1223 continue;
1226 pnote = &REG_NOTES (insn);
1227 while (*pnote != 0)
1229 if (REG_NOTE_KIND (*pnote) == REG_DEAD
1230 || REG_NOTE_KIND (*pnote) == REG_UNUSED
1231 || REG_NOTE_KIND (*pnote) == REG_INC)
1232 *pnote = XEXP (*pnote, 1);
1233 else
1234 pnote = &XEXP (*pnote, 1);
1237 #ifdef AUTO_INC_DEC
1238 add_auto_inc_notes (insn, PATTERN (insn));
1239 #endif
1241 /* Simplify (subreg (reg)) if it appears as an operand. */
1242 cleanup_subreg_operands (insn);
1244 /* Clean up invalid ASMs so that they don't confuse later passes.
1245 See PR 21299. */
1246 if (asm_noperands (PATTERN (insn)) >= 0)
1248 extract_insn (insn);
1249 if (!constrain_operands (1))
1251 error_for_asm (insn,
1252 "%<asm%> operand has impossible constraints");
1253 delete_insn (insn);
1254 continue;
1259 /* If we are doing generic stack checking, give a warning if this
1260 function's frame size is larger than we expect. */
1261 if (flag_stack_check == GENERIC_STACK_CHECK)
1263 HOST_WIDE_INT size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
1264 static int verbose_warned = 0;
1266 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1267 if (df_regs_ever_live_p (i) && ! fixed_regs[i] && call_used_regs[i])
1268 size += UNITS_PER_WORD;
1270 if (size > STACK_CHECK_MAX_FRAME_SIZE)
1272 warning (0, "frame size too large for reliable stack checking");
1273 if (! verbose_warned)
1275 warning (0, "try reducing the number of local variables");
1276 verbose_warned = 1;
1281 free (temp_pseudo_reg_arr);
1283 /* Indicate that we no longer have known memory locations or constants. */
1284 free_reg_equiv ();
1286 free (reg_max_ref_width);
1287 free (reg_old_renumber);
1288 free (pseudo_previous_regs);
1289 free (pseudo_forbidden_regs);
1291 CLEAR_HARD_REG_SET (used_spill_regs);
1292 for (i = 0; i < n_spills; i++)
1293 SET_HARD_REG_BIT (used_spill_regs, spill_regs[i]);
1295 /* Free all the insn_chain structures at once. */
1296 obstack_free (&reload_obstack, reload_startobj);
1297 unused_insn_chains = 0;
1299 inserted = fixup_abnormal_edges ();
1301 /* We've possibly turned single trapping insn into multiple ones. */
1302 if (cfun->can_throw_non_call_exceptions)
1304 sbitmap blocks;
1305 blocks = sbitmap_alloc (last_basic_block);
1306 sbitmap_ones (blocks);
1307 find_many_sub_basic_blocks (blocks);
1308 sbitmap_free (blocks);
1311 if (inserted)
1312 commit_edge_insertions ();
1314 /* Replacing pseudos with their memory equivalents might have
1315 created shared rtx. Subsequent passes would get confused
1316 by this, so unshare everything here. */
1317 unshare_all_rtl_again (first);
1319 #ifdef STACK_BOUNDARY
1320 /* init_emit has set the alignment of the hard frame pointer
1321 to STACK_BOUNDARY. It is very likely no longer valid if
1322 the hard frame pointer was used for register allocation. */
1323 if (!frame_pointer_needed)
1324 REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM) = BITS_PER_UNIT;
1325 #endif
1327 VEC_free (rtx_p, heap, substitute_stack);
1329 gcc_assert (bitmap_empty_p (&spilled_pseudos));
1331 reload_completed = !failure;
1333 return need_dce;
1336 /* Yet another special case. Unfortunately, reg-stack forces people to
1337 write incorrect clobbers in asm statements. These clobbers must not
1338 cause the register to appear in bad_spill_regs, otherwise we'll call
1339 fatal_insn later. We clear the corresponding regnos in the live
1340 register sets to avoid this.
1341 The whole thing is rather sick, I'm afraid. */
1343 static void
1344 maybe_fix_stack_asms (void)
1346 #ifdef STACK_REGS
1347 const char *constraints[MAX_RECOG_OPERANDS];
1348 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
1349 struct insn_chain *chain;
1351 for (chain = reload_insn_chain; chain != 0; chain = chain->next)
1353 int i, noperands;
1354 HARD_REG_SET clobbered, allowed;
1355 rtx pat;
1357 if (! INSN_P (chain->insn)
1358 || (noperands = asm_noperands (PATTERN (chain->insn))) < 0)
1359 continue;
1360 pat = PATTERN (chain->insn);
1361 if (GET_CODE (pat) != PARALLEL)
1362 continue;
1364 CLEAR_HARD_REG_SET (clobbered);
1365 CLEAR_HARD_REG_SET (allowed);
1367 /* First, make a mask of all stack regs that are clobbered. */
1368 for (i = 0; i < XVECLEN (pat, 0); i++)
1370 rtx t = XVECEXP (pat, 0, i);
1371 if (GET_CODE (t) == CLOBBER && STACK_REG_P (XEXP (t, 0)))
1372 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
1375 /* Get the operand values and constraints out of the insn. */
1376 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
1377 constraints, operand_mode, NULL);
1379 /* For every operand, see what registers are allowed. */
1380 for (i = 0; i < noperands; i++)
1382 const char *p = constraints[i];
1383 /* For every alternative, we compute the class of registers allowed
1384 for reloading in CLS, and merge its contents into the reg set
1385 ALLOWED. */
1386 int cls = (int) NO_REGS;
1388 for (;;)
1390 char c = *p;
1392 if (c == '\0' || c == ',' || c == '#')
1394 /* End of one alternative - mark the regs in the current
1395 class, and reset the class. */
1396 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
1397 cls = NO_REGS;
1398 p++;
1399 if (c == '#')
1400 do {
1401 c = *p++;
1402 } while (c != '\0' && c != ',');
1403 if (c == '\0')
1404 break;
1405 continue;
1408 switch (c)
1410 case '=': case '+': case '*': case '%': case '?': case '!':
1411 case '0': case '1': case '2': case '3': case '4': case '<':
1412 case '>': case 'V': case 'o': case '&': case 'E': case 'F':
1413 case 's': case 'i': case 'n': case 'X': case 'I': case 'J':
1414 case 'K': case 'L': case 'M': case 'N': case 'O': case 'P':
1415 case TARGET_MEM_CONSTRAINT:
1416 break;
1418 case 'p':
1419 cls = (int) reg_class_subunion[cls]
1420 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1421 ADDRESS, SCRATCH)];
1422 break;
1424 case 'g':
1425 case 'r':
1426 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
1427 break;
1429 default:
1430 if (EXTRA_ADDRESS_CONSTRAINT (c, p))
1431 cls = (int) reg_class_subunion[cls]
1432 [(int) base_reg_class (VOIDmode, ADDR_SPACE_GENERIC,
1433 ADDRESS, SCRATCH)];
1434 else
1435 cls = (int) reg_class_subunion[cls]
1436 [(int) REG_CLASS_FROM_CONSTRAINT (c, p)];
1438 p += CONSTRAINT_LEN (c, p);
1441 /* Those of the registers which are clobbered, but allowed by the
1442 constraints, must be usable as reload registers. So clear them
1443 out of the life information. */
1444 AND_HARD_REG_SET (allowed, clobbered);
1445 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1446 if (TEST_HARD_REG_BIT (allowed, i))
1448 CLEAR_REGNO_REG_SET (&chain->live_throughout, i);
1449 CLEAR_REGNO_REG_SET (&chain->dead_or_set, i);
1453 #endif
1456 /* Copy the global variables n_reloads and rld into the corresponding elts
1457 of CHAIN. */
1458 static void
1459 copy_reloads (struct insn_chain *chain)
1461 chain->n_reloads = n_reloads;
1462 chain->rld = XOBNEWVEC (&reload_obstack, struct reload, n_reloads);
1463 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
1464 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1467 /* Walk the chain of insns, and determine for each whether it needs reloads
1468 and/or eliminations. Build the corresponding insns_need_reload list, and
1469 set something_needs_elimination as appropriate. */
1470 static void
1471 calculate_needs_all_insns (int global)
1473 struct insn_chain **pprev_reload = &insns_need_reload;
1474 struct insn_chain *chain, *next = 0;
1476 something_needs_elimination = 0;
1478 reload_insn_firstobj = XOBNEWVAR (&reload_obstack, char, 0);
1479 for (chain = reload_insn_chain; chain != 0; chain = next)
1481 rtx insn = chain->insn;
1483 next = chain->next;
1485 /* Clear out the shortcuts. */
1486 chain->n_reloads = 0;
1487 chain->need_elim = 0;
1488 chain->need_reload = 0;
1489 chain->need_operand_change = 0;
1491 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1492 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1493 what effects this has on the known offsets at labels. */
1495 if (LABEL_P (insn) || JUMP_P (insn)
1496 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1497 set_label_offsets (insn, insn, 0);
1499 if (INSN_P (insn))
1501 rtx old_body = PATTERN (insn);
1502 int old_code = INSN_CODE (insn);
1503 rtx old_notes = REG_NOTES (insn);
1504 int did_elimination = 0;
1505 int operands_changed = 0;
1506 rtx set = single_set (insn);
1508 /* Skip insns that only set an equivalence. */
1509 if (set && REG_P (SET_DEST (set))
1510 && reg_renumber[REGNO (SET_DEST (set))] < 0
1511 && (reg_equiv_constant (REGNO (SET_DEST (set)))
1512 || (reg_equiv_invariant (REGNO (SET_DEST (set)))))
1513 && reg_equiv_init (REGNO (SET_DEST (set))))
1514 continue;
1516 /* If needed, eliminate any eliminable registers. */
1517 if (num_eliminable || num_eliminable_invariants)
1518 did_elimination = eliminate_regs_in_insn (insn, 0);
1520 /* Analyze the instruction. */
1521 operands_changed = find_reloads (insn, 0, spill_indirect_levels,
1522 global, spill_reg_order);
1524 /* If a no-op set needs more than one reload, this is likely
1525 to be something that needs input address reloads. We
1526 can't get rid of this cleanly later, and it is of no use
1527 anyway, so discard it now.
1528 We only do this when expensive_optimizations is enabled,
1529 since this complements reload inheritance / output
1530 reload deletion, and it can make debugging harder. */
1531 if (flag_expensive_optimizations && n_reloads > 1)
1533 rtx set = single_set (insn);
1534 if (set
1536 ((SET_SRC (set) == SET_DEST (set)
1537 && REG_P (SET_SRC (set))
1538 && REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1539 || (REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))
1540 && reg_renumber[REGNO (SET_SRC (set))] < 0
1541 && reg_renumber[REGNO (SET_DEST (set))] < 0
1542 && reg_equiv_memory_loc (REGNO (SET_SRC (set))) != NULL
1543 && reg_equiv_memory_loc (REGNO (SET_DEST (set))) != NULL
1544 && rtx_equal_p (reg_equiv_memory_loc (REGNO (SET_SRC (set))),
1545 reg_equiv_memory_loc (REGNO (SET_DEST (set)))))))
1547 if (ira_conflicts_p)
1548 /* Inform IRA about the insn deletion. */
1549 ira_mark_memory_move_deletion (REGNO (SET_DEST (set)),
1550 REGNO (SET_SRC (set)));
1551 delete_insn (insn);
1552 /* Delete it from the reload chain. */
1553 if (chain->prev)
1554 chain->prev->next = next;
1555 else
1556 reload_insn_chain = next;
1557 if (next)
1558 next->prev = chain->prev;
1559 chain->next = unused_insn_chains;
1560 unused_insn_chains = chain;
1561 continue;
1564 if (num_eliminable)
1565 update_eliminable_offsets ();
1567 /* Remember for later shortcuts which insns had any reloads or
1568 register eliminations. */
1569 chain->need_elim = did_elimination;
1570 chain->need_reload = n_reloads > 0;
1571 chain->need_operand_change = operands_changed;
1573 /* Discard any register replacements done. */
1574 if (did_elimination)
1576 obstack_free (&reload_obstack, reload_insn_firstobj);
1577 PATTERN (insn) = old_body;
1578 INSN_CODE (insn) = old_code;
1579 REG_NOTES (insn) = old_notes;
1580 something_needs_elimination = 1;
1583 something_needs_operands_changed |= operands_changed;
1585 if (n_reloads != 0)
1587 copy_reloads (chain);
1588 *pprev_reload = chain;
1589 pprev_reload = &chain->next_need_reload;
1593 *pprev_reload = 0;
1596 /* This function is called from the register allocator to set up estimates
1597 for the cost of eliminating pseudos which have REG_EQUIV equivalences to
1598 an invariant. The structure is similar to calculate_needs_all_insns. */
1600 void
1601 calculate_elim_costs_all_insns (void)
1603 int *reg_equiv_init_cost;
1604 basic_block bb;
1605 int i;
1607 reg_equiv_init_cost = XCNEWVEC (int, max_regno);
1608 init_elim_table ();
1609 init_eliminable_invariants (get_insns (), false);
1611 set_initial_elim_offsets ();
1612 set_initial_label_offsets ();
1614 FOR_EACH_BB (bb)
1616 rtx insn;
1617 elim_bb = bb;
1619 FOR_BB_INSNS (bb, insn)
1621 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which might
1622 include REG_LABEL_OPERAND and REG_LABEL_TARGET), we need to see
1623 what effects this has on the known offsets at labels. */
1625 if (LABEL_P (insn) || JUMP_P (insn)
1626 || (INSN_P (insn) && REG_NOTES (insn) != 0))
1627 set_label_offsets (insn, insn, 0);
1629 if (INSN_P (insn))
1631 rtx set = single_set (insn);
1633 /* Skip insns that only set an equivalence. */
1634 if (set && REG_P (SET_DEST (set))
1635 && reg_renumber[REGNO (SET_DEST (set))] < 0
1636 && (reg_equiv_constant (REGNO (SET_DEST (set)))
1637 || reg_equiv_invariant (REGNO (SET_DEST (set)))))
1639 unsigned regno = REGNO (SET_DEST (set));
1640 rtx init = reg_equiv_init (regno);
1641 if (init)
1643 rtx t = eliminate_regs_1 (SET_SRC (set), VOIDmode, insn,
1644 false, true);
1645 int cost = set_src_cost (t, optimize_bb_for_speed_p (bb));
1646 int freq = REG_FREQ_FROM_BB (bb);
1648 reg_equiv_init_cost[regno] = cost * freq;
1649 continue;
1652 /* If needed, eliminate any eliminable registers. */
1653 if (num_eliminable || num_eliminable_invariants)
1654 elimination_costs_in_insn (insn);
1656 if (num_eliminable)
1657 update_eliminable_offsets ();
1661 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1663 if (reg_equiv_invariant (i))
1665 if (reg_equiv_init (i))
1667 int cost = reg_equiv_init_cost[i];
1668 if (dump_file)
1669 fprintf (dump_file,
1670 "Reg %d has equivalence, initial gains %d\n", i, cost);
1671 if (cost != 0)
1672 ira_adjust_equiv_reg_cost (i, cost);
1674 else
1676 if (dump_file)
1677 fprintf (dump_file,
1678 "Reg %d had equivalence, but can't be eliminated\n",
1680 ira_adjust_equiv_reg_cost (i, 0);
1685 free (reg_equiv_init_cost);
1686 free (offsets_known_at);
1687 free (offsets_at);
1688 offsets_at = NULL;
1689 offsets_known_at = NULL;
1692 /* Comparison function for qsort to decide which of two reloads
1693 should be handled first. *P1 and *P2 are the reload numbers. */
1695 static int
1696 reload_reg_class_lower (const void *r1p, const void *r2p)
1698 int r1 = *(const short *) r1p, r2 = *(const short *) r2p;
1699 int t;
1701 /* Consider required reloads before optional ones. */
1702 t = rld[r1].optional - rld[r2].optional;
1703 if (t != 0)
1704 return t;
1706 /* Count all solitary classes before non-solitary ones. */
1707 t = ((reg_class_size[(int) rld[r2].rclass] == 1)
1708 - (reg_class_size[(int) rld[r1].rclass] == 1));
1709 if (t != 0)
1710 return t;
1712 /* Aside from solitaires, consider all multi-reg groups first. */
1713 t = rld[r2].nregs - rld[r1].nregs;
1714 if (t != 0)
1715 return t;
1717 /* Consider reloads in order of increasing reg-class number. */
1718 t = (int) rld[r1].rclass - (int) rld[r2].rclass;
1719 if (t != 0)
1720 return t;
1722 /* If reloads are equally urgent, sort by reload number,
1723 so that the results of qsort leave nothing to chance. */
1724 return r1 - r2;
1727 /* The cost of spilling each hard reg. */
1728 static int spill_cost[FIRST_PSEUDO_REGISTER];
1730 /* When spilling multiple hard registers, we use SPILL_COST for the first
1731 spilled hard reg and SPILL_ADD_COST for subsequent regs. SPILL_ADD_COST
1732 only the first hard reg for a multi-reg pseudo. */
1733 static int spill_add_cost[FIRST_PSEUDO_REGISTER];
1735 /* Map of hard regno to pseudo regno currently occupying the hard
1736 reg. */
1737 static int hard_regno_to_pseudo_regno[FIRST_PSEUDO_REGISTER];
1739 /* Update the spill cost arrays, considering that pseudo REG is live. */
1741 static void
1742 count_pseudo (int reg)
1744 int freq = REG_FREQ (reg);
1745 int r = reg_renumber[reg];
1746 int nregs;
1748 /* Ignore spilled pseudo-registers which can be here only if IRA is used. */
1749 if (ira_conflicts_p && r < 0)
1750 return;
1752 if (REGNO_REG_SET_P (&pseudos_counted, reg)
1753 || REGNO_REG_SET_P (&spilled_pseudos, reg))
1754 return;
1756 SET_REGNO_REG_SET (&pseudos_counted, reg);
1758 gcc_assert (r >= 0);
1760 spill_add_cost[r] += freq;
1761 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1762 while (nregs-- > 0)
1764 hard_regno_to_pseudo_regno[r + nregs] = reg;
1765 spill_cost[r + nregs] += freq;
1769 /* Calculate the SPILL_COST and SPILL_ADD_COST arrays and determine the
1770 contents of BAD_SPILL_REGS for the insn described by CHAIN. */
1772 static void
1773 order_regs_for_reload (struct insn_chain *chain)
1775 unsigned i;
1776 HARD_REG_SET used_by_pseudos;
1777 HARD_REG_SET used_by_pseudos2;
1778 reg_set_iterator rsi;
1780 COPY_HARD_REG_SET (bad_spill_regs, fixed_reg_set);
1782 memset (spill_cost, 0, sizeof spill_cost);
1783 memset (spill_add_cost, 0, sizeof spill_add_cost);
1784 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1785 hard_regno_to_pseudo_regno[i] = -1;
1787 /* Count number of uses of each hard reg by pseudo regs allocated to it
1788 and then order them by decreasing use. First exclude hard registers
1789 that are live in or across this insn. */
1791 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
1792 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
1793 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos);
1794 IOR_HARD_REG_SET (bad_spill_regs, used_by_pseudos2);
1796 /* Now find out which pseudos are allocated to it, and update
1797 hard_reg_n_uses. */
1798 CLEAR_REG_SET (&pseudos_counted);
1800 EXECUTE_IF_SET_IN_REG_SET
1801 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
1803 count_pseudo (i);
1805 EXECUTE_IF_SET_IN_REG_SET
1806 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
1808 count_pseudo (i);
1810 CLEAR_REG_SET (&pseudos_counted);
1813 /* Vector of reload-numbers showing the order in which the reloads should
1814 be processed. */
1815 static short reload_order[MAX_RELOADS];
1817 /* This is used to keep track of the spill regs used in one insn. */
1818 static HARD_REG_SET used_spill_regs_local;
1820 /* We decided to spill hard register SPILLED, which has a size of
1821 SPILLED_NREGS. Determine how pseudo REG, which is live during the insn,
1822 is affected. We will add it to SPILLED_PSEUDOS if necessary, and we will
1823 update SPILL_COST/SPILL_ADD_COST. */
1825 static void
1826 count_spilled_pseudo (int spilled, int spilled_nregs, int reg)
1828 int freq = REG_FREQ (reg);
1829 int r = reg_renumber[reg];
1830 int nregs;
1832 /* Ignore spilled pseudo-registers which can be here only if IRA is used. */
1833 if (ira_conflicts_p && r < 0)
1834 return;
1836 gcc_assert (r >= 0);
1838 nregs = hard_regno_nregs[r][PSEUDO_REGNO_MODE (reg)];
1840 if (REGNO_REG_SET_P (&spilled_pseudos, reg)
1841 || spilled + spilled_nregs <= r || r + nregs <= spilled)
1842 return;
1844 SET_REGNO_REG_SET (&spilled_pseudos, reg);
1846 spill_add_cost[r] -= freq;
1847 while (nregs-- > 0)
1849 hard_regno_to_pseudo_regno[r + nregs] = -1;
1850 spill_cost[r + nregs] -= freq;
1854 /* Find reload register to use for reload number ORDER. */
1856 static int
1857 find_reg (struct insn_chain *chain, int order)
1859 int rnum = reload_order[order];
1860 struct reload *rl = rld + rnum;
1861 int best_cost = INT_MAX;
1862 int best_reg = -1;
1863 unsigned int i, j, n;
1864 int k;
1865 HARD_REG_SET not_usable;
1866 HARD_REG_SET used_by_other_reload;
1867 reg_set_iterator rsi;
1868 static int regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1869 static int best_regno_pseudo_regs[FIRST_PSEUDO_REGISTER];
1871 COPY_HARD_REG_SET (not_usable, bad_spill_regs);
1872 IOR_HARD_REG_SET (not_usable, bad_spill_regs_global);
1873 IOR_COMPL_HARD_REG_SET (not_usable, reg_class_contents[rl->rclass]);
1875 CLEAR_HARD_REG_SET (used_by_other_reload);
1876 for (k = 0; k < order; k++)
1878 int other = reload_order[k];
1880 if (rld[other].regno >= 0 && reloads_conflict (other, rnum))
1881 for (j = 0; j < rld[other].nregs; j++)
1882 SET_HARD_REG_BIT (used_by_other_reload, rld[other].regno + j);
1885 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1887 #ifdef REG_ALLOC_ORDER
1888 unsigned int regno = reg_alloc_order[i];
1889 #else
1890 unsigned int regno = i;
1891 #endif
1893 if (! TEST_HARD_REG_BIT (not_usable, regno)
1894 && ! TEST_HARD_REG_BIT (used_by_other_reload, regno)
1895 && HARD_REGNO_MODE_OK (regno, rl->mode))
1897 int this_cost = spill_cost[regno];
1898 int ok = 1;
1899 unsigned int this_nregs = hard_regno_nregs[regno][rl->mode];
1901 for (j = 1; j < this_nregs; j++)
1903 this_cost += spill_add_cost[regno + j];
1904 if ((TEST_HARD_REG_BIT (not_usable, regno + j))
1905 || TEST_HARD_REG_BIT (used_by_other_reload, regno + j))
1906 ok = 0;
1908 if (! ok)
1909 continue;
1911 if (ira_conflicts_p)
1913 /* Ask IRA to find a better pseudo-register for
1914 spilling. */
1915 for (n = j = 0; j < this_nregs; j++)
1917 int r = hard_regno_to_pseudo_regno[regno + j];
1919 if (r < 0)
1920 continue;
1921 if (n == 0 || regno_pseudo_regs[n - 1] != r)
1922 regno_pseudo_regs[n++] = r;
1924 regno_pseudo_regs[n++] = -1;
1925 if (best_reg < 0
1926 || ira_better_spill_reload_regno_p (regno_pseudo_regs,
1927 best_regno_pseudo_regs,
1928 rl->in, rl->out,
1929 chain->insn))
1931 best_reg = regno;
1932 for (j = 0;; j++)
1934 best_regno_pseudo_regs[j] = regno_pseudo_regs[j];
1935 if (regno_pseudo_regs[j] < 0)
1936 break;
1939 continue;
1942 if (rl->in && REG_P (rl->in) && REGNO (rl->in) == regno)
1943 this_cost--;
1944 if (rl->out && REG_P (rl->out) && REGNO (rl->out) == regno)
1945 this_cost--;
1946 if (this_cost < best_cost
1947 /* Among registers with equal cost, prefer caller-saved ones, or
1948 use REG_ALLOC_ORDER if it is defined. */
1949 || (this_cost == best_cost
1950 #ifdef REG_ALLOC_ORDER
1951 && (inv_reg_alloc_order[regno]
1952 < inv_reg_alloc_order[best_reg])
1953 #else
1954 && call_used_regs[regno]
1955 && ! call_used_regs[best_reg]
1956 #endif
1959 best_reg = regno;
1960 best_cost = this_cost;
1964 if (best_reg == -1)
1965 return 0;
1967 if (dump_file)
1968 fprintf (dump_file, "Using reg %d for reload %d\n", best_reg, rnum);
1970 rl->nregs = hard_regno_nregs[best_reg][rl->mode];
1971 rl->regno = best_reg;
1973 EXECUTE_IF_SET_IN_REG_SET
1974 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, j, rsi)
1976 count_spilled_pseudo (best_reg, rl->nregs, j);
1979 EXECUTE_IF_SET_IN_REG_SET
1980 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, j, rsi)
1982 count_spilled_pseudo (best_reg, rl->nregs, j);
1985 for (i = 0; i < rl->nregs; i++)
1987 gcc_assert (spill_cost[best_reg + i] == 0);
1988 gcc_assert (spill_add_cost[best_reg + i] == 0);
1989 gcc_assert (hard_regno_to_pseudo_regno[best_reg + i] == -1);
1990 SET_HARD_REG_BIT (used_spill_regs_local, best_reg + i);
1992 return 1;
1995 /* Find more reload regs to satisfy the remaining need of an insn, which
1996 is given by CHAIN.
1997 Do it by ascending class number, since otherwise a reg
1998 might be spilled for a big class and might fail to count
1999 for a smaller class even though it belongs to that class. */
2001 static void
2002 find_reload_regs (struct insn_chain *chain)
2004 int i;
2006 /* In order to be certain of getting the registers we need,
2007 we must sort the reloads into order of increasing register class.
2008 Then our grabbing of reload registers will parallel the process
2009 that provided the reload registers. */
2010 for (i = 0; i < chain->n_reloads; i++)
2012 /* Show whether this reload already has a hard reg. */
2013 if (chain->rld[i].reg_rtx)
2015 int regno = REGNO (chain->rld[i].reg_rtx);
2016 chain->rld[i].regno = regno;
2017 chain->rld[i].nregs
2018 = hard_regno_nregs[regno][GET_MODE (chain->rld[i].reg_rtx)];
2020 else
2021 chain->rld[i].regno = -1;
2022 reload_order[i] = i;
2025 n_reloads = chain->n_reloads;
2026 memcpy (rld, chain->rld, n_reloads * sizeof (struct reload));
2028 CLEAR_HARD_REG_SET (used_spill_regs_local);
2030 if (dump_file)
2031 fprintf (dump_file, "Spilling for insn %d.\n", INSN_UID (chain->insn));
2033 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
2035 /* Compute the order of preference for hard registers to spill. */
2037 order_regs_for_reload (chain);
2039 for (i = 0; i < n_reloads; i++)
2041 int r = reload_order[i];
2043 /* Ignore reloads that got marked inoperative. */
2044 if ((rld[r].out != 0 || rld[r].in != 0 || rld[r].secondary_p)
2045 && ! rld[r].optional
2046 && rld[r].regno == -1)
2047 if (! find_reg (chain, i))
2049 if (dump_file)
2050 fprintf (dump_file, "reload failure for reload %d\n", r);
2051 spill_failure (chain->insn, rld[r].rclass);
2052 failure = 1;
2053 return;
2057 COPY_HARD_REG_SET (chain->used_spill_regs, used_spill_regs_local);
2058 IOR_HARD_REG_SET (used_spill_regs, used_spill_regs_local);
2060 memcpy (chain->rld, rld, n_reloads * sizeof (struct reload));
2063 static void
2064 select_reload_regs (void)
2066 struct insn_chain *chain;
2068 /* Try to satisfy the needs for each insn. */
2069 for (chain = insns_need_reload; chain != 0;
2070 chain = chain->next_need_reload)
2071 find_reload_regs (chain);
2074 /* Delete all insns that were inserted by emit_caller_save_insns during
2075 this iteration. */
2076 static void
2077 delete_caller_save_insns (void)
2079 struct insn_chain *c = reload_insn_chain;
2081 while (c != 0)
2083 while (c != 0 && c->is_caller_save_insn)
2085 struct insn_chain *next = c->next;
2086 rtx insn = c->insn;
2088 if (c == reload_insn_chain)
2089 reload_insn_chain = next;
2090 delete_insn (insn);
2092 if (next)
2093 next->prev = c->prev;
2094 if (c->prev)
2095 c->prev->next = next;
2096 c->next = unused_insn_chains;
2097 unused_insn_chains = c;
2098 c = next;
2100 if (c != 0)
2101 c = c->next;
2105 /* Handle the failure to find a register to spill.
2106 INSN should be one of the insns which needed this particular spill reg. */
2108 static void
2109 spill_failure (rtx insn, enum reg_class rclass)
2111 if (asm_noperands (PATTERN (insn)) >= 0)
2112 error_for_asm (insn, "can%'t find a register in class %qs while "
2113 "reloading %<asm%>",
2114 reg_class_names[rclass]);
2115 else
2117 error ("unable to find a register to spill in class %qs",
2118 reg_class_names[rclass]);
2120 if (dump_file)
2122 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
2123 debug_reload_to_stream (dump_file);
2125 fatal_insn ("this is the insn:", insn);
2129 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2130 data that is dead in INSN. */
2132 static void
2133 delete_dead_insn (rtx insn)
2135 rtx prev = prev_active_insn (insn);
2136 rtx prev_dest;
2138 /* If the previous insn sets a register that dies in our insn make
2139 a note that we want to run DCE immediately after reload.
2141 We used to delete the previous insn & recurse, but that's wrong for
2142 block local equivalences. Instead of trying to figure out the exact
2143 circumstances where we can delete the potentially dead insns, just
2144 let DCE do the job. */
2145 if (prev && GET_CODE (PATTERN (prev)) == SET
2146 && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
2147 && reg_mentioned_p (prev_dest, PATTERN (insn))
2148 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
2149 && ! side_effects_p (SET_SRC (PATTERN (prev))))
2150 need_dce = 1;
2152 SET_INSN_DELETED (insn);
2155 /* Modify the home of pseudo-reg I.
2156 The new home is present in reg_renumber[I].
2158 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2159 or it may be -1, meaning there is none or it is not relevant.
2160 This is used so that all pseudos spilled from a given hard reg
2161 can share one stack slot. */
2163 static void
2164 alter_reg (int i, int from_reg, bool dont_share_p)
2166 /* When outputting an inline function, this can happen
2167 for a reg that isn't actually used. */
2168 if (regno_reg_rtx[i] == 0)
2169 return;
2171 /* If the reg got changed to a MEM at rtl-generation time,
2172 ignore it. */
2173 if (!REG_P (regno_reg_rtx[i]))
2174 return;
2176 /* Modify the reg-rtx to contain the new hard reg
2177 number or else to contain its pseudo reg number. */
2178 SET_REGNO (regno_reg_rtx[i],
2179 reg_renumber[i] >= 0 ? reg_renumber[i] : i);
2181 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2182 allocate a stack slot for it. */
2184 if (reg_renumber[i] < 0
2185 && REG_N_REFS (i) > 0
2186 && reg_equiv_constant (i) == 0
2187 && (reg_equiv_invariant (i) == 0
2188 || reg_equiv_init (i) == 0)
2189 && reg_equiv_memory_loc (i) == 0)
2191 rtx x = NULL_RTX;
2192 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2193 unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
2194 unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
2195 unsigned int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2196 unsigned int min_align = reg_max_ref_width[i] * BITS_PER_UNIT;
2197 int adjust = 0;
2199 something_was_spilled = true;
2201 if (ira_conflicts_p)
2203 /* Mark the spill for IRA. */
2204 SET_REGNO_REG_SET (&spilled_pseudos, i);
2205 if (!dont_share_p)
2206 x = ira_reuse_stack_slot (i, inherent_size, total_size);
2209 if (x)
2212 /* Each pseudo reg has an inherent size which comes from its own mode,
2213 and a total size which provides room for paradoxical subregs
2214 which refer to the pseudo reg in wider modes.
2216 We can use a slot already allocated if it provides both
2217 enough inherent space and enough total space.
2218 Otherwise, we allocate a new slot, making sure that it has no less
2219 inherent space, and no less total space, then the previous slot. */
2220 else if (from_reg == -1 || (!dont_share_p && ira_conflicts_p))
2222 rtx stack_slot;
2224 /* No known place to spill from => no slot to reuse. */
2225 x = assign_stack_local (mode, total_size,
2226 min_align > inherent_align
2227 || total_size > inherent_size ? -1 : 0);
2229 stack_slot = x;
2231 /* Cancel the big-endian correction done in assign_stack_local.
2232 Get the address of the beginning of the slot. This is so we
2233 can do a big-endian correction unconditionally below. */
2234 if (BYTES_BIG_ENDIAN)
2236 adjust = inherent_size - total_size;
2237 if (adjust)
2238 stack_slot
2239 = adjust_address_nv (x, mode_for_size (total_size
2240 * BITS_PER_UNIT,
2241 MODE_INT, 1),
2242 adjust);
2245 if (! dont_share_p && ira_conflicts_p)
2246 /* Inform IRA about allocation a new stack slot. */
2247 ira_mark_new_stack_slot (stack_slot, i, total_size);
2250 /* Reuse a stack slot if possible. */
2251 else if (spill_stack_slot[from_reg] != 0
2252 && spill_stack_slot_width[from_reg] >= total_size
2253 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2254 >= inherent_size)
2255 && MEM_ALIGN (spill_stack_slot[from_reg]) >= min_align)
2256 x = spill_stack_slot[from_reg];
2258 /* Allocate a bigger slot. */
2259 else
2261 /* Compute maximum size needed, both for inherent size
2262 and for total size. */
2263 rtx stack_slot;
2265 if (spill_stack_slot[from_reg])
2267 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2268 > inherent_size)
2269 mode = GET_MODE (spill_stack_slot[from_reg]);
2270 if (spill_stack_slot_width[from_reg] > total_size)
2271 total_size = spill_stack_slot_width[from_reg];
2272 if (MEM_ALIGN (spill_stack_slot[from_reg]) > min_align)
2273 min_align = MEM_ALIGN (spill_stack_slot[from_reg]);
2276 /* Make a slot with that size. */
2277 x = assign_stack_local (mode, total_size,
2278 min_align > inherent_align
2279 || total_size > inherent_size ? -1 : 0);
2280 stack_slot = x;
2282 /* Cancel the big-endian correction done in assign_stack_local.
2283 Get the address of the beginning of the slot. This is so we
2284 can do a big-endian correction unconditionally below. */
2285 if (BYTES_BIG_ENDIAN)
2287 adjust = GET_MODE_SIZE (mode) - total_size;
2288 if (adjust)
2289 stack_slot
2290 = adjust_address_nv (x, mode_for_size (total_size
2291 * BITS_PER_UNIT,
2292 MODE_INT, 1),
2293 adjust);
2296 spill_stack_slot[from_reg] = stack_slot;
2297 spill_stack_slot_width[from_reg] = total_size;
2300 /* On a big endian machine, the "address" of the slot
2301 is the address of the low part that fits its inherent mode. */
2302 if (BYTES_BIG_ENDIAN && inherent_size < total_size)
2303 adjust += (total_size - inherent_size);
2305 /* If we have any adjustment to make, or if the stack slot is the
2306 wrong mode, make a new stack slot. */
2307 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
2309 /* Set all of the memory attributes as appropriate for a spill. */
2310 set_mem_attrs_for_spill (x);
2312 /* Save the stack slot for later. */
2313 reg_equiv_memory_loc (i) = x;
2317 /* Mark the slots in regs_ever_live for the hard regs used by
2318 pseudo-reg number REGNO, accessed in MODE. */
2320 static void
2321 mark_home_live_1 (int regno, enum machine_mode mode)
2323 int i, lim;
2325 i = reg_renumber[regno];
2326 if (i < 0)
2327 return;
2328 lim = end_hard_regno (mode, i);
2329 while (i < lim)
2330 df_set_regs_ever_live(i++, true);
2333 /* Mark the slots in regs_ever_live for the hard regs
2334 used by pseudo-reg number REGNO. */
2336 void
2337 mark_home_live (int regno)
2339 if (reg_renumber[regno] >= 0)
2340 mark_home_live_1 (regno, PSEUDO_REGNO_MODE (regno));
2343 /* This function handles the tracking of elimination offsets around branches.
2345 X is a piece of RTL being scanned.
2347 INSN is the insn that it came from, if any.
2349 INITIAL_P is nonzero if we are to set the offset to be the initial
2350 offset and zero if we are setting the offset of the label to be the
2351 current offset. */
2353 static void
2354 set_label_offsets (rtx x, rtx insn, int initial_p)
2356 enum rtx_code code = GET_CODE (x);
2357 rtx tem;
2358 unsigned int i;
2359 struct elim_table *p;
2361 switch (code)
2363 case LABEL_REF:
2364 if (LABEL_REF_NONLOCAL_P (x))
2365 return;
2367 x = XEXP (x, 0);
2369 /* ... fall through ... */
2371 case CODE_LABEL:
2372 /* If we know nothing about this label, set the desired offsets. Note
2373 that this sets the offset at a label to be the offset before a label
2374 if we don't know anything about the label. This is not correct for
2375 the label after a BARRIER, but is the best guess we can make. If
2376 we guessed wrong, we will suppress an elimination that might have
2377 been possible had we been able to guess correctly. */
2379 if (! offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num])
2381 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2382 offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2383 = (initial_p ? reg_eliminate[i].initial_offset
2384 : reg_eliminate[i].offset);
2385 offsets_known_at[CODE_LABEL_NUMBER (x) - first_label_num] = 1;
2388 /* Otherwise, if this is the definition of a label and it is
2389 preceded by a BARRIER, set our offsets to the known offset of
2390 that label. */
2392 else if (x == insn
2393 && (tem = prev_nonnote_insn (insn)) != 0
2394 && BARRIER_P (tem))
2395 set_offsets_for_label (insn);
2396 else
2397 /* If neither of the above cases is true, compare each offset
2398 with those previously recorded and suppress any eliminations
2399 where the offsets disagree. */
2401 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2402 if (offsets_at[CODE_LABEL_NUMBER (x) - first_label_num][i]
2403 != (initial_p ? reg_eliminate[i].initial_offset
2404 : reg_eliminate[i].offset))
2405 reg_eliminate[i].can_eliminate = 0;
2407 return;
2409 case JUMP_INSN:
2410 set_label_offsets (PATTERN (insn), insn, initial_p);
2412 /* ... fall through ... */
2414 case INSN:
2415 case CALL_INSN:
2416 /* Any labels mentioned in REG_LABEL_OPERAND notes can be branched
2417 to indirectly and hence must have all eliminations at their
2418 initial offsets. */
2419 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2420 if (REG_NOTE_KIND (tem) == REG_LABEL_OPERAND)
2421 set_label_offsets (XEXP (tem, 0), insn, 1);
2422 return;
2424 case PARALLEL:
2425 case ADDR_VEC:
2426 case ADDR_DIFF_VEC:
2427 /* Each of the labels in the parallel or address vector must be
2428 at their initial offsets. We want the first field for PARALLEL
2429 and ADDR_VEC and the second field for ADDR_DIFF_VEC. */
2431 for (i = 0; i < (unsigned) XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2432 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2433 insn, initial_p);
2434 return;
2436 case SET:
2437 /* We only care about setting PC. If the source is not RETURN,
2438 IF_THEN_ELSE, or a label, disable any eliminations not at
2439 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2440 isn't one of those possibilities. For branches to a label,
2441 call ourselves recursively.
2443 Note that this can disable elimination unnecessarily when we have
2444 a non-local goto since it will look like a non-constant jump to
2445 someplace in the current function. This isn't a significant
2446 problem since such jumps will normally be when all elimination
2447 pairs are back to their initial offsets. */
2449 if (SET_DEST (x) != pc_rtx)
2450 return;
2452 switch (GET_CODE (SET_SRC (x)))
2454 case PC:
2455 case RETURN:
2456 return;
2458 case LABEL_REF:
2459 set_label_offsets (SET_SRC (x), insn, initial_p);
2460 return;
2462 case IF_THEN_ELSE:
2463 tem = XEXP (SET_SRC (x), 1);
2464 if (GET_CODE (tem) == LABEL_REF)
2465 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2466 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2467 break;
2469 tem = XEXP (SET_SRC (x), 2);
2470 if (GET_CODE (tem) == LABEL_REF)
2471 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2472 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2473 break;
2474 return;
2476 default:
2477 break;
2480 /* If we reach here, all eliminations must be at their initial
2481 offset because we are doing a jump to a variable address. */
2482 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2483 if (p->offset != p->initial_offset)
2484 p->can_eliminate = 0;
2485 break;
2487 default:
2488 break;
2492 /* Called through for_each_rtx, this function examines every reg that occurs
2493 in PX and adjusts the costs for its elimination which are gathered by IRA.
2494 DATA is the insn in which PX occurs. We do not recurse into MEM
2495 expressions. */
2497 static int
2498 note_reg_elim_costly (rtx *px, void *data)
2500 rtx insn = (rtx)data;
2501 rtx x = *px;
2503 if (MEM_P (x))
2504 return -1;
2506 if (REG_P (x)
2507 && REGNO (x) >= FIRST_PSEUDO_REGISTER
2508 && reg_equiv_init (REGNO (x))
2509 && reg_equiv_invariant (REGNO (x)))
2511 rtx t = reg_equiv_invariant (REGNO (x));
2512 rtx new_rtx = eliminate_regs_1 (t, Pmode, insn, true, true);
2513 int cost = set_src_cost (new_rtx, optimize_bb_for_speed_p (elim_bb));
2514 int freq = REG_FREQ_FROM_BB (elim_bb);
2516 if (cost != 0)
2517 ira_adjust_equiv_reg_cost (REGNO (x), -cost * freq);
2519 return 0;
2522 /* Scan X and replace any eliminable registers (such as fp) with a
2523 replacement (such as sp), plus an offset.
2525 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2526 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2527 MEM, we are allowed to replace a sum of a register and the constant zero
2528 with the register, which we cannot do outside a MEM. In addition, we need
2529 to record the fact that a register is referenced outside a MEM.
2531 If INSN is an insn, it is the insn containing X. If we replace a REG
2532 in a SET_DEST with an equivalent MEM and INSN is nonzero, write a
2533 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2534 the REG is being modified.
2536 Alternatively, INSN may be a note (an EXPR_LIST or INSN_LIST).
2537 That's used when we eliminate in expressions stored in notes.
2538 This means, do not set ref_outside_mem even if the reference
2539 is outside of MEMs.
2541 If FOR_COSTS is true, we are being called before reload in order to
2542 estimate the costs of keeping registers with an equivalence unallocated.
2544 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2545 replacements done assuming all offsets are at their initial values. If
2546 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2547 encounter, return the actual location so that find_reloads will do
2548 the proper thing. */
2550 static rtx
2551 eliminate_regs_1 (rtx x, enum machine_mode mem_mode, rtx insn,
2552 bool may_use_invariant, bool for_costs)
2554 enum rtx_code code = GET_CODE (x);
2555 struct elim_table *ep;
2556 int regno;
2557 rtx new_rtx;
2558 int i, j;
2559 const char *fmt;
2560 int copied = 0;
2562 if (! current_function_decl)
2563 return x;
2565 switch (code)
2567 case CONST_INT:
2568 case CONST_DOUBLE:
2569 case CONST_FIXED:
2570 case CONST_VECTOR:
2571 case CONST:
2572 case SYMBOL_REF:
2573 case CODE_LABEL:
2574 case PC:
2575 case CC0:
2576 case ASM_INPUT:
2577 case ADDR_VEC:
2578 case ADDR_DIFF_VEC:
2579 case RETURN:
2580 return x;
2582 case REG:
2583 regno = REGNO (x);
2585 /* First handle the case where we encounter a bare register that
2586 is eliminable. Replace it with a PLUS. */
2587 if (regno < FIRST_PSEUDO_REGISTER)
2589 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2590 ep++)
2591 if (ep->from_rtx == x && ep->can_eliminate)
2592 return plus_constant (Pmode, ep->to_rtx, ep->previous_offset);
2595 else if (reg_renumber && reg_renumber[regno] < 0
2596 && reg_equivs
2597 && reg_equiv_invariant (regno))
2599 if (may_use_invariant || (insn && DEBUG_INSN_P (insn)))
2600 return eliminate_regs_1 (copy_rtx (reg_equiv_invariant (regno)),
2601 mem_mode, insn, true, for_costs);
2602 /* There exists at least one use of REGNO that cannot be
2603 eliminated. Prevent the defining insn from being deleted. */
2604 reg_equiv_init (regno) = NULL_RTX;
2605 if (!for_costs)
2606 alter_reg (regno, -1, true);
2608 return x;
2610 /* You might think handling MINUS in a manner similar to PLUS is a
2611 good idea. It is not. It has been tried multiple times and every
2612 time the change has had to have been reverted.
2614 Other parts of reload know a PLUS is special (gen_reload for example)
2615 and require special code to handle code a reloaded PLUS operand.
2617 Also consider backends where the flags register is clobbered by a
2618 MINUS, but we can emit a PLUS that does not clobber flags (IA-32,
2619 lea instruction comes to mind). If we try to reload a MINUS, we
2620 may kill the flags register that was holding a useful value.
2622 So, please before trying to handle MINUS, consider reload as a
2623 whole instead of this little section as well as the backend issues. */
2624 case PLUS:
2625 /* If this is the sum of an eliminable register and a constant, rework
2626 the sum. */
2627 if (REG_P (XEXP (x, 0))
2628 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2629 && CONSTANT_P (XEXP (x, 1)))
2631 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2632 ep++)
2633 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2635 /* The only time we want to replace a PLUS with a REG (this
2636 occurs when the constant operand of the PLUS is the negative
2637 of the offset) is when we are inside a MEM. We won't want
2638 to do so at other times because that would change the
2639 structure of the insn in a way that reload can't handle.
2640 We special-case the commonest situation in
2641 eliminate_regs_in_insn, so just replace a PLUS with a
2642 PLUS here, unless inside a MEM. */
2643 if (mem_mode != 0 && CONST_INT_P (XEXP (x, 1))
2644 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2645 return ep->to_rtx;
2646 else
2647 return gen_rtx_PLUS (Pmode, ep->to_rtx,
2648 plus_constant (Pmode, XEXP (x, 1),
2649 ep->previous_offset));
2652 /* If the register is not eliminable, we are done since the other
2653 operand is a constant. */
2654 return x;
2657 /* If this is part of an address, we want to bring any constant to the
2658 outermost PLUS. We will do this by doing register replacement in
2659 our operands and seeing if a constant shows up in one of them.
2661 Note that there is no risk of modifying the structure of the insn,
2662 since we only get called for its operands, thus we are either
2663 modifying the address inside a MEM, or something like an address
2664 operand of a load-address insn. */
2667 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true,
2668 for_costs);
2669 rtx new1 = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2670 for_costs);
2672 if (reg_renumber && (new0 != XEXP (x, 0) || new1 != XEXP (x, 1)))
2674 /* If one side is a PLUS and the other side is a pseudo that
2675 didn't get a hard register but has a reg_equiv_constant,
2676 we must replace the constant here since it may no longer
2677 be in the position of any operand. */
2678 if (GET_CODE (new0) == PLUS && REG_P (new1)
2679 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2680 && reg_renumber[REGNO (new1)] < 0
2681 && reg_equivs
2682 && reg_equiv_constant (REGNO (new1)) != 0)
2683 new1 = reg_equiv_constant (REGNO (new1));
2684 else if (GET_CODE (new1) == PLUS && REG_P (new0)
2685 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2686 && reg_renumber[REGNO (new0)] < 0
2687 && reg_equiv_constant (REGNO (new0)) != 0)
2688 new0 = reg_equiv_constant (REGNO (new0));
2690 new_rtx = form_sum (GET_MODE (x), new0, new1);
2692 /* As above, if we are not inside a MEM we do not want to
2693 turn a PLUS into something else. We might try to do so here
2694 for an addition of 0 if we aren't optimizing. */
2695 if (! mem_mode && GET_CODE (new_rtx) != PLUS)
2696 return gen_rtx_PLUS (GET_MODE (x), new_rtx, const0_rtx);
2697 else
2698 return new_rtx;
2701 return x;
2703 case MULT:
2704 /* If this is the product of an eliminable register and a
2705 constant, apply the distribute law and move the constant out
2706 so that we have (plus (mult ..) ..). This is needed in order
2707 to keep load-address insns valid. This case is pathological.
2708 We ignore the possibility of overflow here. */
2709 if (REG_P (XEXP (x, 0))
2710 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2711 && CONST_INT_P (XEXP (x, 1)))
2712 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2713 ep++)
2714 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2716 if (! mem_mode
2717 /* Refs inside notes or in DEBUG_INSNs don't count for
2718 this purpose. */
2719 && ! (insn != 0 && (GET_CODE (insn) == EXPR_LIST
2720 || GET_CODE (insn) == INSN_LIST
2721 || DEBUG_INSN_P (insn))))
2722 ep->ref_outside_mem = 1;
2724 return
2725 plus_constant (Pmode,
2726 gen_rtx_MULT (Pmode, ep->to_rtx, XEXP (x, 1)),
2727 ep->previous_offset * INTVAL (XEXP (x, 1)));
2730 /* ... fall through ... */
2732 case CALL:
2733 case COMPARE:
2734 /* See comments before PLUS about handling MINUS. */
2735 case MINUS:
2736 case DIV: case UDIV:
2737 case MOD: case UMOD:
2738 case AND: case IOR: case XOR:
2739 case ROTATERT: case ROTATE:
2740 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2741 case NE: case EQ:
2742 case GE: case GT: case GEU: case GTU:
2743 case LE: case LT: case LEU: case LTU:
2745 rtx new0 = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2746 for_costs);
2747 rtx new1 = XEXP (x, 1)
2748 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, false,
2749 for_costs) : 0;
2751 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2752 return gen_rtx_fmt_ee (code, GET_MODE (x), new0, new1);
2754 return x;
2756 case EXPR_LIST:
2757 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2758 if (XEXP (x, 0))
2760 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, true,
2761 for_costs);
2762 if (new_rtx != XEXP (x, 0))
2764 /* If this is a REG_DEAD note, it is not valid anymore.
2765 Using the eliminated version could result in creating a
2766 REG_DEAD note for the stack or frame pointer. */
2767 if (REG_NOTE_KIND (x) == REG_DEAD)
2768 return (XEXP (x, 1)
2769 ? eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2770 for_costs)
2771 : NULL_RTX);
2773 x = alloc_reg_note (REG_NOTE_KIND (x), new_rtx, XEXP (x, 1));
2777 /* ... fall through ... */
2779 case INSN_LIST:
2780 /* Now do eliminations in the rest of the chain. If this was
2781 an EXPR_LIST, this might result in allocating more memory than is
2782 strictly needed, but it simplifies the code. */
2783 if (XEXP (x, 1))
2785 new_rtx = eliminate_regs_1 (XEXP (x, 1), mem_mode, insn, true,
2786 for_costs);
2787 if (new_rtx != XEXP (x, 1))
2788 return
2789 gen_rtx_fmt_ee (GET_CODE (x), GET_MODE (x), XEXP (x, 0), new_rtx);
2791 return x;
2793 case PRE_INC:
2794 case POST_INC:
2795 case PRE_DEC:
2796 case POST_DEC:
2797 /* We do not support elimination of a register that is modified.
2798 elimination_effects has already make sure that this does not
2799 happen. */
2800 return x;
2802 case PRE_MODIFY:
2803 case POST_MODIFY:
2804 /* We do not support elimination of a register that is modified.
2805 elimination_effects has already make sure that this does not
2806 happen. The only remaining case we need to consider here is
2807 that the increment value may be an eliminable register. */
2808 if (GET_CODE (XEXP (x, 1)) == PLUS
2809 && XEXP (XEXP (x, 1), 0) == XEXP (x, 0))
2811 rtx new_rtx = eliminate_regs_1 (XEXP (XEXP (x, 1), 1), mem_mode,
2812 insn, true, for_costs);
2814 if (new_rtx != XEXP (XEXP (x, 1), 1))
2815 return gen_rtx_fmt_ee (code, GET_MODE (x), XEXP (x, 0),
2816 gen_rtx_PLUS (GET_MODE (x),
2817 XEXP (x, 0), new_rtx));
2819 return x;
2821 case STRICT_LOW_PART:
2822 case NEG: case NOT:
2823 case SIGN_EXTEND: case ZERO_EXTEND:
2824 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2825 case FLOAT: case FIX:
2826 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2827 case ABS:
2828 case SQRT:
2829 case FFS:
2830 case CLZ:
2831 case CTZ:
2832 case POPCOUNT:
2833 case PARITY:
2834 case BSWAP:
2835 new_rtx = eliminate_regs_1 (XEXP (x, 0), mem_mode, insn, false,
2836 for_costs);
2837 if (new_rtx != XEXP (x, 0))
2838 return gen_rtx_fmt_e (code, GET_MODE (x), new_rtx);
2839 return x;
2841 case SUBREG:
2842 /* Similar to above processing, but preserve SUBREG_BYTE.
2843 Convert (subreg (mem)) to (mem) if not paradoxical.
2844 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2845 pseudo didn't get a hard reg, we must replace this with the
2846 eliminated version of the memory location because push_reload
2847 may do the replacement in certain circumstances. */
2848 if (REG_P (SUBREG_REG (x))
2849 && !paradoxical_subreg_p (x)
2850 && reg_equivs
2851 && reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
2853 new_rtx = SUBREG_REG (x);
2855 else
2856 new_rtx = eliminate_regs_1 (SUBREG_REG (x), mem_mode, insn, false, for_costs);
2858 if (new_rtx != SUBREG_REG (x))
2860 int x_size = GET_MODE_SIZE (GET_MODE (x));
2861 int new_size = GET_MODE_SIZE (GET_MODE (new_rtx));
2863 if (MEM_P (new_rtx)
2864 && ((x_size < new_size
2865 #ifdef WORD_REGISTER_OPERATIONS
2866 /* On these machines, combine can create rtl of the form
2867 (set (subreg:m1 (reg:m2 R) 0) ...)
2868 where m1 < m2, and expects something interesting to
2869 happen to the entire word. Moreover, it will use the
2870 (reg:m2 R) later, expecting all bits to be preserved.
2871 So if the number of words is the same, preserve the
2872 subreg so that push_reload can see it. */
2873 && ! ((x_size - 1) / UNITS_PER_WORD
2874 == (new_size -1 ) / UNITS_PER_WORD)
2875 #endif
2877 || x_size == new_size)
2879 return adjust_address_nv (new_rtx, GET_MODE (x), SUBREG_BYTE (x));
2880 else
2881 return gen_rtx_SUBREG (GET_MODE (x), new_rtx, SUBREG_BYTE (x));
2884 return x;
2886 case MEM:
2887 /* Our only special processing is to pass the mode of the MEM to our
2888 recursive call and copy the flags. While we are here, handle this
2889 case more efficiently. */
2891 new_rtx = eliminate_regs_1 (XEXP (x, 0), GET_MODE (x), insn, true,
2892 for_costs);
2893 if (for_costs
2894 && memory_address_p (GET_MODE (x), XEXP (x, 0))
2895 && !memory_address_p (GET_MODE (x), new_rtx))
2896 for_each_rtx (&XEXP (x, 0), note_reg_elim_costly, insn);
2898 return replace_equiv_address_nv (x, new_rtx);
2900 case USE:
2901 /* Handle insn_list USE that a call to a pure function may generate. */
2902 new_rtx = eliminate_regs_1 (XEXP (x, 0), VOIDmode, insn, false,
2903 for_costs);
2904 if (new_rtx != XEXP (x, 0))
2905 return gen_rtx_USE (GET_MODE (x), new_rtx);
2906 return x;
2908 case CLOBBER:
2909 case ASM_OPERANDS:
2910 gcc_assert (insn && DEBUG_INSN_P (insn));
2911 break;
2913 case SET:
2914 gcc_unreachable ();
2916 default:
2917 break;
2920 /* Process each of our operands recursively. If any have changed, make a
2921 copy of the rtx. */
2922 fmt = GET_RTX_FORMAT (code);
2923 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2925 if (*fmt == 'e')
2927 new_rtx = eliminate_regs_1 (XEXP (x, i), mem_mode, insn, false,
2928 for_costs);
2929 if (new_rtx != XEXP (x, i) && ! copied)
2931 x = shallow_copy_rtx (x);
2932 copied = 1;
2934 XEXP (x, i) = new_rtx;
2936 else if (*fmt == 'E')
2938 int copied_vec = 0;
2939 for (j = 0; j < XVECLEN (x, i); j++)
2941 new_rtx = eliminate_regs_1 (XVECEXP (x, i, j), mem_mode, insn, false,
2942 for_costs);
2943 if (new_rtx != XVECEXP (x, i, j) && ! copied_vec)
2945 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2946 XVEC (x, i)->elem);
2947 if (! copied)
2949 x = shallow_copy_rtx (x);
2950 copied = 1;
2952 XVEC (x, i) = new_v;
2953 copied_vec = 1;
2955 XVECEXP (x, i, j) = new_rtx;
2960 return x;
2964 eliminate_regs (rtx x, enum machine_mode mem_mode, rtx insn)
2966 return eliminate_regs_1 (x, mem_mode, insn, false, false);
2969 /* Scan rtx X for modifications of elimination target registers. Update
2970 the table of eliminables to reflect the changed state. MEM_MODE is
2971 the mode of an enclosing MEM rtx, or VOIDmode if not within a MEM. */
2973 static void
2974 elimination_effects (rtx x, enum machine_mode mem_mode)
2976 enum rtx_code code = GET_CODE (x);
2977 struct elim_table *ep;
2978 int regno;
2979 int i, j;
2980 const char *fmt;
2982 switch (code)
2984 case CONST_INT:
2985 case CONST_DOUBLE:
2986 case CONST_FIXED:
2987 case CONST_VECTOR:
2988 case CONST:
2989 case SYMBOL_REF:
2990 case CODE_LABEL:
2991 case PC:
2992 case CC0:
2993 case ASM_INPUT:
2994 case ADDR_VEC:
2995 case ADDR_DIFF_VEC:
2996 case RETURN:
2997 return;
2999 case REG:
3000 regno = REGNO (x);
3002 /* First handle the case where we encounter a bare register that
3003 is eliminable. Replace it with a PLUS. */
3004 if (regno < FIRST_PSEUDO_REGISTER)
3006 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3007 ep++)
3008 if (ep->from_rtx == x && ep->can_eliminate)
3010 if (! mem_mode)
3011 ep->ref_outside_mem = 1;
3012 return;
3016 else if (reg_renumber[regno] < 0
3017 && reg_equivs != 0
3018 && reg_equiv_constant (regno)
3019 && ! function_invariant_p (reg_equiv_constant (regno)))
3020 elimination_effects (reg_equiv_constant (regno), mem_mode);
3021 return;
3023 case PRE_INC:
3024 case POST_INC:
3025 case PRE_DEC:
3026 case POST_DEC:
3027 case POST_MODIFY:
3028 case PRE_MODIFY:
3029 /* If we modify the source of an elimination rule, disable it. */
3030 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3031 if (ep->from_rtx == XEXP (x, 0))
3032 ep->can_eliminate = 0;
3034 /* If we modify the target of an elimination rule by adding a constant,
3035 update its offset. If we modify the target in any other way, we'll
3036 have to disable the rule as well. */
3037 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3038 if (ep->to_rtx == XEXP (x, 0))
3040 int size = GET_MODE_SIZE (mem_mode);
3042 /* If more bytes than MEM_MODE are pushed, account for them. */
3043 #ifdef PUSH_ROUNDING
3044 if (ep->to_rtx == stack_pointer_rtx)
3045 size = PUSH_ROUNDING (size);
3046 #endif
3047 if (code == PRE_DEC || code == POST_DEC)
3048 ep->offset += size;
3049 else if (code == PRE_INC || code == POST_INC)
3050 ep->offset -= size;
3051 else if (code == PRE_MODIFY || code == POST_MODIFY)
3053 if (GET_CODE (XEXP (x, 1)) == PLUS
3054 && XEXP (x, 0) == XEXP (XEXP (x, 1), 0)
3055 && CONST_INT_P (XEXP (XEXP (x, 1), 1)))
3056 ep->offset -= INTVAL (XEXP (XEXP (x, 1), 1));
3057 else
3058 ep->can_eliminate = 0;
3062 /* These two aren't unary operators. */
3063 if (code == POST_MODIFY || code == PRE_MODIFY)
3064 break;
3066 /* Fall through to generic unary operation case. */
3067 case STRICT_LOW_PART:
3068 case NEG: case NOT:
3069 case SIGN_EXTEND: case ZERO_EXTEND:
3070 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3071 case FLOAT: case FIX:
3072 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3073 case ABS:
3074 case SQRT:
3075 case FFS:
3076 case CLZ:
3077 case CTZ:
3078 case POPCOUNT:
3079 case PARITY:
3080 case BSWAP:
3081 elimination_effects (XEXP (x, 0), mem_mode);
3082 return;
3084 case SUBREG:
3085 if (REG_P (SUBREG_REG (x))
3086 && (GET_MODE_SIZE (GET_MODE (x))
3087 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3088 && reg_equivs != 0
3089 && reg_equiv_memory_loc (REGNO (SUBREG_REG (x))) != 0)
3090 return;
3092 elimination_effects (SUBREG_REG (x), mem_mode);
3093 return;
3095 case USE:
3096 /* If using a register that is the source of an eliminate we still
3097 think can be performed, note it cannot be performed since we don't
3098 know how this register is used. */
3099 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3100 if (ep->from_rtx == XEXP (x, 0))
3101 ep->can_eliminate = 0;
3103 elimination_effects (XEXP (x, 0), mem_mode);
3104 return;
3106 case CLOBBER:
3107 /* If clobbering a register that is the replacement register for an
3108 elimination we still think can be performed, note that it cannot
3109 be performed. Otherwise, we need not be concerned about it. */
3110 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3111 if (ep->to_rtx == XEXP (x, 0))
3112 ep->can_eliminate = 0;
3114 elimination_effects (XEXP (x, 0), mem_mode);
3115 return;
3117 case SET:
3118 /* Check for setting a register that we know about. */
3119 if (REG_P (SET_DEST (x)))
3121 /* See if this is setting the replacement register for an
3122 elimination.
3124 If DEST is the hard frame pointer, we do nothing because we
3125 assume that all assignments to the frame pointer are for
3126 non-local gotos and are being done at a time when they are valid
3127 and do not disturb anything else. Some machines want to
3128 eliminate a fake argument pointer (or even a fake frame pointer)
3129 with either the real frame or the stack pointer. Assignments to
3130 the hard frame pointer must not prevent this elimination. */
3132 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3133 ep++)
3134 if (ep->to_rtx == SET_DEST (x)
3135 && SET_DEST (x) != hard_frame_pointer_rtx)
3137 /* If it is being incremented, adjust the offset. Otherwise,
3138 this elimination can't be done. */
3139 rtx src = SET_SRC (x);
3141 if (GET_CODE (src) == PLUS
3142 && XEXP (src, 0) == SET_DEST (x)
3143 && CONST_INT_P (XEXP (src, 1)))
3144 ep->offset -= INTVAL (XEXP (src, 1));
3145 else
3146 ep->can_eliminate = 0;
3150 elimination_effects (SET_DEST (x), VOIDmode);
3151 elimination_effects (SET_SRC (x), VOIDmode);
3152 return;
3154 case MEM:
3155 /* Our only special processing is to pass the mode of the MEM to our
3156 recursive call. */
3157 elimination_effects (XEXP (x, 0), GET_MODE (x));
3158 return;
3160 default:
3161 break;
3164 fmt = GET_RTX_FORMAT (code);
3165 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3167 if (*fmt == 'e')
3168 elimination_effects (XEXP (x, i), mem_mode);
3169 else if (*fmt == 'E')
3170 for (j = 0; j < XVECLEN (x, i); j++)
3171 elimination_effects (XVECEXP (x, i, j), mem_mode);
3175 /* Descend through rtx X and verify that no references to eliminable registers
3176 remain. If any do remain, mark the involved register as not
3177 eliminable. */
3179 static void
3180 check_eliminable_occurrences (rtx x)
3182 const char *fmt;
3183 int i;
3184 enum rtx_code code;
3186 if (x == 0)
3187 return;
3189 code = GET_CODE (x);
3191 if (code == REG && REGNO (x) < FIRST_PSEUDO_REGISTER)
3193 struct elim_table *ep;
3195 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3196 if (ep->from_rtx == x)
3197 ep->can_eliminate = 0;
3198 return;
3201 fmt = GET_RTX_FORMAT (code);
3202 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3204 if (*fmt == 'e')
3205 check_eliminable_occurrences (XEXP (x, i));
3206 else if (*fmt == 'E')
3208 int j;
3209 for (j = 0; j < XVECLEN (x, i); j++)
3210 check_eliminable_occurrences (XVECEXP (x, i, j));
3215 /* Scan INSN and eliminate all eliminable registers in it.
3217 If REPLACE is nonzero, do the replacement destructively. Also
3218 delete the insn as dead it if it is setting an eliminable register.
3220 If REPLACE is zero, do all our allocations in reload_obstack.
3222 If no eliminations were done and this insn doesn't require any elimination
3223 processing (these are not identical conditions: it might be updating sp,
3224 but not referencing fp; this needs to be seen during reload_as_needed so
3225 that the offset between fp and sp can be taken into consideration), zero
3226 is returned. Otherwise, 1 is returned. */
3228 static int
3229 eliminate_regs_in_insn (rtx insn, int replace)
3231 int icode = recog_memoized (insn);
3232 rtx old_body = PATTERN (insn);
3233 int insn_is_asm = asm_noperands (old_body) >= 0;
3234 rtx old_set = single_set (insn);
3235 rtx new_body;
3236 int val = 0;
3237 int i;
3238 rtx substed_operand[MAX_RECOG_OPERANDS];
3239 rtx orig_operand[MAX_RECOG_OPERANDS];
3240 struct elim_table *ep;
3241 rtx plus_src, plus_cst_src;
3243 if (! insn_is_asm && icode < 0)
3245 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3246 || GET_CODE (PATTERN (insn)) == CLOBBER
3247 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3248 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3249 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3250 || DEBUG_INSN_P (insn));
3251 if (DEBUG_INSN_P (insn))
3252 INSN_VAR_LOCATION_LOC (insn)
3253 = eliminate_regs (INSN_VAR_LOCATION_LOC (insn), VOIDmode, insn);
3254 return 0;
3257 if (old_set != 0 && REG_P (SET_DEST (old_set))
3258 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3260 /* Check for setting an eliminable register. */
3261 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3262 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3264 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
3265 /* If this is setting the frame pointer register to the
3266 hardware frame pointer register and this is an elimination
3267 that will be done (tested above), this insn is really
3268 adjusting the frame pointer downward to compensate for
3269 the adjustment done before a nonlocal goto. */
3270 if (ep->from == FRAME_POINTER_REGNUM
3271 && ep->to == HARD_FRAME_POINTER_REGNUM)
3273 rtx base = SET_SRC (old_set);
3274 rtx base_insn = insn;
3275 HOST_WIDE_INT offset = 0;
3277 while (base != ep->to_rtx)
3279 rtx prev_insn, prev_set;
3281 if (GET_CODE (base) == PLUS
3282 && CONST_INT_P (XEXP (base, 1)))
3284 offset += INTVAL (XEXP (base, 1));
3285 base = XEXP (base, 0);
3287 else if ((prev_insn = prev_nonnote_insn (base_insn)) != 0
3288 && (prev_set = single_set (prev_insn)) != 0
3289 && rtx_equal_p (SET_DEST (prev_set), base))
3291 base = SET_SRC (prev_set);
3292 base_insn = prev_insn;
3294 else
3295 break;
3298 if (base == ep->to_rtx)
3300 rtx src = plus_constant (Pmode, ep->to_rtx,
3301 offset - ep->offset);
3303 new_body = old_body;
3304 if (! replace)
3306 new_body = copy_insn (old_body);
3307 if (REG_NOTES (insn))
3308 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3310 PATTERN (insn) = new_body;
3311 old_set = single_set (insn);
3313 /* First see if this insn remains valid when we
3314 make the change. If not, keep the INSN_CODE
3315 the same and let reload fit it up. */
3316 validate_change (insn, &SET_SRC (old_set), src, 1);
3317 validate_change (insn, &SET_DEST (old_set),
3318 ep->to_rtx, 1);
3319 if (! apply_change_group ())
3321 SET_SRC (old_set) = src;
3322 SET_DEST (old_set) = ep->to_rtx;
3325 val = 1;
3326 goto done;
3329 #endif
3331 /* In this case this insn isn't serving a useful purpose. We
3332 will delete it in reload_as_needed once we know that this
3333 elimination is, in fact, being done.
3335 If REPLACE isn't set, we can't delete this insn, but needn't
3336 process it since it won't be used unless something changes. */
3337 if (replace)
3339 delete_dead_insn (insn);
3340 return 1;
3342 val = 1;
3343 goto done;
3347 /* We allow one special case which happens to work on all machines we
3348 currently support: a single set with the source or a REG_EQUAL
3349 note being a PLUS of an eliminable register and a constant. */
3350 plus_src = plus_cst_src = 0;
3351 if (old_set && REG_P (SET_DEST (old_set)))
3353 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3354 plus_src = SET_SRC (old_set);
3355 /* First see if the source is of the form (plus (...) CST). */
3356 if (plus_src
3357 && CONST_INT_P (XEXP (plus_src, 1)))
3358 plus_cst_src = plus_src;
3359 else if (REG_P (SET_SRC (old_set))
3360 || plus_src)
3362 /* Otherwise, see if we have a REG_EQUAL note of the form
3363 (plus (...) CST). */
3364 rtx links;
3365 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3367 if ((REG_NOTE_KIND (links) == REG_EQUAL
3368 || REG_NOTE_KIND (links) == REG_EQUIV)
3369 && GET_CODE (XEXP (links, 0)) == PLUS
3370 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3372 plus_cst_src = XEXP (links, 0);
3373 break;
3378 /* Check that the first operand of the PLUS is a hard reg or
3379 the lowpart subreg of one. */
3380 if (plus_cst_src)
3382 rtx reg = XEXP (plus_cst_src, 0);
3383 if (GET_CODE (reg) == SUBREG && subreg_lowpart_p (reg))
3384 reg = SUBREG_REG (reg);
3386 if (!REG_P (reg) || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
3387 plus_cst_src = 0;
3390 if (plus_cst_src)
3392 rtx reg = XEXP (plus_cst_src, 0);
3393 HOST_WIDE_INT offset = INTVAL (XEXP (plus_cst_src, 1));
3395 if (GET_CODE (reg) == SUBREG)
3396 reg = SUBREG_REG (reg);
3398 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3399 if (ep->from_rtx == reg && ep->can_eliminate)
3401 rtx to_rtx = ep->to_rtx;
3402 offset += ep->offset;
3403 offset = trunc_int_for_mode (offset, GET_MODE (plus_cst_src));
3405 if (GET_CODE (XEXP (plus_cst_src, 0)) == SUBREG)
3406 to_rtx = gen_lowpart (GET_MODE (XEXP (plus_cst_src, 0)),
3407 to_rtx);
3408 /* If we have a nonzero offset, and the source is already
3409 a simple REG, the following transformation would
3410 increase the cost of the insn by replacing a simple REG
3411 with (plus (reg sp) CST). So try only when we already
3412 had a PLUS before. */
3413 if (offset == 0 || plus_src)
3415 rtx new_src = plus_constant (GET_MODE (to_rtx),
3416 to_rtx, offset);
3418 new_body = old_body;
3419 if (! replace)
3421 new_body = copy_insn (old_body);
3422 if (REG_NOTES (insn))
3423 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3425 PATTERN (insn) = new_body;
3426 old_set = single_set (insn);
3428 /* First see if this insn remains valid when we make the
3429 change. If not, try to replace the whole pattern with
3430 a simple set (this may help if the original insn was a
3431 PARALLEL that was only recognized as single_set due to
3432 REG_UNUSED notes). If this isn't valid either, keep
3433 the INSN_CODE the same and let reload fix it up. */
3434 if (!validate_change (insn, &SET_SRC (old_set), new_src, 0))
3436 rtx new_pat = gen_rtx_SET (VOIDmode,
3437 SET_DEST (old_set), new_src);
3439 if (!validate_change (insn, &PATTERN (insn), new_pat, 0))
3440 SET_SRC (old_set) = new_src;
3443 else
3444 break;
3446 val = 1;
3447 /* This can't have an effect on elimination offsets, so skip right
3448 to the end. */
3449 goto done;
3453 /* Determine the effects of this insn on elimination offsets. */
3454 elimination_effects (old_body, VOIDmode);
3456 /* Eliminate all eliminable registers occurring in operands that
3457 can be handled by reload. */
3458 extract_insn (insn);
3459 for (i = 0; i < recog_data.n_operands; i++)
3461 orig_operand[i] = recog_data.operand[i];
3462 substed_operand[i] = recog_data.operand[i];
3464 /* For an asm statement, every operand is eliminable. */
3465 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3467 bool is_set_src, in_plus;
3469 /* Check for setting a register that we know about. */
3470 if (recog_data.operand_type[i] != OP_IN
3471 && REG_P (orig_operand[i]))
3473 /* If we are assigning to a register that can be eliminated, it
3474 must be as part of a PARALLEL, since the code above handles
3475 single SETs. We must indicate that we can no longer
3476 eliminate this reg. */
3477 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3478 ep++)
3479 if (ep->from_rtx == orig_operand[i])
3480 ep->can_eliminate = 0;
3483 /* Companion to the above plus substitution, we can allow
3484 invariants as the source of a plain move. */
3485 is_set_src = false;
3486 if (old_set
3487 && recog_data.operand_loc[i] == &SET_SRC (old_set))
3488 is_set_src = true;
3489 in_plus = false;
3490 if (plus_src
3491 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3492 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3493 in_plus = true;
3495 substed_operand[i]
3496 = eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3497 replace ? insn : NULL_RTX,
3498 is_set_src || in_plus, false);
3499 if (substed_operand[i] != orig_operand[i])
3500 val = 1;
3501 /* Terminate the search in check_eliminable_occurrences at
3502 this point. */
3503 *recog_data.operand_loc[i] = 0;
3505 /* If an output operand changed from a REG to a MEM and INSN is an
3506 insn, write a CLOBBER insn. */
3507 if (recog_data.operand_type[i] != OP_IN
3508 && REG_P (orig_operand[i])
3509 && MEM_P (substed_operand[i])
3510 && replace)
3511 emit_insn_after (gen_clobber (orig_operand[i]), insn);
3515 for (i = 0; i < recog_data.n_dups; i++)
3516 *recog_data.dup_loc[i]
3517 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3519 /* If any eliminable remain, they aren't eliminable anymore. */
3520 check_eliminable_occurrences (old_body);
3522 /* Substitute the operands; the new values are in the substed_operand
3523 array. */
3524 for (i = 0; i < recog_data.n_operands; i++)
3525 *recog_data.operand_loc[i] = substed_operand[i];
3526 for (i = 0; i < recog_data.n_dups; i++)
3527 *recog_data.dup_loc[i] = substed_operand[(int) recog_data.dup_num[i]];
3529 /* If we are replacing a body that was a (set X (plus Y Z)), try to
3530 re-recognize the insn. We do this in case we had a simple addition
3531 but now can do this as a load-address. This saves an insn in this
3532 common case.
3533 If re-recognition fails, the old insn code number will still be used,
3534 and some register operands may have changed into PLUS expressions.
3535 These will be handled by find_reloads by loading them into a register
3536 again. */
3538 if (val)
3540 /* If we aren't replacing things permanently and we changed something,
3541 make another copy to ensure that all the RTL is new. Otherwise
3542 things can go wrong if find_reload swaps commutative operands
3543 and one is inside RTL that has been copied while the other is not. */
3544 new_body = old_body;
3545 if (! replace)
3547 new_body = copy_insn (old_body);
3548 if (REG_NOTES (insn))
3549 REG_NOTES (insn) = copy_insn_1 (REG_NOTES (insn));
3551 PATTERN (insn) = new_body;
3553 /* If we had a move insn but now we don't, rerecognize it. This will
3554 cause spurious re-recognition if the old move had a PARALLEL since
3555 the new one still will, but we can't call single_set without
3556 having put NEW_BODY into the insn and the re-recognition won't
3557 hurt in this rare case. */
3558 /* ??? Why this huge if statement - why don't we just rerecognize the
3559 thing always? */
3560 if (! insn_is_asm
3561 && old_set != 0
3562 && ((REG_P (SET_SRC (old_set))
3563 && (GET_CODE (new_body) != SET
3564 || !REG_P (SET_SRC (new_body))))
3565 /* If this was a load from or store to memory, compare
3566 the MEM in recog_data.operand to the one in the insn.
3567 If they are not equal, then rerecognize the insn. */
3568 || (old_set != 0
3569 && ((MEM_P (SET_SRC (old_set))
3570 && SET_SRC (old_set) != recog_data.operand[1])
3571 || (MEM_P (SET_DEST (old_set))
3572 && SET_DEST (old_set) != recog_data.operand[0])))
3573 /* If this was an add insn before, rerecognize. */
3574 || GET_CODE (SET_SRC (old_set)) == PLUS))
3576 int new_icode = recog (PATTERN (insn), insn, 0);
3577 if (new_icode >= 0)
3578 INSN_CODE (insn) = new_icode;
3582 /* Restore the old body. If there were any changes to it, we made a copy
3583 of it while the changes were still in place, so we'll correctly return
3584 a modified insn below. */
3585 if (! replace)
3587 /* Restore the old body. */
3588 for (i = 0; i < recog_data.n_operands; i++)
3589 /* Restoring a top-level match_parallel would clobber the new_body
3590 we installed in the insn. */
3591 if (recog_data.operand_loc[i] != &PATTERN (insn))
3592 *recog_data.operand_loc[i] = orig_operand[i];
3593 for (i = 0; i < recog_data.n_dups; i++)
3594 *recog_data.dup_loc[i] = orig_operand[(int) recog_data.dup_num[i]];
3597 /* Update all elimination pairs to reflect the status after the current
3598 insn. The changes we make were determined by the earlier call to
3599 elimination_effects.
3601 We also detect cases where register elimination cannot be done,
3602 namely, if a register would be both changed and referenced outside a MEM
3603 in the resulting insn since such an insn is often undefined and, even if
3604 not, we cannot know what meaning will be given to it. Note that it is
3605 valid to have a register used in an address in an insn that changes it
3606 (presumably with a pre- or post-increment or decrement).
3608 If anything changes, return nonzero. */
3610 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3612 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3613 ep->can_eliminate = 0;
3615 ep->ref_outside_mem = 0;
3617 if (ep->previous_offset != ep->offset)
3618 val = 1;
3621 done:
3622 /* If we changed something, perform elimination in REG_NOTES. This is
3623 needed even when REPLACE is zero because a REG_DEAD note might refer
3624 to a register that we eliminate and could cause a different number
3625 of spill registers to be needed in the final reload pass than in
3626 the pre-passes. */
3627 if (val && REG_NOTES (insn) != 0)
3628 REG_NOTES (insn)
3629 = eliminate_regs_1 (REG_NOTES (insn), VOIDmode, REG_NOTES (insn), true,
3630 false);
3632 return val;
3635 /* Like eliminate_regs_in_insn, but only estimate costs for the use of the
3636 register allocator. INSN is the instruction we need to examine, we perform
3637 eliminations in its operands and record cases where eliminating a reg with
3638 an invariant equivalence would add extra cost. */
3640 static void
3641 elimination_costs_in_insn (rtx insn)
3643 int icode = recog_memoized (insn);
3644 rtx old_body = PATTERN (insn);
3645 int insn_is_asm = asm_noperands (old_body) >= 0;
3646 rtx old_set = single_set (insn);
3647 int i;
3648 rtx orig_operand[MAX_RECOG_OPERANDS];
3649 rtx orig_dup[MAX_RECOG_OPERANDS];
3650 struct elim_table *ep;
3651 rtx plus_src, plus_cst_src;
3652 bool sets_reg_p;
3654 if (! insn_is_asm && icode < 0)
3656 gcc_assert (GET_CODE (PATTERN (insn)) == USE
3657 || GET_CODE (PATTERN (insn)) == CLOBBER
3658 || GET_CODE (PATTERN (insn)) == ADDR_VEC
3659 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3660 || GET_CODE (PATTERN (insn)) == ASM_INPUT
3661 || DEBUG_INSN_P (insn));
3662 return;
3665 if (old_set != 0 && REG_P (SET_DEST (old_set))
3666 && REGNO (SET_DEST (old_set)) < FIRST_PSEUDO_REGISTER)
3668 /* Check for setting an eliminable register. */
3669 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3670 if (ep->from_rtx == SET_DEST (old_set) && ep->can_eliminate)
3671 return;
3674 /* We allow one special case which happens to work on all machines we
3675 currently support: a single set with the source or a REG_EQUAL
3676 note being a PLUS of an eliminable register and a constant. */
3677 plus_src = plus_cst_src = 0;
3678 sets_reg_p = false;
3679 if (old_set && REG_P (SET_DEST (old_set)))
3681 sets_reg_p = true;
3682 if (GET_CODE (SET_SRC (old_set)) == PLUS)
3683 plus_src = SET_SRC (old_set);
3684 /* First see if the source is of the form (plus (...) CST). */
3685 if (plus_src
3686 && CONST_INT_P (XEXP (plus_src, 1)))
3687 plus_cst_src = plus_src;
3688 else if (REG_P (SET_SRC (old_set))
3689 || plus_src)
3691 /* Otherwise, see if we have a REG_EQUAL note of the form
3692 (plus (...) CST). */
3693 rtx links;
3694 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
3696 if ((REG_NOTE_KIND (links) == REG_EQUAL
3697 || REG_NOTE_KIND (links) == REG_EQUIV)
3698 && GET_CODE (XEXP (links, 0)) == PLUS
3699 && CONST_INT_P (XEXP (XEXP (links, 0), 1)))
3701 plus_cst_src = XEXP (links, 0);
3702 break;
3708 /* Determine the effects of this insn on elimination offsets. */
3709 elimination_effects (old_body, VOIDmode);
3711 /* Eliminate all eliminable registers occurring in operands that
3712 can be handled by reload. */
3713 extract_insn (insn);
3714 for (i = 0; i < recog_data.n_dups; i++)
3715 orig_dup[i] = *recog_data.dup_loc[i];
3717 for (i = 0; i < recog_data.n_operands; i++)
3719 orig_operand[i] = recog_data.operand[i];
3721 /* For an asm statement, every operand is eliminable. */
3722 if (insn_is_asm || insn_data[icode].operand[i].eliminable)
3724 bool is_set_src, in_plus;
3726 /* Check for setting a register that we know about. */
3727 if (recog_data.operand_type[i] != OP_IN
3728 && REG_P (orig_operand[i]))
3730 /* If we are assigning to a register that can be eliminated, it
3731 must be as part of a PARALLEL, since the code above handles
3732 single SETs. We must indicate that we can no longer
3733 eliminate this reg. */
3734 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
3735 ep++)
3736 if (ep->from_rtx == orig_operand[i])
3737 ep->can_eliminate = 0;
3740 /* Companion to the above plus substitution, we can allow
3741 invariants as the source of a plain move. */
3742 is_set_src = false;
3743 if (old_set && recog_data.operand_loc[i] == &SET_SRC (old_set))
3744 is_set_src = true;
3745 if (is_set_src && !sets_reg_p)
3746 note_reg_elim_costly (&SET_SRC (old_set), insn);
3747 in_plus = false;
3748 if (plus_src && sets_reg_p
3749 && (recog_data.operand_loc[i] == &XEXP (plus_src, 0)
3750 || recog_data.operand_loc[i] == &XEXP (plus_src, 1)))
3751 in_plus = true;
3753 eliminate_regs_1 (recog_data.operand[i], VOIDmode,
3754 NULL_RTX,
3755 is_set_src || in_plus, true);
3756 /* Terminate the search in check_eliminable_occurrences at
3757 this point. */
3758 *recog_data.operand_loc[i] = 0;
3762 for (i = 0; i < recog_data.n_dups; i++)
3763 *recog_data.dup_loc[i]
3764 = *recog_data.operand_loc[(int) recog_data.dup_num[i]];
3766 /* If any eliminable remain, they aren't eliminable anymore. */
3767 check_eliminable_occurrences (old_body);
3769 /* Restore the old body. */
3770 for (i = 0; i < recog_data.n_operands; i++)
3771 *recog_data.operand_loc[i] = orig_operand[i];
3772 for (i = 0; i < recog_data.n_dups; i++)
3773 *recog_data.dup_loc[i] = orig_dup[i];
3775 /* Update all elimination pairs to reflect the status after the current
3776 insn. The changes we make were determined by the earlier call to
3777 elimination_effects. */
3779 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3781 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3782 ep->can_eliminate = 0;
3784 ep->ref_outside_mem = 0;
3787 return;
3790 /* Loop through all elimination pairs.
3791 Recalculate the number not at initial offset.
3793 Compute the maximum offset (minimum offset if the stack does not
3794 grow downward) for each elimination pair. */
3796 static void
3797 update_eliminable_offsets (void)
3799 struct elim_table *ep;
3801 num_not_at_initial_offset = 0;
3802 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3804 ep->previous_offset = ep->offset;
3805 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3806 num_not_at_initial_offset++;
3810 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3811 replacement we currently believe is valid, mark it as not eliminable if X
3812 modifies DEST in any way other than by adding a constant integer to it.
3814 If DEST is the frame pointer, we do nothing because we assume that
3815 all assignments to the hard frame pointer are nonlocal gotos and are being
3816 done at a time when they are valid and do not disturb anything else.
3817 Some machines want to eliminate a fake argument pointer with either the
3818 frame or stack pointer. Assignments to the hard frame pointer must not
3819 prevent this elimination.
3821 Called via note_stores from reload before starting its passes to scan
3822 the insns of the function. */
3824 static void
3825 mark_not_eliminable (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
3827 unsigned int i;
3829 /* A SUBREG of a hard register here is just changing its mode. We should
3830 not see a SUBREG of an eliminable hard register, but check just in
3831 case. */
3832 if (GET_CODE (dest) == SUBREG)
3833 dest = SUBREG_REG (dest);
3835 if (dest == hard_frame_pointer_rtx)
3836 return;
3838 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3839 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3840 && (GET_CODE (x) != SET
3841 || GET_CODE (SET_SRC (x)) != PLUS
3842 || XEXP (SET_SRC (x), 0) != dest
3843 || !CONST_INT_P (XEXP (SET_SRC (x), 1))))
3845 reg_eliminate[i].can_eliminate_previous
3846 = reg_eliminate[i].can_eliminate = 0;
3847 num_eliminable--;
3851 /* Verify that the initial elimination offsets did not change since the
3852 last call to set_initial_elim_offsets. This is used to catch cases
3853 where something illegal happened during reload_as_needed that could
3854 cause incorrect code to be generated if we did not check for it. */
3856 static bool
3857 verify_initial_elim_offsets (void)
3859 HOST_WIDE_INT t;
3861 if (!num_eliminable)
3862 return true;
3864 #ifdef ELIMINABLE_REGS
3866 struct elim_table *ep;
3868 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3870 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, t);
3871 if (t != ep->initial_offset)
3872 return false;
3875 #else
3876 INITIAL_FRAME_POINTER_OFFSET (t);
3877 if (t != reg_eliminate[0].initial_offset)
3878 return false;
3879 #endif
3881 return true;
3884 /* Reset all offsets on eliminable registers to their initial values. */
3886 static void
3887 set_initial_elim_offsets (void)
3889 struct elim_table *ep = reg_eliminate;
3891 #ifdef ELIMINABLE_REGS
3892 for (; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3894 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
3895 ep->previous_offset = ep->offset = ep->initial_offset;
3897 #else
3898 INITIAL_FRAME_POINTER_OFFSET (ep->initial_offset);
3899 ep->previous_offset = ep->offset = ep->initial_offset;
3900 #endif
3902 num_not_at_initial_offset = 0;
3905 /* Subroutine of set_initial_label_offsets called via for_each_eh_label. */
3907 static void
3908 set_initial_eh_label_offset (rtx label)
3910 set_label_offsets (label, NULL_RTX, 1);
3913 /* Initialize the known label offsets.
3914 Set a known offset for each forced label to be at the initial offset
3915 of each elimination. We do this because we assume that all
3916 computed jumps occur from a location where each elimination is
3917 at its initial offset.
3918 For all other labels, show that we don't know the offsets. */
3920 static void
3921 set_initial_label_offsets (void)
3923 rtx x;
3924 memset (offsets_known_at, 0, num_labels);
3926 for (x = forced_labels; x; x = XEXP (x, 1))
3927 if (XEXP (x, 0))
3928 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3930 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
3931 if (XEXP (x, 0))
3932 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
3934 for_each_eh_label (set_initial_eh_label_offset);
3937 /* Set all elimination offsets to the known values for the code label given
3938 by INSN. */
3940 static void
3941 set_offsets_for_label (rtx insn)
3943 unsigned int i;
3944 int label_nr = CODE_LABEL_NUMBER (insn);
3945 struct elim_table *ep;
3947 num_not_at_initial_offset = 0;
3948 for (i = 0, ep = reg_eliminate; i < NUM_ELIMINABLE_REGS; ep++, i++)
3950 ep->offset = ep->previous_offset
3951 = offsets_at[label_nr - first_label_num][i];
3952 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3953 num_not_at_initial_offset++;
3957 /* See if anything that happened changes which eliminations are valid.
3958 For example, on the SPARC, whether or not the frame pointer can
3959 be eliminated can depend on what registers have been used. We need
3960 not check some conditions again (such as flag_omit_frame_pointer)
3961 since they can't have changed. */
3963 static void
3964 update_eliminables (HARD_REG_SET *pset)
3966 int previous_frame_pointer_needed = frame_pointer_needed;
3967 struct elim_table *ep;
3969 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3970 if ((ep->from == HARD_FRAME_POINTER_REGNUM
3971 && targetm.frame_pointer_required ())
3972 #ifdef ELIMINABLE_REGS
3973 || ! targetm.can_eliminate (ep->from, ep->to)
3974 #endif
3976 ep->can_eliminate = 0;
3978 /* Look for the case where we have discovered that we can't replace
3979 register A with register B and that means that we will now be
3980 trying to replace register A with register C. This means we can
3981 no longer replace register C with register B and we need to disable
3982 such an elimination, if it exists. This occurs often with A == ap,
3983 B == sp, and C == fp. */
3985 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3987 struct elim_table *op;
3988 int new_to = -1;
3990 if (! ep->can_eliminate && ep->can_eliminate_previous)
3992 /* Find the current elimination for ep->from, if there is a
3993 new one. */
3994 for (op = reg_eliminate;
3995 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
3996 if (op->from == ep->from && op->can_eliminate)
3998 new_to = op->to;
3999 break;
4002 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
4003 disable it. */
4004 for (op = reg_eliminate;
4005 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
4006 if (op->from == new_to && op->to == ep->to)
4007 op->can_eliminate = 0;
4011 /* See if any registers that we thought we could eliminate the previous
4012 time are no longer eliminable. If so, something has changed and we
4013 must spill the register. Also, recompute the number of eliminable
4014 registers and see if the frame pointer is needed; it is if there is
4015 no elimination of the frame pointer that we can perform. */
4017 frame_pointer_needed = 1;
4018 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4020 if (ep->can_eliminate
4021 && ep->from == FRAME_POINTER_REGNUM
4022 && ep->to != HARD_FRAME_POINTER_REGNUM
4023 && (! SUPPORTS_STACK_ALIGNMENT
4024 || ! crtl->stack_realign_needed))
4025 frame_pointer_needed = 0;
4027 if (! ep->can_eliminate && ep->can_eliminate_previous)
4029 ep->can_eliminate_previous = 0;
4030 SET_HARD_REG_BIT (*pset, ep->from);
4031 num_eliminable--;
4035 /* If we didn't need a frame pointer last time, but we do now, spill
4036 the hard frame pointer. */
4037 if (frame_pointer_needed && ! previous_frame_pointer_needed)
4038 SET_HARD_REG_BIT (*pset, HARD_FRAME_POINTER_REGNUM);
4041 /* Return true if X is used as the target register of an elimination. */
4043 bool
4044 elimination_target_reg_p (rtx x)
4046 struct elim_table *ep;
4048 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4049 if (ep->to_rtx == x && ep->can_eliminate)
4050 return true;
4052 return false;
4055 /* Initialize the table of registers to eliminate.
4056 Pre-condition: global flag frame_pointer_needed has been set before
4057 calling this function. */
4059 static void
4060 init_elim_table (void)
4062 struct elim_table *ep;
4063 #ifdef ELIMINABLE_REGS
4064 const struct elim_table_1 *ep1;
4065 #endif
4067 if (!reg_eliminate)
4068 reg_eliminate = XCNEWVEC (struct elim_table, NUM_ELIMINABLE_REGS);
4070 num_eliminable = 0;
4072 #ifdef ELIMINABLE_REGS
4073 for (ep = reg_eliminate, ep1 = reg_eliminate_1;
4074 ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++, ep1++)
4076 ep->from = ep1->from;
4077 ep->to = ep1->to;
4078 ep->can_eliminate = ep->can_eliminate_previous
4079 = (targetm.can_eliminate (ep->from, ep->to)
4080 && ! (ep->to == STACK_POINTER_REGNUM
4081 && frame_pointer_needed
4082 && (! SUPPORTS_STACK_ALIGNMENT
4083 || ! stack_realign_fp)));
4085 #else
4086 reg_eliminate[0].from = reg_eliminate_1[0].from;
4087 reg_eliminate[0].to = reg_eliminate_1[0].to;
4088 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
4089 = ! frame_pointer_needed;
4090 #endif
4092 /* Count the number of eliminable registers and build the FROM and TO
4093 REG rtx's. Note that code in gen_rtx_REG will cause, e.g.,
4094 gen_rtx_REG (Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
4095 We depend on this. */
4096 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
4098 num_eliminable += ep->can_eliminate;
4099 ep->from_rtx = gen_rtx_REG (Pmode, ep->from);
4100 ep->to_rtx = gen_rtx_REG (Pmode, ep->to);
4104 /* Find all the pseudo registers that didn't get hard regs
4105 but do have known equivalent constants or memory slots.
4106 These include parameters (known equivalent to parameter slots)
4107 and cse'd or loop-moved constant memory addresses.
4109 Record constant equivalents in reg_equiv_constant
4110 so they will be substituted by find_reloads.
4111 Record memory equivalents in reg_mem_equiv so they can
4112 be substituted eventually by altering the REG-rtx's. */
4114 static void
4115 init_eliminable_invariants (rtx first, bool do_subregs)
4117 int i;
4118 rtx insn;
4120 grow_reg_equivs ();
4121 if (do_subregs)
4122 reg_max_ref_width = XCNEWVEC (unsigned int, max_regno);
4123 else
4124 reg_max_ref_width = NULL;
4126 num_eliminable_invariants = 0;
4128 first_label_num = get_first_label_num ();
4129 num_labels = max_label_num () - first_label_num;
4131 /* Allocate the tables used to store offset information at labels. */
4132 offsets_known_at = XNEWVEC (char, num_labels);
4133 offsets_at = (HOST_WIDE_INT (*)[NUM_ELIMINABLE_REGS]) xmalloc (num_labels * NUM_ELIMINABLE_REGS * sizeof (HOST_WIDE_INT));
4135 /* Look for REG_EQUIV notes; record what each pseudo is equivalent
4136 to. If DO_SUBREGS is true, also find all paradoxical subregs and
4137 find largest such for each pseudo. FIRST is the head of the insn
4138 list. */
4140 for (insn = first; insn; insn = NEXT_INSN (insn))
4142 rtx set = single_set (insn);
4144 /* We may introduce USEs that we want to remove at the end, so
4145 we'll mark them with QImode. Make sure there are no
4146 previously-marked insns left by say regmove. */
4147 if (INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
4148 && GET_MODE (insn) != VOIDmode)
4149 PUT_MODE (insn, VOIDmode);
4151 if (do_subregs && NONDEBUG_INSN_P (insn))
4152 scan_paradoxical_subregs (PATTERN (insn));
4154 if (set != 0 && REG_P (SET_DEST (set)))
4156 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
4157 rtx x;
4159 if (! note)
4160 continue;
4162 i = REGNO (SET_DEST (set));
4163 x = XEXP (note, 0);
4165 if (i <= LAST_VIRTUAL_REGISTER)
4166 continue;
4168 /* If flag_pic and we have constant, verify it's legitimate. */
4169 if (!CONSTANT_P (x)
4170 || !flag_pic || LEGITIMATE_PIC_OPERAND_P (x))
4172 /* It can happen that a REG_EQUIV note contains a MEM
4173 that is not a legitimate memory operand. As later
4174 stages of reload assume that all addresses found
4175 in the reg_equiv_* arrays were originally legitimate,
4176 we ignore such REG_EQUIV notes. */
4177 if (memory_operand (x, VOIDmode))
4179 /* Always unshare the equivalence, so we can
4180 substitute into this insn without touching the
4181 equivalence. */
4182 reg_equiv_memory_loc (i) = copy_rtx (x);
4184 else if (function_invariant_p (x))
4186 enum machine_mode mode;
4188 mode = GET_MODE (SET_DEST (set));
4189 if (GET_CODE (x) == PLUS)
4191 /* This is PLUS of frame pointer and a constant,
4192 and might be shared. Unshare it. */
4193 reg_equiv_invariant (i) = copy_rtx (x);
4194 num_eliminable_invariants++;
4196 else if (x == frame_pointer_rtx || x == arg_pointer_rtx)
4198 reg_equiv_invariant (i) = x;
4199 num_eliminable_invariants++;
4201 else if (targetm.legitimate_constant_p (mode, x))
4202 reg_equiv_constant (i) = x;
4203 else
4205 reg_equiv_memory_loc (i) = force_const_mem (mode, x);
4206 if (! reg_equiv_memory_loc (i))
4207 reg_equiv_init (i) = NULL_RTX;
4210 else
4212 reg_equiv_init (i) = NULL_RTX;
4213 continue;
4216 else
4217 reg_equiv_init (i) = NULL_RTX;
4221 if (dump_file)
4222 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4223 if (reg_equiv_init (i))
4225 fprintf (dump_file, "init_insns for %u: ", i);
4226 print_inline_rtx (dump_file, reg_equiv_init (i), 20);
4227 fprintf (dump_file, "\n");
4231 /* Indicate that we no longer have known memory locations or constants.
4232 Free all data involved in tracking these. */
4234 static void
4235 free_reg_equiv (void)
4237 int i;
4240 free (offsets_known_at);
4241 free (offsets_at);
4242 offsets_at = 0;
4243 offsets_known_at = 0;
4245 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4246 if (reg_equiv_alt_mem_list (i))
4247 free_EXPR_LIST_list (&reg_equiv_alt_mem_list (i));
4248 VEC_free (reg_equivs_t, gc, reg_equivs);
4249 reg_equivs = NULL;
4253 /* Kick all pseudos out of hard register REGNO.
4255 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
4256 because we found we can't eliminate some register. In the case, no pseudos
4257 are allowed to be in the register, even if they are only in a block that
4258 doesn't require spill registers, unlike the case when we are spilling this
4259 hard reg to produce another spill register.
4261 Return nonzero if any pseudos needed to be kicked out. */
4263 static void
4264 spill_hard_reg (unsigned int regno, int cant_eliminate)
4266 int i;
4268 if (cant_eliminate)
4270 SET_HARD_REG_BIT (bad_spill_regs_global, regno);
4271 df_set_regs_ever_live (regno, true);
4274 /* Spill every pseudo reg that was allocated to this reg
4275 or to something that overlaps this reg. */
4277 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
4278 if (reg_renumber[i] >= 0
4279 && (unsigned int) reg_renumber[i] <= regno
4280 && end_hard_regno (PSEUDO_REGNO_MODE (i), reg_renumber[i]) > regno)
4281 SET_REGNO_REG_SET (&spilled_pseudos, i);
4284 /* After find_reload_regs has been run for all insn that need reloads,
4285 and/or spill_hard_regs was called, this function is used to actually
4286 spill pseudo registers and try to reallocate them. It also sets up the
4287 spill_regs array for use by choose_reload_regs. */
4289 static int
4290 finish_spills (int global)
4292 struct insn_chain *chain;
4293 int something_changed = 0;
4294 unsigned i;
4295 reg_set_iterator rsi;
4297 /* Build the spill_regs array for the function. */
4298 /* If there are some registers still to eliminate and one of the spill regs
4299 wasn't ever used before, additional stack space may have to be
4300 allocated to store this register. Thus, we may have changed the offset
4301 between the stack and frame pointers, so mark that something has changed.
4303 One might think that we need only set VAL to 1 if this is a call-used
4304 register. However, the set of registers that must be saved by the
4305 prologue is not identical to the call-used set. For example, the
4306 register used by the call insn for the return PC is a call-used register,
4307 but must be saved by the prologue. */
4309 n_spills = 0;
4310 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4311 if (TEST_HARD_REG_BIT (used_spill_regs, i))
4313 spill_reg_order[i] = n_spills;
4314 spill_regs[n_spills++] = i;
4315 if (num_eliminable && ! df_regs_ever_live_p (i))
4316 something_changed = 1;
4317 df_set_regs_ever_live (i, true);
4319 else
4320 spill_reg_order[i] = -1;
4322 EXECUTE_IF_SET_IN_REG_SET (&spilled_pseudos, FIRST_PSEUDO_REGISTER, i, rsi)
4323 if (! ira_conflicts_p || reg_renumber[i] >= 0)
4325 /* Record the current hard register the pseudo is allocated to
4326 in pseudo_previous_regs so we avoid reallocating it to the
4327 same hard reg in a later pass. */
4328 gcc_assert (reg_renumber[i] >= 0);
4330 SET_HARD_REG_BIT (pseudo_previous_regs[i], reg_renumber[i]);
4331 /* Mark it as no longer having a hard register home. */
4332 reg_renumber[i] = -1;
4333 if (ira_conflicts_p)
4334 /* Inform IRA about the change. */
4335 ira_mark_allocation_change (i);
4336 /* We will need to scan everything again. */
4337 something_changed = 1;
4340 /* Retry global register allocation if possible. */
4341 if (global && ira_conflicts_p)
4343 unsigned int n;
4345 memset (pseudo_forbidden_regs, 0, max_regno * sizeof (HARD_REG_SET));
4346 /* For every insn that needs reloads, set the registers used as spill
4347 regs in pseudo_forbidden_regs for every pseudo live across the
4348 insn. */
4349 for (chain = insns_need_reload; chain; chain = chain->next_need_reload)
4351 EXECUTE_IF_SET_IN_REG_SET
4352 (&chain->live_throughout, FIRST_PSEUDO_REGISTER, i, rsi)
4354 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4355 chain->used_spill_regs);
4357 EXECUTE_IF_SET_IN_REG_SET
4358 (&chain->dead_or_set, FIRST_PSEUDO_REGISTER, i, rsi)
4360 IOR_HARD_REG_SET (pseudo_forbidden_regs[i],
4361 chain->used_spill_regs);
4365 /* Retry allocating the pseudos spilled in IRA and the
4366 reload. For each reg, merge the various reg sets that
4367 indicate which hard regs can't be used, and call
4368 ira_reassign_pseudos. */
4369 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
4370 if (reg_old_renumber[i] != reg_renumber[i])
4372 if (reg_renumber[i] < 0)
4373 temp_pseudo_reg_arr[n++] = i;
4374 else
4375 CLEAR_REGNO_REG_SET (&spilled_pseudos, i);
4377 if (ira_reassign_pseudos (temp_pseudo_reg_arr, n,
4378 bad_spill_regs_global,
4379 pseudo_forbidden_regs, pseudo_previous_regs,
4380 &spilled_pseudos))
4381 something_changed = 1;
4383 /* Fix up the register information in the insn chain.
4384 This involves deleting those of the spilled pseudos which did not get
4385 a new hard register home from the live_{before,after} sets. */
4386 for (chain = reload_insn_chain; chain; chain = chain->next)
4388 HARD_REG_SET used_by_pseudos;
4389 HARD_REG_SET used_by_pseudos2;
4391 if (! ira_conflicts_p)
4393 /* Don't do it for IRA because IRA and the reload still can
4394 assign hard registers to the spilled pseudos on next
4395 reload iterations. */
4396 AND_COMPL_REG_SET (&chain->live_throughout, &spilled_pseudos);
4397 AND_COMPL_REG_SET (&chain->dead_or_set, &spilled_pseudos);
4399 /* Mark any unallocated hard regs as available for spills. That
4400 makes inheritance work somewhat better. */
4401 if (chain->need_reload)
4403 REG_SET_TO_HARD_REG_SET (used_by_pseudos, &chain->live_throughout);
4404 REG_SET_TO_HARD_REG_SET (used_by_pseudos2, &chain->dead_or_set);
4405 IOR_HARD_REG_SET (used_by_pseudos, used_by_pseudos2);
4407 compute_use_by_pseudos (&used_by_pseudos, &chain->live_throughout);
4408 compute_use_by_pseudos (&used_by_pseudos, &chain->dead_or_set);
4409 /* Value of chain->used_spill_regs from previous iteration
4410 may be not included in the value calculated here because
4411 of possible removing caller-saves insns (see function
4412 delete_caller_save_insns. */
4413 COMPL_HARD_REG_SET (chain->used_spill_regs, used_by_pseudos);
4414 AND_HARD_REG_SET (chain->used_spill_regs, used_spill_regs);
4418 CLEAR_REG_SET (&changed_allocation_pseudos);
4419 /* Let alter_reg modify the reg rtx's for the modified pseudos. */
4420 for (i = FIRST_PSEUDO_REGISTER; i < (unsigned)max_regno; i++)
4422 int regno = reg_renumber[i];
4423 if (reg_old_renumber[i] == regno)
4424 continue;
4426 SET_REGNO_REG_SET (&changed_allocation_pseudos, i);
4428 alter_reg (i, reg_old_renumber[i], false);
4429 reg_old_renumber[i] = regno;
4430 if (dump_file)
4432 if (regno == -1)
4433 fprintf (dump_file, " Register %d now on stack.\n\n", i);
4434 else
4435 fprintf (dump_file, " Register %d now in %d.\n\n",
4436 i, reg_renumber[i]);
4440 return something_changed;
4443 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
4445 static void
4446 scan_paradoxical_subregs (rtx x)
4448 int i;
4449 const char *fmt;
4450 enum rtx_code code = GET_CODE (x);
4452 switch (code)
4454 case REG:
4455 case CONST_INT:
4456 case CONST:
4457 case SYMBOL_REF:
4458 case LABEL_REF:
4459 case CONST_DOUBLE:
4460 case CONST_FIXED:
4461 case CONST_VECTOR: /* shouldn't happen, but just in case. */
4462 case CC0:
4463 case PC:
4464 case USE:
4465 case CLOBBER:
4466 return;
4468 case SUBREG:
4469 if (REG_P (SUBREG_REG (x))
4470 && (GET_MODE_SIZE (GET_MODE (x))
4471 > reg_max_ref_width[REGNO (SUBREG_REG (x))]))
4473 reg_max_ref_width[REGNO (SUBREG_REG (x))]
4474 = GET_MODE_SIZE (GET_MODE (x));
4475 mark_home_live_1 (REGNO (SUBREG_REG (x)), GET_MODE (x));
4477 return;
4479 default:
4480 break;
4483 fmt = GET_RTX_FORMAT (code);
4484 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4486 if (fmt[i] == 'e')
4487 scan_paradoxical_subregs (XEXP (x, i));
4488 else if (fmt[i] == 'E')
4490 int j;
4491 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4492 scan_paradoxical_subregs (XVECEXP (x, i, j));
4497 /* *OP_PTR and *OTHER_PTR are two operands to a conceptual reload.
4498 If *OP_PTR is a paradoxical subreg, try to remove that subreg
4499 and apply the corresponding narrowing subreg to *OTHER_PTR.
4500 Return true if the operands were changed, false otherwise. */
4502 static bool
4503 strip_paradoxical_subreg (rtx *op_ptr, rtx *other_ptr)
4505 rtx op, inner, other, tem;
4507 op = *op_ptr;
4508 if (!paradoxical_subreg_p (op))
4509 return false;
4510 inner = SUBREG_REG (op);
4512 other = *other_ptr;
4513 tem = gen_lowpart_common (GET_MODE (inner), other);
4514 if (!tem)
4515 return false;
4517 /* If the lowpart operation turned a hard register into a subreg,
4518 rather than simplifying it to another hard register, then the
4519 mode change cannot be properly represented. For example, OTHER
4520 might be valid in its current mode, but not in the new one. */
4521 if (GET_CODE (tem) == SUBREG
4522 && REG_P (other)
4523 && HARD_REGISTER_P (other))
4524 return false;
4526 *op_ptr = inner;
4527 *other_ptr = tem;
4528 return true;
4531 /* A subroutine of reload_as_needed. If INSN has a REG_EH_REGION note,
4532 examine all of the reload insns between PREV and NEXT exclusive, and
4533 annotate all that may trap. */
4535 static void
4536 fixup_eh_region_note (rtx insn, rtx prev, rtx next)
4538 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
4539 if (note == NULL)
4540 return;
4541 if (!insn_could_throw_p (insn))
4542 remove_note (insn, note);
4543 copy_reg_eh_region_note_forward (note, NEXT_INSN (prev), next);
4546 /* Reload pseudo-registers into hard regs around each insn as needed.
4547 Additional register load insns are output before the insn that needs it
4548 and perhaps store insns after insns that modify the reloaded pseudo reg.
4550 reg_last_reload_reg and reg_reloaded_contents keep track of
4551 which registers are already available in reload registers.
4552 We update these for the reloads that we perform,
4553 as the insns are scanned. */
4555 static void
4556 reload_as_needed (int live_known)
4558 struct insn_chain *chain;
4559 #if defined (AUTO_INC_DEC)
4560 int i;
4561 #endif
4562 rtx x, marker;
4564 memset (spill_reg_rtx, 0, sizeof spill_reg_rtx);
4565 memset (spill_reg_store, 0, sizeof spill_reg_store);
4566 reg_last_reload_reg = XCNEWVEC (rtx, max_regno);
4567 INIT_REG_SET (&reg_has_output_reload);
4568 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4569 CLEAR_HARD_REG_SET (reg_reloaded_call_part_clobbered);
4571 set_initial_elim_offsets ();
4573 /* Generate a marker insn that we will move around. */
4574 marker = emit_note (NOTE_INSN_DELETED);
4575 unlink_insn_chain (marker, marker);
4577 for (chain = reload_insn_chain; chain; chain = chain->next)
4579 rtx prev = 0;
4580 rtx insn = chain->insn;
4581 rtx old_next = NEXT_INSN (insn);
4582 #ifdef AUTO_INC_DEC
4583 rtx old_prev = PREV_INSN (insn);
4584 #endif
4586 /* If we pass a label, copy the offsets from the label information
4587 into the current offsets of each elimination. */
4588 if (LABEL_P (insn))
4589 set_offsets_for_label (insn);
4591 else if (INSN_P (insn))
4593 regset_head regs_to_forget;
4594 INIT_REG_SET (&regs_to_forget);
4595 note_stores (PATTERN (insn), forget_old_reloads_1, &regs_to_forget);
4597 /* If this is a USE and CLOBBER of a MEM, ensure that any
4598 references to eliminable registers have been removed. */
4600 if ((GET_CODE (PATTERN (insn)) == USE
4601 || GET_CODE (PATTERN (insn)) == CLOBBER)
4602 && MEM_P (XEXP (PATTERN (insn), 0)))
4603 XEXP (XEXP (PATTERN (insn), 0), 0)
4604 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
4605 GET_MODE (XEXP (PATTERN (insn), 0)),
4606 NULL_RTX);
4608 /* If we need to do register elimination processing, do so.
4609 This might delete the insn, in which case we are done. */
4610 if ((num_eliminable || num_eliminable_invariants) && chain->need_elim)
4612 eliminate_regs_in_insn (insn, 1);
4613 if (NOTE_P (insn))
4615 update_eliminable_offsets ();
4616 CLEAR_REG_SET (&regs_to_forget);
4617 continue;
4621 /* If need_elim is nonzero but need_reload is zero, one might think
4622 that we could simply set n_reloads to 0. However, find_reloads
4623 could have done some manipulation of the insn (such as swapping
4624 commutative operands), and these manipulations are lost during
4625 the first pass for every insn that needs register elimination.
4626 So the actions of find_reloads must be redone here. */
4628 if (! chain->need_elim && ! chain->need_reload
4629 && ! chain->need_operand_change)
4630 n_reloads = 0;
4631 /* First find the pseudo regs that must be reloaded for this insn.
4632 This info is returned in the tables reload_... (see reload.h).
4633 Also modify the body of INSN by substituting RELOAD
4634 rtx's for those pseudo regs. */
4635 else
4637 CLEAR_REG_SET (&reg_has_output_reload);
4638 CLEAR_HARD_REG_SET (reg_is_output_reload);
4640 find_reloads (insn, 1, spill_indirect_levels, live_known,
4641 spill_reg_order);
4644 if (n_reloads > 0)
4646 rtx next = NEXT_INSN (insn);
4647 rtx p;
4649 /* ??? PREV can get deleted by reload inheritance.
4650 Work around this by emitting a marker note. */
4651 prev = PREV_INSN (insn);
4652 reorder_insns_nobb (marker, marker, prev);
4654 /* Now compute which reload regs to reload them into. Perhaps
4655 reusing reload regs from previous insns, or else output
4656 load insns to reload them. Maybe output store insns too.
4657 Record the choices of reload reg in reload_reg_rtx. */
4658 choose_reload_regs (chain);
4660 /* Generate the insns to reload operands into or out of
4661 their reload regs. */
4662 emit_reload_insns (chain);
4664 /* Substitute the chosen reload regs from reload_reg_rtx
4665 into the insn's body (or perhaps into the bodies of other
4666 load and store insn that we just made for reloading
4667 and that we moved the structure into). */
4668 subst_reloads (insn);
4670 prev = PREV_INSN (marker);
4671 unlink_insn_chain (marker, marker);
4673 /* Adjust the exception region notes for loads and stores. */
4674 if (cfun->can_throw_non_call_exceptions && !CALL_P (insn))
4675 fixup_eh_region_note (insn, prev, next);
4677 /* Adjust the location of REG_ARGS_SIZE. */
4678 p = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4679 if (p)
4681 remove_note (insn, p);
4682 fixup_args_size_notes (prev, PREV_INSN (next),
4683 INTVAL (XEXP (p, 0)));
4686 /* If this was an ASM, make sure that all the reload insns
4687 we have generated are valid. If not, give an error
4688 and delete them. */
4689 if (asm_noperands (PATTERN (insn)) >= 0)
4690 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
4691 if (p != insn && INSN_P (p)
4692 && GET_CODE (PATTERN (p)) != USE
4693 && (recog_memoized (p) < 0
4694 || (extract_insn (p), ! constrain_operands (1))))
4696 error_for_asm (insn,
4697 "%<asm%> operand requires "
4698 "impossible reload");
4699 delete_insn (p);
4703 if (num_eliminable && chain->need_elim)
4704 update_eliminable_offsets ();
4706 /* Any previously reloaded spilled pseudo reg, stored in this insn,
4707 is no longer validly lying around to save a future reload.
4708 Note that this does not detect pseudos that were reloaded
4709 for this insn in order to be stored in
4710 (obeying register constraints). That is correct; such reload
4711 registers ARE still valid. */
4712 forget_marked_reloads (&regs_to_forget);
4713 CLEAR_REG_SET (&regs_to_forget);
4715 /* There may have been CLOBBER insns placed after INSN. So scan
4716 between INSN and NEXT and use them to forget old reloads. */
4717 for (x = NEXT_INSN (insn); x != old_next; x = NEXT_INSN (x))
4718 if (NONJUMP_INSN_P (x) && GET_CODE (PATTERN (x)) == CLOBBER)
4719 note_stores (PATTERN (x), forget_old_reloads_1, NULL);
4721 #ifdef AUTO_INC_DEC
4722 /* Likewise for regs altered by auto-increment in this insn.
4723 REG_INC notes have been changed by reloading:
4724 find_reloads_address_1 records substitutions for them,
4725 which have been performed by subst_reloads above. */
4726 for (i = n_reloads - 1; i >= 0; i--)
4728 rtx in_reg = rld[i].in_reg;
4729 if (in_reg)
4731 enum rtx_code code = GET_CODE (in_reg);
4732 /* PRE_INC / PRE_DEC will have the reload register ending up
4733 with the same value as the stack slot, but that doesn't
4734 hold true for POST_INC / POST_DEC. Either we have to
4735 convert the memory access to a true POST_INC / POST_DEC,
4736 or we can't use the reload register for inheritance. */
4737 if ((code == POST_INC || code == POST_DEC)
4738 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4739 REGNO (rld[i].reg_rtx))
4740 /* Make sure it is the inc/dec pseudo, and not
4741 some other (e.g. output operand) pseudo. */
4742 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4743 == REGNO (XEXP (in_reg, 0))))
4746 rtx reload_reg = rld[i].reg_rtx;
4747 enum machine_mode mode = GET_MODE (reload_reg);
4748 int n = 0;
4749 rtx p;
4751 for (p = PREV_INSN (old_next); p != prev; p = PREV_INSN (p))
4753 /* We really want to ignore REG_INC notes here, so
4754 use PATTERN (p) as argument to reg_set_p . */
4755 if (reg_set_p (reload_reg, PATTERN (p)))
4756 break;
4757 n = count_occurrences (PATTERN (p), reload_reg, 0);
4758 if (! n)
4759 continue;
4760 if (n == 1)
4762 rtx replace_reg
4763 = gen_rtx_fmt_e (code, mode, reload_reg);
4765 validate_replace_rtx_group (reload_reg,
4766 replace_reg, p);
4767 n = verify_changes (0);
4769 /* We must also verify that the constraints
4770 are met after the replacement. Make sure
4771 extract_insn is only called for an insn
4772 where the replacements were found to be
4773 valid so far. */
4774 if (n)
4776 extract_insn (p);
4777 n = constrain_operands (1);
4780 /* If the constraints were not met, then
4781 undo the replacement, else confirm it. */
4782 if (!n)
4783 cancel_changes (0);
4784 else
4785 confirm_change_group ();
4787 break;
4789 if (n == 1)
4791 add_reg_note (p, REG_INC, reload_reg);
4792 /* Mark this as having an output reload so that the
4793 REG_INC processing code below won't invalidate
4794 the reload for inheritance. */
4795 SET_HARD_REG_BIT (reg_is_output_reload,
4796 REGNO (reload_reg));
4797 SET_REGNO_REG_SET (&reg_has_output_reload,
4798 REGNO (XEXP (in_reg, 0)));
4800 else
4801 forget_old_reloads_1 (XEXP (in_reg, 0), NULL_RTX,
4802 NULL);
4804 else if ((code == PRE_INC || code == PRE_DEC)
4805 && TEST_HARD_REG_BIT (reg_reloaded_valid,
4806 REGNO (rld[i].reg_rtx))
4807 /* Make sure it is the inc/dec pseudo, and not
4808 some other (e.g. output operand) pseudo. */
4809 && ((unsigned) reg_reloaded_contents[REGNO (rld[i].reg_rtx)]
4810 == REGNO (XEXP (in_reg, 0))))
4812 SET_HARD_REG_BIT (reg_is_output_reload,
4813 REGNO (rld[i].reg_rtx));
4814 SET_REGNO_REG_SET (&reg_has_output_reload,
4815 REGNO (XEXP (in_reg, 0)));
4817 else if (code == PRE_INC || code == PRE_DEC
4818 || code == POST_INC || code == POST_DEC)
4820 int in_regno = REGNO (XEXP (in_reg, 0));
4822 if (reg_last_reload_reg[in_regno] != NULL_RTX)
4824 int in_hard_regno;
4825 bool forget_p = true;
4827 in_hard_regno = REGNO (reg_last_reload_reg[in_regno]);
4828 if (TEST_HARD_REG_BIT (reg_reloaded_valid,
4829 in_hard_regno))
4831 for (x = old_prev ? NEXT_INSN (old_prev) : insn;
4832 x != old_next;
4833 x = NEXT_INSN (x))
4834 if (x == reg_reloaded_insn[in_hard_regno])
4836 forget_p = false;
4837 break;
4840 /* If for some reasons, we didn't set up
4841 reg_last_reload_reg in this insn,
4842 invalidate inheritance from previous
4843 insns for the incremented/decremented
4844 register. Such registers will be not in
4845 reg_has_output_reload. Invalidate it
4846 also if the corresponding element in
4847 reg_reloaded_insn is also
4848 invalidated. */
4849 if (forget_p)
4850 forget_old_reloads_1 (XEXP (in_reg, 0),
4851 NULL_RTX, NULL);
4856 /* If a pseudo that got a hard register is auto-incremented,
4857 we must purge records of copying it into pseudos without
4858 hard registers. */
4859 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
4860 if (REG_NOTE_KIND (x) == REG_INC)
4862 /* See if this pseudo reg was reloaded in this insn.
4863 If so, its last-reload info is still valid
4864 because it is based on this insn's reload. */
4865 for (i = 0; i < n_reloads; i++)
4866 if (rld[i].out == XEXP (x, 0))
4867 break;
4869 if (i == n_reloads)
4870 forget_old_reloads_1 (XEXP (x, 0), NULL_RTX, NULL);
4872 #endif
4874 /* A reload reg's contents are unknown after a label. */
4875 if (LABEL_P (insn))
4876 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4878 /* Don't assume a reload reg is still good after a call insn
4879 if it is a call-used reg, or if it contains a value that will
4880 be partially clobbered by the call. */
4881 else if (CALL_P (insn))
4883 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, call_used_reg_set);
4884 AND_COMPL_HARD_REG_SET (reg_reloaded_valid, reg_reloaded_call_part_clobbered);
4886 /* If this is a call to a setjmp-type function, we must not
4887 reuse any reload reg contents across the call; that will
4888 just be clobbered by other uses of the register in later
4889 code, before the longjmp. */
4890 if (find_reg_note (insn, REG_SETJMP, NULL_RTX))
4891 CLEAR_HARD_REG_SET (reg_reloaded_valid);
4895 /* Clean up. */
4896 free (reg_last_reload_reg);
4897 CLEAR_REG_SET (&reg_has_output_reload);
4900 /* Discard all record of any value reloaded from X,
4901 or reloaded in X from someplace else;
4902 unless X is an output reload reg of the current insn.
4904 X may be a hard reg (the reload reg)
4905 or it may be a pseudo reg that was reloaded from.
4907 When DATA is non-NULL just mark the registers in regset
4908 to be forgotten later. */
4910 static void
4911 forget_old_reloads_1 (rtx x, const_rtx ignored ATTRIBUTE_UNUSED,
4912 void *data)
4914 unsigned int regno;
4915 unsigned int nr;
4916 regset regs = (regset) data;
4918 /* note_stores does give us subregs of hard regs,
4919 subreg_regno_offset requires a hard reg. */
4920 while (GET_CODE (x) == SUBREG)
4922 /* We ignore the subreg offset when calculating the regno,
4923 because we are using the entire underlying hard register
4924 below. */
4925 x = SUBREG_REG (x);
4928 if (!REG_P (x))
4929 return;
4931 regno = REGNO (x);
4933 if (regno >= FIRST_PSEUDO_REGISTER)
4934 nr = 1;
4935 else
4937 unsigned int i;
4939 nr = hard_regno_nregs[regno][GET_MODE (x)];
4940 /* Storing into a spilled-reg invalidates its contents.
4941 This can happen if a block-local pseudo is allocated to that reg
4942 and it wasn't spilled because this block's total need is 0.
4943 Then some insn might have an optional reload and use this reg. */
4944 if (!regs)
4945 for (i = 0; i < nr; i++)
4946 /* But don't do this if the reg actually serves as an output
4947 reload reg in the current instruction. */
4948 if (n_reloads == 0
4949 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i))
4951 CLEAR_HARD_REG_BIT (reg_reloaded_valid, regno + i);
4952 spill_reg_store[regno + i] = 0;
4956 if (regs)
4957 while (nr-- > 0)
4958 SET_REGNO_REG_SET (regs, regno + nr);
4959 else
4961 /* Since value of X has changed,
4962 forget any value previously copied from it. */
4964 while (nr-- > 0)
4965 /* But don't forget a copy if this is the output reload
4966 that establishes the copy's validity. */
4967 if (n_reloads == 0
4968 || !REGNO_REG_SET_P (&reg_has_output_reload, regno + nr))
4969 reg_last_reload_reg[regno + nr] = 0;
4973 /* Forget the reloads marked in regset by previous function. */
4974 static void
4975 forget_marked_reloads (regset regs)
4977 unsigned int reg;
4978 reg_set_iterator rsi;
4979 EXECUTE_IF_SET_IN_REG_SET (regs, 0, reg, rsi)
4981 if (reg < FIRST_PSEUDO_REGISTER
4982 /* But don't do this if the reg actually serves as an output
4983 reload reg in the current instruction. */
4984 && (n_reloads == 0
4985 || ! TEST_HARD_REG_BIT (reg_is_output_reload, reg)))
4987 CLEAR_HARD_REG_BIT (reg_reloaded_valid, reg);
4988 spill_reg_store[reg] = 0;
4990 if (n_reloads == 0
4991 || !REGNO_REG_SET_P (&reg_has_output_reload, reg))
4992 reg_last_reload_reg[reg] = 0;
4996 /* The following HARD_REG_SETs indicate when each hard register is
4997 used for a reload of various parts of the current insn. */
4999 /* If reg is unavailable for all reloads. */
5000 static HARD_REG_SET reload_reg_unavailable;
5001 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
5002 static HARD_REG_SET reload_reg_used;
5003 /* If reg is in use for a RELOAD_FOR_INPUT_ADDRESS reload for operand I. */
5004 static HARD_REG_SET reload_reg_used_in_input_addr[MAX_RECOG_OPERANDS];
5005 /* If reg is in use for a RELOAD_FOR_INPADDR_ADDRESS reload for operand I. */
5006 static HARD_REG_SET reload_reg_used_in_inpaddr_addr[MAX_RECOG_OPERANDS];
5007 /* If reg is in use for a RELOAD_FOR_OUTPUT_ADDRESS reload for operand I. */
5008 static HARD_REG_SET reload_reg_used_in_output_addr[MAX_RECOG_OPERANDS];
5009 /* If reg is in use for a RELOAD_FOR_OUTADDR_ADDRESS reload for operand I. */
5010 static HARD_REG_SET reload_reg_used_in_outaddr_addr[MAX_RECOG_OPERANDS];
5011 /* If reg is in use for a RELOAD_FOR_INPUT reload for operand I. */
5012 static HARD_REG_SET reload_reg_used_in_input[MAX_RECOG_OPERANDS];
5013 /* If reg is in use for a RELOAD_FOR_OUTPUT reload for operand I. */
5014 static HARD_REG_SET reload_reg_used_in_output[MAX_RECOG_OPERANDS];
5015 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
5016 static HARD_REG_SET reload_reg_used_in_op_addr;
5017 /* If reg is in use for a RELOAD_FOR_OPADDR_ADDR reload. */
5018 static HARD_REG_SET reload_reg_used_in_op_addr_reload;
5019 /* If reg is in use for a RELOAD_FOR_INSN reload. */
5020 static HARD_REG_SET reload_reg_used_in_insn;
5021 /* If reg is in use for a RELOAD_FOR_OTHER_ADDRESS reload. */
5022 static HARD_REG_SET reload_reg_used_in_other_addr;
5024 /* If reg is in use as a reload reg for any sort of reload. */
5025 static HARD_REG_SET reload_reg_used_at_all;
5027 /* If reg is use as an inherited reload. We just mark the first register
5028 in the group. */
5029 static HARD_REG_SET reload_reg_used_for_inherit;
5031 /* Records which hard regs are used in any way, either as explicit use or
5032 by being allocated to a pseudo during any point of the current insn. */
5033 static HARD_REG_SET reg_used_in_insn;
5035 /* Mark reg REGNO as in use for a reload of the sort spec'd by OPNUM and
5036 TYPE. MODE is used to indicate how many consecutive regs are
5037 actually used. */
5039 static void
5040 mark_reload_reg_in_use (unsigned int regno, int opnum, enum reload_type type,
5041 enum machine_mode mode)
5043 switch (type)
5045 case RELOAD_OTHER:
5046 add_to_hard_reg_set (&reload_reg_used, mode, regno);
5047 break;
5049 case RELOAD_FOR_INPUT_ADDRESS:
5050 add_to_hard_reg_set (&reload_reg_used_in_input_addr[opnum], mode, regno);
5051 break;
5053 case RELOAD_FOR_INPADDR_ADDRESS:
5054 add_to_hard_reg_set (&reload_reg_used_in_inpaddr_addr[opnum], mode, regno);
5055 break;
5057 case RELOAD_FOR_OUTPUT_ADDRESS:
5058 add_to_hard_reg_set (&reload_reg_used_in_output_addr[opnum], mode, regno);
5059 break;
5061 case RELOAD_FOR_OUTADDR_ADDRESS:
5062 add_to_hard_reg_set (&reload_reg_used_in_outaddr_addr[opnum], mode, regno);
5063 break;
5065 case RELOAD_FOR_OPERAND_ADDRESS:
5066 add_to_hard_reg_set (&reload_reg_used_in_op_addr, mode, regno);
5067 break;
5069 case RELOAD_FOR_OPADDR_ADDR:
5070 add_to_hard_reg_set (&reload_reg_used_in_op_addr_reload, mode, regno);
5071 break;
5073 case RELOAD_FOR_OTHER_ADDRESS:
5074 add_to_hard_reg_set (&reload_reg_used_in_other_addr, mode, regno);
5075 break;
5077 case RELOAD_FOR_INPUT:
5078 add_to_hard_reg_set (&reload_reg_used_in_input[opnum], mode, regno);
5079 break;
5081 case RELOAD_FOR_OUTPUT:
5082 add_to_hard_reg_set (&reload_reg_used_in_output[opnum], mode, regno);
5083 break;
5085 case RELOAD_FOR_INSN:
5086 add_to_hard_reg_set (&reload_reg_used_in_insn, mode, regno);
5087 break;
5090 add_to_hard_reg_set (&reload_reg_used_at_all, mode, regno);
5093 /* Similarly, but show REGNO is no longer in use for a reload. */
5095 static void
5096 clear_reload_reg_in_use (unsigned int regno, int opnum,
5097 enum reload_type type, enum machine_mode mode)
5099 unsigned int nregs = hard_regno_nregs[regno][mode];
5100 unsigned int start_regno, end_regno, r;
5101 int i;
5102 /* A complication is that for some reload types, inheritance might
5103 allow multiple reloads of the same types to share a reload register.
5104 We set check_opnum if we have to check only reloads with the same
5105 operand number, and check_any if we have to check all reloads. */
5106 int check_opnum = 0;
5107 int check_any = 0;
5108 HARD_REG_SET *used_in_set;
5110 switch (type)
5112 case RELOAD_OTHER:
5113 used_in_set = &reload_reg_used;
5114 break;
5116 case RELOAD_FOR_INPUT_ADDRESS:
5117 used_in_set = &reload_reg_used_in_input_addr[opnum];
5118 break;
5120 case RELOAD_FOR_INPADDR_ADDRESS:
5121 check_opnum = 1;
5122 used_in_set = &reload_reg_used_in_inpaddr_addr[opnum];
5123 break;
5125 case RELOAD_FOR_OUTPUT_ADDRESS:
5126 used_in_set = &reload_reg_used_in_output_addr[opnum];
5127 break;
5129 case RELOAD_FOR_OUTADDR_ADDRESS:
5130 check_opnum = 1;
5131 used_in_set = &reload_reg_used_in_outaddr_addr[opnum];
5132 break;
5134 case RELOAD_FOR_OPERAND_ADDRESS:
5135 used_in_set = &reload_reg_used_in_op_addr;
5136 break;
5138 case RELOAD_FOR_OPADDR_ADDR:
5139 check_any = 1;
5140 used_in_set = &reload_reg_used_in_op_addr_reload;
5141 break;
5143 case RELOAD_FOR_OTHER_ADDRESS:
5144 used_in_set = &reload_reg_used_in_other_addr;
5145 check_any = 1;
5146 break;
5148 case RELOAD_FOR_INPUT:
5149 used_in_set = &reload_reg_used_in_input[opnum];
5150 break;
5152 case RELOAD_FOR_OUTPUT:
5153 used_in_set = &reload_reg_used_in_output[opnum];
5154 break;
5156 case RELOAD_FOR_INSN:
5157 used_in_set = &reload_reg_used_in_insn;
5158 break;
5159 default:
5160 gcc_unreachable ();
5162 /* We resolve conflicts with remaining reloads of the same type by
5163 excluding the intervals of reload registers by them from the
5164 interval of freed reload registers. Since we only keep track of
5165 one set of interval bounds, we might have to exclude somewhat
5166 more than what would be necessary if we used a HARD_REG_SET here.
5167 But this should only happen very infrequently, so there should
5168 be no reason to worry about it. */
5170 start_regno = regno;
5171 end_regno = regno + nregs;
5172 if (check_opnum || check_any)
5174 for (i = n_reloads - 1; i >= 0; i--)
5176 if (rld[i].when_needed == type
5177 && (check_any || rld[i].opnum == opnum)
5178 && rld[i].reg_rtx)
5180 unsigned int conflict_start = true_regnum (rld[i].reg_rtx);
5181 unsigned int conflict_end
5182 = end_hard_regno (rld[i].mode, conflict_start);
5184 /* If there is an overlap with the first to-be-freed register,
5185 adjust the interval start. */
5186 if (conflict_start <= start_regno && conflict_end > start_regno)
5187 start_regno = conflict_end;
5188 /* Otherwise, if there is a conflict with one of the other
5189 to-be-freed registers, adjust the interval end. */
5190 if (conflict_start > start_regno && conflict_start < end_regno)
5191 end_regno = conflict_start;
5196 for (r = start_regno; r < end_regno; r++)
5197 CLEAR_HARD_REG_BIT (*used_in_set, r);
5200 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
5201 specified by OPNUM and TYPE. */
5203 static int
5204 reload_reg_free_p (unsigned int regno, int opnum, enum reload_type type)
5206 int i;
5208 /* In use for a RELOAD_OTHER means it's not available for anything. */
5209 if (TEST_HARD_REG_BIT (reload_reg_used, regno)
5210 || TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5211 return 0;
5213 switch (type)
5215 case RELOAD_OTHER:
5216 /* In use for anything means we can't use it for RELOAD_OTHER. */
5217 if (TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno)
5218 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5219 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
5220 || TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5221 return 0;
5223 for (i = 0; i < reload_n_operands; i++)
5224 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5225 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5226 || TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5227 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5228 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5229 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5230 return 0;
5232 return 1;
5234 case RELOAD_FOR_INPUT:
5235 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5236 || TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
5237 return 0;
5239 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5240 return 0;
5242 /* If it is used for some other input, can't use it. */
5243 for (i = 0; i < reload_n_operands; i++)
5244 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5245 return 0;
5247 /* If it is used in a later operand's address, can't use it. */
5248 for (i = opnum + 1; i < reload_n_operands; i++)
5249 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5250 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5251 return 0;
5253 return 1;
5255 case RELOAD_FOR_INPUT_ADDRESS:
5256 /* Can't use a register if it is used for an input address for this
5257 operand or used as an input in an earlier one. */
5258 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno)
5259 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
5260 return 0;
5262 for (i = 0; i < opnum; i++)
5263 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5264 return 0;
5266 return 1;
5268 case RELOAD_FOR_INPADDR_ADDRESS:
5269 /* Can't use a register if it is used for an input address
5270 for this operand or used as an input in an earlier
5271 one. */
5272 if (TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[opnum], regno))
5273 return 0;
5275 for (i = 0; i < opnum; i++)
5276 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5277 return 0;
5279 return 1;
5281 case RELOAD_FOR_OUTPUT_ADDRESS:
5282 /* Can't use a register if it is used for an output address for this
5283 operand or used as an output in this or a later operand. Note
5284 that multiple output operands are emitted in reverse order, so
5285 the conflicting ones are those with lower indices. */
5286 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[opnum], regno))
5287 return 0;
5289 for (i = 0; i <= opnum; i++)
5290 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5291 return 0;
5293 return 1;
5295 case RELOAD_FOR_OUTADDR_ADDRESS:
5296 /* Can't use a register if it is used for an output address
5297 for this operand or used as an output in this or a
5298 later operand. Note that multiple output operands are
5299 emitted in reverse order, so the conflicting ones are
5300 those with lower indices. */
5301 if (TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
5302 return 0;
5304 for (i = 0; i <= opnum; i++)
5305 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5306 return 0;
5308 return 1;
5310 case RELOAD_FOR_OPERAND_ADDRESS:
5311 for (i = 0; i < reload_n_operands; i++)
5312 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5313 return 0;
5315 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5316 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5318 case RELOAD_FOR_OPADDR_ADDR:
5319 for (i = 0; i < reload_n_operands; i++)
5320 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5321 return 0;
5323 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno));
5325 case RELOAD_FOR_OUTPUT:
5326 /* This cannot share a register with RELOAD_FOR_INSN reloads, other
5327 outputs, or an operand address for this or an earlier output.
5328 Note that multiple output operands are emitted in reverse order,
5329 so the conflicting ones are those with higher indices. */
5330 if (TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno))
5331 return 0;
5333 for (i = 0; i < reload_n_operands; i++)
5334 if (TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5335 return 0;
5337 for (i = opnum; i < reload_n_operands; i++)
5338 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5339 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5340 return 0;
5342 return 1;
5344 case RELOAD_FOR_INSN:
5345 for (i = 0; i < reload_n_operands; i++)
5346 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno)
5347 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5348 return 0;
5350 return (! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5351 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno));
5353 case RELOAD_FOR_OTHER_ADDRESS:
5354 return ! TEST_HARD_REG_BIT (reload_reg_used_in_other_addr, regno);
5356 default:
5357 gcc_unreachable ();
5361 /* Return 1 if the value in reload reg REGNO, as used by the reload with
5362 the number RELOADNUM, is still available in REGNO at the end of the insn.
5364 We can assume that the reload reg was already tested for availability
5365 at the time it is needed, and we should not check this again,
5366 in case the reg has already been marked in use. */
5368 static int
5369 reload_reg_reaches_end_p (unsigned int regno, int reloadnum)
5371 int opnum = rld[reloadnum].opnum;
5372 enum reload_type type = rld[reloadnum].when_needed;
5373 int i;
5375 /* See if there is a reload with the same type for this operand, using
5376 the same register. This case is not handled by the code below. */
5377 for (i = reloadnum + 1; i < n_reloads; i++)
5379 rtx reg;
5380 int nregs;
5382 if (rld[i].opnum != opnum || rld[i].when_needed != type)
5383 continue;
5384 reg = rld[i].reg_rtx;
5385 if (reg == NULL_RTX)
5386 continue;
5387 nregs = hard_regno_nregs[REGNO (reg)][GET_MODE (reg)];
5388 if (regno >= REGNO (reg) && regno < REGNO (reg) + nregs)
5389 return 0;
5392 switch (type)
5394 case RELOAD_OTHER:
5395 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
5396 its value must reach the end. */
5397 return 1;
5399 /* If this use is for part of the insn,
5400 its value reaches if no subsequent part uses the same register.
5401 Just like the above function, don't try to do this with lots
5402 of fallthroughs. */
5404 case RELOAD_FOR_OTHER_ADDRESS:
5405 /* Here we check for everything else, since these don't conflict
5406 with anything else and everything comes later. */
5408 for (i = 0; i < reload_n_operands; i++)
5409 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5410 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5411 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno)
5412 || TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5413 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5414 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5415 return 0;
5417 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5418 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno)
5419 && ! TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5420 && ! TEST_HARD_REG_BIT (reload_reg_used, regno));
5422 case RELOAD_FOR_INPUT_ADDRESS:
5423 case RELOAD_FOR_INPADDR_ADDRESS:
5424 /* Similar, except that we check only for this and subsequent inputs
5425 and the address of only subsequent inputs and we do not need
5426 to check for RELOAD_OTHER objects since they are known not to
5427 conflict. */
5429 for (i = opnum; i < reload_n_operands; i++)
5430 if (TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5431 return 0;
5433 /* Reload register of reload with type RELOAD_FOR_INPADDR_ADDRESS
5434 could be killed if the register is also used by reload with type
5435 RELOAD_FOR_INPUT_ADDRESS, so check it. */
5436 if (type == RELOAD_FOR_INPADDR_ADDRESS
5437 && TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[opnum], regno))
5438 return 0;
5440 for (i = opnum + 1; i < reload_n_operands; i++)
5441 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5442 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno))
5443 return 0;
5445 for (i = 0; i < reload_n_operands; i++)
5446 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5447 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5448 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5449 return 0;
5451 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr_reload, regno))
5452 return 0;
5454 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5455 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5456 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5458 case RELOAD_FOR_INPUT:
5459 /* Similar to input address, except we start at the next operand for
5460 both input and input address and we do not check for
5461 RELOAD_FOR_OPERAND_ADDRESS and RELOAD_FOR_INSN since these
5462 would conflict. */
5464 for (i = opnum + 1; i < reload_n_operands; i++)
5465 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr[i], regno)
5466 || TEST_HARD_REG_BIT (reload_reg_used_in_inpaddr_addr[i], regno)
5467 || TEST_HARD_REG_BIT (reload_reg_used_in_input[i], regno))
5468 return 0;
5470 /* ... fall through ... */
5472 case RELOAD_FOR_OPERAND_ADDRESS:
5473 /* Check outputs and their addresses. */
5475 for (i = 0; i < reload_n_operands; i++)
5476 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5477 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5478 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5479 return 0;
5481 return (!TEST_HARD_REG_BIT (reload_reg_used, regno));
5483 case RELOAD_FOR_OPADDR_ADDR:
5484 for (i = 0; i < reload_n_operands; i++)
5485 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5486 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno)
5487 || TEST_HARD_REG_BIT (reload_reg_used_in_output[i], regno))
5488 return 0;
5490 return (!TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
5491 && !TEST_HARD_REG_BIT (reload_reg_used_in_insn, regno)
5492 && !TEST_HARD_REG_BIT (reload_reg_used, regno));
5494 case RELOAD_FOR_INSN:
5495 /* These conflict with other outputs with RELOAD_OTHER. So
5496 we need only check for output addresses. */
5498 opnum = reload_n_operands;
5500 /* ... fall through ... */
5502 case RELOAD_FOR_OUTPUT:
5503 case RELOAD_FOR_OUTPUT_ADDRESS:
5504 case RELOAD_FOR_OUTADDR_ADDRESS:
5505 /* We already know these can't conflict with a later output. So the
5506 only thing to check are later output addresses.
5507 Note that multiple output operands are emitted in reverse order,
5508 so the conflicting ones are those with lower indices. */
5509 for (i = 0; i < opnum; i++)
5510 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr[i], regno)
5511 || TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[i], regno))
5512 return 0;
5514 /* Reload register of reload with type RELOAD_FOR_OUTADDR_ADDRESS
5515 could be killed if the register is also used by reload with type
5516 RELOAD_FOR_OUTPUT_ADDRESS, so check it. */
5517 if (type == RELOAD_FOR_OUTADDR_ADDRESS
5518 && TEST_HARD_REG_BIT (reload_reg_used_in_outaddr_addr[opnum], regno))
5519 return 0;
5521 return 1;
5523 default:
5524 gcc_unreachable ();
5528 /* Like reload_reg_reaches_end_p, but check that the condition holds for
5529 every register in REG. */
5531 static bool
5532 reload_reg_rtx_reaches_end_p (rtx reg, int reloadnum)
5534 unsigned int i;
5536 for (i = REGNO (reg); i < END_REGNO (reg); i++)
5537 if (!reload_reg_reaches_end_p (i, reloadnum))
5538 return false;
5539 return true;
5543 /* Returns whether R1 and R2 are uniquely chained: the value of one
5544 is used by the other, and that value is not used by any other
5545 reload for this insn. This is used to partially undo the decision
5546 made in find_reloads when in the case of multiple
5547 RELOAD_FOR_OPERAND_ADDRESS reloads it converts all
5548 RELOAD_FOR_OPADDR_ADDR reloads into RELOAD_FOR_OPERAND_ADDRESS
5549 reloads. This code tries to avoid the conflict created by that
5550 change. It might be cleaner to explicitly keep track of which
5551 RELOAD_FOR_OPADDR_ADDR reload is associated with which
5552 RELOAD_FOR_OPERAND_ADDRESS reload, rather than to try to detect
5553 this after the fact. */
5554 static bool
5555 reloads_unique_chain_p (int r1, int r2)
5557 int i;
5559 /* We only check input reloads. */
5560 if (! rld[r1].in || ! rld[r2].in)
5561 return false;
5563 /* Avoid anything with output reloads. */
5564 if (rld[r1].out || rld[r2].out)
5565 return false;
5567 /* "chained" means one reload is a component of the other reload,
5568 not the same as the other reload. */
5569 if (rld[r1].opnum != rld[r2].opnum
5570 || rtx_equal_p (rld[r1].in, rld[r2].in)
5571 || rld[r1].optional || rld[r2].optional
5572 || ! (reg_mentioned_p (rld[r1].in, rld[r2].in)
5573 || reg_mentioned_p (rld[r2].in, rld[r1].in)))
5574 return false;
5576 for (i = 0; i < n_reloads; i ++)
5577 /* Look for input reloads that aren't our two */
5578 if (i != r1 && i != r2 && rld[i].in)
5580 /* If our reload is mentioned at all, it isn't a simple chain. */
5581 if (reg_mentioned_p (rld[r1].in, rld[i].in))
5582 return false;
5584 return true;
5587 /* The recursive function change all occurrences of WHAT in *WHERE
5588 to REPL. */
5589 static void
5590 substitute (rtx *where, const_rtx what, rtx repl)
5592 const char *fmt;
5593 int i;
5594 enum rtx_code code;
5596 if (*where == 0)
5597 return;
5599 if (*where == what || rtx_equal_p (*where, what))
5601 /* Record the location of the changed rtx. */
5602 VEC_safe_push (rtx_p, heap, substitute_stack, where);
5603 *where = repl;
5604 return;
5607 code = GET_CODE (*where);
5608 fmt = GET_RTX_FORMAT (code);
5609 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5611 if (fmt[i] == 'E')
5613 int j;
5615 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
5616 substitute (&XVECEXP (*where, i, j), what, repl);
5618 else if (fmt[i] == 'e')
5619 substitute (&XEXP (*where, i), what, repl);
5623 /* The function returns TRUE if chain of reload R1 and R2 (in any
5624 order) can be evaluated without usage of intermediate register for
5625 the reload containing another reload. It is important to see
5626 gen_reload to understand what the function is trying to do. As an
5627 example, let us have reload chain
5629 r2: const
5630 r1: <something> + const
5632 and reload R2 got reload reg HR. The function returns true if
5633 there is a correct insn HR = HR + <something>. Otherwise,
5634 gen_reload will use intermediate register (and this is the reload
5635 reg for R1) to reload <something>.
5637 We need this function to find a conflict for chain reloads. In our
5638 example, if HR = HR + <something> is incorrect insn, then we cannot
5639 use HR as a reload register for R2. If we do use it then we get a
5640 wrong code:
5642 HR = const
5643 HR = <something>
5644 HR = HR + HR
5647 static bool
5648 gen_reload_chain_without_interm_reg_p (int r1, int r2)
5650 /* Assume other cases in gen_reload are not possible for
5651 chain reloads or do need an intermediate hard registers. */
5652 bool result = true;
5653 int regno, n, code;
5654 rtx out, in, insn;
5655 rtx last = get_last_insn ();
5657 /* Make r2 a component of r1. */
5658 if (reg_mentioned_p (rld[r1].in, rld[r2].in))
5660 n = r1;
5661 r1 = r2;
5662 r2 = n;
5664 gcc_assert (reg_mentioned_p (rld[r2].in, rld[r1].in));
5665 regno = rld[r1].regno >= 0 ? rld[r1].regno : rld[r2].regno;
5666 gcc_assert (regno >= 0);
5667 out = gen_rtx_REG (rld[r1].mode, regno);
5668 in = rld[r1].in;
5669 substitute (&in, rld[r2].in, gen_rtx_REG (rld[r2].mode, regno));
5671 /* If IN is a paradoxical SUBREG, remove it and try to put the
5672 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
5673 strip_paradoxical_subreg (&in, &out);
5675 if (GET_CODE (in) == PLUS
5676 && (REG_P (XEXP (in, 0))
5677 || GET_CODE (XEXP (in, 0)) == SUBREG
5678 || MEM_P (XEXP (in, 0)))
5679 && (REG_P (XEXP (in, 1))
5680 || GET_CODE (XEXP (in, 1)) == SUBREG
5681 || CONSTANT_P (XEXP (in, 1))
5682 || MEM_P (XEXP (in, 1))))
5684 insn = emit_insn (gen_rtx_SET (VOIDmode, out, in));
5685 code = recog_memoized (insn);
5686 result = false;
5688 if (code >= 0)
5690 extract_insn (insn);
5691 /* We want constrain operands to treat this insn strictly in
5692 its validity determination, i.e., the way it would after
5693 reload has completed. */
5694 result = constrain_operands (1);
5697 delete_insns_since (last);
5700 /* Restore the original value at each changed address within R1. */
5701 while (!VEC_empty (rtx_p, substitute_stack))
5703 rtx *where = VEC_pop (rtx_p, substitute_stack);
5704 *where = rld[r2].in;
5707 return result;
5710 /* Return 1 if the reloads denoted by R1 and R2 cannot share a register.
5711 Return 0 otherwise.
5713 This function uses the same algorithm as reload_reg_free_p above. */
5715 static int
5716 reloads_conflict (int r1, int r2)
5718 enum reload_type r1_type = rld[r1].when_needed;
5719 enum reload_type r2_type = rld[r2].when_needed;
5720 int r1_opnum = rld[r1].opnum;
5721 int r2_opnum = rld[r2].opnum;
5723 /* RELOAD_OTHER conflicts with everything. */
5724 if (r2_type == RELOAD_OTHER)
5725 return 1;
5727 /* Otherwise, check conflicts differently for each type. */
5729 switch (r1_type)
5731 case RELOAD_FOR_INPUT:
5732 return (r2_type == RELOAD_FOR_INSN
5733 || r2_type == RELOAD_FOR_OPERAND_ADDRESS
5734 || r2_type == RELOAD_FOR_OPADDR_ADDR
5735 || r2_type == RELOAD_FOR_INPUT
5736 || ((r2_type == RELOAD_FOR_INPUT_ADDRESS
5737 || r2_type == RELOAD_FOR_INPADDR_ADDRESS)
5738 && r2_opnum > r1_opnum));
5740 case RELOAD_FOR_INPUT_ADDRESS:
5741 return ((r2_type == RELOAD_FOR_INPUT_ADDRESS && r1_opnum == r2_opnum)
5742 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5744 case RELOAD_FOR_INPADDR_ADDRESS:
5745 return ((r2_type == RELOAD_FOR_INPADDR_ADDRESS && r1_opnum == r2_opnum)
5746 || (r2_type == RELOAD_FOR_INPUT && r2_opnum < r1_opnum));
5748 case RELOAD_FOR_OUTPUT_ADDRESS:
5749 return ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS && r2_opnum == r1_opnum)
5750 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5752 case RELOAD_FOR_OUTADDR_ADDRESS:
5753 return ((r2_type == RELOAD_FOR_OUTADDR_ADDRESS && r2_opnum == r1_opnum)
5754 || (r2_type == RELOAD_FOR_OUTPUT && r2_opnum <= r1_opnum));
5756 case RELOAD_FOR_OPERAND_ADDRESS:
5757 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_INSN
5758 || (r2_type == RELOAD_FOR_OPERAND_ADDRESS
5759 && (!reloads_unique_chain_p (r1, r2)
5760 || !gen_reload_chain_without_interm_reg_p (r1, r2))));
5762 case RELOAD_FOR_OPADDR_ADDR:
5763 return (r2_type == RELOAD_FOR_INPUT
5764 || r2_type == RELOAD_FOR_OPADDR_ADDR);
5766 case RELOAD_FOR_OUTPUT:
5767 return (r2_type == RELOAD_FOR_INSN || r2_type == RELOAD_FOR_OUTPUT
5768 || ((r2_type == RELOAD_FOR_OUTPUT_ADDRESS
5769 || r2_type == RELOAD_FOR_OUTADDR_ADDRESS)
5770 && r2_opnum >= r1_opnum));
5772 case RELOAD_FOR_INSN:
5773 return (r2_type == RELOAD_FOR_INPUT || r2_type == RELOAD_FOR_OUTPUT
5774 || r2_type == RELOAD_FOR_INSN
5775 || r2_type == RELOAD_FOR_OPERAND_ADDRESS);
5777 case RELOAD_FOR_OTHER_ADDRESS:
5778 return r2_type == RELOAD_FOR_OTHER_ADDRESS;
5780 case RELOAD_OTHER:
5781 return 1;
5783 default:
5784 gcc_unreachable ();
5788 /* Indexed by reload number, 1 if incoming value
5789 inherited from previous insns. */
5790 static char reload_inherited[MAX_RELOADS];
5792 /* For an inherited reload, this is the insn the reload was inherited from,
5793 if we know it. Otherwise, this is 0. */
5794 static rtx reload_inheritance_insn[MAX_RELOADS];
5796 /* If nonzero, this is a place to get the value of the reload,
5797 rather than using reload_in. */
5798 static rtx reload_override_in[MAX_RELOADS];
5800 /* For each reload, the hard register number of the register used,
5801 or -1 if we did not need a register for this reload. */
5802 static int reload_spill_index[MAX_RELOADS];
5804 /* Index X is the value of rld[X].reg_rtx, adjusted for the input mode. */
5805 static rtx reload_reg_rtx_for_input[MAX_RELOADS];
5807 /* Index X is the value of rld[X].reg_rtx, adjusted for the output mode. */
5808 static rtx reload_reg_rtx_for_output[MAX_RELOADS];
5810 /* Subroutine of free_for_value_p, used to check a single register.
5811 START_REGNO is the starting regno of the full reload register
5812 (possibly comprising multiple hard registers) that we are considering. */
5814 static int
5815 reload_reg_free_for_value_p (int start_regno, int regno, int opnum,
5816 enum reload_type type, rtx value, rtx out,
5817 int reloadnum, int ignore_address_reloads)
5819 int time1;
5820 /* Set if we see an input reload that must not share its reload register
5821 with any new earlyclobber, but might otherwise share the reload
5822 register with an output or input-output reload. */
5823 int check_earlyclobber = 0;
5824 int i;
5825 int copy = 0;
5827 if (TEST_HARD_REG_BIT (reload_reg_unavailable, regno))
5828 return 0;
5830 if (out == const0_rtx)
5832 copy = 1;
5833 out = NULL_RTX;
5836 /* We use some pseudo 'time' value to check if the lifetimes of the
5837 new register use would overlap with the one of a previous reload
5838 that is not read-only or uses a different value.
5839 The 'time' used doesn't have to be linear in any shape or form, just
5840 monotonic.
5841 Some reload types use different 'buckets' for each operand.
5842 So there are MAX_RECOG_OPERANDS different time values for each
5843 such reload type.
5844 We compute TIME1 as the time when the register for the prospective
5845 new reload ceases to be live, and TIME2 for each existing
5846 reload as the time when that the reload register of that reload
5847 becomes live.
5848 Where there is little to be gained by exact lifetime calculations,
5849 we just make conservative assumptions, i.e. a longer lifetime;
5850 this is done in the 'default:' cases. */
5851 switch (type)
5853 case RELOAD_FOR_OTHER_ADDRESS:
5854 /* RELOAD_FOR_OTHER_ADDRESS conflicts with RELOAD_OTHER reloads. */
5855 time1 = copy ? 0 : 1;
5856 break;
5857 case RELOAD_OTHER:
5858 time1 = copy ? 1 : MAX_RECOG_OPERANDS * 5 + 5;
5859 break;
5860 /* For each input, we may have a sequence of RELOAD_FOR_INPADDR_ADDRESS,
5861 RELOAD_FOR_INPUT_ADDRESS and RELOAD_FOR_INPUT. By adding 0 / 1 / 2 ,
5862 respectively, to the time values for these, we get distinct time
5863 values. To get distinct time values for each operand, we have to
5864 multiply opnum by at least three. We round that up to four because
5865 multiply by four is often cheaper. */
5866 case RELOAD_FOR_INPADDR_ADDRESS:
5867 time1 = opnum * 4 + 2;
5868 break;
5869 case RELOAD_FOR_INPUT_ADDRESS:
5870 time1 = opnum * 4 + 3;
5871 break;
5872 case RELOAD_FOR_INPUT:
5873 /* All RELOAD_FOR_INPUT reloads remain live till the instruction
5874 executes (inclusive). */
5875 time1 = copy ? opnum * 4 + 4 : MAX_RECOG_OPERANDS * 4 + 3;
5876 break;
5877 case RELOAD_FOR_OPADDR_ADDR:
5878 /* opnum * 4 + 4
5879 <= (MAX_RECOG_OPERANDS - 1) * 4 + 4 == MAX_RECOG_OPERANDS * 4 */
5880 time1 = MAX_RECOG_OPERANDS * 4 + 1;
5881 break;
5882 case RELOAD_FOR_OPERAND_ADDRESS:
5883 /* RELOAD_FOR_OPERAND_ADDRESS reloads are live even while the insn
5884 is executed. */
5885 time1 = copy ? MAX_RECOG_OPERANDS * 4 + 2 : MAX_RECOG_OPERANDS * 4 + 3;
5886 break;
5887 case RELOAD_FOR_OUTADDR_ADDRESS:
5888 time1 = MAX_RECOG_OPERANDS * 4 + 4 + opnum;
5889 break;
5890 case RELOAD_FOR_OUTPUT_ADDRESS:
5891 time1 = MAX_RECOG_OPERANDS * 4 + 5 + opnum;
5892 break;
5893 default:
5894 time1 = MAX_RECOG_OPERANDS * 5 + 5;
5897 for (i = 0; i < n_reloads; i++)
5899 rtx reg = rld[i].reg_rtx;
5900 if (reg && REG_P (reg)
5901 && ((unsigned) regno - true_regnum (reg)
5902 <= hard_regno_nregs[REGNO (reg)][GET_MODE (reg)] - (unsigned) 1)
5903 && i != reloadnum)
5905 rtx other_input = rld[i].in;
5907 /* If the other reload loads the same input value, that
5908 will not cause a conflict only if it's loading it into
5909 the same register. */
5910 if (true_regnum (reg) != start_regno)
5911 other_input = NULL_RTX;
5912 if (! other_input || ! rtx_equal_p (other_input, value)
5913 || rld[i].out || out)
5915 int time2;
5916 switch (rld[i].when_needed)
5918 case RELOAD_FOR_OTHER_ADDRESS:
5919 time2 = 0;
5920 break;
5921 case RELOAD_FOR_INPADDR_ADDRESS:
5922 /* find_reloads makes sure that a
5923 RELOAD_FOR_{INP,OP,OUT}ADDR_ADDRESS reload is only used
5924 by at most one - the first -
5925 RELOAD_FOR_{INPUT,OPERAND,OUTPUT}_ADDRESS . If the
5926 address reload is inherited, the address address reload
5927 goes away, so we can ignore this conflict. */
5928 if (type == RELOAD_FOR_INPUT_ADDRESS && reloadnum == i + 1
5929 && ignore_address_reloads
5930 /* Unless the RELOAD_FOR_INPUT is an auto_inc expression.
5931 Then the address address is still needed to store
5932 back the new address. */
5933 && ! rld[reloadnum].out)
5934 continue;
5935 /* Likewise, if a RELOAD_FOR_INPUT can inherit a value, its
5936 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS
5937 reloads go away. */
5938 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5939 && ignore_address_reloads
5940 /* Unless we are reloading an auto_inc expression. */
5941 && ! rld[reloadnum].out)
5942 continue;
5943 time2 = rld[i].opnum * 4 + 2;
5944 break;
5945 case RELOAD_FOR_INPUT_ADDRESS:
5946 if (type == RELOAD_FOR_INPUT && opnum == rld[i].opnum
5947 && ignore_address_reloads
5948 && ! rld[reloadnum].out)
5949 continue;
5950 time2 = rld[i].opnum * 4 + 3;
5951 break;
5952 case RELOAD_FOR_INPUT:
5953 time2 = rld[i].opnum * 4 + 4;
5954 check_earlyclobber = 1;
5955 break;
5956 /* rld[i].opnum * 4 + 4 <= (MAX_RECOG_OPERAND - 1) * 4 + 4
5957 == MAX_RECOG_OPERAND * 4 */
5958 case RELOAD_FOR_OPADDR_ADDR:
5959 if (type == RELOAD_FOR_OPERAND_ADDRESS && reloadnum == i + 1
5960 && ignore_address_reloads
5961 && ! rld[reloadnum].out)
5962 continue;
5963 time2 = MAX_RECOG_OPERANDS * 4 + 1;
5964 break;
5965 case RELOAD_FOR_OPERAND_ADDRESS:
5966 time2 = MAX_RECOG_OPERANDS * 4 + 2;
5967 check_earlyclobber = 1;
5968 break;
5969 case RELOAD_FOR_INSN:
5970 time2 = MAX_RECOG_OPERANDS * 4 + 3;
5971 break;
5972 case RELOAD_FOR_OUTPUT:
5973 /* All RELOAD_FOR_OUTPUT reloads become live just after the
5974 instruction is executed. */
5975 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5976 break;
5977 /* The first RELOAD_FOR_OUTADDR_ADDRESS reload conflicts with
5978 the RELOAD_FOR_OUTPUT reloads, so assign it the same time
5979 value. */
5980 case RELOAD_FOR_OUTADDR_ADDRESS:
5981 if (type == RELOAD_FOR_OUTPUT_ADDRESS && reloadnum == i + 1
5982 && ignore_address_reloads
5983 && ! rld[reloadnum].out)
5984 continue;
5985 time2 = MAX_RECOG_OPERANDS * 4 + 4 + rld[i].opnum;
5986 break;
5987 case RELOAD_FOR_OUTPUT_ADDRESS:
5988 time2 = MAX_RECOG_OPERANDS * 4 + 5 + rld[i].opnum;
5989 break;
5990 case RELOAD_OTHER:
5991 /* If there is no conflict in the input part, handle this
5992 like an output reload. */
5993 if (! rld[i].in || rtx_equal_p (other_input, value))
5995 time2 = MAX_RECOG_OPERANDS * 4 + 4;
5996 /* Earlyclobbered outputs must conflict with inputs. */
5997 if (earlyclobber_operand_p (rld[i].out))
5998 time2 = MAX_RECOG_OPERANDS * 4 + 3;
6000 break;
6002 time2 = 1;
6003 /* RELOAD_OTHER might be live beyond instruction execution,
6004 but this is not obvious when we set time2 = 1. So check
6005 here if there might be a problem with the new reload
6006 clobbering the register used by the RELOAD_OTHER. */
6007 if (out)
6008 return 0;
6009 break;
6010 default:
6011 return 0;
6013 if ((time1 >= time2
6014 && (! rld[i].in || rld[i].out
6015 || ! rtx_equal_p (other_input, value)))
6016 || (out && rld[reloadnum].out_reg
6017 && time2 >= MAX_RECOG_OPERANDS * 4 + 3))
6018 return 0;
6023 /* Earlyclobbered outputs must conflict with inputs. */
6024 if (check_earlyclobber && out && earlyclobber_operand_p (out))
6025 return 0;
6027 return 1;
6030 /* Return 1 if the value in reload reg REGNO, as used by a reload
6031 needed for the part of the insn specified by OPNUM and TYPE,
6032 may be used to load VALUE into it.
6034 MODE is the mode in which the register is used, this is needed to
6035 determine how many hard regs to test.
6037 Other read-only reloads with the same value do not conflict
6038 unless OUT is nonzero and these other reloads have to live while
6039 output reloads live.
6040 If OUT is CONST0_RTX, this is a special case: it means that the
6041 test should not be for using register REGNO as reload register, but
6042 for copying from register REGNO into the reload register.
6044 RELOADNUM is the number of the reload we want to load this value for;
6045 a reload does not conflict with itself.
6047 When IGNORE_ADDRESS_RELOADS is set, we can not have conflicts with
6048 reloads that load an address for the very reload we are considering.
6050 The caller has to make sure that there is no conflict with the return
6051 register. */
6053 static int
6054 free_for_value_p (int regno, enum machine_mode mode, int opnum,
6055 enum reload_type type, rtx value, rtx out, int reloadnum,
6056 int ignore_address_reloads)
6058 int nregs = hard_regno_nregs[regno][mode];
6059 while (nregs-- > 0)
6060 if (! reload_reg_free_for_value_p (regno, regno + nregs, opnum, type,
6061 value, out, reloadnum,
6062 ignore_address_reloads))
6063 return 0;
6064 return 1;
6067 /* Return nonzero if the rtx X is invariant over the current function. */
6068 /* ??? Actually, the places where we use this expect exactly what is
6069 tested here, and not everything that is function invariant. In
6070 particular, the frame pointer and arg pointer are special cased;
6071 pic_offset_table_rtx is not, and we must not spill these things to
6072 memory. */
6075 function_invariant_p (const_rtx x)
6077 if (CONSTANT_P (x))
6078 return 1;
6079 if (x == frame_pointer_rtx || x == arg_pointer_rtx)
6080 return 1;
6081 if (GET_CODE (x) == PLUS
6082 && (XEXP (x, 0) == frame_pointer_rtx || XEXP (x, 0) == arg_pointer_rtx)
6083 && GET_CODE (XEXP (x, 1)) == CONST_INT)
6084 return 1;
6085 return 0;
6088 /* Determine whether the reload reg X overlaps any rtx'es used for
6089 overriding inheritance. Return nonzero if so. */
6091 static int
6092 conflicts_with_override (rtx x)
6094 int i;
6095 for (i = 0; i < n_reloads; i++)
6096 if (reload_override_in[i]
6097 && reg_overlap_mentioned_p (x, reload_override_in[i]))
6098 return 1;
6099 return 0;
6102 /* Give an error message saying we failed to find a reload for INSN,
6103 and clear out reload R. */
6104 static void
6105 failed_reload (rtx insn, int r)
6107 if (asm_noperands (PATTERN (insn)) < 0)
6108 /* It's the compiler's fault. */
6109 fatal_insn ("could not find a spill register", insn);
6111 /* It's the user's fault; the operand's mode and constraint
6112 don't match. Disable this reload so we don't crash in final. */
6113 error_for_asm (insn,
6114 "%<asm%> operand constraint incompatible with operand size");
6115 rld[r].in = 0;
6116 rld[r].out = 0;
6117 rld[r].reg_rtx = 0;
6118 rld[r].optional = 1;
6119 rld[r].secondary_p = 1;
6122 /* I is the index in SPILL_REG_RTX of the reload register we are to allocate
6123 for reload R. If it's valid, get an rtx for it. Return nonzero if
6124 successful. */
6125 static int
6126 set_reload_reg (int i, int r)
6128 /* regno is 'set but not used' if HARD_REGNO_MODE_OK doesn't use its first
6129 parameter. */
6130 int regno ATTRIBUTE_UNUSED;
6131 rtx reg = spill_reg_rtx[i];
6133 if (reg == 0 || GET_MODE (reg) != rld[r].mode)
6134 spill_reg_rtx[i] = reg
6135 = gen_rtx_REG (rld[r].mode, spill_regs[i]);
6137 regno = true_regnum (reg);
6139 /* Detect when the reload reg can't hold the reload mode.
6140 This used to be one `if', but Sequent compiler can't handle that. */
6141 if (HARD_REGNO_MODE_OK (regno, rld[r].mode))
6143 enum machine_mode test_mode = VOIDmode;
6144 if (rld[r].in)
6145 test_mode = GET_MODE (rld[r].in);
6146 /* If rld[r].in has VOIDmode, it means we will load it
6147 in whatever mode the reload reg has: to wit, rld[r].mode.
6148 We have already tested that for validity. */
6149 /* Aside from that, we need to test that the expressions
6150 to reload from or into have modes which are valid for this
6151 reload register. Otherwise the reload insns would be invalid. */
6152 if (! (rld[r].in != 0 && test_mode != VOIDmode
6153 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
6154 if (! (rld[r].out != 0
6155 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (rld[r].out))))
6157 /* The reg is OK. */
6158 last_spill_reg = i;
6160 /* Mark as in use for this insn the reload regs we use
6161 for this. */
6162 mark_reload_reg_in_use (spill_regs[i], rld[r].opnum,
6163 rld[r].when_needed, rld[r].mode);
6165 rld[r].reg_rtx = reg;
6166 reload_spill_index[r] = spill_regs[i];
6167 return 1;
6170 return 0;
6173 /* Find a spill register to use as a reload register for reload R.
6174 LAST_RELOAD is nonzero if this is the last reload for the insn being
6175 processed.
6177 Set rld[R].reg_rtx to the register allocated.
6179 We return 1 if successful, or 0 if we couldn't find a spill reg and
6180 we didn't change anything. */
6182 static int
6183 allocate_reload_reg (struct insn_chain *chain ATTRIBUTE_UNUSED, int r,
6184 int last_reload)
6186 int i, pass, count;
6188 /* If we put this reload ahead, thinking it is a group,
6189 then insist on finding a group. Otherwise we can grab a
6190 reg that some other reload needs.
6191 (That can happen when we have a 68000 DATA_OR_FP_REG
6192 which is a group of data regs or one fp reg.)
6193 We need not be so restrictive if there are no more reloads
6194 for this insn.
6196 ??? Really it would be nicer to have smarter handling
6197 for that kind of reg class, where a problem like this is normal.
6198 Perhaps those classes should be avoided for reloading
6199 by use of more alternatives. */
6201 int force_group = rld[r].nregs > 1 && ! last_reload;
6203 /* If we want a single register and haven't yet found one,
6204 take any reg in the right class and not in use.
6205 If we want a consecutive group, here is where we look for it.
6207 We use three passes so we can first look for reload regs to
6208 reuse, which are already in use for other reloads in this insn,
6209 and only then use additional registers which are not "bad", then
6210 finally any register.
6212 I think that maximizing reuse is needed to make sure we don't
6213 run out of reload regs. Suppose we have three reloads, and
6214 reloads A and B can share regs. These need two regs.
6215 Suppose A and B are given different regs.
6216 That leaves none for C. */
6217 for (pass = 0; pass < 3; pass++)
6219 /* I is the index in spill_regs.
6220 We advance it round-robin between insns to use all spill regs
6221 equally, so that inherited reloads have a chance
6222 of leapfrogging each other. */
6224 i = last_spill_reg;
6226 for (count = 0; count < n_spills; count++)
6228 int rclass = (int) rld[r].rclass;
6229 int regnum;
6231 i++;
6232 if (i >= n_spills)
6233 i -= n_spills;
6234 regnum = spill_regs[i];
6236 if ((reload_reg_free_p (regnum, rld[r].opnum,
6237 rld[r].when_needed)
6238 || (rld[r].in
6239 /* We check reload_reg_used to make sure we
6240 don't clobber the return register. */
6241 && ! TEST_HARD_REG_BIT (reload_reg_used, regnum)
6242 && free_for_value_p (regnum, rld[r].mode, rld[r].opnum,
6243 rld[r].when_needed, rld[r].in,
6244 rld[r].out, r, 1)))
6245 && TEST_HARD_REG_BIT (reg_class_contents[rclass], regnum)
6246 && HARD_REGNO_MODE_OK (regnum, rld[r].mode)
6247 /* Look first for regs to share, then for unshared. But
6248 don't share regs used for inherited reloads; they are
6249 the ones we want to preserve. */
6250 && (pass
6251 || (TEST_HARD_REG_BIT (reload_reg_used_at_all,
6252 regnum)
6253 && ! TEST_HARD_REG_BIT (reload_reg_used_for_inherit,
6254 regnum))))
6256 int nr = hard_regno_nregs[regnum][rld[r].mode];
6258 /* During the second pass we want to avoid reload registers
6259 which are "bad" for this reload. */
6260 if (pass == 1
6261 && ira_bad_reload_regno (regnum, rld[r].in, rld[r].out))
6262 continue;
6264 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
6265 (on 68000) got us two FP regs. If NR is 1,
6266 we would reject both of them. */
6267 if (force_group)
6268 nr = rld[r].nregs;
6269 /* If we need only one reg, we have already won. */
6270 if (nr == 1)
6272 /* But reject a single reg if we demand a group. */
6273 if (force_group)
6274 continue;
6275 break;
6277 /* Otherwise check that as many consecutive regs as we need
6278 are available here. */
6279 while (nr > 1)
6281 int regno = regnum + nr - 1;
6282 if (!(TEST_HARD_REG_BIT (reg_class_contents[rclass], regno)
6283 && spill_reg_order[regno] >= 0
6284 && reload_reg_free_p (regno, rld[r].opnum,
6285 rld[r].when_needed)))
6286 break;
6287 nr--;
6289 if (nr == 1)
6290 break;
6294 /* If we found something on the current pass, omit later passes. */
6295 if (count < n_spills)
6296 break;
6299 /* We should have found a spill register by now. */
6300 if (count >= n_spills)
6301 return 0;
6303 /* I is the index in SPILL_REG_RTX of the reload register we are to
6304 allocate. Get an rtx for it and find its register number. */
6306 return set_reload_reg (i, r);
6309 /* Initialize all the tables needed to allocate reload registers.
6310 CHAIN is the insn currently being processed; SAVE_RELOAD_REG_RTX
6311 is the array we use to restore the reg_rtx field for every reload. */
6313 static void
6314 choose_reload_regs_init (struct insn_chain *chain, rtx *save_reload_reg_rtx)
6316 int i;
6318 for (i = 0; i < n_reloads; i++)
6319 rld[i].reg_rtx = save_reload_reg_rtx[i];
6321 memset (reload_inherited, 0, MAX_RELOADS);
6322 memset (reload_inheritance_insn, 0, MAX_RELOADS * sizeof (rtx));
6323 memset (reload_override_in, 0, MAX_RELOADS * sizeof (rtx));
6325 CLEAR_HARD_REG_SET (reload_reg_used);
6326 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
6327 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
6328 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr_reload);
6329 CLEAR_HARD_REG_SET (reload_reg_used_in_insn);
6330 CLEAR_HARD_REG_SET (reload_reg_used_in_other_addr);
6332 CLEAR_HARD_REG_SET (reg_used_in_insn);
6334 HARD_REG_SET tmp;
6335 REG_SET_TO_HARD_REG_SET (tmp, &chain->live_throughout);
6336 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
6337 REG_SET_TO_HARD_REG_SET (tmp, &chain->dead_or_set);
6338 IOR_HARD_REG_SET (reg_used_in_insn, tmp);
6339 compute_use_by_pseudos (&reg_used_in_insn, &chain->live_throughout);
6340 compute_use_by_pseudos (&reg_used_in_insn, &chain->dead_or_set);
6343 for (i = 0; i < reload_n_operands; i++)
6345 CLEAR_HARD_REG_SET (reload_reg_used_in_output[i]);
6346 CLEAR_HARD_REG_SET (reload_reg_used_in_input[i]);
6347 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr[i]);
6348 CLEAR_HARD_REG_SET (reload_reg_used_in_inpaddr_addr[i]);
6349 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr[i]);
6350 CLEAR_HARD_REG_SET (reload_reg_used_in_outaddr_addr[i]);
6353 COMPL_HARD_REG_SET (reload_reg_unavailable, chain->used_spill_regs);
6355 CLEAR_HARD_REG_SET (reload_reg_used_for_inherit);
6357 for (i = 0; i < n_reloads; i++)
6358 /* If we have already decided to use a certain register,
6359 don't use it in another way. */
6360 if (rld[i].reg_rtx)
6361 mark_reload_reg_in_use (REGNO (rld[i].reg_rtx), rld[i].opnum,
6362 rld[i].when_needed, rld[i].mode);
6365 /* Assign hard reg targets for the pseudo-registers we must reload
6366 into hard regs for this insn.
6367 Also output the instructions to copy them in and out of the hard regs.
6369 For machines with register classes, we are responsible for
6370 finding a reload reg in the proper class. */
6372 static void
6373 choose_reload_regs (struct insn_chain *chain)
6375 rtx insn = chain->insn;
6376 int i, j;
6377 unsigned int max_group_size = 1;
6378 enum reg_class group_class = NO_REGS;
6379 int pass, win, inheritance;
6381 rtx save_reload_reg_rtx[MAX_RELOADS];
6383 /* In order to be certain of getting the registers we need,
6384 we must sort the reloads into order of increasing register class.
6385 Then our grabbing of reload registers will parallel the process
6386 that provided the reload registers.
6388 Also note whether any of the reloads wants a consecutive group of regs.
6389 If so, record the maximum size of the group desired and what
6390 register class contains all the groups needed by this insn. */
6392 for (j = 0; j < n_reloads; j++)
6394 reload_order[j] = j;
6395 if (rld[j].reg_rtx != NULL_RTX)
6397 gcc_assert (REG_P (rld[j].reg_rtx)
6398 && HARD_REGISTER_P (rld[j].reg_rtx));
6399 reload_spill_index[j] = REGNO (rld[j].reg_rtx);
6401 else
6402 reload_spill_index[j] = -1;
6404 if (rld[j].nregs > 1)
6406 max_group_size = MAX (rld[j].nregs, max_group_size);
6407 group_class
6408 = reg_class_superunion[(int) rld[j].rclass][(int) group_class];
6411 save_reload_reg_rtx[j] = rld[j].reg_rtx;
6414 if (n_reloads > 1)
6415 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
6417 /* If -O, try first with inheritance, then turning it off.
6418 If not -O, don't do inheritance.
6419 Using inheritance when not optimizing leads to paradoxes
6420 with fp on the 68k: fp numbers (not NaNs) fail to be equal to themselves
6421 because one side of the comparison might be inherited. */
6422 win = 0;
6423 for (inheritance = optimize > 0; inheritance >= 0; inheritance--)
6425 choose_reload_regs_init (chain, save_reload_reg_rtx);
6427 /* Process the reloads in order of preference just found.
6428 Beyond this point, subregs can be found in reload_reg_rtx.
6430 This used to look for an existing reloaded home for all of the
6431 reloads, and only then perform any new reloads. But that could lose
6432 if the reloads were done out of reg-class order because a later
6433 reload with a looser constraint might have an old home in a register
6434 needed by an earlier reload with a tighter constraint.
6436 To solve this, we make two passes over the reloads, in the order
6437 described above. In the first pass we try to inherit a reload
6438 from a previous insn. If there is a later reload that needs a
6439 class that is a proper subset of the class being processed, we must
6440 also allocate a spill register during the first pass.
6442 Then make a second pass over the reloads to allocate any reloads
6443 that haven't been given registers yet. */
6445 for (j = 0; j < n_reloads; j++)
6447 int r = reload_order[j];
6448 rtx search_equiv = NULL_RTX;
6450 /* Ignore reloads that got marked inoperative. */
6451 if (rld[r].out == 0 && rld[r].in == 0
6452 && ! rld[r].secondary_p)
6453 continue;
6455 /* If find_reloads chose to use reload_in or reload_out as a reload
6456 register, we don't need to chose one. Otherwise, try even if it
6457 found one since we might save an insn if we find the value lying
6458 around.
6459 Try also when reload_in is a pseudo without a hard reg. */
6460 if (rld[r].in != 0 && rld[r].reg_rtx != 0
6461 && (rtx_equal_p (rld[r].in, rld[r].reg_rtx)
6462 || (rtx_equal_p (rld[r].out, rld[r].reg_rtx)
6463 && !MEM_P (rld[r].in)
6464 && true_regnum (rld[r].in) < FIRST_PSEUDO_REGISTER)))
6465 continue;
6467 #if 0 /* No longer needed for correct operation.
6468 It might give better code, or might not; worth an experiment? */
6469 /* If this is an optional reload, we can't inherit from earlier insns
6470 until we are sure that any non-optional reloads have been allocated.
6471 The following code takes advantage of the fact that optional reloads
6472 are at the end of reload_order. */
6473 if (rld[r].optional != 0)
6474 for (i = 0; i < j; i++)
6475 if ((rld[reload_order[i]].out != 0
6476 || rld[reload_order[i]].in != 0
6477 || rld[reload_order[i]].secondary_p)
6478 && ! rld[reload_order[i]].optional
6479 && rld[reload_order[i]].reg_rtx == 0)
6480 allocate_reload_reg (chain, reload_order[i], 0);
6481 #endif
6483 /* First see if this pseudo is already available as reloaded
6484 for a previous insn. We cannot try to inherit for reloads
6485 that are smaller than the maximum number of registers needed
6486 for groups unless the register we would allocate cannot be used
6487 for the groups.
6489 We could check here to see if this is a secondary reload for
6490 an object that is already in a register of the desired class.
6491 This would avoid the need for the secondary reload register.
6492 But this is complex because we can't easily determine what
6493 objects might want to be loaded via this reload. So let a
6494 register be allocated here. In `emit_reload_insns' we suppress
6495 one of the loads in the case described above. */
6497 if (inheritance)
6499 int byte = 0;
6500 int regno = -1;
6501 enum machine_mode mode = VOIDmode;
6503 if (rld[r].in == 0)
6505 else if (REG_P (rld[r].in))
6507 regno = REGNO (rld[r].in);
6508 mode = GET_MODE (rld[r].in);
6510 else if (REG_P (rld[r].in_reg))
6512 regno = REGNO (rld[r].in_reg);
6513 mode = GET_MODE (rld[r].in_reg);
6515 else if (GET_CODE (rld[r].in_reg) == SUBREG
6516 && REG_P (SUBREG_REG (rld[r].in_reg)))
6518 regno = REGNO (SUBREG_REG (rld[r].in_reg));
6519 if (regno < FIRST_PSEUDO_REGISTER)
6520 regno = subreg_regno (rld[r].in_reg);
6521 else
6522 byte = SUBREG_BYTE (rld[r].in_reg);
6523 mode = GET_MODE (rld[r].in_reg);
6525 #ifdef AUTO_INC_DEC
6526 else if (GET_RTX_CLASS (GET_CODE (rld[r].in_reg)) == RTX_AUTOINC
6527 && REG_P (XEXP (rld[r].in_reg, 0)))
6529 regno = REGNO (XEXP (rld[r].in_reg, 0));
6530 mode = GET_MODE (XEXP (rld[r].in_reg, 0));
6531 rld[r].out = rld[r].in;
6533 #endif
6534 #if 0
6535 /* This won't work, since REGNO can be a pseudo reg number.
6536 Also, it takes much more hair to keep track of all the things
6537 that can invalidate an inherited reload of part of a pseudoreg. */
6538 else if (GET_CODE (rld[r].in) == SUBREG
6539 && REG_P (SUBREG_REG (rld[r].in)))
6540 regno = subreg_regno (rld[r].in);
6541 #endif
6543 if (regno >= 0
6544 && reg_last_reload_reg[regno] != 0
6545 && (GET_MODE_SIZE (GET_MODE (reg_last_reload_reg[regno]))
6546 >= GET_MODE_SIZE (mode) + byte)
6547 #ifdef CANNOT_CHANGE_MODE_CLASS
6548 /* Verify that the register it's in can be used in
6549 mode MODE. */
6550 && !REG_CANNOT_CHANGE_MODE_P (REGNO (reg_last_reload_reg[regno]),
6551 GET_MODE (reg_last_reload_reg[regno]),
6552 mode)
6553 #endif
6556 enum reg_class rclass = rld[r].rclass, last_class;
6557 rtx last_reg = reg_last_reload_reg[regno];
6559 i = REGNO (last_reg);
6560 i += subreg_regno_offset (i, GET_MODE (last_reg), byte, mode);
6561 last_class = REGNO_REG_CLASS (i);
6563 if (reg_reloaded_contents[i] == regno
6564 && TEST_HARD_REG_BIT (reg_reloaded_valid, i)
6565 && HARD_REGNO_MODE_OK (i, rld[r].mode)
6566 && (TEST_HARD_REG_BIT (reg_class_contents[(int) rclass], i)
6567 /* Even if we can't use this register as a reload
6568 register, we might use it for reload_override_in,
6569 if copying it to the desired class is cheap
6570 enough. */
6571 || ((register_move_cost (mode, last_class, rclass)
6572 < memory_move_cost (mode, rclass, true))
6573 && (secondary_reload_class (1, rclass, mode,
6574 last_reg)
6575 == NO_REGS)
6576 #ifdef SECONDARY_MEMORY_NEEDED
6577 && ! SECONDARY_MEMORY_NEEDED (last_class, rclass,
6578 mode)
6579 #endif
6582 && (rld[r].nregs == max_group_size
6583 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
6585 && free_for_value_p (i, rld[r].mode, rld[r].opnum,
6586 rld[r].when_needed, rld[r].in,
6587 const0_rtx, r, 1))
6589 /* If a group is needed, verify that all the subsequent
6590 registers still have their values intact. */
6591 int nr = hard_regno_nregs[i][rld[r].mode];
6592 int k;
6594 for (k = 1; k < nr; k++)
6595 if (reg_reloaded_contents[i + k] != regno
6596 || ! TEST_HARD_REG_BIT (reg_reloaded_valid, i + k))
6597 break;
6599 if (k == nr)
6601 int i1;
6602 int bad_for_class;
6604 last_reg = (GET_MODE (last_reg) == mode
6605 ? last_reg : gen_rtx_REG (mode, i));
6607 bad_for_class = 0;
6608 for (k = 0; k < nr; k++)
6609 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6610 i+k);
6612 /* We found a register that contains the
6613 value we need. If this register is the
6614 same as an `earlyclobber' operand of the
6615 current insn, just mark it as a place to
6616 reload from since we can't use it as the
6617 reload register itself. */
6619 for (i1 = 0; i1 < n_earlyclobbers; i1++)
6620 if (reg_overlap_mentioned_for_reload_p
6621 (reg_last_reload_reg[regno],
6622 reload_earlyclobbers[i1]))
6623 break;
6625 if (i1 != n_earlyclobbers
6626 || ! (free_for_value_p (i, rld[r].mode,
6627 rld[r].opnum,
6628 rld[r].when_needed, rld[r].in,
6629 rld[r].out, r, 1))
6630 /* Don't use it if we'd clobber a pseudo reg. */
6631 || (TEST_HARD_REG_BIT (reg_used_in_insn, i)
6632 && rld[r].out
6633 && ! TEST_HARD_REG_BIT (reg_reloaded_dead, i))
6634 /* Don't clobber the frame pointer. */
6635 || (i == HARD_FRAME_POINTER_REGNUM
6636 && frame_pointer_needed
6637 && rld[r].out)
6638 /* Don't really use the inherited spill reg
6639 if we need it wider than we've got it. */
6640 || (GET_MODE_SIZE (rld[r].mode)
6641 > GET_MODE_SIZE (mode))
6642 || bad_for_class
6644 /* If find_reloads chose reload_out as reload
6645 register, stay with it - that leaves the
6646 inherited register for subsequent reloads. */
6647 || (rld[r].out && rld[r].reg_rtx
6648 && rtx_equal_p (rld[r].out, rld[r].reg_rtx)))
6650 if (! rld[r].optional)
6652 reload_override_in[r] = last_reg;
6653 reload_inheritance_insn[r]
6654 = reg_reloaded_insn[i];
6657 else
6659 int k;
6660 /* We can use this as a reload reg. */
6661 /* Mark the register as in use for this part of
6662 the insn. */
6663 mark_reload_reg_in_use (i,
6664 rld[r].opnum,
6665 rld[r].when_needed,
6666 rld[r].mode);
6667 rld[r].reg_rtx = last_reg;
6668 reload_inherited[r] = 1;
6669 reload_inheritance_insn[r]
6670 = reg_reloaded_insn[i];
6671 reload_spill_index[r] = i;
6672 for (k = 0; k < nr; k++)
6673 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6674 i + k);
6681 /* Here's another way to see if the value is already lying around. */
6682 if (inheritance
6683 && rld[r].in != 0
6684 && ! reload_inherited[r]
6685 && rld[r].out == 0
6686 && (CONSTANT_P (rld[r].in)
6687 || GET_CODE (rld[r].in) == PLUS
6688 || REG_P (rld[r].in)
6689 || MEM_P (rld[r].in))
6690 && (rld[r].nregs == max_group_size
6691 || ! reg_classes_intersect_p (rld[r].rclass, group_class)))
6692 search_equiv = rld[r].in;
6694 if (search_equiv)
6696 rtx equiv
6697 = find_equiv_reg (search_equiv, insn, rld[r].rclass,
6698 -1, NULL, 0, rld[r].mode);
6699 int regno = 0;
6701 if (equiv != 0)
6703 if (REG_P (equiv))
6704 regno = REGNO (equiv);
6705 else
6707 /* This must be a SUBREG of a hard register.
6708 Make a new REG since this might be used in an
6709 address and not all machines support SUBREGs
6710 there. */
6711 gcc_assert (GET_CODE (equiv) == SUBREG);
6712 regno = subreg_regno (equiv);
6713 equiv = gen_rtx_REG (rld[r].mode, regno);
6714 /* If we choose EQUIV as the reload register, but the
6715 loop below decides to cancel the inheritance, we'll
6716 end up reloading EQUIV in rld[r].mode, not the mode
6717 it had originally. That isn't safe when EQUIV isn't
6718 available as a spill register since its value might
6719 still be live at this point. */
6720 for (i = regno; i < regno + (int) rld[r].nregs; i++)
6721 if (TEST_HARD_REG_BIT (reload_reg_unavailable, i))
6722 equiv = 0;
6726 /* If we found a spill reg, reject it unless it is free
6727 and of the desired class. */
6728 if (equiv != 0)
6730 int regs_used = 0;
6731 int bad_for_class = 0;
6732 int max_regno = regno + rld[r].nregs;
6734 for (i = regno; i < max_regno; i++)
6736 regs_used |= TEST_HARD_REG_BIT (reload_reg_used_at_all,
6738 bad_for_class |= ! TEST_HARD_REG_BIT (reg_class_contents[(int) rld[r].rclass],
6742 if ((regs_used
6743 && ! free_for_value_p (regno, rld[r].mode,
6744 rld[r].opnum, rld[r].when_needed,
6745 rld[r].in, rld[r].out, r, 1))
6746 || bad_for_class)
6747 equiv = 0;
6750 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, rld[r].mode))
6751 equiv = 0;
6753 /* We found a register that contains the value we need.
6754 If this register is the same as an `earlyclobber' operand
6755 of the current insn, just mark it as a place to reload from
6756 since we can't use it as the reload register itself. */
6758 if (equiv != 0)
6759 for (i = 0; i < n_earlyclobbers; i++)
6760 if (reg_overlap_mentioned_for_reload_p (equiv,
6761 reload_earlyclobbers[i]))
6763 if (! rld[r].optional)
6764 reload_override_in[r] = equiv;
6765 equiv = 0;
6766 break;
6769 /* If the equiv register we have found is explicitly clobbered
6770 in the current insn, it depends on the reload type if we
6771 can use it, use it for reload_override_in, or not at all.
6772 In particular, we then can't use EQUIV for a
6773 RELOAD_FOR_OUTPUT_ADDRESS reload. */
6775 if (equiv != 0)
6777 if (regno_clobbered_p (regno, insn, rld[r].mode, 2))
6778 switch (rld[r].when_needed)
6780 case RELOAD_FOR_OTHER_ADDRESS:
6781 case RELOAD_FOR_INPADDR_ADDRESS:
6782 case RELOAD_FOR_INPUT_ADDRESS:
6783 case RELOAD_FOR_OPADDR_ADDR:
6784 break;
6785 case RELOAD_OTHER:
6786 case RELOAD_FOR_INPUT:
6787 case RELOAD_FOR_OPERAND_ADDRESS:
6788 if (! rld[r].optional)
6789 reload_override_in[r] = equiv;
6790 /* Fall through. */
6791 default:
6792 equiv = 0;
6793 break;
6795 else if (regno_clobbered_p (regno, insn, rld[r].mode, 1))
6796 switch (rld[r].when_needed)
6798 case RELOAD_FOR_OTHER_ADDRESS:
6799 case RELOAD_FOR_INPADDR_ADDRESS:
6800 case RELOAD_FOR_INPUT_ADDRESS:
6801 case RELOAD_FOR_OPADDR_ADDR:
6802 case RELOAD_FOR_OPERAND_ADDRESS:
6803 case RELOAD_FOR_INPUT:
6804 break;
6805 case RELOAD_OTHER:
6806 if (! rld[r].optional)
6807 reload_override_in[r] = equiv;
6808 /* Fall through. */
6809 default:
6810 equiv = 0;
6811 break;
6815 /* If we found an equivalent reg, say no code need be generated
6816 to load it, and use it as our reload reg. */
6817 if (equiv != 0
6818 && (regno != HARD_FRAME_POINTER_REGNUM
6819 || !frame_pointer_needed))
6821 int nr = hard_regno_nregs[regno][rld[r].mode];
6822 int k;
6823 rld[r].reg_rtx = equiv;
6824 reload_spill_index[r] = regno;
6825 reload_inherited[r] = 1;
6827 /* If reg_reloaded_valid is not set for this register,
6828 there might be a stale spill_reg_store lying around.
6829 We must clear it, since otherwise emit_reload_insns
6830 might delete the store. */
6831 if (! TEST_HARD_REG_BIT (reg_reloaded_valid, regno))
6832 spill_reg_store[regno] = NULL_RTX;
6833 /* If any of the hard registers in EQUIV are spill
6834 registers, mark them as in use for this insn. */
6835 for (k = 0; k < nr; k++)
6837 i = spill_reg_order[regno + k];
6838 if (i >= 0)
6840 mark_reload_reg_in_use (regno, rld[r].opnum,
6841 rld[r].when_needed,
6842 rld[r].mode);
6843 SET_HARD_REG_BIT (reload_reg_used_for_inherit,
6844 regno + k);
6850 /* If we found a register to use already, or if this is an optional
6851 reload, we are done. */
6852 if (rld[r].reg_rtx != 0 || rld[r].optional != 0)
6853 continue;
6855 #if 0
6856 /* No longer needed for correct operation. Might or might
6857 not give better code on the average. Want to experiment? */
6859 /* See if there is a later reload that has a class different from our
6860 class that intersects our class or that requires less register
6861 than our reload. If so, we must allocate a register to this
6862 reload now, since that reload might inherit a previous reload
6863 and take the only available register in our class. Don't do this
6864 for optional reloads since they will force all previous reloads
6865 to be allocated. Also don't do this for reloads that have been
6866 turned off. */
6868 for (i = j + 1; i < n_reloads; i++)
6870 int s = reload_order[i];
6872 if ((rld[s].in == 0 && rld[s].out == 0
6873 && ! rld[s].secondary_p)
6874 || rld[s].optional)
6875 continue;
6877 if ((rld[s].rclass != rld[r].rclass
6878 && reg_classes_intersect_p (rld[r].rclass,
6879 rld[s].rclass))
6880 || rld[s].nregs < rld[r].nregs)
6881 break;
6884 if (i == n_reloads)
6885 continue;
6887 allocate_reload_reg (chain, r, j == n_reloads - 1);
6888 #endif
6891 /* Now allocate reload registers for anything non-optional that
6892 didn't get one yet. */
6893 for (j = 0; j < n_reloads; j++)
6895 int r = reload_order[j];
6897 /* Ignore reloads that got marked inoperative. */
6898 if (rld[r].out == 0 && rld[r].in == 0 && ! rld[r].secondary_p)
6899 continue;
6901 /* Skip reloads that already have a register allocated or are
6902 optional. */
6903 if (rld[r].reg_rtx != 0 || rld[r].optional)
6904 continue;
6906 if (! allocate_reload_reg (chain, r, j == n_reloads - 1))
6907 break;
6910 /* If that loop got all the way, we have won. */
6911 if (j == n_reloads)
6913 win = 1;
6914 break;
6917 /* Loop around and try without any inheritance. */
6920 if (! win)
6922 /* First undo everything done by the failed attempt
6923 to allocate with inheritance. */
6924 choose_reload_regs_init (chain, save_reload_reg_rtx);
6926 /* Some sanity tests to verify that the reloads found in the first
6927 pass are identical to the ones we have now. */
6928 gcc_assert (chain->n_reloads == n_reloads);
6930 for (i = 0; i < n_reloads; i++)
6932 if (chain->rld[i].regno < 0 || chain->rld[i].reg_rtx != 0)
6933 continue;
6934 gcc_assert (chain->rld[i].when_needed == rld[i].when_needed);
6935 for (j = 0; j < n_spills; j++)
6936 if (spill_regs[j] == chain->rld[i].regno)
6937 if (! set_reload_reg (j, i))
6938 failed_reload (chain->insn, i);
6942 /* If we thought we could inherit a reload, because it seemed that
6943 nothing else wanted the same reload register earlier in the insn,
6944 verify that assumption, now that all reloads have been assigned.
6945 Likewise for reloads where reload_override_in has been set. */
6947 /* If doing expensive optimizations, do one preliminary pass that doesn't
6948 cancel any inheritance, but removes reloads that have been needed only
6949 for reloads that we know can be inherited. */
6950 for (pass = flag_expensive_optimizations; pass >= 0; pass--)
6952 for (j = 0; j < n_reloads; j++)
6954 int r = reload_order[j];
6955 rtx check_reg;
6956 if (reload_inherited[r] && rld[r].reg_rtx)
6957 check_reg = rld[r].reg_rtx;
6958 else if (reload_override_in[r]
6959 && (REG_P (reload_override_in[r])
6960 || GET_CODE (reload_override_in[r]) == SUBREG))
6961 check_reg = reload_override_in[r];
6962 else
6963 continue;
6964 if (! free_for_value_p (true_regnum (check_reg), rld[r].mode,
6965 rld[r].opnum, rld[r].when_needed, rld[r].in,
6966 (reload_inherited[r]
6967 ? rld[r].out : const0_rtx),
6968 r, 1))
6970 if (pass)
6971 continue;
6972 reload_inherited[r] = 0;
6973 reload_override_in[r] = 0;
6975 /* If we can inherit a RELOAD_FOR_INPUT, or can use a
6976 reload_override_in, then we do not need its related
6977 RELOAD_FOR_INPUT_ADDRESS / RELOAD_FOR_INPADDR_ADDRESS reloads;
6978 likewise for other reload types.
6979 We handle this by removing a reload when its only replacement
6980 is mentioned in reload_in of the reload we are going to inherit.
6981 A special case are auto_inc expressions; even if the input is
6982 inherited, we still need the address for the output. We can
6983 recognize them because they have RELOAD_OUT set to RELOAD_IN.
6984 If we succeeded removing some reload and we are doing a preliminary
6985 pass just to remove such reloads, make another pass, since the
6986 removal of one reload might allow us to inherit another one. */
6987 else if (rld[r].in
6988 && rld[r].out != rld[r].in
6989 && remove_address_replacements (rld[r].in) && pass)
6990 pass = 2;
6994 /* Now that reload_override_in is known valid,
6995 actually override reload_in. */
6996 for (j = 0; j < n_reloads; j++)
6997 if (reload_override_in[j])
6998 rld[j].in = reload_override_in[j];
7000 /* If this reload won't be done because it has been canceled or is
7001 optional and not inherited, clear reload_reg_rtx so other
7002 routines (such as subst_reloads) don't get confused. */
7003 for (j = 0; j < n_reloads; j++)
7004 if (rld[j].reg_rtx != 0
7005 && ((rld[j].optional && ! reload_inherited[j])
7006 || (rld[j].in == 0 && rld[j].out == 0
7007 && ! rld[j].secondary_p)))
7009 int regno = true_regnum (rld[j].reg_rtx);
7011 if (spill_reg_order[regno] >= 0)
7012 clear_reload_reg_in_use (regno, rld[j].opnum,
7013 rld[j].when_needed, rld[j].mode);
7014 rld[j].reg_rtx = 0;
7015 reload_spill_index[j] = -1;
7018 /* Record which pseudos and which spill regs have output reloads. */
7019 for (j = 0; j < n_reloads; j++)
7021 int r = reload_order[j];
7023 i = reload_spill_index[r];
7025 /* I is nonneg if this reload uses a register.
7026 If rld[r].reg_rtx is 0, this is an optional reload
7027 that we opted to ignore. */
7028 if (rld[r].out_reg != 0 && REG_P (rld[r].out_reg)
7029 && rld[r].reg_rtx != 0)
7031 int nregno = REGNO (rld[r].out_reg);
7032 int nr = 1;
7034 if (nregno < FIRST_PSEUDO_REGISTER)
7035 nr = hard_regno_nregs[nregno][rld[r].mode];
7037 while (--nr >= 0)
7038 SET_REGNO_REG_SET (&reg_has_output_reload,
7039 nregno + nr);
7041 if (i >= 0)
7042 add_to_hard_reg_set (&reg_is_output_reload, rld[r].mode, i);
7044 gcc_assert (rld[r].when_needed == RELOAD_OTHER
7045 || rld[r].when_needed == RELOAD_FOR_OUTPUT
7046 || rld[r].when_needed == RELOAD_FOR_INSN);
7051 /* Deallocate the reload register for reload R. This is called from
7052 remove_address_replacements. */
7054 void
7055 deallocate_reload_reg (int r)
7057 int regno;
7059 if (! rld[r].reg_rtx)
7060 return;
7061 regno = true_regnum (rld[r].reg_rtx);
7062 rld[r].reg_rtx = 0;
7063 if (spill_reg_order[regno] >= 0)
7064 clear_reload_reg_in_use (regno, rld[r].opnum, rld[r].when_needed,
7065 rld[r].mode);
7066 reload_spill_index[r] = -1;
7069 /* These arrays are filled by emit_reload_insns and its subroutines. */
7070 static rtx input_reload_insns[MAX_RECOG_OPERANDS];
7071 static rtx other_input_address_reload_insns = 0;
7072 static rtx other_input_reload_insns = 0;
7073 static rtx input_address_reload_insns[MAX_RECOG_OPERANDS];
7074 static rtx inpaddr_address_reload_insns[MAX_RECOG_OPERANDS];
7075 static rtx output_reload_insns[MAX_RECOG_OPERANDS];
7076 static rtx output_address_reload_insns[MAX_RECOG_OPERANDS];
7077 static rtx outaddr_address_reload_insns[MAX_RECOG_OPERANDS];
7078 static rtx operand_reload_insns = 0;
7079 static rtx other_operand_reload_insns = 0;
7080 static rtx other_output_reload_insns[MAX_RECOG_OPERANDS];
7082 /* Values to be put in spill_reg_store are put here first. Instructions
7083 must only be placed here if the associated reload register reaches
7084 the end of the instruction's reload sequence. */
7085 static rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
7086 static HARD_REG_SET reg_reloaded_died;
7088 /* Check if *RELOAD_REG is suitable as an intermediate or scratch register
7089 of class NEW_CLASS with mode NEW_MODE. Or alternatively, if alt_reload_reg
7090 is nonzero, if that is suitable. On success, change *RELOAD_REG to the
7091 adjusted register, and return true. Otherwise, return false. */
7092 static bool
7093 reload_adjust_reg_for_temp (rtx *reload_reg, rtx alt_reload_reg,
7094 enum reg_class new_class,
7095 enum machine_mode new_mode)
7098 rtx reg;
7100 for (reg = *reload_reg; reg; reg = alt_reload_reg, alt_reload_reg = 0)
7102 unsigned regno = REGNO (reg);
7104 if (!TEST_HARD_REG_BIT (reg_class_contents[(int) new_class], regno))
7105 continue;
7106 if (GET_MODE (reg) != new_mode)
7108 if (!HARD_REGNO_MODE_OK (regno, new_mode))
7109 continue;
7110 if (hard_regno_nregs[regno][new_mode]
7111 > hard_regno_nregs[regno][GET_MODE (reg)])
7112 continue;
7113 reg = reload_adjust_reg_for_mode (reg, new_mode);
7115 *reload_reg = reg;
7116 return true;
7118 return false;
7121 /* Check if *RELOAD_REG is suitable as a scratch register for the reload
7122 pattern with insn_code ICODE, or alternatively, if alt_reload_reg is
7123 nonzero, if that is suitable. On success, change *RELOAD_REG to the
7124 adjusted register, and return true. Otherwise, return false. */
7125 static bool
7126 reload_adjust_reg_for_icode (rtx *reload_reg, rtx alt_reload_reg,
7127 enum insn_code icode)
7130 enum reg_class new_class = scratch_reload_class (icode);
7131 enum machine_mode new_mode = insn_data[(int) icode].operand[2].mode;
7133 return reload_adjust_reg_for_temp (reload_reg, alt_reload_reg,
7134 new_class, new_mode);
7137 /* Generate insns to perform reload RL, which is for the insn in CHAIN and
7138 has the number J. OLD contains the value to be used as input. */
7140 static void
7141 emit_input_reload_insns (struct insn_chain *chain, struct reload *rl,
7142 rtx old, int j)
7144 rtx insn = chain->insn;
7145 rtx reloadreg;
7146 rtx oldequiv_reg = 0;
7147 rtx oldequiv = 0;
7148 int special = 0;
7149 enum machine_mode mode;
7150 rtx *where;
7152 /* delete_output_reload is only invoked properly if old contains
7153 the original pseudo register. Since this is replaced with a
7154 hard reg when RELOAD_OVERRIDE_IN is set, see if we can
7155 find the pseudo in RELOAD_IN_REG. */
7156 if (reload_override_in[j]
7157 && REG_P (rl->in_reg))
7159 oldequiv = old;
7160 old = rl->in_reg;
7162 if (oldequiv == 0)
7163 oldequiv = old;
7164 else if (REG_P (oldequiv))
7165 oldequiv_reg = oldequiv;
7166 else if (GET_CODE (oldequiv) == SUBREG)
7167 oldequiv_reg = SUBREG_REG (oldequiv);
7169 reloadreg = reload_reg_rtx_for_input[j];
7170 mode = GET_MODE (reloadreg);
7172 /* If we are reloading from a register that was recently stored in
7173 with an output-reload, see if we can prove there was
7174 actually no need to store the old value in it. */
7176 if (optimize && REG_P (oldequiv)
7177 && REGNO (oldequiv) < FIRST_PSEUDO_REGISTER
7178 && spill_reg_store[REGNO (oldequiv)]
7179 && REG_P (old)
7180 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (oldequiv)])
7181 || rtx_equal_p (spill_reg_stored_to[REGNO (oldequiv)],
7182 rl->out_reg)))
7183 delete_output_reload (insn, j, REGNO (oldequiv), reloadreg);
7185 /* Encapsulate OLDEQUIV into the reload mode, then load RELOADREG from
7186 OLDEQUIV. */
7188 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
7189 oldequiv = SUBREG_REG (oldequiv);
7190 if (GET_MODE (oldequiv) != VOIDmode
7191 && mode != GET_MODE (oldequiv))
7192 oldequiv = gen_lowpart_SUBREG (mode, oldequiv);
7194 /* Switch to the right place to emit the reload insns. */
7195 switch (rl->when_needed)
7197 case RELOAD_OTHER:
7198 where = &other_input_reload_insns;
7199 break;
7200 case RELOAD_FOR_INPUT:
7201 where = &input_reload_insns[rl->opnum];
7202 break;
7203 case RELOAD_FOR_INPUT_ADDRESS:
7204 where = &input_address_reload_insns[rl->opnum];
7205 break;
7206 case RELOAD_FOR_INPADDR_ADDRESS:
7207 where = &inpaddr_address_reload_insns[rl->opnum];
7208 break;
7209 case RELOAD_FOR_OUTPUT_ADDRESS:
7210 where = &output_address_reload_insns[rl->opnum];
7211 break;
7212 case RELOAD_FOR_OUTADDR_ADDRESS:
7213 where = &outaddr_address_reload_insns[rl->opnum];
7214 break;
7215 case RELOAD_FOR_OPERAND_ADDRESS:
7216 where = &operand_reload_insns;
7217 break;
7218 case RELOAD_FOR_OPADDR_ADDR:
7219 where = &other_operand_reload_insns;
7220 break;
7221 case RELOAD_FOR_OTHER_ADDRESS:
7222 where = &other_input_address_reload_insns;
7223 break;
7224 default:
7225 gcc_unreachable ();
7228 push_to_sequence (*where);
7230 /* Auto-increment addresses must be reloaded in a special way. */
7231 if (rl->out && ! rl->out_reg)
7233 /* We are not going to bother supporting the case where a
7234 incremented register can't be copied directly from
7235 OLDEQUIV since this seems highly unlikely. */
7236 gcc_assert (rl->secondary_in_reload < 0);
7238 if (reload_inherited[j])
7239 oldequiv = reloadreg;
7241 old = XEXP (rl->in_reg, 0);
7243 /* Prevent normal processing of this reload. */
7244 special = 1;
7245 /* Output a special code sequence for this case. */
7246 inc_for_reload (reloadreg, oldequiv, rl->out, rl->inc);
7249 /* If we are reloading a pseudo-register that was set by the previous
7250 insn, see if we can get rid of that pseudo-register entirely
7251 by redirecting the previous insn into our reload register. */
7253 else if (optimize && REG_P (old)
7254 && REGNO (old) >= FIRST_PSEUDO_REGISTER
7255 && dead_or_set_p (insn, old)
7256 /* This is unsafe if some other reload
7257 uses the same reg first. */
7258 && ! conflicts_with_override (reloadreg)
7259 && free_for_value_p (REGNO (reloadreg), rl->mode, rl->opnum,
7260 rl->when_needed, old, rl->out, j, 0))
7262 rtx temp = PREV_INSN (insn);
7263 while (temp && (NOTE_P (temp) || DEBUG_INSN_P (temp)))
7264 temp = PREV_INSN (temp);
7265 if (temp
7266 && NONJUMP_INSN_P (temp)
7267 && GET_CODE (PATTERN (temp)) == SET
7268 && SET_DEST (PATTERN (temp)) == old
7269 /* Make sure we can access insn_operand_constraint. */
7270 && asm_noperands (PATTERN (temp)) < 0
7271 /* This is unsafe if operand occurs more than once in current
7272 insn. Perhaps some occurrences aren't reloaded. */
7273 && count_occurrences (PATTERN (insn), old, 0) == 1)
7275 rtx old = SET_DEST (PATTERN (temp));
7276 /* Store into the reload register instead of the pseudo. */
7277 SET_DEST (PATTERN (temp)) = reloadreg;
7279 /* Verify that resulting insn is valid. */
7280 extract_insn (temp);
7281 if (constrain_operands (1))
7283 /* If the previous insn is an output reload, the source is
7284 a reload register, and its spill_reg_store entry will
7285 contain the previous destination. This is now
7286 invalid. */
7287 if (REG_P (SET_SRC (PATTERN (temp)))
7288 && REGNO (SET_SRC (PATTERN (temp))) < FIRST_PSEUDO_REGISTER)
7290 spill_reg_store[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7291 spill_reg_stored_to[REGNO (SET_SRC (PATTERN (temp)))] = 0;
7294 /* If these are the only uses of the pseudo reg,
7295 pretend for GDB it lives in the reload reg we used. */
7296 if (REG_N_DEATHS (REGNO (old)) == 1
7297 && REG_N_SETS (REGNO (old)) == 1)
7299 reg_renumber[REGNO (old)] = REGNO (reloadreg);
7300 if (ira_conflicts_p)
7301 /* Inform IRA about the change. */
7302 ira_mark_allocation_change (REGNO (old));
7303 alter_reg (REGNO (old), -1, false);
7305 special = 1;
7307 /* Adjust any debug insns between temp and insn. */
7308 while ((temp = NEXT_INSN (temp)) != insn)
7309 if (DEBUG_INSN_P (temp))
7310 replace_rtx (PATTERN (temp), old, reloadreg);
7311 else
7312 gcc_assert (NOTE_P (temp));
7314 else
7316 SET_DEST (PATTERN (temp)) = old;
7321 /* We can't do that, so output an insn to load RELOADREG. */
7323 /* If we have a secondary reload, pick up the secondary register
7324 and icode, if any. If OLDEQUIV and OLD are different or
7325 if this is an in-out reload, recompute whether or not we
7326 still need a secondary register and what the icode should
7327 be. If we still need a secondary register and the class or
7328 icode is different, go back to reloading from OLD if using
7329 OLDEQUIV means that we got the wrong type of register. We
7330 cannot have different class or icode due to an in-out reload
7331 because we don't make such reloads when both the input and
7332 output need secondary reload registers. */
7334 if (! special && rl->secondary_in_reload >= 0)
7336 rtx second_reload_reg = 0;
7337 rtx third_reload_reg = 0;
7338 int secondary_reload = rl->secondary_in_reload;
7339 rtx real_oldequiv = oldequiv;
7340 rtx real_old = old;
7341 rtx tmp;
7342 enum insn_code icode;
7343 enum insn_code tertiary_icode = CODE_FOR_nothing;
7345 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
7346 and similarly for OLD.
7347 See comments in get_secondary_reload in reload.c. */
7348 /* If it is a pseudo that cannot be replaced with its
7349 equivalent MEM, we must fall back to reload_in, which
7350 will have all the necessary substitutions registered.
7351 Likewise for a pseudo that can't be replaced with its
7352 equivalent constant.
7354 Take extra care for subregs of such pseudos. Note that
7355 we cannot use reg_equiv_mem in this case because it is
7356 not in the right mode. */
7358 tmp = oldequiv;
7359 if (GET_CODE (tmp) == SUBREG)
7360 tmp = SUBREG_REG (tmp);
7361 if (REG_P (tmp)
7362 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7363 && (reg_equiv_memory_loc (REGNO (tmp)) != 0
7364 || reg_equiv_constant (REGNO (tmp)) != 0))
7366 if (! reg_equiv_mem (REGNO (tmp))
7367 || num_not_at_initial_offset
7368 || GET_CODE (oldequiv) == SUBREG)
7369 real_oldequiv = rl->in;
7370 else
7371 real_oldequiv = reg_equiv_mem (REGNO (tmp));
7374 tmp = old;
7375 if (GET_CODE (tmp) == SUBREG)
7376 tmp = SUBREG_REG (tmp);
7377 if (REG_P (tmp)
7378 && REGNO (tmp) >= FIRST_PSEUDO_REGISTER
7379 && (reg_equiv_memory_loc (REGNO (tmp)) != 0
7380 || reg_equiv_constant (REGNO (tmp)) != 0))
7382 if (! reg_equiv_mem (REGNO (tmp))
7383 || num_not_at_initial_offset
7384 || GET_CODE (old) == SUBREG)
7385 real_old = rl->in;
7386 else
7387 real_old = reg_equiv_mem (REGNO (tmp));
7390 second_reload_reg = rld[secondary_reload].reg_rtx;
7391 if (rld[secondary_reload].secondary_in_reload >= 0)
7393 int tertiary_reload = rld[secondary_reload].secondary_in_reload;
7395 third_reload_reg = rld[tertiary_reload].reg_rtx;
7396 tertiary_icode = rld[secondary_reload].secondary_in_icode;
7397 /* We'd have to add more code for quartary reloads. */
7398 gcc_assert (rld[tertiary_reload].secondary_in_reload < 0);
7400 icode = rl->secondary_in_icode;
7402 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
7403 || (rl->in != 0 && rl->out != 0))
7405 secondary_reload_info sri, sri2;
7406 enum reg_class new_class, new_t_class;
7408 sri.icode = CODE_FOR_nothing;
7409 sri.prev_sri = NULL;
7410 new_class
7411 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7412 rl->rclass, mode,
7413 &sri);
7415 if (new_class == NO_REGS && sri.icode == CODE_FOR_nothing)
7416 second_reload_reg = 0;
7417 else if (new_class == NO_REGS)
7419 if (reload_adjust_reg_for_icode (&second_reload_reg,
7420 third_reload_reg,
7421 (enum insn_code) sri.icode))
7423 icode = (enum insn_code) sri.icode;
7424 third_reload_reg = 0;
7426 else
7428 oldequiv = old;
7429 real_oldequiv = real_old;
7432 else if (sri.icode != CODE_FOR_nothing)
7433 /* We currently lack a way to express this in reloads. */
7434 gcc_unreachable ();
7435 else
7437 sri2.icode = CODE_FOR_nothing;
7438 sri2.prev_sri = &sri;
7439 new_t_class
7440 = (enum reg_class) targetm.secondary_reload (1, real_oldequiv,
7441 new_class, mode,
7442 &sri);
7443 if (new_t_class == NO_REGS && sri2.icode == CODE_FOR_nothing)
7445 if (reload_adjust_reg_for_temp (&second_reload_reg,
7446 third_reload_reg,
7447 new_class, mode))
7449 third_reload_reg = 0;
7450 tertiary_icode = (enum insn_code) sri2.icode;
7452 else
7454 oldequiv = old;
7455 real_oldequiv = real_old;
7458 else if (new_t_class == NO_REGS && sri2.icode != CODE_FOR_nothing)
7460 rtx intermediate = second_reload_reg;
7462 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7463 new_class, mode)
7464 && reload_adjust_reg_for_icode (&third_reload_reg, NULL,
7465 ((enum insn_code)
7466 sri2.icode)))
7468 second_reload_reg = intermediate;
7469 tertiary_icode = (enum insn_code) sri2.icode;
7471 else
7473 oldequiv = old;
7474 real_oldequiv = real_old;
7477 else if (new_t_class != NO_REGS && sri2.icode == CODE_FOR_nothing)
7479 rtx intermediate = second_reload_reg;
7481 if (reload_adjust_reg_for_temp (&intermediate, NULL,
7482 new_class, mode)
7483 && reload_adjust_reg_for_temp (&third_reload_reg, NULL,
7484 new_t_class, mode))
7486 second_reload_reg = intermediate;
7487 tertiary_icode = (enum insn_code) sri2.icode;
7489 else
7491 oldequiv = old;
7492 real_oldequiv = real_old;
7495 else
7497 /* This could be handled more intelligently too. */
7498 oldequiv = old;
7499 real_oldequiv = real_old;
7504 /* If we still need a secondary reload register, check
7505 to see if it is being used as a scratch or intermediate
7506 register and generate code appropriately. If we need
7507 a scratch register, use REAL_OLDEQUIV since the form of
7508 the insn may depend on the actual address if it is
7509 a MEM. */
7511 if (second_reload_reg)
7513 if (icode != CODE_FOR_nothing)
7515 /* We'd have to add extra code to handle this case. */
7516 gcc_assert (!third_reload_reg);
7518 emit_insn (GEN_FCN (icode) (reloadreg, real_oldequiv,
7519 second_reload_reg));
7520 special = 1;
7522 else
7524 /* See if we need a scratch register to load the
7525 intermediate register (a tertiary reload). */
7526 if (tertiary_icode != CODE_FOR_nothing)
7528 emit_insn ((GEN_FCN (tertiary_icode)
7529 (second_reload_reg, real_oldequiv,
7530 third_reload_reg)));
7532 else if (third_reload_reg)
7534 gen_reload (third_reload_reg, real_oldequiv,
7535 rl->opnum,
7536 rl->when_needed);
7537 gen_reload (second_reload_reg, third_reload_reg,
7538 rl->opnum,
7539 rl->when_needed);
7541 else
7542 gen_reload (second_reload_reg, real_oldequiv,
7543 rl->opnum,
7544 rl->when_needed);
7546 oldequiv = second_reload_reg;
7551 if (! special && ! rtx_equal_p (reloadreg, oldequiv))
7553 rtx real_oldequiv = oldequiv;
7555 if ((REG_P (oldequiv)
7556 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
7557 && (reg_equiv_memory_loc (REGNO (oldequiv)) != 0
7558 || reg_equiv_constant (REGNO (oldequiv)) != 0))
7559 || (GET_CODE (oldequiv) == SUBREG
7560 && REG_P (SUBREG_REG (oldequiv))
7561 && (REGNO (SUBREG_REG (oldequiv))
7562 >= FIRST_PSEUDO_REGISTER)
7563 && ((reg_equiv_memory_loc (REGNO (SUBREG_REG (oldequiv))) != 0)
7564 || (reg_equiv_constant (REGNO (SUBREG_REG (oldequiv))) != 0)))
7565 || (CONSTANT_P (oldequiv)
7566 && (targetm.preferred_reload_class (oldequiv,
7567 REGNO_REG_CLASS (REGNO (reloadreg)))
7568 == NO_REGS)))
7569 real_oldequiv = rl->in;
7570 gen_reload (reloadreg, real_oldequiv, rl->opnum,
7571 rl->when_needed);
7574 if (cfun->can_throw_non_call_exceptions)
7575 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7577 /* End this sequence. */
7578 *where = get_insns ();
7579 end_sequence ();
7581 /* Update reload_override_in so that delete_address_reloads_1
7582 can see the actual register usage. */
7583 if (oldequiv_reg)
7584 reload_override_in[j] = oldequiv;
7587 /* Generate insns to for the output reload RL, which is for the insn described
7588 by CHAIN and has the number J. */
7589 static void
7590 emit_output_reload_insns (struct insn_chain *chain, struct reload *rl,
7591 int j)
7593 rtx reloadreg;
7594 rtx insn = chain->insn;
7595 int special = 0;
7596 rtx old = rl->out;
7597 enum machine_mode mode;
7598 rtx p;
7599 rtx rl_reg_rtx;
7601 if (rl->when_needed == RELOAD_OTHER)
7602 start_sequence ();
7603 else
7604 push_to_sequence (output_reload_insns[rl->opnum]);
7606 rl_reg_rtx = reload_reg_rtx_for_output[j];
7607 mode = GET_MODE (rl_reg_rtx);
7609 reloadreg = rl_reg_rtx;
7611 /* If we need two reload regs, set RELOADREG to the intermediate
7612 one, since it will be stored into OLD. We might need a secondary
7613 register only for an input reload, so check again here. */
7615 if (rl->secondary_out_reload >= 0)
7617 rtx real_old = old;
7618 int secondary_reload = rl->secondary_out_reload;
7619 int tertiary_reload = rld[secondary_reload].secondary_out_reload;
7621 if (REG_P (old) && REGNO (old) >= FIRST_PSEUDO_REGISTER
7622 && reg_equiv_mem (REGNO (old)) != 0)
7623 real_old = reg_equiv_mem (REGNO (old));
7625 if (secondary_reload_class (0, rl->rclass, mode, real_old) != NO_REGS)
7627 rtx second_reloadreg = reloadreg;
7628 reloadreg = rld[secondary_reload].reg_rtx;
7630 /* See if RELOADREG is to be used as a scratch register
7631 or as an intermediate register. */
7632 if (rl->secondary_out_icode != CODE_FOR_nothing)
7634 /* We'd have to add extra code to handle this case. */
7635 gcc_assert (tertiary_reload < 0);
7637 emit_insn ((GEN_FCN (rl->secondary_out_icode)
7638 (real_old, second_reloadreg, reloadreg)));
7639 special = 1;
7641 else
7643 /* See if we need both a scratch and intermediate reload
7644 register. */
7646 enum insn_code tertiary_icode
7647 = rld[secondary_reload].secondary_out_icode;
7649 /* We'd have to add more code for quartary reloads. */
7650 gcc_assert (tertiary_reload < 0
7651 || rld[tertiary_reload].secondary_out_reload < 0);
7653 if (GET_MODE (reloadreg) != mode)
7654 reloadreg = reload_adjust_reg_for_mode (reloadreg, mode);
7656 if (tertiary_icode != CODE_FOR_nothing)
7658 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7660 /* Copy primary reload reg to secondary reload reg.
7661 (Note that these have been swapped above, then
7662 secondary reload reg to OLD using our insn.) */
7664 /* If REAL_OLD is a paradoxical SUBREG, remove it
7665 and try to put the opposite SUBREG on
7666 RELOADREG. */
7667 strip_paradoxical_subreg (&real_old, &reloadreg);
7669 gen_reload (reloadreg, second_reloadreg,
7670 rl->opnum, rl->when_needed);
7671 emit_insn ((GEN_FCN (tertiary_icode)
7672 (real_old, reloadreg, third_reloadreg)));
7673 special = 1;
7676 else
7678 /* Copy between the reload regs here and then to
7679 OUT later. */
7681 gen_reload (reloadreg, second_reloadreg,
7682 rl->opnum, rl->when_needed);
7683 if (tertiary_reload >= 0)
7685 rtx third_reloadreg = rld[tertiary_reload].reg_rtx;
7687 gen_reload (third_reloadreg, reloadreg,
7688 rl->opnum, rl->when_needed);
7689 reloadreg = third_reloadreg;
7696 /* Output the last reload insn. */
7697 if (! special)
7699 rtx set;
7701 /* Don't output the last reload if OLD is not the dest of
7702 INSN and is in the src and is clobbered by INSN. */
7703 if (! flag_expensive_optimizations
7704 || !REG_P (old)
7705 || !(set = single_set (insn))
7706 || rtx_equal_p (old, SET_DEST (set))
7707 || !reg_mentioned_p (old, SET_SRC (set))
7708 || !((REGNO (old) < FIRST_PSEUDO_REGISTER)
7709 && regno_clobbered_p (REGNO (old), insn, rl->mode, 0)))
7710 gen_reload (old, reloadreg, rl->opnum,
7711 rl->when_needed);
7714 /* Look at all insns we emitted, just to be safe. */
7715 for (p = get_insns (); p; p = NEXT_INSN (p))
7716 if (INSN_P (p))
7718 rtx pat = PATTERN (p);
7720 /* If this output reload doesn't come from a spill reg,
7721 clear any memory of reloaded copies of the pseudo reg.
7722 If this output reload comes from a spill reg,
7723 reg_has_output_reload will make this do nothing. */
7724 note_stores (pat, forget_old_reloads_1, NULL);
7726 if (reg_mentioned_p (rl_reg_rtx, pat))
7728 rtx set = single_set (insn);
7729 if (reload_spill_index[j] < 0
7730 && set
7731 && SET_SRC (set) == rl_reg_rtx)
7733 int src = REGNO (SET_SRC (set));
7735 reload_spill_index[j] = src;
7736 SET_HARD_REG_BIT (reg_is_output_reload, src);
7737 if (find_regno_note (insn, REG_DEAD, src))
7738 SET_HARD_REG_BIT (reg_reloaded_died, src);
7740 if (HARD_REGISTER_P (rl_reg_rtx))
7742 int s = rl->secondary_out_reload;
7743 set = single_set (p);
7744 /* If this reload copies only to the secondary reload
7745 register, the secondary reload does the actual
7746 store. */
7747 if (s >= 0 && set == NULL_RTX)
7748 /* We can't tell what function the secondary reload
7749 has and where the actual store to the pseudo is
7750 made; leave new_spill_reg_store alone. */
7752 else if (s >= 0
7753 && SET_SRC (set) == rl_reg_rtx
7754 && SET_DEST (set) == rld[s].reg_rtx)
7756 /* Usually the next instruction will be the
7757 secondary reload insn; if we can confirm
7758 that it is, setting new_spill_reg_store to
7759 that insn will allow an extra optimization. */
7760 rtx s_reg = rld[s].reg_rtx;
7761 rtx next = NEXT_INSN (p);
7762 rld[s].out = rl->out;
7763 rld[s].out_reg = rl->out_reg;
7764 set = single_set (next);
7765 if (set && SET_SRC (set) == s_reg
7766 && reload_reg_rtx_reaches_end_p (s_reg, s))
7768 SET_HARD_REG_BIT (reg_is_output_reload,
7769 REGNO (s_reg));
7770 new_spill_reg_store[REGNO (s_reg)] = next;
7773 else if (reload_reg_rtx_reaches_end_p (rl_reg_rtx, j))
7774 new_spill_reg_store[REGNO (rl_reg_rtx)] = p;
7779 if (rl->when_needed == RELOAD_OTHER)
7781 emit_insn (other_output_reload_insns[rl->opnum]);
7782 other_output_reload_insns[rl->opnum] = get_insns ();
7784 else
7785 output_reload_insns[rl->opnum] = get_insns ();
7787 if (cfun->can_throw_non_call_exceptions)
7788 copy_reg_eh_region_note_forward (insn, get_insns (), NULL);
7790 end_sequence ();
7793 /* Do input reloading for reload RL, which is for the insn described by CHAIN
7794 and has the number J. */
7795 static void
7796 do_input_reload (struct insn_chain *chain, struct reload *rl, int j)
7798 rtx insn = chain->insn;
7799 rtx old = (rl->in && MEM_P (rl->in)
7800 ? rl->in_reg : rl->in);
7801 rtx reg_rtx = rl->reg_rtx;
7803 if (old && reg_rtx)
7805 enum machine_mode mode;
7807 /* Determine the mode to reload in.
7808 This is very tricky because we have three to choose from.
7809 There is the mode the insn operand wants (rl->inmode).
7810 There is the mode of the reload register RELOADREG.
7811 There is the intrinsic mode of the operand, which we could find
7812 by stripping some SUBREGs.
7813 It turns out that RELOADREG's mode is irrelevant:
7814 we can change that arbitrarily.
7816 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
7817 then the reload reg may not support QImode moves, so use SImode.
7818 If foo is in memory due to spilling a pseudo reg, this is safe,
7819 because the QImode value is in the least significant part of a
7820 slot big enough for a SImode. If foo is some other sort of
7821 memory reference, then it is impossible to reload this case,
7822 so previous passes had better make sure this never happens.
7824 Then consider a one-word union which has SImode and one of its
7825 members is a float, being fetched as (SUBREG:SF union:SI).
7826 We must fetch that as SFmode because we could be loading into
7827 a float-only register. In this case OLD's mode is correct.
7829 Consider an immediate integer: it has VOIDmode. Here we need
7830 to get a mode from something else.
7832 In some cases, there is a fourth mode, the operand's
7833 containing mode. If the insn specifies a containing mode for
7834 this operand, it overrides all others.
7836 I am not sure whether the algorithm here is always right,
7837 but it does the right things in those cases. */
7839 mode = GET_MODE (old);
7840 if (mode == VOIDmode)
7841 mode = rl->inmode;
7843 /* We cannot use gen_lowpart_common since it can do the wrong thing
7844 when REG_RTX has a multi-word mode. Note that REG_RTX must
7845 always be a REG here. */
7846 if (GET_MODE (reg_rtx) != mode)
7847 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7849 reload_reg_rtx_for_input[j] = reg_rtx;
7851 if (old != 0
7852 /* AUTO_INC reloads need to be handled even if inherited. We got an
7853 AUTO_INC reload if reload_out is set but reload_out_reg isn't. */
7854 && (! reload_inherited[j] || (rl->out && ! rl->out_reg))
7855 && ! rtx_equal_p (reg_rtx, old)
7856 && reg_rtx != 0)
7857 emit_input_reload_insns (chain, rld + j, old, j);
7859 /* When inheriting a wider reload, we have a MEM in rl->in,
7860 e.g. inheriting a SImode output reload for
7861 (mem:HI (plus:SI (reg:SI 14 fp) (const_int 10))) */
7862 if (optimize && reload_inherited[j] && rl->in
7863 && MEM_P (rl->in)
7864 && MEM_P (rl->in_reg)
7865 && reload_spill_index[j] >= 0
7866 && TEST_HARD_REG_BIT (reg_reloaded_valid, reload_spill_index[j]))
7867 rl->in = regno_reg_rtx[reg_reloaded_contents[reload_spill_index[j]]];
7869 /* If we are reloading a register that was recently stored in with an
7870 output-reload, see if we can prove there was
7871 actually no need to store the old value in it. */
7873 if (optimize
7874 && (reload_inherited[j] || reload_override_in[j])
7875 && reg_rtx
7876 && REG_P (reg_rtx)
7877 && spill_reg_store[REGNO (reg_rtx)] != 0
7878 #if 0
7879 /* There doesn't seem to be any reason to restrict this to pseudos
7880 and doing so loses in the case where we are copying from a
7881 register of the wrong class. */
7882 && !HARD_REGISTER_P (spill_reg_stored_to[REGNO (reg_rtx)])
7883 #endif
7884 /* The insn might have already some references to stackslots
7885 replaced by MEMs, while reload_out_reg still names the
7886 original pseudo. */
7887 && (dead_or_set_p (insn, spill_reg_stored_to[REGNO (reg_rtx)])
7888 || rtx_equal_p (spill_reg_stored_to[REGNO (reg_rtx)], rl->out_reg)))
7889 delete_output_reload (insn, j, REGNO (reg_rtx), reg_rtx);
7892 /* Do output reloading for reload RL, which is for the insn described by
7893 CHAIN and has the number J.
7894 ??? At some point we need to support handling output reloads of
7895 JUMP_INSNs or insns that set cc0. */
7896 static void
7897 do_output_reload (struct insn_chain *chain, struct reload *rl, int j)
7899 rtx note, old;
7900 rtx insn = chain->insn;
7901 /* If this is an output reload that stores something that is
7902 not loaded in this same reload, see if we can eliminate a previous
7903 store. */
7904 rtx pseudo = rl->out_reg;
7905 rtx reg_rtx = rl->reg_rtx;
7907 if (rl->out && reg_rtx)
7909 enum machine_mode mode;
7911 /* Determine the mode to reload in.
7912 See comments above (for input reloading). */
7913 mode = GET_MODE (rl->out);
7914 if (mode == VOIDmode)
7916 /* VOIDmode should never happen for an output. */
7917 if (asm_noperands (PATTERN (insn)) < 0)
7918 /* It's the compiler's fault. */
7919 fatal_insn ("VOIDmode on an output", insn);
7920 error_for_asm (insn, "output operand is constant in %<asm%>");
7921 /* Prevent crash--use something we know is valid. */
7922 mode = word_mode;
7923 rl->out = gen_rtx_REG (mode, REGNO (reg_rtx));
7925 if (GET_MODE (reg_rtx) != mode)
7926 reg_rtx = reload_adjust_reg_for_mode (reg_rtx, mode);
7928 reload_reg_rtx_for_output[j] = reg_rtx;
7930 if (pseudo
7931 && optimize
7932 && REG_P (pseudo)
7933 && ! rtx_equal_p (rl->in_reg, pseudo)
7934 && REGNO (pseudo) >= FIRST_PSEUDO_REGISTER
7935 && reg_last_reload_reg[REGNO (pseudo)])
7937 int pseudo_no = REGNO (pseudo);
7938 int last_regno = REGNO (reg_last_reload_reg[pseudo_no]);
7940 /* We don't need to test full validity of last_regno for
7941 inherit here; we only want to know if the store actually
7942 matches the pseudo. */
7943 if (TEST_HARD_REG_BIT (reg_reloaded_valid, last_regno)
7944 && reg_reloaded_contents[last_regno] == pseudo_no
7945 && spill_reg_store[last_regno]
7946 && rtx_equal_p (pseudo, spill_reg_stored_to[last_regno]))
7947 delete_output_reload (insn, j, last_regno, reg_rtx);
7950 old = rl->out_reg;
7951 if (old == 0
7952 || reg_rtx == 0
7953 || rtx_equal_p (old, reg_rtx))
7954 return;
7956 /* An output operand that dies right away does need a reload,
7957 but need not be copied from it. Show the new location in the
7958 REG_UNUSED note. */
7959 if ((REG_P (old) || GET_CODE (old) == SCRATCH)
7960 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
7962 XEXP (note, 0) = reg_rtx;
7963 return;
7965 /* Likewise for a SUBREG of an operand that dies. */
7966 else if (GET_CODE (old) == SUBREG
7967 && REG_P (SUBREG_REG (old))
7968 && 0 != (note = find_reg_note (insn, REG_UNUSED,
7969 SUBREG_REG (old))))
7971 XEXP (note, 0) = gen_lowpart_common (GET_MODE (old), reg_rtx);
7972 return;
7974 else if (GET_CODE (old) == SCRATCH)
7975 /* If we aren't optimizing, there won't be a REG_UNUSED note,
7976 but we don't want to make an output reload. */
7977 return;
7979 /* If is a JUMP_INSN, we can't support output reloads yet. */
7980 gcc_assert (NONJUMP_INSN_P (insn));
7982 emit_output_reload_insns (chain, rld + j, j);
7985 /* A reload copies values of MODE from register SRC to register DEST.
7986 Return true if it can be treated for inheritance purposes like a
7987 group of reloads, each one reloading a single hard register. The
7988 caller has already checked that (reg:MODE SRC) and (reg:MODE DEST)
7989 occupy the same number of hard registers. */
7991 static bool
7992 inherit_piecemeal_p (int dest ATTRIBUTE_UNUSED,
7993 int src ATTRIBUTE_UNUSED,
7994 enum machine_mode mode ATTRIBUTE_UNUSED)
7996 #ifdef CANNOT_CHANGE_MODE_CLASS
7997 return (!REG_CANNOT_CHANGE_MODE_P (dest, mode, reg_raw_mode[dest])
7998 && !REG_CANNOT_CHANGE_MODE_P (src, mode, reg_raw_mode[src]));
7999 #else
8000 return true;
8001 #endif
8004 /* Output insns to reload values in and out of the chosen reload regs. */
8006 static void
8007 emit_reload_insns (struct insn_chain *chain)
8009 rtx insn = chain->insn;
8011 int j;
8013 CLEAR_HARD_REG_SET (reg_reloaded_died);
8015 for (j = 0; j < reload_n_operands; j++)
8016 input_reload_insns[j] = input_address_reload_insns[j]
8017 = inpaddr_address_reload_insns[j]
8018 = output_reload_insns[j] = output_address_reload_insns[j]
8019 = outaddr_address_reload_insns[j]
8020 = other_output_reload_insns[j] = 0;
8021 other_input_address_reload_insns = 0;
8022 other_input_reload_insns = 0;
8023 operand_reload_insns = 0;
8024 other_operand_reload_insns = 0;
8026 /* Dump reloads into the dump file. */
8027 if (dump_file)
8029 fprintf (dump_file, "\nReloads for insn # %d\n", INSN_UID (insn));
8030 debug_reload_to_stream (dump_file);
8033 for (j = 0; j < n_reloads; j++)
8034 if (rld[j].reg_rtx && HARD_REGISTER_P (rld[j].reg_rtx))
8036 unsigned int i;
8038 for (i = REGNO (rld[j].reg_rtx); i < END_REGNO (rld[j].reg_rtx); i++)
8039 new_spill_reg_store[i] = 0;
8042 /* Now output the instructions to copy the data into and out of the
8043 reload registers. Do these in the order that the reloads were reported,
8044 since reloads of base and index registers precede reloads of operands
8045 and the operands may need the base and index registers reloaded. */
8047 for (j = 0; j < n_reloads; j++)
8049 do_input_reload (chain, rld + j, j);
8050 do_output_reload (chain, rld + j, j);
8053 /* Now write all the insns we made for reloads in the order expected by
8054 the allocation functions. Prior to the insn being reloaded, we write
8055 the following reloads:
8057 RELOAD_FOR_OTHER_ADDRESS reloads for input addresses.
8059 RELOAD_OTHER reloads.
8061 For each operand, any RELOAD_FOR_INPADDR_ADDRESS reloads followed
8062 by any RELOAD_FOR_INPUT_ADDRESS reloads followed by the
8063 RELOAD_FOR_INPUT reload for the operand.
8065 RELOAD_FOR_OPADDR_ADDRS reloads.
8067 RELOAD_FOR_OPERAND_ADDRESS reloads.
8069 After the insn being reloaded, we write the following:
8071 For each operand, any RELOAD_FOR_OUTADDR_ADDRESS reloads followed
8072 by any RELOAD_FOR_OUTPUT_ADDRESS reload followed by the
8073 RELOAD_FOR_OUTPUT reload, followed by any RELOAD_OTHER output
8074 reloads for the operand. The RELOAD_OTHER output reloads are
8075 output in descending order by reload number. */
8077 emit_insn_before (other_input_address_reload_insns, insn);
8078 emit_insn_before (other_input_reload_insns, insn);
8080 for (j = 0; j < reload_n_operands; j++)
8082 emit_insn_before (inpaddr_address_reload_insns[j], insn);
8083 emit_insn_before (input_address_reload_insns[j], insn);
8084 emit_insn_before (input_reload_insns[j], insn);
8087 emit_insn_before (other_operand_reload_insns, insn);
8088 emit_insn_before (operand_reload_insns, insn);
8090 for (j = 0; j < reload_n_operands; j++)
8092 rtx x = emit_insn_after (outaddr_address_reload_insns[j], insn);
8093 x = emit_insn_after (output_address_reload_insns[j], x);
8094 x = emit_insn_after (output_reload_insns[j], x);
8095 emit_insn_after (other_output_reload_insns[j], x);
8098 /* For all the spill regs newly reloaded in this instruction,
8099 record what they were reloaded from, so subsequent instructions
8100 can inherit the reloads.
8102 Update spill_reg_store for the reloads of this insn.
8103 Copy the elements that were updated in the loop above. */
8105 for (j = 0; j < n_reloads; j++)
8107 int r = reload_order[j];
8108 int i = reload_spill_index[r];
8110 /* If this is a non-inherited input reload from a pseudo, we must
8111 clear any memory of a previous store to the same pseudo. Only do
8112 something if there will not be an output reload for the pseudo
8113 being reloaded. */
8114 if (rld[r].in_reg != 0
8115 && ! (reload_inherited[r] || reload_override_in[r]))
8117 rtx reg = rld[r].in_reg;
8119 if (GET_CODE (reg) == SUBREG)
8120 reg = SUBREG_REG (reg);
8122 if (REG_P (reg)
8123 && REGNO (reg) >= FIRST_PSEUDO_REGISTER
8124 && !REGNO_REG_SET_P (&reg_has_output_reload, REGNO (reg)))
8126 int nregno = REGNO (reg);
8128 if (reg_last_reload_reg[nregno])
8130 int last_regno = REGNO (reg_last_reload_reg[nregno]);
8132 if (reg_reloaded_contents[last_regno] == nregno)
8133 spill_reg_store[last_regno] = 0;
8138 /* I is nonneg if this reload used a register.
8139 If rld[r].reg_rtx is 0, this is an optional reload
8140 that we opted to ignore. */
8142 if (i >= 0 && rld[r].reg_rtx != 0)
8144 int nr = hard_regno_nregs[i][GET_MODE (rld[r].reg_rtx)];
8145 int k;
8147 /* For a multi register reload, we need to check if all or part
8148 of the value lives to the end. */
8149 for (k = 0; k < nr; k++)
8150 if (reload_reg_reaches_end_p (i + k, r))
8151 CLEAR_HARD_REG_BIT (reg_reloaded_valid, i + k);
8153 /* Maybe the spill reg contains a copy of reload_out. */
8154 if (rld[r].out != 0
8155 && (REG_P (rld[r].out)
8156 || (rld[r].out_reg
8157 ? REG_P (rld[r].out_reg)
8158 /* The reload value is an auto-modification of
8159 some kind. For PRE_INC, POST_INC, PRE_DEC
8160 and POST_DEC, we record an equivalence
8161 between the reload register and the operand
8162 on the optimistic assumption that we can make
8163 the equivalence hold. reload_as_needed must
8164 then either make it hold or invalidate the
8165 equivalence.
8167 PRE_MODIFY and POST_MODIFY addresses are reloaded
8168 somewhat differently, and allowing them here leads
8169 to problems. */
8170 : (GET_CODE (rld[r].out) != POST_MODIFY
8171 && GET_CODE (rld[r].out) != PRE_MODIFY))))
8173 rtx reg;
8175 reg = reload_reg_rtx_for_output[r];
8176 if (reload_reg_rtx_reaches_end_p (reg, r))
8178 enum machine_mode mode = GET_MODE (reg);
8179 int regno = REGNO (reg);
8180 int nregs = hard_regno_nregs[regno][mode];
8181 rtx out = (REG_P (rld[r].out)
8182 ? rld[r].out
8183 : rld[r].out_reg
8184 ? rld[r].out_reg
8185 /* AUTO_INC */ : XEXP (rld[r].in_reg, 0));
8186 int out_regno = REGNO (out);
8187 int out_nregs = (!HARD_REGISTER_NUM_P (out_regno) ? 1
8188 : hard_regno_nregs[out_regno][mode]);
8189 bool piecemeal;
8191 spill_reg_store[regno] = new_spill_reg_store[regno];
8192 spill_reg_stored_to[regno] = out;
8193 reg_last_reload_reg[out_regno] = reg;
8195 piecemeal = (HARD_REGISTER_NUM_P (out_regno)
8196 && nregs == out_nregs
8197 && inherit_piecemeal_p (out_regno, regno, mode));
8199 /* If OUT_REGNO is a hard register, it may occupy more than
8200 one register. If it does, say what is in the
8201 rest of the registers assuming that both registers
8202 agree on how many words the object takes. If not,
8203 invalidate the subsequent registers. */
8205 if (HARD_REGISTER_NUM_P (out_regno))
8206 for (k = 1; k < out_nregs; k++)
8207 reg_last_reload_reg[out_regno + k]
8208 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
8210 /* Now do the inverse operation. */
8211 for (k = 0; k < nregs; k++)
8213 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
8214 reg_reloaded_contents[regno + k]
8215 = (!HARD_REGISTER_NUM_P (out_regno) || !piecemeal
8216 ? out_regno
8217 : out_regno + k);
8218 reg_reloaded_insn[regno + k] = insn;
8219 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
8220 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
8221 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8222 regno + k);
8223 else
8224 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8225 regno + k);
8229 /* Maybe the spill reg contains a copy of reload_in. Only do
8230 something if there will not be an output reload for
8231 the register being reloaded. */
8232 else if (rld[r].out_reg == 0
8233 && rld[r].in != 0
8234 && ((REG_P (rld[r].in)
8235 && !HARD_REGISTER_P (rld[r].in)
8236 && !REGNO_REG_SET_P (&reg_has_output_reload,
8237 REGNO (rld[r].in)))
8238 || (REG_P (rld[r].in_reg)
8239 && !REGNO_REG_SET_P (&reg_has_output_reload,
8240 REGNO (rld[r].in_reg))))
8241 && !reg_set_p (reload_reg_rtx_for_input[r], PATTERN (insn)))
8243 rtx reg;
8245 reg = reload_reg_rtx_for_input[r];
8246 if (reload_reg_rtx_reaches_end_p (reg, r))
8248 enum machine_mode mode;
8249 int regno;
8250 int nregs;
8251 int in_regno;
8252 int in_nregs;
8253 rtx in;
8254 bool piecemeal;
8256 mode = GET_MODE (reg);
8257 regno = REGNO (reg);
8258 nregs = hard_regno_nregs[regno][mode];
8259 if (REG_P (rld[r].in)
8260 && REGNO (rld[r].in) >= FIRST_PSEUDO_REGISTER)
8261 in = rld[r].in;
8262 else if (REG_P (rld[r].in_reg))
8263 in = rld[r].in_reg;
8264 else
8265 in = XEXP (rld[r].in_reg, 0);
8266 in_regno = REGNO (in);
8268 in_nregs = (!HARD_REGISTER_NUM_P (in_regno) ? 1
8269 : hard_regno_nregs[in_regno][mode]);
8271 reg_last_reload_reg[in_regno] = reg;
8273 piecemeal = (HARD_REGISTER_NUM_P (in_regno)
8274 && nregs == in_nregs
8275 && inherit_piecemeal_p (regno, in_regno, mode));
8277 if (HARD_REGISTER_NUM_P (in_regno))
8278 for (k = 1; k < in_nregs; k++)
8279 reg_last_reload_reg[in_regno + k]
8280 = (piecemeal ? regno_reg_rtx[regno + k] : 0);
8282 /* Unless we inherited this reload, show we haven't
8283 recently done a store.
8284 Previous stores of inherited auto_inc expressions
8285 also have to be discarded. */
8286 if (! reload_inherited[r]
8287 || (rld[r].out && ! rld[r].out_reg))
8288 spill_reg_store[regno] = 0;
8290 for (k = 0; k < nregs; k++)
8292 CLEAR_HARD_REG_BIT (reg_reloaded_dead, regno + k);
8293 reg_reloaded_contents[regno + k]
8294 = (!HARD_REGISTER_NUM_P (in_regno) || !piecemeal
8295 ? in_regno
8296 : in_regno + k);
8297 reg_reloaded_insn[regno + k] = insn;
8298 SET_HARD_REG_BIT (reg_reloaded_valid, regno + k);
8299 if (HARD_REGNO_CALL_PART_CLOBBERED (regno + k, mode))
8300 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8301 regno + k);
8302 else
8303 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8304 regno + k);
8310 /* The following if-statement was #if 0'd in 1.34 (or before...).
8311 It's reenabled in 1.35 because supposedly nothing else
8312 deals with this problem. */
8314 /* If a register gets output-reloaded from a non-spill register,
8315 that invalidates any previous reloaded copy of it.
8316 But forget_old_reloads_1 won't get to see it, because
8317 it thinks only about the original insn. So invalidate it here.
8318 Also do the same thing for RELOAD_OTHER constraints where the
8319 output is discarded. */
8320 if (i < 0
8321 && ((rld[r].out != 0
8322 && (REG_P (rld[r].out)
8323 || (MEM_P (rld[r].out)
8324 && REG_P (rld[r].out_reg))))
8325 || (rld[r].out == 0 && rld[r].out_reg
8326 && REG_P (rld[r].out_reg))))
8328 rtx out = ((rld[r].out && REG_P (rld[r].out))
8329 ? rld[r].out : rld[r].out_reg);
8330 int out_regno = REGNO (out);
8331 enum machine_mode mode = GET_MODE (out);
8333 /* REG_RTX is now set or clobbered by the main instruction.
8334 As the comment above explains, forget_old_reloads_1 only
8335 sees the original instruction, and there is no guarantee
8336 that the original instruction also clobbered REG_RTX.
8337 For example, if find_reloads sees that the input side of
8338 a matched operand pair dies in this instruction, it may
8339 use the input register as the reload register.
8341 Calling forget_old_reloads_1 is a waste of effort if
8342 REG_RTX is also the output register.
8344 If we know that REG_RTX holds the value of a pseudo
8345 register, the code after the call will record that fact. */
8346 if (rld[r].reg_rtx && rld[r].reg_rtx != out)
8347 forget_old_reloads_1 (rld[r].reg_rtx, NULL_RTX, NULL);
8349 if (!HARD_REGISTER_NUM_P (out_regno))
8351 rtx src_reg, store_insn = NULL_RTX;
8353 reg_last_reload_reg[out_regno] = 0;
8355 /* If we can find a hard register that is stored, record
8356 the storing insn so that we may delete this insn with
8357 delete_output_reload. */
8358 src_reg = reload_reg_rtx_for_output[r];
8360 if (src_reg)
8362 if (reload_reg_rtx_reaches_end_p (src_reg, r))
8363 store_insn = new_spill_reg_store[REGNO (src_reg)];
8364 else
8365 src_reg = NULL_RTX;
8367 else
8369 /* If this is an optional reload, try to find the
8370 source reg from an input reload. */
8371 rtx set = single_set (insn);
8372 if (set && SET_DEST (set) == rld[r].out)
8374 int k;
8376 src_reg = SET_SRC (set);
8377 store_insn = insn;
8378 for (k = 0; k < n_reloads; k++)
8380 if (rld[k].in == src_reg)
8382 src_reg = reload_reg_rtx_for_input[k];
8383 break;
8388 if (src_reg && REG_P (src_reg)
8389 && REGNO (src_reg) < FIRST_PSEUDO_REGISTER)
8391 int src_regno, src_nregs, k;
8392 rtx note;
8394 gcc_assert (GET_MODE (src_reg) == mode);
8395 src_regno = REGNO (src_reg);
8396 src_nregs = hard_regno_nregs[src_regno][mode];
8397 /* The place where to find a death note varies with
8398 PRESERVE_DEATH_INFO_REGNO_P . The condition is not
8399 necessarily checked exactly in the code that moves
8400 notes, so just check both locations. */
8401 note = find_regno_note (insn, REG_DEAD, src_regno);
8402 if (! note && store_insn)
8403 note = find_regno_note (store_insn, REG_DEAD, src_regno);
8404 for (k = 0; k < src_nregs; k++)
8406 spill_reg_store[src_regno + k] = store_insn;
8407 spill_reg_stored_to[src_regno + k] = out;
8408 reg_reloaded_contents[src_regno + k] = out_regno;
8409 reg_reloaded_insn[src_regno + k] = store_insn;
8410 CLEAR_HARD_REG_BIT (reg_reloaded_dead, src_regno + k);
8411 SET_HARD_REG_BIT (reg_reloaded_valid, src_regno + k);
8412 if (HARD_REGNO_CALL_PART_CLOBBERED (src_regno + k,
8413 mode))
8414 SET_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8415 src_regno + k);
8416 else
8417 CLEAR_HARD_REG_BIT (reg_reloaded_call_part_clobbered,
8418 src_regno + k);
8419 SET_HARD_REG_BIT (reg_is_output_reload, src_regno + k);
8420 if (note)
8421 SET_HARD_REG_BIT (reg_reloaded_died, src_regno);
8422 else
8423 CLEAR_HARD_REG_BIT (reg_reloaded_died, src_regno);
8425 reg_last_reload_reg[out_regno] = src_reg;
8426 /* We have to set reg_has_output_reload here, or else
8427 forget_old_reloads_1 will clear reg_last_reload_reg
8428 right away. */
8429 SET_REGNO_REG_SET (&reg_has_output_reload,
8430 out_regno);
8433 else
8435 int k, out_nregs = hard_regno_nregs[out_regno][mode];
8437 for (k = 0; k < out_nregs; k++)
8438 reg_last_reload_reg[out_regno + k] = 0;
8442 IOR_HARD_REG_SET (reg_reloaded_dead, reg_reloaded_died);
8445 /* Go through the motions to emit INSN and test if it is strictly valid.
8446 Return the emitted insn if valid, else return NULL. */
8448 static rtx
8449 emit_insn_if_valid_for_reload (rtx insn)
8451 rtx last = get_last_insn ();
8452 int code;
8454 insn = emit_insn (insn);
8455 code = recog_memoized (insn);
8457 if (code >= 0)
8459 extract_insn (insn);
8460 /* We want constrain operands to treat this insn strictly in its
8461 validity determination, i.e., the way it would after reload has
8462 completed. */
8463 if (constrain_operands (1))
8464 return insn;
8467 delete_insns_since (last);
8468 return NULL;
8471 /* Emit code to perform a reload from IN (which may be a reload register) to
8472 OUT (which may also be a reload register). IN or OUT is from operand
8473 OPNUM with reload type TYPE.
8475 Returns first insn emitted. */
8477 static rtx
8478 gen_reload (rtx out, rtx in, int opnum, enum reload_type type)
8480 rtx last = get_last_insn ();
8481 rtx tem;
8483 /* If IN is a paradoxical SUBREG, remove it and try to put the
8484 opposite SUBREG on OUT. Likewise for a paradoxical SUBREG on OUT. */
8485 if (!strip_paradoxical_subreg (&in, &out))
8486 strip_paradoxical_subreg (&out, &in);
8488 /* How to do this reload can get quite tricky. Normally, we are being
8489 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
8490 register that didn't get a hard register. In that case we can just
8491 call emit_move_insn.
8493 We can also be asked to reload a PLUS that adds a register or a MEM to
8494 another register, constant or MEM. This can occur during frame pointer
8495 elimination and while reloading addresses. This case is handled by
8496 trying to emit a single insn to perform the add. If it is not valid,
8497 we use a two insn sequence.
8499 Or we can be asked to reload an unary operand that was a fragment of
8500 an addressing mode, into a register. If it isn't recognized as-is,
8501 we try making the unop operand and the reload-register the same:
8502 (set reg:X (unop:X expr:Y))
8503 -> (set reg:Y expr:Y) (set reg:X (unop:X reg:Y)).
8505 Finally, we could be called to handle an 'o' constraint by putting
8506 an address into a register. In that case, we first try to do this
8507 with a named pattern of "reload_load_address". If no such pattern
8508 exists, we just emit a SET insn and hope for the best (it will normally
8509 be valid on machines that use 'o').
8511 This entire process is made complex because reload will never
8512 process the insns we generate here and so we must ensure that
8513 they will fit their constraints and also by the fact that parts of
8514 IN might be being reloaded separately and replaced with spill registers.
8515 Because of this, we are, in some sense, just guessing the right approach
8516 here. The one listed above seems to work.
8518 ??? At some point, this whole thing needs to be rethought. */
8520 if (GET_CODE (in) == PLUS
8521 && (REG_P (XEXP (in, 0))
8522 || GET_CODE (XEXP (in, 0)) == SUBREG
8523 || MEM_P (XEXP (in, 0)))
8524 && (REG_P (XEXP (in, 1))
8525 || GET_CODE (XEXP (in, 1)) == SUBREG
8526 || CONSTANT_P (XEXP (in, 1))
8527 || MEM_P (XEXP (in, 1))))
8529 /* We need to compute the sum of a register or a MEM and another
8530 register, constant, or MEM, and put it into the reload
8531 register. The best possible way of doing this is if the machine
8532 has a three-operand ADD insn that accepts the required operands.
8534 The simplest approach is to try to generate such an insn and see if it
8535 is recognized and matches its constraints. If so, it can be used.
8537 It might be better not to actually emit the insn unless it is valid,
8538 but we need to pass the insn as an operand to `recog' and
8539 `extract_insn' and it is simpler to emit and then delete the insn if
8540 not valid than to dummy things up. */
8542 rtx op0, op1, tem, insn;
8543 enum insn_code code;
8545 op0 = find_replacement (&XEXP (in, 0));
8546 op1 = find_replacement (&XEXP (in, 1));
8548 /* Since constraint checking is strict, commutativity won't be
8549 checked, so we need to do that here to avoid spurious failure
8550 if the add instruction is two-address and the second operand
8551 of the add is the same as the reload reg, which is frequently
8552 the case. If the insn would be A = B + A, rearrange it so
8553 it will be A = A + B as constrain_operands expects. */
8555 if (REG_P (XEXP (in, 1))
8556 && REGNO (out) == REGNO (XEXP (in, 1)))
8557 tem = op0, op0 = op1, op1 = tem;
8559 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
8560 in = gen_rtx_PLUS (GET_MODE (in), op0, op1);
8562 insn = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8563 if (insn)
8564 return insn;
8566 /* If that failed, we must use a conservative two-insn sequence.
8568 Use a move to copy one operand into the reload register. Prefer
8569 to reload a constant, MEM or pseudo since the move patterns can
8570 handle an arbitrary operand. If OP1 is not a constant, MEM or
8571 pseudo and OP1 is not a valid operand for an add instruction, then
8572 reload OP1.
8574 After reloading one of the operands into the reload register, add
8575 the reload register to the output register.
8577 If there is another way to do this for a specific machine, a
8578 DEFINE_PEEPHOLE should be specified that recognizes the sequence
8579 we emit below. */
8581 code = optab_handler (add_optab, GET_MODE (out));
8583 if (CONSTANT_P (op1) || MEM_P (op1) || GET_CODE (op1) == SUBREG
8584 || (REG_P (op1)
8585 && REGNO (op1) >= FIRST_PSEUDO_REGISTER)
8586 || (code != CODE_FOR_nothing
8587 && !insn_operand_matches (code, 2, op1)))
8588 tem = op0, op0 = op1, op1 = tem;
8590 gen_reload (out, op0, opnum, type);
8592 /* If OP0 and OP1 are the same, we can use OUT for OP1.
8593 This fixes a problem on the 32K where the stack pointer cannot
8594 be used as an operand of an add insn. */
8596 if (rtx_equal_p (op0, op1))
8597 op1 = out;
8599 insn = emit_insn_if_valid_for_reload (gen_add2_insn (out, op1));
8600 if (insn)
8602 /* Add a REG_EQUIV note so that find_equiv_reg can find it. */
8603 set_dst_reg_note (insn, REG_EQUIV, in, out);
8604 return insn;
8607 /* If that failed, copy the address register to the reload register.
8608 Then add the constant to the reload register. */
8610 gcc_assert (!reg_overlap_mentioned_p (out, op0));
8611 gen_reload (out, op1, opnum, type);
8612 insn = emit_insn (gen_add2_insn (out, op0));
8613 set_dst_reg_note (insn, REG_EQUIV, in, out);
8616 #ifdef SECONDARY_MEMORY_NEEDED
8617 /* If we need a memory location to do the move, do it that way. */
8618 else if ((REG_P (in)
8619 || (GET_CODE (in) == SUBREG && REG_P (SUBREG_REG (in))))
8620 && reg_or_subregno (in) < FIRST_PSEUDO_REGISTER
8621 && (REG_P (out)
8622 || (GET_CODE (out) == SUBREG && REG_P (SUBREG_REG (out))))
8623 && reg_or_subregno (out) < FIRST_PSEUDO_REGISTER
8624 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (reg_or_subregno (in)),
8625 REGNO_REG_CLASS (reg_or_subregno (out)),
8626 GET_MODE (out)))
8628 /* Get the memory to use and rewrite both registers to its mode. */
8629 rtx loc = get_secondary_mem (in, GET_MODE (out), opnum, type);
8631 if (GET_MODE (loc) != GET_MODE (out))
8632 out = gen_rtx_REG (GET_MODE (loc), reg_or_subregno (out));
8634 if (GET_MODE (loc) != GET_MODE (in))
8635 in = gen_rtx_REG (GET_MODE (loc), reg_or_subregno (in));
8637 gen_reload (loc, in, opnum, type);
8638 gen_reload (out, loc, opnum, type);
8640 #endif
8641 else if (REG_P (out) && UNARY_P (in))
8643 rtx insn;
8644 rtx op1;
8645 rtx out_moded;
8646 rtx set;
8648 op1 = find_replacement (&XEXP (in, 0));
8649 if (op1 != XEXP (in, 0))
8650 in = gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in), op1);
8652 /* First, try a plain SET. */
8653 set = emit_insn_if_valid_for_reload (gen_rtx_SET (VOIDmode, out, in));
8654 if (set)
8655 return set;
8657 /* If that failed, move the inner operand to the reload
8658 register, and try the same unop with the inner expression
8659 replaced with the reload register. */
8661 if (GET_MODE (op1) != GET_MODE (out))
8662 out_moded = gen_rtx_REG (GET_MODE (op1), REGNO (out));
8663 else
8664 out_moded = out;
8666 gen_reload (out_moded, op1, opnum, type);
8668 insn
8669 = gen_rtx_SET (VOIDmode, out,
8670 gen_rtx_fmt_e (GET_CODE (in), GET_MODE (in),
8671 out_moded));
8672 insn = emit_insn_if_valid_for_reload (insn);
8673 if (insn)
8675 set_unique_reg_note (insn, REG_EQUIV, in);
8676 return insn;
8679 fatal_insn ("failure trying to reload:", set);
8681 /* If IN is a simple operand, use gen_move_insn. */
8682 else if (OBJECT_P (in) || GET_CODE (in) == SUBREG)
8684 tem = emit_insn (gen_move_insn (out, in));
8685 /* IN may contain a LABEL_REF, if so add a REG_LABEL_OPERAND note. */
8686 mark_jump_label (in, tem, 0);
8689 #ifdef HAVE_reload_load_address
8690 else if (HAVE_reload_load_address)
8691 emit_insn (gen_reload_load_address (out, in));
8692 #endif
8694 /* Otherwise, just write (set OUT IN) and hope for the best. */
8695 else
8696 emit_insn (gen_rtx_SET (VOIDmode, out, in));
8698 /* Return the first insn emitted.
8699 We can not just return get_last_insn, because there may have
8700 been multiple instructions emitted. Also note that gen_move_insn may
8701 emit more than one insn itself, so we can not assume that there is one
8702 insn emitted per emit_insn_before call. */
8704 return last ? NEXT_INSN (last) : get_insns ();
8707 /* Delete a previously made output-reload whose result we now believe
8708 is not needed. First we double-check.
8710 INSN is the insn now being processed.
8711 LAST_RELOAD_REG is the hard register number for which we want to delete
8712 the last output reload.
8713 J is the reload-number that originally used REG. The caller has made
8714 certain that reload J doesn't use REG any longer for input.
8715 NEW_RELOAD_REG is reload register that reload J is using for REG. */
8717 static void
8718 delete_output_reload (rtx insn, int j, int last_reload_reg, rtx new_reload_reg)
8720 rtx output_reload_insn = spill_reg_store[last_reload_reg];
8721 rtx reg = spill_reg_stored_to[last_reload_reg];
8722 int k;
8723 int n_occurrences;
8724 int n_inherited = 0;
8725 rtx i1;
8726 rtx substed;
8727 unsigned regno;
8728 int nregs;
8730 /* It is possible that this reload has been only used to set another reload
8731 we eliminated earlier and thus deleted this instruction too. */
8732 if (INSN_DELETED_P (output_reload_insn))
8733 return;
8735 /* Get the raw pseudo-register referred to. */
8737 while (GET_CODE (reg) == SUBREG)
8738 reg = SUBREG_REG (reg);
8739 substed = reg_equiv_memory_loc (REGNO (reg));
8741 /* This is unsafe if the operand occurs more often in the current
8742 insn than it is inherited. */
8743 for (k = n_reloads - 1; k >= 0; k--)
8745 rtx reg2 = rld[k].in;
8746 if (! reg2)
8747 continue;
8748 if (MEM_P (reg2) || reload_override_in[k])
8749 reg2 = rld[k].in_reg;
8750 #ifdef AUTO_INC_DEC
8751 if (rld[k].out && ! rld[k].out_reg)
8752 reg2 = XEXP (rld[k].in_reg, 0);
8753 #endif
8754 while (GET_CODE (reg2) == SUBREG)
8755 reg2 = SUBREG_REG (reg2);
8756 if (rtx_equal_p (reg2, reg))
8758 if (reload_inherited[k] || reload_override_in[k] || k == j)
8759 n_inherited++;
8760 else
8761 return;
8764 n_occurrences = count_occurrences (PATTERN (insn), reg, 0);
8765 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
8766 n_occurrences += count_occurrences (CALL_INSN_FUNCTION_USAGE (insn),
8767 reg, 0);
8768 if (substed)
8769 n_occurrences += count_occurrences (PATTERN (insn),
8770 eliminate_regs (substed, VOIDmode,
8771 NULL_RTX), 0);
8772 for (i1 = reg_equiv_alt_mem_list (REGNO (reg)); i1; i1 = XEXP (i1, 1))
8774 gcc_assert (!rtx_equal_p (XEXP (i1, 0), substed));
8775 n_occurrences += count_occurrences (PATTERN (insn), XEXP (i1, 0), 0);
8777 if (n_occurrences > n_inherited)
8778 return;
8780 regno = REGNO (reg);
8781 if (regno >= FIRST_PSEUDO_REGISTER)
8782 nregs = 1;
8783 else
8784 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
8786 /* If the pseudo-reg we are reloading is no longer referenced
8787 anywhere between the store into it and here,
8788 and we're within the same basic block, then the value can only
8789 pass through the reload reg and end up here.
8790 Otherwise, give up--return. */
8791 for (i1 = NEXT_INSN (output_reload_insn);
8792 i1 != insn; i1 = NEXT_INSN (i1))
8794 if (NOTE_INSN_BASIC_BLOCK_P (i1))
8795 return;
8796 if ((NONJUMP_INSN_P (i1) || CALL_P (i1))
8797 && refers_to_regno_p (regno, regno + nregs, PATTERN (i1), NULL))
8799 /* If this is USE in front of INSN, we only have to check that
8800 there are no more references than accounted for by inheritance. */
8801 while (NONJUMP_INSN_P (i1) && GET_CODE (PATTERN (i1)) == USE)
8803 n_occurrences += rtx_equal_p (reg, XEXP (PATTERN (i1), 0)) != 0;
8804 i1 = NEXT_INSN (i1);
8806 if (n_occurrences <= n_inherited && i1 == insn)
8807 break;
8808 return;
8812 /* We will be deleting the insn. Remove the spill reg information. */
8813 for (k = hard_regno_nregs[last_reload_reg][GET_MODE (reg)]; k-- > 0; )
8815 spill_reg_store[last_reload_reg + k] = 0;
8816 spill_reg_stored_to[last_reload_reg + k] = 0;
8819 /* The caller has already checked that REG dies or is set in INSN.
8820 It has also checked that we are optimizing, and thus some
8821 inaccuracies in the debugging information are acceptable.
8822 So we could just delete output_reload_insn. But in some cases
8823 we can improve the debugging information without sacrificing
8824 optimization - maybe even improving the code: See if the pseudo
8825 reg has been completely replaced with reload regs. If so, delete
8826 the store insn and forget we had a stack slot for the pseudo. */
8827 if (rld[j].out != rld[j].in
8828 && REG_N_DEATHS (REGNO (reg)) == 1
8829 && REG_N_SETS (REGNO (reg)) == 1
8830 && REG_BASIC_BLOCK (REGNO (reg)) >= NUM_FIXED_BLOCKS
8831 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
8833 rtx i2;
8835 /* We know that it was used only between here and the beginning of
8836 the current basic block. (We also know that the last use before
8837 INSN was the output reload we are thinking of deleting, but never
8838 mind that.) Search that range; see if any ref remains. */
8839 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8841 rtx set = single_set (i2);
8843 /* Uses which just store in the pseudo don't count,
8844 since if they are the only uses, they are dead. */
8845 if (set != 0 && SET_DEST (set) == reg)
8846 continue;
8847 if (LABEL_P (i2)
8848 || JUMP_P (i2))
8849 break;
8850 if ((NONJUMP_INSN_P (i2) || CALL_P (i2))
8851 && reg_mentioned_p (reg, PATTERN (i2)))
8853 /* Some other ref remains; just delete the output reload we
8854 know to be dead. */
8855 delete_address_reloads (output_reload_insn, insn);
8856 delete_insn (output_reload_insn);
8857 return;
8861 /* Delete the now-dead stores into this pseudo. Note that this
8862 loop also takes care of deleting output_reload_insn. */
8863 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
8865 rtx set = single_set (i2);
8867 if (set != 0 && SET_DEST (set) == reg)
8869 delete_address_reloads (i2, insn);
8870 delete_insn (i2);
8872 if (LABEL_P (i2)
8873 || JUMP_P (i2))
8874 break;
8877 /* For the debugging info, say the pseudo lives in this reload reg. */
8878 reg_renumber[REGNO (reg)] = REGNO (new_reload_reg);
8879 if (ira_conflicts_p)
8880 /* Inform IRA about the change. */
8881 ira_mark_allocation_change (REGNO (reg));
8882 alter_reg (REGNO (reg), -1, false);
8884 else
8886 delete_address_reloads (output_reload_insn, insn);
8887 delete_insn (output_reload_insn);
8891 /* We are going to delete DEAD_INSN. Recursively delete loads of
8892 reload registers used in DEAD_INSN that are not used till CURRENT_INSN.
8893 CURRENT_INSN is being reloaded, so we have to check its reloads too. */
8894 static void
8895 delete_address_reloads (rtx dead_insn, rtx current_insn)
8897 rtx set = single_set (dead_insn);
8898 rtx set2, dst, prev, next;
8899 if (set)
8901 rtx dst = SET_DEST (set);
8902 if (MEM_P (dst))
8903 delete_address_reloads_1 (dead_insn, XEXP (dst, 0), current_insn);
8905 /* If we deleted the store from a reloaded post_{in,de}c expression,
8906 we can delete the matching adds. */
8907 prev = PREV_INSN (dead_insn);
8908 next = NEXT_INSN (dead_insn);
8909 if (! prev || ! next)
8910 return;
8911 set = single_set (next);
8912 set2 = single_set (prev);
8913 if (! set || ! set2
8914 || GET_CODE (SET_SRC (set)) != PLUS || GET_CODE (SET_SRC (set2)) != PLUS
8915 || !CONST_INT_P (XEXP (SET_SRC (set), 1))
8916 || !CONST_INT_P (XEXP (SET_SRC (set2), 1)))
8917 return;
8918 dst = SET_DEST (set);
8919 if (! rtx_equal_p (dst, SET_DEST (set2))
8920 || ! rtx_equal_p (dst, XEXP (SET_SRC (set), 0))
8921 || ! rtx_equal_p (dst, XEXP (SET_SRC (set2), 0))
8922 || (INTVAL (XEXP (SET_SRC (set), 1))
8923 != -INTVAL (XEXP (SET_SRC (set2), 1))))
8924 return;
8925 delete_related_insns (prev);
8926 delete_related_insns (next);
8929 /* Subfunction of delete_address_reloads: process registers found in X. */
8930 static void
8931 delete_address_reloads_1 (rtx dead_insn, rtx x, rtx current_insn)
8933 rtx prev, set, dst, i2;
8934 int i, j;
8935 enum rtx_code code = GET_CODE (x);
8937 if (code != REG)
8939 const char *fmt = GET_RTX_FORMAT (code);
8940 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8942 if (fmt[i] == 'e')
8943 delete_address_reloads_1 (dead_insn, XEXP (x, i), current_insn);
8944 else if (fmt[i] == 'E')
8946 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8947 delete_address_reloads_1 (dead_insn, XVECEXP (x, i, j),
8948 current_insn);
8951 return;
8954 if (spill_reg_order[REGNO (x)] < 0)
8955 return;
8957 /* Scan backwards for the insn that sets x. This might be a way back due
8958 to inheritance. */
8959 for (prev = PREV_INSN (dead_insn); prev; prev = PREV_INSN (prev))
8961 code = GET_CODE (prev);
8962 if (code == CODE_LABEL || code == JUMP_INSN)
8963 return;
8964 if (!INSN_P (prev))
8965 continue;
8966 if (reg_set_p (x, PATTERN (prev)))
8967 break;
8968 if (reg_referenced_p (x, PATTERN (prev)))
8969 return;
8971 if (! prev || INSN_UID (prev) < reload_first_uid)
8972 return;
8973 /* Check that PREV only sets the reload register. */
8974 set = single_set (prev);
8975 if (! set)
8976 return;
8977 dst = SET_DEST (set);
8978 if (!REG_P (dst)
8979 || ! rtx_equal_p (dst, x))
8980 return;
8981 if (! reg_set_p (dst, PATTERN (dead_insn)))
8983 /* Check if DST was used in a later insn -
8984 it might have been inherited. */
8985 for (i2 = NEXT_INSN (dead_insn); i2; i2 = NEXT_INSN (i2))
8987 if (LABEL_P (i2))
8988 break;
8989 if (! INSN_P (i2))
8990 continue;
8991 if (reg_referenced_p (dst, PATTERN (i2)))
8993 /* If there is a reference to the register in the current insn,
8994 it might be loaded in a non-inherited reload. If no other
8995 reload uses it, that means the register is set before
8996 referenced. */
8997 if (i2 == current_insn)
8999 for (j = n_reloads - 1; j >= 0; j--)
9000 if ((rld[j].reg_rtx == dst && reload_inherited[j])
9001 || reload_override_in[j] == dst)
9002 return;
9003 for (j = n_reloads - 1; j >= 0; j--)
9004 if (rld[j].in && rld[j].reg_rtx == dst)
9005 break;
9006 if (j >= 0)
9007 break;
9009 return;
9011 if (JUMP_P (i2))
9012 break;
9013 /* If DST is still live at CURRENT_INSN, check if it is used for
9014 any reload. Note that even if CURRENT_INSN sets DST, we still
9015 have to check the reloads. */
9016 if (i2 == current_insn)
9018 for (j = n_reloads - 1; j >= 0; j--)
9019 if ((rld[j].reg_rtx == dst && reload_inherited[j])
9020 || reload_override_in[j] == dst)
9021 return;
9022 /* ??? We can't finish the loop here, because dst might be
9023 allocated to a pseudo in this block if no reload in this
9024 block needs any of the classes containing DST - see
9025 spill_hard_reg. There is no easy way to tell this, so we
9026 have to scan till the end of the basic block. */
9028 if (reg_set_p (dst, PATTERN (i2)))
9029 break;
9032 delete_address_reloads_1 (prev, SET_SRC (set), current_insn);
9033 reg_reloaded_contents[REGNO (dst)] = -1;
9034 delete_insn (prev);
9037 /* Output reload-insns to reload VALUE into RELOADREG.
9038 VALUE is an autoincrement or autodecrement RTX whose operand
9039 is a register or memory location;
9040 so reloading involves incrementing that location.
9041 IN is either identical to VALUE, or some cheaper place to reload from.
9043 INC_AMOUNT is the number to increment or decrement by (always positive).
9044 This cannot be deduced from VALUE. */
9046 static void
9047 inc_for_reload (rtx reloadreg, rtx in, rtx value, int inc_amount)
9049 /* REG or MEM to be copied and incremented. */
9050 rtx incloc = find_replacement (&XEXP (value, 0));
9051 /* Nonzero if increment after copying. */
9052 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC
9053 || GET_CODE (value) == POST_MODIFY);
9054 rtx last;
9055 rtx inc;
9056 rtx add_insn;
9057 int code;
9058 rtx real_in = in == value ? incloc : in;
9060 /* No hard register is equivalent to this register after
9061 inc/dec operation. If REG_LAST_RELOAD_REG were nonzero,
9062 we could inc/dec that register as well (maybe even using it for
9063 the source), but I'm not sure it's worth worrying about. */
9064 if (REG_P (incloc))
9065 reg_last_reload_reg[REGNO (incloc)] = 0;
9067 if (GET_CODE (value) == PRE_MODIFY || GET_CODE (value) == POST_MODIFY)
9069 gcc_assert (GET_CODE (XEXP (value, 1)) == PLUS);
9070 inc = find_replacement (&XEXP (XEXP (value, 1), 1));
9072 else
9074 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
9075 inc_amount = -inc_amount;
9077 inc = GEN_INT (inc_amount);
9080 /* If this is post-increment, first copy the location to the reload reg. */
9081 if (post && real_in != reloadreg)
9082 emit_insn (gen_move_insn (reloadreg, real_in));
9084 if (in == value)
9086 /* See if we can directly increment INCLOC. Use a method similar to
9087 that in gen_reload. */
9089 last = get_last_insn ();
9090 add_insn = emit_insn (gen_rtx_SET (VOIDmode, incloc,
9091 gen_rtx_PLUS (GET_MODE (incloc),
9092 incloc, inc)));
9094 code = recog_memoized (add_insn);
9095 if (code >= 0)
9097 extract_insn (add_insn);
9098 if (constrain_operands (1))
9100 /* If this is a pre-increment and we have incremented the value
9101 where it lives, copy the incremented value to RELOADREG to
9102 be used as an address. */
9104 if (! post)
9105 emit_insn (gen_move_insn (reloadreg, incloc));
9106 return;
9109 delete_insns_since (last);
9112 /* If couldn't do the increment directly, must increment in RELOADREG.
9113 The way we do this depends on whether this is pre- or post-increment.
9114 For pre-increment, copy INCLOC to the reload register, increment it
9115 there, then save back. */
9117 if (! post)
9119 if (in != reloadreg)
9120 emit_insn (gen_move_insn (reloadreg, real_in));
9121 emit_insn (gen_add2_insn (reloadreg, inc));
9122 emit_insn (gen_move_insn (incloc, reloadreg));
9124 else
9126 /* Postincrement.
9127 Because this might be a jump insn or a compare, and because RELOADREG
9128 may not be available after the insn in an input reload, we must do
9129 the incrementation before the insn being reloaded for.
9131 We have already copied IN to RELOADREG. Increment the copy in
9132 RELOADREG, save that back, then decrement RELOADREG so it has
9133 the original value. */
9135 emit_insn (gen_add2_insn (reloadreg, inc));
9136 emit_insn (gen_move_insn (incloc, reloadreg));
9137 if (CONST_INT_P (inc))
9138 emit_insn (gen_add2_insn (reloadreg, GEN_INT (-INTVAL (inc))));
9139 else
9140 emit_insn (gen_sub2_insn (reloadreg, inc));
9144 #ifdef AUTO_INC_DEC
9145 static void
9146 add_auto_inc_notes (rtx insn, rtx x)
9148 enum rtx_code code = GET_CODE (x);
9149 const char *fmt;
9150 int i, j;
9152 if (code == MEM && auto_inc_p (XEXP (x, 0)))
9154 add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
9155 return;
9158 /* Scan all the operand sub-expressions. */
9159 fmt = GET_RTX_FORMAT (code);
9160 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9162 if (fmt[i] == 'e')
9163 add_auto_inc_notes (insn, XEXP (x, i));
9164 else if (fmt[i] == 'E')
9165 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9166 add_auto_inc_notes (insn, XVECEXP (x, i, j));
9169 #endif