GCC Middle and Back End API Reference
caller-save.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "rtl.h"
#include "regs.h"
#include "insn-config.h"
#include "flags.h"
#include "hard-reg-set.h"
#include "recog.h"
#include "basic-block.h"
#include "df.h"
#include "reload.h"
#include "function.h"
#include "expr.h"
#include "diagnostic-core.h"
#include "tm_p.h"
#include "addresses.h"
#include "ggc.h"
#include "dumpfile.h"
#include "gt-caller-save.h"
Include dependency graph for caller-save.c:

Data Structures

struct  saved_hard_reg

Macros

#define MOVE_MAX_WORDS   (MOVE_MAX / UNITS_PER_WORD)
#define regno_save_mode   (this_target_reload->x_regno_save_mode)
#define cached_reg_save_code   (this_target_reload->x_cached_reg_save_code)
#define cached_reg_restore_code   (this_target_reload->x_cached_reg_restore_code)

Typedefs

typedef void refmarker_fn (rtx *loc, enum machine_mode mode, int hardregno, void *mark_arg)

Functions

static int reg_save_code (int, enum machine_mode)
static int reg_restore_code (int, enum machine_mode)
static void initiate_saved_hard_regs (void)
static void new_saved_hard_reg (int, int)
static void finish_saved_hard_regs (void)
static int saved_hard_reg_compare_func (const void *, const void *)
static void mark_set_regs (rtx, const_rtx, void *)
static void mark_referenced_regs (rtx *, refmarker_fn *mark, void *mark_arg)
static int insert_save (struct insn_chain *, int, int, HARD_REG_SET *, enum machine_mode *)
static int insert_restore (struct insn_chain *, int, int, int, enum machine_mode *)
static struct insn_chain * insert_one_insn (struct insn_chain *, int, int, rtx)
static void add_stored_regs (rtx, const_rtx, void *)
static int reg_save_code ()
static int reg_restore_code ()
void init_caller_save ()
void init_save_areas ()
static void new_saved_hard_reg ()
static int saved_hard_reg_compare_func ()
void setup_save_areas ()
void save_call_clobbered_regs ()
static void mark_set_regs ()
static void add_stored_regs ()
static void mark_referenced_regs ()
static void mark_reg_as_referenced (rtx *loc, enum machine_mode mode, int hardregno, void *arg)
static void replace_reg_with_saved_mem (rtx *loc, enum machine_mode mode, int regno, void *arg)
static int add_used_regs_1 ()
static void add_used_regs ()
static struct insn_chain * insert_one_insn ()

Variables

static rtx regno_save_mem [FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX/MIN_UNITS_PER_WORD+1]
static int save_slots_num
static rtx save_slots [FIRST_PSEUDO_REGISTER]
static HARD_REG_SET hard_regs_saved
static int n_regs_saved
static HARD_REG_SET referenced_regs
static refmarker_fn mark_reg_as_referenced
static refmarker_fn replace_reg_with_saved_mem
static rtx savepat
static rtx restpat
static rtx test_reg
static rtx test_mem
static rtx saveinsn
static rtx restinsn
static struct saved_hard_reghard_reg_map [FIRST_PSEUDO_REGISTER]
static int saved_regs_num
static struct saved_hard_regall_saved_regs [FIRST_PSEUDO_REGISTER]

Macro Definition Documentation

#define cached_reg_restore_code   (this_target_reload->x_cached_reg_restore_code)
#define cached_reg_save_code   (this_target_reload->x_cached_reg_save_code)
#define MOVE_MAX_WORDS   (MOVE_MAX / UNITS_PER_WORD)

Save and restore call-clobbered registers which are live across a call. Copyright (C) 1989-2013 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with GCC; see the file COPYING3. If not see http://www.gnu.org/licenses/.

Referenced by init_caller_save(), and insert_restore().

#define regno_save_mode   (this_target_reload->x_regno_save_mode)

Typedef Documentation

typedef void refmarker_fn(rtx *loc, enum machine_mode mode, int hardregno, void *mark_arg)

Function Documentation

static void add_stored_regs ( rtx  ,
const_rtx  ,
void *   
)
static
static void add_stored_regs ( )
static

Here from note_stores when an insn stores a value in a register. Set the proper bit or bits in the passed regset. All pseudos that have been assigned hard regs have had their register number changed already, so we can ignore pseudos.

static void add_used_regs ( )
static

A note_uses callback used by insert_one_insn. Add the hard-register equivalent of each REG to regset DATA.

static int add_used_regs_1 ( )
static

A for_each_rtx callback used by add_used_regs. Add the hard-register equivalent of each REG to regset DATA.

static void finish_saved_hard_regs ( )
static

Free memory allocated for the saved hard registers.

void init_caller_save ( void  )

Initialize for caller-save.

