GCC Middle and Back End API Reference
regrename.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "target.h"
#include "rtl.h"
#include "df.h"
#include "memmodel.h"
#include "tm_p.h"
#include "insn-config.h"
#include "regs.h"
#include "emit-rtl.h"
#include "recog.h"
#include "addresses.h"
#include "cfganal.h"
#include "tree-pass.h"
#include "function-abi.h"
#include "regrename.h"
Include dependency graph for regrename.cc:

Data Structures

struct  incoming_reg_info
 
class  bb_rename_info
 

Enumerations

enum  scan_actions {
  terminate_write , terminate_dead , mark_all_read , mark_read ,
  mark_write , mark_access
}
 

Functions

static void scan_rtx (rtx_insn *, rtx *, enum reg_class, enum scan_actions, enum op_type)
 
static bool build_def_use (basic_block)
 
du_head_p regrename_chain_from_id (unsigned int id)
 
static void dump_def_use_chain (int from)
 
static void free_chain_data (void)
 
static void mark_conflict (class du_head *chains, unsigned id)
 
static void record_operand_use (class du_head *head, struct du_chain *this_du)
 
static du_head_p create_new_chain (unsigned this_regno, unsigned this_nregs, rtx *loc, rtx_insn *insn, enum reg_class cl)
 
static void merge_overlapping_regs (HARD_REG_SET *pset, class du_head *head)
 
static bool call_clobbered_in_chain_p (du_head *this_head, machine_mode mode, unsigned int regno)
 
static bool check_new_reg_p (int reg, int new_reg, class du_head *this_head, HARD_REG_SET this_unavailable)
 
int find_rename_reg (du_head_p this_head, enum reg_class super_class, HARD_REG_SET *unavailable, int old_reg, bool best_rename)
 
reg_class regrename_find_superclass (du_head_p head, int *pn_uses, HARD_REG_SET *punavailable)
 
static void rename_chains (void)
 
static void init_rename_info (class bb_rename_info *p, basic_block bb)
 
static void set_incoming_from_chain (class bb_rename_info *ri, du_head_p chain)
 
static void merge_chains (du_head_p c1, du_head_p c2)
 
void regrename_analyze (bitmap bb_mask, bool include_all_block_p)
 
bool regrename_do_replace (class du_head *head, int reg)
 
static bool verify_reg_in_set (rtx op, HARD_REG_SET *pset)
 
static bool verify_reg_tracked (rtx op)
 
static void note_sets_clobbers (rtx x, const_rtx set, void *data)
 
static void scan_rtx_reg (rtx_insn *insn, rtx *loc, enum reg_class cl, enum scan_actions action, enum op_type type)
 
static reg_class base_reg_class_for_rename (rtx_insn *insn, machine_mode mode, addr_space_t as, rtx_code code, rtx_code index_code)
 
static void scan_rtx_address (rtx_insn *insn, rtx *loc, enum reg_class cl, enum scan_actions action, machine_mode mode, addr_space_t as)
 
static void hide_operands (int n_ops, rtx *old_operands, rtx *old_dups, unsigned HOST_WIDE_INT do_not_hide, bool inout_and_ec_only)
 
static void restore_operands (rtx_insn *insn, int n_ops, rtx *old_operands, rtx *old_dups)
 
static void record_out_operands (rtx_insn *insn, bool earlyclobber, insn_rr_info *insn_info)
 
void regrename_init (bool insn_info)
 
void regrename_finish (void)
 
static unsigned int regrename_optimize (void)
 
rtl_opt_passmake_pass_regrename (gcc::context *ctxt)
 

Variables

static const char *const scan_actions_name []
 
static int tick [FIRST_PSEUDO_REGISTER]
 
static int this_tick = 0
 
static struct obstack rename_obstack
 
vec< insn_rr_infoinsn_rr
 
static unsigned current_id
 
static vec< du_head_pid_to_chain
 
static class du_headopen_chains
 
static bitmap_head open_chains_set
 
static HARD_REG_SET live_in_chains
 
static HARD_REG_SET live_hard_regs
 
static operand_rr_infocur_operand
 
static class du_headterminated_this_insn
 
static bool fail_current_block
 

Enumeration Type Documentation

◆ scan_actions

