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 #define DEBUG_TYPE "reg-scavenging"
18 #include "llvm/CodeGen/RegisterScavenging.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Target/TargetRegisterInfo.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/STLExtras.h"
34 /// setUsed - Set the register and its sub-registers as being used.
35 void RegScavenger::setUsed(unsigned Reg) {
36 RegsAvailable.reset(Reg);
38 for (const unsigned *SubRegs = TRI->getSubRegisters(Reg);
39 unsigned SubReg = *SubRegs; ++SubRegs)
40 RegsAvailable.reset(SubReg);
43 bool RegScavenger::isAliasUsed(unsigned Reg) const {
46 for (const unsigned *R = TRI->getAliasSet(Reg); *R; ++R)
52 void RegScavenger::initRegState() {
55 ScavengeRestore = NULL;
57 // All registers started out unused.
60 // Reserved registers are always used.
61 RegsAvailable ^= ReservedRegs;
66 // Live-in registers are in use.
67 for (MachineBasicBlock::const_livein_iterator I = MBB->livein_begin(),
68 E = MBB->livein_end(); I != E; ++I)
71 // Pristine CSRs are also unavailable.
72 BitVector PR = MBB->getParent()->getFrameInfo()->getPristineRegs(MBB);
73 for (int I = PR.find_first(); I>0; I = PR.find_next(I))
77 void RegScavenger::enterBasicBlock(MachineBasicBlock *mbb) {
78 MachineFunction &MF = *mbb->getParent();
79 const TargetMachine &TM = MF.getTarget();
80 TII = TM.getInstrInfo();
81 TRI = TM.getRegisterInfo();
82 MRI = &MF.getRegInfo();
84 assert((NumPhysRegs == 0 || NumPhysRegs == TRI->getNumRegs()) &&
89 NumPhysRegs = TRI->getNumRegs();
90 RegsAvailable.resize(NumPhysRegs);
92 // Create reserved registers bitvector.
93 ReservedRegs = TRI->getReservedRegs(MF);
95 // Create callee-saved registers bitvector.
96 CalleeSavedRegs.resize(NumPhysRegs);
97 const unsigned *CSRegs = TRI->getCalleeSavedRegs();
99 for (unsigned i = 0; CSRegs[i]; ++i)
100 CalleeSavedRegs.set(CSRegs[i]);
103 // RS used within emit{Pro,Epi}logue()
112 void RegScavenger::addRegWithSubRegs(BitVector &BV, unsigned Reg) {
114 for (const unsigned *R = TRI->getSubRegisters(Reg); *R; R++)
118 void RegScavenger::addRegWithAliases(BitVector &BV, unsigned Reg) {
120 for (const unsigned *R = TRI->getAliasSet(Reg); *R; R++)
124 void RegScavenger::forward() {
130 assert(MBBI != MBB->end() && "Already at the end of the basic block!");
134 MachineInstr *MI = MBBI;
136 if (MI == ScavengeRestore) {
139 ScavengeRestore = NULL;
142 // Find out which registers are early clobbered, killed, defined, and marked
143 // def-dead in this instruction.
144 BitVector EarlyClobberRegs(NumPhysRegs);
145 BitVector KillRegs(NumPhysRegs);
146 BitVector DefRegs(NumPhysRegs);
147 BitVector DeadRegs(NumPhysRegs);
148 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
149 const MachineOperand &MO = MI->getOperand(i);
150 if (!MO.isReg() || MO.isUndef())
152 unsigned Reg = MO.getReg();
153 if (!Reg || isReserved(Reg))
157 // Two-address operands implicitly kill.
158 if (MO.isKill() || MI->isRegTiedToDefOperand(i))
159 addRegWithSubRegs(KillRegs, Reg);
163 addRegWithSubRegs(DeadRegs, Reg);
165 addRegWithSubRegs(DefRegs, Reg);
166 if (MO.isEarlyClobber())
167 addRegWithAliases(EarlyClobberRegs, Reg);
171 // Verify uses and defs.
172 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
173 const MachineOperand &MO = MI->getOperand(i);
174 if (!MO.isReg() || MO.isUndef())
176 unsigned Reg = MO.getReg();
177 if (!Reg || isReserved(Reg))
180 assert(isUsed(Reg) && "Using an undefined register!");
181 assert((!EarlyClobberRegs.test(Reg) || MI->isRegTiedToDefOperand(i)) &&
182 "Using an early clobbered register!");
186 // FIXME: Enable this once we've figured out how to correctly transfer
187 // implicit kills during codegen passes like the coalescer.
188 assert((KillRegs.test(Reg) || isUnused(Reg) ||
189 isLiveInButUnusedBefore(Reg, MI, MBB, TRI, MRI)) &&
190 "Re-defining a live register!");
195 // Commit the changes.
201 void RegScavenger::getRegsUsed(BitVector &used, bool includeReserved) {
203 used = ~RegsAvailable;
205 used = ~RegsAvailable & ~ReservedRegs;
208 /// CreateRegClassMask - Set the bits that represent the registers in the
209 /// TargetRegisterClass.
210 static void CreateRegClassMask(const TargetRegisterClass *RC, BitVector &Mask) {
211 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I != E;
216 unsigned RegScavenger::FindUnusedReg(const TargetRegisterClass *RC) const {
217 for (TargetRegisterClass::iterator I = RC->begin(), E = RC->end();
219 if (!isAliasUsed(*I))
224 /// findSurvivorReg - Return the candidate register that is unused for the
225 /// longest after MBBI. UseMI is set to the instruction where the search
228 /// No more than InstrLimit instructions are inspected.
230 unsigned RegScavenger::findSurvivorReg(MachineBasicBlock::iterator MI,
231 BitVector &Candidates,
233 MachineBasicBlock::iterator &UseMI) {
234 int Survivor = Candidates.find_first();
235 assert(Survivor > 0 && "No candidates for scavenging");
237 MachineBasicBlock::iterator ME = MBB->getFirstTerminator();
238 assert(MI != ME && "MI already at terminator");
240 for (++MI; InstrLimit > 0 && MI != ME; ++MI, --InstrLimit) {
241 // Remove any candidates touched by instruction.
242 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
243 const MachineOperand &MO = MI->getOperand(i);
244 if (!MO.isReg() || MO.isUndef() || !MO.getReg() ||
245 TargetRegisterInfo::isVirtualRegister(MO.getReg()))
247 Candidates.reset(MO.getReg());
248 for (const unsigned *R = TRI->getAliasSet(MO.getReg()); *R; R++)
249 Candidates.reset(*R);
252 // Was our survivor untouched by this instruction?
253 if (Candidates.test(Survivor))
256 // All candidates gone?
257 if (Candidates.none())
260 Survivor = Candidates.find_first();
263 // We ran out of candidates, so stop the search.
268 unsigned RegScavenger::scavengeRegister(const TargetRegisterClass *RC,
269 MachineBasicBlock::iterator I,
271 // Mask off the registers which are not in the TargetRegisterClass.
272 BitVector Candidates(NumPhysRegs, false);
273 CreateRegClassMask(RC, Candidates);
274 // Do not include reserved registers.
275 Candidates ^= ReservedRegs & Candidates;
277 // Exclude all the registers being used by the instruction.
278 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
279 MachineOperand &MO = I->getOperand(i);
280 if (MO.isReg() && MO.getReg() != 0 &&
281 !TargetRegisterInfo::isVirtualRegister(MO.getReg()))
282 Candidates.reset(MO.getReg());
285 // Find the register whose use is furthest away.
286 MachineBasicBlock::iterator UseMI;
287 unsigned SReg = findSurvivorReg(I, Candidates, 25, UseMI);
289 // If we found an unused register there is no reason to spill it. We have
290 // probably found a callee-saved register that has been saved in the
291 // prologue, but happens to be unused at this point.
292 if (!isAliasUsed(SReg))
295 assert(ScavengedReg == 0 &&
296 "Scavenger slot is live, unable to scavenge another register!");
298 // Avoid infinite regress
301 // If the target knows how to save/restore the register, let it do so;
302 // otherwise, use the emergency stack spill slot.
303 if (!TRI->saveScavengerRegister(*MBB, I, UseMI, RC, SReg)) {
304 // Spill the scavenged register before I.
305 assert(ScavengingFrameIndex >= 0 &&
306 "Cannot scavenge register without an emergency spill slot!");
307 TII->storeRegToStackSlot(*MBB, I, SReg, true, ScavengingFrameIndex, RC);
308 MachineBasicBlock::iterator II = prior(I);
309 TRI->eliminateFrameIndex(II, SPAdj, NULL, this);
311 // Restore the scavenged register before its use (or first terminator).
312 TII->loadRegFromStackSlot(*MBB, UseMI, SReg, ScavengingFrameIndex, RC);
315 ScavengeRestore = prior(UseMI);
317 // Doing this here leads to infinite regress.
318 // ScavengedReg = SReg;