GCC Middle and Back End API Reference
lra-remat.cc File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "rtl.h"
#include "df.h"
#include "insn-config.h"
#include "regs.h"
#include "memmodel.h"
#include "ira.h"
#include "recog.h"
#include "lra.h"
#include "lra-int.h"
#include "function-abi.h"
Include dependency graph for lra-remat.cc:

Data Structures

struct  cand
 
class  remat_bb_data
 

Typedefs

typedef struct candcand_t
 
typedef const struct candconst_cand_t
 
typedef class remat_bb_dataremat_bb_data_t
 

Functions

static remat_bb_data_t get_remat_bb_data (basic_block bb)
 
static remat_bb_data_t get_remat_bb_data_by_index (int index)
 
static hashval_t cand_hash (const void *cand)
 
static int cand_eq_p (const void *cand1, const void *cand2)
 
static cand_t insert_cand (cand_t cand)
 
static void free_cand (void *cand)
 
static void initiate_cand_table (void)
 
static void finish_cand_table (void)
 
static bool bad_for_rematerialization_p (rtx x)
 
static int operand_to_remat (rtx_insn *insn)
 
static void create_cand (rtx_insn *insn, int nop, int regno, rtx_insn *activation=NULL)
 
static void create_cands (void)
 
static void create_remat_bb_data (void)
 
static void dump_cands (FILE *dump_file)
 
static void dump_candidates_and_remat_bb_data (void)
 
static void finish_remat_bb_data (void)
 
static void set_bb_regs (basic_block bb, rtx_insn *insn)
 
static void calculate_local_reg_remat_bb_data (void)
 
static bool reg_overlap_for_remat_p (lra_insn_reg *reg, rtx_insn *insn)
 
static bool call_used_input_regno_present_p (const function_abi &abi, rtx_insn *insn)
 
static void calculate_livein_cands (void)
 
static void calculate_gen_cands (void)
 
static bool cand_trans_fun (int bb_index, bitmap bb_in, bitmap bb_out)
 
static bool cand_pav_trans_fun (int bb_index)
 
static void cand_pav_con_fun_0 (basic_block bb)
 
static bool cand_pav_con_fun_n (edge e)
 
static bool cand_av_trans_fun (int bb_index)
 
static void cand_av_con_fun_0 (basic_block bb)
 
static bool cand_av_con_fun_n (edge e)
 
static void calculate_global_remat_bb_data (void)
 
static void change_sp_offset (rtx_insn *insns, poly_int64 sp_offset)
 
static int get_hard_regs (struct lra_insn_reg *reg, int &nregs)
 
static void update_scratch_ops (rtx_insn *remat_insn)
 
static bool do_remat (void)
 
bool lra_remat (void)
 

Variables

static unsigned int cands_num
 
static bitmap_head temp_bitmap
 
static bitmap_head subreg_regs
 
static vec< cand_tall_cands
 
static cand_tinsn_to_cand
 
static cand_tinsn_to_cand_activation
 
static cand_tregno_cands
 
static bitmap_head all_blocks
 
static remat_bb_data_t remat_bb_data
 
static htab_t cand_table
 
int lra_rematerialization_iter
 

Typedef Documentation

◆ cand_t

◆ const_cand_t

◆ remat_bb_data_t

Array for all BB data.  Indexed by the corresponding BB index.   

Function Documentation

◆ bad_for_rematerialization_p()

static bool bad_for_rematerialization_p ( rtx x)
static
Return true if X contains memory or some UNSPEC.  We cannot just
check insn operands as memory or unspec might be not an operand
itself but contain an operand.  Insn with memory access is not
profitable for rematerialization.  Rematerialization of UNSPEC
might result in wrong code generation as the UNPEC effect is
unknown (e.g. generating a label).   

References bad_for_rematerialization_p(), GET_CODE, GET_RTX_FORMAT, GET_RTX_LENGTH, ggc_alloc(), i, MEM_P, XEXP, XVECEXP, and XVECLEN.

Referenced by bad_for_rematerialization_p(), and operand_to_remat().

◆ calculate_gen_cands()

◆ calculate_global_remat_bb_data()

◆ calculate_livein_cands()

◆ calculate_local_reg_remat_bb_data()

static void calculate_local_reg_remat_bb_data ( void )
static
Calculate changed_regs and dead_regs for each BB.   

References cfun, FOR_BB_INSNS, FOR_EACH_BB_FN, NONDEBUG_INSN_P, and set_bb_regs().

Referenced by lra_remat().

◆ call_used_input_regno_present_p()

static bool call_used_input_regno_present_p ( const function_abi & abi,
rtx_insn * insn )
static

◆ cand_av_con_fun_0()

static void cand_av_con_fun_0 ( basic_block bb)
static
The confluence function used by the DF equation solver to set up
cand_av info for a block BB without predecessor.   

References bitmap_clear(), and get_remat_bb_data().

Referenced by calculate_global_remat_bb_data().

◆ cand_av_con_fun_n()

static bool cand_av_con_fun_n ( edge e)
static
The confluence function used by the DF equation solver to propagate
 cand_av info from predecessor to successor on edge E (pred->bb)
 according to the following equation:

    bb.avin_cands = 0 for entry block | AND (avout_cands of predecessors)

References bitmap_and_into(), get_remat_bb_data(), and ggc_alloc().

Referenced by calculate_global_remat_bb_data().

◆ cand_av_trans_fun()

static bool cand_av_trans_fun ( int bb_index)
static
The transfer function used by the DF equation solver to propagate
  candidate availability info through block with BB_INDEX according
  to the following equation:

     bb.avout =  ((bb.avin & bb.livein) - bb.killed) OR  bb.gen

References cand_trans_fun(), get_remat_bb_data_by_index(), and ggc_alloc().

Referenced by calculate_global_remat_bb_data().

◆ cand_eq_p()

static int cand_eq_p ( const void * cand1,
const void * cand2 )
static
Equal function for candidates CAND1 and CAND2.  They are equal if
the corresponding candidate insns have the same code, the same
regno for rematerialization, the same input operands.   

References gcc_assert, ggc_alloc(), i, INSN_CODE, lra_get_insn_recog_data(), and OP_IN.

Referenced by initiate_cand_table().

◆ cand_hash()

static hashval_t cand_hash ( const void * cand)
static
Hash function for candidate CAND.   

References ggc_alloc(), i, cand::insn, lra_get_insn_recog_data(), cand::nop, OP_IN, and cand::regno.

Referenced by initiate_cand_table().

◆ cand_pav_con_fun_0()

static void cand_pav_con_fun_0 ( basic_block bb)
static
The confluence function used by the DF equation solver to set up
cand_pav info for a block BB without predecessor.   

References bitmap_clear(), and get_remat_bb_data().

Referenced by calculate_global_remat_bb_data().

◆ cand_pav_con_fun_n()

static bool cand_pav_con_fun_n ( edge e)
static
The confluence function used by the DF equation solver to propagate
 partial candidate availability info from predecessor to successor
 on edge E (pred->bb) according to the following equation:

    bb.pavin_cands = 0 for entry block | OR (pavout_cands of predecessors)

References bitmap_ior_into(), get_remat_bb_data(), and ggc_alloc().

Referenced by calculate_global_remat_bb_data().

◆ cand_pav_trans_fun()

static bool cand_pav_trans_fun ( int bb_index)
static
The transfer function used by the DF equation solver to propagate
  partial candidate availability info through block with BB_INDEX
  according to the following equation:

    bb.pavout = ((bb.pavin & bb.livein) - bb.killed) OR bb.gen

References cand_trans_fun(), get_remat_bb_data_by_index(), and ggc_alloc().

Referenced by calculate_global_remat_bb_data().

◆ cand_trans_fun()

static bool cand_trans_fun ( int bb_index,
bitmap bb_in,
bitmap bb_out )
static
The common transfer function used by the DF equation solver to
  propagate (partial) availability info BB_IN to BB_OUT through block
  with BB_INDEX according to the following equation:

     bb.out =  ((bb.in & bb.livein) - bb.killed) OR  bb.gen

References all_cands, bitmap_bit_p, bitmap_clear(), bitmap_ior_and_compl(), bitmap_set_bit, EXECUTE_IF_SET_IN_BITMAP, get_remat_bb_data_by_index(), ggc_alloc(), cand::insn, lra_get_insn_recog_data(), lra_insn_reg::next, NULL, OP_OUT, lra_insn_reg::regno, cand::regno, temp_bitmap, and lra_insn_reg::type.

Referenced by cand_av_trans_fun(), and cand_pav_trans_fun().

◆ change_sp_offset()

static void change_sp_offset ( rtx_insn * insns,
poly_int64 sp_offset )
static
Setup sp offset attribute to SP_OFFSET for all INSNS.   

References eliminate_regs_in_insn(), insns, NEXT_INSN(), and NULL.

Referenced by do_remat().

◆ create_cand()

static void create_cand ( rtx_insn * insn,
int nop,
int regno,
rtx_insn * activation = NULL )
static
Create candidate for INSN with rematerialization operand NOP and
REGNO.  Insert the candidate into the table and set up the
corresponding INSN_TO_CAND element.   

References all_cands, free(), gcc_assert, ggc_alloc(), cand::index, insert_cand(), cand::insn, insn_to_cand, insn_to_cand_activation, INSN_UID(), lra_get_insn_recog_data(), cand::next_regno_cand, cand::nop, REG_P, lra_insn_reg::regno, cand::regno, REGNO, regno_cands, and cand::reload_regno.

Referenced by create_cands().

◆ create_cands()

◆ create_remat_bb_data()

static void create_remat_bb_data ( void )
static

◆ do_remat()

◆ dump_candidates_and_remat_bb_data()

◆ dump_cands()

static void dump_cands ( FILE * dump_file)
static

◆ finish_cand_table()

static void finish_cand_table ( void )
static
Finish the candidate table.   

References cand_table, and ggc_alloc().

Referenced by lra_remat().

◆ finish_remat_bb_data()

static void finish_remat_bb_data ( void )
static
Free all BB data.   

References bitmap_clear(), cfun, FOR_EACH_BB_FN, free(), and get_remat_bb_data().

Referenced by lra_remat().

◆ free_cand()

static void free_cand ( void * cand)
static
Free candidate CAND memory.   

References free().

Referenced by initiate_cand_table().

◆ get_hard_regs()

static int get_hard_regs ( struct lra_insn_reg * reg,
int & nregs )
static
Return start hard register of REG (can be a hard or a pseudo reg)
or -1 (if it is a spilled pseudo).  Return number of hard registers
occupied by REG through parameter NREGS if the start hard reg is
not negative.   

References lra_insn_reg::biggest_mode, ggc_alloc(), hard_regno_nregs(), reg_renumber, and lra_insn_reg::regno.

Referenced by do_remat().

◆ get_remat_bb_data()

◆ get_remat_bb_data_by_index()

static remat_bb_data_t get_remat_bb_data_by_index ( int index)
inlinestatic

◆ initiate_cand_table()

static void initiate_cand_table ( void )
static
Initiate the candidate table.   

References cand_eq_p(), cand_hash(), cand_table, free_cand(), and ggc_alloc().

Referenced by lra_remat().

◆ insert_cand()

static cand_t insert_cand ( cand_t cand)
static
Insert candidate CAND into the table if it is not there yet.
Return candidate which is in the table.   

References cand_table, ggc_alloc(), and NULL.

Referenced by create_cand().

◆ lra_remat()

◆ operand_to_remat()

static int operand_to_remat ( rtx_insn * insn)
static
If INSN cannot be used for rematerialization, return negative
value.  If INSN can be considered as a candidate for
rematerialization, return value which is the operand number of the
pseudo for which the insn can be used for rematerialization.  Here
we consider the insns without any memory, spilled pseudo (except
for the rematerialization pseudo), or dying or unused regs.   

References bad_for_rematerialization_p(), lra_insn_reg::biggest_mode, bitmap_bit_p, CALL_P, end_hard_regno(), find_regno_note(), frame_pointer_needed, ggc_alloc(), i, JUMP_P, lra_get_insn_recog_data(), lra_insn_reg::next, NULL, OP_IN, OP_INOUT, OP_OUT, PATTERN(), REG_P, reg_renumber, lra_insn_reg::regno, REGNO, lra_insn_reg::subreg_p, subreg_regs, and lra_insn_reg::type.

Referenced by create_cands().

◆ reg_overlap_for_remat_p()

static bool reg_overlap_for_remat_p ( lra_insn_reg * reg,
rtx_insn * insn )
static
Return true if REG overlaps an input operand or non-input hard register of
INSN.  Basically the function returns false if we can move rematerialization
candidate INSN through another insn with output REG or dead input REG (we
consider it to avoid extending reg live range) with possible output pseudo
renaming in INSN.   

