LLVM 24.0.0git
llvm::Rematerializer Class Reference

MIR-level target-independent rematerializer. More...

#include "llvm/CodeGen/Rematerializer.h"

Classes

struct  DependencyReuseInfo
 When rematerializating a register (called the "root" register in this context) to a given position, we must decide what to do with all its rematerializable dependencies (for unrematerializable dependencies, we have no choice but to re-use the same register). More...
class  Listener
 Rematerializer listener. More...
struct  Reg
 A rematerializable register defined by a single machine instruction. More...

Public Types

using RegisterIdx = unsigned
 Index type for rematerializable registers.
using RegionBoundaries
 A region's boundaries i.e.
using RematsOf = SmallDenseSet<RegisterIdx, 4>

Public Member Functions

LLVM_ABI Rematerializer (MachineFunction &MF, SmallVectorImpl< RegionBoundaries > &Regions, LiveIntervals &LIS)
 Simply initializes some internal state, does not identify rematerialization candidates.
LLVM_ABI bool analyze ()
 Goes through the whole MF and identifies all rematerializable registers.
void addListener (Listener *Listen)
 Adds a new listener to the rematerializer.
void removeListener (Listener *Listen)
 Removes a listener from the rematerializer.
void clearListeners ()
 Removes all listeners from the rematerializer.
const ReggetReg (RegisterIdx RegIdx) const
ArrayRef< ReggetRegs () const
unsigned getNumRegs () const
bool isPermanentlyDead (RegisterIdx RegIdx) const
 Determines whether register RegIdx fully disappeared from the MIR.
const RegionBoundariesgetRegion (RegisterIdx RegionIdx) const
unsigned getNumRegions () const
bool isOriginalRegister (RegisterIdx RegIdx) const
 Whether register RegIdx is an original register.
bool isRematerializedRegister (RegisterIdx RegIdx) const
 Whether register RegIdx is a rematerialization of some original register.
RegisterIdx getOriginOf (RegisterIdx RematRegIdx) const
 Returns the origin index of rematerializable register RegIdx.
RegisterIdx getOriginOrSelf (RegisterIdx RegIdx) const
 If RegIdx is a rematerialization, returns its origin's index.
ArrayRef< std::pair< Register, LaneBitmask > > getUnrematableDeps (RegisterIdx RegIdx) const
 Returns unreamaterializable read lanes of register operands for register RegIdx.
LLVM_ABI RegisterIdx getDefRegIdx (const MachineInstr &MI) const
 If MI's first operand defines a register and that register is a rematerializable register tracked by the rematerializer, returns its index in the Regs vector.
LLVM_ABI RegisterIdx rematerializeToRegion (RegisterIdx RootIdx, unsigned UseRegion, DependencyReuseInfo &DRI)
 Rematerializes register RootIdx just before its first user inside region UseRegion (or at the end of the region if it has no user), transfers all its users in the region to the new register, and returns the latter's index.
LLVM_ABI RegisterIdx rematerializeToPos (RegisterIdx RootIdx, unsigned UseRegion, MachineBasicBlock::iterator InsertPos, DependencyReuseInfo &DRI)
 Rematerializes register RootIdx before position InsertPos in UseRegion and returns the new register's index.
LLVM_ABI RegisterIdx rematerializeReg (RegisterIdx RegIdx, unsigned UseRegion, MachineBasicBlock::iterator InsertPos, SmallVectorImpl< RegisterIdx > &&Dependencies)
 Rematerializes register RegIdx before InsertPos in UseRegion, adding the new rematerializable register to the backing vector Regs and returning its index inside the vector.
LLVM_ABI void recreateReg (RegisterIdx RegIdx, MachineBasicBlock::iterator InsertPos, Register DefReg)
 Re-creates a previously deleted register RegIdx before InsertPos, which must be in the register's original defining region.
LLVM_ABI void transferRegionUsers (RegisterIdx FromRegIdx, RegisterIdx ToRegIdx, unsigned UseRegion)
 Transfers all users of register FromRegIdx in region UseRegion to ToRegIdx, the latter of which must be a rematerialization of the former or have the same origin register.
LLVM_ABI void transferUser (RegisterIdx FromRegIdx, RegisterIdx ToRegIdx, unsigned UserRegion, MachineInstr &UserMI)
 Transfers user UserMI in region UserRegion from register FromRegIdx to ToRegIdx, the latter of which must be a rematerialization of the former or have the same origin register.