Register renaming for the GNU compiler.
Copyright (C) 2000-2024 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/>.   
This file implements the RTL register renaming pass of the compiler.  It is
 a semi-local pass whose goal is to maximize the usage of the register file
 of the processor by substituting registers for others in the solution given
 by the register allocator.  The algorithm is as follows:

   1. Local def/use chains are built: within each basic block, chains are
      opened and closed; if a chain isn't closed at the end of the block,
      it is dropped.  We pre-open chains if we have already examined a
      predecessor block and found chains live at the end which match
      live registers at the start of the new block.

   2. We try to combine the local chains across basic block boundaries by
      comparing chains that were open at the start or end of a block to
      those in successor/predecessor blocks.

   3. For each chain, the set of possible renaming registers is computed.
      This takes into account the renaming of previously processed chains.
      Optionally, a preferred class is computed for the renaming register.

   4. The best renaming register is computed for the chain in the above set,
      using a round-robin allocation.  If a preferred class exists, then the
      round-robin allocation is done within the class first, if possible.
      The round-robin allocation of renaming registers itself is global.

   5. If a renaming register has been found, it is substituted in the chain.

Targets can parameterize the pass by specifying a preferred class for the
renaming register for a given (super)class of registers to be renamed.

DEBUG_INSNs are treated specially, in particular registers occurring inside
them are treated as requiring ALL_REGS as a class.   
Enumerator
terminate_write 
terminate_dead 
mark_all_read 
mark_read 
mark_write 
mark_access 

Function Documentation

◆ base_reg_class_for_rename()

static reg_class base_reg_class_for_rename ( rtx_insn * insn,
machine_mode mode,
addr_space_t as,
rtx_code code,
rtx_code index_code )
static
A wrapper around base_reg_class which returns ALL_REGS if INSN is a
DEBUG_INSN.  The arguments MODE, AS, CODE and INDEX_CODE are as for
base_reg_class.   

References base_reg_class(), DEBUG_INSN_P, and du_chain::insn.

Referenced by scan_rtx(), and scan_rtx_address().

◆ build_def_use()

static bool build_def_use ( basic_block bb)
static

◆ call_clobbered_in_chain_p()

static bool call_clobbered_in_chain_p ( du_head * this_head,
machine_mode mode,
unsigned int regno )
static
Return true if (reg:MODE REGNO) would be clobbered by a call covered
by THIS_HEAD.   

References du_head::call_abis, du_head::call_clobber_mask, and call_clobbered_in_region_p().

Referenced by check_new_reg_p().

◆ check_new_reg_p()

static bool check_new_reg_p ( int reg,
int new_reg,
class du_head * this_head,
HARD_REG_SET this_unavailable )
static
Check if NEW_REG can be the candidate register to rename for
REG in THIS_HEAD chain.  THIS_UNAVAILABLE is a set of unavailable hard
registers.   

References call_clobbered_in_chain_p(), crtl, DEBUG_INSN_P, df_regs_ever_live_p(), du_head::first, fixed_regs, GET_MODE, global_regs, hard_regno_nregs(), HARD_REGNO_RENAME_OK, i, du_chain::insn, du_chain::loc, du_chain::next_use, targetm, and TEST_HARD_REG_BIT.

Referenced by find_rename_reg().

◆ create_new_chain()

static du_head_p create_new_chain ( unsigned this_regno,
unsigned this_nregs,
rtx * loc,
rtx_insn * insn,
enum reg_class cl )
static
Create a new chain for THIS_NREGS registers starting at THIS_REGNO,
and record its occurrence in *LOC, which is being written to in INSN.
This access requires a register of class CL.   

References bitmap_copy(), bitmap_default_obstack, bitmap_initialize(), bitmap_set_bit, du_chain::cl, CLEAR_HARD_REG_BIT, current_id, dump_file, head::first, id_to_chain, du_chain::insn, INSN_UID(), head::last, live_hard_regs, live_in_chains, du_chain::loc, mark_conflict(), du_chain::next_use, NULL, NULL_RTX, open_chains, open_chains_set, record_operand_use(), reg_names, rename_obstack, and SET_HARD_REG_BIT.

Referenced by build_def_use(), init_rename_info(), and scan_rtx_reg().

◆ dump_def_use_chain()

static void dump_def_use_chain ( int from)
static
Dump all def/use chains, starting at id FROM.   

References du_chain::cl, dump_file, head::first, FOR_EACH_VEC_ELT_FROM, i, id_to_chain, du_chain::insn, INSN_UID(), du_chain::next_use, reg_class_names, and reg_names.

Referenced by regrename_analyze().

◆ find_rename_reg()

int find_rename_reg ( du_head_p this_head,
enum reg_class super_class,
HARD_REG_SET * unavailable,
int old_reg,
bool best_rename )
For the chain THIS_HEAD, compute and return the best register to
rename to.  SUPER_CLASS is the superunion of register classes in
the chain.  UNAVAILABLE is a set of registers that cannot be used.
OLD_REG is the register currently used for the chain.  BEST_RENAME
controls whether the register chosen must be better than the
current one or just respect the given constraint.   

References check_new_reg_p(), DEBUG_INSN_P, merge_overlapping_regs(), du_chain::next_use, noop_move_p(), reg_class_contents, targetm, TEST_HARD_REG_BIT, and tick.

Referenced by rename_chains().

◆ free_chain_data()

static void free_chain_data ( void )
static

References bitmap_clear(), i, and id_to_chain.

Referenced by regrename_finish().

◆ hide_operands()

static void hide_operands ( int n_ops,
rtx * old_operands,
rtx * old_dups,
unsigned HOST_WIDE_INT do_not_hide,
bool inout_and_ec_only )
static
Hide operands of the current insn (of which there are N_OPS) by
substituting pc for them.
Previous values are stored in the OLD_OPERANDS and OLD_DUPS.
For every bit set in DO_NOT_HIDE, we leave the operand alone.
If INOUT_AND_EC_ONLY is set, we only do this for OP_INOUT type operands
and earlyclobbers.   

References recog_data_d::constraints, recog_data_d::dup_loc, recog_data_d::dup_num, operand_alternative::earlyclobber, i, recog_data_d::n_dups, OP_INOUT, recog_data_d::operand, recog_data_d::operand_loc, recog_data_d::operand_type, pc_rtx, recog_data, and which_op_alt().

Referenced by build_def_use().

◆ init_rename_info()

◆ make_pass_regrename()

rtl_opt_pass * make_pass_regrename ( gcc::context * ctxt)

◆ mark_conflict()

static void mark_conflict ( class du_head * chains,
unsigned id )
static
Walk all chains starting with CHAINS and record that they conflict with
another chain whose id is ID.   

References bitmap_set_bit, du_head::conflicts, and du_head::next_chain.

Referenced by create_new_chain().

◆ merge_chains()

static void merge_chains ( du_head_p c1,
du_head_p c2 )
static
Merge the two chains C1 and C2 so that all conflict information is
recorded and C1, and the id of C2 is changed to that of C1.   

References bitmap_ior_into(), and NULL.

Referenced by regrename_analyze().

◆ merge_overlapping_regs()

static void merge_overlapping_regs ( HARD_REG_SET * pset,
class du_head * head )
static
For a def-use chain HEAD, find which registers overlap its lifetime and
set the corresponding bits in *PSET.   

References EXECUTE_IF_SET_IN_BITMAP, gcc_assert, i, regrename_chain_from_id(), and SET_HARD_REG_BIT.

Referenced by find_rename_reg().

◆ note_sets_clobbers()

static void note_sets_clobbers ( rtx x,
const_rtx set,
void * data )
static
Called through note_stores.  DATA points to a rtx_code, either SET or
CLOBBER, which tells us which kind of rtx to look at.  If we have a
match, record the set register in live_hard_regs and in the hard_conflicts
bitmap of open chains.   

References add_to_hard_reg_set(), gcc_assert, GET_CODE, GET_MODE, HARD_REGISTER_P, live_hard_regs, open_chains, REG_P, REGNO, and SUBREG_REG.

Referenced by build_def_use().

◆ record_operand_use()

static void record_operand_use ( class du_head * head,
struct du_chain * this_du )
static
Examine cur_operand, and if it is nonnull, record information about the
use THIS_DU which is part of the chain HEAD.   

References operand_rr_info::chains, cur_operand, operand_rr_info::failed, gcc_assert, operand_rr_info::heads, operand_rr_info::n_chains, and NULL.

Referenced by create_new_chain(), and scan_rtx_reg().

◆ record_out_operands()

