Index: include/llvm/CodeGen/RegisterScavenging.h =================================================================== --- include/llvm/CodeGen/RegisterScavenging.h +++ include/llvm/CodeGen/RegisterScavenging.h @@ -159,9 +159,11 @@ /// This is deprecated as it depends on the quality of the kill flags being /// present; Use scavengeRegisterBackwards() instead! unsigned scavengeRegister(const TargetRegisterClass *RC, - MachineBasicBlock::iterator I, int SPAdj); - unsigned scavengeRegister(const TargetRegisterClass *RegClass, int SPAdj) { - return scavengeRegister(RegClass, MBBI, SPAdj); + MachineBasicBlock::iterator I, int SPAdj, + bool AllowSpill = true); + unsigned scavengeRegister(const TargetRegisterClass *RegClass, int SPAdj, + bool AllowSpill = true) { + return scavengeRegister(RegClass, MBBI, SPAdj, AllowSpill); } /// Make a register of the specific register class available from the current @@ -170,9 +172,13 @@ /// SPAdj is the stack adjustment due to call frame, it's passed along to /// eliminateFrameIndex(). /// Returns the scavenged register. + /// + /// If \p AllowSpill is false, fail if a spill is required to make the + /// register available, and return NoRegister. unsigned scavengeRegisterBackwards(const TargetRegisterClass &RC, MachineBasicBlock::iterator To, - bool RestoreAfter, int SPAdj); + bool RestoreAfter, int SPAdj, + bool AllowSpill = true); /// Tell the scavenger a register is used. void setRegUsed(unsigned Reg, LaneBitmask LaneMask = LaneBitmask::getAll()); Index: lib/CodeGen/RegisterScavenging.cpp =================================================================== --- lib/CodeGen/RegisterScavenging.cpp +++ lib/CodeGen/RegisterScavenging.cpp @@ -534,7 +534,7 @@ unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC, MachineBasicBlock::iterator I, - int SPAdj) { + int SPAdj, bool AllowSpill) { MachineInstr &MI = *I; const MachineFunction &MF = *MI.getMF(); // Consider all allocatable registers in the register class initially @@ -565,6 +565,9 @@ return SReg; } + if (!AllowSpill) + return 0; + ScavengedInfo &Scavenged = spill(SReg, *RC, SPAdj, I, UseMI); Scavenged.Restore = &*std::prev(UseMI); @@ -576,7 +579,8 @@ unsigned RegScavenger::scavengeRegisterBackwards(const TargetRegisterClass &RC, MachineBasicBlock::iterator To, - bool RestoreAfter, int SPAdj) { + bool RestoreAfter, int SPAdj, + bool AllowSpill) { const MachineBasicBlock &MBB = *To->getParent(); const MachineFunction &MF = *MBB.getParent(); @@ -590,21 +594,25 @@ MachineBasicBlock::iterator SpillBefore = P.second; assert(Reg != 0 && "No register left to scavenge!"); // Found an available register? - if (SpillBefore != MBB.end()) { - MachineBasicBlock::iterator ReloadAfter = - RestoreAfter ? std::next(MBBI) : MBBI; - MachineBasicBlock::iterator ReloadBefore = std::next(ReloadAfter); - if (ReloadBefore != MBB.end()) - LLVM_DEBUG(dbgs() << "Reload before: " << *ReloadBefore << '\n'); - ScavengedInfo &Scavenged = spill(Reg, RC, SPAdj, SpillBefore, ReloadBefore); - Scavenged.Restore = &*std::prev(SpillBefore); - LiveUnits.removeReg(Reg); - LLVM_DEBUG(dbgs() << "Scavenged register with spill: " << printReg(Reg, TRI) - << " until " << *SpillBefore); - } else { + if (SpillBefore == MBB.end()) { LLVM_DEBUG(dbgs() << "Scavenged free register: " << printReg(Reg, TRI) - << '\n'); + << '\n'); + return Reg; } + + if (!AllowSpill) + return 0; + + MachineBasicBlock::iterator ReloadAfter = + RestoreAfter ? std::next(MBBI) : MBBI; + MachineBasicBlock::iterator ReloadBefore = std::next(ReloadAfter); + if (ReloadBefore != MBB.end()) + LLVM_DEBUG(dbgs() << "Reload before: " << *ReloadBefore << '\n'); + ScavengedInfo &Scavenged = spill(Reg, RC, SPAdj, SpillBefore, ReloadBefore); + Scavenged.Restore = &*std::prev(SpillBefore); + LiveUnits.removeReg(Reg); + LLVM_DEBUG(dbgs() << "Scavenged register with spill: " << printReg(Reg, TRI) + << " until " << *SpillBefore); return Reg; }