LLVM_ABI void transferAllUsers (RegisterIdx FromRegIdx, RegisterIdx ToRegIdx)
 Transfers all users of register FromRegIdx to register ToRegIdx, the latter of which must be a rematerialization of the former or have the same origin register.
LLVM_ABI bool isMOIdenticalAtUses (MachineOperand &MO, ArrayRef< SlotIndex > Uses) const
 Determines whether (sub-)register operand MO has the same value at all Uses as at MO.
LLVM_ABI bool isRegIdenticalAtUses (Register Reg, LaneBitmask Mask, SlotIndex RefSlot, ArrayRef< SlotIndex > Uses) const
 Determines whether lanes Mask of register Reg habe the same value at all Uses as at RefSlot.
LLVM_ABI RegisterIdx findRematInRegion (RegisterIdx RegIdx, unsigned Region, SlotIndex Before) const
 Finds the closest rematerialization of register RegIdx in region Region that exists before slot Before.
LLVM_ABI Printable printDependencyDAG (RegisterIdx RootIdx) const
LLVM_ABI Printable printID (RegisterIdx RegIdx) const
LLVM_ABI Printable printRematReg (RegisterIdx RegIdx, bool SkipRegions=false) const
LLVM_ABI Printable printRegUsers (RegisterIdx RegIdx) const
LLVM_ABI Printable printUser (const MachineInstr *MI, std::optional< unsigned > UseRegion=std::nullopt) const

Static Public Attributes

static constexpr unsigned NoReg = ~0
 Error value for register indices.

Detailed Description

MIR-level target-independent rematerializer.

Provides an API to identify and rematerialize registers within a machine function.

At the moment this supports rematerializing registers that meet all of the following constraints.

  1. The register is virtual and has a single defining instruction.
  2. The single defining instruction is deemed rematerializable by the TII and doesn't have any physical register use that is both non-constant and non-ignorable.
  3. The register has at least one non-debug use that is inside or at a region boundary (see below for what we consider to be a region).

Rematerializable registers (represented by Rematerializer::Reg) form a DAG of their own, with every register having incoming edges from all rematerializable registers which are read by the instruction defining it. It is possible to rematerialize registers with unrematerializable dependencies; however the latter are not considered part of this DAG since their position/identity never change and therefore do not require the same level of tracking.

Each register has a "dependency DAG" which is defined as the subset of nodes in the overall DAG that have at least one path to the register, which is called the "root" register in this context. Semantically, these nodes are the registers which are involved into the computation of the root register i.e., all of its transitive dependencies. We use the term "root" because all paths within the dependency DAG of a register terminate at it; however, there may be multiple paths between a non-root node and the root node, so a dependency DAG is not always a tree.

The API uses dense unsigned integers starting at 0 to reference rematerializable registers. These indices are immutable i.e., even when registers are deleted their respective integer handle remain valid. Method which perform actual rematerializations should however be assumed to invalidate addresses to Rematerializer::Reg objects.

The rematerializer tracks def/use points of registers based on regions. These are alike the regions the machine scheduler works on. A region is simply a pair on MBB iterators encoding a range of machine instructions. The first iterator (beginning of the region) is inclusive whereas the second iterator (end of the region) is exclusive and can either point to a MBB's end sentinel or an actual MI (not necessarily a terminator). Regions must be non-empty, cannot overlap, and cannot contain terminators. However, they do not have to cover the whole function.

The API uses dense unsigned integers starting at 0 to reference regions. These map directly to the indices of the corresponding regions in the region vector passed during construction.

The rematerializer supports rematerializing arbitrary complex DAGs of registers to regions where these registers are used, with the option of re-using non-root registers or their previous rematerializations instead of rematerializing them again.

Throughout its lifetime, the rematerializer tracks new registers it creates (which are rematerializable by construction) and their relations to other registers. It performs DAG and live interval updates immediately on rematerialization and/or user transfer. Importantly, missing dead flags on partial definitions of unrematerializable registers can yield dead definitions when rematerializing their users. They are deleted to preserve live interval validity. These deletions can cascade to other (un)rematerializable registers that also become dead as a result.

In its nomenclature, the rematerializer differentiates between "original registers" (registers that were present when it analyzed the function) and rematerializations of these original registers. Rematerializations have an "origin" which is the index of the original regiser they were rematerialized from (transitivity applies; a rematerialization and all of its own rematerializations have the same origin). Semantically, only original registers have rematerializations.

Definition at line 96 of file Rematerializer.h.

Member Typedef Documentation

