📄 caller-save.c
字号:
/* Save and restore call-clobbered registers which are live across a call. Copyright (C) 1989, 1992, 1994, 1995 Free Software Foundation, Inc.This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. */#include "config.h"#include "rtl.h"#include "insn-config.h"#include "flags.h"#include "regs.h"#include "hard-reg-set.h"#include "recog.h"#include "basic-block.h"#include "reload.h"#include "expr.h"#ifndef MAX_MOVE_MAX#define MAX_MOVE_MAX MOVE_MAX#endif#ifndef MIN_UNITS_PER_WORD#define MIN_UNITS_PER_WORD UNITS_PER_WORD#endif/* Modes for each hard register that we can save. The smallest mode is wide enough to save the entire contents of the register. When saving the register because it is live we first try to save in multi-register modes. If that is not possible the save is done one register at a time. */static enum machine_mode regno_save_mode[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];/* For each hard register, a place on the stack where it can be saved, if needed. */static rtx regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];/* We will only make a register eligible for caller-save if it can be saved in its widest mode with a simple SET insn as long as the memory address is valid. We record the INSN_CODE is those insns here since when we emit them, the addresses might not be valid, so they might not be recognized. */static enum insn_code reg_save_code[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];static enum insn_code reg_restore_code[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX / MIN_UNITS_PER_WORD + 1];/* Set of hard regs currently live (during scan of all insns). */static HARD_REG_SET hard_regs_live;/* Set of hard regs currently residing in save area (during insn scan). */static HARD_REG_SET hard_regs_saved;/* Set of hard regs which need to be restored before referenced. */static HARD_REG_SET hard_regs_need_restore;/* Number of registers currently in hard_regs_saved. */int n_regs_saved;static void set_reg_live PROTO((rtx, rtx));static void clear_reg_live PROTO((rtx));static void restore_referenced_regs PROTO((rtx, rtx, enum machine_mode));static int insert_save_restore PROTO((rtx, int, int, enum machine_mode, int));/* Initialize for caller-save. Look at all the hard registers that are used by a call and for which regclass.c has not already excluded from being used across a call. Ensure that we can find a mode to save the register and that there is a simple insn to save and restore the register. This latter check avoids problems that would occur if we tried to save the MQ register of some machines directly into memory. */voidinit_caller_save (){ char *first_obj = (char *) oballoc (0); rtx addr_reg; int offset; rtx address; int i, j; /* First find all the registers that we need to deal with and all the modes that they can have. If we can't find a mode to use, we can't have the register live over calls. */ for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) { if (call_used_regs[i] && ! call_fixed_regs[i]) { for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++) { regno_save_mode[i][j] = choose_hard_reg_mode (i, j); if (regno_save_mode[i][j] == VOIDmode && j == 1) { call_fixed_regs[i] = 1; SET_HARD_REG_BIT (call_fixed_reg_set, i); } } } else regno_save_mode[i][1] = VOIDmode; } /* The following code tries to approximate the conditions under which we can easily save and restore a register without scratch registers or other complexities. It will usually work, except under conditions where the validity of an insn operand is dependent on the address offset. No such cases are currently known. We first find a typical offset from some BASE_REG_CLASS register. This address is chosen by finding the first register in the class and by finding the smallest power of two that is a valid offset from that register in every mode we will use to save registers. */ for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) if (TEST_HARD_REG_BIT (reg_class_contents[(int) BASE_REG_CLASS], i)) break; if (i == FIRST_PSEUDO_REGISTER) abort (); addr_reg = gen_rtx (REG, Pmode, i); for (offset = 1 << (HOST_BITS_PER_INT / 2); offset; offset >>= 1) { address = gen_rtx (PLUS, Pmode, addr_reg, GEN_INT (offset)); for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) if (regno_save_mode[i][1] != VOIDmode && ! strict_memory_address_p (regno_save_mode[i][1], address)) break; if (i == FIRST_PSEUDO_REGISTER) break; } /* If we didn't find a valid address, we must use register indirect. */ if (offset == 0) address = addr_reg; /* Next we try to form an insn to save and restore the register. We see if such an insn is recognized and meets its constraints. */ start_sequence (); for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++) if (regno_save_mode[i][j] != VOIDmode) { rtx mem = gen_rtx (MEM, regno_save_mode[i][j], address); rtx reg = gen_rtx (REG, regno_save_mode[i][j], i); rtx savepat = gen_rtx (SET, VOIDmode, mem, reg); rtx restpat = gen_rtx (SET, VOIDmode, reg, mem); rtx saveinsn = emit_insn (savepat); rtx restinsn = emit_insn (restpat); int ok; reg_save_code[i][j] = recog_memoized (saveinsn); reg_restore_code[i][j] = recog_memoized (restinsn); /* Now extract both insns and see if we can meet their constraints. */ ok = (reg_save_code[i][j] != -1 && reg_restore_code[i][j] != -1); if (ok) { insn_extract (saveinsn); ok = constrain_operands (reg_save_code[i][j], 1); insn_extract (restinsn); ok &= constrain_operands (reg_restore_code[i][j], 1); } if (! ok) { regno_save_mode[i][j] = VOIDmode; if (j == 1) { call_fixed_regs[i] = 1; SET_HARD_REG_BIT (call_fixed_reg_set, i); } } } end_sequence (); obfree (first_obj);}/* Initialize save areas by showing that we haven't allocated any yet. */voidinit_save_areas (){ int i, j; for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++) regno_save_mem[i][j] = 0;}/* Allocate save areas for any hard registers that might need saving. We take a conservative approach here and look for call-clobbered hard registers that are assigned to pseudos that cross calls. This may overestimate slightly (especially if some of these registers are later used as spill registers), but it should not be significant. Then perform register elimination in the addresses of the save area locations; return 1 if all eliminated addresses are strictly valid. We assume that our caller has set up the elimination table to the worst (largest) possible offsets. Set *PCHANGED to 1 if we had to allocate some memory for the save area. Future work: In the fallback case we should iterate backwards across all possible modes for the save, choosing the largest available one instead of falling back to the smallest mode immediately. (eg TF -> DF -> SF). We do not try to use "move multiple" instructions that exist on some machines (such as the 68k moveml). It could be a win to try and use them when possible. The hard part is doing it in a way that is machine independent since they might be saving non-consecutive registers. (imagine caller-saving d0,d1,a0,a1 on the 68k) */intsetup_save_areas (pchanged) int *pchanged;{ int i, j, k; HARD_REG_SET hard_regs_used; int ok = 1; /* Allocate space in the save area for the largest multi-register pseudos first, then work backwards to single register pseudos. */ /* Find and record all call-used hard-registers in this function. */ CLEAR_HARD_REG_SET (hard_regs_used); for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++) if (reg_renumber[i] >= 0 && reg_n_calls_crossed[i] > 0) { int regno = reg_renumber[i]; int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (regno_reg_rtx[i])); int nregs = endregno - regno; for (j = 0; j < nregs; j++) { if (call_used_regs[regno+j]) SET_HARD_REG_BIT (hard_regs_used, regno+j); } } /* Now run through all the call-used hard-registers and allocate space for them in the caller-save area. Try to allocate space in a manner which allows multi-register saves/restores to be done. */ for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) for (j = MOVE_MAX / UNITS_PER_WORD; j > 0; j--) { int ok = 1; int do_save; /* If no mode exists for this size, try another. Also break out if we have already saved this hard register. */ if (regno_save_mode[i][j] == VOIDmode || regno_save_mem[i][1] != 0) continue; /* See if any register in this group has been saved. */ do_save = 1; for (k = 0; k < j; k++) if (regno_save_mem[i + k][1]) { do_save = 0; break; } if (! do_save) continue; for (k = 0; k < j; k++) { int regno = i + k; ok &= (TEST_HARD_REG_BIT (hard_regs_used, regno) != 0); } /* We have found an acceptable mode to store in. */ if (ok) { regno_save_mem[i][j] = assign_stack_local (regno_save_mode[i][j], GET_MODE_SIZE (regno_save_mode[i][j]), 0); /* Setup single word save area just in case... */ for (k = 0; k < j; k++) { /* This should not depend on WORDS_BIG_ENDIAN. The order of words in regs is the same as in memory. */ rtx temp = gen_rtx (MEM, regno_save_mode[i+k][1], XEXP (regno_save_mem[i][j], 0)); regno_save_mem[i+k][1] = adj_offsettable_operand (temp, k * UNITS_PER_WORD); } *pchanged = 1; } } for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) for (j = 1; j <= MOVE_MAX / UNITS_PER_WORD; j++) if (regno_save_mem[i][j] != 0) ok &= strict_memory_address_p (GET_MODE (regno_save_mem[i][j]), XEXP (eliminate_regs (regno_save_mem[i][j], 0, NULL_RTX), 0)); return ok;}/* Find the places where hard regs are live across calls and save them. INSN_MODE is the mode to assign to any insns that we add. This is used by reload to determine whether or not reloads or register eliminations need be done on these insns. */voidsave_call_clobbered_regs (insn_mode) enum machine_mode insn_mode;{ rtx insn; int b; for (b = 0; b < n_basic_blocks; b++) { regset regs_live = basic_block_live_at_start[b]; rtx prev_block_last = PREV_INSN (basic_block_head[b]); REGSET_ELT_TYPE bit; int offset, i, j; int regno; /* Compute hard regs live at start of block -- this is the real hard regs marked live, plus live pseudo regs that have been renumbered to hard regs. No registers have yet been saved because we restore all of them before the end of the basic block. */#ifdef HARD_REG_SET hard_regs_live = *regs_live;#else COPY_HARD_REG_SET (hard_regs_live, regs_live);#endif CLEAR_HARD_REG_SET (hard_regs_saved); CLEAR_HARD_REG_SET (hard_regs_need_restore); n_regs_saved = 0; for (offset = 0, i = 0; offset < regset_size; offset++) { if (regs_live[offset] == 0) i += REGSET_ELT_BITS; else for (bit = 1; bit && i < max_regno; bit <<= 1, i++) if ((regs_live[offset] & bit) && (regno = reg_renumber[i]) >= 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -