1 /* Save and restore call-clobbered registers which are live across a call.
2 Copyright (C) 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)
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. */
22 #include "insn-config.h"
25 #include "hard-reg-set.h"
27 #include "basic-block.h"
31 /* A mode for each hard register that we can save. This mode is wide enough
32 to save the entire contents of the register and will be used whenever the
33 register must be saved because it is live. */
35 static enum machine_mode regno_save_mode[FIRST_PSEUDO_REGISTER];
37 /* For each hard register, a place on the stack where it can be saved,
40 static rtx regno_save_mem[FIRST_PSEUDO_REGISTER];
42 /* We will only make a register eligible for caller-save if it can be
43 saved in its widest mode with a simple SET insn as long as the memory
44 address is valid. We record the INSN_CODE is those insns here since
45 when we emit them, the addresses might not be valid, so they might not
48 static enum insn_code reg_save_code[FIRST_PSEUDO_REGISTER];
49 static enum insn_code reg_restore_code[FIRST_PSEUDO_REGISTER];
51 /* Set of hard regs currently live (during scan of all insns). */
53 static HARD_REG_SET hard_regs_live;
55 /* Set of hard regs currently residing in save area (during insn scan). */
57 static HARD_REG_SET hard_regs_saved;
59 /* Number of registers currently in hard_regs_saved. */
63 static void set_reg_live ();
64 static void clear_reg_live ();
65 static void restore_referenced_regs ();
66 static void insert_save_restore ();
68 /* Return a machine mode that is legitimate for hard reg REGNO and large
69 enough to save the whole register. If we can't find one,
72 static enum machine_mode
73 choose_hard_reg_mode (regno)
76 enum machine_mode found_mode = VOIDmode, mode;
78 /* We first look for the largest integer mode that can be validly
79 held in REGNO. If none, we look for the largest floating-point mode.
80 If we still didn't find a valid mode, try CCmode. */
82 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
83 mode = GET_MODE_WIDER_MODE (mode))
84 if (HARD_REGNO_NREGS (regno, mode) == 1
85 && HARD_REGNO_MODE_OK (regno, mode))
88 if (found_mode != VOIDmode)
91 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
92 mode = GET_MODE_WIDER_MODE (mode))
93 if (HARD_REGNO_NREGS (regno, mode) == 1
94 && HARD_REGNO_MODE_OK (regno, mode))
97 if (found_mode != VOIDmode)
100 if (HARD_REGNO_NREGS (regno, CCmode) == 1
101 && HARD_REGNO_MODE_OK (regno, CCmode))
104 /* We can't find a mode valid for this register. */
108 /* Initialize for caller-save.
110 Look at all the hard registers that are used by a call and for which
111 regclass.c has not already excluded from being used across a call.
113 Ensure that we can find a mode to save the register and that there is a
114 simple insn to save and restore the register. This latter check avoids
115 problems that would occur if we tried to save the MQ register of some
116 machines directly into memory. */
121 char *first_obj = (char *) oballoc (0);
127 /* First find all the registers that we need to deal with and all
128 the modes that they can have. If we can't find a mode to use,
129 we can't have the register live over calls. */
131 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
133 if (call_used_regs[i] && ! call_fixed_regs[i])
135 regno_save_mode[i] = choose_hard_reg_mode (i);
136 if (regno_save_mode[i] == VOIDmode)
138 call_fixed_regs[i] = 1;
139 SET_HARD_REG_BIT (call_fixed_reg_set, i);
143 regno_save_mode[i] = VOIDmode;
146 /* The following code tries to approximate the conditions under which
147 we can easily save and restore a register without scratch registers or
148 other complexities. It will usually work, except under conditions where
149 the validity of an insn operand is dependent on the address offset.
150 No such cases are currently known.
152 We first find a typical offset from some BASE_REG_CLASS register.
153 This address is chosen by finding the first register in the class
154 and by finding the smallest power of two that is a valid offset from
155 that register in every mode we will use to save registers. */
157 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
158 if (TEST_HARD_REG_BIT (reg_class_contents[(int) BASE_REG_CLASS], i))
161 if (i == FIRST_PSEUDO_REGISTER)
164 addr_reg = gen_rtx (REG, Pmode, i);
166 for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1)
168 address = gen_rtx (PLUS, Pmode, addr_reg,
169 gen_rtx (CONST_INT, VOIDmode, offset));
171 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
172 if (regno_save_mode[i] != VOIDmode
173 && ! strict_memory_address_p (regno_save_mode[i], address))
176 if (i == FIRST_PSEUDO_REGISTER)
180 /* If we didn't find a valid address, we must use register indirect. */
184 /* Next we try to form an insn to save and restore the register. We
185 see if such an insn is recognized and meets its constraints. */
189 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
190 if (regno_save_mode[i] != VOIDmode)
192 rtx mem = gen_rtx (MEM, regno_save_mode[i], address);
193 rtx reg = gen_rtx (REG, regno_save_mode[i], i);
194 rtx savepat = gen_rtx (SET, VOIDmode, mem, reg);
195 rtx restpat = gen_rtx (SET, VOIDmode, reg, mem);
196 rtx saveinsn = emit_insn (savepat);
197 rtx restinsn = emit_insn (restpat);
200 reg_save_code[i] = recog_memoized (saveinsn);
201 reg_restore_code[i] = recog_memoized (restinsn);
203 /* Now extract both insns and see if we can meet their constraints. */
204 ok = (reg_save_code[i] != -1 && reg_restore_code[i] != -1);
207 insn_extract (saveinsn);
208 ok = constrain_operands (reg_save_code[i], 1);
209 insn_extract (restinsn);
210 ok &= constrain_operands (reg_restore_code[i], 1);
215 call_fixed_regs[i] = 1;
216 SET_HARD_REG_BIT (call_fixed_reg_set, i);
225 /* Initialize save areas by showing that we haven't allocated any yet. */
232 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
233 regno_save_mem[i] = 0;
236 /* Allocate save areas for any hard registers that might need saving.
237 We take a conservative approach here and look for call-clobbered hard
238 registers that are assigned to pseudos that cross calls. This may
239 overestimate slightly (especially if some of these registers are later
240 used as spill registers), but it should not be significant.
242 Then perform register elimination in the addresses of the save area
243 locations; return 1 if all eliminated addresses are strictly valid.
244 We assume that our caller has set up the elimination table to the
245 worst (largest) possible offsets.
247 Set *PCHANGED to 1 if we had to allocate some memory for the save area. */
250 setup_save_areas (pchanged)
256 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
257 if (reg_renumber[i] >= 0 && reg_n_calls_crossed[i] > 0)
259 int regno = reg_renumber[i];
261 = regno + HARD_REGNO_NREGS (regno, GET_MODE (regno_reg_rtx[i]));
264 for (j = regno; j < endregno; j++)
265 if (call_used_regs[j] && regno_save_mem[j] == 0)
268 = assign_stack_local (regno_save_mode[j],
269 GET_MODE_SIZE (regno_save_mode[j]), 0);
274 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
275 if (regno_save_mem[i] != 0)
276 ok &= strict_memory_address_p (regno_save_mode[i],
277 XEXP (eliminate_regs (regno_save_mem[i],
284 /* Find the places where hard regs are live across calls and save them.
286 INSN_MODE is the mode to assign to any insns that we add. This is used
287 by reload to determine whether or not reloads or register eliminations
288 need be done on these insns. */
291 save_call_clobbered_regs (insn_mode)
292 enum machine_mode insn_mode;
297 for (b = 0; b < n_basic_blocks; b++)
299 regset regs_live = basic_block_live_at_start[b];
300 int offset, bit, i, j;
303 /* Compute hard regs live at start of block -- this is the
304 real hard regs marked live, plus live pseudo regs that
305 have been renumbered to hard regs. No registers have yet been
306 saved because we restore all of them before the end of the basic
310 hard_regs_live = *regs_live;
312 COPY_HARD_REG_SET (hard_regs_live, regs_live);
315 CLEAR_HARD_REG_SET (hard_regs_saved);
318 for (offset = 0, i = 0; offset < regset_size; offset++)
320 if (regs_live[offset] == 0)
321 i += HOST_BITS_PER_INT;
323 for (bit = 1; bit && i < max_regno; bit <<= 1, i++)
324 if ((regs_live[offset] & bit)
325 && (regno = reg_renumber[i]) >= 0)
327 j < regno + HARD_REGNO_NREGS (regno,
328 PSEUDO_REGNO_MODE (i));
330 SET_HARD_REG_BIT (hard_regs_live, j);
333 /* Now scan the insns in the block, keeping track of what hard
334 regs are live as we go. When we see a call, save the live
335 call-clobbered hard regs. */
337 for (insn = basic_block_head[b]; ; insn = NEXT_INSN (insn))
339 RTX_CODE code = GET_CODE (insn);
341 if (GET_RTX_CLASS (code) == 'i')
345 /* If some registers have been saved, see if INSN references
346 any of them. We must restore them before the insn if so. */
349 restore_referenced_regs (PATTERN (insn), insn, insn_mode);
351 /* NB: the normal procedure is to first enliven any
352 registers set by insn, then deaden any registers that
353 had their last use at insn. This is incorrect now,
354 since multiple pseudos may have been mapped to the
355 same hard reg, and the death notes are ambiguous. So
356 it must be done in the other, safe, order. */
358 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
359 if (REG_NOTE_KIND (link) == REG_DEAD)
360 clear_reg_live (XEXP (link, 0));
362 /* When we reach a call, we need to save all registers that are
363 live, call-used, not fixed, and not already saved. We must
364 test at this point because registers that die in a CALL_INSN
365 are not live across the call and likewise for registers that
366 are born in the CALL_INSN. */
368 if (code == CALL_INSN)
369 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
370 if (call_used_regs[regno] && ! call_fixed_regs[regno]
371 && TEST_HARD_REG_BIT (hard_regs_live, regno)
372 && ! TEST_HARD_REG_BIT (hard_regs_saved, regno))
373 insert_save_restore (insn, 1, regno, insn_mode);
375 note_stores (PATTERN (insn), set_reg_live);
377 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
378 if (REG_NOTE_KIND (link) == REG_UNUSED)
379 clear_reg_live (XEXP (link, 0));
382 if (insn == basic_block_end[b])
386 /* At the end of the basic block, we must restore any registers that
387 remain saved. If the last insn in the block is a JUMP_INSN, put
388 the restore before the insn, otherwise, put it after the insn. */
391 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
392 if (TEST_HARD_REG_BIT (hard_regs_saved, regno))
393 insert_save_restore ((GET_CODE (insn) == JUMP_INSN
394 ? insn : NEXT_INSN (insn)),
395 0, regno, insn_mode);
399 /* Here from note_stores when an insn stores a value in a register.
400 Set the proper bit or bits in hard_regs_live. All pseudos that have
401 been assigned hard regs have had their register number changed already,
402 so we can ignore pseudos. */
405 set_reg_live (reg, setter)
408 register int regno, endregno, i;
411 if (GET_CODE (reg) == SUBREG)
413 word = SUBREG_WORD (reg);
414 reg = SUBREG_REG (reg);
417 if (GET_CODE (reg) != REG || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
420 regno = REGNO (reg) + word;
421 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
423 for (i = regno; i < endregno; i++)
424 SET_HARD_REG_BIT (hard_regs_live, i);
427 /* Here when a REG_DEAD note records the last use of a reg. Clear
428 the appropriate bit or bits in hard_regs_live. Again we can ignore
435 register int regno, endregno, i;
437 if (GET_CODE (reg) != REG || REGNO (reg) >= FIRST_PSEUDO_REGISTER)
441 endregno= regno + HARD_REGNO_NREGS (regno, GET_MODE (reg));
443 for (i = regno; i < endregno; i++)
444 CLEAR_HARD_REG_BIT (hard_regs_live, i);
447 /* If any register currently residing in the save area is referenced in X,
448 which is part of INSN, emit code to restore the register in front of INSN.
449 INSN_MODE is the mode to assign to any insns that we add. */
452 restore_referenced_regs (x, insn, insn_mode)
455 enum machine_mode insn_mode;
457 enum rtx_code code = GET_CODE (x);
463 int regno = REGNO (x);
465 /* If this is a pseudo, scan its memory location, since it might
466 involve the use of another register, which might be saved. */
468 if (regno >= FIRST_PSEUDO_REGISTER
469 && reg_equiv_mem[regno] != 0)
470 restore_referenced_regs (XEXP (reg_equiv_mem[regno], 0),
472 else if (regno >= FIRST_PSEUDO_REGISTER
473 && reg_equiv_address[regno] != 0)
474 restore_referenced_regs (XEXP (reg_equiv_address[regno], 0),
477 /* Otherwise if this is a hard register, restore any piece of it that
478 is currently saved. */
480 else if (regno < FIRST_PSEUDO_REGISTER)
482 int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
484 for (i = regno; i < endregno; i ++)
485 if (TEST_HARD_REG_BIT (hard_regs_saved, i))
486 insert_save_restore (insn, 0, i, insn_mode);
492 fmt = GET_RTX_FORMAT (code);
493 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
496 restore_referenced_regs (XEXP (x, i), insn, insn_mode);
497 else if (fmt[i] == 'E')
498 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
499 restore_referenced_regs (XVECEXP (x, i, j), insn, insn_mode);
503 /* Insert a sequence of insns to save or restore, SAVE_P says which,
504 REGNO. Place these insns in front of INSN. INSN_MODE is the mode
505 to assign to these insns.
507 Note that we have verified in init_caller_save that we can do this
508 with a simple SET, so use it. Set INSN_CODE to what we save there
509 since the address might not be valid so the insn might not be recognized.
510 These insns will be reloaded and have register elimination done by
511 find_reload, so we need not worry about that here. */
514 insert_save_restore (insn, save_p, regno, insn_mode)
518 enum machine_mode insn_mode;
524 /* If INSN is a CALL_INSN, we must insert our insns before any
525 USE insns in front of the CALL_INSN. */
527 if (GET_CODE (insn) == CALL_INSN)
528 while (GET_CODE (PREV_INSN (insn)) == INSN
529 && GET_CODE (PATTERN (PREV_INSN (insn))) == USE)
530 insn = PREV_INSN (insn);
533 /* If INSN references CC0, put our insns in front of the insn that sets
534 CC0. This is always safe, since the only way we could be passed an
535 insn that references CC0 is for a restore, and doing a restore earlier
536 isn't a problem. We do, however, assume here that CALL_INSNs don't
537 reference CC0. Guard against non-INSN's like CODE_LABEL. */
539 if ((GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
540 && reg_referenced_p (cc0_rtx, PATTERN (insn)))
541 insn = prev_nonnote_insn (insn);
544 /* Get the pattern to emit and update our status. */
547 pat = gen_rtx (SET, VOIDmode, regno_save_mem[regno],
548 gen_rtx (REG, regno_save_mode[regno], regno));
549 code = reg_save_code[regno];
550 SET_HARD_REG_BIT (hard_regs_saved, regno);
555 pat = gen_rtx (SET, VOIDmode,
556 gen_rtx (REG, regno_save_mode[regno], regno),
557 regno_save_mem[regno]);
558 code = reg_restore_code[regno];
559 CLEAR_HARD_REG_BIT (hard_regs_saved, regno);
563 /* Emit the insn and set the code and mode. */
565 insn = emit_insn_before (pat, insn);
566 PUT_MODE (insn, insn_mode);
567 INSN_CODE (insn) = code;