◆ RegionBoundaries

Initial value:
std::pair<MachineBasicBlock::iterator, MachineBasicBlock::iterator>

A region's boundaries i.e.

a pair of instruction bundle iterators. The lower boundary is inclusive, the upper boundary is exclusive.

Definition at line 199 of file Rematerializer.h.

◆ RegisterIdx

Index type for rematerializable registers.

Definition at line 99 of file Rematerializer.h.

◆ RematsOf

Constructor & Destructor Documentation

◆ Rematerializer()

Rematerializer::Rematerializer ( MachineFunction & MF,
SmallVectorImpl< RegionBoundaries > & Regions,
LiveIntervals & LIS )

Member Function Documentation

◆ addListener()

void llvm::Rematerializer::addListener ( Listener * Listen)
inline

Adds a new listener to the rematerializer.

Definition at line 215 of file Rematerializer.h.

References assert(), and llvm_unreachable.

◆ analyze()

bool Rematerializer::analyze ( )

Goes through the whole MF and identifies all rematerializable registers.

Returns whether there is any rematerializable register in regions.

Maps all MIs to their parent region. Region terminators are considered part of the region they terminate.

Definition at line 420 of file Rematerializer.cpp.

References assert(), llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::contains(), llvm::dbgs(), getNumRegs(), llvm::RegionBase< Tr >::getParent(), I, llvm::DenseMapBase< DerivedT, KeyT, ValueT, KeyInfoT, BucketT >::insert(), LLVM_DEBUG, MBB, MI, and printDependencyDAG().

◆ clearListeners()

void llvm::Rematerializer::clearListeners ( )
inline

Removes all listeners from the rematerializer.

Definition at line 228 of file Rematerializer.h.

◆ findRematInRegion()

RegisterIdx Rematerializer::findRematInRegion ( RegisterIdx RegIdx,
unsigned Region,
SlotIndex Before ) const

Finds the closest rematerialization of register RegIdx in region Region that exists before slot Before.

If no such rematerialization exists, returns Rematerializer::NoReg.

Definition at line 222 of file Rematerializer.cpp.

References llvm::Rematerializer::Reg::DefMI, llvm::Rematerializer::Reg::DefRegion, getOriginOrSelf(), getReg(), llvm::SlotIndex::getRegSlot(), NoReg, and llvm::Rematerializer::Reg::Uses.

◆ getDefRegIdx()

RegisterIdx Rematerializer::getDefRegIdx ( const MachineInstr & MI) const

If MI's first operand defines a register and that register is a rematerializable register tracked by the rematerializer, returns its index in the Regs vector.

Otherwise returns Rematerializer::NoReg.

Definition at line 556 of file Rematerializer.cpp.

References MI, and NoReg.

Referenced by printUser().

◆ getNumRegions()

unsigned llvm::Rematerializer::getNumRegions ( ) const
inline

Definition at line 249 of file Rematerializer.h.

◆ getNumRegs()

unsigned llvm::Rematerializer::getNumRegs ( ) const
inline

Definition at line 235 of file Rematerializer.h.

Referenced by analyze().

◆ getOriginOf()

RegisterIdx llvm::Rematerializer::getOriginOf ( RegisterIdx RematRegIdx) const
inline

Returns the origin index of rematerializable register RegIdx.

Definition at line 262 of file Rematerializer.h.

References assert(), and isRematerializedRegister().

Referenced by getOriginOrSelf(), recreateReg(), and llvm::Rollbacker::rematerializerNoteRegCreated().

◆ getOriginOrSelf()

RegisterIdx llvm::Rematerializer::getOriginOrSelf ( RegisterIdx RegIdx) const
inline

If RegIdx is a rematerialization, returns its origin's index.

If it is an original register's index, returns the same index.

Definition at line 268 of file Rematerializer.h.

References getOriginOf(), and isRematerializedRegister().

Referenced by findRematInRegion(), getUnrematableDeps(), isPermanentlyDead(), and rematerializeReg().

◆ getReg()

◆ getRegion()

const RegionBoundaries & llvm::Rematerializer::getRegion ( RegisterIdx RegionIdx) const
inline

Definition at line 245 of file Rematerializer.h.

References assert().

◆ getRegs()

ArrayRef< Reg > llvm::Rematerializer::getRegs ( ) const
inline

Definition at line 234 of file Rematerializer.h.

◆ getUnrematableDeps()

ArrayRef< std::pair< Register, LaneBitmask > > llvm::Rematerializer::getUnrematableDeps ( RegisterIdx RegIdx) const
inline

