46#define DEBUG_TYPE "reg-scavenging"
48STATISTIC(NumScavengedRegs,
"Number of frame index regs scavenged");
51 LiveUnits.addRegMasked(Reg, LaneMask);
63 for (ScavengedInfo &
SI : Scavenged) {
71 LiveUnits.addLiveIns(MBB);
77 LiveUnits.addLiveOuts(MBB);
83 if (!
MI.isDebugInstr())
84 LiveUnits.stepBackward(
MI);
87 for (ScavengedInfo &
I : Scavenged) {
88 if (
I.Restore == &
MI) {
97 return includeReserved;
98 return !LiveUnits.available(Reg);
127static std::pair<MCPhysReg, MachineBasicBlock::iterator>
132 bool FoundTo =
false;
141 assert(From->getParent() == To->getParent() &&
142 "Target instruction is in other than current basic block, use "
143 "enterBasicBlockEnd first");
150 if (MOP.getReg().isPhysical() && MOP.isEarlyClobber())
151 Used.addReg(MOP.getReg());
165 return std::make_pair(
Reg,
MBB.end());
175 Used.accumulate(*std::next(From));
185 if (Survivor == 0 || !Used.available(Survivor)) {
193 if (AvilableReg == 0)
195 Survivor = AvilableReg;
197 if (--InstrCountDown == 0)
202 bool FoundVReg =
false;
204 if (MO.isReg() && MO.getReg().isVirtual()) {
213 if (
I ==
MBB.begin())
216 assert(
I !=
MBB.begin() &&
"Did not find target instruction while "
217 "iterating backwards");
220 return std::make_pair(Survivor, Pos);
225 while (!
MI.getOperand(i).isFI()) {
227 assert(i <
MI.getNumOperands() &&
"Instr doesn't have FrameIndex operand!");
232RegScavenger::ScavengedInfo &
238 const MachineFunction &MF = *Before->getMF();
240 unsigned NeedSize = TRI->getSpillSize(RC);
241 Align NeedAlign = TRI->getSpillAlign(RC);
243 unsigned SI = Scavenged.size(), Diff = std::numeric_limits<unsigned>::max();
245 for (
unsigned I = 0;
I < Scavenged.size(); ++
I) {
246 if (Scavenged[
I].
Reg != 0)
249 int FI = Scavenged[
I].FrameIndex;
250 if (FI < FIB || FI >= FIE)
254 if (NeedSize > S || NeedAlign >
A)
262 unsigned D = (S - NeedSize) + (
A.value() - NeedAlign.
value());
269 if (SI == Scavenged.size()) {
272 Scavenged.push_back(ScavengedInfo(FIE));
276 Scavenged[
SI].Reg =
Reg;
280 if (!TRI->saveScavengerRegister(*MBB, Before,
UseMI, &RC,
Reg)) {
282 int FI = Scavenged[
SI].FrameIndex;
283 if (FI < FIB || FI >= FIE) {
285 TRI->getName(
Reg) +
" from class " +
286 TRI->getRegClassName(&RC) +
287 ": Cannot scavenge register without an emergency "
290 TII->storeRegToStackSlot(*MBB, Before,
Reg,
true, FI, &RC,
Register());
294 TRI->eliminateFrameIndex(
II, SPAdj, FIOperandNum,
this);
301 TRI->eliminateFrameIndex(
II, SPAdj, FIOperandNum,
this);
303 return Scavenged[
SI];
308 bool RestoreAfter,
int SPAdj,
320 if (Reg != 0 && SpillBefore == MBB.end()) {
329 assert(Reg != 0 &&
"No register left to scavenge!");
332 RestoreAfter ? std::next(MBBI) : MBBI;
333 if (ReloadBefore != MBB.end())
335 ScavengedInfo &Scavenged = spill(Reg, RC, SPAdj, SpillBefore, ReloadBefore);
336 Scavenged.Restore = &*std::prev(SpillBefore);
337 LiveUnits.removeReg(Reg);
339 <<
" until " << *SpillBefore);
358 if (CommonMBB ==
nullptr)
360 assert(
MBB == CommonMBB &&
"All defs+uses must be in the same basic block");
363 if (!
MI.readsRegister(VReg, &
TRI)) {
364 assert((!RealDef || RealDef == &
MI) &&
365 "Can have at most one definition which is not a redefinition");
370 assert(RealDef !=
nullptr &&
"Must have at least 1 Def");
381 return !MO.getParent()->readsRegister(VReg, &TRI);
384 "Must have one definition that does not redefine vreg");
391 Register SReg = RS.scavengeRegisterBackwards(RC,
DefMI.getIterator(),
392 ReserveAfter, SPAdj);
405 RS.enterBasicBlockEnd(
MBB);
408 bool NextInstructionReadsVReg =
false;
415 if (NextInstructionReadsVReg) {
425 if (!
Reg.isVirtual() ||
Reg.virtRegIndex() >= InitialNumVirtRegs)
431 N->addRegisterKilled(SReg, &
TRI,
false);
437 NextInstructionReadsVReg =
false;
444 if (!
Reg.isVirtual() ||
Reg.virtRegIndex() >= InitialNumVirtRegs)
449 assert(!MO.isInternalRead() &&
"Cannot assign inside bundles");
450 assert((!MO.isUndef() || MO.isDef()) &&
"Cannot handle undef uses");
452 NextInstructionReadsVReg =
true;
456 I->addRegisterDead(SReg, &
TRI,
false);
462 if (!MO.isReg() || !MO.getReg().isVirtual())
464 assert(!MO.isInternalRead() &&
"Cannot assign inside bundles");
465 assert((!MO.isUndef() || MO.isDef()) &&
"Cannot handle undef uses");
466 assert(!MO.readsReg() &&
"Vreg use in first instruction not allowed");
491 LLVM_DEBUG(
dbgs() <<
"Warning: Required two scavenging passes for block "
492 <<
MBB.getName() <<
'\n');
515 bool runOnMachineFunction(MachineFunction &MF)
override {
535char ScavengerTest::ID;
538 "Scavenge virtual registers inside basic blocks",
false,
false)
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static cl::opt< unsigned > InstrLimit("dfa-instr-limit", cl::Hidden, cl::init(0), cl::desc("If present, stops packetizing after N instructions"))
const HexagonInstrInfo * TII
Register const TargetRegisterInfo * TRI
Promote Memory to Register
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static Register scavengeVReg(MachineRegisterInfo &MRI, RegScavenger &RS, Register VReg, bool ReserveAfter)
Allocate a register for the virtual register VReg.
static unsigned getFrameIndexOperandNum(MachineInstr &MI)
static bool scavengeFrameVirtualRegsInBlock(MachineRegisterInfo &MRI, RegScavenger &RS, MachineBasicBlock &MBB)
Allocate (scavenge) vregs inside a single basic block.
static std::pair< MCPhysReg, MachineBasicBlock::iterator > findSurvivorBackwards(const MachineRegisterInfo &MRI, MachineBasicBlock::iterator From, MachineBasicBlock::iterator To, const LiveRegUnits &LiveOut, ArrayRef< MCPhysReg > AllocationOrder, bool RestoreAfter)
Given the bitvector Available of free register units at position From.
This file declares the machine register scavenger class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
A set of register units used to track register liveness.
bool available(MCRegister Reg) const
Returns true if no part of physical register Reg is live.
void init(const TargetRegisterInfo &TRI)
Initialize and clear the set.
MachineInstrBundleIterator< MachineInstr > iterator
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
int getObjectIndexBegin() const
Return the minimum frame object index.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineFunctionProperties & getProperties() const
Get the function properties.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
LLVM_ABI void clearVirtRegs()
clearVirtRegs - Remove all virtual registers (after physreg assignment).
defusechain_iterator< false, true, false, true, false > def_iterator
def_iterator/def_begin/def_end - Walk all defs of the specified register.
iterator_range< def_iterator > def_operands(Register Reg) const
static def_iterator def_end()
const TargetRegisterInfo * getTargetRegisterInfo() const
iterator_range< reg_nodbg_iterator > reg_nodbg_operands(Register Reg) const
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
void enterBasicBlockEnd(MachineBasicBlock &MBB)
Start tracking liveness from the end of basic block MBB.
bool isRegUsed(Register Reg, bool includeReserved=true) const
Return if a specific register is currently used.
Register FindUnusedReg(const TargetRegisterClass *RC) const
Find an unused register of the specified register class.
void setRegUsed(Register Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Tell the scavenger a register is used.
void backward()
Update internal register state and move MBB iterator backwards.
void enterBasicBlock(MachineBasicBlock &MBB)
Start tracking liveness from the begin of basic block MBB.
Register scavengeRegisterBackwards(const TargetRegisterClass &RC, MachineBasicBlock::iterator To, bool RestoreAfter, int SPAdj, bool AllowSpill=true)
Make a register of the specific register class available from the current position backwards to the p...
BitVector getRegsAvailable(const TargetRegisterClass *RC)
Return all available registers in the register class in Mask.
Wrapper class representing virtual and physical registers.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
ArrayRef< MCPhysReg > getRawAllocationOrder(const MachineFunction &MF, bool Rev=false) const
Returns the preferred order for allocating registers from this register class in MF.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
void scavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger &RS)
Replaces all frame index virtual registers with physical registers.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.