static void record_out_operands ( rtx_insn * insn,
bool earlyclobber,
insn_rr_info * insn_info )
static
For each output operand of INSN, call scan_rtx to create a new
open chain.  Do this only for normal or earlyclobber outputs,
depending on EARLYCLOBBER.  If INSN_INFO is nonnull, use it to
record information about the operands in the insn.   

References alternative_class(), asm_noperands(), CALL_P, du_head::cannot_rename, du_chain::cl, cur_operand, recog_data_d::dup_loc, recog_data_d::dup_num, operand_alternative::earlyclobber, i, du_chain::loc, mark_write, recog_data_d::n_dups, recog_data_d::n_operands, NULL, insn_rr_info::op_info, OP_OUT, open_chains, recog_data_d::operand_loc, recog_data_d::operand_type, ORIGINAL_REGNO, PATTERN(), recog_data, REG_P, REGNO, scan_rtx(), terminate_write, and which_op_alt().

Referenced by build_def_use().

◆ regrename_analyze()

◆ regrename_chain_from_id()

du_head_p regrename_chain_from_id ( unsigned int id)
Return the chain corresponding to id number ID.  Take into account that
chains may have been merged.   

References du_head::id, and id_to_chain.

Referenced by merge_overlapping_regs(), and regrename_analyze().

◆ regrename_do_replace()

bool regrename_do_replace ( class du_head * head,
int reg )
Attempt to replace all uses of the register in the chain beginning with
HEAD with REG.  Returns true on success and false if the replacement is
rejected because the insns would not validate.  The latter can happen
e.g. if a match_parallel predicate enforces restrictions on register
numbering in its subpatterns.   

References apply_change_group(), DEBUG_INSN_P, head::first, gen_raw_REG(), gen_rtx_UNKNOWN_VAR_LOC, GET_MODE, hard_regno_nregs(), INSN_VAR_LOCATION_LOC, NULL_RTX, ORIGINAL_REGNO, REG_ATTRS, REG_POINTER, REGNO, and validate_change().

Referenced by rename_chains().

◆ regrename_find_superclass()

reg_class regrename_find_superclass ( du_head_p head,
int * pn_uses,
HARD_REG_SET * punavailable )
Iterate over elements in the chain HEAD in order to:
1. Count number of uses, storing it in *PN_USES.
2. Narrow the set of registers we can use for renaming, adding
   unavailable registers to *PUNAVAILABLE, which must be
   initialized by the caller.
3. Compute the superunion of register classes in this chain
   and return it.   

References DEBUG_INSN_P, head::first, and reg_class_superunion.

Referenced by rename_chains().

◆ regrename_finish()

void regrename_finish ( void )
Free all global data used by the register renamer.   

References free_chain_data(), insn_rr, NULL, and rename_obstack.

Referenced by regrename_optimize().

◆ regrename_init()

void regrename_init ( bool insn_info)
Initialize the register renamer.  If INSN_INFO is true, ensure that
insn_rr is nonnull.   

References gcc_obstack_init, get_max_uid(), insn_rr, and rename_obstack.

Referenced by regrename_optimize().

◆ regrename_optimize()

static unsigned int regrename_optimize ( void )
static

◆ rename_chains()

◆ restore_operands()

static void restore_operands ( rtx_insn * insn,
int n_ops,
rtx * old_operands,
rtx * old_dups )
static
Undo the substitution performed by hide_operands.  INSN is the insn we
are processing; the arguments are the same as in hide_operands.   

References df_insn_rescan(), recog_data_d::dup_loc, i, du_chain::insn, recog_data_d::n_dups, recog_data_d::operand_loc, and recog_data.

Referenced by build_def_use().

◆ scan_rtx()

◆ scan_rtx_address()

static void scan_rtx_address ( rtx_insn * insn,
rtx * loc,
enum reg_class cl,
enum scan_actions action,
machine_mode mode,
addr_space_t as )
static
Adapted from find_reloads_address_1.  CL is INDEX_REG_CLASS or
BASE_REG_CLASS depending on how the register is being considered.   

