1 //===-- RegisterScavenging.cpp - Machine register scavenging --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// This file implements the machine register scavenger. It can provide
12 /// information, such as unused registers, at any point in a machine basic
13 /// block. It also provides a mechanism to make registers available by evicting
14 /// them to spill slots.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "llvm/CodeGen/RegisterScavenging.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetRegisterInfo.h"
29 #include "llvm/Target/TargetSubtargetInfo.h"
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "reg-scavenging"
33 
34 void RegScavenger::setRegUsed(unsigned Reg, LaneBitmask LaneMask) {
35   for (MCRegUnitMaskIterator RUI(Reg, TRI); RUI.isValid(); ++RUI) {
36     LaneBitmask UnitMask = (*RUI).second;
37     if (UnitMask == 0 || (LaneMask & UnitMask) != 0)
38       RegUnitsAvailable.reset((*RUI).first);
39   }
40 }
41 
42 void RegScavenger::init(MachineBasicBlock &MBB) {
43   MachineFunction &MF = *MBB.getParent();
44   TII = MF.getSubtarget().getInstrInfo();
45   TRI = MF.getSubtarget().getRegisterInfo();
46   MRI = &MF.getRegInfo();
47 
48   assert((NumRegUnits == 0 || NumRegUnits == TRI->getNumRegUnits()) &&
49          "Target changed?");
50 
51   // It is not possible to use the register scavenger after late optimization
52   // passes that don't preserve accurate liveness information.
53   assert(MRI->tracksLiveness() &&
54          "Cannot use register scavenger with inaccurate liveness");
55 
56   // Self-initialize.
57   if (!this->MBB) {
58     NumRegUnits = TRI->getNumRegUnits();
59     RegUnitsAvailable.resize(NumRegUnits);
60     KillRegUnits.resize(NumRegUnits);
61     DefRegUnits.resize(NumRegUnits);
62     TmpRegUnits.resize(NumRegUnits);
63   }
64   this->MBB = &MBB;
65 
66   for (SmallVectorImpl<ScavengedInfo>::iterator I = Scavenged.begin(),
67          IE = Scavenged.end(); I != IE; ++I) {
68     I->Reg = 0;
69     I->Restore = nullptr;
70   }
71 
72   // All register units start out unused.
73   RegUnitsAvailable.set();
74 
75   // Pristine CSRs are not available.
76   BitVector PR = MF.getFrameInfo().getPristineRegs(MF);
77   for (int I = PR.find_first(); I>0; I = PR.find_next(I))
78     setRegUsed(I);
79 
80   Tracking = false;
81 }
82 
83 void RegScavenger::setLiveInsUsed(const MachineBasicBlock &MBB) {
84   for (const auto &LI : MBB.liveins())
85     setRegUsed(LI.PhysReg, LI.LaneMask);
86 }
87 
88 void RegScavenger::enterBasicBlock(MachineBasicBlock &MBB) {
89   init(MBB);
90   setLiveInsUsed(MBB);
91 }
92 
93 void RegScavenger::enterBasicBlockEnd(MachineBasicBlock &MBB) {
94   init(MBB);
95   // Merge live-ins of successors to get live-outs.
96   for (const MachineBasicBlock *Succ : MBB.successors())
97     setLiveInsUsed(*Succ);
98 
99   // Move internal iterator at the last instruction of the block.
100   if (MBB.begin() != MBB.end()) {
101     MBBI = std::prev(MBB.end());
102     Tracking = true;
103   }
104 }
105 
106 void RegScavenger::addRegUnits(BitVector &BV, unsigned Reg) {
107   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI)
108     BV.set(*RUI);
109 }
110 
111 void RegScavenger::removeRegUnits(BitVector &BV, unsigned Reg) {
112   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI)
113     BV.reset(*RUI);
114 }
115 
116 void RegScavenger::determineKillsAndDefs() {
117   assert(Tracking && "Must be tracking to determine kills and defs");
118 
119   MachineInstr &MI = *MBBI;
120   assert(!MI.isDebugValue() && "Debug values have no kills or defs");
121 
122   // Find out which registers are early clobbered, killed, defined, and marked
123   // def-dead in this instruction.
124   KillRegUnits.reset();
125   DefRegUnits.reset();
126   for (const MachineOperand &MO : MI.operands()) {
127     if (MO.isRegMask()) {
128       TmpRegUnits.clear();
129       for (unsigned RU = 0, RUEnd = TRI->getNumRegUnits(); RU != RUEnd; ++RU) {
130         for (MCRegUnitRootIterator RURI(RU, TRI); RURI.isValid(); ++RURI) {
131           if (MO.clobbersPhysReg(*RURI)) {
132             TmpRegUnits.set(RU);
133             break;
134           }
135         }
136       }
137 
138       // Apply the mask.
139       KillRegUnits |= TmpRegUnits;
140     }
141     if (!MO.isReg())
142       continue;
143     unsigned Reg = MO.getReg();
144     if (!TargetRegisterInfo::isPhysicalRegister(Reg) || isReserved(Reg))
145       continue;
146 
147     if (MO.isUse()) {
148       // Ignore undef uses.
149       if (MO.isUndef())
150         continue;
151       if (MO.isKill())
152         addRegUnits(KillRegUnits, Reg);
153     } else {
154       assert(MO.isDef());
155       if (MO.isDead())
156         addRegUnits(KillRegUnits, Reg);
157       else
158         addRegUnits(DefRegUnits, Reg);
159     }
160   }
161 }
162 
163 void RegScavenger::unprocess() {
164   assert(Tracking && "Cannot unprocess because we're not tracking");
165 
166   MachineInstr &MI = *MBBI;
167   if (!MI.isDebugValue()) {
168     determineKillsAndDefs();
169 
170     // Commit the changes.
171     setUsed(KillRegUnits);
172     setUnused(DefRegUnits);
173   }
174 
175   if (MBBI == MBB->begin()) {
176     MBBI = MachineBasicBlock::iterator(nullptr);
177     Tracking = false;
178   } else
179     --MBBI;
180 }
181 
182 void RegScavenger::forward() {
183   // Move ptr forward.
184   if (!Tracking) {
185     MBBI = MBB->begin();
186     Tracking = true;
187   } else {
188     assert(MBBI != MBB->end() && "Already past the end of the basic block!");
189     MBBI = std::next(MBBI);
190   }
191   assert(MBBI != MBB->end() && "Already at the end of the basic block!");
192 
193   MachineInstr &MI = *MBBI;
194 
195   for (SmallVectorImpl<ScavengedInfo>::iterator I = Scavenged.begin(),
196          IE = Scavenged.end(); I != IE; ++I) {
197     if (I->Restore != &MI)
198       continue;
199 
200     I->Reg = 0;
201     I->Restore = nullptr;
202   }
203 
204   if (MI.isDebugValue())
205     return;
206 
207   determineKillsAndDefs();
208 
209   // Verify uses and defs.
210 #ifndef NDEBUG
211   for (const MachineOperand &MO : MI.operands()) {
212     if (!MO.isReg())
213       continue;
214     unsigned Reg = MO.getReg();
215     if (!TargetRegisterInfo::isPhysicalRegister(Reg) || isReserved(Reg))
216       continue;
217     if (MO.isUse()) {
218       if (MO.isUndef())
219         continue;
220       if (!isRegUsed(Reg)) {
221         // Check if it's partial live: e.g.
222         // D0 = insert_subreg D0<undef>, S0
223         // ... D0
224         // The problem is the insert_subreg could be eliminated. The use of
225         // D0 is using a partially undef value. This is not *incorrect* since
226         // S1 is can be freely clobbered.
227         // Ideally we would like a way to model this, but leaving the
228         // insert_subreg around causes both correctness and performance issues.
229         bool SubUsed = false;
230         for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
231           if (isRegUsed(*SubRegs)) {
232             SubUsed = true;
233             break;
234           }
235         bool SuperUsed = false;
236         for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
237           if (isRegUsed(*SR)) {
238             SuperUsed = true;
239             break;
240           }
241         }
242         if (!SubUsed && !SuperUsed) {
243           MBB->getParent()->verify(nullptr, "In Register Scavenger");
244           llvm_unreachable("Using an undefined register!");
245         }
246         (void)SubUsed;
247         (void)SuperUsed;
248       }
249     } else {
250       assert(MO.isDef());
251 #if 0
252       // FIXME: Enable this once we've figured out how to correctly transfer
253       // implicit kills during codegen passes like the coalescer.
254       assert((KillRegs.test(Reg) || isUnused(Reg) ||
255               isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
256              "Re-defining a live register!");
257 #endif
258     }
259   }
260 #endif // NDEBUG
261 
262   // Commit the changes.
263   setUnused(KillRegUnits);
264   setUsed(DefRegUnits);
265 }
266 
267 void RegScavenger::backward() {
268   assert(Tracking && "Must be tracking to determine kills and defs");
269 
270   const MachineInstr &MI = *MBBI;
271   // Defined or clobbered registers are available now.
272   for (const MachineOperand &MO : MI.operands()) {
273     if (MO.isRegMask()) {
274       for (unsigned RU = 0, RUEnd = TRI->getNumRegUnits(); RU != RUEnd;
275            ++RU) {
276         for (MCRegUnitRootIterator RURI(RU, TRI); RURI.isValid(); ++RURI) {
277           if (MO.clobbersPhysReg(*RURI)) {
278             RegUnitsAvailable.set(RU);
279             break;
280           }
281         }
282       }
283     } else if (MO.isReg() && MO.isDef()) {
284       unsigned Reg = MO.getReg();
285       if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) ||
286           isReserved(Reg))
287         continue;
288       addRegUnits(RegUnitsAvailable, Reg);
289     }
290   }
291   // Mark read registers as unavailable.
292   for (const MachineOperand &MO : MI.uses()) {
293     if (MO.isReg() && MO.readsReg()) {
294       unsigned Reg = MO.getReg();
295       if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg) ||
296           isReserved(Reg))
297         continue;
298       removeRegUnits(RegUnitsAvailable, Reg);
299     }
300   }
301 
302   // Expire scavenge spill frameindex uses.
303   for (ScavengedInfo &I : Scavenged) {
304     if (I.Restore == &MI) {
305       I.Reg = 0;
306       I.Restore = nullptr;
307     }
308   }
309 
310   if (MBBI == MBB->begin()) {
311     MBBI = MachineBasicBlock::iterator(nullptr);
312     Tracking = false;
313   } else
314     --MBBI;
315 }
316 
317 bool RegScavenger::isRegUsed(unsigned Reg, bool includeReserved) const {
318   if (includeReserved && isReserved(Reg))
319     return true;
320   for (MCRegUnitIterator RUI(Reg, TRI); RUI.isValid(); ++RUI)
321     if (!RegUnitsAvailable.test(*RUI))
322       return true;
323   return false;
324 }
325 
326 unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const {
327   for (unsigned Reg : *RC) {
328     if (!isRegUsed(Reg)) {
329       DEBUG(dbgs() << "Scavenger found unused reg: " << TRI->getName(Reg) <<
330             "\n");
331       return Reg;
332     }
333   }
334   return 0;
335 }
336 
337 BitVector RegScavenger::getRegsAvailable(const TargetRegisterClass *RC) {
338   BitVector Mask(TRI->getNumRegs());
339   for (unsigned Reg : *RC)
340     if (!isRegUsed(Reg))
341       Mask.set(Reg);
342   return Mask;
343 }
344 
345 unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator StartMI,
346                                        BitVector &Candidates,
347                                        unsigned InstrLimit,
348                                        MachineBasicBlock::iterator &UseMI) {
349   int Survivor = Candidates.find_first();
350   assert(Survivor > 0 && "No candidates for scavenging");
351 
352   MachineBasicBlock::iterator ME = MBB->getFirstTerminator();
353   assert(StartMI != ME && "MI already at terminator");
354   MachineBasicBlock::iterator RestorePointMI = StartMI;
355   MachineBasicBlock::iterator MI = StartMI;
356 
357   bool inVirtLiveRange = false;
358   for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
359     if (MI->isDebugValue()) {
360       ++InstrLimit; // Don't count debug instructions
361       continue;
362     }
363     bool isVirtKillInsn = false;
364     bool isVirtDefInsn = false;
365     // Remove any candidates touched by instruction.
366     for (const MachineOperand &MO : MI->operands()) {
367       if (MO.isRegMask())
368         Candidates.clearBitsNotInMask(MO.getRegMask());
369       if (!MO.isReg() || MO.isUndef() || !MO.getReg())
370         continue;
371       if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
372         if (MO.isDef())
373           isVirtDefInsn = true;
374         else if (MO.isKill())
375           isVirtKillInsn = true;
376         continue;
377       }
378       for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid(); ++AI)
379         Candidates.reset(*AI);
380     }
381     // If we're not in a virtual reg's live range, this is a valid
382     // restore point.
383     if (!inVirtLiveRange) RestorePointMI = MI;
384 
385     // Update whether we're in the live range of a virtual register
386     if (isVirtKillInsn) inVirtLiveRange = false;
387     if (isVirtDefInsn) inVirtLiveRange = true;
388 
389     // Was our survivor untouched by this instruction?
390     if (Candidates.test(Survivor))
391       continue;
392 
393     // All candidates gone?
394     if (Candidates.none())
395       break;
396 
397     Survivor = Candidates.find_first();
398   }
399   // If we ran off the end, that's where we want to restore.
400   if (MI == ME) RestorePointMI = ME;
401   assert(RestorePointMI != StartMI &&
402          "No available scavenger restore location!");
403 
404   // We ran out of candidates, so stop the search.
405   UseMI = RestorePointMI;
406   return Survivor;
407 }
408 
409 static std::pair<unsigned, MachineBasicBlock::iterator>
410 findSurvivorBackwards(const TargetRegisterInfo &TRI,
411     MachineBasicBlock::iterator From, MachineBasicBlock::iterator To,
412     BitVector &Available, BitVector &Candidates) {
413   bool FoundTo = false;
414   unsigned Survivor = 0;
415   MachineBasicBlock::iterator Pos;
416   MachineBasicBlock &MBB = *From->getParent();
417   unsigned InstrLimit = 25;
418   unsigned InstrCountDown = InstrLimit;
419   for (MachineBasicBlock::iterator I = From;; --I) {
420     const MachineInstr &MI = *I;
421 
422     // Remove any candidates touched by instruction.
423     bool FoundVReg = false;
424     for (const MachineOperand &MO : MI.operands()) {
425       if (MO.isRegMask()) {
426         Candidates.clearBitsNotInMask(MO.getRegMask());
427         continue;
428       }
429       if (!MO.isReg() || MO.isUndef() || MO.isDebug())
430         continue;
431       unsigned Reg = MO.getReg();
432       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
433         FoundVReg = true;
434       } else if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
435         for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
436           Candidates.reset(*AI);
437       }
438     }
439 
440     if (I == To) {
441       // If one of the available registers survived this long take it.
442       Available &= Candidates;
443       int Reg = Available.find_first();
444       if (Reg != -1)
445         return std::make_pair(Reg, MBB.end());
446       // Otherwise we will continue up to InstrLimit instructions to find
447       // the register which is not defined/used for the longest time.
448       FoundTo = true;
449       Pos = To;
450     }
451     if (FoundTo) {
452       if (Survivor == 0 || !Candidates.test(Survivor)) {
453         int Reg = Candidates.find_first();
454         if (Reg == -1)
455           break;
456         Survivor = Reg;
457       }
458       if (--InstrCountDown == 0 || I == MBB.begin())
459         break;
460       if (FoundVReg) {
461         // Keep searching when we find a vreg since the spilled register will
462         // be usefull for this other vreg as well later.
463         InstrCountDown = InstrLimit;
464         Pos = I;
465       }
466     }
467   }
468 
469   return std::make_pair(Survivor, Pos);
470 }
471 
472 static unsigned getFrameIndexOperandNum(MachineInstr &MI) {
473   unsigned i = 0;
474   while (!MI.getOperand(i).isFI()) {
475     ++i;
476     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
477   }
478   return i;
479 }
480 
481 RegScavenger::ScavengedInfo &
482 RegScavenger::spill(unsigned Reg, const TargetRegisterClass &RC, int SPAdj,
483                     MachineBasicBlock::iterator Before,
484                     MachineBasicBlock::iterator &UseMI) {
485   // Find an available scavenging slot with size and alignment matching
486   // the requirements of the class RC.
487   const MachineFunction &MF = *Before->getParent()->getParent();
488   const MachineFrameInfo &MFI = MF.getFrameInfo();
489   unsigned NeedSize = RC.getSize();
490   unsigned NeedAlign = RC.getAlignment();
491 
492   unsigned SI = Scavenged.size(), Diff = UINT_MAX;
493   int FIB = MFI.getObjectIndexBegin(), FIE = MFI.getObjectIndexEnd();
494   for (unsigned I = 0; I < Scavenged.size(); ++I) {
495     if (Scavenged[I].Reg != 0)
496       continue;
497     // Verify that this slot is valid for this register.
498     int FI = Scavenged[I].FrameIndex;
499     if (FI < FIB || FI >= FIE)
500       continue;
501     unsigned S = MFI.getObjectSize(FI);
502     unsigned A = MFI.getObjectAlignment(FI);
503     if (NeedSize > S || NeedAlign > A)
504       continue;
505     // Avoid wasting slots with large size and/or large alignment. Pick one
506     // that is the best fit for this register class (in street metric).
507     // Picking a larger slot than necessary could happen if a slot for a
508     // larger register is reserved before a slot for a smaller one. When
509     // trying to spill a smaller register, the large slot would be found
510     // first, thus making it impossible to spill the larger register later.
511     unsigned D = (S-NeedSize) + (A-NeedAlign);
512     if (D < Diff) {
513       SI = I;
514       Diff = D;
515     }
516   }
517 
518   if (SI == Scavenged.size()) {
519     // We need to scavenge a register but have no spill slot, the target
520     // must know how to do it (if not, we'll assert below).
521     Scavenged.push_back(ScavengedInfo(FIE));
522   }
523 
524   // Avoid infinite regress
525   Scavenged[SI].Reg = Reg;
526 
527   // If the target knows how to save/restore the register, let it do so;
528   // otherwise, use the emergency stack spill slot.
529   if (!TRI->saveScavengerRegister(*MBB, Before, UseMI, &RC, Reg)) {
530     // Spill the scavenged register before \p Before.
531     int FI = Scavenged[SI].FrameIndex;
532     if (FI < FIB || FI >= FIE) {
533       std::string Msg = std::string("Error while trying to spill ") +
534           TRI->getName(Reg) + " from class " + TRI->getRegClassName(&RC) +
535           ": Cannot scavenge register without an emergency spill slot!";
536       report_fatal_error(Msg.c_str());
537     }
538     TII->storeRegToStackSlot(*MBB, Before, Reg, true, Scavenged[SI].FrameIndex,
539                              &RC, TRI);
540     MachineBasicBlock::iterator II = std::prev(Before);
541 
542     unsigned FIOperandNum = getFrameIndexOperandNum(*II);
543     TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this);
544 
545     // Restore the scavenged register before its use (or first terminator).
546     TII->loadRegFromStackSlot(*MBB, UseMI, Reg, Scavenged[SI].FrameIndex,
547                               &RC, TRI);
548     II = std::prev(UseMI);
549 
550     FIOperandNum = getFrameIndexOperandNum(*II);
551     TRI->eliminateFrameIndex(II, SPAdj, FIOperandNum, this);
552   }
553   return Scavenged[SI];
554 }
555 
556 unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
557                                         MachineBasicBlock::iterator I,
558                                         int SPAdj) {
559   MachineInstr &MI = *I;
560   const MachineFunction &MF = *MI.getParent()->getParent();
561   // Consider all allocatable registers in the register class initially
562   BitVector Candidates = TRI->getAllocatableSet(MF, RC);
563 
564   // Exclude all the registers being used by the instruction.
565   for (const MachineOperand &MO : MI.operands()) {
566     if (MO.isReg() && MO.getReg() != 0 && !(MO.isUse() && MO.isUndef()) &&
567         !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
568       Candidates.reset(MO.getReg());
569   }
570 
571   // Try to find a register that's unused if there is one, as then we won't
572   // have to spill.
573   BitVector Available = getRegsAvailable(RC);
574   Available &= Candidates;
575   if (Available.any())
576     Candidates = Available;
577 
578   // Find the register whose use is furthest away.
579   MachineBasicBlock::iterator UseMI;
580   unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
581 
582   // If we found an unused register there is no reason to spill it.
583   if (!isRegUsed(SReg)) {
584     DEBUG(dbgs() << "Scavenged register: " << TRI->getName(SReg) << "\n");
585     return SReg;
586   }
587 
588   ScavengedInfo &Scavenged = spill(SReg, *RC, SPAdj, I, UseMI);
589   Scavenged.Restore = &*std::prev(UseMI);
590 
591   DEBUG(dbgs() << "Scavenged register (with spill): " << TRI->getName(SReg) <<
592         "\n");
593 
594   return SReg;
595 }
596 
597 unsigned RegScavenger::scavengeRegisterBackwards(const TargetRegisterClass &RC,
598                                                  MachineBasicBlock::iterator To,
599                                                  bool RestoreAfter, int SPAdj) {
600   const MachineBasicBlock &MBB = *To->getParent();
601   const MachineFunction &MF = *MBB.getParent();
602   // Consider all allocatable registers in the register class initially
603   BitVector Candidates = TRI->getAllocatableSet(MF, &RC);
604 
605   // Try to find a register that's unused if there is one, as then we won't
606   // have to spill.
607   BitVector Available = getRegsAvailable(&RC);
608 
609   // Find the register whose use is furthest away.
610   MachineBasicBlock::iterator UseMI;
611   std::pair<unsigned, MachineBasicBlock::iterator> P =
612       findSurvivorBackwards(*TRI, MBBI, To, Available, Candidates);
613   unsigned Reg = P.first;
614   assert(Reg != 0 && "No register left to scavenge!");
615   // Found an available register?
616   if (!Available.test(Reg)) {
617     MachineBasicBlock::iterator ReloadAfter =
618       RestoreAfter ? std::next(MBBI) : MBBI;
619     MachineBasicBlock::iterator ReloadBefore = std::next(ReloadAfter);
620     DEBUG(dbgs() << "Reload before: " << *ReloadBefore << '\n');
621     MachineBasicBlock::iterator SpillBefore = P.second;
622     ScavengedInfo &Scavenged = spill(Reg, RC, SPAdj, SpillBefore, ReloadBefore);
623     Scavenged.Restore = &*std::prev(SpillBefore);
624     addRegUnits(RegUnitsAvailable, Reg);
625     DEBUG(dbgs() << "Scavenged register with spill: " << PrintReg(Reg, TRI)
626           << " until " << *SpillBefore);
627   } else {
628     DEBUG(dbgs() << "Scavenged free register: " << PrintReg(Reg, TRI) << '\n');
629   }
630   return Reg;
631 }
632