1 /* Save and restore call-clobbered registers which are live across a call.
2 Copyright (C) 1989, 1992, 1994, 1995, 1997 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)
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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "insn-config.h"
26 #include "hard-reg-set.h"
28 #include "basic-block.h"
33 #define MAX_MOVE_MAX MOVE_MAX
36 #ifndef MIN_UNITS_PER_WORD
37 #define MIN_UNITS_PER_WORD UNITS_PER_WORD
40 /* Modes for each hard register that we can save. The smallest mode is wide
41 enough to save the entire contents of the register. When saving the
42 register because it is live we first try to save in multi-register modes.
43 If that is not possible the save is done one register at a time. */
45 static enum machine_mode
46 regno_save_mode[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
48 /* For each hard register, a place on the stack where it can be saved,
52 regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
54 /* We will only make a register eligible for caller-save if it can be
55 saved in its widest mode with a simple SET insn as long as the memory
56 address is valid. We record the INSN_CODE is those insns here since
57 when we emit them, the addresses might not be valid, so they might not
61 reg_save_code[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
63 reg_restore_code[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];
65 /* Set of hard regs currently live (during scan of all insns). */
67 static HARD_REG_SET hard_regs_live;
69 /* Set of hard regs currently residing in save area (during insn scan). */
71 static HARD_REG_SET hard_regs_saved;
73 /* Set of hard regs which need to be restored before referenced. */
75 static HARD_REG_SET hard_regs_need_restore;
77 /* Number of registers currently in hard_regs_saved. */
81 static void set_reg_live PROTO((rtx, rtx));
82 static void clear_reg_live PROTO((rtx));
83 static void restore_referenced_regs PROTO((rtx, rtx, enum machine_mode));
84 static int insert_save_restore PROTO((rtx, int, int,
85 enum machine_mode, int));
87 /* Initialize for caller-save.
89 Look at all the hard registers that are used by a call and for which
90 regclass.c has not already excluded from being used across a call.
92 Ensure that we can find a mode to save the register and that there is a
93 simple insn to save and restore the register. This latter check avoids
94 problems that would occur if we tried to save the MQ register of some
95 machines directly into memory. */
100 char *first_obj = (char *) oballoc (0);
106 /* First find all the registers that we need to deal with and all
107 the modes that they can have. If we can't find a mode to use,
108 we can't have the register live over calls. */
110 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
112 if (call_used_regs[i] && ! call_fixed_regs[i])
114 for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
116 regno_save_mode[i][j] = choose_hard_reg_mode (i, j);
117 if (regno_save_mode[i][j] == VOIDmode && j == 1)
119 call_fixed_regs[i] = 1;
120 SET_HARD_REG_BIT (call_fixed_reg_set, i);
125 regno_save_mode[i][1] = VOIDmode;
128 /* The following code tries to approximate the conditions under which
129 we can easily save and restore a register without scratch registers or
130 other complexities. It will usually work, except under conditions where
131 the validity of an insn operand is dependent on the address offset.
132 No such cases are currently known.
134 We first find a typical offset from some BASE_REG_CLASS register.
135 This address is chosen by finding the first register in the class
136 and by finding the smallest power of two that is a valid offset from
137 that register in every mode we will use to save registers. */
139 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
140 if (TEST_HARD_REG_BIT (reg_class_contents[(int) BASE_REG_CLASS], i))
143 if (i == FIRST_PSEUDO_REGISTER)
146 addr_reg = gen_rtx (REG, Pmode, i);
148 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
150 address = gen_rtx (PLUS, Pmode, addr_reg, GEN_INT (offset));
152 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
153 if (regno_save_mode[i][1] != VOIDmode
154 && ! strict_memory_address_p (regno_save_mode[i][1], address))
157 if (i == FIRST_PSEUDO_REGISTER)
161 /* If we didn't find a valid address, we must use register indirect. */
165 /* Next we try to form an insn to save and restore the register. We
166 see if such an insn is recognized and meets its constraints. */
170 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
171 for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
172 if (regno_save_mode[i][j] != VOIDmode)
174 rtx mem = gen_rtx (MEM, regno_save_mode[i][j], address);
175 rtx reg = gen_rtx (REG, regno_save_mode[i][j], i);
176 rtx savepat = gen_rtx (SET, VOIDmode, mem, reg);
177 rtx restpat = gen_rtx (SET, VOIDmode, reg, mem);
178 rtx saveinsn = emit_insn (savepat);
179 rtx restinsn = emit_insn (restpat);
182 reg_save_code[i][j] = recog_memoized (saveinsn);
183 reg_restore_code[i][j] = recog_memoized (restinsn);
185 /* Now extract both insns and see if we can meet their
187 ok = (reg_save_code[i][j] != -1 && reg_restore_code[i][j] != -1);
190 insn_extract (saveinsn);
191 ok = constrain_operands (reg_save_code[i][j], 1);
192 insn_extract (restinsn);
193 ok &= constrain_operands (reg_restore_code[i][j], 1);
198 regno_save_mode[i][j] = VOIDmode;
201 call_fixed_regs[i] = 1;
202 SET_HARD_REG_BIT (call_fixed_reg_set, i);
212 /* Initialize save areas by showing that we haven't allocated any yet. */
219 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
220 for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
221 regno_save_mem[i][j] = 0;
224 /* Allocate save areas for any hard registers that might need saving.
225 We take a conservative approach here and look for call-clobbered hard
226 registers that are assigned to pseudos that cross calls. This may
227 overestimate slightly (especially if some of these registers are later
228 used as spill registers), but it should not be significant.
230 Then perform register elimination in the addresses of the save area
231 locations; return 1 if all eliminated addresses are strictly valid.
232 We assume that our caller has set up the elimination table to the
233 worst (largest) possible offsets.
235 Set *PCHANGED to 1 if we had to allocate some memory for the save area.
239 In the fallback case we should iterate backwards across all possible
240 modes for the save, choosing the largest available one instead of
241 falling back to the smallest mode immediately. (eg TF -> DF -> SF).
243 We do not try to use "move multiple" instructions that exist
244 on some machines (such as the 68k moveml). It could be a win to try
245 and use them when possible. The hard part is doing it in a way that is
246 machine independent since they might be saving non-consecutive
247 registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */
250 setup_save_areas (pchanged)
254 HARD_REG_SET hard_regs_used;
258 /* Allocate space in the save area for the largest multi-register
259 pseudos first, then work backwards to single register
262 /* Find and record all call-used hard-registers in this function. */
263 CLEAR_HARD_REG_SET (hard_regs_used);
264 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
265 if (reg_renumber[i] >= 0 && REG_N_CALLS_CROSSED (i) > 0)
267 int regno = reg_renumber[i];
269 = regno + HARD_REGNO_NREGS (regno, GET_MODE (regno_reg_rtx[i]));
270 int nregs = endregno - regno;
272 for (j = 0; j < nregs; j++)
274 if (call_used_regs[regno+j])
275 SET_HARD_REG_BIT (hard_regs_used, regno+j);
279 /* Now run through all the call-used hard-registers and allocate
280 space for them in the caller-save area. Try to allocate space
281 in a manner which allows multi-register saves/restores to be done. */
283 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
284 for (j = MOVE_MAX / UNITS_PER_WORD; j > 0; j--)
289 /* If no mode exists for this size, try another. Also break out
290 if we have already saved this hard register. */
291 if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0)
294 /* See if any register in this group has been saved. */
296 for (k = 0; k < j; k++)
297 if (regno_save_mem[i + k][1])
305 for (k = 0; k < j; k++)
308 ok &= (TEST_HARD_REG_BIT (hard_regs_used, regno) != 0);
311 /* We have found an acceptable mode to store in. */
316 = assign_stack_local (regno_save_mode[i][j],
317 GET_MODE_SIZE (regno_save_mode[i][j]), 0);
319 /* Setup single word save area just in case... */
320 for (k = 0; k < j; k++)
322 /* This should not depend on WORDS_BIG_ENDIAN.
323 The order of words in regs is the same as in memory. */
324 rtx temp = gen_rtx (MEM, regno_save_mode[i+k][1],
325 XEXP (regno_save_mem[i][j], 0));
327 regno_save_mem[i+k][1]
328 = adj_offsettable_operand (temp, k * UNITS_PER_WORD);
334 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
335 for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++)
336 if (regno_save_mem[i][j] != 0)
337 ok &= strict_memory_address_p (GET_MODE (regno_save_mem[i][j]),
338 XEXP (eliminate_regs (regno_save_mem[i][j], 0, NULL_RTX, 1), 0));
343 /* Find the places where hard regs are live across calls and save them.
345 INSN_MODE is the mode to assign to any insns that we add. This is used
346 by reload to determine whether or not reloads or register eliminations
347 need be done on these insns. */
350 save_call_clobbered_regs (insn_mode)
351 enum machine_mode insn_mode;
356 for (b = 0; b < n_basic_blocks; b++)
358 regset regs_live = basic_block_live_at_start[b];
359 rtx prev_block_last = PREV_INSN (basic_block_head[b]);
363 /* Compute hard regs live at start of block -- this is the
364 real hard regs marked live, plus live pseudo regs that
365 have been renumbered to hard regs. No registers have yet been
366 saved because we restore all of them before the end of the basic
369 REG_SET_TO_HARD_REG_SET (hard_regs_live, regs_live);
370 CLEAR_HARD_REG_SET (hard_regs_saved);
371 CLEAR_HARD_REG_SET (hard_regs_need_restore);
374 EXECUTE_IF_SET_IN_REG_SET (regs_live, 0, i,
376 if ((regno = reg_renumber[i]) >= 0)
378 j < regno + HARD_REGNO_NREGS (regno,
379 PSEUDO_REGNO_MODE (i));
381 SET_HARD_REG_BIT (hard_regs_live, j);
384 /* Now scan the insns in the block, keeping track of what hard
385 regs are live as we go. When we see a call, save the live
386 call-clobbered hard regs. */
388 for (insn = basic_block_head[b]; ; insn = NEXT_INSN (insn))
390 RTX_CODE code = GET_CODE (insn);
392 if (GET_RTX_CLASS (code) == 'i')
396 /* If some registers have been saved, see if INSN references
397 any of them. We must restore them before the insn if so. */
400 restore_referenced_regs (PATTERN (insn), insn, insn_mode);
402 /* NB: the normal procedure is to first enliven any
403 registers set by insn, then deaden any registers that
404 had their last use at insn. This is incorrect now,
405 since multiple pseudos may have been mapped to the
406 same hard reg, and the death notes are ambiguous. So
407 it must be done in the other, safe, order. */
409 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
410 if (REG_NOTE_KIND (link) == REG_DEAD)
411 clear_reg_live (XEXP (link, 0));
413 /* When we reach a call, we need to save all registers that are
414 live, call-used, not fixed, and not already saved. We must
415 test at this point because registers that die in a CALL_INSN
416 are not live across the call and likewise for registers that
417 are born in the CALL_INSN.
419 If registers are filled with parameters for this function,
420 and some of these are also being set by this function, then
421 they will not appear to die (no REG_DEAD note for them),
422 to check if in fact they do, collect the set registers in
423 hard_regs_live first. */
425 if (code == CALL_INSN)
427 HARD_REG_SET this_call_sets;
429 HARD_REG_SET old_hard_regs_live;
431 /* Save the hard_regs_live information. */
432 COPY_HARD_REG_SET (old_hard_regs_live, hard_regs_live);
434 /* Now calculate hard_regs_live for this CALL_INSN
436 CLEAR_HARD_REG_SET (hard_regs_live);
437 note_stores (PATTERN (insn), set_reg_live);
438 COPY_HARD_REG_SET (this_call_sets, hard_regs_live);
440 /* Restore the hard_regs_live information. */
441 COPY_HARD_REG_SET (hard_regs_live, old_hard_regs_live);
444 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
445 if (call_used_regs[regno] && ! call_fixed_regs[regno]
446 && TEST_HARD_REG_BIT (hard_regs_live, regno)
447 /* It must not be set by this instruction. */
448 && ! TEST_HARD_REG_BIT (this_call_sets, regno)
449 && ! TEST_HARD_REG_BIT (hard_regs_saved, regno))
450 regno += insert_save_restore (insn, 1, regno,
453 /* Put the information for this CALL_INSN on top of what
455 IOR_HARD_REG_SET (hard_regs_live, this_call_sets);
456 COPY_HARD_REG_SET (hard_regs_need_restore, hard_regs_saved);
458 /* Must recompute n_regs_saved. */
460 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
461 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
466 note_stores (PATTERN (insn), set_reg_live);
468 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
469 if (REG_NOTE_KIND (link) == REG_INC)
470 set_reg_live (XEXP (link, 0), NULL_RTX);
474 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
475 if (REG_NOTE_KIND (link) == REG_UNUSED)
476 clear_reg_live (XEXP (link, 0));
479 if (insn == basic_block_end[b])
483 /* At the end of the basic block, we must restore any registers that
484 remain saved. If the last insn in the block is a JUMP_INSN, put
485 the restore before the insn, otherwise, put it after the insn. */
488 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
489 if (TEST_HARD_REG_BIT (hard_regs_need_restore, regno))
490 regno += insert_save_restore ((GET_CODE (insn) == JUMP_INSN
491 ? insn : NEXT_INSN (insn)), 0,
492 regno, insn_mode, MOVE_MAX / UNITS_PER_WORD);
494 /* If we added any insns at the start of the block, update the start
495 of the block to point at those insns. */
496 basic_block_head[b] = NEXT_INSN (prev_block_last);
500 /* Here from note_stores when an insn stores a value in a register.
501 Set the proper bit or bits in hard_regs_live. All pseudos that have
502 been assigned hard regs have had their register number changed already,
503 so we can ignore pseudos. */
506 set_reg_live (reg, setter)
509 register int regno, endregno, i;
510 enum machine_mode mode = GET_MODE (reg);
513 if (GET_CODE (reg) == SUBREG)
515 word = SUBREG_WORD (reg);
516 reg = SUBREG_REG (reg);
519 if (GET_CODE (reg) != REG || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
522 regno = REGNO (reg) + word;
523 endregno = regno + HARD_REGNO_NREGS (regno, mode);
525 for (i = regno; i < endregno; i++)
527 SET_HARD_REG_BIT (hard_regs_live, i);
528 CLEAR_HARD_REG_BIT (hard_regs_saved, i);
529 CLEAR_HARD_REG_BIT (hard_regs_need_restore, i);
533 /* Here when a REG_DEAD note records the last use of a reg. Clear
534 the appropriate bit or bits in hard_regs_live. Again we can ignore
541 register int regno, endregno, i;
543 if (GET_CODE (reg) != REG || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
547 endregno= regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
549 for (i = regno; i < endregno; i++)
551 CLEAR_HARD_REG_BIT (hard_regs_live, i);
552 CLEAR_HARD_REG_BIT (hard_regs_need_restore, i);
553 CLEAR_HARD_REG_BIT (hard_regs_saved, i);
557 /* If any register currently residing in the save area is referenced in X,
558 which is part of INSN, emit code to restore the register in front of INSN.
559 INSN_MODE is the mode to assign to any insns that we add. */
562 restore_referenced_regs (x, insn, insn_mode)
565 enum machine_mode insn_mode;
567 enum rtx_code code = GET_CODE (x);
576 int regno = REGNO (x);
578 /* If this is a pseudo, scan its memory location, since it might
579 involve the use of another register, which might be saved. */
581 if (regno >= FIRST_PSEUDO_REGISTER
582 && reg_equiv_mem[regno] != 0)
583 restore_referenced_regs (XEXP (reg_equiv_mem[regno], 0),
585 else if (regno >= FIRST_PSEUDO_REGISTER
586 && reg_equiv_address[regno] != 0)
587 restore_referenced_regs (reg_equiv_address[regno],
590 /* Otherwise if this is a hard register, restore any piece of it that
591 is currently saved. */
593 else if (regno < FIRST_PSEUDO_REGISTER)
595 int numregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
596 /* Save at most SAVEREGS at a time. This can not be larger than
597 MOVE_MAX, because that causes insert_save_restore to fail. */
598 int saveregs = MIN (numregs, MOVE_MAX / UNITS_PER_WORD);
599 int endregno = regno + numregs;
601 for (i = regno; i < endregno; i++)
602 if (TEST_HARD_REG_BIT (hard_regs_need_restore, i))
603 i += insert_save_restore (insn, 0, i, insn_mode, saveregs);
609 fmt = GET_RTX_FORMAT (code);
610 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
613 restore_referenced_regs (XEXP (x, i), insn, insn_mode);
614 else if (fmt[i] == 'E')
615 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
616 restore_referenced_regs (XVECEXP (x, i, j), insn, insn_mode);
620 /* Insert a sequence of insns to save or restore, SAVE_P says which,
621 REGNO. Place these insns in front of INSN. INSN_MODE is the mode
622 to assign to these insns. MAXRESTORE is the maximum number of registers
623 which should be restored during this call (when SAVE_P == 0). It should
624 never be less than 1 since we only work with entire registers.
626 Note that we have verified in init_caller_save that we can do this
627 with a simple SET, so use it. Set INSN_CODE to what we save there
628 since the address might not be valid so the insn might not be recognized.
629 These insns will be reloaded and have register elimination done by
630 find_reload, so we need not worry about that here.
632 Return the extra number of registers saved. */
635 insert_save_restore (insn, save_p, regno, insn_mode, maxrestore)
639 enum machine_mode insn_mode;
646 /* A common failure mode if register status is not correct in the RTL
647 is for this routine to be called with a REGNO we didn't expect to
648 save. That will cause us to write an insn with a (nil) SET_DEST
649 or SET_SRC. Instead of doing so and causing a crash later, check
650 for this common case and abort here instead. This will remove one
651 step in debugging such problems. */
653 if (regno_save_mem[regno][1] == 0)
657 /* If INSN references CC0, put our insns in front of the insn that sets
658 CC0. This is always safe, since the only way we could be passed an
659 insn that references CC0 is for a restore, and doing a restore earlier
660 isn't a problem. We do, however, assume here that CALL_INSNs don't
661 reference CC0. Guard against non-INSN's like CODE_LABEL. */
663 if ((GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
664 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
665 insn = prev_nonnote_insn (insn);
668 /* Get the pattern to emit and update our status. */
674 /* See if we can save several registers with a single instruction.
675 Work backwards to the single register case. */
676 for (i = MOVE_MAX / UNITS_PER_WORD; i > 0; i--)
679 if (regno_save_mem[regno][i] != 0)
680 for (j = 0; j < i; j++)
682 if (! call_used_regs[regno + j] || call_fixed_regs[regno + j]
683 || ! TEST_HARD_REG_BIT (hard_regs_live, regno + j)
684 || TEST_HARD_REG_BIT (hard_regs_saved, regno + j))
690 /* Must do this one save at a time */
694 pat = gen_rtx (SET, VOIDmode, regno_save_mem[regno][i],
695 gen_rtx (REG, GET_MODE (regno_save_mem[regno][i]), regno));
696 code = reg_save_code[regno][i];
698 /* Set hard_regs_saved for all the registers we saved. */
699 for (k = 0; k < i; k++)
701 SET_HARD_REG_BIT (hard_regs_saved, regno + k);
702 SET_HARD_REG_BIT (hard_regs_need_restore, regno + k);
715 /* See if we can restore `maxrestore' registers at once. Work
716 backwards to the single register case. */
717 for (i = maxrestore; i > 0; i--)
720 if (regno_save_mem[regno][i])
721 for (j = 0; j < i; j++)
723 if (! TEST_HARD_REG_BIT (hard_regs_need_restore, regno + j))
729 /* Must do this one restore at a time */
733 pat = gen_rtx (SET, VOIDmode,
734 gen_rtx (REG, GET_MODE (regno_save_mem[regno][i]),
736 regno_save_mem[regno][i]);
737 code = reg_restore_code[regno][i];
740 /* Clear status for all registers we restored. */
741 for (k = 0; k < i; k++)
743 CLEAR_HARD_REG_BIT (hard_regs_need_restore, regno + k);
751 /* Emit the insn and set the code and mode. */
753 insn = emit_insn_before (pat, insn);
754 PUT_MODE (insn, insn_mode);
755 INSN_CODE (insn) = code;
757 /* Tell our callers how many extra registers we saved/restored */