Look at all the hard registers that are used by a call and for which reginfo.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.

 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.   
 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.   
 If we didn't find a valid address, we must use register indirect.   
 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.

 To avoid lots of unnecessary RTL allocation, we construct all the RTL
 once, then modify the memory and register operands in-place.   

References call_fixed_reg_set, call_used_regs, HARD_REGNO_CALLER_SAVE_MODE, MOVE_MAX_WORDS, regno_save_mode, SET_HARD_REG_BIT, and TEST_HARD_REG_BIT.

Referenced by split_live_ranges_for_shrink_wrap().

void init_save_areas ( void  )

Initialize save areas by showing that we haven't allocated any yet.

static void initiate_saved_hard_regs ( )
static

First called function for work with saved hard registers.

Referenced by saved_hard_reg_compare_func().

static struct insn_chain* insert_one_insn ( struct insn_chain *  ,
int  ,
int  ,
rtx   
)
staticread
static struct insn_chain* insert_one_insn ( )
staticread

Emit a new caller-save insn and set the code.

     ??? It would be nice if we could exclude the already / still saved
     registers from the live sets.   
     If CHAIN->INSN is a call, then the registers which contain
     the arguments to the function are live in the new insn.   
     ??? It would be nice if we could exclude the already / still saved
     registers from the live sets, and observe REG_UNUSED notes.   
     Registers that are set in CHAIN->INSN live in the new insn.
     (Unless there is a REG_UNUSED note for them, but we don't
      look for them here.)  
static int insert_restore ( struct insn_chain *  chain,
int  before_p,
int  regno,
int  maxrestore,
enum machine_mode *  save_mode 
)
static

Insert a sequence of insns to restore. Place these insns in front of CHAIN if BEFORE_P is nonzero, behind the insn otherwise. MAXRESTORE is the maximum number of registers which should be restored during this call. It should never be less than 1 since we only work with entire registers.

Note that we have verified in init_caller_save that we can do this with a simple SET, so use it. Set INSN_CODE to what we save there since the address might not be valid so the insn might not be recognized. These insns will be reloaded and have register elimination done by find_reload, so we need not worry about that here.