References lra_insn_reg::biggest_mode, ggc_alloc(), hard_regno_nregs(), lra_get_insn_recog_data(), NULL, OP_IN, reg_renumber, and lra_insn_reg::regno.

Referenced by calculate_gen_cands(), and do_remat().

◆ set_bb_regs()

◆ update_scratch_ops()

static void update_scratch_ops ( rtx_insn * remat_insn)
static
Make copy of and register scratch pseudos in rematerialized insn
REMAT_INSN.   

References GET_MODE, ggc_alloc(), i, ira_former_scratch_p(), ira_register_new_scratch_op(), lra_create_new_reg(), lra_get_allocno_class(), lra_get_insn_recog_data(), NULL, REG_P, and REGNO.

Referenced by do_remat().

Variable Documentation

◆ all_blocks

bitmap_head all_blocks
static
Basic blocks for data flow problems -- all bocks except the special
ones.   

Referenced by calculate_global_remat_bb_data(), and lra_remat().

◆ all_cands

vec<cand_t> all_cands
static
Vector containing all candidates.   

Referenced by calculate_livein_cands(), cand_trans_fun(), create_cand(), create_cands(), do_remat(), dump_cands(), and lra_remat().

◆ cand_table

htab_t cand_table
static
Hash table for the candidates.  Different insns (e.g. structurally
the same insns or even insns with different unused output regs) can
be represented by the same candidate in the table.   

Referenced by finish_cand_table(), initiate_cand_table(), and insert_cand().

◆ cands_num

unsigned int cands_num
static
Rematerialize pseudos values.
   Copyright (C) 2014-2024 Free Software Foundation, Inc.
   Contributed by Vladimir Makarov <vmakarov@redhat.com>.

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 code objective is to rematerialize spilled pseudo values.  To
do this we calculate available insn candidates.  The candidate is
available at some point if there is dominated set of insns with the
same pattern, the insn inputs are not dying or modified on any path
from the set, the outputs are not modified.

The insns containing memory or spilled pseudos (except for the
rematerialized pseudo) are not considered as such insns are not
profitable in comparison with regular loads of spilled pseudo
values.  That simplifies the implementation as we don't need to
deal with memory aliasing.

To speed up available candidate calculation, we calculate partially
available candidates first and use them for initialization of the
availability.  That is because (partial) availability sets are
sparse.

The rematerialization sub-pass could be improved further in the
following ways:

o We could make longer live ranges of inputs in the
  rematerialization candidates if their hard registers are not used
  for other purposes.  This could be complicated if we need to
  update BB live info information as LRA does not use
  DF-infrastructure for compile-time reasons.  This problem could
  be overcome if constrain making live ranges longer only in BB/EBB
  scope.
o We could use cost-based decision to choose rematerialization insn
  (currently all insns without memory is can be used).
o We could use other free hard regs for unused output pseudos in
  rematerialization candidates although such cases probably will
  be very rare.   
Number of candidates for rematerialization.   

Referenced by calculate_livein_cands(), create_cands(), and dump_cands().

◆ insn_to_cand

cand_t* insn_to_cand
static
Map: insn -> candidate representing it.  It is null if the insn cannot
be used for rematerialization.   

Referenced by calculate_gen_cands(), create_cand(), do_remat(), and lra_remat().

◆ insn_to_cand_activation

cand_t* insn_to_cand_activation
static
A secondary map, for candidates that involve two insns, where the
second one makes the equivalence.  The candidate must not be used
before seeing this activation insn.   

Referenced by create_cand(), do_remat(), and lra_remat().

◆ lra_rematerialization_iter

int lra_rematerialization_iter
Current number of rematerialization iteration.   

Referenced by lra(), and lra_remat().

◆ regno_cands

cand_t* regno_cands
static
Map regno -> candidates can be used for the regno
rematerialization.   

Referenced by create_cand(), do_remat(), and lra_remat().

◆ remat_bb_data

remat_bb_data_t remat_bb_data
static
All basic block data are referred through the following array.   

◆ subreg_regs

bitmap_head subreg_regs
static
Registers accessed via subreg_p.   

Referenced by dump_candidates_and_remat_bb_data(), lra_remat(), operand_to_remat(), and set_bb_regs().

◆ temp_bitmap

bitmap_head temp_bitmap
static
Bitmap used for different calculations.   

Referenced by calculate_gen_cands(), cand_trans_fun(), do_remat(), and lra_remat().