1 //===- RegisterScavenging.cpp - Machine register scavenging ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// This file implements the machine register scavenger. It can provide
11 /// information, such as unused registers, at any point in a machine basic
12 /// block. It also provides a mechanism to make registers available by evicting
13 /// them to spill slots.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/RegisterScavenging.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/BitVector.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/LiveRegUnits.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/TargetFrameLowering.h"
31 #include "llvm/CodeGen/TargetInstrInfo.h"
32 #include "llvm/CodeGen/TargetRegisterInfo.h"
33 #include "llvm/CodeGen/TargetSubtargetInfo.h"
34 #include "llvm/InitializePasses.h"
35 #include "llvm/MC/MCRegisterInfo.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/raw_ostream.h"
47 #define DEBUG_TYPE "reg-scavenging"
49 STATISTIC(NumScavengedRegs
, "Number of frame index regs scavenged");
51 void RegScavenger::setRegUsed(Register Reg
, LaneBitmask LaneMask
) {
52 LiveUnits
.addRegMasked(Reg
, LaneMask
);
55 void RegScavenger::init(MachineBasicBlock
&MBB
) {
56 MachineFunction
&MF
= *MBB
.getParent();
57 TII
= MF
.getSubtarget().getInstrInfo();
58 TRI
= MF
.getSubtarget().getRegisterInfo();
59 MRI
= &MF
.getRegInfo();
64 for (ScavengedInfo
&SI
: Scavenged
) {
70 void RegScavenger::enterBasicBlock(MachineBasicBlock
&MBB
) {
72 LiveUnits
.addLiveIns(MBB
);
76 void RegScavenger::enterBasicBlockEnd(MachineBasicBlock
&MBB
) {
78 LiveUnits
.addLiveOuts(MBB
);
82 void RegScavenger::backward() {
83 const MachineInstr
&MI
= *--MBBI
;
84 LiveUnits
.stepBackward(MI
);
86 // Expire scavenge spill frameindex uses.
87 for (ScavengedInfo
&I
: Scavenged
) {
88 if (I
.Restore
== &MI
) {
95 bool RegScavenger::isRegUsed(Register Reg
, bool includeReserved
) const {
97 return includeReserved
;
98 return !LiveUnits
.available(Reg
);
101 Register
RegScavenger::FindUnusedReg(const TargetRegisterClass
*RC
) const {
102 for (Register Reg
: *RC
) {
103 if (!isRegUsed(Reg
)) {
104 LLVM_DEBUG(dbgs() << "Scavenger found unused reg: " << printReg(Reg
, TRI
)
112 BitVector
RegScavenger::getRegsAvailable(const TargetRegisterClass
*RC
) {
113 BitVector
Mask(TRI
->getNumRegs());
114 for (Register Reg
: *RC
)
120 /// Given the bitvector \p Available of free register units at position
121 /// \p From. Search backwards to find a register that is part of \p
122 /// Candidates and not used/clobbered until the point \p To. If there is
123 /// multiple candidates continue searching and pick the one that is not used/
124 /// clobbered for the longest time.
125 /// Returns the register and the earliest position we know it to be free or
126 /// the position MBB.end() if no register is available.
127 static std::pair
<MCPhysReg
, MachineBasicBlock::iterator
>
128 findSurvivorBackwards(const MachineRegisterInfo
&MRI
,
129 MachineBasicBlock::iterator From
, MachineBasicBlock::iterator To
,
130 const LiveRegUnits
&LiveOut
, ArrayRef
<MCPhysReg
> AllocationOrder
,
132 bool FoundTo
= false;
133 MCPhysReg Survivor
= 0;
134 MachineBasicBlock::iterator Pos
;
135 MachineBasicBlock
&MBB
= *From
->getParent();
136 unsigned InstrLimit
= 25;
137 unsigned InstrCountDown
= InstrLimit
;
138 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
139 LiveRegUnits
Used(TRI
);
141 assert(From
->getParent() == To
->getParent() &&
142 "Target instruction is in other than current basic block, use "
143 "enterBasicBlockEnd first");
145 for (MachineBasicBlock::iterator I
= From
;; --I
) {
146 const MachineInstr
&MI
= *I
;
151 // See if one of the registers in RC wasn't used so far.
152 for (MCPhysReg Reg
: AllocationOrder
) {
153 if (!MRI
.isReserved(Reg
) && Used
.available(Reg
) &&
154 LiveOut
.available(Reg
))
155 return std::make_pair(Reg
, MBB
.end());
157 // Otherwise we will continue up to InstrLimit instructions to find
158 // the register which is not defined/used for the longest time.
161 // Note: It was fine so far to start our search at From, however now that
162 // we have to spill, and can only place the restore after From then
163 // add the regs used/defed by std::next(From) to the set.
165 Used
.accumulate(*std::next(From
));
168 // Don't search to FrameSetup instructions if we were searching from
169 // Non-FrameSetup instructions. Otherwise, the spill position may point
170 // before FrameSetup instructions.
171 if (!From
->getFlag(MachineInstr::FrameSetup
) &&
172 MI
.getFlag(MachineInstr::FrameSetup
))
175 if (Survivor
== 0 || !Used
.available(Survivor
)) {
176 MCPhysReg AvilableReg
= 0;
177 for (MCPhysReg Reg
: AllocationOrder
) {
178 if (!MRI
.isReserved(Reg
) && Used
.available(Reg
)) {
183 if (AvilableReg
== 0)
185 Survivor
= AvilableReg
;
187 if (--InstrCountDown
== 0)
190 // Keep searching when we find a vreg since the spilled register will
191 // be usefull for this other vreg as well later.
192 bool FoundVReg
= false;
193 for (const MachineOperand
&MO
: MI
.operands()) {
194 if (MO
.isReg() && MO
.getReg().isVirtual()) {
200 InstrCountDown
= InstrLimit
;
203 if (I
== MBB
.begin())
206 assert(I
!= MBB
.begin() && "Did not find target instruction while "
207 "iterating backwards");
210 return std::make_pair(Survivor
, Pos
);
213 static unsigned getFrameIndexOperandNum(MachineInstr
&MI
) {
215 while (!MI
.getOperand(i
).isFI()) {
217 assert(i
< MI
.getNumOperands() && "Instr doesn't have FrameIndex operand!");
222 RegScavenger::ScavengedInfo
&
223 RegScavenger::spill(Register Reg
, const TargetRegisterClass
&RC
, int SPAdj
,
224 MachineBasicBlock::iterator Before
,
225 MachineBasicBlock::iterator
&UseMI
) {
226 // Find an available scavenging slot with size and alignment matching
227 // the requirements of the class RC.
228 const MachineFunction
&MF
= *Before
->getMF();
229 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
230 unsigned NeedSize
= TRI
->getSpillSize(RC
);
231 Align NeedAlign
= TRI
->getSpillAlign(RC
);
233 unsigned SI
= Scavenged
.size(), Diff
= std::numeric_limits
<unsigned>::max();
234 int FIB
= MFI
.getObjectIndexBegin(), FIE
= MFI
.getObjectIndexEnd();
235 for (unsigned I
= 0; I
< Scavenged
.size(); ++I
) {
236 if (Scavenged
[I
].Reg
!= 0)
238 // Verify that this slot is valid for this register.
239 int FI
= Scavenged
[I
].FrameIndex
;
240 if (FI
< FIB
|| FI
>= FIE
)
242 unsigned S
= MFI
.getObjectSize(FI
);
243 Align A
= MFI
.getObjectAlign(FI
);
244 if (NeedSize
> S
|| NeedAlign
> A
)
246 // Avoid wasting slots with large size and/or large alignment. Pick one
247 // that is the best fit for this register class (in street metric).
248 // Picking a larger slot than necessary could happen if a slot for a
249 // larger register is reserved before a slot for a smaller one. When
250 // trying to spill a smaller register, the large slot would be found
251 // first, thus making it impossible to spill the larger register later.
252 unsigned D
= (S
- NeedSize
) + (A
.value() - NeedAlign
.value());
259 if (SI
== Scavenged
.size()) {
260 // We need to scavenge a register but have no spill slot, the target
261 // must know how to do it (if not, we'll assert below).
262 Scavenged
.push_back(ScavengedInfo(FIE
));
265 // Avoid infinite regress
266 Scavenged
[SI
].Reg
= Reg
;
268 // If the target knows how to save/restore the register, let it do so;
269 // otherwise, use the emergency stack spill slot.
270 if (!TRI
->saveScavengerRegister(*MBB
, Before
, UseMI
, &RC
, Reg
)) {
271 // Spill the scavenged register before \p Before.
272 int FI
= Scavenged
[SI
].FrameIndex
;
273 if (FI
< FIB
|| FI
>= FIE
) {
274 report_fatal_error(Twine("Error while trying to spill ") +
275 TRI
->getName(Reg
) + " from class " +
276 TRI
->getRegClassName(&RC
) +
277 ": Cannot scavenge register without an emergency "
280 TII
->storeRegToStackSlot(*MBB
, Before
, Reg
, true, FI
, &RC
, TRI
, Register());
281 MachineBasicBlock::iterator II
= std::prev(Before
);
283 unsigned FIOperandNum
= getFrameIndexOperandNum(*II
);
284 TRI
->eliminateFrameIndex(II
, SPAdj
, FIOperandNum
, this);
286 // Restore the scavenged register before its use (or first terminator).
287 TII
->loadRegFromStackSlot(*MBB
, UseMI
, Reg
, FI
, &RC
, TRI
, Register());
288 II
= std::prev(UseMI
);
290 FIOperandNum
= getFrameIndexOperandNum(*II
);
291 TRI
->eliminateFrameIndex(II
, SPAdj
, FIOperandNum
, this);
293 return Scavenged
[SI
];
296 Register
RegScavenger::scavengeRegisterBackwards(const TargetRegisterClass
&RC
,
297 MachineBasicBlock::iterator To
,
298 bool RestoreAfter
, int SPAdj
,
300 const MachineBasicBlock
&MBB
= *To
->getParent();
301 const MachineFunction
&MF
= *MBB
.getParent();
303 // Find the register whose use is furthest away.
304 MachineBasicBlock::iterator UseMI
;
305 ArrayRef
<MCPhysReg
> AllocationOrder
= RC
.getRawAllocationOrder(MF
);
306 std::pair
<MCPhysReg
, MachineBasicBlock::iterator
> P
= findSurvivorBackwards(
307 *MRI
, std::prev(MBBI
), To
, LiveUnits
, AllocationOrder
, RestoreAfter
);
308 MCPhysReg Reg
= P
.first
;
309 MachineBasicBlock::iterator SpillBefore
= P
.second
;
310 // Found an available register?
311 if (Reg
!= 0 && SpillBefore
== MBB
.end()) {
312 LLVM_DEBUG(dbgs() << "Scavenged free register: " << printReg(Reg
, TRI
)
320 assert(Reg
!= 0 && "No register left to scavenge!");
322 MachineBasicBlock::iterator ReloadBefore
=
323 RestoreAfter
? std::next(MBBI
) : MBBI
;
324 if (ReloadBefore
!= MBB
.end())
325 LLVM_DEBUG(dbgs() << "Reload before: " << *ReloadBefore
<< '\n');
326 ScavengedInfo
&Scavenged
= spill(Reg
, RC
, SPAdj
, SpillBefore
, ReloadBefore
);
327 Scavenged
.Restore
= &*std::prev(SpillBefore
);
328 LiveUnits
.removeReg(Reg
);
329 LLVM_DEBUG(dbgs() << "Scavenged register with spill: " << printReg(Reg
, TRI
)
330 << " until " << *SpillBefore
);
334 /// Allocate a register for the virtual register \p VReg. The last use of
335 /// \p VReg is around the current position of the register scavenger \p RS.
336 /// \p ReserveAfter controls whether the scavenged register needs to be reserved
337 /// after the current instruction, otherwise it will only be reserved before the
338 /// current instruction.
339 static Register
scavengeVReg(MachineRegisterInfo
&MRI
, RegScavenger
&RS
,
340 Register VReg
, bool ReserveAfter
) {
341 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
343 // Verify that all definitions and uses are in the same basic block.
344 const MachineBasicBlock
*CommonMBB
= nullptr;
345 // Real definition for the reg, re-definitions are not considered.
346 const MachineInstr
*RealDef
= nullptr;
347 for (MachineOperand
&MO
: MRI
.reg_nodbg_operands(VReg
)) {
348 MachineBasicBlock
*MBB
= MO
.getParent()->getParent();
349 if (CommonMBB
== nullptr)
351 assert(MBB
== CommonMBB
&& "All defs+uses must be in the same basic block");
353 const MachineInstr
&MI
= *MO
.getParent();
354 if (!MI
.readsRegister(VReg
, &TRI
)) {
355 assert((!RealDef
|| RealDef
== &MI
) &&
356 "Can have at most one definition which is not a redefinition");
361 assert(RealDef
!= nullptr && "Must have at least 1 Def");
364 // We should only have one definition of the register. However to accommodate
365 // the requirements of two address code we also allow definitions in
366 // subsequent instructions provided they also read the register. That way
367 // we get a single contiguous lifetime.
369 // Definitions in MRI.def_begin() are unordered, search for the first.
370 MachineRegisterInfo::def_iterator FirstDef
= llvm::find_if(
371 MRI
.def_operands(VReg
), [VReg
, &TRI
](const MachineOperand
&MO
) {
372 return !MO
.getParent()->readsRegister(VReg
, &TRI
);
374 assert(FirstDef
!= MRI
.def_end() &&
375 "Must have one definition that does not redefine vreg");
376 MachineInstr
&DefMI
= *FirstDef
->getParent();
378 // The register scavenger will report a free register inserting an emergency
379 // spill/reload if necessary.
381 const TargetRegisterClass
&RC
= *MRI
.getRegClass(VReg
);
382 Register SReg
= RS
.scavengeRegisterBackwards(RC
, DefMI
.getIterator(),
383 ReserveAfter
, SPAdj
);
384 MRI
.replaceRegWith(VReg
, SReg
);
389 /// Allocate (scavenge) vregs inside a single basic block.
390 /// Returns true if the target spill callback created new vregs and a 2nd pass
392 static bool scavengeFrameVirtualRegsInBlock(MachineRegisterInfo
&MRI
,
394 MachineBasicBlock
&MBB
) {
395 const TargetRegisterInfo
&TRI
= *MRI
.getTargetRegisterInfo();
396 RS
.enterBasicBlockEnd(MBB
);
398 unsigned InitialNumVirtRegs
= MRI
.getNumVirtRegs();
399 bool NextInstructionReadsVReg
= false;
400 for (MachineBasicBlock::iterator I
= MBB
.end(); I
!= MBB
.begin(); ) {
401 // Move RegScavenger to the position between *std::prev(I) and *I.
405 // Look for unassigned vregs in the uses of *std::next(I).
406 if (NextInstructionReadsVReg
) {
407 MachineBasicBlock::iterator N
= std::next(I
);
408 const MachineInstr
&NMI
= *N
;
409 for (const MachineOperand
&MO
: NMI
.operands()) {
412 Register Reg
= MO
.getReg();
413 // We only care about virtual registers and ignore virtual registers
414 // created by the target callbacks in the process (those will be handled
415 // in a scavenging round).
416 if (!Reg
.isVirtual() ||
417 Register::virtReg2Index(Reg
) >= InitialNumVirtRegs
)
422 Register SReg
= scavengeVReg(MRI
, RS
, Reg
, true);
423 N
->addRegisterKilled(SReg
, &TRI
, false);
428 // Look for unassigned vregs in the defs of *I.
429 NextInstructionReadsVReg
= false;
430 const MachineInstr
&MI
= *I
;
431 for (const MachineOperand
&MO
: MI
.operands()) {
434 Register Reg
= MO
.getReg();
435 // Only vregs, no newly created vregs (see above).
436 if (!Reg
.isVirtual() ||
437 Register::virtReg2Index(Reg
) >= InitialNumVirtRegs
)
439 // We have to look at all operands anyway so we can precalculate here
440 // whether there is a reading operand. This allows use to skip the use
441 // step in the next iteration if there was none.
442 assert(!MO
.isInternalRead() && "Cannot assign inside bundles");
443 assert((!MO
.isUndef() || MO
.isDef()) && "Cannot handle undef uses");
445 NextInstructionReadsVReg
= true;
448 Register SReg
= scavengeVReg(MRI
, RS
, Reg
, false);
449 I
->addRegisterDead(SReg
, &TRI
, false);
454 for (const MachineOperand
&MO
: MBB
.front().operands()) {
455 if (!MO
.isReg() || !MO
.getReg().isVirtual())
457 assert(!MO
.isInternalRead() && "Cannot assign inside bundles");
458 assert((!MO
.isUndef() || MO
.isDef()) && "Cannot handle undef uses");
459 assert(!MO
.readsReg() && "Vreg use in first instruction not allowed");
463 return MRI
.getNumVirtRegs() != InitialNumVirtRegs
;
466 void llvm::scavengeFrameVirtualRegs(MachineFunction
&MF
, RegScavenger
&RS
) {
467 // FIXME: Iterating over the instruction stream is unnecessary. We can simply
468 // iterate over the vreg use list, which at this point only contains machine
469 // operands for which eliminateFrameIndex need a new scratch reg.
470 MachineRegisterInfo
&MRI
= MF
.getRegInfo();
472 if (MRI
.getNumVirtRegs() == 0) {
473 MF
.getProperties().set(MachineFunctionProperties::Property::NoVRegs
);
477 // Run through the instructions and find any virtual registers.
478 for (MachineBasicBlock
&MBB
: MF
) {
482 bool Again
= scavengeFrameVirtualRegsInBlock(MRI
, RS
, MBB
);
484 LLVM_DEBUG(dbgs() << "Warning: Required two scavenging passes for block "
485 << MBB
.getName() << '\n');
486 Again
= scavengeFrameVirtualRegsInBlock(MRI
, RS
, MBB
);
487 // The target required a 2nd run (because it created new vregs while
488 // spilling). Refuse to do another pass to keep compiletime in check.
490 report_fatal_error("Incomplete scavenging after 2nd pass");
495 MF
.getProperties().set(MachineFunctionProperties::Property::NoVRegs
);
500 /// This class runs register scavenging independ of the PrologEpilogInserter.
501 /// This is used in for testing.
502 class ScavengerTest
: public MachineFunctionPass
{
506 ScavengerTest() : MachineFunctionPass(ID
) {}
508 bool runOnMachineFunction(MachineFunction
&MF
) override
{
509 const TargetSubtargetInfo
&STI
= MF
.getSubtarget();
510 const TargetFrameLowering
&TFL
= *STI
.getFrameLowering();
513 // Let's hope that calling those outside of PrologEpilogueInserter works
514 // well enough to initialize the scavenger with some emergency spillslots
517 TFL
.determineCalleeSaves(MF
, SavedRegs
, &RS
);
518 TFL
.processFunctionBeforeFrameFinalized(MF
, &RS
);
520 // Let's scavenge the current function
521 scavengeFrameVirtualRegs(MF
, RS
);
526 } // end anonymous namespace
528 char ScavengerTest::ID
;
530 INITIALIZE_PASS(ScavengerTest
, "scavenger-test",
531 "Scavenge virtual registers inside basic blocks", false, false)