1 //===-- RegisterScavenging.cpp - Machine register scavenging --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the machine register scavenger. It can provide
11 // information, such as unused registers, at any point in a machine basic block.
12 // It also provides a mechanism to make registers available by evicting them to
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/RegisterScavenging.h"
18 #include "llvm/CodeGen/MachineBasicBlock.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
28 #include "llvm/Target/TargetSubtargetInfo.h"
31 #define DEBUG_TYPE "reg-scavenging"
33 /// setUsed - Set the register units of this register as used.
34 void RegScavenger::setRegUsed(unsigned Reg) {
35 for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI)
36 RegUnitsAvailable.reset(*RUI);
39 void RegScavenger::initRegState() {
40 for (SmallVectorImpl<ScavengedInfo>::iterator I = Scavenged.begin(),
41 IE = Scavenged.end(); I != IE; ++I) {
46 // All register units start out unused.
47 RegUnitsAvailable.set();
52 // Live-in registers are in use.
53 for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(),
54 E = MBB->livein_end(); I != E; ++I)
57 // Pristine CSRs are also unavailable.
58 BitVector PR = MBB->getParent()->getFrameInfo()->getPristineRegs(MBB);
59 for (int I = PR.find_first(); I>0; I = PR.find_next(I))
63 void RegScavenger::enterBasicBlock(MachineBasicBlock *mbb) {
64 MachineFunction &MF = *mbb->getParent();
65 TII = MF.getSubtarget().getInstrInfo();
66 TRI = MF.getSubtarget().getRegisterInfo();
67 MRI = &MF.getRegInfo();
69 assert((NumRegUnits == 0 || NumRegUnits == TRI->getNumRegUnits()) &&
72 // It is not possible to use the register scavenger after late optimization
73 // passes that don't preserve accurate liveness information.
74 assert(MRI->tracksLiveness() &&
75 "Cannot use register scavenger with inaccurate liveness");
79 NumRegUnits = TRI->getNumRegUnits();
80 RegUnitsAvailable.resize(NumRegUnits);
81 KillRegUnits.resize(NumRegUnits);
82 DefRegUnits.resize(NumRegUnits);
83 TmpRegUnits.resize(NumRegUnits);
92 void RegScavenger::addRegUnits(BitVector &BV, unsigned Reg) {
93 for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI)
97 void RegScavenger::determineKillsAndDefs() {
98 assert(Tracking && "Must be tracking to determine kills and defs");
100 MachineInstr *MI = MBBI;
101 assert(!MI->isDebugValue() && "Debug values have no kills or defs");
103 // Find out which registers are early clobbered, killed, defined, and marked
104 // def-dead in this instruction.
105 // FIXME: The scavenger is not predication aware. If the instruction is
106 // predicated, conservatively assume "kill" markers do not actually kill the
107 // register. Similarly ignores "dead" markers.
108 bool isPred = TII->isPredicated(MI);
109 KillRegUnits.reset();
111 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
112 const MachineOperand &MO = MI->getOperand(i);
113 if (MO.isRegMask()) {
116 for (unsigned RU = 0, RUEnd = TRI->getNumRegUnits(); RU != RUEnd; ++RU) {
117 for (MCRegUnitRootIterator RURI(RU, TRI); RURI.isValid(); ++RURI) {
118 if (MO.clobbersPhysReg(*RURI)) {
126 (isPred ? DefRegUnits : KillRegUnits) |= TmpRegUnits;
130 unsigned Reg = MO.getReg();
131 if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) || isReserved(Reg))
135 // Ignore undef uses.
138 if (!isPred && MO.isKill())
139 addRegUnits(KillRegUnits, Reg);
142 if (!isPred && MO.isDead())
143 addRegUnits(KillRegUnits, Reg);
145 addRegUnits(DefRegUnits, Reg);
150 void RegScavenger::unprocess() {
151 assert(Tracking && "Cannot unprocess because we're not tracking");
153 MachineInstr *MI = MBBI;
154 if (!MI->isDebugValue()) {
155 determineKillsAndDefs();
157 // Commit the changes.
158 setUsed(KillRegUnits);
159 setUnused(DefRegUnits);
162 if (MBBI == MBB->begin()) {
163 MBBI = MachineBasicBlock::iterator(nullptr);
169 void RegScavenger::forward() {
175 assert(MBBI != MBB->end() && "Already past the end of the basic block!");
176 MBBI = std::next(MBBI);
178 assert(MBBI != MBB->end() && "Already at the end of the basic block!");
180 MachineInstr *MI = MBBI;
182 for (SmallVectorImpl<ScavengedInfo>::iterator I = Scavenged.begin(),
183 IE = Scavenged.end(); I != IE; ++I) {
184 if (I->Restore != MI)
188 I->Restore = nullptr;
191 if (MI->isDebugValue())
194 determineKillsAndDefs();
196 // Verify uses and defs.
198 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
199 const MachineOperand &MO = MI->getOperand(i);
202 unsigned Reg = MO.getReg();
203 if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) || isReserved(Reg))
208 if (!isRegUsed(Reg)) {
209 // Check if it's partial live: e.g.
210 // D0 = insert_subreg D0<undef>, S0
212 // The problem is the insert_subreg could be eliminated. The use of
213 // D0 is using a partially undef value. This is not *incorrect* since
214 // S1 is can be freely clobbered.
215 // Ideally we would like a way to model this, but leaving the
216 // insert_subreg around causes both correctness and performance issues.
217 bool SubUsed = false;
218 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
219 if (isRegUsed(*SubRegs)) {
223 bool SuperUsed = false;
224 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
225 if (isRegUsed(*SR)) {
230 if (!SubUsed && !SuperUsed) {
231 MBB->getParent()->verify(nullptr, "In Register Scavenger");
232 llvm_unreachable("Using an undefined register!");
240 // FIXME: Enable this once we've figured out how to correctly transfer
241 // implicit kills during codegen passes like the coalescer.
242 assert((KillRegs.test(Reg) || isUnused(Reg) ||
243 isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
244 "Re-defining a live register!");
250 // Commit the changes.
251 setUnused(KillRegUnits);
252 setUsed(DefRegUnits);
255 bool RegScavenger::isRegUsed(unsigned Reg, bool includeReserved) const {
256 if (includeReserved && isReserved(Reg))
258 for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI)
259 if (!RegUnitsAvailable.test(*RUI))
264 unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const {
265 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
267 if (!isRegUsed(*I)) {
268 DEBUG(dbgs() << "Scavenger found unused reg: " << TRI->getName(*I) <<
275 /// getRegsAvailable - Return all available registers in the register class
277 BitVector RegScavenger::getRegsAvailable(const TargetRegisterClass *RC) {
278 BitVector Mask(TRI->getNumRegs());
279 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
286 /// findSurvivorReg - Return the candidate register that is unused for the
287 /// longest after StartMII. UseMI is set to the instruction where the search
290 /// No more than InstrLimit instructions are inspected.
292 unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator StartMI,
293 BitVector &Candidates,
295 MachineBasicBlock::iterator &UseMI) {
296 int Survivor = Candidates.find_first();
297 assert(Survivor > 0 && "No candidates for scavenging");
299 MachineBasicBlock::iterator ME = MBB->getFirstTerminator();
300 assert(StartMI != ME && "MI already at terminator");
301 MachineBasicBlock::iterator RestorePointMI = StartMI;
302 MachineBasicBlock::iterator MI = StartMI;
304 bool inVirtLiveRange = false;
305 for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
306 if (MI->isDebugValue()) {
307 ++InstrLimit; // Don't count debug instructions
310 bool isVirtKillInsn = false;
311 bool isVirtDefInsn = false;
312 // Remove any candidates touched by instruction.
313 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
314 const MachineOperand &MO = MI->getOperand(i);
316 Candidates.clearBitsNotInMask(MO.getRegMask());
317 if (!MO.isReg() || MO.isUndef() || !MO.getReg())
319 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
321 isVirtDefInsn = true;
322 else if (MO.isKill())
323 isVirtKillInsn = true;
326 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
327 Candidates.reset(*AI);
329 // If we're not in a virtual reg's live range, this is a valid
331 if (!inVirtLiveRange) RestorePointMI = MI;
333 // Update whether we're in the live range of a virtual register
334 if (isVirtKillInsn) inVirtLiveRange = false;
335 if (isVirtDefInsn) inVirtLiveRange = true;
337 // Was our survivor untouched by this instruction?
338 if (Candidates.test(Survivor))
341 // All candidates gone?
342 if (Candidates.none())
345 Survivor = Candidates.find_first();
347 // If we ran off the end, that's where we want to restore.
348 if (MI == ME) RestorePointMI = ME;
349 assert (RestorePointMI != StartMI &&
350 "No available scavenger restore location!");
352 // We ran out of candidates, so stop the search.
353 UseMI = RestorePointMI;
357 static unsigned getFrameIndexOperandNum(MachineInstr *MI) {
359 while (!MI->getOperand(i).isFI()) {
361 assert(i < MI->getNumOperands() &&
362 "Instr doesn't have FrameIndex operand!");
367 unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
368 MachineBasicBlock::iterator I,
370 // Consider all allocatable registers in the register class initially
371 BitVector Candidates =
372 TRI->getAllocatableSet(*I->getParent()->getParent(), RC);
374 // Exclude all the registers being used by the instruction.
375 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
376 MachineOperand &MO = I->getOperand(i);
377 if (MO.isReg() && MO.getReg() != 0 && !(MO.isUse() && MO.isUndef()) &&
378 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
379 Candidates.reset(MO.getReg());
382 // Try to find a register that's unused if there is one, as then we won't
384 BitVector Available = getRegsAvailable(RC);
385 Available &= Candidates;
387 Candidates = Available;
389 // Find the register whose use is furthest away.
390 MachineBasicBlock::iterator UseMI;
391 unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
393 // If we found an unused register there is no reason to spill it.
394 if (!isRegUsed(SReg)) {
395 DEBUG(dbgs() << "Scavenged register: " << TRI->getName(SReg) << "\n");
399 // Find an available scavenging slot.
401 for (SI = 0; SI < Scavenged.size(); ++SI)
402 if (Scavenged[SI].Reg == 0)
405 if (SI == Scavenged.size()) {
406 // We need to scavenge a register but have no spill slot, the target
407 // must know how to do it (if not, we'll assert below).
408 Scavenged.push_back(ScavengedInfo());
411 // Avoid infinite regress
412 Scavenged[SI].Reg = SReg;
414 // If the target knows how to save/restore the register, let it do so;
415 // otherwise, use the emergency stack spill slot.
416 if (!TRI->saveScavengerRegister(*MBB, I, UseMI, RC, SReg)) {
417 // Spill the scavenged register before I.
418 assert(Scavenged[SI].FrameIndex >= 0 &&
419 "Cannot scavenge register without an emergency spill slot!");
420 TII->storeRegToStackSlot(*MBB, I, SReg, true, Scavenged[SI].FrameIndex,
422 MachineBasicBlock::iterator II = std::prev(I);
424 unsigned FIOperandNum = getFrameIndexOperandNum(II);
425 TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this);
427 // Restore the scavenged register before its use (or first terminator).
428 TII->loadRegFromStackSlot(*MBB, UseMI, SReg, Scavenged[SI].FrameIndex,
430 II = std::prev(UseMI);
432 FIOperandNum = getFrameIndexOperandNum(II);
433 TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this);
436 Scavenged[SI].Restore = std::prev(UseMI);
438 // Doing this here leads to infinite regress.
439 // Scavenged[SI].Reg = SReg;
441 DEBUG(dbgs() << "Scavenged register (with spill): " << TRI->getName(SReg) <<