Returns unreamaterializable read lanes of register operands for register RegIdx.

Definition at line 276 of file Rematerializer.h.

References getOriginOrSelf().

◆ isMOIdenticalAtUses()

bool Rematerializer::isMOIdenticalAtUses ( MachineOperand & MO,
ArrayRef< SlotIndex > Uses ) const

Determines whether (sub-)register operand MO has the same value at all Uses as at MO.

This implies that it is also available at all Uses according to its current live interval.

Definition at line 198 of file Rematerializer.cpp.

References llvm::MachineOperand::getParent(), llvm::MachineOperand::getReg(), llvm::MachineOperand::getSubReg(), isRegIdenticalAtUses(), and Uses.

◆ isOriginalRegister()

bool llvm::Rematerializer::isOriginalRegister ( RegisterIdx RegIdx) const
inline

Whether register RegIdx is an original register.

Definition at line 252 of file Rematerializer.h.

References isRematerializedRegister().

Referenced by recreateReg().

◆ isPermanentlyDead()

bool llvm::Rematerializer::isPermanentlyDead ( RegisterIdx RegIdx) const
inline

Determines whether register RegIdx fully disappeared from the MIR.

This may happen when it was only used by instructions which became dead during the rematerializer's lifetime.

Definition at line 240 of file Rematerializer.h.

References getOriginOrSelf(), getReg(), and llvm::Rematerializer::Reg::isAlive().

Referenced by llvm::Rollbacker::rollback().

◆ isRegIdenticalAtUses()

bool Rematerializer::isRegIdenticalAtUses ( Register Reg,
LaneBitmask Mask,
SlotIndex RefSlot,
ArrayRef< SlotIndex > Uses ) const

Determines whether lanes Mask of register Reg habe the same value at all Uses as at RefSlot.

This implies that it is also available at all Uses according to its current live interval.

Definition at line 208 of file Rematerializer.cpp.

References llvm::LiveRange::getVNInfoAt(), isIdenticalAtUse(), and Uses.

Referenced by isMOIdenticalAtUses().

◆ isRematerializedRegister()

bool llvm::Rematerializer::isRematerializedRegister ( RegisterIdx RegIdx) const
inline

Whether register RegIdx is a rematerialization of some original register.

Definition at line 257 of file Rematerializer.h.

References assert().

Referenced by getOriginOf(), getOriginOrSelf(), isOriginalRegister(), llvm::Rollbacker::rematerializerNoteRegCreated(), and llvm::Rollbacker::rematerializerNoteRegWillBeDeleted().

◆ printDependencyDAG()

◆ printID()

◆ printRegUsers()

Printable Rematerializer::printRegUsers ( RegisterIdx RegIdx) const

Definition at line 942 of file Rematerializer.cpp.

References getReg(), MI, printUser(), Users, and Uses.

Referenced by printDependencyDAG().

◆ printRematReg()

◆ printUser()

Printable Rematerializer::printUser ( const MachineInstr * MI,
std::optional< unsigned > UseRegion = std::nullopt ) const

Definition at line 951 of file Rematerializer.cpp.

References getDefRegIdx(), MI, NoReg, and printID().

Referenced by printRegUsers().

◆ recreateReg()

void Rematerializer::recreateReg ( RegisterIdx RegIdx,
MachineBasicBlock::iterator InsertPos,
Register DefReg )

Re-creates a previously deleted register RegIdx before InsertPos, which must be in the register's original defining region.

DefReg must be the original virtual register that RegIdx used to define. Dependencies are assumed to already exist in the MIR.

Definition at line 601 of file Rematerializer.cpp.

References assert(), llvm::dbgs(), llvm::Rematerializer::Reg::DefMI, llvm::Rematerializer::Reg::DefRegion, getOriginOf(), getReg(), isOriginalRegister(), LLVM_DEBUG, printID(), and printRematReg().

Referenced by llvm::Rollbacker::rollback().

◆ rematerializeReg()

RegisterIdx Rematerializer::rematerializeReg ( RegisterIdx RegIdx,
unsigned UseRegion,
MachineBasicBlock::iterator InsertPos,
SmallVectorImpl< RegisterIdx > && Dependencies )

Rematerializes register RegIdx before InsertPos in UseRegion, adding the new rematerializable register to the backing vector Regs and returning its index inside the vector.