References AUTO_INC_DEC, base_reg_class_for_rename(), du_chain::cl, DEBUG_INSN_P, GET_CODE, GET_MODE, GET_RTX_FORMAT, GET_RTX_LENGTH, i, du_chain::insn, du_chain::loc, mark_access, mark_all_read, mark_write, MEM_ADDR_SPACE, NULL, OP_IN, REGNO, regno_ok_for_base_p(), RTX_CODE, scan_rtx_address(), scan_rtx_reg(), SUBREG_REG, XEXP, XVECEXP, and XVECLEN.

Referenced by build_def_use(), scan_rtx(), and scan_rtx_address().

◆ scan_rtx_reg()

◆ set_incoming_from_chain()

static void set_incoming_from_chain ( class bb_rename_info * ri,
du_head_p chain )
static
Record in RI that the block corresponding to it has an incoming
live value, described by CHAIN.   

References dump_file, i, bb_rename_info::incoming, incoming_reg_info::nregs, and incoming_reg_info::unusable.

Referenced by regrename_analyze().

◆ verify_reg_in_set()

static bool verify_reg_in_set ( rtx op,
HARD_REG_SET * pset )
static
Return true if OP is a reg for which all bits are set in PSET, false
if all bits are clear.
In other cases, set fail_current_block and return false.   

References fail_current_block, REG_NREGS, REG_P, REGNO, and TEST_HARD_REG_BIT.

Referenced by build_def_use(), and verify_reg_tracked().

◆ verify_reg_tracked()

static bool verify_reg_tracked ( rtx op)
static
Return true if OP is a reg that is being tracked already in some form.
May set fail_current_block if it sees an unhandled case of overlap.   

References live_hard_regs, live_in_chains, and verify_reg_in_set().

Referenced by build_def_use(), and scan_rtx().

Variable Documentation

◆ cur_operand

operand_rr_info* cur_operand
static
Set while scanning RTL if INSN_RR is nonnull, i.e. if the current analysis
is for a caller that requires operand data.  Used in
record_operand_use.   

Referenced by build_def_use(), record_operand_use(), and record_out_operands().

◆ current_id

unsigned current_id
static
The id to be given to the next opened chain.   

Referenced by create_new_chain(), and regrename_analyze().

◆ fail_current_block

bool fail_current_block
static
True if we found a register with a size mismatch, which means that we
can't track its lifetime accurately.  If so, we abort the current block
without renaming.   

Referenced by build_def_use(), scan_rtx_reg(), and verify_reg_in_set().

◆ id_to_chain

vec<du_head_p> id_to_chain
static

◆ insn_rr

vec<insn_rr_info> insn_rr
If nonnull, the code calling into the register renamer requested
information about insn operands, and we store it here.   

Referenced by build_def_use(), regrename_analyze(), regrename_finish(), and regrename_init().

◆ live_hard_regs

HARD_REG_SET live_hard_regs
static
Record the registers that are live but not tracked.  The intersection
between this and live_in_chains is empty.   

Referenced by build_def_use(), create_new_chain(), do_remat(), inherit_in_ebb(), init_rename_info(), note_sets_clobbers(), scan_rtx_reg(), and verify_reg_tracked().

◆ live_in_chains

HARD_REG_SET live_in_chains
static
Record the registers being tracked in open_chains.   

Referenced by build_def_use(), create_new_chain(), init_rename_info(), scan_rtx_reg(), and verify_reg_tracked().

◆ open_chains

class du_head* open_chains
static

◆ open_chains_set

bitmap_head open_chains_set
static
Bitmap of open chains.  The bits set always match the list found in
open_chains.   

Referenced by create_new_chain(), init_rename_info(), regrename_analyze(), and scan_rtx_reg().

◆ rename_obstack

struct obstack rename_obstack
static

◆ scan_actions_name

const char* const scan_actions_name[]
static
Initial value:
=
{
"terminate_write",
"terminate_dead",
"mark_all_read",
"mark_read",
"mark_write",
"mark_access"
}

Referenced by scan_rtx_reg().

◆ terminated_this_insn

class du_head* terminated_this_insn
static
Set while scanning RTL if a register dies.  Used to tie chains.   

Referenced by build_def_use(), and scan_rtx_reg().

◆ this_tick

int this_tick = 0
static

Referenced by rename_chains().

◆ tick

int tick[FIRST_PSEUDO_REGISTER]
static
TICK and THIS_TICK are used to record the last time we saw each
register.   

Referenced by find_rename_reg(), get_last_value_validate(), graphds_dfs(), and rename_chains().