Return the extra number of registers saved.

 A common failure mode if register status is not correct in the
 RTL is for this routine to be called with a REGNO we didn't
 expect to save.  That will cause us to write an insn with a (nil)
 SET_DEST or SET_SRC.  Instead of doing so and causing a crash
 later, check for this common case here instead.  This will remove
 one step in debugging such problems.   
 Get the pattern to emit and update our status.

 See if we can restore `maxrestore' registers at once.  Work
 backwards to the single register case.   
     Must do this one restore at a time.   
     Check that insn to restore REGNO in save_mode[regno] is
     correct.   
 Verify that the alignment of spill space is equal to or greater
 than required.   
 Clear status for all registers we restored.   
 Tell our callers how many extra registers we saved/restored.   

References adjust_address_nv, copy_rtx(), gcc_assert, gen_rtx_REG(), GET_MODE, GET_MODE_ALIGNMENT, hard_regs_saved, insert_one_insn(), MAX_SUPPORTED_STACK_ALIGNMENT, MEM_ALIGN, MIN, MOVE_MAX_WORDS, n_regs_saved, NULL_RTX, reg_save_code(), SET_HARD_REG_BIT, SET_REGNO_REG_SET, and TEST_HARD_REG_BIT.

static int insert_save ( struct insn_chain *  chain,
int  before_p,
int  regno,
HARD_REG_SET to_save,
enum machine_mode *  save_mode 
)
static

Like insert_restore above, but save registers instead.

 A common failure mode if register status is not correct in the
 RTL is for this routine to be called with a REGNO we didn't
 expect to save.  That will cause us to write an insn with a (nil)
 SET_DEST or SET_SRC.  Instead of doing so and causing a crash
 later, check for this common case here.  This will remove one
 step in debugging such problems.   
 Get the pattern to emit and update our status.

 See if we can save several registers with a single instruction.
 Work backwards to the single register case.   
     Must do this one save at a time.   
     Check that insn to save REGNO in save_mode[regno] is
     correct.   
 Verify that the alignment of spill space is equal to or greater
 than required.   
 Set hard_regs_saved and dead_or_set for all the registers we saved.   
 Tell our callers how many extra registers we saved/restored.   

References bitmap_set_range(), GET_MODE, HARD_REGISTER_NUM_P, reg_renumber, and REGNO.

Referenced by save_call_clobbered_regs().

static void mark_referenced_regs ( rtx ,
refmarker_fn mark,
void *  mark_arg 
)
static
static void mark_referenced_regs ( )
static

Walk X and record all referenced registers in REFERENCED_REGS.

  If we're setting only part of a multi-word register,
  we shall mark it as referenced, because the words
  that are not being set should be restored.   
       ??? Will we ever end up with an equiv expression in a debug
       insn, that would have required restoring a reg, or will
       reload take care of it for us?   


     If this is a pseudo that did not get a hard register, scan its
     memory location, since it might involve the use of another
     register, which might be saved.   

References add_to_hard_reg_set(), and referenced_regs.

static void mark_reg_as_referenced ( rtx loc,
enum machine_mode  mode,
int  hardregno,
void *  arg 
)
static

Parameter function for mark_referenced_regs() that adds registers present in the insn and in equivalent mems and addresses to referenced_regs.

static void mark_set_regs ( rtx  ,
const_rtx  ,
void *   
)
static
static void mark_set_regs ( )
static

Here from note_stores, or directly from save_call_clobbered_regs, when an insn stores a value in a register. Set the proper bit or bits in this_insn_sets. All pseudos that have been assigned hard regs have had their register number changed already, so we can ignore pseudos.

static void new_saved_hard_reg ( int  ,
int   
)
static
static void new_saved_hard_reg ( )
static

Allocate and return new saved hard register with given REGNO and CALL_FREQ.

References saved_hard_reg::call_freq.

static int reg_restore_code ( int  ,
enum  machine_mode 
)
static
static int reg_restore_code ( )
static

Return the INSN_CODE used to restore register REG in mode MODE.

Populate our cache.

References caller_save_initialized_p, CLEAR_HARD_REG_SET, no_caller_save_reg_set, and offset.

static int reg_save_code ( int  ,
enum  machine_mode 
)
static
static int reg_save_code ( )
static

Return the INSN_CODE used to save register REG in mode MODE.

Depending on how HARD_REGNO_MODE_OK is defined, range propagation
might deduce here that reg >= FIRST_PSEUDO_REGISTER.  So the assert
below silences a warning.   
 Update the register number and modes of the register
 and memory operand.   


 Force re-recognition of the modified insns.   
 Now extract both insns and see if we can meet their
 constraints.   
static void replace_reg_with_saved_mem ( rtx loc,
enum machine_mode  mode,
int  regno,
void *  arg 
)
static

Parameter function for mark_referenced_regs() that replaces registers referenced in a debug_insn that would have been restored, should it be a non-debug_insn, with their save locations.

 If none of the registers in the range would need restoring, we're
 all set.   
         This is gen_lowpart_if_possible(), but without validating
         the newly-formed address.   
           Adjust the address so that the address-after-the-data is
           unchanged.   

References adjust_address_nv, CLEAR_HARD_REG_BIT, copy_rtx(), gcc_assert, gen_rtx_REG(), GET_MODE, GET_MODE_ALIGNMENT, hard_regs_saved, insert_one_insn(), MAX_SUPPORTED_STACK_ALIGNMENT, MEM_ALIGN, MIN, n_regs_saved, NULL_RTX, reg_restore_code(), reg_save_code(), SET_REGNO_REG_SET, and TEST_HARD_REG_BIT.

void save_call_clobbered_regs ( void  )

Find the places where hard regs are live across calls and save them.

 Computed in mark_set_regs, holds all registers set by the current
 instruction.   
         If some registers have been saved, see if INSN references
         any of them.  We must restore them before the insn if so.   
               Restore all registers if this is a JUMP_INSN.   
             If a saved register is set after the call, this means we no
             longer should restore it.  This can happen when parts of a
             multi-word pseudo do not conflict with other pseudos, so
             IRA may allocate the same hard register for both.  One may
             be live across the call, while the other is set
             afterwards.   
             Use the register life information in CHAIN to compute which
             regs are live during the call.   
             Save hard registers always in the widest mode available.   
             Look through all live pseudos, mark their hard registers
             and choose proper mode for saving.   
             Record all registers set in this call insn.  These don't need
             to be saved.  N.B. the call insn might set a subreg of a
             multi-hard-reg pseudo; then the pseudo is considered live
             during the call, but the subreg that is set isn't.   
             Compute which hard regs must be saved before this call.   
             Must recompute n_regs_saved.   
         At the end of the basic block, we must restore any registers that
         remain saved.  If the last insn in the block is a JUMP_INSN, put
         the restore before the insn, otherwise, put it after the insn.   
             When adding hard reg restores after a DEBUG_INSN, move
             all notes between last real insn and this DEBUG_INSN after
             the DEBUG_INSN, otherwise we could get code
             -g/-g0 differences.   

References AND_COMPL_HARD_REG_SET, AND_HARD_REG_SET, call_fixed_reg_set, call_used_reg_set, CLEAR_HARD_REG_SET, copy_rtx(), EXECUTE_IF_SET_IN_REG_SET, find_reg_note(), GET_CODE, GET_MODE_BITSIZE, HARD_REG_SET, HARD_REGISTER_P, HARD_REGNO_CALLER_SAVE_MODE, hard_regs_saved, insert_one_insn(), insert_save(), mark_set_regs(), n_regs_saved, note_stores(), NULL, PATTERN, PSEUDO_REGNO_MODE, reg_renumber, REG_SET_TO_HARD_REG_SET, REGNO, regno_reg_rtx, regno_save_mode, SET_DEST, SET_HARD_REG_BIT, TEST_HARD_REG_BIT, XEXP, and XVECEXP.

static int saved_hard_reg_compare_func ( const void *  ,
const void *   
)
static
static int saved_hard_reg_compare_func ( )
static

The function is used to sort the saved hard register structures according their frequency.

References BLOCK_FOR_INSN, CALL_P, call_used_reg_set, CLEAR_HARD_REG_SET, COPY_HARD_REG_SET, find_reg_note(), HARD_REG_SET, initiate_saved_hard_regs(), NULL, REG_FREQ_FROM_BB, REG_SET_TO_HARD_REG_SET, reload_insn_chain, and used_regs.

void setup_save_areas ( void  )

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.

For IRA we use priority coloring to decrease stack slots needed for saving hard registers through calls. We build conflicts for them to do coloring.

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)

 Find every CALL_INSN and record which hard regs are live across the
 call into HARD_REG_MAP and HARD_REGS_USED.   
 Create hard reg saved regs.   
     Record all registers set in this call insn.  These don't
     need to be saved.  N.B. the call insn might set a subreg
     of a multi-hard-reg pseudo; then the pseudo is considered
     live during the call, but the subreg that is set
     isn't.   
     Sibcalls are considered to set the return value.   
     Look through all live pseudos, mark their hard registers.   
 If requested, figure out which hard regs can share save slots.   
     Find saved hard register conflicts.   
         Record all registers set in this call insn.  These don't
         need to be saved.  N.B. the call insn might set a subreg
         of a multi-hard-reg pseudo; then the pseudo is considered
         live during the call, but the subreg that is set
         isn't.   
         Sibcalls are considered to set the return value,
         compare df-scan.c:df_get_call_refs.   
         Look through all live pseudos, mark their hard registers.   
     Sort saved hard regs.   
     Initiate slots available from the previous reload
     iteration.   
     Allocate stack slots for the saved hard registers.   
     We are not sharing slots. 

     Run through all the call-used hard-registers and allocate
     space for each in the caller-save area.  Try to allocate space
     in a manner which allows multi-register saves/restores to be done.   
           If no mode exists for this size, try another.  Also break out
           if we have already saved this hard register.   
           See if any register in this group has been saved.   
           We have found an acceptable mode to store in.  Since
           hard register is always saved in the widest mode
           available, the mode may be wider than necessary, it is
           OK to reduce the alignment of spill space.  We will
           verify that it is equal to or greater than required
           when we restore and save the hard register in
           insert_restore and insert_save.   
           Setup single word save area just in case...   
             This should not depend on WORDS_BIG_ENDIAN.
             The order of words in regs is the same as in memory.   
 Now loop again and set the alias set of any save areas we made to
 the alias set used to represent frame objects.   

Variable Documentation

struct saved_hard_reg* all_saved_regs[FIRST_PSEUDO_REGISTER]
static

Pointers to all the structures. Index is the order number of the corresponding structure.

struct saved_hard_reg* hard_reg_map[FIRST_PSEUDO_REGISTER]
static

Map: hard register number to the corresponding structure.

HARD_REG_SET hard_regs_saved
static

Set of hard regs currently residing in save area (during insn scan).

Referenced by insert_restore(), replace_reg_with_saved_mem(), and save_call_clobbered_regs().

refmarker_fn mark_reg_as_referenced
static
int n_regs_saved
static

Number of registers currently in hard_regs_saved.

Referenced by insert_restore(), replace_reg_with_saved_mem(), and save_call_clobbered_regs().

HARD_REG_SET referenced_regs
static

Computed by mark_referenced_regs, all regs referenced in a given insn.

Referenced by mark_referenced_regs().

rtx regno_save_mem[FIRST_PSEUDO_REGISTER][MAX_MOVE_MAX/MIN_UNITS_PER_WORD+1]
static

For each hard register, a place on the stack where it can be saved, if needed.

refmarker_fn replace_reg_with_saved_mem
static
rtx restinsn
static
rtx restpat
static
rtx save_slots[FIRST_PSEUDO_REGISTER]
static

Allocated slots so far.

int save_slots_num
static

The number of elements in the subsequent array.

int saved_regs_num
static

The number of all structures representing hard registers should be saved, in order words, the number of used elements in the following array.

rtx saveinsn
static
rtx savepat
static
rtx test_mem
static
rtx test_reg
static