Sets the new register's rematerializable dependencies to Dependencies (these are assumed to already exist in the MIR) and its unrematerializable dependencies to the same as RegIdx. The new register initially has no user. Since the method appends to Regs, references to elements within it should be considered invalidated across calls to this method unless the vector can be guaranteed to have enough space for an extra element.

Definition at line 568 of file Rematerializer.cpp.

References llvm::dbgs(), llvm::Rematerializer::Reg::DefMI, llvm::Rematerializer::Reg::DefRegion, llvm::Rematerializer::Reg::Dependencies, llvm::Rematerializer::Reg::getDefReg(), getOriginOrSelf(), LLVM_DEBUG, llvm::Rematerializer::Reg::Mask, printID(), and printRematReg().

Referenced by rematerializeToPos().

◆ rematerializeToPos()

RegisterIdx Rematerializer::rematerializeToPos ( RegisterIdx RootIdx,
unsigned UseRegion,
MachineBasicBlock::iterator InsertPos,
DependencyReuseInfo & DRI )

Rematerializes register RootIdx before position InsertPos in UseRegion and returns the new register's index.

The root's dependency DAG is rematerialized or re-used according to DRI.

When the method returns, DRI contains additional entries for non-root registers of the root's dependency DAG that needed to be rematerialized along the root. References to Rematerializer::Reg should be considered invalidated by calls to this method.

Definition at line 93 of file Rematerializer.cpp.

References assert(), llvm::dbgs(), llvm::Rematerializer::DependencyReuseInfo::DependencyMap, getReg(), LLVM_DEBUG, printID(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), rematerializeReg(), and rematerializeToPos().

Referenced by rematerializeToPos(), and rematerializeToRegion().

◆ rematerializeToRegion()

RegisterIdx Rematerializer::rematerializeToRegion ( RegisterIdx RootIdx,
unsigned UseRegion,
DependencyReuseInfo & DRI )

Rematerializes register RootIdx just before its first user inside region UseRegion (or at the end of the region if it has no user), transfers all its users in the region to the new register, and returns the latter's index.

The root's dependency DAG is rematerialized or re-used according to DRI.

When the method returns, DRI contains additional entries for non-root registers of the root's dependency DAG that needed to be rematerialized along the root. References to Rematerializer::Reg should be considered invalidated by calls to this method.

Definition at line 77 of file Rematerializer.cpp.

References getReg(), llvm::Rematerializer::Reg::getRegionUseBounds(), rematerializeToPos(), and transferRegionUsers().

◆ removeListener()

void llvm::Rematerializer::removeListener ( Listener * Listen)
inline

Removes a listener from the rematerializer.

Definition at line 222 of file Rematerializer.h.

References llvm_unreachable.

◆ transferAllUsers()

void Rematerializer::transferAllUsers ( RegisterIdx FromRegIdx,
RegisterIdx ToRegIdx )

Transfers all users of register FromRegIdx to register ToRegIdx, the latter of which must be a rematerialization of the former or have the same origin register.

Users of FromRegIdx must be reachable from ToRegIdx.

Definition at line 154 of file Rematerializer.cpp.

References llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::Rematerializer::Reg::Uses.

Referenced by llvm::Rollbacker::rollback().

◆ transferRegionUsers()

void Rematerializer::transferRegionUsers ( RegisterIdx FromRegIdx,
RegisterIdx ToRegIdx,
unsigned UseRegion )

Transfers all users of register FromRegIdx in region UseRegion to ToRegIdx, the latter of which must be a rematerialization of the former or have the same origin register.

Users in UseRegion must be reachable from ToRegIdx.

Definition at line 132 of file Rematerializer.cpp.

References llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::Rematerializer::Reg::Uses.

Referenced by rematerializeToRegion().

◆ transferUser()

void Rematerializer::transferUser ( RegisterIdx FromRegIdx,
RegisterIdx ToRegIdx,
unsigned UserRegion,
MachineInstr & UserMI )

Transfers user UserMI in region UserRegion from register FromRegIdx to ToRegIdx, the latter of which must be a rematerialization of the former or have the same origin register.

UserMI must be a direct user of FromRegIdx. UserMI must be reachable from ToRegIdx.

Definition at line 121 of file Rematerializer.cpp.

Member Data Documentation

◆ NoReg

unsigned llvm::Rematerializer::NoReg = ~0
staticconstexpr

Error value for register indices.

Definition at line 195 of file Rematerializer.h.

Referenced by findRematInRegion(), getDefRegIdx(), and printUser().


The documentation for this class was generated from the following files: