Add expand_complex_abs decl.
[official-gcc.git] / gcc / reload1.c
blob4374d1f633840dbb4b8ca92576391d9ff108de3d
1 /* Reload pseudo regs into hard regs for insns that require hard regs.
2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <stdio.h>
22 #include "config.h"
23 #include "rtl.h"
24 #include "obstack.h"
25 #include "insn-config.h"
26 #include "insn-flags.h"
27 #include "insn-codes.h"
28 #include "flags.h"
29 #include "expr.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "reload.h"
33 #include "recog.h"
34 #include "basic-block.h"
35 #include "output.h"
37 /* This file contains the reload pass of the compiler, which is
38 run after register allocation has been done. It checks that
39 each insn is valid (operands required to be in registers really
40 are in registers of the proper class) and fixes up invalid ones
41 by copying values temporarily into registers for the insns
42 that need them.
44 The results of register allocation are described by the vector
45 reg_renumber; the insns still contain pseudo regs, but reg_renumber
46 can be used to find which hard reg, if any, a pseudo reg is in.
48 The technique we always use is to free up a few hard regs that are
49 called ``reload regs'', and for each place where a pseudo reg
50 must be in a hard reg, copy it temporarily into one of the reload regs.
52 All the pseudos that were formerly allocated to the hard regs that
53 are now in use as reload regs must be ``spilled''. This means
54 that they go to other hard regs, or to stack slots if no other
55 available hard regs can be found. Spilling can invalidate more
56 insns, requiring additional need for reloads, so we must keep checking
57 until the process stabilizes.
59 For machines with different classes of registers, we must keep track
60 of the register class needed for each reload, and make sure that
61 we allocate enough reload registers of each class.
63 The file reload.c contains the code that checks one insn for
64 validity and reports the reloads that it needs. This file
65 is in charge of scanning the entire rtl code, accumulating the
66 reload needs, spilling, assigning reload registers to use for
67 fixing up each insn, and generating the new insns to copy values
68 into the reload registers. */
70 /* During reload_as_needed, element N contains a REG rtx for the hard reg
71 into which pseudo reg N has been reloaded (perhaps for a previous insn). */
72 static rtx *reg_last_reload_reg;
74 /* Elt N nonzero if reg_last_reload_reg[N] has been set in this insn
75 for an output reload that stores into reg N. */
76 static char *reg_has_output_reload;
78 /* Indicates which hard regs are reload-registers for an output reload
79 in the current insn. */
80 static HARD_REG_SET reg_is_output_reload;
82 /* Element N is the constant value to which pseudo reg N is equivalent,
83 or zero if pseudo reg N is not equivalent to a constant.
84 find_reloads looks at this in order to replace pseudo reg N
85 with the constant it stands for. */
86 rtx *reg_equiv_constant;
88 /* Element N is a memory location to which pseudo reg N is equivalent,
89 prior to any register elimination (such as frame pointer to stack
90 pointer). Depending on whether or not it is a valid address, this value
91 is transferred to either reg_equiv_address or reg_equiv_mem. */
92 rtx *reg_equiv_memory_loc;
94 /* Element N is the address of stack slot to which pseudo reg N is equivalent.
95 This is used when the address is not valid as a memory address
96 (because its displacement is too big for the machine.) */
97 rtx *reg_equiv_address;
99 /* Element N is the memory slot to which pseudo reg N is equivalent,
100 or zero if pseudo reg N is not equivalent to a memory slot. */
101 rtx *reg_equiv_mem;
103 /* Widest width in which each pseudo reg is referred to (via subreg). */
104 static int *reg_max_ref_width;
106 /* Element N is the insn that initialized reg N from its equivalent
107 constant or memory slot. */
108 static rtx *reg_equiv_init;
110 /* During reload_as_needed, element N contains the last pseudo regno
111 reloaded into the Nth reload register. This vector is in parallel
112 with spill_regs. If that pseudo reg occupied more than one register,
113 reg_reloaded_contents points to that pseudo for each spill register in
114 use; all of these must remain set for an inheritance to occur. */
115 static int reg_reloaded_contents[FIRST_PSEUDO_REGISTER];
117 /* During reload_as_needed, element N contains the insn for which
118 the Nth reload register was last used. This vector is in parallel
119 with spill_regs, and its contents are significant only when
120 reg_reloaded_contents is significant. */
121 static rtx reg_reloaded_insn[FIRST_PSEUDO_REGISTER];
123 /* Number of spill-regs so far; number of valid elements of spill_regs. */
124 static int n_spills;
126 /* In parallel with spill_regs, contains REG rtx's for those regs.
127 Holds the last rtx used for any given reg, or 0 if it has never
128 been used for spilling yet. This rtx is reused, provided it has
129 the proper mode. */
130 static rtx spill_reg_rtx[FIRST_PSEUDO_REGISTER];
132 /* In parallel with spill_regs, contains nonzero for a spill reg
133 that was stored after the last time it was used.
134 The precise value is the insn generated to do the store. */
135 static rtx spill_reg_store[FIRST_PSEUDO_REGISTER];
137 /* This table is the inverse mapping of spill_regs:
138 indexed by hard reg number,
139 it contains the position of that reg in spill_regs,
140 or -1 for something that is not in spill_regs. */
141 static short spill_reg_order[FIRST_PSEUDO_REGISTER];
143 /* This reg set indicates registers that may not be used for retrying global
144 allocation. The registers that may not be used include all spill registers
145 and the frame pointer (if we are using one). */
146 HARD_REG_SET forbidden_regs;
148 /* This reg set indicates registers that are not good for spill registers.
149 They will not be used to complete groups of spill registers. This includes
150 all fixed registers, registers that may be eliminated, and registers
151 explicitly used in the rtl.
153 (spill_reg_order prevents these registers from being used to start a
154 group.) */
155 static HARD_REG_SET bad_spill_regs;
157 /* Describes order of use of registers for reloading
158 of spilled pseudo-registers. `spills' is the number of
159 elements that are actually valid; new ones are added at the end. */
160 static short spill_regs[FIRST_PSEUDO_REGISTER];
162 /* Describes order of preference for putting regs into spill_regs.
163 Contains the numbers of all the hard regs, in order most preferred first.
164 This order is different for each function.
165 It is set up by order_regs_for_reload.
166 Empty elements at the end contain -1. */
167 static short potential_reload_regs[FIRST_PSEUDO_REGISTER];
169 /* 1 for a hard register that appears explicitly in the rtl
170 (for example, function value registers, special registers
171 used by insns, structure value pointer registers). */
172 static char regs_explicitly_used[FIRST_PSEUDO_REGISTER];
174 /* Indicates if a register was counted against the need for
175 groups. 0 means it can count against max_nongroup instead. */
176 static HARD_REG_SET counted_for_groups;
178 /* Indicates if a register was counted against the need for
179 non-groups. 0 means it can become part of a new group.
180 During choose_reload_regs, 1 here means don't use this reg
181 as part of a group, even if it seems to be otherwise ok. */
182 static HARD_REG_SET counted_for_nongroups;
184 /* Nonzero if indirect addressing is supported on the machine; this means
185 that spilling (REG n) does not require reloading it into a register in
186 order to do (MEM (REG n)) or (MEM (PLUS (REG n) (CONST_INT c))). The
187 value indicates the level of indirect addressing supported, e.g., two
188 means that (MEM (MEM (REG n))) is also valid if (REG n) does not get
189 a hard register. */
191 static char spill_indirect_levels;
193 /* Nonzero if indirect addressing is supported when the innermost MEM is
194 of the form (MEM (SYMBOL_REF sym)). It is assumed that the level to
195 which these are valid is the same as spill_indirect_levels, above. */
197 char indirect_symref_ok;
199 /* Nonzero if an address (plus (reg frame_pointer) (reg ...)) is valid. */
201 char double_reg_address_ok;
203 /* Record the stack slot for each spilled hard register. */
205 static rtx spill_stack_slot[FIRST_PSEUDO_REGISTER];
207 /* Width allocated so far for that stack slot. */
209 static int spill_stack_slot_width[FIRST_PSEUDO_REGISTER];
211 /* Indexed by register class and basic block number, nonzero if there is
212 any need for a spill register of that class in that basic block.
213 The pointer is 0 if we did stupid allocation and don't know
214 the structure of basic blocks. */
216 char *basic_block_needs[N_REG_CLASSES];
218 /* First uid used by insns created by reload in this function.
219 Used in find_equiv_reg. */
220 int reload_first_uid;
222 /* Flag set by local-alloc or global-alloc if anything is live in
223 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. */
230 int reload_in_progress = 0;
232 /* These arrays record the insn_code of insns that may be needed to
233 perform input and output reloads of special objects. They provide a
234 place to pass a scratch register. */
236 enum insn_code reload_in_optab[NUM_MACHINE_MODES];
237 enum insn_code reload_out_optab[NUM_MACHINE_MODES];
239 /* This obstack is used for allocation of rtl during register elimination.
240 The allocated storage can be freed once find_reloads has processed the
241 insn. */
243 struct obstack reload_obstack;
244 char *reload_firstobj;
246 #define obstack_chunk_alloc xmalloc
247 #define obstack_chunk_free free
249 /* List of labels that must never be deleted. */
250 extern rtx forced_labels;
252 /* This structure is used to record information about register eliminations.
253 Each array entry describes one possible way of eliminating a register
254 in favor of another. If there is more than one way of eliminating a
255 particular register, the most preferred should be specified first. */
257 static struct elim_table
259 int from; /* Register number to be eliminated. */
260 int to; /* Register number used as replacement. */
261 int initial_offset; /* Initial difference between values. */
262 int can_eliminate; /* Non-zero if this elimination can be done. */
263 int can_eliminate_previous; /* Value of CAN_ELIMINATE in previous scan over
264 insns made by reload. */
265 int offset; /* Current offset between the two regs. */
266 int max_offset; /* Maximum offset between the two regs. */
267 int previous_offset; /* Offset at end of previous insn. */
268 int ref_outside_mem; /* "to" has been referenced outside a MEM. */
269 rtx from_rtx; /* REG rtx for the register to be eliminated.
270 We cannot simply compare the number since
271 we might then spuriously replace a hard
272 register corresponding to a pseudo
273 assigned to the reg to be eliminated. */
274 rtx to_rtx; /* REG rtx for the replacement. */
275 } reg_eliminate[] =
277 /* If a set of eliminable registers was specified, define the table from it.
278 Otherwise, default to the normal case of the frame pointer being
279 replaced by the stack pointer. */
281 #ifdef ELIMINABLE_REGS
282 ELIMINABLE_REGS;
283 #else
284 {{ FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM}};
285 #endif
287 #define NUM_ELIMINABLE_REGS (sizeof reg_eliminate / sizeof reg_eliminate[0])
289 /* Record the number of pending eliminations that have an offset not equal
290 to their initial offset. If non-zero, we use a new copy of each
291 replacement result in any insns encountered. */
292 static int num_not_at_initial_offset;
294 /* Count the number of registers that we may be able to eliminate. */
295 static int num_eliminable;
297 /* For each label, we record the offset of each elimination. If we reach
298 a label by more than one path and an offset differs, we cannot do the
299 elimination. This information is indexed by the number of the label.
300 The first table is an array of flags that records whether we have yet
301 encountered a label and the second table is an array of arrays, one
302 entry in the latter array for each elimination. */
304 static char *offsets_known_at;
305 static int (*offsets_at)[NUM_ELIMINABLE_REGS];
307 /* Number of labels in the current function. */
309 static int num_labels;
311 void mark_home_live ();
312 static void count_possible_groups ();
313 static int possible_group_p ();
314 static void scan_paradoxical_subregs ();
315 static void reload_as_needed ();
316 static int modes_equiv_for_class_p ();
317 static void alter_reg ();
318 static void delete_dead_insn ();
319 static void spill_failure ();
320 static int new_spill_reg();
321 static void set_label_offsets ();
322 static int eliminate_regs_in_insn ();
323 static void mark_not_eliminable ();
324 static int spill_hard_reg ();
325 static void choose_reload_regs ();
326 static void emit_reload_insns ();
327 static void delete_output_reload ();
328 static void forget_old_reloads_1 ();
329 static void order_regs_for_reload ();
330 static rtx inc_for_reload ();
331 static int constraint_accepts_reg_p ();
332 static int count_occurrences ();
334 extern void remove_death ();
335 extern rtx adj_offsettable_operand ();
336 extern rtx form_sum ();
338 void
339 init_reload ()
341 register int i;
343 /* Often (MEM (REG n)) is still valid even if (REG n) is put on the stack.
344 Set spill_indirect_levels to the number of levels such addressing is
345 permitted, zero if it is not permitted at all. */
347 register rtx tem
348 = gen_rtx (MEM, Pmode,
349 gen_rtx (PLUS, Pmode,
350 gen_rtx (REG, Pmode, LAST_VIRTUAL_REGISTER + 1),
351 GEN_INT (4)));
352 spill_indirect_levels = 0;
354 while (memory_address_p (QImode, tem))
356 spill_indirect_levels++;
357 tem = gen_rtx (MEM, Pmode, tem);
360 /* See if indirect addressing is valid for (MEM (SYMBOL_REF ...)). */
362 tem = gen_rtx (MEM, Pmode, gen_rtx (SYMBOL_REF, Pmode, "foo"));
363 indirect_symref_ok = memory_address_p (QImode, tem);
365 /* See if reg+reg is a valid (and offsettable) address. */
367 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
369 tem = gen_rtx (PLUS, Pmode,
370 gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM),
371 gen_rtx (REG, Pmode, i));
372 /* This way, we make sure that reg+reg is an offsettable address. */
373 tem = plus_constant (tem, 4);
375 if (memory_address_p (QImode, tem))
377 double_reg_address_ok = 1;
378 break;
382 /* Initialize obstack for our rtl allocation. */
383 gcc_obstack_init (&reload_obstack);
384 reload_firstobj = (char *) obstack_alloc (&reload_obstack, 0);
386 #ifdef HAVE_SECONDARY_RELOADS
388 /* Initialize the optabs for doing special input and output reloads. */
390 for (i = 0; i < NUM_MACHINE_MODES; i++)
391 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
393 #ifdef HAVE_reload_inqi
394 if (HAVE_reload_inqi)
395 reload_in_optab[(int) QImode] = CODE_FOR_reload_inqi;
396 #endif
397 #ifdef HAVE_reload_inhi
398 if (HAVE_reload_inhi)
399 reload_in_optab[(int) HImode] = CODE_FOR_reload_inhi;
400 #endif
401 #ifdef HAVE_reload_insi
402 if (HAVE_reload_insi)
403 reload_in_optab[(int) SImode] = CODE_FOR_reload_insi;
404 #endif
405 #ifdef HAVE_reload_indi
406 if (HAVE_reload_indi)
407 reload_in_optab[(int) DImode] = CODE_FOR_reload_indi;
408 #endif
409 #ifdef HAVE_reload_inti
410 if (HAVE_reload_inti)
411 reload_in_optab[(int) TImode] = CODE_FOR_reload_inti;
412 #endif
413 #ifdef HAVE_reload_insf
414 if (HAVE_reload_insf)
415 reload_in_optab[(int) SFmode] = CODE_FOR_reload_insf;
416 #endif
417 #ifdef HAVE_reload_indf
418 if (HAVE_reload_indf)
419 reload_in_optab[(int) DFmode] = CODE_FOR_reload_indf;
420 #endif
421 #ifdef HAVE_reload_inxf
422 if (HAVE_reload_inxf)
423 reload_in_optab[(int) XFmode] = CODE_FOR_reload_inxf;
424 #endif
425 #ifdef HAVE_reload_intf
426 if (HAVE_reload_intf)
427 reload_in_optab[(int) TFmode] = CODE_FOR_reload_intf;
428 #endif
430 #ifdef HAVE_reload_outqi
431 if (HAVE_reload_outqi)
432 reload_out_optab[(int) QImode] = CODE_FOR_reload_outqi;
433 #endif
434 #ifdef HAVE_reload_outhi
435 if (HAVE_reload_outhi)
436 reload_out_optab[(int) HImode] = CODE_FOR_reload_outhi;
437 #endif
438 #ifdef HAVE_reload_outsi
439 if (HAVE_reload_outsi)
440 reload_out_optab[(int) SImode] = CODE_FOR_reload_outsi;
441 #endif
442 #ifdef HAVE_reload_outdi
443 if (HAVE_reload_outdi)
444 reload_out_optab[(int) DImode] = CODE_FOR_reload_outdi;
445 #endif
446 #ifdef HAVE_reload_outti
447 if (HAVE_reload_outti)
448 reload_out_optab[(int) TImode] = CODE_FOR_reload_outti;
449 #endif
450 #ifdef HAVE_reload_outsf
451 if (HAVE_reload_outsf)
452 reload_out_optab[(int) SFmode] = CODE_FOR_reload_outsf;
453 #endif
454 #ifdef HAVE_reload_outdf
455 if (HAVE_reload_outdf)
456 reload_out_optab[(int) DFmode] = CODE_FOR_reload_outdf;
457 #endif
458 #ifdef HAVE_reload_outxf
459 if (HAVE_reload_outxf)
460 reload_out_optab[(int) XFmode] = CODE_FOR_reload_outxf;
461 #endif
462 #ifdef HAVE_reload_outtf
463 if (HAVE_reload_outtf)
464 reload_out_optab[(int) TFmode] = CODE_FOR_reload_outtf;
465 #endif
467 #endif /* HAVE_SECONDARY_RELOADS */
471 /* Main entry point for the reload pass, and only entry point
472 in this file.
474 FIRST is the first insn of the function being compiled.
476 GLOBAL nonzero means we were called from global_alloc
477 and should attempt to reallocate any pseudoregs that we
478 displace from hard regs we will use for reloads.
479 If GLOBAL is zero, we do not have enough information to do that,
480 so any pseudo reg that is spilled must go to the stack.
482 DUMPFILE is the global-reg debugging dump file stream, or 0.
483 If it is nonzero, messages are written to it to describe
484 which registers are seized as reload regs, which pseudo regs
485 are spilled from them, and where the pseudo regs are reallocated to.
487 Return value is nonzero if reload failed
488 and we must not do any more for this function. */
491 reload (first, global, dumpfile)
492 rtx first;
493 int global;
494 FILE *dumpfile;
496 register int class;
497 register int i;
498 register rtx insn;
499 register struct elim_table *ep;
501 int something_changed;
502 int something_needs_reloads;
503 int something_needs_elimination;
504 int new_basic_block_needs;
505 enum reg_class caller_save_spill_class = NO_REGS;
506 int caller_save_group_size = 1;
508 /* Nonzero means we couldn't get enough spill regs. */
509 int failure = 0;
511 /* The basic block number currently being processed for INSN. */
512 int this_block;
514 /* Make sure even insns with volatile mem refs are recognizable. */
515 init_recog ();
517 /* Enable find_equiv_reg to distinguish insns made by reload. */
518 reload_first_uid = get_max_uid ();
520 for (i = 0; i < N_REG_CLASSES; i++)
521 basic_block_needs[i] = 0;
523 #ifdef SECONDARY_MEMORY_NEEDED
524 /* Initialize the secondary memory table. */
525 clear_secondary_mem ();
526 #endif
528 /* Remember which hard regs appear explicitly
529 before we merge into `regs_ever_live' the ones in which
530 pseudo regs have been allocated. */
531 bcopy (regs_ever_live, regs_explicitly_used, sizeof regs_ever_live);
533 /* We don't have a stack slot for any spill reg yet. */
534 bzero (spill_stack_slot, sizeof spill_stack_slot);
535 bzero (spill_stack_slot_width, sizeof spill_stack_slot_width);
537 /* Initialize the save area information for caller-save, in case some
538 are needed. */
539 init_save_areas ();
541 /* Compute which hard registers are now in use
542 as homes for pseudo registers.
543 This is done here rather than (eg) in global_alloc
544 because this point is reached even if not optimizing. */
546 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
547 mark_home_live (i);
549 /* Make sure that the last insn in the chain
550 is not something that needs reloading. */
551 emit_note (NULL_PTR, NOTE_INSN_DELETED);
553 /* Find all the pseudo registers that didn't get hard regs
554 but do have known equivalent constants or memory slots.
555 These include parameters (known equivalent to parameter slots)
556 and cse'd or loop-moved constant memory addresses.
558 Record constant equivalents in reg_equiv_constant
559 so they will be substituted by find_reloads.
560 Record memory equivalents in reg_mem_equiv so they can
561 be substituted eventually by altering the REG-rtx's. */
563 reg_equiv_constant = (rtx *) alloca (max_regno * sizeof (rtx));
564 bzero (reg_equiv_constant, max_regno * sizeof (rtx));
565 reg_equiv_memory_loc = (rtx *) alloca (max_regno * sizeof (rtx));
566 bzero (reg_equiv_memory_loc, max_regno * sizeof (rtx));
567 reg_equiv_mem = (rtx *) alloca (max_regno * sizeof (rtx));
568 bzero (reg_equiv_mem, max_regno * sizeof (rtx));
569 reg_equiv_init = (rtx *) alloca (max_regno * sizeof (rtx));
570 bzero (reg_equiv_init, max_regno * sizeof (rtx));
571 reg_equiv_address = (rtx *) alloca (max_regno * sizeof (rtx));
572 bzero (reg_equiv_address, max_regno * sizeof (rtx));
573 reg_max_ref_width = (int *) alloca (max_regno * sizeof (int));
574 bzero (reg_max_ref_width, max_regno * sizeof (int));
576 /* Look for REG_EQUIV notes; record what each pseudo is equivalent to.
577 Also find all paradoxical subregs
578 and find largest such for each pseudo. */
580 for (insn = first; insn; insn = NEXT_INSN (insn))
582 rtx set = single_set (insn);
584 if (set != 0 && GET_CODE (SET_DEST (set)) == REG)
586 rtx note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
587 if (note
588 #ifdef LEGITIMATE_PIC_OPERAND_P
589 && (! CONSTANT_P (XEXP (note, 0)) || ! flag_pic
590 || LEGITIMATE_PIC_OPERAND_P (XEXP (note, 0)))
591 #endif
594 rtx x = XEXP (note, 0);
595 i = REGNO (SET_DEST (set));
596 if (i > LAST_VIRTUAL_REGISTER)
598 if (GET_CODE (x) == MEM)
599 reg_equiv_memory_loc[i] = x;
600 else if (CONSTANT_P (x))
602 if (LEGITIMATE_CONSTANT_P (x))
603 reg_equiv_constant[i] = x;
604 else
605 reg_equiv_memory_loc[i]
606 = force_const_mem (GET_MODE (SET_DEST (set)), x);
608 else
609 continue;
611 /* If this register is being made equivalent to a MEM
612 and the MEM is not SET_SRC, the equivalencing insn
613 is one with the MEM as a SET_DEST and it occurs later.
614 So don't mark this insn now. */
615 if (GET_CODE (x) != MEM
616 || rtx_equal_p (SET_SRC (set), x))
617 reg_equiv_init[i] = insn;
622 /* If this insn is setting a MEM from a register equivalent to it,
623 this is the equivalencing insn. */
624 else if (set && GET_CODE (SET_DEST (set)) == MEM
625 && GET_CODE (SET_SRC (set)) == REG
626 && reg_equiv_memory_loc[REGNO (SET_SRC (set))]
627 && rtx_equal_p (SET_DEST (set),
628 reg_equiv_memory_loc[REGNO (SET_SRC (set))]))
629 reg_equiv_init[REGNO (SET_SRC (set))] = insn;
631 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
632 scan_paradoxical_subregs (PATTERN (insn));
635 /* Does this function require a frame pointer? */
637 frame_pointer_needed = (! flag_omit_frame_pointer
638 #ifdef EXIT_IGNORE_STACK
639 /* ?? If EXIT_IGNORE_STACK is set, we will not save
640 and restore sp for alloca. So we can't eliminate
641 the frame pointer in that case. At some point,
642 we should improve this by emitting the
643 sp-adjusting insns for this case. */
644 || (current_function_calls_alloca
645 && EXIT_IGNORE_STACK)
646 #endif
647 || FRAME_POINTER_REQUIRED);
649 num_eliminable = 0;
651 /* Initialize the table of registers to eliminate. The way we do this
652 depends on how the eliminable registers were defined. */
653 #ifdef ELIMINABLE_REGS
654 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
656 ep->can_eliminate = ep->can_eliminate_previous
657 = (CAN_ELIMINATE (ep->from, ep->to)
658 && (ep->from != FRAME_POINTER_REGNUM || ! frame_pointer_needed));
660 #else
661 reg_eliminate[0].can_eliminate = reg_eliminate[0].can_eliminate_previous
662 = ! frame_pointer_needed;
663 #endif
665 /* Count the number of eliminable registers and build the FROM and TO
666 REG rtx's. Note that code in gen_rtx will cause, e.g.,
667 gen_rtx (REG, Pmode, STACK_POINTER_REGNUM) to equal stack_pointer_rtx.
668 We depend on this. */
669 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
671 num_eliminable += ep->can_eliminate;
672 ep->from_rtx = gen_rtx (REG, Pmode, ep->from);
673 ep->to_rtx = gen_rtx (REG, Pmode, ep->to);
676 num_labels = max_label_num () - get_first_label_num ();
678 /* Allocate the tables used to store offset information at labels. */
679 offsets_known_at = (char *) alloca (num_labels);
680 offsets_at
681 = (int (*)[NUM_ELIMINABLE_REGS])
682 alloca (num_labels * NUM_ELIMINABLE_REGS * sizeof (int));
684 offsets_known_at -= get_first_label_num ();
685 offsets_at -= get_first_label_num ();
687 /* Alter each pseudo-reg rtx to contain its hard reg number.
688 Assign stack slots to the pseudos that lack hard regs or equivalents.
689 Do not touch virtual registers. */
691 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_regno; i++)
692 alter_reg (i, -1);
694 /* Round size of stack frame to BIGGEST_ALIGNMENT. This must be done here
695 because the stack size may be a part of the offset computation for
696 register elimination. */
697 assign_stack_local (BLKmode, 0, 0);
699 /* If we have some registers we think can be eliminated, scan all insns to
700 see if there is an insn that sets one of these registers to something
701 other than itself plus a constant. If so, the register cannot be
702 eliminated. Doing this scan here eliminates an extra pass through the
703 main reload loop in the most common case where register elimination
704 cannot be done. */
705 for (insn = first; insn && num_eliminable; insn = NEXT_INSN (insn))
706 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
707 || GET_CODE (insn) == CALL_INSN)
708 note_stores (PATTERN (insn), mark_not_eliminable);
710 #ifndef REGISTER_CONSTRAINTS
711 /* If all the pseudo regs have hard regs,
712 except for those that are never referenced,
713 we know that no reloads are needed. */
714 /* But that is not true if there are register constraints, since
715 in that case some pseudos might be in the wrong kind of hard reg. */
717 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
718 if (reg_renumber[i] == -1 && reg_n_refs[i] != 0)
719 break;
721 if (i == max_regno && num_eliminable == 0 && ! caller_save_needed)
722 return;
723 #endif
725 /* Compute the order of preference for hard registers to spill.
726 Store them by decreasing preference in potential_reload_regs. */
728 order_regs_for_reload ();
730 /* So far, no hard regs have been spilled. */
731 n_spills = 0;
732 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
733 spill_reg_order[i] = -1;
735 /* On most machines, we can't use any register explicitly used in the
736 rtl as a spill register. But on some, we have to. Those will have
737 taken care to keep the life of hard regs as short as possible. */
739 #ifdef SMALL_REGISTER_CLASSES
740 CLEAR_HARD_REG_SET (forbidden_regs);
741 #else
742 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
743 #endif
745 /* Spill any hard regs that we know we can't eliminate. */
746 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
747 if (! ep->can_eliminate)
749 spill_hard_reg (ep->from, global, dumpfile, 1);
750 regs_ever_live[ep->from] = 1;
753 if (global)
754 for (i = 0; i < N_REG_CLASSES; i++)
756 basic_block_needs[i] = (char *)alloca (n_basic_blocks);
757 bzero (basic_block_needs[i], n_basic_blocks);
760 /* From now on, we need to emit any moves without making new pseudos. */
761 reload_in_progress = 1;
763 /* This loop scans the entire function each go-round
764 and repeats until one repetition spills no additional hard regs. */
766 /* This flag is set when a pseudo reg is spilled,
767 to require another pass. Note that getting an additional reload
768 reg does not necessarily imply any pseudo reg was spilled;
769 sometimes we find a reload reg that no pseudo reg was allocated in. */
770 something_changed = 1;
771 /* This flag is set if there are any insns that require reloading. */
772 something_needs_reloads = 0;
773 /* This flag is set if there are any insns that require register
774 eliminations. */
775 something_needs_elimination = 0;
776 while (something_changed)
778 rtx after_call = 0;
780 /* For each class, number of reload regs needed in that class.
781 This is the maximum over all insns of the needs in that class
782 of the individual insn. */
783 int max_needs[N_REG_CLASSES];
784 /* For each class, size of group of consecutive regs
785 that is needed for the reloads of this class. */
786 int group_size[N_REG_CLASSES];
787 /* For each class, max number of consecutive groups needed.
788 (Each group contains group_size[CLASS] consecutive registers.) */
789 int max_groups[N_REG_CLASSES];
790 /* For each class, max number needed of regs that don't belong
791 to any of the groups. */
792 int max_nongroups[N_REG_CLASSES];
793 /* For each class, the machine mode which requires consecutive
794 groups of regs of that class.
795 If two different modes ever require groups of one class,
796 they must be the same size and equally restrictive for that class,
797 otherwise we can't handle the complexity. */
798 enum machine_mode group_mode[N_REG_CLASSES];
799 /* Record the insn where each maximum need is first found. */
800 rtx max_needs_insn[N_REG_CLASSES];
801 rtx max_groups_insn[N_REG_CLASSES];
802 rtx max_nongroups_insn[N_REG_CLASSES];
803 rtx x;
804 int starting_frame_size = get_frame_size ();
805 static char *reg_class_names[] = REG_CLASS_NAMES;
807 something_changed = 0;
808 bzero (max_needs, sizeof max_needs);
809 bzero (max_groups, sizeof max_groups);
810 bzero (max_nongroups, sizeof max_nongroups);
811 bzero (max_needs_insn, sizeof max_needs_insn);
812 bzero (max_groups_insn, sizeof max_groups_insn);
813 bzero (max_nongroups_insn, sizeof max_nongroups_insn);
814 bzero (group_size, sizeof group_size);
815 for (i = 0; i < N_REG_CLASSES; i++)
816 group_mode[i] = VOIDmode;
818 /* Keep track of which basic blocks are needing the reloads. */
819 this_block = 0;
821 /* Remember whether any element of basic_block_needs
822 changes from 0 to 1 in this pass. */
823 new_basic_block_needs = 0;
825 /* Reset all offsets on eliminable registers to their initial values. */
826 #ifdef ELIMINABLE_REGS
827 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
829 INITIAL_ELIMINATION_OFFSET (ep->from, ep->to, ep->initial_offset);
830 ep->previous_offset = ep->offset
831 = ep->max_offset = ep->initial_offset;
833 #else
834 #ifdef INITIAL_FRAME_POINTER_OFFSET
835 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
836 #else
837 if (!FRAME_POINTER_REQUIRED)
838 abort ();
839 reg_eliminate[0].initial_offset = 0;
840 #endif
841 reg_eliminate[0].previous_offset = reg_eliminate[0].max_offset
842 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
843 #endif
845 num_not_at_initial_offset = 0;
847 bzero (&offsets_known_at[get_first_label_num ()], num_labels);
849 /* Set a known offset for each forced label to be at the initial offset
850 of each elimination. We do this because we assume that all
851 computed jumps occur from a location where each elimination is
852 at its initial offset. */
854 for (x = forced_labels; x; x = XEXP (x, 1))
855 if (XEXP (x, 0))
856 set_label_offsets (XEXP (x, 0), NULL_RTX, 1);
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.
872 If the location is not addressable, but does not have one of the
873 above forms, assign a stack slot. We have to do this to avoid the
874 potential of producing lots of reloads if, e.g., a location involves
875 a pseudo that didn't get a hard register and has an equivalent memory
876 location that also involves a pseudo that didn't get a hard register.
878 Perhaps at some point we will improve reload_when_needed handling
879 so this problem goes away. But that's very hairy. */
881 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
882 if (reg_renumber[i] < 0 && reg_equiv_memory_loc[i])
884 rtx x = eliminate_regs (reg_equiv_memory_loc[i], 0, NULL_RTX);
886 if (strict_memory_address_p (GET_MODE (regno_reg_rtx[i]),
887 XEXP (x, 0)))
888 reg_equiv_mem[i] = x, reg_equiv_address[i] = 0;
889 else if (CONSTANT_P (XEXP (x, 0))
890 || (GET_CODE (XEXP (x, 0)) == PLUS
891 && GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
892 && (REGNO (XEXP (XEXP (x, 0), 0))
893 < FIRST_PSEUDO_REGISTER)
894 && CONSTANT_P (XEXP (XEXP (x, 0), 1))))
895 reg_equiv_address[i] = XEXP (x, 0), reg_equiv_mem[i] = 0;
896 else
898 /* Make a new stack slot. Then indicate that something
899 changed so we go back and recompute offsets for
900 eliminable registers because the allocation of memory
901 below might change some offset. reg_equiv_{mem,address}
902 will be set up for this pseudo on the next pass around
903 the loop. */
904 reg_equiv_memory_loc[i] = 0;
905 reg_equiv_init[i] = 0;
906 alter_reg (i, -1);
907 something_changed = 1;
911 /* If we allocated another pseudo to the stack, redo elimination
912 bookkeeping. */
913 if (something_changed)
914 continue;
916 /* If caller-saves needs a group, initialize the group to include
917 the size and mode required for caller-saves. */
919 if (caller_save_group_size > 1)
921 group_mode[(int) caller_save_spill_class] = Pmode;
922 group_size[(int) caller_save_spill_class] = caller_save_group_size;
925 /* Compute the most additional registers needed by any instruction.
926 Collect information separately for each class of regs. */
928 for (insn = first; insn; insn = NEXT_INSN (insn))
930 if (global && this_block + 1 < n_basic_blocks
931 && insn == basic_block_head[this_block+1])
932 ++this_block;
934 /* If this is a label, a JUMP_INSN, or has REG_NOTES (which
935 might include REG_LABEL), we need to see what effects this
936 has on the known offsets at labels. */
938 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == JUMP_INSN
939 || (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
940 && REG_NOTES (insn) != 0))
941 set_label_offsets (insn, insn, 0);
943 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
945 /* Nonzero means don't use a reload reg that overlaps
946 the place where a function value can be returned. */
947 rtx avoid_return_reg = 0;
949 rtx old_body = PATTERN (insn);
950 int old_code = INSN_CODE (insn);
951 rtx old_notes = REG_NOTES (insn);
952 int did_elimination = 0;
954 /* Initially, count RELOAD_OTHER reloads.
955 Later, merge in the other kinds. */
956 int insn_needs[N_REG_CLASSES];
957 int insn_groups[N_REG_CLASSES];
958 int insn_total_groups = 0;
960 /* Count RELOAD_FOR_INPUT_RELOAD_ADDRESS reloads. */
961 int insn_needs_for_inputs[N_REG_CLASSES];
962 int insn_groups_for_inputs[N_REG_CLASSES];
963 int insn_total_groups_for_inputs = 0;
965 /* Count RELOAD_FOR_OUTPUT_RELOAD_ADDRESS reloads. */
966 int insn_needs_for_outputs[N_REG_CLASSES];
967 int insn_groups_for_outputs[N_REG_CLASSES];
968 int insn_total_groups_for_outputs = 0;
970 /* Count RELOAD_FOR_OPERAND_ADDRESS reloads. */
971 int insn_needs_for_operands[N_REG_CLASSES];
972 int insn_groups_for_operands[N_REG_CLASSES];
973 int insn_total_groups_for_operands = 0;
975 #if 0 /* This wouldn't work nowadays, since optimize_bit_field
976 looks for non-strict memory addresses. */
977 /* Optimization: a bit-field instruction whose field
978 happens to be a byte or halfword in memory
979 can be changed to a move instruction. */
981 if (GET_CODE (PATTERN (insn)) == SET)
983 rtx dest = SET_DEST (PATTERN (insn));
984 rtx src = SET_SRC (PATTERN (insn));
986 if (GET_CODE (dest) == ZERO_EXTRACT
987 || GET_CODE (dest) == SIGN_EXTRACT)
988 optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
989 if (GET_CODE (src) == ZERO_EXTRACT
990 || GET_CODE (src) == SIGN_EXTRACT)
991 optimize_bit_field (PATTERN (insn), insn, reg_equiv_mem);
993 #endif
995 /* If needed, eliminate any eliminable registers. */
996 if (num_eliminable)
997 did_elimination = eliminate_regs_in_insn (insn, 0);
999 #ifdef SMALL_REGISTER_CLASSES
1000 /* Set avoid_return_reg if this is an insn
1001 that might use the value of a function call. */
1002 if (GET_CODE (insn) == CALL_INSN)
1004 if (GET_CODE (PATTERN (insn)) == SET)
1005 after_call = SET_DEST (PATTERN (insn));
1006 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1007 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1008 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1009 else
1010 after_call = 0;
1012 else if (after_call != 0
1013 && !(GET_CODE (PATTERN (insn)) == SET
1014 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
1016 if (reg_mentioned_p (after_call, PATTERN (insn)))
1017 avoid_return_reg = after_call;
1018 after_call = 0;
1020 #endif /* SMALL_REGISTER_CLASSES */
1022 /* Analyze the instruction. */
1023 find_reloads (insn, 0, spill_indirect_levels, global,
1024 spill_reg_order);
1026 /* Remember for later shortcuts which insns had any reloads or
1027 register eliminations.
1029 One might think that it would be worthwhile to mark insns
1030 that need register replacements but not reloads, but this is
1031 not safe because find_reloads may do some manipulation of
1032 the insn (such as swapping commutative operands), which would
1033 be lost when we restore the old pattern after register
1034 replacement. So the actions of find_reloads must be redone in
1035 subsequent passes or in reload_as_needed.
1037 However, it is safe to mark insns that need reloads
1038 but not register replacement. */
1040 PUT_MODE (insn, (did_elimination ? QImode
1041 : n_reloads ? HImode
1042 : VOIDmode));
1044 /* Discard any register replacements done. */
1045 if (did_elimination)
1047 obstack_free (&reload_obstack, reload_firstobj);
1048 PATTERN (insn) = old_body;
1049 INSN_CODE (insn) = old_code;
1050 REG_NOTES (insn) = old_notes;
1051 something_needs_elimination = 1;
1054 /* If this insn has no reloads, we need not do anything except
1055 in the case of a CALL_INSN when we have caller-saves and
1056 caller-save needs reloads. */
1058 if (n_reloads == 0
1059 && ! (GET_CODE (insn) == CALL_INSN
1060 && caller_save_spill_class != NO_REGS))
1061 continue;
1063 something_needs_reloads = 1;
1065 for (i = 0; i < N_REG_CLASSES; i++)
1067 insn_needs[i] = 0, insn_groups[i] = 0;
1068 insn_needs_for_inputs[i] = 0, insn_groups_for_inputs[i] = 0;
1069 insn_needs_for_outputs[i] = 0, insn_groups_for_outputs[i] = 0;
1070 insn_needs_for_operands[i] = 0, insn_groups_for_operands[i] = 0;
1073 /* Count each reload once in every class
1074 containing the reload's own class. */
1076 for (i = 0; i < n_reloads; i++)
1078 register enum reg_class *p;
1079 enum reg_class class = reload_reg_class[i];
1080 int size;
1081 enum machine_mode mode;
1082 int *this_groups;
1083 int *this_needs;
1084 int *this_total_groups;
1086 /* Don't count the dummy reloads, for which one of the
1087 regs mentioned in the insn can be used for reloading.
1088 Don't count optional reloads.
1089 Don't count reloads that got combined with others. */
1090 if (reload_reg_rtx[i] != 0
1091 || reload_optional[i] != 0
1092 || (reload_out[i] == 0 && reload_in[i] == 0
1093 && ! reload_secondary_p[i]))
1094 continue;
1096 /* Show that a reload register of this class is needed
1097 in this basic block. We do not use insn_needs and
1098 insn_groups because they are overly conservative for
1099 this purpose. */
1100 if (global && ! basic_block_needs[(int) class][this_block])
1102 basic_block_needs[(int) class][this_block] = 1;
1103 new_basic_block_needs = 1;
1106 /* Decide which time-of-use to count this reload for. */
1107 switch (reload_when_needed[i])
1109 case RELOAD_OTHER:
1110 case RELOAD_FOR_OUTPUT:
1111 case RELOAD_FOR_INPUT:
1112 this_needs = insn_needs;
1113 this_groups = insn_groups;
1114 this_total_groups = &insn_total_groups;
1115 break;
1117 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
1118 this_needs = insn_needs_for_inputs;
1119 this_groups = insn_groups_for_inputs;
1120 this_total_groups = &insn_total_groups_for_inputs;
1121 break;
1123 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
1124 this_needs = insn_needs_for_outputs;
1125 this_groups = insn_groups_for_outputs;
1126 this_total_groups = &insn_total_groups_for_outputs;
1127 break;
1129 case RELOAD_FOR_OPERAND_ADDRESS:
1130 this_needs = insn_needs_for_operands;
1131 this_groups = insn_groups_for_operands;
1132 this_total_groups = &insn_total_groups_for_operands;
1133 break;
1136 mode = reload_inmode[i];
1137 if (GET_MODE_SIZE (reload_outmode[i]) > GET_MODE_SIZE (mode))
1138 mode = reload_outmode[i];
1139 size = CLASS_MAX_NREGS (class, mode);
1140 if (size > 1)
1142 enum machine_mode other_mode, allocate_mode;
1144 /* Count number of groups needed separately from
1145 number of individual regs needed. */
1146 this_groups[(int) class]++;
1147 p = reg_class_superclasses[(int) class];
1148 while (*p != LIM_REG_CLASSES)
1149 this_groups[(int) *p++]++;
1150 (*this_total_groups)++;
1152 /* Record size and mode of a group of this class. */
1153 /* If more than one size group is needed,
1154 make all groups the largest needed size. */
1155 if (group_size[(int) class] < size)
1157 other_mode = group_mode[(int) class];
1158 allocate_mode = mode;
1160 group_size[(int) class] = size;
1161 group_mode[(int) class] = mode;
1163 else
1165 other_mode = mode;
1166 allocate_mode = group_mode[(int) class];
1169 /* Crash if two dissimilar machine modes both need
1170 groups of consecutive regs of the same class. */
1172 if (other_mode != VOIDmode
1173 && other_mode != allocate_mode
1174 && ! modes_equiv_for_class_p (allocate_mode,
1175 other_mode,
1176 class))
1177 abort ();
1179 else if (size == 1)
1181 this_needs[(int) class] += 1;
1182 p = reg_class_superclasses[(int) class];
1183 while (*p != LIM_REG_CLASSES)
1184 this_needs[(int) *p++] += 1;
1186 else
1187 abort ();
1190 /* All reloads have been counted for this insn;
1191 now merge the various times of use.
1192 This sets insn_needs, etc., to the maximum total number
1193 of registers needed at any point in this insn. */
1195 for (i = 0; i < N_REG_CLASSES; i++)
1197 int this_max;
1198 this_max = insn_needs_for_inputs[i];
1199 if (insn_needs_for_outputs[i] > this_max)
1200 this_max = insn_needs_for_outputs[i];
1201 if (insn_needs_for_operands[i] > this_max)
1202 this_max = insn_needs_for_operands[i];
1203 insn_needs[i] += this_max;
1204 this_max = insn_groups_for_inputs[i];
1205 if (insn_groups_for_outputs[i] > this_max)
1206 this_max = insn_groups_for_outputs[i];
1207 if (insn_groups_for_operands[i] > this_max)
1208 this_max = insn_groups_for_operands[i];
1209 insn_groups[i] += this_max;
1212 insn_total_groups += MAX (insn_total_groups_for_inputs,
1213 MAX (insn_total_groups_for_outputs,
1214 insn_total_groups_for_operands));
1216 /* If this is a CALL_INSN and caller-saves will need
1217 a spill register, act as if the spill register is
1218 needed for this insn. However, the spill register
1219 can be used by any reload of this insn, so we only
1220 need do something if no need for that class has
1221 been recorded.
1223 The assumption that every CALL_INSN will trigger a
1224 caller-save is highly conservative, however, the number
1225 of cases where caller-saves will need a spill register but
1226 a block containing a CALL_INSN won't need a spill register
1227 of that class should be quite rare.
1229 If a group is needed, the size and mode of the group will
1230 have been set up at the beginning of this loop. */
1232 if (GET_CODE (insn) == CALL_INSN
1233 && caller_save_spill_class != NO_REGS)
1235 int *caller_save_needs
1236 = (caller_save_group_size > 1 ? insn_groups : insn_needs);
1238 if (caller_save_needs[(int) caller_save_spill_class] == 0)
1240 register enum reg_class *p
1241 = reg_class_superclasses[(int) caller_save_spill_class];
1243 caller_save_needs[(int) caller_save_spill_class]++;
1245 while (*p != LIM_REG_CLASSES)
1246 caller_save_needs[(int) *p++] += 1;
1249 if (caller_save_group_size > 1)
1250 insn_total_groups = MAX (insn_total_groups, 1);
1253 /* Show that this basic block will need a register of
1254 this class. */
1256 if (global
1257 && ! (basic_block_needs[(int) caller_save_spill_class]
1258 [this_block]))
1260 basic_block_needs[(int) caller_save_spill_class]
1261 [this_block] = 1;
1262 new_basic_block_needs = 1;
1266 #ifdef SMALL_REGISTER_CLASSES
1267 /* If this insn stores the value of a function call,
1268 and that value is in a register that has been spilled,
1269 and if the insn needs a reload in a class
1270 that might use that register as the reload register,
1271 then add add an extra need in that class.
1272 This makes sure we have a register available that does
1273 not overlap the return value. */
1274 if (avoid_return_reg)
1276 int regno = REGNO (avoid_return_reg);
1277 int nregs
1278 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
1279 int r;
1280 int inc_groups = 0;
1281 for (r = regno; r < regno + nregs; r++)
1282 if (spill_reg_order[r] >= 0)
1283 for (i = 0; i < N_REG_CLASSES; i++)
1284 if (TEST_HARD_REG_BIT (reg_class_contents[i], r))
1286 if (insn_needs[i] > 0)
1287 insn_needs[i]++;
1288 if (insn_groups[i] > 0
1289 && nregs > 1)
1290 inc_groups = 1;
1292 if (inc_groups)
1293 insn_groups[i]++;
1295 #endif /* SMALL_REGISTER_CLASSES */
1297 /* For each class, collect maximum need of any insn. */
1299 for (i = 0; i < N_REG_CLASSES; i++)
1301 if (max_needs[i] < insn_needs[i])
1303 max_needs[i] = insn_needs[i];
1304 max_needs_insn[i] = insn;
1306 if (max_groups[i] < insn_groups[i])
1308 max_groups[i] = insn_groups[i];
1309 max_groups_insn[i] = insn;
1311 if (insn_total_groups > 0)
1312 if (max_nongroups[i] < insn_needs[i])
1314 max_nongroups[i] = insn_needs[i];
1315 max_nongroups_insn[i] = insn;
1319 /* Note that there is a continue statement above. */
1322 /* If we allocated any new memory locations, make another pass
1323 since it might have changed elimination offsets. */
1324 if (starting_frame_size != get_frame_size ())
1325 something_changed = 1;
1327 if (dumpfile)
1328 for (i = 0; i < N_REG_CLASSES; i++)
1330 if (max_needs[i] > 0)
1331 fprintf (dumpfile,
1332 ";; Need %d reg%s of class %s (for insn %d).\n",
1333 max_needs[i], max_needs[i] == 1 ? "" : "s",
1334 reg_class_names[i], INSN_UID (max_needs_insn[i]));
1335 if (max_nongroups[i] > 0)
1336 fprintf (dumpfile,
1337 ";; Need %d nongroup reg%s of class %s (for insn %d).\n",
1338 max_nongroups[i], max_nongroups[i] == 1 ? "" : "s",
1339 reg_class_names[i], INSN_UID (max_nongroups_insn[i]));
1340 if (max_groups[i] > 0)
1341 fprintf (dumpfile,
1342 ";; Need %d group%s (%smode) of class %s (for insn %d).\n",
1343 max_groups[i], max_groups[i] == 1 ? "" : "s",
1344 mode_name[(int) group_mode[i]],
1345 reg_class_names[i], INSN_UID (max_groups_insn[i]));
1348 /* If we have caller-saves, set up the save areas and see if caller-save
1349 will need a spill register. */
1351 if (caller_save_needed
1352 && ! setup_save_areas (&something_changed)
1353 && caller_save_spill_class == NO_REGS)
1355 /* The class we will need depends on whether the machine
1356 supports the sum of two registers for an address; see
1357 find_address_reloads for details. */
1359 caller_save_spill_class
1360 = double_reg_address_ok ? INDEX_REG_CLASS : BASE_REG_CLASS;
1361 caller_save_group_size
1362 = CLASS_MAX_NREGS (caller_save_spill_class, Pmode);
1363 something_changed = 1;
1366 /* Now deduct from the needs for the registers already
1367 available (already spilled). */
1369 CLEAR_HARD_REG_SET (counted_for_groups);
1370 CLEAR_HARD_REG_SET (counted_for_nongroups);
1372 /* First find all regs alone in their class
1373 and count them (if desired) for non-groups.
1374 We would be screwed if a group took the only reg in a class
1375 for which a non-group reload is needed.
1376 (Note there is still a bug; if a class has 2 regs,
1377 both could be stolen by groups and we would lose the same way.
1378 With luck, no machine will need a nongroup in a 2-reg class.) */
1380 for (i = 0; i < n_spills; i++)
1382 register enum reg_class *p;
1383 class = (int) REGNO_REG_CLASS (spill_regs[i]);
1385 if (reg_class_size[class] == 1 && max_nongroups[class] > 0)
1387 max_needs[class]--;
1388 p = reg_class_superclasses[class];
1389 while (*p != LIM_REG_CLASSES)
1390 max_needs[(int) *p++]--;
1392 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1393 max_nongroups[class]--;
1394 p = reg_class_superclasses[class];
1395 while (*p != LIM_REG_CLASSES)
1397 if (max_nongroups[(int) *p] > 0)
1398 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1399 max_nongroups[(int) *p++]--;
1404 /* Now find all consecutive groups of spilled registers
1405 and mark each group off against the need for such groups.
1406 But don't count them against ordinary need, yet. */
1408 count_possible_groups (group_size, group_mode, max_groups);
1410 /* Now count all spill regs against the individual need,
1411 This includes those counted above for groups,
1412 but not those previously counted for nongroups.
1414 Those that weren't counted_for_groups can also count against
1415 the not-in-group need. */
1417 for (i = 0; i < n_spills; i++)
1419 register enum reg_class *p;
1420 class = (int) REGNO_REG_CLASS (spill_regs[i]);
1422 /* Those counted at the beginning shouldn't be counted twice. */
1423 if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
1425 max_needs[class]--;
1426 p = reg_class_superclasses[class];
1427 while (*p != LIM_REG_CLASSES)
1428 max_needs[(int) *p++]--;
1430 if (! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[i]))
1432 if (max_nongroups[class] > 0)
1433 SET_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]);
1434 max_nongroups[class]--;
1435 p = reg_class_superclasses[class];
1436 while (*p != LIM_REG_CLASSES)
1438 if (max_nongroups[(int) *p] > 0)
1439 SET_HARD_REG_BIT (counted_for_nongroups,
1440 spill_regs[i]);
1441 max_nongroups[(int) *p++]--;
1447 /* See if anything that happened changes which eliminations are valid.
1448 For example, on the Sparc, whether or not the frame pointer can
1449 be eliminated can depend on what registers have been used. We need
1450 not check some conditions again (such as flag_omit_frame_pointer)
1451 since they can't have changed. */
1453 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1454 if ((ep->from == FRAME_POINTER_REGNUM && FRAME_POINTER_REQUIRED)
1455 #ifdef ELIMINABLE_REGS
1456 || ! CAN_ELIMINATE (ep->from, ep->to)
1457 #endif
1459 ep->can_eliminate = 0;
1461 /* Look for the case where we have discovered that we can't replace
1462 register A with register B and that means that we will now be
1463 trying to replace register A with register C. This means we can
1464 no longer replace register C with register B and we need to disable
1465 such an elimination, if it exists. This occurs often with A == ap,
1466 B == sp, and C == fp. */
1468 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1470 struct elim_table *op;
1471 register int new_to = -1;
1473 if (! ep->can_eliminate && ep->can_eliminate_previous)
1475 /* Find the current elimination for ep->from, if there is a
1476 new one. */
1477 for (op = reg_eliminate;
1478 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1479 if (op->from == ep->from && op->can_eliminate)
1481 new_to = op->to;
1482 break;
1485 /* See if there is an elimination of NEW_TO -> EP->TO. If so,
1486 disable it. */
1487 for (op = reg_eliminate;
1488 op < &reg_eliminate[NUM_ELIMINABLE_REGS]; op++)
1489 if (op->from == new_to && op->to == ep->to)
1490 op->can_eliminate = 0;
1494 /* See if any registers that we thought we could eliminate the previous
1495 time are no longer eliminable. If so, something has changed and we
1496 must spill the register. Also, recompute the number of eliminable
1497 registers and see if the frame pointer is needed; it is if there is
1498 no elimination of the frame pointer that we can perform. */
1500 frame_pointer_needed = 1;
1501 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1503 if (ep->can_eliminate && ep->from == FRAME_POINTER_REGNUM)
1504 frame_pointer_needed = 0;
1506 if (! ep->can_eliminate && ep->can_eliminate_previous)
1508 ep->can_eliminate_previous = 0;
1509 spill_hard_reg (ep->from, global, dumpfile, 1);
1510 regs_ever_live[ep->from] = 1;
1511 something_changed = 1;
1512 num_eliminable--;
1516 /* If all needs are met, we win. */
1518 for (i = 0; i < N_REG_CLASSES; i++)
1519 if (max_needs[i] > 0 || max_groups[i] > 0 || max_nongroups[i] > 0)
1520 break;
1521 if (i == N_REG_CLASSES && !new_basic_block_needs && ! something_changed)
1522 break;
1524 /* Not all needs are met; must spill more hard regs. */
1526 /* If any element of basic_block_needs changed from 0 to 1,
1527 re-spill all the regs already spilled. This may spill
1528 additional pseudos that didn't spill before. */
1530 if (new_basic_block_needs)
1531 for (i = 0; i < n_spills; i++)
1532 something_changed
1533 |= spill_hard_reg (spill_regs[i], global, dumpfile, 0);
1535 /* Now find more reload regs to satisfy the remaining need
1536 Do it by ascending class number, since otherwise a reg
1537 might be spilled for a big class and might fail to count
1538 for a smaller class even though it belongs to that class.
1540 Count spilled regs in `spills', and add entries to
1541 `spill_regs' and `spill_reg_order'.
1543 ??? Note there is a problem here.
1544 When there is a need for a group in a high-numbered class,
1545 and also need for non-group regs that come from a lower class,
1546 the non-group regs are chosen first. If there aren't many regs,
1547 they might leave no room for a group.
1549 This was happening on the 386. To fix it, we added the code
1550 that calls possible_group_p, so that the lower class won't
1551 break up the last possible group.
1553 Really fixing the problem would require changes above
1554 in counting the regs already spilled, and in choose_reload_regs.
1555 It might be hard to avoid introducing bugs there. */
1557 for (class = 0; class < N_REG_CLASSES; class++)
1559 /* First get the groups of registers.
1560 If we got single registers first, we might fragment
1561 possible groups. */
1562 while (max_groups[class] > 0)
1564 /* If any single spilled regs happen to form groups,
1565 count them now. Maybe we don't really need
1566 to spill another group. */
1567 count_possible_groups (group_size, group_mode, max_groups);
1569 /* Groups of size 2 (the only groups used on most machines)
1570 are treated specially. */
1571 if (group_size[class] == 2)
1573 /* First, look for a register that will complete a group. */
1574 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1576 int j = potential_reload_regs[i];
1577 int other;
1578 if (j >= 0 && ! TEST_HARD_REG_BIT (bad_spill_regs, j)
1580 ((j > 0 && (other = j - 1, spill_reg_order[other] >= 0)
1581 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1582 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1583 && HARD_REGNO_MODE_OK (other, group_mode[class])
1584 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1585 other)
1586 /* We don't want one part of another group.
1587 We could get "two groups" that overlap! */
1588 && ! TEST_HARD_REG_BIT (counted_for_groups, other))
1590 (j < FIRST_PSEUDO_REGISTER - 1
1591 && (other = j + 1, spill_reg_order[other] >= 0)
1592 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1593 && TEST_HARD_REG_BIT (reg_class_contents[class], other)
1594 && HARD_REGNO_MODE_OK (j, group_mode[class])
1595 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1596 other)
1597 && ! TEST_HARD_REG_BIT (counted_for_groups,
1598 other))))
1600 register enum reg_class *p;
1602 /* We have found one that will complete a group,
1603 so count off one group as provided. */
1604 max_groups[class]--;
1605 p = reg_class_superclasses[class];
1606 while (*p != LIM_REG_CLASSES)
1607 max_groups[(int) *p++]--;
1609 /* Indicate both these regs are part of a group. */
1610 SET_HARD_REG_BIT (counted_for_groups, j);
1611 SET_HARD_REG_BIT (counted_for_groups, other);
1612 break;
1615 /* We can't complete a group, so start one. */
1616 if (i == FIRST_PSEUDO_REGISTER)
1617 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1619 int j = potential_reload_regs[i];
1620 if (j >= 0 && j + 1 < FIRST_PSEUDO_REGISTER
1621 && spill_reg_order[j] < 0 && spill_reg_order[j + 1] < 0
1622 && TEST_HARD_REG_BIT (reg_class_contents[class], j)
1623 && TEST_HARD_REG_BIT (reg_class_contents[class], j + 1)
1624 && HARD_REGNO_MODE_OK (j, group_mode[class])
1625 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1626 j + 1))
1627 break;
1630 /* I should be the index in potential_reload_regs
1631 of the new reload reg we have found. */
1633 if (i >= FIRST_PSEUDO_REGISTER)
1635 /* There are no groups left to spill. */
1636 spill_failure (max_groups_insn[class]);
1637 failure = 1;
1638 goto failed;
1640 else
1641 something_changed
1642 |= new_spill_reg (i, class, max_needs, NULL_PTR,
1643 global, dumpfile);
1645 else
1647 /* For groups of more than 2 registers,
1648 look for a sufficient sequence of unspilled registers,
1649 and spill them all at once. */
1650 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1652 int j = potential_reload_regs[i];
1653 int k;
1654 if (j >= 0
1655 && j + group_size[class] <= FIRST_PSEUDO_REGISTER
1656 && HARD_REGNO_MODE_OK (j, group_mode[class]))
1658 /* Check each reg in the sequence. */
1659 for (k = 0; k < group_size[class]; k++)
1660 if (! (spill_reg_order[j + k] < 0
1661 && ! TEST_HARD_REG_BIT (bad_spill_regs, j + k)
1662 && TEST_HARD_REG_BIT (reg_class_contents[class], j + k)))
1663 break;
1664 /* We got a full sequence, so spill them all. */
1665 if (k == group_size[class])
1667 register enum reg_class *p;
1668 for (k = 0; k < group_size[class]; k++)
1670 int idx;
1671 SET_HARD_REG_BIT (counted_for_groups, j + k);
1672 for (idx = 0; idx < FIRST_PSEUDO_REGISTER; idx++)
1673 if (potential_reload_regs[idx] == j + k)
1674 break;
1675 something_changed
1676 |= new_spill_reg (idx, class,
1677 max_needs, NULL_PTR,
1678 global, dumpfile);
1681 /* We have found one that will complete a group,
1682 so count off one group as provided. */
1683 max_groups[class]--;
1684 p = reg_class_superclasses[class];
1685 while (*p != LIM_REG_CLASSES)
1686 max_groups[(int) *p++]--;
1688 break;
1692 /* We couldn't find any registers for this reload.
1693 Avoid going into an infinite loop. */
1694 if (i >= FIRST_PSEUDO_REGISTER)
1696 /* There are no groups left. */
1697 spill_failure (max_groups_insn[class]);
1698 failure = 1;
1699 goto failed;
1704 /* Now similarly satisfy all need for single registers. */
1706 while (max_needs[class] > 0 || max_nongroups[class] > 0)
1708 /* Consider the potential reload regs that aren't
1709 yet in use as reload regs, in order of preference.
1710 Find the most preferred one that's in this class. */
1712 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1713 if (potential_reload_regs[i] >= 0
1714 && TEST_HARD_REG_BIT (reg_class_contents[class],
1715 potential_reload_regs[i])
1716 /* If this reg will not be available for groups,
1717 pick one that does not foreclose possible groups.
1718 This is a kludge, and not very general,
1719 but it should be sufficient to make the 386 work,
1720 and the problem should not occur on machines with
1721 more registers. */
1722 && (max_nongroups[class] == 0
1723 || possible_group_p (potential_reload_regs[i], max_groups)))
1724 break;
1726 /* If we couldn't get a register, try to get one even if we
1727 might foreclose possible groups. This may cause problems
1728 later, but that's better than aborting now, since it is
1729 possible that we will, in fact, be able to form the needed
1730 group even with this allocation. */
1732 if (i >= FIRST_PSEUDO_REGISTER
1733 && (asm_noperands (max_needs[class] > 0
1734 ? max_needs_insn[class]
1735 : max_nongroups_insn[class])
1736 < 0))
1737 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1738 if (potential_reload_regs[i] >= 0
1739 && TEST_HARD_REG_BIT (reg_class_contents[class],
1740 potential_reload_regs[i]))
1741 break;
1743 /* I should be the index in potential_reload_regs
1744 of the new reload reg we have found. */
1746 if (i >= FIRST_PSEUDO_REGISTER)
1748 /* There are no possible registers left to spill. */
1749 spill_failure (max_needs[class] > 0 ? max_needs_insn[class]
1750 : max_nongroups_insn[class]);
1751 failure = 1;
1752 goto failed;
1754 else
1755 something_changed
1756 |= new_spill_reg (i, class, max_needs, max_nongroups,
1757 global, dumpfile);
1762 /* If global-alloc was run, notify it of any register eliminations we have
1763 done. */
1764 if (global)
1765 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
1766 if (ep->can_eliminate)
1767 mark_elimination (ep->from, ep->to);
1769 /* Insert code to save and restore call-clobbered hard regs
1770 around calls. Tell if what mode to use so that we will process
1771 those insns in reload_as_needed if we have to. */
1773 if (caller_save_needed)
1774 save_call_clobbered_regs (num_eliminable ? QImode
1775 : caller_save_spill_class != NO_REGS ? HImode
1776 : VOIDmode);
1778 /* If a pseudo has no hard reg, delete the insns that made the equivalence.
1779 If that insn didn't set the register (i.e., it copied the register to
1780 memory), just delete that insn instead of the equivalencing insn plus
1781 anything now dead. If we call delete_dead_insn on that insn, we may
1782 delete the insn that actually sets the register if the register die
1783 there and that is incorrect. */
1785 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1786 if (reg_renumber[i] < 0 && reg_equiv_init[i] != 0
1787 && GET_CODE (reg_equiv_init[i]) != NOTE)
1789 if (reg_set_p (regno_reg_rtx[i], PATTERN (reg_equiv_init[i])))
1790 delete_dead_insn (reg_equiv_init[i]);
1791 else
1793 PUT_CODE (reg_equiv_init[i], NOTE);
1794 NOTE_SOURCE_FILE (reg_equiv_init[i]) = 0;
1795 NOTE_LINE_NUMBER (reg_equiv_init[i]) = NOTE_INSN_DELETED;
1799 /* Use the reload registers where necessary
1800 by generating move instructions to move the must-be-register
1801 values into or out of the reload registers. */
1803 if (something_needs_reloads || something_needs_elimination
1804 || (caller_save_needed && num_eliminable)
1805 || caller_save_spill_class != NO_REGS)
1806 reload_as_needed (first, global);
1808 reload_in_progress = 0;
1810 /* Come here (with failure set nonzero) if we can't get enough spill regs
1811 and we decide not to abort about it. */
1812 failed:
1814 /* Now eliminate all pseudo regs by modifying them into
1815 their equivalent memory references.
1816 The REG-rtx's for the pseudos are modified in place,
1817 so all insns that used to refer to them now refer to memory.
1819 For a reg that has a reg_equiv_address, all those insns
1820 were changed by reloading so that no insns refer to it any longer;
1821 but the DECL_RTL of a variable decl may refer to it,
1822 and if so this causes the debugging info to mention the variable. */
1824 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
1826 rtx addr = 0;
1827 int in_struct = 0;
1828 if (reg_equiv_mem[i])
1830 addr = XEXP (reg_equiv_mem[i], 0);
1831 in_struct = MEM_IN_STRUCT_P (reg_equiv_mem[i]);
1833 if (reg_equiv_address[i])
1834 addr = reg_equiv_address[i];
1835 if (addr)
1837 if (reg_renumber[i] < 0)
1839 rtx reg = regno_reg_rtx[i];
1840 XEXP (reg, 0) = addr;
1841 REG_USERVAR_P (reg) = 0;
1842 MEM_IN_STRUCT_P (reg) = in_struct;
1843 PUT_CODE (reg, MEM);
1845 else if (reg_equiv_mem[i])
1846 XEXP (reg_equiv_mem[i], 0) = addr;
1850 #ifdef PRESERVE_DEATH_INFO_REGNO_P
1851 /* Make a pass over all the insns and remove death notes for things that
1852 are no longer registers or no longer die in the insn (e.g., an input
1853 and output pseudo being tied). */
1855 for (insn = first; insn; insn = NEXT_INSN (insn))
1856 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1858 rtx note, next;
1860 for (note = REG_NOTES (insn); note; note = next)
1862 next = XEXP (note, 1);
1863 if (REG_NOTE_KIND (note) == REG_DEAD
1864 && (GET_CODE (XEXP (note, 0)) != REG
1865 || reg_set_p (XEXP (note, 0), PATTERN (insn))))
1866 remove_note (insn, note);
1869 #endif
1871 /* Indicate that we no longer have known memory locations or constants. */
1872 reg_equiv_constant = 0;
1873 reg_equiv_memory_loc = 0;
1875 return failure;
1878 /* Nonzero if, after spilling reg REGNO for non-groups,
1879 it will still be possible to find a group if we still need one. */
1881 static int
1882 possible_group_p (regno, max_groups)
1883 int regno;
1884 int *max_groups;
1886 int i;
1887 int class = (int) NO_REGS;
1889 for (i = 0; i < (int) N_REG_CLASSES; i++)
1890 if (max_groups[i] > 0)
1892 class = i;
1893 break;
1896 if (class == (int) NO_REGS)
1897 return 1;
1899 /* Consider each pair of consecutive registers. */
1900 for (i = 0; i < FIRST_PSEUDO_REGISTER - 1; i++)
1902 /* Ignore pairs that include reg REGNO. */
1903 if (i == regno || i + 1 == regno)
1904 continue;
1906 /* Ignore pairs that are outside the class that needs the group.
1907 ??? Here we fail to handle the case where two different classes
1908 independently need groups. But this never happens with our
1909 current machine descriptions. */
1910 if (! (TEST_HARD_REG_BIT (reg_class_contents[class], i)
1911 && TEST_HARD_REG_BIT (reg_class_contents[class], i + 1)))
1912 continue;
1914 /* A pair of consecutive regs we can still spill does the trick. */
1915 if (spill_reg_order[i] < 0 && spill_reg_order[i + 1] < 0
1916 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
1917 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1))
1918 return 1;
1920 /* A pair of one already spilled and one we can spill does it
1921 provided the one already spilled is not otherwise reserved. */
1922 if (spill_reg_order[i] < 0
1923 && ! TEST_HARD_REG_BIT (bad_spill_regs, i)
1924 && spill_reg_order[i + 1] >= 0
1925 && ! TEST_HARD_REG_BIT (counted_for_groups, i + 1)
1926 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i + 1))
1927 return 1;
1928 if (spill_reg_order[i + 1] < 0
1929 && ! TEST_HARD_REG_BIT (bad_spill_regs, i + 1)
1930 && spill_reg_order[i] >= 0
1931 && ! TEST_HARD_REG_BIT (counted_for_groups, i)
1932 && ! TEST_HARD_REG_BIT (counted_for_nongroups, i))
1933 return 1;
1936 return 0;
1939 /* Count any groups that can be formed from the registers recently spilled.
1940 This is done class by class, in order of ascending class number. */
1942 static void
1943 count_possible_groups (group_size, group_mode, max_groups)
1944 int *group_size, *max_groups;
1945 enum machine_mode *group_mode;
1947 int i;
1948 /* Now find all consecutive groups of spilled registers
1949 and mark each group off against the need for such groups.
1950 But don't count them against ordinary need, yet. */
1952 for (i = 0; i < N_REG_CLASSES; i++)
1953 if (group_size[i] > 1)
1955 char regmask[FIRST_PSEUDO_REGISTER];
1956 int j;
1958 bzero (regmask, sizeof regmask);
1959 /* Make a mask of all the regs that are spill regs in class I. */
1960 for (j = 0; j < n_spills; j++)
1961 if (TEST_HARD_REG_BIT (reg_class_contents[i], spill_regs[j])
1962 && ! TEST_HARD_REG_BIT (counted_for_groups, spill_regs[j])
1963 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
1964 spill_regs[j]))
1965 regmask[spill_regs[j]] = 1;
1966 /* Find each consecutive group of them. */
1967 for (j = 0; j < FIRST_PSEUDO_REGISTER && max_groups[i] > 0; j++)
1968 if (regmask[j] && j + group_size[i] <= FIRST_PSEUDO_REGISTER
1969 /* Next line in case group-mode for this class
1970 demands an even-odd pair. */
1971 && HARD_REGNO_MODE_OK (j, group_mode[i]))
1973 int k;
1974 for (k = 1; k < group_size[i]; k++)
1975 if (! regmask[j + k])
1976 break;
1977 if (k == group_size[i])
1979 /* We found a group. Mark it off against this class's
1980 need for groups, and against each superclass too. */
1981 register enum reg_class *p;
1982 max_groups[i]--;
1983 p = reg_class_superclasses[i];
1984 while (*p != LIM_REG_CLASSES)
1985 max_groups[(int) *p++]--;
1986 /* Don't count these registers again. */
1987 for (k = 0; k < group_size[i]; k++)
1988 SET_HARD_REG_BIT (counted_for_groups, j + k);
1990 /* Skip to the last reg in this group. When j is incremented
1991 above, it will then point to the first reg of the next
1992 possible group. */
1993 j += k - 1;
1999 /* ALLOCATE_MODE is a register mode that needs to be reloaded. OTHER_MODE is
2000 another mode that needs to be reloaded for the same register class CLASS.
2001 If any reg in CLASS allows ALLOCATE_MODE but not OTHER_MODE, fail.
2002 ALLOCATE_MODE will never be smaller than OTHER_MODE.
2004 This code used to also fail if any reg in CLASS allows OTHER_MODE but not
2005 ALLOCATE_MODE. This test is unnecessary, because we will never try to put
2006 something of mode ALLOCATE_MODE into an OTHER_MODE register. Testing this
2007 causes unnecessary failures on machines requiring alignment of register
2008 groups when the two modes are different sizes, because the larger mode has
2009 more strict alignment rules than the smaller mode. */
2011 static int
2012 modes_equiv_for_class_p (allocate_mode, other_mode, class)
2013 enum machine_mode allocate_mode, other_mode;
2014 enum reg_class class;
2016 register int regno;
2017 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2019 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno)
2020 && HARD_REGNO_MODE_OK (regno, allocate_mode)
2021 && ! HARD_REGNO_MODE_OK (regno, other_mode))
2022 return 0;
2024 return 1;
2027 /* Handle the failure to find a register to spill.
2028 INSN should be one of the insns which needed this particular spill reg. */
2030 static void
2031 spill_failure (insn)
2032 rtx insn;
2034 if (asm_noperands (PATTERN (insn)) >= 0)
2035 error_for_asm (insn, "`asm' needs too many reloads");
2036 else
2037 abort ();
2040 /* Add a new register to the tables of available spill-registers
2041 (as well as spilling all pseudos allocated to the register).
2042 I is the index of this register in potential_reload_regs.
2043 CLASS is the regclass whose need is being satisfied.
2044 MAX_NEEDS and MAX_NONGROUPS are the vectors of needs,
2045 so that this register can count off against them.
2046 MAX_NONGROUPS is 0 if this register is part of a group.
2047 GLOBAL and DUMPFILE are the same as the args that `reload' got. */
2049 static int
2050 new_spill_reg (i, class, max_needs, max_nongroups, global, dumpfile)
2051 int i;
2052 int class;
2053 int *max_needs;
2054 int *max_nongroups;
2055 int global;
2056 FILE *dumpfile;
2058 register enum reg_class *p;
2059 int val;
2060 int regno = potential_reload_regs[i];
2062 if (i >= FIRST_PSEUDO_REGISTER)
2063 abort (); /* Caller failed to find any register. */
2065 if (fixed_regs[regno] || TEST_HARD_REG_BIT (forbidden_regs, regno))
2066 fatal ("fixed or forbidden register was spilled.\n\
2067 This may be due to a compiler bug or to impossible asm statements.");
2069 /* Make reg REGNO an additional reload reg. */
2071 potential_reload_regs[i] = -1;
2072 spill_regs[n_spills] = regno;
2073 spill_reg_order[regno] = n_spills;
2074 if (dumpfile)
2075 fprintf (dumpfile, "Spilling reg %d.\n", spill_regs[n_spills]);
2077 /* Clear off the needs we just satisfied. */
2079 max_needs[class]--;
2080 p = reg_class_superclasses[class];
2081 while (*p != LIM_REG_CLASSES)
2082 max_needs[(int) *p++]--;
2084 if (max_nongroups && max_nongroups[class] > 0)
2086 SET_HARD_REG_BIT (counted_for_nongroups, regno);
2087 max_nongroups[class]--;
2088 p = reg_class_superclasses[class];
2089 while (*p != LIM_REG_CLASSES)
2090 max_nongroups[(int) *p++]--;
2093 /* Spill every pseudo reg that was allocated to this reg
2094 or to something that overlaps this reg. */
2096 val = spill_hard_reg (spill_regs[n_spills], global, dumpfile, 0);
2098 /* If there are some registers still to eliminate and this register
2099 wasn't ever used before, additional stack space may have to be
2100 allocated to store this register. Thus, we may have changed the offset
2101 between the stack and frame pointers, so mark that something has changed.
2102 (If new pseudos were spilled, thus requiring more space, VAL would have
2103 been set non-zero by the call to spill_hard_reg above since additional
2104 reloads may be needed in that case.
2106 One might think that we need only set VAL to 1 if this is a call-used
2107 register. However, the set of registers that must be saved by the
2108 prologue is not identical to the call-used set. For example, the
2109 register used by the call insn for the return PC is a call-used register,
2110 but must be saved by the prologue. */
2111 if (num_eliminable && ! regs_ever_live[spill_regs[n_spills]])
2112 val = 1;
2114 regs_ever_live[spill_regs[n_spills]] = 1;
2115 n_spills++;
2117 return val;
2120 /* Delete an unneeded INSN and any previous insns who sole purpose is loading
2121 data that is dead in INSN. */
2123 static void
2124 delete_dead_insn (insn)
2125 rtx insn;
2127 rtx prev = prev_real_insn (insn);
2128 rtx prev_dest;
2130 /* If the previous insn sets a register that dies in our insn, delete it
2131 too. */
2132 if (prev && GET_CODE (PATTERN (prev)) == SET
2133 && (prev_dest = SET_DEST (PATTERN (prev)), GET_CODE (prev_dest) == REG)
2134 && reg_mentioned_p (prev_dest, PATTERN (insn))
2135 && find_regno_note (insn, REG_DEAD, REGNO (prev_dest)))
2136 delete_dead_insn (prev);
2138 PUT_CODE (insn, NOTE);
2139 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2140 NOTE_SOURCE_FILE (insn) = 0;
2143 /* Modify the home of pseudo-reg I.
2144 The new home is present in reg_renumber[I].
2146 FROM_REG may be the hard reg that the pseudo-reg is being spilled from;
2147 or it may be -1, meaning there is none or it is not relevant.
2148 This is used so that all pseudos spilled from a given hard reg
2149 can share one stack slot. */
2151 static void
2152 alter_reg (i, from_reg)
2153 register int i;
2154 int from_reg;
2156 /* When outputting an inline function, this can happen
2157 for a reg that isn't actually used. */
2158 if (regno_reg_rtx[i] == 0)
2159 return;
2161 /* If the reg got changed to a MEM at rtl-generation time,
2162 ignore it. */
2163 if (GET_CODE (regno_reg_rtx[i]) != REG)
2164 return;
2166 /* Modify the reg-rtx to contain the new hard reg
2167 number or else to contain its pseudo reg number. */
2168 REGNO (regno_reg_rtx[i])
2169 = reg_renumber[i] >= 0 ? reg_renumber[i] : i;
2171 /* If we have a pseudo that is needed but has no hard reg or equivalent,
2172 allocate a stack slot for it. */
2174 if (reg_renumber[i] < 0
2175 && reg_n_refs[i] > 0
2176 && reg_equiv_constant[i] == 0
2177 && reg_equiv_memory_loc[i] == 0)
2179 register rtx x;
2180 int inherent_size = PSEUDO_REGNO_BYTES (i);
2181 int total_size = MAX (inherent_size, reg_max_ref_width[i]);
2182 int adjust = 0;
2184 /* Each pseudo reg has an inherent size which comes from its own mode,
2185 and a total size which provides room for paradoxical subregs
2186 which refer to the pseudo reg in wider modes.
2188 We can use a slot already allocated if it provides both
2189 enough inherent space and enough total space.
2190 Otherwise, we allocate a new slot, making sure that it has no less
2191 inherent space, and no less total space, then the previous slot. */
2192 if (from_reg == -1)
2194 /* No known place to spill from => no slot to reuse. */
2195 x = assign_stack_local (GET_MODE (regno_reg_rtx[i]), total_size, -1);
2196 #if BYTES_BIG_ENDIAN
2197 /* Cancel the big-endian correction done in assign_stack_local.
2198 Get the address of the beginning of the slot.
2199 This is so we can do a big-endian correction unconditionally
2200 below. */
2201 adjust = inherent_size - total_size;
2202 #endif
2204 /* Reuse a stack slot if possible. */
2205 else if (spill_stack_slot[from_reg] != 0
2206 && spill_stack_slot_width[from_reg] >= total_size
2207 && (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2208 >= inherent_size))
2209 x = spill_stack_slot[from_reg];
2210 /* Allocate a bigger slot. */
2211 else
2213 /* Compute maximum size needed, both for inherent size
2214 and for total size. */
2215 enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
2216 if (spill_stack_slot[from_reg])
2218 if (GET_MODE_SIZE (GET_MODE (spill_stack_slot[from_reg]))
2219 > inherent_size)
2220 mode = GET_MODE (spill_stack_slot[from_reg]);
2221 if (spill_stack_slot_width[from_reg] > total_size)
2222 total_size = spill_stack_slot_width[from_reg];
2224 /* Make a slot with that size. */
2225 x = assign_stack_local (mode, total_size, -1);
2226 #if BYTES_BIG_ENDIAN
2227 /* Cancel the big-endian correction done in assign_stack_local.
2228 Get the address of the beginning of the slot.
2229 This is so we can do a big-endian correction unconditionally
2230 below. */
2231 adjust = GET_MODE_SIZE (mode) - total_size;
2232 #endif
2233 spill_stack_slot[from_reg] = x;
2234 spill_stack_slot_width[from_reg] = total_size;
2237 #if BYTES_BIG_ENDIAN
2238 /* On a big endian machine, the "address" of the slot
2239 is the address of the low part that fits its inherent mode. */
2240 if (inherent_size < total_size)
2241 adjust += (total_size - inherent_size);
2242 #endif /* BYTES_BIG_ENDIAN */
2244 /* If we have any adjustment to make, or if the stack slot is the
2245 wrong mode, make a new stack slot. */
2246 if (adjust != 0 || GET_MODE (x) != GET_MODE (regno_reg_rtx[i]))
2248 x = gen_rtx (MEM, GET_MODE (regno_reg_rtx[i]),
2249 plus_constant (XEXP (x, 0), adjust));
2250 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (regno_reg_rtx[i]);
2253 /* Save the stack slot for later. */
2254 reg_equiv_memory_loc[i] = x;
2258 /* Mark the slots in regs_ever_live for the hard regs
2259 used by pseudo-reg number REGNO. */
2261 void
2262 mark_home_live (regno)
2263 int regno;
2265 register int i, lim;
2266 i = reg_renumber[regno];
2267 if (i < 0)
2268 return;
2269 lim = i + HARD_REGNO_NREGS (i, PSEUDO_REGNO_MODE (regno));
2270 while (i < lim)
2271 regs_ever_live[i++] = 1;
2274 /* This function handles the tracking of elimination offsets around branches.
2276 X is a piece of RTL being scanned.
2278 INSN is the insn that it came from, if any.
2280 INITIAL_P is non-zero if we are to set the offset to be the initial
2281 offset and zero if we are setting the offset of the label to be the
2282 current offset. */
2284 static void
2285 set_label_offsets (x, insn, initial_p)
2286 rtx x;
2287 rtx insn;
2288 int initial_p;
2290 enum rtx_code code = GET_CODE (x);
2291 rtx tem;
2292 int i;
2293 struct elim_table *p;
2295 switch (code)
2297 case LABEL_REF:
2298 if (LABEL_REF_NONLOCAL_P (x))
2299 return;
2301 x = XEXP (x, 0);
2303 /* ... fall through ... */
2305 case CODE_LABEL:
2306 /* If we know nothing about this label, set the desired offsets. Note
2307 that this sets the offset at a label to be the offset before a label
2308 if we don't know anything about the label. This is not correct for
2309 the label after a BARRIER, but is the best guess we can make. If
2310 we guessed wrong, we will suppress an elimination that might have
2311 been possible had we been able to guess correctly. */
2313 if (! offsets_known_at[CODE_LABEL_NUMBER (x)])
2315 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2316 offsets_at[CODE_LABEL_NUMBER (x)][i]
2317 = (initial_p ? reg_eliminate[i].initial_offset
2318 : reg_eliminate[i].offset);
2319 offsets_known_at[CODE_LABEL_NUMBER (x)] = 1;
2322 /* Otherwise, if this is the definition of a label and it is
2323 preceded by a BARRIER, set our offsets to the known offset of
2324 that label. */
2326 else if (x == insn
2327 && (tem = prev_nonnote_insn (insn)) != 0
2328 && GET_CODE (tem) == BARRIER)
2330 num_not_at_initial_offset = 0;
2331 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2333 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
2334 = offsets_at[CODE_LABEL_NUMBER (x)][i];
2335 if (reg_eliminate[i].can_eliminate
2336 && (reg_eliminate[i].offset
2337 != reg_eliminate[i].initial_offset))
2338 num_not_at_initial_offset++;
2342 else
2343 /* If neither of the above cases is true, compare each offset
2344 with those previously recorded and suppress any eliminations
2345 where the offsets disagree. */
2347 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
2348 if (offsets_at[CODE_LABEL_NUMBER (x)][i]
2349 != (initial_p ? reg_eliminate[i].initial_offset
2350 : reg_eliminate[i].offset))
2351 reg_eliminate[i].can_eliminate = 0;
2353 return;
2355 case JUMP_INSN:
2356 set_label_offsets (PATTERN (insn), insn, initial_p);
2358 /* ... fall through ... */
2360 case INSN:
2361 case CALL_INSN:
2362 /* Any labels mentioned in REG_LABEL notes can be branched to indirectly
2363 and hence must have all eliminations at their initial offsets. */
2364 for (tem = REG_NOTES (x); tem; tem = XEXP (tem, 1))
2365 if (REG_NOTE_KIND (tem) == REG_LABEL)
2366 set_label_offsets (XEXP (tem, 0), insn, 1);
2367 return;
2369 case ADDR_VEC:
2370 case ADDR_DIFF_VEC:
2371 /* Each of the labels in the address vector must be at their initial
2372 offsets. We want the first first for ADDR_VEC and the second
2373 field for ADDR_DIFF_VEC. */
2375 for (i = 0; i < XVECLEN (x, code == ADDR_DIFF_VEC); i++)
2376 set_label_offsets (XVECEXP (x, code == ADDR_DIFF_VEC, i),
2377 insn, initial_p);
2378 return;
2380 case SET:
2381 /* We only care about setting PC. If the source is not RETURN,
2382 IF_THEN_ELSE, or a label, disable any eliminations not at
2383 their initial offsets. Similarly if any arm of the IF_THEN_ELSE
2384 isn't one of those possibilities. For branches to a label,
2385 call ourselves recursively.
2387 Note that this can disable elimination unnecessarily when we have
2388 a non-local goto since it will look like a non-constant jump to
2389 someplace in the current function. This isn't a significant
2390 problem since such jumps will normally be when all elimination
2391 pairs are back to their initial offsets. */
2393 if (SET_DEST (x) != pc_rtx)
2394 return;
2396 switch (GET_CODE (SET_SRC (x)))
2398 case PC:
2399 case RETURN:
2400 return;
2402 case LABEL_REF:
2403 set_label_offsets (XEXP (SET_SRC (x), 0), insn, initial_p);
2404 return;
2406 case IF_THEN_ELSE:
2407 tem = XEXP (SET_SRC (x), 1);
2408 if (GET_CODE (tem) == LABEL_REF)
2409 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2410 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2411 break;
2413 tem = XEXP (SET_SRC (x), 2);
2414 if (GET_CODE (tem) == LABEL_REF)
2415 set_label_offsets (XEXP (tem, 0), insn, initial_p);
2416 else if (GET_CODE (tem) != PC && GET_CODE (tem) != RETURN)
2417 break;
2418 return;
2421 /* If we reach here, all eliminations must be at their initial
2422 offset because we are doing a jump to a variable address. */
2423 for (p = reg_eliminate; p < &reg_eliminate[NUM_ELIMINABLE_REGS]; p++)
2424 if (p->offset != p->initial_offset)
2425 p->can_eliminate = 0;
2429 /* Used for communication between the next two function to properly share
2430 the vector for an ASM_OPERANDS. */
2432 static struct rtvec_def *old_asm_operands_vec, *new_asm_operands_vec;
2434 /* Scan X and replace any eliminable registers (such as fp) with a
2435 replacement (such as sp), plus an offset.
2437 MEM_MODE is the mode of an enclosing MEM. We need this to know how
2438 much to adjust a register for, e.g., PRE_DEC. Also, if we are inside a
2439 MEM, we are allowed to replace a sum of a register and the constant zero
2440 with the register, which we cannot do outside a MEM. In addition, we need
2441 to record the fact that a register is referenced outside a MEM.
2443 If INSN is nonzero, it is the insn containing X. If we replace a REG
2444 in a SET_DEST with an equivalent MEM and INSN is non-zero, write a
2445 CLOBBER of the pseudo after INSN so find_equiv_regs will know that
2446 that the REG is being modified.
2448 If we see a modification to a register we know about, take the
2449 appropriate action (see case SET, below).
2451 REG_EQUIV_MEM and REG_EQUIV_ADDRESS contain address that have had
2452 replacements done assuming all offsets are at their initial values. If
2453 they are not, or if REG_EQUIV_ADDRESS is nonzero for a pseudo we
2454 encounter, return the actual location so that find_reloads will do
2455 the proper thing. */
2458 eliminate_regs (x, mem_mode, insn)
2459 rtx x;
2460 enum machine_mode mem_mode;
2461 rtx insn;
2463 enum rtx_code code = GET_CODE (x);
2464 struct elim_table *ep;
2465 int regno;
2466 rtx new;
2467 int i, j;
2468 char *fmt;
2469 int copied = 0;
2471 switch (code)
2473 case CONST_INT:
2474 case CONST_DOUBLE:
2475 case CONST:
2476 case SYMBOL_REF:
2477 case CODE_LABEL:
2478 case PC:
2479 case CC0:
2480 case ASM_INPUT:
2481 case ADDR_VEC:
2482 case ADDR_DIFF_VEC:
2483 case RETURN:
2484 return x;
2486 case REG:
2487 regno = REGNO (x);
2489 /* First handle the case where we encounter a bare register that
2490 is eliminable. Replace it with a PLUS. */
2491 if (regno < FIRST_PSEUDO_REGISTER)
2493 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2494 ep++)
2495 if (ep->from_rtx == x && ep->can_eliminate)
2497 if (! mem_mode)
2498 ep->ref_outside_mem = 1;
2499 return plus_constant (ep->to_rtx, ep->previous_offset);
2503 else if (reg_equiv_memory_loc && reg_equiv_memory_loc[regno]
2504 && (reg_equiv_address[regno] || num_not_at_initial_offset))
2506 /* In this case, find_reloads would attempt to either use an
2507 incorrect address (if something is not at its initial offset)
2508 or substitute an replaced address into an insn (which loses
2509 if the offset is changed by some later action). So we simply
2510 return the replaced stack slot (assuming it is changed by
2511 elimination) and ignore the fact that this is actually a
2512 reference to the pseudo. Ensure we make a copy of the
2513 address in case it is shared. */
2514 new = eliminate_regs (reg_equiv_memory_loc[regno],
2515 mem_mode, NULL_RTX);
2516 if (new != reg_equiv_memory_loc[regno])
2517 return copy_rtx (new);
2519 return x;
2521 case PLUS:
2522 /* If this is the sum of an eliminable register and a constant, rework
2523 the sum. */
2524 if (GET_CODE (XEXP (x, 0)) == REG
2525 && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER
2526 && CONSTANT_P (XEXP (x, 1)))
2528 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2529 ep++)
2530 if (ep->from_rtx == XEXP (x, 0) && ep->can_eliminate)
2532 if (! mem_mode)
2533 ep->ref_outside_mem = 1;
2535 /* The only time we want to replace a PLUS with a REG (this
2536 occurs when the constant operand of the PLUS is the negative
2537 of the offset) is when we are inside a MEM. We won't want
2538 to do so at other times because that would change the
2539 structure of the insn in a way that reload can't handle.
2540 We special-case the commonest situation in
2541 eliminate_regs_in_insn, so just replace a PLUS with a
2542 PLUS here, unless inside a MEM. */
2543 if (mem_mode != 0 && GET_CODE (XEXP (x, 1)) == CONST_INT
2544 && INTVAL (XEXP (x, 1)) == - ep->previous_offset)
2545 return ep->to_rtx;
2546 else
2547 return gen_rtx (PLUS, Pmode, ep->to_rtx,
2548 plus_constant (XEXP (x, 1),
2549 ep->previous_offset));
2552 /* If the register is not eliminable, we are done since the other
2553 operand is a constant. */
2554 return x;
2557 /* If this is part of an address, we want to bring any constant to the
2558 outermost PLUS. We will do this by doing register replacement in
2559 our operands and seeing if a constant shows up in one of them.
2561 We assume here this is part of an address (or a "load address" insn)
2562 since an eliminable register is not likely to appear in any other
2563 context.
2565 If we have (plus (eliminable) (reg)), we want to produce
2566 (plus (plus (replacement) (reg) (const))). If this was part of a
2567 normal add insn, (plus (replacement) (reg)) will be pushed as a
2568 reload. This is the desired action. */
2571 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2572 rtx new1 = eliminate_regs (XEXP (x, 1), mem_mode, NULL_RTX);
2574 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2576 /* If one side is a PLUS and the other side is a pseudo that
2577 didn't get a hard register but has a reg_equiv_constant,
2578 we must replace the constant here since it may no longer
2579 be in the position of any operand. */
2580 if (GET_CODE (new0) == PLUS && GET_CODE (new1) == REG
2581 && REGNO (new1) >= FIRST_PSEUDO_REGISTER
2582 && reg_renumber[REGNO (new1)] < 0
2583 && reg_equiv_constant != 0
2584 && reg_equiv_constant[REGNO (new1)] != 0)
2585 new1 = reg_equiv_constant[REGNO (new1)];
2586 else if (GET_CODE (new1) == PLUS && GET_CODE (new0) == REG
2587 && REGNO (new0) >= FIRST_PSEUDO_REGISTER
2588 && reg_renumber[REGNO (new0)] < 0
2589 && reg_equiv_constant[REGNO (new0)] != 0)
2590 new0 = reg_equiv_constant[REGNO (new0)];
2592 new = form_sum (new0, new1);
2594 /* As above, if we are not inside a MEM we do not want to
2595 turn a PLUS into something else. We might try to do so here
2596 for an addition of 0 if we aren't optimizing. */
2597 if (! mem_mode && GET_CODE (new) != PLUS)
2598 return gen_rtx (PLUS, GET_MODE (x), new, const0_rtx);
2599 else
2600 return new;
2603 return x;
2605 case EXPR_LIST:
2606 /* If we have something in XEXP (x, 0), the usual case, eliminate it. */
2607 if (XEXP (x, 0))
2609 new = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2610 if (new != XEXP (x, 0))
2611 x = gen_rtx (EXPR_LIST, REG_NOTE_KIND (x), new, XEXP (x, 1));
2614 /* ... fall through ... */
2616 case INSN_LIST:
2617 /* Now do eliminations in the rest of the chain. If this was
2618 an EXPR_LIST, this might result in allocating more memory than is
2619 strictly needed, but it simplifies the code. */
2620 if (XEXP (x, 1))
2622 new = eliminate_regs (XEXP (x, 1), mem_mode, NULL_RTX);
2623 if (new != XEXP (x, 1))
2624 return gen_rtx (INSN_LIST, GET_MODE (x), XEXP (x, 0), new);
2626 return x;
2628 case CALL:
2629 case COMPARE:
2630 case MINUS:
2631 case MULT:
2632 case DIV: case UDIV:
2633 case MOD: case UMOD:
2634 case AND: case IOR: case XOR:
2635 case LSHIFT: case ASHIFT: case ROTATE:
2636 case ASHIFTRT: case LSHIFTRT: case ROTATERT:
2637 case NE: case EQ:
2638 case GE: case GT: case GEU: case GTU:
2639 case LE: case LT: case LEU: case LTU:
2641 rtx new0 = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2642 rtx new1
2643 = XEXP (x, 1) ? eliminate_regs (XEXP (x, 1), mem_mode, NULL_RTX) : 0;
2645 if (new0 != XEXP (x, 0) || new1 != XEXP (x, 1))
2646 return gen_rtx (code, GET_MODE (x), new0, new1);
2648 return x;
2650 case PRE_INC:
2651 case POST_INC:
2652 case PRE_DEC:
2653 case POST_DEC:
2654 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2655 if (ep->to_rtx == XEXP (x, 0))
2657 if (code == PRE_DEC || code == POST_DEC)
2658 ep->offset += GET_MODE_SIZE (mem_mode);
2659 else
2660 ep->offset -= GET_MODE_SIZE (mem_mode);
2663 /* Fall through to generic unary operation case. */
2664 case USE:
2665 case STRICT_LOW_PART:
2666 case NEG: case NOT:
2667 case SIGN_EXTEND: case ZERO_EXTEND:
2668 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2669 case FLOAT: case FIX:
2670 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2671 case ABS:
2672 case SQRT:
2673 case FFS:
2674 new = eliminate_regs (XEXP (x, 0), mem_mode, NULL_RTX);
2675 if (new != XEXP (x, 0))
2676 return gen_rtx (code, GET_MODE (x), new);
2677 return x;
2679 case SUBREG:
2680 /* Similar to above processing, but preserve SUBREG_WORD.
2681 Convert (subreg (mem)) to (mem) if not paradoxical.
2682 Also, if we have a non-paradoxical (subreg (pseudo)) and the
2683 pseudo didn't get a hard reg, we must replace this with the
2684 eliminated version of the memory location because push_reloads
2685 may do the replacement in certain circumstances. */
2686 if (GET_CODE (SUBREG_REG (x)) == REG
2687 && (GET_MODE_SIZE (GET_MODE (x))
2688 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2689 && reg_equiv_memory_loc != 0
2690 && reg_equiv_memory_loc[REGNO (SUBREG_REG (x))] != 0)
2692 new = eliminate_regs (reg_equiv_memory_loc[REGNO (SUBREG_REG (x))],
2693 mem_mode, NULL_RTX);
2695 /* If we didn't change anything, we must retain the pseudo. */
2696 if (new == reg_equiv_memory_loc[REGNO (SUBREG_REG (x))])
2697 new = XEXP (x, 0);
2698 else
2699 /* Otherwise, ensure NEW isn't shared in case we have to reload
2700 it. */
2701 new = copy_rtx (new);
2703 else
2704 new = eliminate_regs (SUBREG_REG (x), mem_mode, NULL_RTX);
2706 if (new != XEXP (x, 0))
2708 if (GET_CODE (new) == MEM
2709 && (GET_MODE_SIZE (GET_MODE (x))
2710 <= GET_MODE_SIZE (GET_MODE (new))))
2712 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2713 enum machine_mode mode = GET_MODE (x);
2715 #if BYTES_BIG_ENDIAN
2716 offset += (MIN (UNITS_PER_WORD,
2717 GET_MODE_SIZE (GET_MODE (new)))
2718 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2719 #endif
2721 PUT_MODE (new, mode);
2722 XEXP (new, 0) = plus_constant (XEXP (new, 0), offset);
2723 return new;
2725 else
2726 return gen_rtx (SUBREG, GET_MODE (x), new, SUBREG_WORD (x));
2729 return x;
2731 case CLOBBER:
2732 /* If clobbering a register that is the replacement register for an
2733 elimination we still think can be performed, note that it cannot
2734 be performed. Otherwise, we need not be concerned about it. */
2735 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2736 if (ep->to_rtx == XEXP (x, 0))
2737 ep->can_eliminate = 0;
2739 return x;
2741 case ASM_OPERANDS:
2743 rtx *temp_vec;
2744 /* Properly handle sharing input and constraint vectors. */
2745 if (ASM_OPERANDS_INPUT_VEC (x) != old_asm_operands_vec)
2747 /* When we come to a new vector not seen before,
2748 scan all its elements; keep the old vector if none
2749 of them changes; otherwise, make a copy. */
2750 old_asm_operands_vec = ASM_OPERANDS_INPUT_VEC (x);
2751 temp_vec = (rtx *) alloca (XVECLEN (x, 3) * sizeof (rtx));
2752 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2753 temp_vec[i] = eliminate_regs (ASM_OPERANDS_INPUT (x, i),
2754 mem_mode, NULL_RTX);
2756 for (i = 0; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
2757 if (temp_vec[i] != ASM_OPERANDS_INPUT (x, i))
2758 break;
2760 if (i == ASM_OPERANDS_INPUT_LENGTH (x))
2761 new_asm_operands_vec = old_asm_operands_vec;
2762 else
2763 new_asm_operands_vec
2764 = gen_rtvec_v (ASM_OPERANDS_INPUT_LENGTH (x), temp_vec);
2767 /* If we had to copy the vector, copy the entire ASM_OPERANDS. */
2768 if (new_asm_operands_vec == old_asm_operands_vec)
2769 return x;
2771 new = gen_rtx (ASM_OPERANDS, VOIDmode, ASM_OPERANDS_TEMPLATE (x),
2772 ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
2773 ASM_OPERANDS_OUTPUT_IDX (x), new_asm_operands_vec,
2774 ASM_OPERANDS_INPUT_CONSTRAINT_VEC (x),
2775 ASM_OPERANDS_SOURCE_FILE (x),
2776 ASM_OPERANDS_SOURCE_LINE (x));
2777 new->volatil = x->volatil;
2778 return new;
2781 case SET:
2782 /* Check for setting a register that we know about. */
2783 if (GET_CODE (SET_DEST (x)) == REG)
2785 /* See if this is setting the replacement register for an
2786 elimination.
2788 If DEST is the frame pointer, we do nothing because we assume that
2789 all assignments to the frame pointer are for non-local gotos and
2790 are being done at a time when they are valid and do not disturb
2791 anything else. Some machines want to eliminate a fake argument
2792 pointer with either the frame or stack pointer. Assignments to
2793 the frame pointer must not prevent this elimination. */
2795 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2796 ep++)
2797 if (ep->to_rtx == SET_DEST (x)
2798 && SET_DEST (x) != frame_pointer_rtx)
2800 /* If it is being incremented, adjust the offset. Otherwise,
2801 this elimination can't be done. */
2802 rtx src = SET_SRC (x);
2804 if (GET_CODE (src) == PLUS
2805 && XEXP (src, 0) == SET_DEST (x)
2806 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2807 ep->offset -= INTVAL (XEXP (src, 1));
2808 else
2809 ep->can_eliminate = 0;
2812 /* Now check to see we are assigning to a register that can be
2813 eliminated. If so, it must be as part of a PARALLEL, since we
2814 will not have been called if this is a single SET. So indicate
2815 that we can no longer eliminate this reg. */
2816 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2817 ep++)
2818 if (ep->from_rtx == SET_DEST (x) && ep->can_eliminate)
2819 ep->can_eliminate = 0;
2822 /* Now avoid the loop below in this common case. */
2824 rtx new0 = eliminate_regs (SET_DEST (x), 0, NULL_RTX);
2825 rtx new1 = eliminate_regs (SET_SRC (x), 0, NULL_RTX);
2827 /* If SET_DEST changed from a REG to a MEM and INSN is non-zero,
2828 write a CLOBBER insn. */
2829 if (GET_CODE (SET_DEST (x)) == REG && GET_CODE (new0) == MEM
2830 && insn != 0)
2831 emit_insn_after (gen_rtx (CLOBBER, VOIDmode, SET_DEST (x)), insn);
2833 if (new0 != SET_DEST (x) || new1 != SET_SRC (x))
2834 return gen_rtx (SET, VOIDmode, new0, new1);
2837 return x;
2839 case MEM:
2840 /* Our only special processing is to pass the mode of the MEM to our
2841 recursive call and copy the flags. While we are here, handle this
2842 case more efficiently. */
2843 new = eliminate_regs (XEXP (x, 0), GET_MODE (x), NULL_RTX);
2844 if (new != XEXP (x, 0))
2846 new = gen_rtx (MEM, GET_MODE (x), new);
2847 new->volatil = x->volatil;
2848 new->unchanging = x->unchanging;
2849 new->in_struct = x->in_struct;
2850 return new;
2852 else
2853 return x;
2856 /* Process each of our operands recursively. If any have changed, make a
2857 copy of the rtx. */
2858 fmt = GET_RTX_FORMAT (code);
2859 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
2861 if (*fmt == 'e')
2863 new = eliminate_regs (XEXP (x, i), mem_mode, NULL_RTX);
2864 if (new != XEXP (x, i) && ! copied)
2866 rtx new_x = rtx_alloc (code);
2867 bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2868 + (sizeof (new_x->fld[0])
2869 * GET_RTX_LENGTH (code))));
2870 x = new_x;
2871 copied = 1;
2873 XEXP (x, i) = new;
2875 else if (*fmt == 'E')
2877 int copied_vec = 0;
2878 for (j = 0; j < XVECLEN (x, i); j++)
2880 new = eliminate_regs (XVECEXP (x, i, j), mem_mode, insn);
2881 if (new != XVECEXP (x, i, j) && ! copied_vec)
2883 rtvec new_v = gen_rtvec_v (XVECLEN (x, i),
2884 &XVECEXP (x, i, 0));
2885 if (! copied)
2887 rtx new_x = rtx_alloc (code);
2888 bcopy (x, new_x, (sizeof (*new_x) - sizeof (new_x->fld)
2889 + (sizeof (new_x->fld[0])
2890 * GET_RTX_LENGTH (code))));
2891 x = new_x;
2892 copied = 1;
2894 XVEC (x, i) = new_v;
2895 copied_vec = 1;
2897 XVECEXP (x, i, j) = new;
2902 return x;
2905 /* Scan INSN and eliminate all eliminable registers in it.
2907 If REPLACE is nonzero, do the replacement destructively. Also
2908 delete the insn as dead it if it is setting an eliminable register.
2910 If REPLACE is zero, do all our allocations in reload_obstack.
2912 If no eliminations were done and this insn doesn't require any elimination
2913 processing (these are not identical conditions: it might be updating sp,
2914 but not referencing fp; this needs to be seen during reload_as_needed so
2915 that the offset between fp and sp can be taken into consideration), zero
2916 is returned. Otherwise, 1 is returned. */
2918 static int
2919 eliminate_regs_in_insn (insn, replace)
2920 rtx insn;
2921 int replace;
2923 rtx old_body = PATTERN (insn);
2924 rtx new_body;
2925 int val = 0;
2926 struct elim_table *ep;
2928 if (! replace)
2929 push_obstacks (&reload_obstack, &reload_obstack);
2931 if (GET_CODE (old_body) == SET && GET_CODE (SET_DEST (old_body)) == REG
2932 && REGNO (SET_DEST (old_body)) < FIRST_PSEUDO_REGISTER)
2934 /* Check for setting an eliminable register. */
2935 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
2936 if (ep->from_rtx == SET_DEST (old_body) && ep->can_eliminate)
2938 /* In this case this insn isn't serving a useful purpose. We
2939 will delete it in reload_as_needed once we know that this
2940 elimination is, in fact, being done.
2942 If REPLACE isn't set, we can't delete this insn, but neededn't
2943 process it since it won't be used unless something changes. */
2944 if (replace)
2945 delete_dead_insn (insn);
2946 val = 1;
2947 goto done;
2950 /* Check for (set (reg) (plus (reg from) (offset))) where the offset
2951 in the insn is the negative of the offset in FROM. Substitute
2952 (set (reg) (reg to)) for the insn and change its code.
2954 We have to do this here, rather than in eliminate_regs, do that we can
2955 change the insn code. */
2957 if (GET_CODE (SET_SRC (old_body)) == PLUS
2958 && GET_CODE (XEXP (SET_SRC (old_body), 0)) == REG
2959 && GET_CODE (XEXP (SET_SRC (old_body), 1)) == CONST_INT)
2960 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS];
2961 ep++)
2962 if (ep->from_rtx == XEXP (SET_SRC (old_body), 0)
2963 && ep->can_eliminate
2964 && ep->offset == - INTVAL (XEXP (SET_SRC (old_body), 1)))
2966 PATTERN (insn) = gen_rtx (SET, VOIDmode,
2967 SET_DEST (old_body), ep->to_rtx);
2968 INSN_CODE (insn) = -1;
2969 val = 1;
2970 goto done;
2974 old_asm_operands_vec = 0;
2976 /* Replace the body of this insn with a substituted form. If we changed
2977 something, return non-zero. If this is the final call for this
2978 insn (REPLACE is non-zero), do the elimination in REG_NOTES as well.
2980 If we are replacing a body that was a (set X (plus Y Z)), try to
2981 re-recognize the insn. We do this in case we had a simple addition
2982 but now can do this as a load-address. This saves an insn in this
2983 common case. */
2985 new_body = eliminate_regs (old_body, 0, replace ? insn : NULL_RTX);
2986 if (new_body != old_body)
2988 /* If we had a move insn but now we don't, rerecognize it. */
2989 if ((GET_CODE (old_body) == SET && GET_CODE (SET_SRC (old_body)) == REG
2990 && (GET_CODE (new_body) != SET
2991 || GET_CODE (SET_SRC (new_body)) != REG))
2992 /* If this was an add insn before, rerecognize. */
2994 (GET_CODE (old_body) == SET
2995 && GET_CODE (SET_SRC (old_body)) == PLUS))
2997 if (! validate_change (insn, &PATTERN (insn), new_body, 0))
2998 /* If recognition fails, store the new body anyway.
2999 It's normal to have recognition failures here
3000 due to bizarre memory addresses; reloading will fix them. */
3001 PATTERN (insn) = new_body;
3003 else
3004 PATTERN (insn) = new_body;
3006 if (replace && REG_NOTES (insn))
3007 REG_NOTES (insn) = eliminate_regs (REG_NOTES (insn), 0, NULL_RTX);
3008 val = 1;
3011 /* Loop through all elimination pairs. See if any have changed and
3012 recalculate the number not at initial offset.
3014 Compute the maximum offset (minimum offset if the stack does not
3015 grow downward) for each elimination pair.
3017 We also detect a cases where register elimination cannot be done,
3018 namely, if a register would be both changed and referenced outside a MEM
3019 in the resulting insn since such an insn is often undefined and, even if
3020 not, we cannot know what meaning will be given to it. Note that it is
3021 valid to have a register used in an address in an insn that changes it
3022 (presumably with a pre- or post-increment or decrement).
3024 If anything changes, return nonzero. */
3026 num_not_at_initial_offset = 0;
3027 for (ep = reg_eliminate; ep < &reg_eliminate[NUM_ELIMINABLE_REGS]; ep++)
3029 if (ep->previous_offset != ep->offset && ep->ref_outside_mem)
3030 ep->can_eliminate = 0;
3032 ep->ref_outside_mem = 0;
3034 if (ep->previous_offset != ep->offset)
3035 val = 1;
3037 ep->previous_offset = ep->offset;
3038 if (ep->can_eliminate && ep->offset != ep->initial_offset)
3039 num_not_at_initial_offset++;
3041 #ifdef STACK_GROWS_DOWNWARD
3042 ep->max_offset = MAX (ep->max_offset, ep->offset);
3043 #else
3044 ep->max_offset = MIN (ep->max_offset, ep->offset);
3045 #endif
3048 done:
3049 if (! replace)
3050 pop_obstacks ();
3052 return val;
3055 /* Given X, a SET or CLOBBER of DEST, if DEST is the target of a register
3056 replacement we currently believe is valid, mark it as not eliminable if X
3057 modifies DEST in any way other than by adding a constant integer to it.
3059 If DEST is the frame pointer, we do nothing because we assume that
3060 all assignments to the frame pointer are nonlocal gotos and are being done
3061 at a time when they are valid and do not disturb anything else.
3062 Some machines want to eliminate a fake argument pointer with either the
3063 frame or stack pointer. Assignments to the frame pointer must not prevent
3064 this elimination.
3066 Called via note_stores from reload before starting its passes to scan
3067 the insns of the function. */
3069 static void
3070 mark_not_eliminable (dest, x)
3071 rtx dest;
3072 rtx x;
3074 register int i;
3076 /* A SUBREG of a hard register here is just changing its mode. We should
3077 not see a SUBREG of an eliminable hard register, but check just in
3078 case. */
3079 if (GET_CODE (dest) == SUBREG)
3080 dest = SUBREG_REG (dest);
3082 if (dest == frame_pointer_rtx)
3083 return;
3085 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3086 if (reg_eliminate[i].can_eliminate && dest == reg_eliminate[i].to_rtx
3087 && (GET_CODE (x) != SET
3088 || GET_CODE (SET_SRC (x)) != PLUS
3089 || XEXP (SET_SRC (x), 0) != dest
3090 || GET_CODE (XEXP (SET_SRC (x), 1)) != CONST_INT))
3092 reg_eliminate[i].can_eliminate_previous
3093 = reg_eliminate[i].can_eliminate = 0;
3094 num_eliminable--;
3098 /* Kick all pseudos out of hard register REGNO.
3099 If GLOBAL is nonzero, try to find someplace else to put them.
3100 If DUMPFILE is nonzero, log actions taken on that file.
3102 If CANT_ELIMINATE is nonzero, it means that we are doing this spill
3103 because we found we can't eliminate some register. In the case, no pseudos
3104 are allowed to be in the register, even if they are only in a block that
3105 doesn't require spill registers, unlike the case when we are spilling this
3106 hard reg to produce another spill register.
3108 Return nonzero if any pseudos needed to be kicked out. */
3110 static int
3111 spill_hard_reg (regno, global, dumpfile, cant_eliminate)
3112 register int regno;
3113 int global;
3114 FILE *dumpfile;
3115 int cant_eliminate;
3117 int something_changed = 0;
3118 register int i;
3120 SET_HARD_REG_BIT (forbidden_regs, regno);
3122 /* Spill every pseudo reg that was allocated to this reg
3123 or to something that overlaps this reg. */
3125 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3126 if (reg_renumber[i] >= 0
3127 && reg_renumber[i] <= regno
3128 && (reg_renumber[i]
3129 + HARD_REGNO_NREGS (reg_renumber[i],
3130 PSEUDO_REGNO_MODE (i))
3131 > regno))
3133 enum reg_class class = REGNO_REG_CLASS (regno);
3135 /* If this register belongs solely to a basic block which needed no
3136 spilling of any class that this register is contained in,
3137 leave it be, unless we are spilling this register because
3138 it was a hard register that can't be eliminated. */
3140 if (! cant_eliminate
3141 && basic_block_needs[0]
3142 && reg_basic_block[i] >= 0
3143 && basic_block_needs[(int) class][reg_basic_block[i]] == 0)
3145 enum reg_class *p;
3147 for (p = reg_class_superclasses[(int) class];
3148 *p != LIM_REG_CLASSES; p++)
3149 if (basic_block_needs[(int) *p][reg_basic_block[i]] > 0)
3150 break;
3152 if (*p == LIM_REG_CLASSES)
3153 continue;
3156 /* Mark it as no longer having a hard register home. */
3157 reg_renumber[i] = -1;
3158 /* We will need to scan everything again. */
3159 something_changed = 1;
3160 if (global)
3161 retry_global_alloc (i, forbidden_regs);
3163 alter_reg (i, regno);
3164 if (dumpfile)
3166 if (reg_renumber[i] == -1)
3167 fprintf (dumpfile, " Register %d now on stack.\n\n", i);
3168 else
3169 fprintf (dumpfile, " Register %d now in %d.\n\n",
3170 i, reg_renumber[i]);
3174 return something_changed;
3177 /* Find all paradoxical subregs within X and update reg_max_ref_width. */
3179 static void
3180 scan_paradoxical_subregs (x)
3181 register rtx x;
3183 register int i;
3184 register char *fmt;
3185 register enum rtx_code code = GET_CODE (x);
3187 switch (code)
3189 case CONST_INT:
3190 case CONST:
3191 case SYMBOL_REF:
3192 case LABEL_REF:
3193 case CONST_DOUBLE:
3194 case CC0:
3195 case PC:
3196 case REG:
3197 case USE:
3198 case CLOBBER:
3199 return;
3201 case SUBREG:
3202 if (GET_CODE (SUBREG_REG (x)) == REG
3203 && GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
3204 reg_max_ref_width[REGNO (SUBREG_REG (x))]
3205 = GET_MODE_SIZE (GET_MODE (x));
3206 return;
3209 fmt = GET_RTX_FORMAT (code);
3210 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3212 if (fmt[i] == 'e')
3213 scan_paradoxical_subregs (XEXP (x, i));
3214 else if (fmt[i] == 'E')
3216 register int j;
3217 for (j = XVECLEN (x, i) - 1; j >=0; j--)
3218 scan_paradoxical_subregs (XVECEXP (x, i, j));
3223 struct hard_reg_n_uses { int regno; int uses; };
3225 static int
3226 hard_reg_use_compare (p1, p2)
3227 struct hard_reg_n_uses *p1, *p2;
3229 int tem = p1->uses - p2->uses;
3230 if (tem != 0) return tem;
3231 /* If regs are equally good, sort by regno,
3232 so that the results of qsort leave nothing to chance. */
3233 return p1->regno - p2->regno;
3236 /* Choose the order to consider regs for use as reload registers
3237 based on how much trouble would be caused by spilling one.
3238 Store them in order of decreasing preference in potential_reload_regs. */
3240 static void
3241 order_regs_for_reload ()
3243 register int i;
3244 register int o = 0;
3245 int large = 0;
3247 struct hard_reg_n_uses hard_reg_n_uses[FIRST_PSEUDO_REGISTER];
3249 CLEAR_HARD_REG_SET (bad_spill_regs);
3251 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3252 potential_reload_regs[i] = -1;
3254 /* Count number of uses of each hard reg by pseudo regs allocated to it
3255 and then order them by decreasing use. */
3257 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3259 hard_reg_n_uses[i].uses = 0;
3260 hard_reg_n_uses[i].regno = i;
3263 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
3265 int regno = reg_renumber[i];
3266 if (regno >= 0)
3268 int lim = regno + HARD_REGNO_NREGS (regno, PSEUDO_REGNO_MODE (i));
3269 while (regno < lim)
3270 hard_reg_n_uses[regno++].uses += reg_n_refs[i];
3272 large += reg_n_refs[i];
3275 /* Now fixed registers (which cannot safely be used for reloading)
3276 get a very high use count so they will be considered least desirable.
3277 Registers used explicitly in the rtl code are almost as bad. */
3279 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3281 if (fixed_regs[i])
3283 hard_reg_n_uses[i].uses += 2 * large + 2;
3284 SET_HARD_REG_BIT (bad_spill_regs, i);
3286 else if (regs_explicitly_used[i])
3288 hard_reg_n_uses[i].uses += large + 1;
3289 /* ??? We are doing this here because of the potential that
3290 bad code may be generated if a register explicitly used in
3291 an insn was used as a spill register for that insn. But
3292 not using these are spill registers may lose on some machine.
3293 We'll have to see how this works out. */
3294 SET_HARD_REG_BIT (bad_spill_regs, i);
3297 hard_reg_n_uses[FRAME_POINTER_REGNUM].uses += 2 * large + 2;
3298 SET_HARD_REG_BIT (bad_spill_regs, FRAME_POINTER_REGNUM);
3300 #ifdef ELIMINABLE_REGS
3301 /* If registers other than the frame pointer are eliminable, mark them as
3302 poor choices. */
3303 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3305 hard_reg_n_uses[reg_eliminate[i].from].uses += 2 * large + 2;
3306 SET_HARD_REG_BIT (bad_spill_regs, reg_eliminate[i].from);
3308 #endif
3310 /* Prefer registers not so far used, for use in temporary loading.
3311 Among them, if REG_ALLOC_ORDER is defined, use that order.
3312 Otherwise, prefer registers not preserved by calls. */
3314 #ifdef REG_ALLOC_ORDER
3315 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3317 int regno = reg_alloc_order[i];
3319 if (hard_reg_n_uses[regno].uses == 0)
3320 potential_reload_regs[o++] = regno;
3322 #else
3323 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3325 if (hard_reg_n_uses[i].uses == 0 && call_used_regs[i])
3326 potential_reload_regs[o++] = i;
3328 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3330 if (hard_reg_n_uses[i].uses == 0 && ! call_used_regs[i])
3331 potential_reload_regs[o++] = i;
3333 #endif
3335 qsort (hard_reg_n_uses, FIRST_PSEUDO_REGISTER,
3336 sizeof hard_reg_n_uses[0], hard_reg_use_compare);
3338 /* Now add the regs that are already used,
3339 preferring those used less often. The fixed and otherwise forbidden
3340 registers will be at the end of this list. */
3342 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3343 if (hard_reg_n_uses[i].uses != 0)
3344 potential_reload_regs[o++] = hard_reg_n_uses[i].regno;
3347 /* Reload pseudo-registers into hard regs around each insn as needed.
3348 Additional register load insns are output before the insn that needs it
3349 and perhaps store insns after insns that modify the reloaded pseudo reg.
3351 reg_last_reload_reg and reg_reloaded_contents keep track of
3352 which pseudo-registers are already available in reload registers.
3353 We update these for the reloads that we perform,
3354 as the insns are scanned. */
3356 static void
3357 reload_as_needed (first, live_known)
3358 rtx first;
3359 int live_known;
3361 register rtx insn;
3362 register int i;
3363 int this_block = 0;
3364 rtx x;
3365 rtx after_call = 0;
3367 bzero (spill_reg_rtx, sizeof spill_reg_rtx);
3368 reg_last_reload_reg = (rtx *) alloca (max_regno * sizeof (rtx));
3369 bzero (reg_last_reload_reg, max_regno * sizeof (rtx));
3370 reg_has_output_reload = (char *) alloca (max_regno);
3371 for (i = 0; i < n_spills; i++)
3373 reg_reloaded_contents[i] = -1;
3374 reg_reloaded_insn[i] = 0;
3377 /* Reset all offsets on eliminable registers to their initial values. */
3378 #ifdef ELIMINABLE_REGS
3379 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3381 INITIAL_ELIMINATION_OFFSET (reg_eliminate[i].from, reg_eliminate[i].to,
3382 reg_eliminate[i].initial_offset);
3383 reg_eliminate[i].previous_offset
3384 = reg_eliminate[i].offset = reg_eliminate[i].initial_offset;
3386 #else
3387 INITIAL_FRAME_POINTER_OFFSET (reg_eliminate[0].initial_offset);
3388 reg_eliminate[0].previous_offset
3389 = reg_eliminate[0].offset = reg_eliminate[0].initial_offset;
3390 #endif
3392 num_not_at_initial_offset = 0;
3394 for (insn = first; insn;)
3396 register rtx next = NEXT_INSN (insn);
3398 /* Notice when we move to a new basic block. */
3399 if (live_known && this_block + 1 < n_basic_blocks
3400 && insn == basic_block_head[this_block+1])
3401 ++this_block;
3403 /* If we pass a label, copy the offsets from the label information
3404 into the current offsets of each elimination. */
3405 if (GET_CODE (insn) == CODE_LABEL)
3407 num_not_at_initial_offset = 0;
3408 for (i = 0; i < NUM_ELIMINABLE_REGS; i++)
3410 reg_eliminate[i].offset = reg_eliminate[i].previous_offset
3411 = offsets_at[CODE_LABEL_NUMBER (insn)][i];
3412 if (reg_eliminate[i].can_eliminate
3413 && (reg_eliminate[i].offset
3414 != reg_eliminate[i].initial_offset))
3415 num_not_at_initial_offset++;
3419 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3421 rtx avoid_return_reg = 0;
3423 #ifdef SMALL_REGISTER_CLASSES
3424 /* Set avoid_return_reg if this is an insn
3425 that might use the value of a function call. */
3426 if (GET_CODE (insn) == CALL_INSN)
3428 if (GET_CODE (PATTERN (insn)) == SET)
3429 after_call = SET_DEST (PATTERN (insn));
3430 else if (GET_CODE (PATTERN (insn)) == PARALLEL
3431 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
3432 after_call = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
3433 else
3434 after_call = 0;
3436 else if (after_call != 0
3437 && !(GET_CODE (PATTERN (insn)) == SET
3438 && SET_DEST (PATTERN (insn)) == stack_pointer_rtx))
3440 if (reg_mentioned_p (after_call, PATTERN (insn)))
3441 avoid_return_reg = after_call;
3442 after_call = 0;
3444 #endif /* SMALL_REGISTER_CLASSES */
3446 /* If this is a USE and CLOBBER of a MEM, ensure that any
3447 references to eliminable registers have been removed. */
3449 if ((GET_CODE (PATTERN (insn)) == USE
3450 || GET_CODE (PATTERN (insn)) == CLOBBER)
3451 && GET_CODE (XEXP (PATTERN (insn), 0)) == MEM)
3452 XEXP (XEXP (PATTERN (insn), 0), 0)
3453 = eliminate_regs (XEXP (XEXP (PATTERN (insn), 0), 0),
3454 GET_MODE (XEXP (PATTERN (insn), 0)), NULL_RTX);
3456 /* If we need to do register elimination processing, do so.
3457 This might delete the insn, in which case we are done. */
3458 if (num_eliminable && GET_MODE (insn) == QImode)
3460 eliminate_regs_in_insn (insn, 1);
3461 if (GET_CODE (insn) == NOTE)
3463 insn = next;
3464 continue;
3468 if (GET_MODE (insn) == VOIDmode)
3469 n_reloads = 0;
3470 /* First find the pseudo regs that must be reloaded for this insn.
3471 This info is returned in the tables reload_... (see reload.h).
3472 Also modify the body of INSN by substituting RELOAD
3473 rtx's for those pseudo regs. */
3474 else
3476 bzero (reg_has_output_reload, max_regno);
3477 CLEAR_HARD_REG_SET (reg_is_output_reload);
3479 find_reloads (insn, 1, spill_indirect_levels, live_known,
3480 spill_reg_order);
3483 if (n_reloads > 0)
3485 rtx prev = PREV_INSN (insn), next = NEXT_INSN (insn);
3486 rtx p;
3487 int class;
3489 /* If this block has not had spilling done for a
3490 particular class, deactivate any optional reloads
3491 of that class lest they try to use a spill-reg which isn't
3492 available here. If we have any non-optionals that need a
3493 spill reg, abort. */
3495 for (class = 0; class < N_REG_CLASSES; class++)
3496 if (basic_block_needs[class] != 0
3497 && basic_block_needs[class][this_block] == 0)
3498 for (i = 0; i < n_reloads; i++)
3499 if (class == (int) reload_reg_class[i])
3501 if (reload_optional[i])
3503 reload_in[i] = reload_out[i] = 0;
3504 reload_secondary_p[i] = 0;
3506 else if (reload_reg_rtx[i] == 0
3507 && (reload_in[i] != 0 || reload_out[i] != 0
3508 || reload_secondary_p[i] != 0))
3509 abort ();
3512 /* Now compute which reload regs to reload them into. Perhaps
3513 reusing reload regs from previous insns, or else output
3514 load insns to reload them. Maybe output store insns too.
3515 Record the choices of reload reg in reload_reg_rtx. */
3516 choose_reload_regs (insn, avoid_return_reg);
3518 /* Generate the insns to reload operands into or out of
3519 their reload regs. */
3520 emit_reload_insns (insn);
3522 /* Substitute the chosen reload regs from reload_reg_rtx
3523 into the insn's body (or perhaps into the bodies of other
3524 load and store insn that we just made for reloading
3525 and that we moved the structure into). */
3526 subst_reloads ();
3528 /* If this was an ASM, make sure that all the reload insns
3529 we have generated are valid. If not, give an error
3530 and delete them. */
3532 if (asm_noperands (PATTERN (insn)) >= 0)
3533 for (p = NEXT_INSN (prev); p != next; p = NEXT_INSN (p))
3534 if (p != insn && GET_RTX_CLASS (GET_CODE (p)) == 'i'
3535 && (recog_memoized (p) < 0
3536 || (insn_extract (p),
3537 ! constrain_operands (INSN_CODE (p), 1))))
3539 error_for_asm (insn,
3540 "`asm' operand requires impossible reload");
3541 PUT_CODE (p, NOTE);
3542 NOTE_SOURCE_FILE (p) = 0;
3543 NOTE_LINE_NUMBER (p) = NOTE_INSN_DELETED;
3546 /* Any previously reloaded spilled pseudo reg, stored in this insn,
3547 is no longer validly lying around to save a future reload.
3548 Note that this does not detect pseudos that were reloaded
3549 for this insn in order to be stored in
3550 (obeying register constraints). That is correct; such reload
3551 registers ARE still valid. */
3552 note_stores (PATTERN (insn), forget_old_reloads_1);
3554 /* There may have been CLOBBER insns placed after INSN. So scan
3555 between INSN and NEXT and use them to forget old reloads. */
3556 for (x = NEXT_INSN (insn); x != next; x = NEXT_INSN (x))
3557 if (GET_CODE (x) == INSN && GET_CODE (PATTERN (x)) == CLOBBER)
3558 note_stores (PATTERN (x), forget_old_reloads_1);
3560 #ifdef AUTO_INC_DEC
3561 /* Likewise for regs altered by auto-increment in this insn.
3562 But note that the reg-notes are not changed by reloading:
3563 they still contain the pseudo-regs, not the spill regs. */
3564 for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
3565 if (REG_NOTE_KIND (x) == REG_INC)
3567 /* See if this pseudo reg was reloaded in this insn.
3568 If so, its last-reload info is still valid
3569 because it is based on this insn's reload. */
3570 for (i = 0; i < n_reloads; i++)
3571 if (reload_out[i] == XEXP (x, 0))
3572 break;
3574 if (i != n_reloads)
3575 forget_old_reloads_1 (XEXP (x, 0));
3577 #endif
3579 /* A reload reg's contents are unknown after a label. */
3580 if (GET_CODE (insn) == CODE_LABEL)
3581 for (i = 0; i < n_spills; i++)
3583 reg_reloaded_contents[i] = -1;
3584 reg_reloaded_insn[i] = 0;
3587 /* Don't assume a reload reg is still good after a call insn
3588 if it is a call-used reg. */
3589 if (GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == CALL_INSN)
3590 for (i = 0; i < n_spills; i++)
3591 if (call_used_regs[spill_regs[i]])
3593 reg_reloaded_contents[i] = -1;
3594 reg_reloaded_insn[i] = 0;
3597 /* In case registers overlap, allow certain insns to invalidate
3598 particular hard registers. */
3600 #ifdef INSN_CLOBBERS_REGNO_P
3601 for (i = 0 ; i < n_spills ; i++)
3602 if (INSN_CLOBBERS_REGNO_P (insn, spill_regs[i]))
3604 reg_reloaded_contents[i] = -1;
3605 reg_reloaded_insn[i] = 0;
3607 #endif
3609 insn = next;
3611 #ifdef USE_C_ALLOCA
3612 alloca (0);
3613 #endif
3617 /* Discard all record of any value reloaded from X,
3618 or reloaded in X from someplace else;
3619 unless X is an output reload reg of the current insn.
3621 X may be a hard reg (the reload reg)
3622 or it may be a pseudo reg that was reloaded from. */
3624 static void
3625 forget_old_reloads_1 (x)
3626 rtx x;
3628 register int regno;
3629 int nr;
3630 int offset = 0;
3632 /* note_stores does give us subregs of hard regs. */
3633 while (GET_CODE (x) == SUBREG)
3635 offset += SUBREG_WORD (x);
3636 x = SUBREG_REG (x);
3639 if (GET_CODE (x) != REG)
3640 return;
3642 regno = REGNO (x) + offset;
3644 if (regno >= FIRST_PSEUDO_REGISTER)
3645 nr = 1;
3646 else
3648 int i;
3649 nr = HARD_REGNO_NREGS (regno, GET_MODE (x));
3650 /* Storing into a spilled-reg invalidates its contents.
3651 This can happen if a block-local pseudo is allocated to that reg
3652 and it wasn't spilled because this block's total need is 0.
3653 Then some insn might have an optional reload and use this reg. */
3654 for (i = 0; i < nr; i++)
3655 if (spill_reg_order[regno + i] >= 0
3656 /* But don't do this if the reg actually serves as an output
3657 reload reg in the current instruction. */
3658 && (n_reloads == 0
3659 || ! TEST_HARD_REG_BIT (reg_is_output_reload, regno + i)))
3661 reg_reloaded_contents[spill_reg_order[regno + i]] = -1;
3662 reg_reloaded_insn[spill_reg_order[regno + i]] = 0;
3666 /* Since value of X has changed,
3667 forget any value previously copied from it. */
3669 while (nr-- > 0)
3670 /* But don't forget a copy if this is the output reload
3671 that establishes the copy's validity. */
3672 if (n_reloads == 0 || reg_has_output_reload[regno + nr] == 0)
3673 reg_last_reload_reg[regno + nr] = 0;
3676 /* For each reload, the mode of the reload register. */
3677 static enum machine_mode reload_mode[MAX_RELOADS];
3679 /* For each reload, the largest number of registers it will require. */
3680 static int reload_nregs[MAX_RELOADS];
3682 /* Comparison function for qsort to decide which of two reloads
3683 should be handled first. *P1 and *P2 are the reload numbers. */
3685 static int
3686 reload_reg_class_lower (p1, p2)
3687 short *p1, *p2;
3689 register int r1 = *p1, r2 = *p2;
3690 register int t;
3692 /* Consider required reloads before optional ones. */
3693 t = reload_optional[r1] - reload_optional[r2];
3694 if (t != 0)
3695 return t;
3697 /* Count all solitary classes before non-solitary ones. */
3698 t = ((reg_class_size[(int) reload_reg_class[r2]] == 1)
3699 - (reg_class_size[(int) reload_reg_class[r1]] == 1));
3700 if (t != 0)
3701 return t;
3703 /* Aside from solitaires, consider all multi-reg groups first. */
3704 t = reload_nregs[r2] - reload_nregs[r1];
3705 if (t != 0)
3706 return t;
3708 /* Consider reloads in order of increasing reg-class number. */
3709 t = (int) reload_reg_class[r1] - (int) reload_reg_class[r2];
3710 if (t != 0)
3711 return t;
3713 /* If reloads are equally urgent, sort by reload number,
3714 so that the results of qsort leave nothing to chance. */
3715 return r1 - r2;
3718 /* The following HARD_REG_SETs indicate when each hard register is
3719 used for a reload of various parts of the current insn. */
3721 /* If reg is in use as a reload reg for a RELOAD_OTHER reload. */
3722 static HARD_REG_SET reload_reg_used;
3723 /* If reg is in use for a RELOAD_FOR_INPUT_RELOAD_ADDRESS reload. */
3724 static HARD_REG_SET reload_reg_used_in_input_addr;
3725 /* If reg is in use for a RELOAD_FOR_OUTPUT_RELOAD_ADDRESS reload. */
3726 static HARD_REG_SET reload_reg_used_in_output_addr;
3727 /* If reg is in use for a RELOAD_FOR_OPERAND_ADDRESS reload. */
3728 static HARD_REG_SET reload_reg_used_in_op_addr;
3729 /* If reg is in use for a RELOAD_FOR_INPUT reload. */
3730 static HARD_REG_SET reload_reg_used_in_input;
3731 /* If reg is in use for a RELOAD_FOR_OUTPUT reload. */
3732 static HARD_REG_SET reload_reg_used_in_output;
3734 /* If reg is in use as a reload reg for any sort of reload. */
3735 static HARD_REG_SET reload_reg_used_at_all;
3737 /* Mark reg REGNO as in use for a reload of the sort spec'd by WHEN_NEEDED.
3738 MODE is used to indicate how many consecutive regs are actually used. */
3740 static void
3741 mark_reload_reg_in_use (regno, when_needed, mode)
3742 int regno;
3743 enum reload_when_needed when_needed;
3744 enum machine_mode mode;
3746 int nregs = HARD_REGNO_NREGS (regno, mode);
3747 int i;
3749 for (i = regno; i < nregs + regno; i++)
3751 switch (when_needed)
3753 case RELOAD_OTHER:
3754 SET_HARD_REG_BIT (reload_reg_used, i);
3755 break;
3757 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3758 SET_HARD_REG_BIT (reload_reg_used_in_input_addr, i);
3759 break;
3761 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3762 SET_HARD_REG_BIT (reload_reg_used_in_output_addr, i);
3763 break;
3765 case RELOAD_FOR_OPERAND_ADDRESS:
3766 SET_HARD_REG_BIT (reload_reg_used_in_op_addr, i);
3767 break;
3769 case RELOAD_FOR_INPUT:
3770 SET_HARD_REG_BIT (reload_reg_used_in_input, i);
3771 break;
3773 case RELOAD_FOR_OUTPUT:
3774 SET_HARD_REG_BIT (reload_reg_used_in_output, i);
3775 break;
3778 SET_HARD_REG_BIT (reload_reg_used_at_all, i);
3782 /* 1 if reg REGNO is free as a reload reg for a reload of the sort
3783 specified by WHEN_NEEDED. */
3785 static int
3786 reload_reg_free_p (regno, when_needed)
3787 int regno;
3788 enum reload_when_needed when_needed;
3790 /* In use for a RELOAD_OTHER means it's not available for anything. */
3791 if (TEST_HARD_REG_BIT (reload_reg_used, regno))
3792 return 0;
3793 switch (when_needed)
3795 case RELOAD_OTHER:
3796 /* In use for anything means not available for a RELOAD_OTHER. */
3797 return ! TEST_HARD_REG_BIT (reload_reg_used_at_all, regno);
3799 /* The other kinds of use can sometimes share a register. */
3800 case RELOAD_FOR_INPUT:
3801 return (! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3802 && ! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3803 && ! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno));
3804 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3805 return (! TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno)
3806 && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno));
3807 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3808 return (! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3809 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3810 case RELOAD_FOR_OPERAND_ADDRESS:
3811 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3812 && ! TEST_HARD_REG_BIT (reload_reg_used_in_input, regno)
3813 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3814 case RELOAD_FOR_OUTPUT:
3815 return (! TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3816 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno)
3817 && ! TEST_HARD_REG_BIT (reload_reg_used_in_output, regno));
3819 abort ();
3822 /* Return 1 if the value in reload reg REGNO, as used by a reload
3823 needed for the part of the insn specified by WHEN_NEEDED,
3824 is not in use for a reload in any prior part of the insn.
3826 We can assume that the reload reg was already tested for availability
3827 at the time it is needed, and we should not check this again,
3828 in case the reg has already been marked in use. */
3830 static int
3831 reload_reg_free_before_p (regno, when_needed)
3832 int regno;
3833 enum reload_when_needed when_needed;
3835 switch (when_needed)
3837 case RELOAD_OTHER:
3838 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3839 its use starts from the beginning, so nothing can use it earlier. */
3840 return 1;
3842 /* If this use is for part of the insn,
3843 check the reg is not in use for any prior part. */
3844 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3845 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno))
3846 return 0;
3847 case RELOAD_FOR_OUTPUT:
3848 if (TEST_HARD_REG_BIT (reload_reg_used_in_input, regno))
3849 return 0;
3850 case RELOAD_FOR_OPERAND_ADDRESS:
3851 if (TEST_HARD_REG_BIT (reload_reg_used_in_input_addr, regno))
3852 return 0;
3853 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3854 case RELOAD_FOR_INPUT:
3855 return 1;
3857 abort ();
3860 /* Return 1 if the value in reload reg REGNO, as used by a reload
3861 needed for the part of the insn specified by WHEN_NEEDED,
3862 is still available in REGNO at the end of the insn.
3864 We can assume that the reload reg was already tested for availability
3865 at the time it is needed, and we should not check this again,
3866 in case the reg has already been marked in use. */
3868 static int
3869 reload_reg_reaches_end_p (regno, when_needed)
3870 int regno;
3871 enum reload_when_needed when_needed;
3873 switch (when_needed)
3875 case RELOAD_OTHER:
3876 /* Since a RELOAD_OTHER reload claims the reg for the entire insn,
3877 its value must reach the end. */
3878 return 1;
3880 /* If this use is for part of the insn,
3881 its value reaches if no subsequent part uses the same register. */
3882 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
3883 case RELOAD_FOR_INPUT:
3884 if (TEST_HARD_REG_BIT (reload_reg_used_in_op_addr, regno)
3885 || TEST_HARD_REG_BIT (reload_reg_used_in_output, regno))
3886 return 0;
3887 case RELOAD_FOR_OPERAND_ADDRESS:
3888 if (TEST_HARD_REG_BIT (reload_reg_used_in_output_addr, regno))
3889 return 0;
3890 case RELOAD_FOR_OUTPUT:
3891 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
3892 return 1;
3894 abort ();
3897 /* Vector of reload-numbers showing the order in which the reloads should
3898 be processed. */
3899 short reload_order[MAX_RELOADS];
3901 /* Indexed by reload number, 1 if incoming value
3902 inherited from previous insns. */
3903 char reload_inherited[MAX_RELOADS];
3905 /* For an inherited reload, this is the insn the reload was inherited from,
3906 if we know it. Otherwise, this is 0. */
3907 rtx reload_inheritance_insn[MAX_RELOADS];
3909 /* If non-zero, this is a place to get the value of the reload,
3910 rather than using reload_in. */
3911 rtx reload_override_in[MAX_RELOADS];
3913 /* For each reload, the index in spill_regs of the spill register used,
3914 or -1 if we did not need one of the spill registers for this reload. */
3915 int reload_spill_index[MAX_RELOADS];
3917 /* Index of last register assigned as a spill register. We allocate in
3918 a round-robin fashio. */
3920 static last_spill_reg = 0;
3922 /* Find a spill register to use as a reload register for reload R.
3923 LAST_RELOAD is non-zero if this is the last reload for the insn being
3924 processed.
3926 Set reload_reg_rtx[R] to the register allocated.
3928 If NOERROR is nonzero, we return 1 if successful,
3929 or 0 if we couldn't find a spill reg and we didn't change anything. */
3931 static int
3932 allocate_reload_reg (r, insn, last_reload, noerror)
3933 int r;
3934 rtx insn;
3935 int last_reload;
3936 int noerror;
3938 int i;
3939 int pass;
3940 int count;
3941 rtx new;
3942 int regno;
3944 /* If we put this reload ahead, thinking it is a group,
3945 then insist on finding a group. Otherwise we can grab a
3946 reg that some other reload needs.
3947 (That can happen when we have a 68000 DATA_OR_FP_REG
3948 which is a group of data regs or one fp reg.)
3949 We need not be so restrictive if there are no more reloads
3950 for this insn.
3952 ??? Really it would be nicer to have smarter handling
3953 for that kind of reg class, where a problem like this is normal.
3954 Perhaps those classes should be avoided for reloading
3955 by use of more alternatives. */
3957 int force_group = reload_nregs[r] > 1 && ! last_reload;
3959 /* If we want a single register and haven't yet found one,
3960 take any reg in the right class and not in use.
3961 If we want a consecutive group, here is where we look for it.
3963 We use two passes so we can first look for reload regs to
3964 reuse, which are already in use for other reloads in this insn,
3965 and only then use additional registers.
3966 I think that maximizing reuse is needed to make sure we don't
3967 run out of reload regs. Suppose we have three reloads, and
3968 reloads A and B can share regs. These need two regs.
3969 Suppose A and B are given different regs.
3970 That leaves none for C. */
3971 for (pass = 0; pass < 2; pass++)
3973 /* I is the index in spill_regs.
3974 We advance it round-robin between insns to use all spill regs
3975 equally, so that inherited reloads have a chance
3976 of leapfrogging each other. */
3978 for (count = 0, i = last_spill_reg; count < n_spills; count++)
3980 int class = (int) reload_reg_class[r];
3982 i = (i + 1) % n_spills;
3984 if (reload_reg_free_p (spill_regs[i], reload_when_needed[r])
3985 && TEST_HARD_REG_BIT (reg_class_contents[class], spill_regs[i])
3986 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
3987 /* Look first for regs to share, then for unshared. */
3988 && (pass || TEST_HARD_REG_BIT (reload_reg_used_at_all,
3989 spill_regs[i])))
3991 int nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
3992 /* Avoid the problem where spilling a GENERAL_OR_FP_REG
3993 (on 68000) got us two FP regs. If NR is 1,
3994 we would reject both of them. */
3995 if (force_group)
3996 nr = CLASS_MAX_NREGS (reload_reg_class[r], reload_mode[r]);
3997 /* If we need only one reg, we have already won. */
3998 if (nr == 1)
4000 /* But reject a single reg if we demand a group. */
4001 if (force_group)
4002 continue;
4003 break;
4005 /* Otherwise check that as many consecutive regs as we need
4006 are available here.
4007 Also, don't use for a group registers that are
4008 needed for nongroups. */
4009 if (! TEST_HARD_REG_BIT (counted_for_nongroups, spill_regs[i]))
4010 while (nr > 1)
4012 regno = spill_regs[i] + nr - 1;
4013 if (!(TEST_HARD_REG_BIT (reg_class_contents[class], regno)
4014 && spill_reg_order[regno] >= 0
4015 && reload_reg_free_p (regno, reload_when_needed[r])
4016 && ! TEST_HARD_REG_BIT (counted_for_nongroups,
4017 regno)))
4018 break;
4019 nr--;
4021 if (nr == 1)
4022 break;
4026 /* If we found something on pass 1, omit pass 2. */
4027 if (count < n_spills)
4028 break;
4031 /* We should have found a spill register by now. */
4032 if (count == n_spills)
4034 if (noerror)
4035 return 0;
4036 abort ();
4039 last_spill_reg = i;
4041 /* Mark as in use for this insn the reload regs we use for this. */
4042 mark_reload_reg_in_use (spill_regs[i], reload_when_needed[r],
4043 reload_mode[r]);
4045 new = spill_reg_rtx[i];
4047 if (new == 0 || GET_MODE (new) != reload_mode[r])
4048 spill_reg_rtx[i] = new = gen_rtx (REG, reload_mode[r], spill_regs[i]);
4050 reload_reg_rtx[r] = new;
4051 reload_spill_index[r] = i;
4052 regno = true_regnum (new);
4054 /* Detect when the reload reg can't hold the reload mode.
4055 This used to be one `if', but Sequent compiler can't handle that. */
4056 if (HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4058 enum machine_mode test_mode = VOIDmode;
4059 if (reload_in[r])
4060 test_mode = GET_MODE (reload_in[r]);
4061 /* If reload_in[r] has VOIDmode, it means we will load it
4062 in whatever mode the reload reg has: to wit, reload_mode[r].
4063 We have already tested that for validity. */
4064 /* Aside from that, we need to test that the expressions
4065 to reload from or into have modes which are valid for this
4066 reload register. Otherwise the reload insns would be invalid. */
4067 if (! (reload_in[r] != 0 && test_mode != VOIDmode
4068 && ! HARD_REGNO_MODE_OK (regno, test_mode)))
4069 if (! (reload_out[r] != 0
4070 && ! HARD_REGNO_MODE_OK (regno, GET_MODE (reload_out[r]))))
4071 /* The reg is OK. */
4072 return 1;
4075 /* The reg is not OK. */
4076 if (noerror)
4077 return 0;
4079 if (asm_noperands (PATTERN (insn)) < 0)
4080 /* It's the compiler's fault. */
4081 abort ();
4083 /* It's the user's fault; the operand's mode and constraint
4084 don't match. Disable this reload so we don't crash in final. */
4085 error_for_asm (insn,
4086 "`asm' operand constraint incompatible with operand size");
4087 reload_in[r] = 0;
4088 reload_out[r] = 0;
4089 reload_reg_rtx[r] = 0;
4090 reload_optional[r] = 1;
4091 reload_secondary_p[r] = 1;
4093 return 1;
4096 /* Assign hard reg targets for the pseudo-registers we must reload
4097 into hard regs for this insn.
4098 Also output the instructions to copy them in and out of the hard regs.
4100 For machines with register classes, we are responsible for
4101 finding a reload reg in the proper class. */
4103 static void
4104 choose_reload_regs (insn, avoid_return_reg)
4105 rtx insn;
4106 /* This argument is currently ignored. */
4107 rtx avoid_return_reg;
4109 register int i, j;
4110 int max_group_size = 1;
4111 enum reg_class group_class = NO_REGS;
4112 int inheritance;
4114 rtx save_reload_reg_rtx[MAX_RELOADS];
4115 char save_reload_inherited[MAX_RELOADS];
4116 rtx save_reload_inheritance_insn[MAX_RELOADS];
4117 rtx save_reload_override_in[MAX_RELOADS];
4118 int save_reload_spill_index[MAX_RELOADS];
4119 HARD_REG_SET save_reload_reg_used;
4120 HARD_REG_SET save_reload_reg_used_in_input_addr;
4121 HARD_REG_SET save_reload_reg_used_in_output_addr;
4122 HARD_REG_SET save_reload_reg_used_in_op_addr;
4123 HARD_REG_SET save_reload_reg_used_in_input;
4124 HARD_REG_SET save_reload_reg_used_in_output;
4125 HARD_REG_SET save_reload_reg_used_at_all;
4127 bzero (reload_inherited, MAX_RELOADS);
4128 bzero (reload_inheritance_insn, MAX_RELOADS * sizeof (rtx));
4129 bzero (reload_override_in, MAX_RELOADS * sizeof (rtx));
4131 CLEAR_HARD_REG_SET (reload_reg_used);
4132 CLEAR_HARD_REG_SET (reload_reg_used_at_all);
4133 CLEAR_HARD_REG_SET (reload_reg_used_in_input_addr);
4134 CLEAR_HARD_REG_SET (reload_reg_used_in_output_addr);
4135 CLEAR_HARD_REG_SET (reload_reg_used_in_op_addr);
4136 CLEAR_HARD_REG_SET (reload_reg_used_in_output);
4137 CLEAR_HARD_REG_SET (reload_reg_used_in_input);
4139 /* Distinguish output-only and input-only reloads
4140 because they can overlap with other things. */
4141 for (j = 0; j < n_reloads; j++)
4142 if (reload_when_needed[j] == RELOAD_OTHER
4143 && ! reload_needed_for_multiple[j])
4145 if (reload_in[j] == 0)
4147 /* But earlyclobber operands must stay as RELOAD_OTHER. */
4148 for (i = 0; i < n_earlyclobbers; i++)
4149 if (rtx_equal_p (reload_out[j], reload_earlyclobbers[i]))
4150 break;
4151 if (i == n_earlyclobbers)
4152 reload_when_needed[j] = RELOAD_FOR_OUTPUT;
4154 if (reload_out[j] == 0)
4155 reload_when_needed[j] = RELOAD_FOR_INPUT;
4157 if (reload_secondary_reload[j] >= 0
4158 && ! reload_needed_for_multiple[reload_secondary_reload[j]])
4159 reload_when_needed[reload_secondary_reload[j]]
4160 = reload_when_needed[j];
4163 #ifdef SMALL_REGISTER_CLASSES
4164 /* Don't bother with avoiding the return reg
4165 if we have no mandatory reload that could use it. */
4166 if (avoid_return_reg)
4168 int do_avoid = 0;
4169 int regno = REGNO (avoid_return_reg);
4170 int nregs
4171 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4172 int r;
4174 for (r = regno; r < regno + nregs; r++)
4175 if (spill_reg_order[r] >= 0)
4176 for (j = 0; j < n_reloads; j++)
4177 if (!reload_optional[j] && reload_reg_rtx[j] == 0
4178 && (reload_in[j] != 0 || reload_out[j] != 0
4179 || reload_secondary_p[j])
4181 TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[j]], r))
4182 do_avoid = 1;
4183 if (!do_avoid)
4184 avoid_return_reg = 0;
4186 #endif /* SMALL_REGISTER_CLASSES */
4188 #if 0 /* Not needed, now that we can always retry without inheritance. */
4189 /* See if we have more mandatory reloads than spill regs.
4190 If so, then we cannot risk optimizations that could prevent
4191 reloads from sharing one spill register.
4193 Since we will try finding a better register than reload_reg_rtx
4194 unless it is equal to reload_in or reload_out, count such reloads. */
4197 int tem = 0;
4198 #ifdef SMALL_REGISTER_CLASSES
4199 int tem = (avoid_return_reg != 0);
4200 #endif
4201 for (j = 0; j < n_reloads; j++)
4202 if (! reload_optional[j]
4203 && (reload_in[j] != 0 || reload_out[j] != 0 || reload_secondary_p[j])
4204 && (reload_reg_rtx[j] == 0
4205 || (! rtx_equal_p (reload_reg_rtx[j], reload_in[j])
4206 && ! rtx_equal_p (reload_reg_rtx[j], reload_out[j]))))
4207 tem++;
4208 if (tem > n_spills)
4209 must_reuse = 1;
4211 #endif
4213 #ifdef SMALL_REGISTER_CLASSES
4214 /* Don't use the subroutine call return reg for a reload
4215 if we are supposed to avoid it. */
4216 if (avoid_return_reg)
4218 int regno = REGNO (avoid_return_reg);
4219 int nregs
4220 = HARD_REGNO_NREGS (regno, GET_MODE (avoid_return_reg));
4221 int r;
4223 for (r = regno; r < regno + nregs; r++)
4224 if (spill_reg_order[r] >= 0)
4225 SET_HARD_REG_BIT (reload_reg_used, r);
4227 #endif /* SMALL_REGISTER_CLASSES */
4229 /* In order to be certain of getting the registers we need,
4230 we must sort the reloads into order of increasing register class.
4231 Then our grabbing of reload registers will parallel the process
4232 that provided the reload registers.
4234 Also note whether any of the reloads wants a consecutive group of regs.
4235 If so, record the maximum size of the group desired and what
4236 register class contains all the groups needed by this insn. */
4238 for (j = 0; j < n_reloads; j++)
4240 reload_order[j] = j;
4241 reload_spill_index[j] = -1;
4243 reload_mode[j]
4244 = (reload_strict_low[j] && reload_out[j]
4245 ? GET_MODE (SUBREG_REG (reload_out[j]))
4246 : (reload_inmode[j] == VOIDmode
4247 || (GET_MODE_SIZE (reload_outmode[j])
4248 > GET_MODE_SIZE (reload_inmode[j])))
4249 ? reload_outmode[j] : reload_inmode[j]);
4251 reload_nregs[j] = CLASS_MAX_NREGS (reload_reg_class[j], reload_mode[j]);
4253 if (reload_nregs[j] > 1)
4255 max_group_size = MAX (reload_nregs[j], max_group_size);
4256 group_class = reg_class_superunion[(int)reload_reg_class[j]][(int)group_class];
4259 /* If we have already decided to use a certain register,
4260 don't use it in another way. */
4261 if (reload_reg_rtx[j])
4262 mark_reload_reg_in_use (REGNO (reload_reg_rtx[j]),
4263 reload_when_needed[j], reload_mode[j]);
4266 if (n_reloads > 1)
4267 qsort (reload_order, n_reloads, sizeof (short), reload_reg_class_lower);
4269 bcopy (reload_reg_rtx, save_reload_reg_rtx, sizeof reload_reg_rtx);
4270 bcopy (reload_inherited, save_reload_inherited, sizeof reload_inherited);
4271 bcopy (reload_inheritance_insn, save_reload_inheritance_insn,
4272 sizeof reload_inheritance_insn);
4273 bcopy (reload_override_in, save_reload_override_in,
4274 sizeof reload_override_in);
4275 bcopy (reload_spill_index, save_reload_spill_index,
4276 sizeof reload_spill_index);
4277 COPY_HARD_REG_SET (save_reload_reg_used, reload_reg_used);
4278 COPY_HARD_REG_SET (save_reload_reg_used_at_all, reload_reg_used_at_all);
4279 COPY_HARD_REG_SET (save_reload_reg_used_in_output,
4280 reload_reg_used_in_output);
4281 COPY_HARD_REG_SET (save_reload_reg_used_in_input,
4282 reload_reg_used_in_input);
4283 COPY_HARD_REG_SET (save_reload_reg_used_in_input_addr,
4284 reload_reg_used_in_input_addr);
4285 COPY_HARD_REG_SET (save_reload_reg_used_in_output_addr,
4286 reload_reg_used_in_output_addr);
4287 COPY_HARD_REG_SET (save_reload_reg_used_in_op_addr,
4288 reload_reg_used_in_op_addr);
4290 /* Try first with inheritance, then turning it off. */
4292 for (inheritance = 1; inheritance >= 0; inheritance--)
4294 /* Process the reloads in order of preference just found.
4295 Beyond this point, subregs can be found in reload_reg_rtx.
4297 This used to look for an existing reloaded home for all
4298 of the reloads, and only then perform any new reloads.
4299 But that could lose if the reloads were done out of reg-class order
4300 because a later reload with a looser constraint might have an old
4301 home in a register needed by an earlier reload with a tighter constraint.
4303 To solve this, we make two passes over the reloads, in the order
4304 described above. In the first pass we try to inherit a reload
4305 from a previous insn. If there is a later reload that needs a
4306 class that is a proper subset of the class being processed, we must
4307 also allocate a spill register during the first pass.
4309 Then make a second pass over the reloads to allocate any reloads
4310 that haven't been given registers yet. */
4312 for (j = 0; j < n_reloads; j++)
4314 register int r = reload_order[j];
4316 /* Ignore reloads that got marked inoperative. */
4317 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4318 continue;
4320 /* If find_reloads chose a to use reload_in or reload_out as a reload
4321 register, we don't need to chose one. Otherwise, try even if it found
4322 one since we might save an insn if we find the value lying around. */
4323 if (reload_in[r] != 0 && reload_reg_rtx[r] != 0
4324 && (rtx_equal_p (reload_in[r], reload_reg_rtx[r])
4325 || rtx_equal_p (reload_out[r], reload_reg_rtx[r])))
4326 continue;
4328 #if 0 /* No longer needed for correct operation.
4329 It might give better code, or might not; worth an experiment? */
4330 /* If this is an optional reload, we can't inherit from earlier insns
4331 until we are sure that any non-optional reloads have been allocated.
4332 The following code takes advantage of the fact that optional reloads
4333 are at the end of reload_order. */
4334 if (reload_optional[r] != 0)
4335 for (i = 0; i < j; i++)
4336 if ((reload_out[reload_order[i]] != 0
4337 || reload_in[reload_order[i]] != 0
4338 || reload_secondary_p[reload_order[i]])
4339 && ! reload_optional[reload_order[i]]
4340 && reload_reg_rtx[reload_order[i]] == 0)
4341 allocate_reload_reg (reload_order[i], insn, 0, inheritance);
4342 #endif
4344 /* First see if this pseudo is already available as reloaded
4345 for a previous insn. We cannot try to inherit for reloads
4346 that are smaller than the maximum number of registers needed
4347 for groups unless the register we would allocate cannot be used
4348 for the groups.
4350 We could check here to see if this is a secondary reload for
4351 an object that is already in a register of the desired class.
4352 This would avoid the need for the secondary reload register.
4353 But this is complex because we can't easily determine what
4354 objects might want to be loaded via this reload. So let a register
4355 be allocated here. In `emit_reload_insns' we suppress one of the
4356 loads in the case described above. */
4358 if (inheritance)
4360 register int regno = -1;
4362 if (reload_in[r] == 0)
4364 else if (GET_CODE (reload_in[r]) == REG)
4365 regno = REGNO (reload_in[r]);
4366 else if (GET_CODE (reload_in_reg[r]) == REG)
4367 regno = REGNO (reload_in_reg[r]);
4368 #if 0
4369 /* This won't work, since REGNO can be a pseudo reg number.
4370 Also, it takes much more hair to keep track of all the things
4371 that can invalidate an inherited reload of part of a pseudoreg. */
4372 else if (GET_CODE (reload_in[r]) == SUBREG
4373 && GET_CODE (SUBREG_REG (reload_in[r])) == REG)
4374 regno = REGNO (SUBREG_REG (reload_in[r])) + SUBREG_WORD (reload_in[r]);
4375 #endif
4377 if (regno >= 0 && reg_last_reload_reg[regno] != 0)
4379 i = spill_reg_order[REGNO (reg_last_reload_reg[regno])];
4381 if (reg_reloaded_contents[i] == regno
4382 && HARD_REGNO_MODE_OK (spill_regs[i], reload_mode[r])
4383 && TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4384 spill_regs[i])
4385 && (reload_nregs[r] == max_group_size
4386 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) group_class],
4387 spill_regs[i]))
4388 && reload_reg_free_p (spill_regs[i], reload_when_needed[r])
4389 && reload_reg_free_before_p (spill_regs[i],
4390 reload_when_needed[r]))
4392 /* If a group is needed, verify that all the subsequent
4393 registers still have their values intact. */
4394 int nr
4395 = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4396 int k;
4398 for (k = 1; k < nr; k++)
4399 if (reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
4400 != regno)
4401 break;
4403 if (k == nr)
4405 /* Mark the register as in use for this part of
4406 the insn. */
4407 mark_reload_reg_in_use (spill_regs[i],
4408 reload_when_needed[r],
4409 reload_mode[r]);
4410 reload_reg_rtx[r] = reg_last_reload_reg[regno];
4411 reload_inherited[r] = 1;
4412 reload_inheritance_insn[r] = reg_reloaded_insn[i];
4413 reload_spill_index[r] = i;
4419 /* Here's another way to see if the value is already lying around. */
4420 if (inheritance
4421 && reload_in[r] != 0
4422 && ! reload_inherited[r]
4423 && reload_out[r] == 0
4424 && (CONSTANT_P (reload_in[r])
4425 || GET_CODE (reload_in[r]) == PLUS
4426 || GET_CODE (reload_in[r]) == REG
4427 || GET_CODE (reload_in[r]) == MEM)
4428 && (reload_nregs[r] == max_group_size
4429 || ! reg_classes_intersect_p (reload_reg_class[r], group_class)))
4431 register rtx equiv
4432 = find_equiv_reg (reload_in[r], insn, reload_reg_class[r],
4433 -1, NULL_PTR, 0, reload_mode[r]);
4434 int regno;
4436 if (equiv != 0)
4438 if (GET_CODE (equiv) == REG)
4439 regno = REGNO (equiv);
4440 else if (GET_CODE (equiv) == SUBREG)
4442 regno = REGNO (SUBREG_REG (equiv));
4443 if (regno < FIRST_PSEUDO_REGISTER)
4444 regno += SUBREG_WORD (equiv);
4446 else
4447 abort ();
4450 /* If we found a spill reg, reject it unless it is free
4451 and of the desired class. */
4452 if (equiv != 0
4453 && ((spill_reg_order[regno] >= 0
4454 && ! reload_reg_free_before_p (regno,
4455 reload_when_needed[r]))
4456 || ! TEST_HARD_REG_BIT (reg_class_contents[(int) reload_reg_class[r]],
4457 regno)))
4458 equiv = 0;
4460 if (equiv != 0 && TEST_HARD_REG_BIT (reload_reg_used_at_all, regno))
4461 equiv = 0;
4463 if (equiv != 0 && ! HARD_REGNO_MODE_OK (regno, reload_mode[r]))
4464 equiv = 0;
4466 /* We found a register that contains the value we need.
4467 If this register is the same as an `earlyclobber' operand
4468 of the current insn, just mark it as a place to reload from
4469 since we can't use it as the reload register itself. */
4471 if (equiv != 0)
4472 for (i = 0; i < n_earlyclobbers; i++)
4473 if (reg_overlap_mentioned_for_reload_p (equiv,
4474 reload_earlyclobbers[i]))
4476 reload_override_in[r] = equiv;
4477 equiv = 0;
4478 break;
4481 /* JRV: If the equiv register we have found is explicitly
4482 clobbered in the current insn, mark but don't use, as above. */
4484 if (equiv != 0 && regno_clobbered_p (regno, insn))
4486 reload_override_in[r] = equiv;
4487 equiv = 0;
4490 /* If we found an equivalent reg, say no code need be generated
4491 to load it, and use it as our reload reg. */
4492 if (equiv != 0 && regno != FRAME_POINTER_REGNUM)
4494 reload_reg_rtx[r] = equiv;
4495 reload_inherited[r] = 1;
4496 /* If it is a spill reg,
4497 mark the spill reg as in use for this insn. */
4498 i = spill_reg_order[regno];
4499 if (i >= 0)
4500 mark_reload_reg_in_use (regno, reload_when_needed[r],
4501 reload_mode[r]);
4505 /* If we found a register to use already, or if this is an optional
4506 reload, we are done. */
4507 if (reload_reg_rtx[r] != 0 || reload_optional[r] != 0)
4508 continue;
4510 #if 0 /* No longer needed for correct operation. Might or might not
4511 give better code on the average. Want to experiment? */
4513 /* See if there is a later reload that has a class different from our
4514 class that intersects our class or that requires less register
4515 than our reload. If so, we must allocate a register to this
4516 reload now, since that reload might inherit a previous reload
4517 and take the only available register in our class. Don't do this
4518 for optional reloads since they will force all previous reloads
4519 to be allocated. Also don't do this for reloads that have been
4520 turned off. */
4522 for (i = j + 1; i < n_reloads; i++)
4524 int s = reload_order[i];
4526 if ((reload_in[s] == 0 && reload_out[s] == 0
4527 && ! reload_secondary_p[s])
4528 || reload_optional[s])
4529 continue;
4531 if ((reload_reg_class[s] != reload_reg_class[r]
4532 && reg_classes_intersect_p (reload_reg_class[r],
4533 reload_reg_class[s]))
4534 || reload_nregs[s] < reload_nregs[r])
4535 break;
4538 if (i == n_reloads)
4539 continue;
4541 allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance);
4542 #endif
4545 /* Now allocate reload registers for anything non-optional that
4546 didn't get one yet. */
4547 for (j = 0; j < n_reloads; j++)
4549 register int r = reload_order[j];
4551 /* Ignore reloads that got marked inoperative. */
4552 if (reload_out[r] == 0 && reload_in[r] == 0 && ! reload_secondary_p[r])
4553 continue;
4555 /* Skip reloads that already have a register allocated or are
4556 optional. */
4557 if (reload_reg_rtx[r] != 0 || reload_optional[r])
4558 continue;
4560 if (! allocate_reload_reg (r, insn, j == n_reloads - 1, inheritance))
4561 break;
4564 /* If that loop got all the way, we have won. */
4565 if (j == n_reloads)
4566 break;
4568 fail:
4569 /* Loop around and try without any inheritance. */
4570 /* First undo everything done by the failed attempt
4571 to allocate with inheritance. */
4572 bcopy (save_reload_reg_rtx, reload_reg_rtx, sizeof reload_reg_rtx);
4573 bcopy (save_reload_inherited, reload_inherited, sizeof reload_inherited);
4574 bcopy (save_reload_inheritance_insn, reload_inheritance_insn,
4575 sizeof reload_inheritance_insn);
4576 bcopy (save_reload_override_in, reload_override_in,
4577 sizeof reload_override_in);
4578 bcopy (save_reload_spill_index, reload_spill_index,
4579 sizeof reload_spill_index);
4580 COPY_HARD_REG_SET (reload_reg_used, save_reload_reg_used);
4581 COPY_HARD_REG_SET (reload_reg_used_at_all, save_reload_reg_used_at_all);
4582 COPY_HARD_REG_SET (reload_reg_used_in_input,
4583 save_reload_reg_used_in_input);
4584 COPY_HARD_REG_SET (reload_reg_used_in_output,
4585 save_reload_reg_used_in_output);
4586 COPY_HARD_REG_SET (reload_reg_used_in_input_addr,
4587 save_reload_reg_used_in_input_addr);
4588 COPY_HARD_REG_SET (reload_reg_used_in_output_addr,
4589 save_reload_reg_used_in_output_addr);
4590 COPY_HARD_REG_SET (reload_reg_used_in_op_addr,
4591 save_reload_reg_used_in_op_addr);
4594 /* If we thought we could inherit a reload, because it seemed that
4595 nothing else wanted the same reload register earlier in the insn,
4596 verify that assumption, now that all reloads have been assigned. */
4598 for (j = 0; j < n_reloads; j++)
4600 register int r = reload_order[j];
4602 if (reload_inherited[r] && reload_reg_rtx[r] != 0
4603 && ! reload_reg_free_before_p (true_regnum (reload_reg_rtx[r]),
4604 reload_when_needed[r]))
4605 reload_inherited[r] = 0;
4607 /* If we found a better place to reload from,
4608 validate it in the same fashion, if it is a reload reg. */
4609 if (reload_override_in[r]
4610 && (GET_CODE (reload_override_in[r]) == REG
4611 || GET_CODE (reload_override_in[r]) == SUBREG))
4613 int regno = true_regnum (reload_override_in[r]);
4614 if (spill_reg_order[regno] >= 0
4615 && ! reload_reg_free_before_p (regno, reload_when_needed[r]))
4616 reload_override_in[r] = 0;
4620 /* Now that reload_override_in is known valid,
4621 actually override reload_in. */
4622 for (j = 0; j < n_reloads; j++)
4623 if (reload_override_in[j])
4624 reload_in[j] = reload_override_in[j];
4626 /* If this reload won't be done because it has been cancelled or is
4627 optional and not inherited, clear reload_reg_rtx so other
4628 routines (such as subst_reloads) don't get confused. */
4629 for (j = 0; j < n_reloads; j++)
4630 if ((reload_optional[j] && ! reload_inherited[j])
4631 || (reload_in[j] == 0 && reload_out[j] == 0
4632 && ! reload_secondary_p[j]))
4633 reload_reg_rtx[j] = 0;
4635 /* Record which pseudos and which spill regs have output reloads. */
4636 for (j = 0; j < n_reloads; j++)
4638 register int r = reload_order[j];
4640 i = reload_spill_index[r];
4642 /* I is nonneg if this reload used one of the spill regs.
4643 If reload_reg_rtx[r] is 0, this is an optional reload
4644 that we opted to ignore. */
4645 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG
4646 && reload_reg_rtx[r] != 0)
4648 register int nregno = REGNO (reload_out[r]);
4649 int nr = 1;
4651 if (nregno < FIRST_PSEUDO_REGISTER)
4652 nr = HARD_REGNO_NREGS (nregno, reload_mode[r]);
4654 while (--nr >= 0)
4655 reg_has_output_reload[nregno + nr] = 1;
4657 if (i >= 0)
4659 nr = HARD_REGNO_NREGS (spill_regs[i], reload_mode[r]);
4660 while (--nr >= 0)
4661 SET_HARD_REG_BIT (reg_is_output_reload, spill_regs[i] + nr);
4664 if (reload_when_needed[r] != RELOAD_OTHER
4665 && reload_when_needed[r] != RELOAD_FOR_OUTPUT)
4666 abort ();
4671 /* Output insns to reload values in and out of the chosen reload regs. */
4673 static void
4674 emit_reload_insns (insn)
4675 rtx insn;
4677 register int j;
4678 rtx following_insn = NEXT_INSN (insn);
4679 rtx before_insn = insn;
4680 rtx first_output_reload_insn = NEXT_INSN (insn);
4681 rtx first_other_reload_insn = insn;
4682 rtx first_operand_address_reload_insn = insn;
4683 int special;
4684 /* Values to be put in spill_reg_store are put here first. */
4685 rtx new_spill_reg_store[FIRST_PSEUDO_REGISTER];
4687 /* If this is a CALL_INSN preceded by USE insns, any reload insns
4688 must go in front of the first USE insn, not in front of INSN. */
4690 if (GET_CODE (insn) == CALL_INSN && GET_CODE (PREV_INSN (insn)) == INSN
4691 && GET_CODE (PATTERN (PREV_INSN (insn))) == USE)
4692 while (GET_CODE (PREV_INSN (before_insn)) == INSN
4693 && GET_CODE (PATTERN (PREV_INSN (before_insn))) == USE)
4694 first_other_reload_insn = first_operand_address_reload_insn
4695 = before_insn = PREV_INSN (before_insn);
4697 /* Now output the instructions to copy the data into and out of the
4698 reload registers. Do these in the order that the reloads were reported,
4699 since reloads of base and index registers precede reloads of operands
4700 and the operands may need the base and index registers reloaded. */
4702 for (j = 0; j < n_reloads; j++)
4704 register rtx old;
4705 rtx oldequiv_reg = 0;
4706 rtx this_reload_insn = 0;
4707 rtx store_insn = 0;
4709 old = reload_in[j];
4710 if (old != 0 && ! reload_inherited[j]
4711 && ! rtx_equal_p (reload_reg_rtx[j], old)
4712 && reload_reg_rtx[j] != 0)
4714 register rtx reloadreg = reload_reg_rtx[j];
4715 rtx oldequiv = 0;
4716 enum machine_mode mode;
4717 rtx where;
4718 rtx reload_insn;
4720 /* Determine the mode to reload in.
4721 This is very tricky because we have three to choose from.
4722 There is the mode the insn operand wants (reload_inmode[J]).
4723 There is the mode of the reload register RELOADREG.
4724 There is the intrinsic mode of the operand, which we could find
4725 by stripping some SUBREGs.
4726 It turns out that RELOADREG's mode is irrelevant:
4727 we can change that arbitrarily.
4729 Consider (SUBREG:SI foo:QI) as an operand that must be SImode;
4730 then the reload reg may not support QImode moves, so use SImode.
4731 If foo is in memory due to spilling a pseudo reg, this is safe,
4732 because the QImode value is in the least significant part of a
4733 slot big enough for a SImode. If foo is some other sort of
4734 memory reference, then it is impossible to reload this case,
4735 so previous passes had better make sure this never happens.
4737 Then consider a one-word union which has SImode and one of its
4738 members is a float, being fetched as (SUBREG:SF union:SI).
4739 We must fetch that as SFmode because we could be loading into
4740 a float-only register. In this case OLD's mode is correct.
4742 Consider an immediate integer: it has VOIDmode. Here we need
4743 to get a mode from something else.
4745 In some cases, there is a fourth mode, the operand's
4746 containing mode. If the insn specifies a containing mode for
4747 this operand, it overrides all others.
4749 I am not sure whether the algorithm here is always right,
4750 but it does the right things in those cases. */
4752 mode = GET_MODE (old);
4753 if (mode == VOIDmode)
4754 mode = reload_inmode[j];
4755 if (reload_strict_low[j])
4756 mode = GET_MODE (SUBREG_REG (reload_in[j]));
4758 #ifdef SECONDARY_INPUT_RELOAD_CLASS
4759 /* If we need a secondary register for this operation, see if
4760 the value is already in a register in that class. Don't
4761 do this if the secondary register will be used as a scratch
4762 register. */
4764 if (reload_secondary_reload[j] >= 0
4765 && reload_secondary_icode[j] == CODE_FOR_nothing)
4766 oldequiv
4767 = find_equiv_reg (old, insn,
4768 reload_reg_class[reload_secondary_reload[j]],
4769 -1, NULL_PTR, 0, mode);
4770 #endif
4772 /* If reloading from memory, see if there is a register
4773 that already holds the same value. If so, reload from there.
4774 We can pass 0 as the reload_reg_p argument because
4775 any other reload has either already been emitted,
4776 in which case find_equiv_reg will see the reload-insn,
4777 or has yet to be emitted, in which case it doesn't matter
4778 because we will use this equiv reg right away. */
4780 if (oldequiv == 0
4781 && (GET_CODE (old) == MEM
4782 || (GET_CODE (old) == REG
4783 && REGNO (old) >= FIRST_PSEUDO_REGISTER
4784 && reg_renumber[REGNO (old)] < 0)))
4785 oldequiv = find_equiv_reg (old, insn, GENERAL_REGS,
4786 -1, NULL_PTR, 0, mode);
4788 if (oldequiv)
4790 int regno = true_regnum (oldequiv);
4792 /* If OLDEQUIV is a spill register, don't use it for this
4793 if any other reload needs it at an earlier stage of this insn
4794 or at this stage. */
4795 if (spill_reg_order[regno] >= 0
4796 && (! reload_reg_free_p (regno, reload_when_needed[j])
4797 || ! reload_reg_free_before_p (regno,
4798 reload_when_needed[j])))
4799 oldequiv = 0;
4801 /* If OLDEQUIV is not a spill register,
4802 don't use it if any other reload wants it. */
4803 if (spill_reg_order[regno] < 0)
4805 int k;
4806 for (k = 0; k < n_reloads; k++)
4807 if (reload_reg_rtx[k] != 0 && k != j
4808 && reg_overlap_mentioned_for_reload_p (reload_reg_rtx[k],
4809 oldequiv))
4811 oldequiv = 0;
4812 break;
4817 if (oldequiv == 0)
4818 oldequiv = old;
4819 else if (GET_CODE (oldequiv) == REG)
4820 oldequiv_reg = oldequiv;
4821 else if (GET_CODE (oldequiv) == SUBREG)
4822 oldequiv_reg = SUBREG_REG (oldequiv);
4824 /* Encapsulate both RELOADREG and OLDEQUIV into that mode,
4825 then load RELOADREG from OLDEQUIV. */
4827 if (GET_MODE (reloadreg) != mode)
4828 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
4829 while (GET_CODE (oldequiv) == SUBREG && GET_MODE (oldequiv) != mode)
4830 oldequiv = SUBREG_REG (oldequiv);
4831 if (GET_MODE (oldequiv) != VOIDmode
4832 && mode != GET_MODE (oldequiv))
4833 oldequiv = gen_rtx (SUBREG, mode, oldequiv, 0);
4835 /* Decide where to put reload insn for this reload. */
4836 switch (reload_when_needed[j])
4838 case RELOAD_FOR_INPUT:
4839 case RELOAD_OTHER:
4840 where = first_operand_address_reload_insn;
4841 break;
4842 case RELOAD_FOR_INPUT_RELOAD_ADDRESS:
4843 where = first_other_reload_insn;
4844 break;
4845 case RELOAD_FOR_OUTPUT_RELOAD_ADDRESS:
4846 where = first_output_reload_insn;
4847 break;
4848 case RELOAD_FOR_OPERAND_ADDRESS:
4849 where = before_insn;
4852 special = 0;
4854 /* Auto-increment addresses must be reloaded in a special way. */
4855 if (GET_CODE (oldequiv) == POST_INC
4856 || GET_CODE (oldequiv) == POST_DEC
4857 || GET_CODE (oldequiv) == PRE_INC
4858 || GET_CODE (oldequiv) == PRE_DEC)
4860 /* We are not going to bother supporting the case where a
4861 incremented register can't be copied directly from
4862 OLDEQUIV since this seems highly unlikely. */
4863 if (reload_secondary_reload[j] >= 0)
4864 abort ();
4865 /* Prevent normal processing of this reload. */
4866 special = 1;
4867 /* Output a special code sequence for this case. */
4868 this_reload_insn
4869 = inc_for_reload (reloadreg, oldequiv, reload_inc[j], where);
4872 /* If we are reloading a pseudo-register that was set by the previous
4873 insn, see if we can get rid of that pseudo-register entirely
4874 by redirecting the previous insn into our reload register. */
4876 else if (optimize && GET_CODE (old) == REG
4877 && REGNO (old) >= FIRST_PSEUDO_REGISTER
4878 && dead_or_set_p (insn, old)
4879 /* This is unsafe if some other reload
4880 uses the same reg first. */
4881 && (reload_when_needed[j] == RELOAD_OTHER
4882 || reload_when_needed[j] == RELOAD_FOR_INPUT
4883 || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS))
4885 rtx temp = PREV_INSN (insn);
4886 while (temp && GET_CODE (temp) == NOTE)
4887 temp = PREV_INSN (temp);
4888 if (temp
4889 && GET_CODE (temp) == INSN
4890 && GET_CODE (PATTERN (temp)) == SET
4891 && SET_DEST (PATTERN (temp)) == old
4892 /* Make sure we can access insn_operand_constraint. */
4893 && asm_noperands (PATTERN (temp)) < 0
4894 /* This is unsafe if prev insn rejects our reload reg. */
4895 && constraint_accepts_reg_p (insn_operand_constraint[recog_memoized (temp)][0],
4896 reloadreg)
4897 /* This is unsafe if operand occurs more than once in current
4898 insn. Perhaps some occurrences aren't reloaded. */
4899 && count_occurrences (PATTERN (insn), old) == 1
4900 /* Don't risk splitting a matching pair of operands. */
4901 && ! reg_mentioned_p (old, SET_SRC (PATTERN (temp))))
4903 /* Store into the reload register instead of the pseudo. */
4904 SET_DEST (PATTERN (temp)) = reloadreg;
4905 /* If these are the only uses of the pseudo reg,
4906 pretend for GDB it lives in the reload reg we used. */
4907 if (reg_n_deaths[REGNO (old)] == 1
4908 && reg_n_sets[REGNO (old)] == 1)
4910 reg_renumber[REGNO (old)] = REGNO (reload_reg_rtx[j]);
4911 alter_reg (REGNO (old), -1);
4913 special = 1;
4917 /* We can't do that, so output an insn to load RELOADREG.
4918 Keep them in the following order:
4919 all reloads for input reload addresses,
4920 all reloads for ordinary input operands,
4921 all reloads for addresses of non-reloaded operands,
4922 the insn being reloaded,
4923 all reloads for addresses of output reloads,
4924 the output reloads. */
4925 if (! special)
4927 #ifdef SECONDARY_INPUT_RELOAD_CLASS
4928 rtx second_reload_reg = 0;
4929 enum insn_code icode;
4931 /* If we have a secondary reload, pick up the secondary register
4932 and icode, if any. If OLDEQUIV and OLD are different or
4933 if this is an in-out reload, recompute whether or not we
4934 still need a secondary register and what the icode should
4935 be. If we still need a secondary register and the class or
4936 icode is different, go back to reloading from OLD if using
4937 OLDEQUIV means that we got the wrong type of register. We
4938 cannot have different class or icode due to an in-out reload
4939 because we don't make such reloads when both the input and
4940 output need secondary reload registers. */
4942 if (reload_secondary_reload[j] >= 0)
4944 int secondary_reload = reload_secondary_reload[j];
4945 rtx real_oldequiv = oldequiv;
4946 rtx real_old = old;
4948 /* If OLDEQUIV is a pseudo with a MEM, get the real MEM
4949 and similarly for OLD.
4950 See comments in find_secondary_reload in reload.c. */
4951 if (GET_CODE (oldequiv) == REG
4952 && REGNO (oldequiv) >= FIRST_PSEUDO_REGISTER
4953 && reg_equiv_mem[REGNO (oldequiv)] != 0)
4954 real_oldequiv = reg_equiv_mem[REGNO (oldequiv)];
4956 if (GET_CODE (old) == REG
4957 && REGNO (old) >= FIRST_PSEUDO_REGISTER
4958 && reg_equiv_mem[REGNO (old)] != 0)
4959 real_old = reg_equiv_mem[REGNO (old)];
4961 second_reload_reg = reload_reg_rtx[secondary_reload];
4962 icode = reload_secondary_icode[j];
4964 if ((old != oldequiv && ! rtx_equal_p (old, oldequiv))
4965 || (reload_in[j] != 0 && reload_out[j] != 0))
4967 enum reg_class new_class
4968 = SECONDARY_INPUT_RELOAD_CLASS (reload_reg_class[j],
4969 mode, real_oldequiv);
4971 if (new_class == NO_REGS)
4972 second_reload_reg = 0;
4973 else
4975 enum insn_code new_icode;
4976 enum machine_mode new_mode;
4978 if (! TEST_HARD_REG_BIT (reg_class_contents[(int) new_class],
4979 REGNO (second_reload_reg)))
4980 oldequiv = old, real_oldequiv = real_old;
4981 else
4983 new_icode = reload_in_optab[(int) mode];
4984 if (new_icode != CODE_FOR_nothing
4985 && ((insn_operand_predicate[(int) new_icode][0]
4986 && ! ((*insn_operand_predicate[(int) new_icode][0])
4987 (reloadreg, mode)))
4988 || (insn_operand_predicate[(int) new_icode][1]
4989 && ! ((*insn_operand_predicate[(int) new_icode][1])
4990 (real_oldequiv, mode)))))
4991 new_icode = CODE_FOR_nothing;
4993 if (new_icode == CODE_FOR_nothing)
4994 new_mode = mode;
4995 else
4996 new_mode = insn_operand_mode[new_icode][2];
4998 if (GET_MODE (second_reload_reg) != new_mode)
5000 if (!HARD_REGNO_MODE_OK (REGNO (second_reload_reg),
5001 new_mode))
5002 oldequiv = old, real_oldequiv = real_old;
5003 else
5004 second_reload_reg
5005 = gen_rtx (REG, new_mode,
5006 REGNO (second_reload_reg));
5012 /* If we still need a secondary reload register, check
5013 to see if it is being used as a scratch or intermediate
5014 register and generate code appropriately. If we need
5015 a scratch register, use REAL_OLDEQUIV since the form of
5016 the insn may depend on the actual address if it is
5017 a MEM. */
5019 if (second_reload_reg)
5021 if (icode != CODE_FOR_nothing)
5023 reload_insn = emit_insn_before (GEN_FCN (icode)
5024 (reloadreg,
5025 real_oldequiv,
5026 second_reload_reg),
5027 where);
5028 if (this_reload_insn == 0)
5029 this_reload_insn = reload_insn;
5030 special = 1;
5032 else
5034 /* See if we need a scratch register to load the
5035 intermediate register (a tertiary reload). */
5036 enum insn_code tertiary_icode
5037 = reload_secondary_icode[secondary_reload];
5039 if (tertiary_icode != CODE_FOR_nothing)
5041 rtx third_reload_reg
5042 = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
5044 reload_insn
5045 = emit_insn_before ((GEN_FCN (tertiary_icode)
5046 (second_reload_reg,
5047 real_oldequiv,
5048 third_reload_reg)),
5049 where);
5050 if (this_reload_insn == 0)
5051 this_reload_insn = reload_insn;
5053 else
5055 reload_insn
5056 = gen_input_reload (second_reload_reg,
5057 oldequiv, where);
5058 if (this_reload_insn == 0)
5059 this_reload_insn = reload_insn;
5060 oldequiv = second_reload_reg;
5065 #endif
5067 if (! special)
5069 reload_insn = gen_input_reload (reloadreg, oldequiv, where);
5070 if (this_reload_insn == 0)
5071 this_reload_insn = reload_insn;
5074 #if defined(SECONDARY_INPUT_RELOAD_CLASS) && defined(PRESERVE_DEATH_INFO_REGNO_P)
5075 /* We may have to make a REG_DEAD note for the secondary reload
5076 register in the insns we just made. Find the last insn that
5077 mentioned the register. */
5078 if (! special && second_reload_reg
5079 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reload_reg)))
5081 rtx prev;
5083 for (prev = where;
5084 prev != PREV_INSN (this_reload_insn);
5085 prev = PREV_INSN (prev))
5086 if (GET_RTX_CLASS (GET_CODE (prev) == 'i')
5087 && reg_overlap_mentioned_for_reload_p (second_reload_reg,
5088 PATTERN (prev)))
5090 REG_NOTES (prev) = gen_rtx (EXPR_LIST, REG_DEAD,
5091 second_reload_reg,
5092 REG_NOTES (prev));
5093 break;
5096 #endif
5099 /* Update where to put other reload insns. */
5100 if (this_reload_insn)
5101 switch (reload_when_needed[j])
5103 case RELOAD_FOR_INPUT:
5104 case RELOAD_OTHER:
5105 if (first_other_reload_insn == first_operand_address_reload_insn)
5106 first_other_reload_insn = this_reload_insn;
5107 break;
5108 case RELOAD_FOR_OPERAND_ADDRESS:
5109 if (first_operand_address_reload_insn == before_insn)
5110 first_operand_address_reload_insn = this_reload_insn;
5111 if (first_other_reload_insn == before_insn)
5112 first_other_reload_insn = this_reload_insn;
5115 /* reload_inc[j] was formerly processed here. */
5118 /* Add a note saying the input reload reg
5119 dies in this insn, if anyone cares. */
5120 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5121 if (old != 0
5122 && reload_reg_rtx[j] != old
5123 && reload_reg_rtx[j] != 0
5124 && reload_out[j] == 0
5125 && ! reload_inherited[j]
5126 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j])))
5128 register rtx reloadreg = reload_reg_rtx[j];
5130 #if 0
5131 /* We can't abort here because we need to support this for sched.c.
5132 It's not terrible to miss a REG_DEAD note, but we should try
5133 to figure out how to do this correctly. */
5134 /* The code below is incorrect for address-only reloads. */
5135 if (reload_when_needed[j] != RELOAD_OTHER
5136 && reload_when_needed[j] != RELOAD_FOR_INPUT)
5137 abort ();
5138 #endif
5140 /* Add a death note to this insn, for an input reload. */
5142 if ((reload_when_needed[j] == RELOAD_OTHER
5143 || reload_when_needed[j] == RELOAD_FOR_INPUT)
5144 && ! dead_or_set_p (insn, reloadreg))
5145 REG_NOTES (insn)
5146 = gen_rtx (EXPR_LIST, REG_DEAD,
5147 reloadreg, REG_NOTES (insn));
5150 /* When we inherit a reload, the last marked death of the reload reg
5151 may no longer really be a death. */
5152 if (reload_reg_rtx[j] != 0
5153 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (reload_reg_rtx[j]))
5154 && reload_inherited[j])
5156 /* Handle inheriting an output reload.
5157 Remove the death note from the output reload insn. */
5158 if (reload_spill_index[j] >= 0
5159 && GET_CODE (reload_in[j]) == REG
5160 && spill_reg_store[reload_spill_index[j]] != 0
5161 && find_regno_note (spill_reg_store[reload_spill_index[j]],
5162 REG_DEAD, REGNO (reload_reg_rtx[j])))
5163 remove_death (REGNO (reload_reg_rtx[j]),
5164 spill_reg_store[reload_spill_index[j]]);
5165 /* Likewise for input reloads that were inherited. */
5166 else if (reload_spill_index[j] >= 0
5167 && GET_CODE (reload_in[j]) == REG
5168 && spill_reg_store[reload_spill_index[j]] == 0
5169 && reload_inheritance_insn[j] != 0
5170 && find_regno_note (reload_inheritance_insn[j], REG_DEAD,
5171 REGNO (reload_reg_rtx[j])))
5172 remove_death (REGNO (reload_reg_rtx[j]),
5173 reload_inheritance_insn[j]);
5174 else
5176 rtx prev;
5178 /* We got this register from find_equiv_reg.
5179 Search back for its last death note and get rid of it.
5180 But don't search back too far.
5181 Don't go past a place where this reg is set,
5182 since a death note before that remains valid. */
5183 for (prev = PREV_INSN (insn);
5184 prev && GET_CODE (prev) != CODE_LABEL;
5185 prev = PREV_INSN (prev))
5186 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
5187 && dead_or_set_p (prev, reload_reg_rtx[j]))
5189 if (find_regno_note (prev, REG_DEAD,
5190 REGNO (reload_reg_rtx[j])))
5191 remove_death (REGNO (reload_reg_rtx[j]), prev);
5192 break;
5197 /* We might have used find_equiv_reg above to choose an alternate
5198 place from which to reload. If so, and it died, we need to remove
5199 that death and move it to one of the insns we just made. */
5201 if (oldequiv_reg != 0
5202 && PRESERVE_DEATH_INFO_REGNO_P (true_regnum (oldequiv_reg)))
5204 rtx prev, prev1;
5206 for (prev = PREV_INSN (insn); prev && GET_CODE (prev) != CODE_LABEL;
5207 prev = PREV_INSN (prev))
5208 if (GET_RTX_CLASS (GET_CODE (prev)) == 'i'
5209 && dead_or_set_p (prev, oldequiv_reg))
5211 if (find_regno_note (prev, REG_DEAD, REGNO (oldequiv_reg)))
5213 for (prev1 = this_reload_insn;
5214 prev1; prev1 = PREV_INSN (prev1))
5215 if (GET_RTX_CLASS (GET_CODE (prev1) == 'i')
5216 && reg_overlap_mentioned_for_reload_p (oldequiv_reg,
5217 PATTERN (prev1)))
5219 REG_NOTES (prev1) = gen_rtx (EXPR_LIST, REG_DEAD,
5220 oldequiv_reg,
5221 REG_NOTES (prev1));
5222 break;
5224 remove_death (REGNO (oldequiv_reg), prev);
5226 break;
5229 #endif
5231 /* If we are reloading a register that was recently stored in with an
5232 output-reload, see if we can prove there was
5233 actually no need to store the old value in it. */
5235 if (optimize && reload_inherited[j] && reload_spill_index[j] >= 0
5236 /* This is unsafe if some other reload uses the same reg first. */
5237 && (reload_when_needed[j] == RELOAD_OTHER
5238 || reload_when_needed[j] == RELOAD_FOR_INPUT
5239 || reload_when_needed[j] == RELOAD_FOR_INPUT_RELOAD_ADDRESS)
5240 && GET_CODE (reload_in[j]) == REG
5241 #if 0
5242 /* There doesn't seem to be any reason to restrict this to pseudos
5243 and doing so loses in the case where we are copying from a
5244 register of the wrong class. */
5245 && REGNO (reload_in[j]) >= FIRST_PSEUDO_REGISTER
5246 #endif
5247 && spill_reg_store[reload_spill_index[j]] != 0
5248 && dead_or_set_p (insn, reload_in[j])
5249 /* This is unsafe if operand occurs more than once in current
5250 insn. Perhaps some occurrences weren't reloaded. */
5251 && count_occurrences (PATTERN (insn), reload_in[j]) == 1)
5252 delete_output_reload (insn, j,
5253 spill_reg_store[reload_spill_index[j]]);
5255 /* Input-reloading is done. Now do output-reloading,
5256 storing the value from the reload-register after the main insn
5257 if reload_out[j] is nonzero.
5259 ??? At some point we need to support handling output reloads of
5260 JUMP_INSNs or insns that set cc0. */
5261 old = reload_out[j];
5262 if (old != 0
5263 && reload_reg_rtx[j] != old
5264 && reload_reg_rtx[j] != 0)
5266 register rtx reloadreg = reload_reg_rtx[j];
5267 register rtx second_reloadreg = 0;
5268 rtx prev_insn = PREV_INSN (first_output_reload_insn);
5269 rtx note, p;
5270 enum machine_mode mode;
5271 int special = 0;
5273 /* An output operand that dies right away does need a reload,
5274 but need not be copied from it. Show the new location in the
5275 REG_UNUSED note. */
5276 if ((GET_CODE (old) == REG || GET_CODE (old) == SCRATCH)
5277 && (note = find_reg_note (insn, REG_UNUSED, old)) != 0)
5279 XEXP (note, 0) = reload_reg_rtx[j];
5280 continue;
5282 else if (GET_CODE (old) == SCRATCH)
5283 /* If we aren't optimizing, there won't be a REG_UNUSED note,
5284 but we don't want to make an output reload. */
5285 continue;
5287 #if 0
5288 /* Strip off of OLD any size-increasing SUBREGs such as
5289 (SUBREG:SI foo:QI 0). */
5291 while (GET_CODE (old) == SUBREG && SUBREG_WORD (old) == 0
5292 && (GET_MODE_SIZE (GET_MODE (old))
5293 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (old)))))
5294 old = SUBREG_REG (old);
5295 #endif
5297 /* If is a JUMP_INSN, we can't support output reloads yet. */
5298 if (GET_CODE (insn) == JUMP_INSN)
5299 abort ();
5301 /* Determine the mode to reload in.
5302 See comments above (for input reloading). */
5304 mode = GET_MODE (old);
5305 if (mode == VOIDmode)
5306 abort (); /* Should never happen for an output. */
5308 /* A strict-low-part output operand needs to be reloaded
5309 in the mode of the entire value. */
5310 if (reload_strict_low[j])
5312 mode = GET_MODE (SUBREG_REG (reload_out[j]));
5313 /* Encapsulate OLD into that mode. */
5314 /* If OLD is a subreg, then strip it, since the subreg will
5315 be altered by this very reload. */
5316 while (GET_CODE (old) == SUBREG && GET_MODE (old) != mode)
5317 old = SUBREG_REG (old);
5318 if (GET_MODE (old) != VOIDmode
5319 && mode != GET_MODE (old))
5320 old = gen_rtx (SUBREG, mode, old, 0);
5323 if (GET_MODE (reloadreg) != mode)
5324 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
5326 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5328 /* If we need two reload regs, set RELOADREG to the intermediate
5329 one, since it will be stored into OUT. We might need a secondary
5330 register only for an input reload, so check again here. */
5332 if (reload_secondary_reload[j] >= 0)
5334 rtx real_old = old;
5336 if (GET_CODE (old) == REG && REGNO (old) >= FIRST_PSEUDO_REGISTER
5337 && reg_equiv_mem[REGNO (old)] != 0)
5338 real_old = reg_equiv_mem[REGNO (old)];
5340 if((SECONDARY_OUTPUT_RELOAD_CLASS (reload_reg_class[j],
5341 mode, real_old)
5342 != NO_REGS))
5344 second_reloadreg = reloadreg;
5345 reloadreg = reload_reg_rtx[reload_secondary_reload[j]];
5347 /* See if RELOADREG is to be used as a scratch register
5348 or as an intermediate register. */
5349 if (reload_secondary_icode[j] != CODE_FOR_nothing)
5351 emit_insn_before ((GEN_FCN (reload_secondary_icode[j])
5352 (real_old, second_reloadreg,
5353 reloadreg)),
5354 first_output_reload_insn);
5355 special = 1;
5357 else
5359 /* See if we need both a scratch and intermediate reload
5360 register. */
5361 int secondary_reload = reload_secondary_reload[j];
5362 enum insn_code tertiary_icode
5363 = reload_secondary_icode[secondary_reload];
5364 rtx pat;
5366 if (GET_MODE (reloadreg) != mode)
5367 reloadreg = gen_rtx (REG, mode, REGNO (reloadreg));
5369 if (tertiary_icode != CODE_FOR_nothing)
5371 rtx third_reloadreg
5372 = reload_reg_rtx[reload_secondary_reload[secondary_reload]];
5373 pat = (GEN_FCN (tertiary_icode)
5374 (reloadreg, second_reloadreg, third_reloadreg));
5376 #ifdef SECONDARY_MEMORY_NEEDED
5377 /* If we need a memory location to do the move, do it that way. */
5378 else if (GET_CODE (reloadreg) == REG
5379 && REGNO (reloadreg) < FIRST_PSEUDO_REGISTER
5380 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (reloadreg)),
5381 REGNO_REG_CLASS (REGNO (second_reloadreg)),
5382 GET_MODE (second_reloadreg)))
5384 /* Get the memory to use and rewrite both registers
5385 to its mode. */
5386 rtx loc = get_secondary_mem (reloadreg,
5387 GET_MODE (second_reloadreg));
5388 rtx tmp_reloadreg;
5390 if (GET_MODE (loc) != GET_MODE (second_reloadreg))
5391 second_reloadreg = gen_rtx (REG, GET_MODE (loc),
5392 REGNO (second_reloadreg));
5394 if (GET_MODE (loc) != GET_MODE (reloadreg))
5395 tmp_reloadreg = gen_rtx (REG, GET_MODE (loc),
5396 REGNO (reloadreg));
5397 else
5398 tmp_reloadreg = reloadreg;
5400 emit_insn_before (gen_move_insn (loc, second_reloadreg),
5401 first_output_reload_insn);
5402 pat = gen_move_insn (tmp_reloadreg, loc);
5404 #endif
5405 else
5406 pat = gen_move_insn (reloadreg, second_reloadreg);
5408 emit_insn_before (pat, first_output_reload_insn);
5412 #endif
5414 /* Output the last reload insn. */
5415 if (! special)
5417 #ifdef SECONDARY_MEMORY_NEEDED
5418 /* If we need a memory location to do the move, do it that way. */
5419 if (GET_CODE (old) == REG && REGNO (old) < FIRST_PSEUDO_REGISTER
5420 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (old)),
5421 REGNO_REG_CLASS (REGNO (reloadreg)),
5422 GET_MODE (reloadreg)))
5424 /* Get the memory to use and rewrite both registers to
5425 its mode. */
5426 rtx loc = get_secondary_mem (old, GET_MODE (reloadreg));
5428 if (GET_MODE (loc) != GET_MODE (reloadreg))
5429 reloadreg = gen_rtx (REG, GET_MODE (loc),
5430 REGNO (reloadreg));
5432 if (GET_MODE (loc) != GET_MODE (old))
5433 old = gen_rtx (REG, GET_MODE (loc), REGNO (old));
5435 emit_insn_before (gen_move_insn (loc, reloadreg),
5436 first_output_reload_insn);
5437 emit_insn_before (gen_move_insn (old, loc),
5438 first_output_reload_insn);
5440 else
5441 #endif
5442 emit_insn_before (gen_move_insn (old, reloadreg),
5443 first_output_reload_insn);
5446 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5447 /* If final will look at death notes for this reg,
5448 put one on the last output-reload insn to use it. Similarly
5449 for any secondary register. */
5450 if (PRESERVE_DEATH_INFO_REGNO_P (REGNO (reloadreg)))
5451 for (p = PREV_INSN (first_output_reload_insn);
5452 p != prev_insn; p = PREV_INSN (p))
5453 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5454 && reg_overlap_mentioned_for_reload_p (reloadreg,
5455 PATTERN (p)))
5456 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5457 reloadreg, REG_NOTES (p));
5459 #ifdef SECONDARY_OUTPUT_RELOAD_CLASS
5460 if (! special
5461 && PRESERVE_DEATH_INFO_REGNO_P (REGNO (second_reloadreg)))
5462 for (p = PREV_INSN (first_output_reload_insn);
5463 p != prev_insn; p = PREV_INSN (p))
5464 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
5465 && reg_overlap_mentioned_for_reload_p (second_reloadreg,
5466 PATTERN (p)))
5467 REG_NOTES (p) = gen_rtx (EXPR_LIST, REG_DEAD,
5468 second_reloadreg, REG_NOTES (p));
5469 #endif
5470 #endif
5471 /* Look at all insns we emitted, just to be safe. */
5472 for (p = NEXT_INSN (prev_insn); p != first_output_reload_insn;
5473 p = NEXT_INSN (p))
5474 if (GET_RTX_CLASS (GET_CODE (p)) == 'i')
5476 /* If this output reload doesn't come from a spill reg,
5477 clear any memory of reloaded copies of the pseudo reg.
5478 If this output reload comes from a spill reg,
5479 reg_has_output_reload will make this do nothing. */
5480 note_stores (PATTERN (p), forget_old_reloads_1);
5482 if (reg_mentioned_p (reload_reg_rtx[j], PATTERN (p)))
5483 store_insn = p;
5486 first_output_reload_insn = NEXT_INSN (prev_insn);
5489 if (reload_spill_index[j] >= 0)
5490 new_spill_reg_store[reload_spill_index[j]] = store_insn;
5493 /* Move death notes from INSN
5494 to output-operand-address and output reload insns. */
5495 #ifdef PRESERVE_DEATH_INFO_REGNO_P
5497 rtx insn1;
5498 /* Loop over those insns, last ones first. */
5499 for (insn1 = PREV_INSN (following_insn); insn1 != insn;
5500 insn1 = PREV_INSN (insn1))
5501 if (GET_CODE (insn1) == INSN && GET_CODE (PATTERN (insn1)) == SET)
5503 rtx source = SET_SRC (PATTERN (insn1));
5504 rtx dest = SET_DEST (PATTERN (insn1));
5506 /* The note we will examine next. */
5507 rtx reg_notes = REG_NOTES (insn);
5508 /* The place that pointed to this note. */
5509 rtx *prev_reg_note = &REG_NOTES (insn);
5511 /* If the note is for something used in the source of this
5512 reload insn, or in the output address, move the note. */
5513 while (reg_notes)
5515 rtx next_reg_notes = XEXP (reg_notes, 1);
5516 if (REG_NOTE_KIND (reg_notes) == REG_DEAD
5517 && GET_CODE (XEXP (reg_notes, 0)) == REG
5518 && ((GET_CODE (dest) != REG
5519 && reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
5520 dest))
5521 || reg_overlap_mentioned_for_reload_p (XEXP (reg_notes, 0),
5522 source)))
5524 *prev_reg_note = next_reg_notes;
5525 XEXP (reg_notes, 1) = REG_NOTES (insn1);
5526 REG_NOTES (insn1) = reg_notes;
5528 else
5529 prev_reg_note = &XEXP (reg_notes, 1);
5531 reg_notes = next_reg_notes;
5535 #endif
5537 /* For all the spill regs newly reloaded in this instruction,
5538 record what they were reloaded from, so subsequent instructions
5539 can inherit the reloads.
5541 Update spill_reg_store for the reloads of this insn.
5542 Copy the elements that were updated in the loop above. */
5544 for (j = 0; j < n_reloads; j++)
5546 register int r = reload_order[j];
5547 register int i = reload_spill_index[r];
5549 /* I is nonneg if this reload used one of the spill regs.
5550 If reload_reg_rtx[r] is 0, this is an optional reload
5551 that we opted to ignore. */
5553 if (i >= 0 && reload_reg_rtx[r] != 0)
5555 /* First, clear out memory of what used to be in this spill reg.
5556 If consecutive registers are used, clear them all. */
5557 int nr
5558 = HARD_REGNO_NREGS (spill_regs[i], GET_MODE (reload_reg_rtx[r]));
5559 int k;
5561 for (k = 0; k < nr; k++)
5563 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]] = -1;
5564 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = 0;
5567 /* Maybe the spill reg contains a copy of reload_out. */
5568 if (reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5570 register int nregno = REGNO (reload_out[r]);
5572 spill_reg_store[i] = new_spill_reg_store[i];
5573 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
5575 for (k = 0; k < nr; k++)
5577 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5578 = nregno;
5579 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]] = insn;
5583 /* Maybe the spill reg contains a copy of reload_in. */
5584 else if (reload_out[r] == 0
5585 && reload_in[r] != 0
5586 && (GET_CODE (reload_in[r]) == REG
5587 || GET_CODE (reload_in_reg[r]) == REG))
5589 register int nregno;
5590 if (GET_CODE (reload_in[r]) == REG)
5591 nregno = REGNO (reload_in[r]);
5592 else
5593 nregno = REGNO (reload_in_reg[r]);
5595 /* If there are two separate reloads (one in and one out)
5596 for the same (hard or pseudo) reg,
5597 leave reg_last_reload_reg set
5598 based on the output reload.
5599 Otherwise, set it from this input reload. */
5600 if (!reg_has_output_reload[nregno]
5601 /* But don't do so if another input reload
5602 will clobber this one's value. */
5603 && reload_reg_reaches_end_p (spill_regs[i],
5604 reload_when_needed[r]))
5606 reg_last_reload_reg[nregno] = reload_reg_rtx[r];
5608 /* Unless we inherited this reload, show we haven't
5609 recently done a store. */
5610 if (! reload_inherited[r])
5611 spill_reg_store[i] = 0;
5613 for (k = 0; k < nr; k++)
5615 reg_reloaded_contents[spill_reg_order[spill_regs[i] + k]]
5616 = nregno;
5617 reg_reloaded_insn[spill_reg_order[spill_regs[i] + k]]
5618 = insn;
5624 /* The following if-statement was #if 0'd in 1.34 (or before...).
5625 It's reenabled in 1.35 because supposedly nothing else
5626 deals with this problem. */
5628 /* If a register gets output-reloaded from a non-spill register,
5629 that invalidates any previous reloaded copy of it.
5630 But forget_old_reloads_1 won't get to see it, because
5631 it thinks only about the original insn. So invalidate it here. */
5632 if (i < 0 && reload_out[r] != 0 && GET_CODE (reload_out[r]) == REG)
5634 register int nregno = REGNO (reload_out[r]);
5635 reg_last_reload_reg[nregno] = 0;
5640 /* Emit code before BEFORE_INSN to perform an input reload of IN to RELOADREG.
5641 Returns first insn emitted. */
5644 gen_input_reload (reloadreg, in, before_insn)
5645 rtx reloadreg;
5646 rtx in;
5647 rtx before_insn;
5649 register rtx prev_insn = PREV_INSN (before_insn);
5651 /* How to do this reload can get quite tricky. Normally, we are being
5652 asked to reload a simple operand, such as a MEM, a constant, or a pseudo
5653 register that didn't get a hard register. In that case we can just
5654 call emit_move_insn.
5656 We can also be asked to reload a PLUS that adds either two registers or
5657 a register and a constant or MEM. This can occur during frame pointer
5658 elimination. That case if handled by trying to emit a single insn
5659 to perform the add. If it is not valid, we use a two insn sequence.
5661 Finally, we could be called to handle an 'o' constraint by putting
5662 an address into a register. In that case, we first try to do this
5663 with a named pattern of "reload_load_address". If no such pattern
5664 exists, we just emit a SET insn and hope for the best (it will normally
5665 be valid on machines that use 'o').
5667 This entire process is made complex because reload will never
5668 process the insns we generate here and so we must ensure that
5669 they will fit their constraints and also by the fact that parts of
5670 IN might be being reloaded separately and replaced with spill registers.
5671 Because of this, we are, in some sense, just guessing the right approach
5672 here. The one listed above seems to work.
5674 ??? At some point, this whole thing needs to be rethought. */
5676 if (GET_CODE (in) == PLUS
5677 && GET_CODE (XEXP (in, 0)) == REG
5678 && (GET_CODE (XEXP (in, 1)) == REG
5679 || CONSTANT_P (XEXP (in, 1))
5680 || GET_CODE (XEXP (in, 1)) == MEM))
5682 /* We need to compute the sum of what is either a register and a
5683 constant, a register and memory, or a hard register and a pseudo
5684 register and put it into the reload register. The best possible way
5685 of doing this is if the machine has a three-operand ADD insn that
5686 accepts the required operands.
5688 The simplest approach is to try to generate such an insn and see if it
5689 is recognized and matches its constraints. If so, it can be used.
5691 It might be better not to actually emit the insn unless it is valid,
5692 but we need to pass the insn as an operand to `recog' and
5693 `insn_extract' and it is simpler to emit and then delete the insn if
5694 not valid than to dummy things up. */
5696 rtx op0, op1, tem, insn;
5697 int code;
5699 op0 = find_replacement (&XEXP (in, 0));
5700 op1 = find_replacement (&XEXP (in, 1));
5702 /* Since constraint checking is strict, commutativity won't be
5703 checked, so we need to do that here to avoid spurious failure
5704 if the add instruction is two-address and the second operand
5705 of the add is the same as the reload reg, which is frequently
5706 the case. If the insn would be A = B + A, rearrange it so
5707 it will be A = A + B as constrain_operands expects. */
5709 if (GET_CODE (XEXP (in, 1)) == REG
5710 && REGNO (reloadreg) == REGNO (XEXP (in, 1)))
5711 tem = op0, op0 = op1, op1 = tem;
5713 if (op0 != XEXP (in, 0) || op1 != XEXP (in, 1))
5714 in = gen_rtx (PLUS, GET_MODE (in), op0, op1);
5716 insn = emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in),
5717 before_insn);
5718 code = recog_memoized (insn);
5720 if (code >= 0)
5722 insn_extract (insn);
5723 /* We want constrain operands to treat this insn strictly in
5724 its validity determination, i.e., the way it would after reload
5725 has completed. */
5726 if (constrain_operands (code, 1))
5727 return insn;
5730 if (PREV_INSN (insn))
5731 NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
5732 if (NEXT_INSN (insn))
5733 PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
5735 /* If that failed, we must use a conservative two-insn sequence.
5736 use move to copy constant, MEM, or pseudo register to the reload
5737 register since "move" will be able to handle an arbitrary operand,
5738 unlike add which can't, in general. Then add the registers.
5740 If there is another way to do this for a specific machine, a
5741 DEFINE_PEEPHOLE should be specified that recognizes the sequence
5742 we emit below. */
5744 if (CONSTANT_P (op1) || GET_CODE (op1) == MEM
5745 || (GET_CODE (op1) == REG
5746 && REGNO (op1) >= FIRST_PSEUDO_REGISTER))
5747 tem = op0, op0 = op1, op1 = tem;
5749 emit_insn_before (gen_move_insn (reloadreg, op0), before_insn);
5750 emit_insn_before (gen_add2_insn (reloadreg, op1), before_insn);
5753 #ifdef SECONDARY_MEMORY_NEEDED
5754 /* If we need a memory location to do the move, do it that way. */
5755 else if (GET_CODE (in) == REG && REGNO (in) < FIRST_PSEUDO_REGISTER
5756 && SECONDARY_MEMORY_NEEDED (REGNO_REG_CLASS (REGNO (in)),
5757 REGNO_REG_CLASS (REGNO (reloadreg)),
5758 GET_MODE (reloadreg)))
5760 /* Get the memory to use and rewrite both registers to its mode. */
5761 rtx loc = get_secondary_mem (in, GET_MODE (reloadreg));
5763 if (GET_MODE (loc) != GET_MODE (reloadreg))
5764 reloadreg = gen_rtx (REG, GET_MODE (loc), REGNO (reloadreg));
5766 if (GET_MODE (loc) != GET_MODE (in))
5767 in = gen_rtx (REG, GET_MODE (loc), REGNO (in));
5769 emit_insn_before (gen_move_insn (loc, in), before_insn);
5770 emit_insn_before (gen_move_insn (reloadreg, loc), before_insn);
5772 #endif
5774 /* If IN is a simple operand, use gen_move_insn. */
5775 else if (GET_RTX_CLASS (GET_CODE (in)) == 'o' || GET_CODE (in) == SUBREG)
5776 emit_insn_before (gen_move_insn (reloadreg, in), before_insn);
5778 #ifdef HAVE_reload_load_address
5779 else if (HAVE_reload_load_address)
5780 emit_insn_before (gen_reload_load_address (reloadreg, in), before_insn);
5781 #endif
5783 /* Otherwise, just write (set REGLOADREG IN) and hope for the best. */
5784 else
5785 emit_insn_before (gen_rtx (SET, VOIDmode, reloadreg, in), before_insn);
5787 /* Return the first insn emitted.
5788 We can not just return PREV_INSN (before_insn), because there may have
5789 been multiple instructions emitted. Also note that gen_move_insn may
5790 emit more than one insn itself, so we can not assume that there is one
5791 insn emitted per emit_insn_before call. */
5793 return NEXT_INSN (prev_insn);
5796 /* Delete a previously made output-reload
5797 whose result we now believe is not needed.
5798 First we double-check.
5800 INSN is the insn now being processed.
5801 OUTPUT_RELOAD_INSN is the insn of the output reload.
5802 J is the reload-number for this insn. */
5804 static void
5805 delete_output_reload (insn, j, output_reload_insn)
5806 rtx insn;
5807 int j;
5808 rtx output_reload_insn;
5810 register rtx i1;
5812 /* Get the raw pseudo-register referred to. */
5814 rtx reg = reload_in[j];
5815 while (GET_CODE (reg) == SUBREG)
5816 reg = SUBREG_REG (reg);
5818 /* If the pseudo-reg we are reloading is no longer referenced
5819 anywhere between the store into it and here,
5820 and no jumps or labels intervene, then the value can get
5821 here through the reload reg alone.
5822 Otherwise, give up--return. */
5823 for (i1 = NEXT_INSN (output_reload_insn);
5824 i1 != insn; i1 = NEXT_INSN (i1))
5826 if (GET_CODE (i1) == CODE_LABEL || GET_CODE (i1) == JUMP_INSN)
5827 return;
5828 if ((GET_CODE (i1) == INSN || GET_CODE (i1) == CALL_INSN)
5829 && reg_mentioned_p (reg, PATTERN (i1)))
5830 return;
5833 /* If this insn will store in the pseudo again,
5834 the previous store can be removed. */
5835 if (reload_out[j] == reload_in[j])
5836 delete_insn (output_reload_insn);
5838 /* See if the pseudo reg has been completely replaced
5839 with reload regs. If so, delete the store insn
5840 and forget we had a stack slot for the pseudo. */
5841 else if (reg_n_deaths[REGNO (reg)] == 1
5842 && reg_basic_block[REGNO (reg)] >= 0
5843 && find_regno_note (insn, REG_DEAD, REGNO (reg)))
5845 rtx i2;
5847 /* We know that it was used only between here
5848 and the beginning of the current basic block.
5849 (We also know that the last use before INSN was
5850 the output reload we are thinking of deleting, but never mind that.)
5851 Search that range; see if any ref remains. */
5852 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5854 rtx set = single_set (i2);
5856 /* Uses which just store in the pseudo don't count,
5857 since if they are the only uses, they are dead. */
5858 if (set != 0 && SET_DEST (set) == reg)
5859 continue;
5860 if (GET_CODE (i2) == CODE_LABEL
5861 || GET_CODE (i2) == JUMP_INSN)
5862 break;
5863 if ((GET_CODE (i2) == INSN || GET_CODE (i2) == CALL_INSN)
5864 && reg_mentioned_p (reg, PATTERN (i2)))
5865 /* Some other ref remains;
5866 we can't do anything. */
5867 return;
5870 /* Delete the now-dead stores into this pseudo. */
5871 for (i2 = PREV_INSN (insn); i2; i2 = PREV_INSN (i2))
5873 rtx set = single_set (i2);
5875 if (set != 0 && SET_DEST (set) == reg)
5876 delete_insn (i2);
5877 if (GET_CODE (i2) == CODE_LABEL
5878 || GET_CODE (i2) == JUMP_INSN)
5879 break;
5882 /* For the debugging info,
5883 say the pseudo lives in this reload reg. */
5884 reg_renumber[REGNO (reg)] = REGNO (reload_reg_rtx[j]);
5885 alter_reg (REGNO (reg), -1);
5890 /* Output reload-insns to reload VALUE into RELOADREG.
5891 VALUE is an autoincrement or autodecrement RTX whose operand
5892 is a register or memory location;
5893 so reloading involves incrementing that location.
5895 INC_AMOUNT is the number to increment or decrement by (always positive).
5896 This cannot be deduced from VALUE.
5898 INSN is the insn before which the new insns should be emitted.
5900 The return value is the first of the insns emitted. */
5902 static rtx
5903 inc_for_reload (reloadreg, value, inc_amount, insn)
5904 rtx reloadreg;
5905 rtx value;
5906 int inc_amount;
5907 rtx insn;
5909 /* REG or MEM to be copied and incremented. */
5910 rtx incloc = XEXP (value, 0);
5911 /* Nonzero if increment after copying. */
5912 int post = (GET_CODE (value) == POST_DEC || GET_CODE (value) == POST_INC);
5913 rtx prev = PREV_INSN (insn);
5914 rtx inc;
5915 rtx add_insn;
5916 int code;
5918 /* No hard register is equivalent to this register after
5919 inc/dec operation. If REG_LAST_RELOAD_REG were non-zero,
5920 we could inc/dec that register as well (maybe even using it for
5921 the source), but I'm not sure it's worth worrying about. */
5922 if (GET_CODE (incloc) == REG)
5923 reg_last_reload_reg[REGNO (incloc)] = 0;
5925 if (GET_CODE (value) == PRE_DEC || GET_CODE (value) == POST_DEC)
5926 inc_amount = - inc_amount;
5928 inc = GEN_INT (inc_amount);
5930 /* If this is post-increment, first copy the location to the reload reg. */
5931 if (post)
5932 emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
5934 /* See if we can directly increment INCLOC. Use a method similar to that
5935 in gen_input_reload. */
5937 add_insn = emit_insn_before (gen_rtx (SET, VOIDmode, incloc,
5938 gen_rtx (PLUS, GET_MODE (incloc),
5939 incloc, inc)), insn);
5941 code = recog_memoized (add_insn);
5942 if (code >= 0)
5944 insn_extract (add_insn);
5945 if (constrain_operands (code, 1))
5947 /* If this is a pre-increment and we have incremented the value
5948 where it lives, copy the incremented value to RELOADREG to
5949 be used as an address. */
5951 if (! post)
5952 emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
5953 return NEXT_INSN (prev);
5957 if (PREV_INSN (add_insn))
5958 NEXT_INSN (PREV_INSN (add_insn)) = NEXT_INSN (add_insn);
5959 if (NEXT_INSN (add_insn))
5960 PREV_INSN (NEXT_INSN (add_insn)) = PREV_INSN (add_insn);
5962 /* If couldn't do the increment directly, must increment in RELOADREG.
5963 The way we do this depends on whether this is pre- or post-increment.
5964 For pre-increment, copy INCLOC to the reload register, increment it
5965 there, then save back. */
5967 if (! post)
5969 emit_insn_before (gen_move_insn (reloadreg, incloc), insn);
5970 emit_insn_before (gen_add2_insn (reloadreg, inc), insn);
5971 emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
5973 else
5975 /* Postincrement.
5976 Because this might be a jump insn or a compare, and because RELOADREG
5977 may not be available after the insn in an input reload, we must do
5978 the incrementation before the insn being reloaded for.
5980 We have already copied INCLOC to RELOADREG. Increment the copy in
5981 RELOADREG, save that back, then decrement RELOADREG so it has
5982 the original value. */
5984 emit_insn_before (gen_add2_insn (reloadreg, inc), insn);
5985 emit_insn_before (gen_move_insn (incloc, reloadreg), insn);
5986 emit_insn_before (gen_add2_insn (reloadreg, GEN_INT (-inc_amount)),
5987 insn);
5990 return NEXT_INSN (prev);
5993 /* Return 1 if we are certain that the constraint-string STRING allows
5994 the hard register REG. Return 0 if we can't be sure of this. */
5996 static int
5997 constraint_accepts_reg_p (string, reg)
5998 char *string;
5999 rtx reg;
6001 int value = 0;
6002 int regno = true_regnum (reg);
6003 int c;
6005 /* Initialize for first alternative. */
6006 value = 0;
6007 /* Check that each alternative contains `g' or `r'. */
6008 while (1)
6009 switch (c = *string++)
6011 case 0:
6012 /* If an alternative lacks `g' or `r', we lose. */
6013 return value;
6014 case ',':
6015 /* If an alternative lacks `g' or `r', we lose. */
6016 if (value == 0)
6017 return 0;
6018 /* Initialize for next alternative. */
6019 value = 0;
6020 break;
6021 case 'g':
6022 case 'r':
6023 /* Any general reg wins for this alternative. */
6024 if (TEST_HARD_REG_BIT (reg_class_contents[(int) GENERAL_REGS], regno))
6025 value = 1;
6026 break;
6027 default:
6028 /* Any reg in specified class wins for this alternative. */
6030 enum reg_class class = REG_CLASS_FROM_LETTER (c);
6032 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], regno))
6033 value = 1;
6038 /* Return the number of places FIND appears within X, but don't count
6039 an occurrence if some SET_DEST is FIND. */
6041 static int
6042 count_occurrences (x, find)
6043 register rtx x, find;
6045 register int i, j;
6046 register enum rtx_code code;
6047 register char *format_ptr;
6048 int count;
6050 if (x == find)
6051 return 1;
6052 if (x == 0)
6053 return 0;
6055 code = GET_CODE (x);
6057 switch (code)
6059 case REG:
6060 case QUEUED:
6061 case CONST_INT:
6062 case CONST_DOUBLE:
6063 case SYMBOL_REF:
6064 case CODE_LABEL:
6065 case PC:
6066 case CC0:
6067 return 0;
6069 case SET:
6070 if (SET_DEST (x) == find)
6071 return count_occurrences (SET_SRC (x), find);
6072 break;
6075 format_ptr = GET_RTX_FORMAT (code);
6076 count = 0;
6078 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6080 switch (*format_ptr++)
6082 case 'e':
6083 count += count_occurrences (XEXP (x, i), find);
6084 break;
6086 case 'E':
6087 if (XVEC (x, i) != NULL)
6089 for (j = 0; j < XVECLEN (x, i); j++)
6090 count += count_occurrences (XVECEXP (x, i, j), find);
6092 break;
6095 return count;