12cab237bSDimitry Andric //===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
102cab237bSDimitry Andric /// \file This register allocator allocates registers to a basic block at a
112cab237bSDimitry Andric /// time, attempting to keep values in registers and reusing registers as
122cab237bSDimitry Andric /// appropriate.
13f22ef01cSRoman Divacky //
14f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
15f22ef01cSRoman Divacky
162cab237bSDimitry Andric #include "llvm/ADT/ArrayRef.h"
17f22ef01cSRoman Divacky #include "llvm/ADT/DenseMap.h"
18f22ef01cSRoman Divacky #include "llvm/ADT/IndexedMap.h"
19f22ef01cSRoman Divacky #include "llvm/ADT/SmallSet.h"
20f22ef01cSRoman Divacky #include "llvm/ADT/SmallVector.h"
21dff0c46cSDimitry Andric #include "llvm/ADT/SparseSet.h"
22f22ef01cSRoman Divacky #include "llvm/ADT/Statistic.h"
232cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
24139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
252cab237bSDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
26139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
27139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
28139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
292cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
30139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
31139f7f9bSDimitry Andric #include "llvm/CodeGen/RegAllocRegistry.h"
32139f7f9bSDimitry Andric #include "llvm/CodeGen/RegisterClassInfo.h"
332cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
342cab237bSDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
352cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
362cab237bSDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
372cab237bSDimitry Andric #include "llvm/IR/DebugLoc.h"
382cab237bSDimitry Andric #include "llvm/IR/Metadata.h"
392cab237bSDimitry Andric #include "llvm/MC/MCInstrDesc.h"
402cab237bSDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
412cab237bSDimitry Andric #include "llvm/Pass.h"
422cab237bSDimitry Andric #include "llvm/Support/Casting.h"
432cab237bSDimitry Andric #include "llvm/Support/Compiler.h"
44139f7f9bSDimitry Andric #include "llvm/Support/Debug.h"
45139f7f9bSDimitry Andric #include "llvm/Support/ErrorHandling.h"
462cab237bSDimitry Andric #include "llvm/Support/raw_ostream.h"
472cab237bSDimitry Andric #include <cassert>
482cab237bSDimitry Andric #include <tuple>
492cab237bSDimitry Andric #include <vector>
502cab237bSDimitry Andric
51f22ef01cSRoman Divacky using namespace llvm;
52f22ef01cSRoman Divacky
5391bc56edSDimitry Andric #define DEBUG_TYPE "regalloc"
5491bc56edSDimitry Andric
55f22ef01cSRoman Divacky STATISTIC(NumStores, "Number of stores added");
56f22ef01cSRoman Divacky STATISTIC(NumLoads , "Number of loads added");
57*b5893f02SDimitry Andric STATISTIC(NumCoalesced, "Number of copies coalesced");
58f22ef01cSRoman Divacky
59f22ef01cSRoman Divacky static RegisterRegAlloc
60f22ef01cSRoman Divacky fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
61f22ef01cSRoman Divacky
62f22ef01cSRoman Divacky namespace {
632cab237bSDimitry Andric
642cab237bSDimitry Andric class RegAllocFast : public MachineFunctionPass {
65f22ef01cSRoman Divacky public:
66f22ef01cSRoman Divacky static char ID;
672cab237bSDimitry Andric
RegAllocFast()682cab237bSDimitry Andric RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {}
693ca95b02SDimitry Andric
70f22ef01cSRoman Divacky private:
712cab237bSDimitry Andric MachineFrameInfo *MFI;
72f22ef01cSRoman Divacky MachineRegisterInfo *MRI;
73f22ef01cSRoman Divacky const TargetRegisterInfo *TRI;
74f22ef01cSRoman Divacky const TargetInstrInfo *TII;
75bd5abe19SDimitry Andric RegisterClassInfo RegClassInfo;
76f22ef01cSRoman Divacky
772cab237bSDimitry Andric /// Basic block currently being allocated.
78f22ef01cSRoman Divacky MachineBasicBlock *MBB;
79f22ef01cSRoman Divacky
802cab237bSDimitry Andric /// Maps virtual regs to the frame index where these values are spilled.
81f22ef01cSRoman Divacky IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
82f22ef01cSRoman Divacky
832cab237bSDimitry Andric /// Everything we know about a live virtual register.
84f22ef01cSRoman Divacky struct LiveReg {
852cab237bSDimitry Andric MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
862cab237bSDimitry Andric unsigned VirtReg; ///< Virtual register number.
872cab237bSDimitry Andric MCPhysReg PhysReg = 0; ///< Currently held here.
882cab237bSDimitry Andric unsigned short LastOpNum = 0; ///< OpNum on LastUse.
892cab237bSDimitry Andric bool Dirty = false; ///< Register needs spill.
90f22ef01cSRoman Divacky
LiveReg__anon233ffc900111::RegAllocFast::LiveReg91*b5893f02SDimitry Andric explicit LiveReg(unsigned VirtReg) : VirtReg(VirtReg) {}
92dff0c46cSDimitry Andric
getSparseSetIndex__anon233ffc900111::RegAllocFast::LiveReg937ae0e2c9SDimitry Andric unsigned getSparseSetIndex() const {
94dff0c46cSDimitry Andric return TargetRegisterInfo::virtReg2Index(VirtReg);
95dff0c46cSDimitry Andric }
96f22ef01cSRoman Divacky };
97f22ef01cSRoman Divacky
982cab237bSDimitry Andric using LiveRegMap = SparseSet<LiveReg>;
992cab237bSDimitry Andric /// This map contains entries for each virtual register that is currently
1002cab237bSDimitry Andric /// available in a physical register.
101f22ef01cSRoman Divacky LiveRegMap LiveVirtRegs;
102f22ef01cSRoman Divacky
103*b5893f02SDimitry Andric DenseMap<unsigned, SmallVector<MachineInstr *, 2>> LiveDbgValueMap;
104e580952dSDimitry Andric
105*b5893f02SDimitry Andric /// State of a physical register.
106f22ef01cSRoman Divacky enum RegState {
1072cab237bSDimitry Andric /// A disabled register is not available for allocation, but an alias may
1082cab237bSDimitry Andric /// be in use. A register can only be moved out of the disabled state if
1092cab237bSDimitry Andric /// all aliases are disabled.
110f22ef01cSRoman Divacky regDisabled,
111f22ef01cSRoman Divacky
1122cab237bSDimitry Andric /// A free register is not currently in use and can be allocated
1132cab237bSDimitry Andric /// immediately without checking aliases.
114f22ef01cSRoman Divacky regFree,
115f22ef01cSRoman Divacky
1162cab237bSDimitry Andric /// A reserved register has been assigned explicitly (e.g., setting up a
1172cab237bSDimitry Andric /// call parameter), and it remains reserved until it is used.
118f22ef01cSRoman Divacky regReserved
119f22ef01cSRoman Divacky
1202cab237bSDimitry Andric /// A register state may also be a virtual register number, indication
1212cab237bSDimitry Andric /// that the physical register is currently allocated to a virtual
1222cab237bSDimitry Andric /// register. In that case, LiveVirtRegs contains the inverse mapping.
123f22ef01cSRoman Divacky };
124f22ef01cSRoman Divacky
125*b5893f02SDimitry Andric /// Maps each physical register to a RegState enum or a virtual register.
126f22ef01cSRoman Divacky std::vector<unsigned> PhysRegState;
127f22ef01cSRoman Divacky
1282cab237bSDimitry Andric SmallVector<unsigned, 16> VirtDead;
1292cab237bSDimitry Andric SmallVector<MachineInstr *, 32> Coalesced;
1303861d79fSDimitry Andric
131*b5893f02SDimitry Andric using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
1322cab237bSDimitry Andric /// Set of register units that are used in the current instruction, and so
1332cab237bSDimitry Andric /// cannot be allocated.
134*b5893f02SDimitry Andric RegUnitSet UsedInInstr;
135*b5893f02SDimitry Andric
136*b5893f02SDimitry Andric void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
137f22ef01cSRoman Divacky
1382cab237bSDimitry Andric /// Mark a physreg as used in this instruction.
markRegUsedInInstr(MCPhysReg PhysReg)1392cab237bSDimitry Andric void markRegUsedInInstr(MCPhysReg PhysReg) {
140139f7f9bSDimitry Andric for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
141139f7f9bSDimitry Andric UsedInInstr.insert(*Units);
142139f7f9bSDimitry Andric }
143139f7f9bSDimitry Andric
1442cab237bSDimitry Andric /// Check if a physreg or any of its aliases are used in this instruction.
isRegUsedInInstr(MCPhysReg PhysReg) const1452cab237bSDimitry Andric bool isRegUsedInInstr(MCPhysReg PhysReg) const {
146139f7f9bSDimitry Andric for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
147139f7f9bSDimitry Andric if (UsedInInstr.count(*Units))
148139f7f9bSDimitry Andric return true;
149139f7f9bSDimitry Andric return false;
150139f7f9bSDimitry Andric }
151139f7f9bSDimitry Andric
15291bc56edSDimitry Andric enum : unsigned {
153*b5893f02SDimitry Andric spillClean = 50,
154f22ef01cSRoman Divacky spillDirty = 100,
155f22ef01cSRoman Divacky spillImpossible = ~0u
156f22ef01cSRoman Divacky };
1572cab237bSDimitry Andric
158f22ef01cSRoman Divacky public:
getPassName() const159d88c1a5aSDimitry Andric StringRef getPassName() const override { return "Fast Register Allocator"; }
160f22ef01cSRoman Divacky
getAnalysisUsage(AnalysisUsage & AU) const16191bc56edSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
162f22ef01cSRoman Divacky AU.setPreservesCFG();
163f22ef01cSRoman Divacky MachineFunctionPass::getAnalysisUsage(AU);
164f22ef01cSRoman Divacky }
165f22ef01cSRoman Divacky
getRequiredProperties() const166d88c1a5aSDimitry Andric MachineFunctionProperties getRequiredProperties() const override {
167d88c1a5aSDimitry Andric return MachineFunctionProperties().set(
168d88c1a5aSDimitry Andric MachineFunctionProperties::Property::NoPHIs);
169d88c1a5aSDimitry Andric }
170d88c1a5aSDimitry Andric
getSetProperties() const1713ca95b02SDimitry Andric MachineFunctionProperties getSetProperties() const override {
1723ca95b02SDimitry Andric return MachineFunctionProperties().set(
173d88c1a5aSDimitry Andric MachineFunctionProperties::Property::NoVRegs);
1743ca95b02SDimitry Andric }
1753ca95b02SDimitry Andric
176f22ef01cSRoman Divacky private:
1774ba319b5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
178*b5893f02SDimitry Andric
1792cab237bSDimitry Andric void allocateBasicBlock(MachineBasicBlock &MBB);
180*b5893f02SDimitry Andric void allocateInstruction(MachineInstr &MI);
181*b5893f02SDimitry Andric void handleDebugValue(MachineInstr &MI);
1822cab237bSDimitry Andric void handleThroughOperands(MachineInstr &MI,
183ffd1746dSEd Schouten SmallVectorImpl<unsigned> &VirtDead);
1842cab237bSDimitry Andric bool isLastUseOfLocalReg(const MachineOperand &MO) const;
185f22ef01cSRoman Divacky
1862cab237bSDimitry Andric void addKillFlag(const LiveReg &LRI);
187*b5893f02SDimitry Andric void killVirtReg(LiveReg &LR);
188f22ef01cSRoman Divacky void killVirtReg(unsigned VirtReg);
189*b5893f02SDimitry Andric void spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR);
190f22ef01cSRoman Divacky void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
191f22ef01cSRoman Divacky
1922cab237bSDimitry Andric void usePhysReg(MachineOperand &MO);
193842d113bSDimitry Andric void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg,
194842d113bSDimitry Andric RegState NewState);
1952cab237bSDimitry Andric unsigned calcSpillCost(MCPhysReg PhysReg) const;
1962cab237bSDimitry Andric void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg);
1972cab237bSDimitry Andric
findLiveVirtReg(unsigned VirtReg)198dff0c46cSDimitry Andric LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
199dff0c46cSDimitry Andric return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
200dff0c46cSDimitry Andric }
2012cab237bSDimitry Andric
findLiveVirtReg(unsigned VirtReg) const202dff0c46cSDimitry Andric LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
203dff0c46cSDimitry Andric return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
204dff0c46cSDimitry Andric }
2052cab237bSDimitry Andric
206*b5893f02SDimitry Andric void allocVirtReg(MachineInstr &MI, LiveReg &LR, unsigned Hint);
207*b5893f02SDimitry Andric MCPhysReg defineVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
208dff0c46cSDimitry Andric unsigned Hint);
209*b5893f02SDimitry Andric LiveReg &reloadVirtReg(MachineInstr &MI, unsigned OpNum, unsigned VirtReg,
210*b5893f02SDimitry Andric unsigned Hint);
2113861d79fSDimitry Andric void spillAll(MachineBasicBlock::iterator MI);
212*b5893f02SDimitry Andric bool setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
213*b5893f02SDimitry Andric
214*b5893f02SDimitry Andric int getStackSpaceFor(unsigned VirtReg);
215*b5893f02SDimitry Andric void spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
216*b5893f02SDimitry Andric MCPhysReg AssignedReg, bool Kill);
217*b5893f02SDimitry Andric void reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
218*b5893f02SDimitry Andric MCPhysReg PhysReg);
2192cab237bSDimitry Andric
2202cab237bSDimitry Andric void dumpState();
221f22ef01cSRoman Divacky };
222f22ef01cSRoman Divacky
2232cab237bSDimitry Andric } // end anonymous namespace
224c4394386SDimitry Andric
2252cab237bSDimitry Andric char RegAllocFast::ID = 0;
2262cab237bSDimitry Andric
2272cab237bSDimitry Andric INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
2282cab237bSDimitry Andric false)
2292cab237bSDimitry Andric
setPhysRegState(MCPhysReg PhysReg,unsigned NewState)230*b5893f02SDimitry Andric void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
231*b5893f02SDimitry Andric PhysRegState[PhysReg] = NewState;
232*b5893f02SDimitry Andric }
233*b5893f02SDimitry Andric
2342cab237bSDimitry Andric /// This allocates space for the specified virtual register to be held on the
2352cab237bSDimitry Andric /// stack.
getStackSpaceFor(unsigned VirtReg)236*b5893f02SDimitry Andric int RegAllocFast::getStackSpaceFor(unsigned VirtReg) {
237f22ef01cSRoman Divacky // Find the location Reg would belong...
238f22ef01cSRoman Divacky int SS = StackSlotForVirtReg[VirtReg];
2392cab237bSDimitry Andric // Already has space allocated?
240f22ef01cSRoman Divacky if (SS != -1)
2412cab237bSDimitry Andric return SS;
242f22ef01cSRoman Divacky
243f22ef01cSRoman Divacky // Allocate a new stack object for this spill location...
244*b5893f02SDimitry Andric const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
2452cab237bSDimitry Andric unsigned Size = TRI->getSpillSize(RC);
2462cab237bSDimitry Andric unsigned Align = TRI->getSpillAlignment(RC);
2472cab237bSDimitry Andric int FrameIdx = MFI->CreateSpillStackObject(Size, Align);
248f22ef01cSRoman Divacky
249f22ef01cSRoman Divacky // Assign the slot.
250f22ef01cSRoman Divacky StackSlotForVirtReg[VirtReg] = FrameIdx;
251f22ef01cSRoman Divacky return FrameIdx;
252f22ef01cSRoman Divacky }
253f22ef01cSRoman Divacky
254*b5893f02SDimitry Andric /// Insert spill instruction for \p AssignedReg before \p Before. Update
255*b5893f02SDimitry Andric /// DBG_VALUEs with \p VirtReg operands with the stack slot.
spill(MachineBasicBlock::iterator Before,unsigned VirtReg,MCPhysReg AssignedReg,bool Kill)256*b5893f02SDimitry Andric void RegAllocFast::spill(MachineBasicBlock::iterator Before, unsigned VirtReg,
257*b5893f02SDimitry Andric MCPhysReg AssignedReg, bool Kill) {
258*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
259*b5893f02SDimitry Andric << " in " << printReg(AssignedReg, TRI));
260*b5893f02SDimitry Andric int FI = getStackSpaceFor(VirtReg);
261*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
262*b5893f02SDimitry Andric
263*b5893f02SDimitry Andric const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
264*b5893f02SDimitry Andric TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
265*b5893f02SDimitry Andric ++NumStores;
266*b5893f02SDimitry Andric
267*b5893f02SDimitry Andric // If this register is used by DBG_VALUE then insert new DBG_VALUE to
268*b5893f02SDimitry Andric // identify spilled location as the place to find corresponding variable's
269*b5893f02SDimitry Andric // value.
270*b5893f02SDimitry Andric SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
271*b5893f02SDimitry Andric for (MachineInstr *DBG : LRIDbgValues) {
272*b5893f02SDimitry Andric MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
273*b5893f02SDimitry Andric assert(NewDV->getParent() == MBB && "dangling parent pointer");
274*b5893f02SDimitry Andric (void)NewDV;
275*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
276*b5893f02SDimitry Andric }
277*b5893f02SDimitry Andric // Now this register is spilled there is should not be any DBG_VALUE
278*b5893f02SDimitry Andric // pointing to this register because they are all pointing to spilled value
279*b5893f02SDimitry Andric // now.
280*b5893f02SDimitry Andric LRIDbgValues.clear();
281*b5893f02SDimitry Andric }
282*b5893f02SDimitry Andric
283*b5893f02SDimitry Andric /// Insert reload instruction for \p PhysReg before \p Before.
reload(MachineBasicBlock::iterator Before,unsigned VirtReg,MCPhysReg PhysReg)284*b5893f02SDimitry Andric void RegAllocFast::reload(MachineBasicBlock::iterator Before, unsigned VirtReg,
285*b5893f02SDimitry Andric MCPhysReg PhysReg) {
286*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
287*b5893f02SDimitry Andric << printReg(PhysReg, TRI) << '\n');
288*b5893f02SDimitry Andric int FI = getStackSpaceFor(VirtReg);
289*b5893f02SDimitry Andric const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
290*b5893f02SDimitry Andric TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
291*b5893f02SDimitry Andric ++NumLoads;
292*b5893f02SDimitry Andric }
293*b5893f02SDimitry Andric
2942cab237bSDimitry Andric /// Return true if MO is the only remaining reference to its virtual register,
2952cab237bSDimitry Andric /// and it is guaranteed to be a block-local register.
isLastUseOfLocalReg(const MachineOperand & MO) const2962cab237bSDimitry Andric bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const {
297f22ef01cSRoman Divacky // If the register has ever been spilled or reloaded, we conservatively assume
298f22ef01cSRoman Divacky // it is a global register used in multiple blocks.
299f22ef01cSRoman Divacky if (StackSlotForVirtReg[MO.getReg()] != -1)
300f22ef01cSRoman Divacky return false;
301f22ef01cSRoman Divacky
302f22ef01cSRoman Divacky // Check that the use/def chain has exactly one operand - MO.
3037ae0e2c9SDimitry Andric MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
30491bc56edSDimitry Andric if (&*I != &MO)
3057ae0e2c9SDimitry Andric return false;
3067ae0e2c9SDimitry Andric return ++I == MRI->reg_nodbg_end();
307f22ef01cSRoman Divacky }
308f22ef01cSRoman Divacky
3092cab237bSDimitry Andric /// Set kill flags on last use of a virtual register.
addKillFlag(const LiveReg & LR)3102cab237bSDimitry Andric void RegAllocFast::addKillFlag(const LiveReg &LR) {
311f22ef01cSRoman Divacky if (!LR.LastUse) return;
312f22ef01cSRoman Divacky MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
313f22ef01cSRoman Divacky if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
314f22ef01cSRoman Divacky if (MO.getReg() == LR.PhysReg)
315f22ef01cSRoman Divacky MO.setIsKill();
316c4394386SDimitry Andric // else, don't do anything we are problably redefining a
317c4394386SDimitry Andric // subreg of this register and given we don't track which
318c4394386SDimitry Andric // lanes are actually dead, we cannot insert a kill flag here.
319c4394386SDimitry Andric // Otherwise we may end up in a situation like this:
3202cab237bSDimitry Andric // ... = (MO) physreg:sub1, implicit killed physreg
321c4394386SDimitry Andric // ... <== Here we would allow later pass to reuse physreg:sub1
322c4394386SDimitry Andric // which is potentially wrong.
323c4394386SDimitry Andric // LR:sub0 = ...
324c4394386SDimitry Andric // ... = LR.sub1 <== This is going to use physreg:sub1
325f22ef01cSRoman Divacky }
326f22ef01cSRoman Divacky }
327f22ef01cSRoman Divacky
3282cab237bSDimitry Andric /// Mark virtreg as no longer available.
killVirtReg(LiveReg & LR)329*b5893f02SDimitry Andric void RegAllocFast::killVirtReg(LiveReg &LR) {
330*b5893f02SDimitry Andric addKillFlag(LR);
331*b5893f02SDimitry Andric assert(PhysRegState[LR.PhysReg] == LR.VirtReg &&
332dff0c46cSDimitry Andric "Broken RegState mapping");
333*b5893f02SDimitry Andric setPhysRegState(LR.PhysReg, regFree);
334*b5893f02SDimitry Andric LR.PhysReg = 0;
335f22ef01cSRoman Divacky }
336f22ef01cSRoman Divacky
3372cab237bSDimitry Andric /// Mark virtreg as no longer available.
killVirtReg(unsigned VirtReg)3382cab237bSDimitry Andric void RegAllocFast::killVirtReg(unsigned VirtReg) {
339f22ef01cSRoman Divacky assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
340f22ef01cSRoman Divacky "killVirtReg needs a virtual register");
341dff0c46cSDimitry Andric LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
342*b5893f02SDimitry Andric if (LRI != LiveVirtRegs.end() && LRI->PhysReg)
343*b5893f02SDimitry Andric killVirtReg(*LRI);
344f22ef01cSRoman Divacky }
345f22ef01cSRoman Divacky
3462cab237bSDimitry Andric /// This method spills the value specified by VirtReg into the corresponding
3472cab237bSDimitry Andric /// stack slot if needed.
spillVirtReg(MachineBasicBlock::iterator MI,unsigned VirtReg)3482cab237bSDimitry Andric void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI,
3492cab237bSDimitry Andric unsigned VirtReg) {
350f22ef01cSRoman Divacky assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
351f22ef01cSRoman Divacky "Spilling a physical register is illegal!");
352dff0c46cSDimitry Andric LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
353*b5893f02SDimitry Andric assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
354*b5893f02SDimitry Andric "Spilling unmapped virtual register");
355*b5893f02SDimitry Andric spillVirtReg(MI, *LRI);
356f22ef01cSRoman Divacky }
357f22ef01cSRoman Divacky
3582cab237bSDimitry Andric /// Do the actual work of spilling.
spillVirtReg(MachineBasicBlock::iterator MI,LiveReg & LR)359*b5893f02SDimitry Andric void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, LiveReg &LR) {
360*b5893f02SDimitry Andric assert(PhysRegState[LR.PhysReg] == LR.VirtReg && "Broken RegState mapping");
361f22ef01cSRoman Divacky
362f22ef01cSRoman Divacky if (LR.Dirty) {
363f22ef01cSRoman Divacky // If this physreg is used by the instruction, we want to kill it on the
364f22ef01cSRoman Divacky // instruction, not on the spill.
3653ca95b02SDimitry Andric bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
366f22ef01cSRoman Divacky LR.Dirty = false;
367f22ef01cSRoman Divacky
368*b5893f02SDimitry Andric spill(MI, LR.VirtReg, LR.PhysReg, SpillKill);
369*b5893f02SDimitry Andric
370f22ef01cSRoman Divacky if (SpillKill)
37191bc56edSDimitry Andric LR.LastUse = nullptr; // Don't kill register again
372f22ef01cSRoman Divacky }
373*b5893f02SDimitry Andric killVirtReg(LR);
374f22ef01cSRoman Divacky }
375f22ef01cSRoman Divacky
3762cab237bSDimitry Andric /// Spill all dirty virtregs without killing them.
spillAll(MachineBasicBlock::iterator MI)3772cab237bSDimitry Andric void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) {
378*b5893f02SDimitry Andric if (LiveVirtRegs.empty())
379*b5893f02SDimitry Andric return;
380f22ef01cSRoman Divacky // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
381f22ef01cSRoman Divacky // of spilling here is deterministic, if arbitrary.
382*b5893f02SDimitry Andric for (LiveReg &LR : LiveVirtRegs) {
383*b5893f02SDimitry Andric if (!LR.PhysReg)
384*b5893f02SDimitry Andric continue;
385*b5893f02SDimitry Andric spillVirtReg(MI, LR);
386*b5893f02SDimitry Andric }
387f22ef01cSRoman Divacky LiveVirtRegs.clear();
388f22ef01cSRoman Divacky }
389f22ef01cSRoman Divacky
3902cab237bSDimitry Andric /// Handle the direct use of a physical register. Check that the register is
3912cab237bSDimitry Andric /// not used by a virtreg. Kill the physreg, marking it free. This may add
3922cab237bSDimitry Andric /// implicit kills to MO->getParent() and invalidate MO.
usePhysReg(MachineOperand & MO)3932cab237bSDimitry Andric void RegAllocFast::usePhysReg(MachineOperand &MO) {
3943ca95b02SDimitry Andric // Ignore undef uses.
3953ca95b02SDimitry Andric if (MO.isUndef())
3963ca95b02SDimitry Andric return;
3973ca95b02SDimitry Andric
3982cab237bSDimitry Andric unsigned PhysReg = MO.getReg();
3992cab237bSDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
4002cab237bSDimitry Andric "Bad usePhysReg operand");
4012cab237bSDimitry Andric
402139f7f9bSDimitry Andric markRegUsedInInstr(PhysReg);
403f22ef01cSRoman Divacky switch (PhysRegState[PhysReg]) {
404f22ef01cSRoman Divacky case regDisabled:
405f22ef01cSRoman Divacky break;
406f22ef01cSRoman Divacky case regReserved:
407f22ef01cSRoman Divacky PhysRegState[PhysReg] = regFree;
408d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
409f22ef01cSRoman Divacky case regFree:
410f22ef01cSRoman Divacky MO.setIsKill();
411f22ef01cSRoman Divacky return;
412f22ef01cSRoman Divacky default:
4132754fe60SDimitry Andric // The physreg was allocated to a virtual register. That means the value we
414f22ef01cSRoman Divacky // wanted has been clobbered.
415f22ef01cSRoman Divacky llvm_unreachable("Instruction uses an allocated register");
416f22ef01cSRoman Divacky }
417f22ef01cSRoman Divacky
418f22ef01cSRoman Divacky // Maybe a superregister is reserved?
4197ae0e2c9SDimitry Andric for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
4202cab237bSDimitry Andric MCPhysReg Alias = *AI;
421f22ef01cSRoman Divacky switch (PhysRegState[Alias]) {
422f22ef01cSRoman Divacky case regDisabled:
423f22ef01cSRoman Divacky break;
424f22ef01cSRoman Divacky case regReserved:
42539d628a0SDimitry Andric // Either PhysReg is a subregister of Alias and we mark the
42639d628a0SDimitry Andric // whole register as free, or PhysReg is the superregister of
42739d628a0SDimitry Andric // Alias and we mark all the aliases as disabled before freeing
42839d628a0SDimitry Andric // PhysReg.
42939d628a0SDimitry Andric // In the latter case, since PhysReg was disabled, this means that
43039d628a0SDimitry Andric // its value is defined only by physical sub-registers. This check
43139d628a0SDimitry Andric // is performed by the assert of the default case in this loop.
43239d628a0SDimitry Andric // Note: The value of the superregister may only be partial
43339d628a0SDimitry Andric // defined, that is why regDisabled is a valid state for aliases.
43439d628a0SDimitry Andric assert((TRI->isSuperRegister(PhysReg, Alias) ||
43539d628a0SDimitry Andric TRI->isSuperRegister(Alias, PhysReg)) &&
436f22ef01cSRoman Divacky "Instruction is not using a subregister of a reserved register");
437d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
438f22ef01cSRoman Divacky case regFree:
439f22ef01cSRoman Divacky if (TRI->isSuperRegister(PhysReg, Alias)) {
440f22ef01cSRoman Divacky // Leave the superregister in the working set.
441*b5893f02SDimitry Andric setPhysRegState(Alias, regFree);
442f22ef01cSRoman Divacky MO.getParent()->addRegisterKilled(Alias, TRI, true);
443f22ef01cSRoman Divacky return;
444f22ef01cSRoman Divacky }
445f22ef01cSRoman Divacky // Some other alias was in the working set - clear it.
446*b5893f02SDimitry Andric setPhysRegState(Alias, regDisabled);
447f22ef01cSRoman Divacky break;
448f22ef01cSRoman Divacky default:
449f22ef01cSRoman Divacky llvm_unreachable("Instruction uses an alias of an allocated register");
450f22ef01cSRoman Divacky }
451f22ef01cSRoman Divacky }
452f22ef01cSRoman Divacky
453f22ef01cSRoman Divacky // All aliases are disabled, bring register into working set.
454*b5893f02SDimitry Andric setPhysRegState(PhysReg, regFree);
455f22ef01cSRoman Divacky MO.setIsKill();
456f22ef01cSRoman Divacky }
457f22ef01cSRoman Divacky
4582cab237bSDimitry Andric /// Mark PhysReg as reserved or free after spilling any virtregs. This is very
4592cab237bSDimitry Andric /// similar to defineVirtReg except the physreg is reserved instead of
4602cab237bSDimitry Andric /// allocated.
definePhysReg(MachineBasicBlock::iterator MI,MCPhysReg PhysReg,RegState NewState)461842d113bSDimitry Andric void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI,
462842d113bSDimitry Andric MCPhysReg PhysReg, RegState NewState) {
463139f7f9bSDimitry Andric markRegUsedInInstr(PhysReg);
464f22ef01cSRoman Divacky switch (unsigned VirtReg = PhysRegState[PhysReg]) {
465f22ef01cSRoman Divacky case regDisabled:
466f22ef01cSRoman Divacky break;
467f22ef01cSRoman Divacky default:
468f22ef01cSRoman Divacky spillVirtReg(MI, VirtReg);
469d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
470f22ef01cSRoman Divacky case regFree:
471f22ef01cSRoman Divacky case regReserved:
472*b5893f02SDimitry Andric setPhysRegState(PhysReg, NewState);
473f22ef01cSRoman Divacky return;
474f22ef01cSRoman Divacky }
475f22ef01cSRoman Divacky
476f22ef01cSRoman Divacky // This is a disabled register, disable all aliases.
477*b5893f02SDimitry Andric setPhysRegState(PhysReg, NewState);
4787ae0e2c9SDimitry Andric for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
4792cab237bSDimitry Andric MCPhysReg Alias = *AI;
480f22ef01cSRoman Divacky switch (unsigned VirtReg = PhysRegState[Alias]) {
481f22ef01cSRoman Divacky case regDisabled:
482f22ef01cSRoman Divacky break;
483f22ef01cSRoman Divacky default:
484f22ef01cSRoman Divacky spillVirtReg(MI, VirtReg);
485d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
486f22ef01cSRoman Divacky case regFree:
487f22ef01cSRoman Divacky case regReserved:
488*b5893f02SDimitry Andric setPhysRegState(Alias, regDisabled);
489f22ef01cSRoman Divacky if (TRI->isSuperRegister(PhysReg, Alias))
490f22ef01cSRoman Divacky return;
491f22ef01cSRoman Divacky break;
492f22ef01cSRoman Divacky }
493f22ef01cSRoman Divacky }
494f22ef01cSRoman Divacky }
495f22ef01cSRoman Divacky
496*b5893f02SDimitry Andric /// Return the cost of spilling clearing out PhysReg and aliases so it is free
497*b5893f02SDimitry Andric /// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
498*b5893f02SDimitry Andric /// disabled - it can be allocated directly.
4992cab237bSDimitry Andric /// \returns spillImpossible when PhysReg or an alias can't be spilled.
calcSpillCost(MCPhysReg PhysReg) const5002cab237bSDimitry Andric unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
501139f7f9bSDimitry Andric if (isRegUsedInInstr(PhysReg)) {
5024ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
5034ba319b5SDimitry Andric << " is already used in instr.\n");
504f22ef01cSRoman Divacky return spillImpossible;
5053b0f4066SDimitry Andric }
506f22ef01cSRoman Divacky switch (unsigned VirtReg = PhysRegState[PhysReg]) {
507f22ef01cSRoman Divacky case regDisabled:
508f22ef01cSRoman Divacky break;
509f22ef01cSRoman Divacky case regFree:
510f22ef01cSRoman Divacky return 0;
511f22ef01cSRoman Divacky case regReserved:
5124ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding "
5132cab237bSDimitry Andric << printReg(PhysReg, TRI) << " is reserved already.\n");
514f22ef01cSRoman Divacky return spillImpossible;
515dff0c46cSDimitry Andric default: {
516*b5893f02SDimitry Andric LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
517*b5893f02SDimitry Andric assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
518*b5893f02SDimitry Andric "Missing VirtReg entry");
519*b5893f02SDimitry Andric return LRI->Dirty ? spillDirty : spillClean;
520dff0c46cSDimitry Andric }
521f22ef01cSRoman Divacky }
522f22ef01cSRoman Divacky
5233b0f4066SDimitry Andric // This is a disabled register, add up cost of aliases.
5244ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n");
525f22ef01cSRoman Divacky unsigned Cost = 0;
5267ae0e2c9SDimitry Andric for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
5272cab237bSDimitry Andric MCPhysReg Alias = *AI;
528f22ef01cSRoman Divacky switch (unsigned VirtReg = PhysRegState[Alias]) {
529f22ef01cSRoman Divacky case regDisabled:
530f22ef01cSRoman Divacky break;
531f22ef01cSRoman Divacky case regFree:
532f22ef01cSRoman Divacky ++Cost;
533f22ef01cSRoman Divacky break;
534f22ef01cSRoman Divacky case regReserved:
535f22ef01cSRoman Divacky return spillImpossible;
536dff0c46cSDimitry Andric default: {
537*b5893f02SDimitry Andric LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
538*b5893f02SDimitry Andric assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
539*b5893f02SDimitry Andric "Missing VirtReg entry");
540*b5893f02SDimitry Andric Cost += LRI->Dirty ? spillDirty : spillClean;
541f22ef01cSRoman Divacky break;
542f22ef01cSRoman Divacky }
543f22ef01cSRoman Divacky }
544dff0c46cSDimitry Andric }
545f22ef01cSRoman Divacky return Cost;
546f22ef01cSRoman Divacky }
547f22ef01cSRoman Divacky
5484ba319b5SDimitry Andric /// This method updates local state so that we know that PhysReg is the
5492cab237bSDimitry Andric /// proper container for VirtReg now. The physical register must not be used
5502cab237bSDimitry Andric /// for anything else when this is called.
assignVirtToPhysReg(LiveReg & LR,MCPhysReg PhysReg)5512cab237bSDimitry Andric void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) {
552*b5893f02SDimitry Andric unsigned VirtReg = LR.VirtReg;
553*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
554*b5893f02SDimitry Andric << printReg(PhysReg, TRI) << '\n');
555*b5893f02SDimitry Andric assert(LR.PhysReg == 0 && "Already assigned a physreg");
556*b5893f02SDimitry Andric assert(PhysReg != 0 && "Trying to assign no register");
557dff0c46cSDimitry Andric LR.PhysReg = PhysReg;
558*b5893f02SDimitry Andric setPhysRegState(PhysReg, VirtReg);
559f22ef01cSRoman Divacky }
560f22ef01cSRoman Divacky
5612cab237bSDimitry Andric /// Allocates a physical register for VirtReg.
allocVirtReg(MachineInstr & MI,LiveReg & LR,unsigned Hint)562*b5893f02SDimitry Andric void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR, unsigned Hint) {
563*b5893f02SDimitry Andric const unsigned VirtReg = LR.VirtReg;
564f22ef01cSRoman Divacky
565f22ef01cSRoman Divacky assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
566f22ef01cSRoman Divacky "Can only allocate virtual registers");
567f22ef01cSRoman Divacky
5682cab237bSDimitry Andric const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
569*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
570*b5893f02SDimitry Andric << " in class " << TRI->getRegClassName(&RC) << '\n');
571*b5893f02SDimitry Andric
572*b5893f02SDimitry Andric // Take hint when possible.
5732cab237bSDimitry Andric if (TargetRegisterInfo::isPhysicalRegister(Hint) &&
5742cab237bSDimitry Andric MRI->isAllocatable(Hint) && RC.contains(Hint)) {
57517a519f9SDimitry Andric // Ignore the hint if we would have to spill a dirty register.
57617a519f9SDimitry Andric unsigned Cost = calcSpillCost(Hint);
57717a519f9SDimitry Andric if (Cost < spillDirty) {
57817a519f9SDimitry Andric if (Cost)
579f22ef01cSRoman Divacky definePhysReg(MI, Hint, regFree);
580*b5893f02SDimitry Andric assignVirtToPhysReg(LR, Hint);
581*b5893f02SDimitry Andric return;
582f22ef01cSRoman Divacky }
583f22ef01cSRoman Divacky }
584f22ef01cSRoman Divacky
585f22ef01cSRoman Divacky // First try to find a completely free register.
586*b5893f02SDimitry Andric ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
587*b5893f02SDimitry Andric for (MCPhysReg PhysReg : AllocationOrder) {
588139f7f9bSDimitry Andric if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
589*b5893f02SDimitry Andric assignVirtToPhysReg(LR, PhysReg);
590*b5893f02SDimitry Andric return;
591dff0c46cSDimitry Andric }
592f22ef01cSRoman Divacky }
593f22ef01cSRoman Divacky
594*b5893f02SDimitry Andric MCPhysReg BestReg = 0;
5952cab237bSDimitry Andric unsigned BestCost = spillImpossible;
596*b5893f02SDimitry Andric for (MCPhysReg PhysReg : AllocationOrder) {
597*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
5982cab237bSDimitry Andric unsigned Cost = calcSpillCost(PhysReg);
599*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
600*b5893f02SDimitry Andric // Immediate take a register with cost 0.
601dff0c46cSDimitry Andric if (Cost == 0) {
602*b5893f02SDimitry Andric assignVirtToPhysReg(LR, PhysReg);
603*b5893f02SDimitry Andric return;
604dff0c46cSDimitry Andric }
605*b5893f02SDimitry Andric if (Cost < BestCost) {
606*b5893f02SDimitry Andric BestReg = PhysReg;
607*b5893f02SDimitry Andric BestCost = Cost;
608*b5893f02SDimitry Andric }
609f22ef01cSRoman Divacky }
610f22ef01cSRoman Divacky
611*b5893f02SDimitry Andric if (!BestReg) {
612*b5893f02SDimitry Andric // Nothing we can do: Report an error and keep going with an invalid
613*b5893f02SDimitry Andric // allocation.
6143ca95b02SDimitry Andric if (MI.isInlineAsm())
6153ca95b02SDimitry Andric MI.emitError("inline assembly requires more registers than available");
616f785676fSDimitry Andric else
6173ca95b02SDimitry Andric MI.emitError("ran out of registers during register allocation");
618*b5893f02SDimitry Andric definePhysReg(MI, *AllocationOrder.begin(), regFree);
619*b5893f02SDimitry Andric assignVirtToPhysReg(LR, *AllocationOrder.begin());
620*b5893f02SDimitry Andric return;
621*b5893f02SDimitry Andric }
622*b5893f02SDimitry Andric
623*b5893f02SDimitry Andric definePhysReg(MI, BestReg, regFree);
624*b5893f02SDimitry Andric assignVirtToPhysReg(LR, BestReg);
625f22ef01cSRoman Divacky }
626f22ef01cSRoman Divacky
6272cab237bSDimitry Andric /// Allocates a register for VirtReg and mark it as dirty.
defineVirtReg(MachineInstr & MI,unsigned OpNum,unsigned VirtReg,unsigned Hint)628*b5893f02SDimitry Andric MCPhysReg RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
629*b5893f02SDimitry Andric unsigned VirtReg, unsigned Hint) {
630f22ef01cSRoman Divacky assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
631f22ef01cSRoman Divacky "Not a virtual register");
632f22ef01cSRoman Divacky LiveRegMap::iterator LRI;
633f22ef01cSRoman Divacky bool New;
63491bc56edSDimitry Andric std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
635*b5893f02SDimitry Andric if (!LRI->PhysReg) {
636f22ef01cSRoman Divacky // If there is no hint, peek at the only use of this register.
637f22ef01cSRoman Divacky if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
638f22ef01cSRoman Divacky MRI->hasOneNonDBGUse(VirtReg)) {
63991bc56edSDimitry Andric const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
640f22ef01cSRoman Divacky // It's a copy, use the destination register as a hint.
641ffd1746dSEd Schouten if (UseMI.isCopyLike())
642ffd1746dSEd Schouten Hint = UseMI.getOperand(0).getReg();
643f22ef01cSRoman Divacky }
644*b5893f02SDimitry Andric allocVirtReg(MI, *LRI, Hint);
645dff0c46cSDimitry Andric } else if (LRI->LastUse) {
646f22ef01cSRoman Divacky // Redefining a live register - kill at the last use, unless it is this
647f22ef01cSRoman Divacky // instruction defining VirtReg multiple times.
6483ca95b02SDimitry Andric if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
649dff0c46cSDimitry Andric addKillFlag(*LRI);
650f22ef01cSRoman Divacky }
651dff0c46cSDimitry Andric assert(LRI->PhysReg && "Register not assigned");
6523ca95b02SDimitry Andric LRI->LastUse = &MI;
653dff0c46cSDimitry Andric LRI->LastOpNum = OpNum;
654dff0c46cSDimitry Andric LRI->Dirty = true;
655139f7f9bSDimitry Andric markRegUsedInInstr(LRI->PhysReg);
656*b5893f02SDimitry Andric return LRI->PhysReg;
657f22ef01cSRoman Divacky }
658f22ef01cSRoman Divacky
6592cab237bSDimitry Andric /// Make sure VirtReg is available in a physreg and return it.
reloadVirtReg(MachineInstr & MI,unsigned OpNum,unsigned VirtReg,unsigned Hint)660*b5893f02SDimitry Andric RegAllocFast::LiveReg &RegAllocFast::reloadVirtReg(MachineInstr &MI,
6613ca95b02SDimitry Andric unsigned OpNum,
6623ca95b02SDimitry Andric unsigned VirtReg,
6633ca95b02SDimitry Andric unsigned Hint) {
664f22ef01cSRoman Divacky assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
665f22ef01cSRoman Divacky "Not a virtual register");
666f22ef01cSRoman Divacky LiveRegMap::iterator LRI;
667f22ef01cSRoman Divacky bool New;
66891bc56edSDimitry Andric std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
6693ca95b02SDimitry Andric MachineOperand &MO = MI.getOperand(OpNum);
670*b5893f02SDimitry Andric if (!LRI->PhysReg) {
671*b5893f02SDimitry Andric allocVirtReg(MI, *LRI, Hint);
672*b5893f02SDimitry Andric reload(MI, VirtReg, LRI->PhysReg);
673dff0c46cSDimitry Andric } else if (LRI->Dirty) {
674f22ef01cSRoman Divacky if (isLastUseOfLocalReg(MO)) {
675*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Killing last use: " << MO << '\n');
676ffd1746dSEd Schouten if (MO.isUse())
677f22ef01cSRoman Divacky MO.setIsKill();
678ffd1746dSEd Schouten else
679ffd1746dSEd Schouten MO.setIsDead();
680f22ef01cSRoman Divacky } else if (MO.isKill()) {
681*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << '\n');
682f22ef01cSRoman Divacky MO.setIsKill(false);
683ffd1746dSEd Schouten } else if (MO.isDead()) {
684*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << '\n');
685ffd1746dSEd Schouten MO.setIsDead(false);
686f22ef01cSRoman Divacky }
687f22ef01cSRoman Divacky } else if (MO.isKill()) {
688f22ef01cSRoman Divacky // We must remove kill flags from uses of reloaded registers because the
689f22ef01cSRoman Divacky // register would be killed immediately, and there might be a second use:
6902cab237bSDimitry Andric // %foo = OR killed %x, %x
691f22ef01cSRoman Divacky // This would cause a second reload of %x into a different register.
692*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << '\n');
693f22ef01cSRoman Divacky MO.setIsKill(false);
694ffd1746dSEd Schouten } else if (MO.isDead()) {
695*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << '\n');
696ffd1746dSEd Schouten MO.setIsDead(false);
697f22ef01cSRoman Divacky }
698dff0c46cSDimitry Andric assert(LRI->PhysReg && "Register not assigned");
6993ca95b02SDimitry Andric LRI->LastUse = &MI;
700dff0c46cSDimitry Andric LRI->LastOpNum = OpNum;
701139f7f9bSDimitry Andric markRegUsedInInstr(LRI->PhysReg);
702*b5893f02SDimitry Andric return *LRI;
703f22ef01cSRoman Divacky }
704f22ef01cSRoman Divacky
7052cab237bSDimitry Andric /// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
7062cab237bSDimitry Andric /// may invalidate any operand pointers. Return true if the operand kills its
7072cab237bSDimitry Andric /// register.
setPhysReg(MachineInstr & MI,MachineOperand & MO,MCPhysReg PhysReg)708*b5893f02SDimitry Andric bool RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
7092cab237bSDimitry Andric MCPhysReg PhysReg) {
7107ae0e2c9SDimitry Andric bool Dead = MO.isDead();
711f22ef01cSRoman Divacky if (!MO.getSubReg()) {
712f22ef01cSRoman Divacky MO.setReg(PhysReg);
7134ba319b5SDimitry Andric MO.setIsRenamable(true);
7147ae0e2c9SDimitry Andric return MO.isKill() || Dead;
715f22ef01cSRoman Divacky }
716f22ef01cSRoman Divacky
717f22ef01cSRoman Divacky // Handle subregister index.
718f22ef01cSRoman Divacky MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
7194ba319b5SDimitry Andric MO.setIsRenamable(true);
720f22ef01cSRoman Divacky MO.setSubReg(0);
721f22ef01cSRoman Divacky
722f22ef01cSRoman Divacky // A kill flag implies killing the full register. Add corresponding super
723f22ef01cSRoman Divacky // register kill.
724f22ef01cSRoman Divacky if (MO.isKill()) {
7252cab237bSDimitry Andric MI.addRegisterKilled(PhysReg, TRI, true);
726f22ef01cSRoman Divacky return true;
727f22ef01cSRoman Divacky }
7287ae0e2c9SDimitry Andric
7297ae0e2c9SDimitry Andric // A <def,read-undef> of a sub-register requires an implicit def of the full
7307ae0e2c9SDimitry Andric // register.
7317ae0e2c9SDimitry Andric if (MO.isDef() && MO.isUndef())
7322cab237bSDimitry Andric MI.addRegisterDefined(PhysReg, TRI);
7337ae0e2c9SDimitry Andric
7347ae0e2c9SDimitry Andric return Dead;
735f22ef01cSRoman Divacky }
736f22ef01cSRoman Divacky
7372cab237bSDimitry Andric // Handles special instruction operand like early clobbers and tied ops when
738ffd1746dSEd Schouten // there are additional physreg defines.
handleThroughOperands(MachineInstr & MI,SmallVectorImpl<unsigned> & VirtDead)7392cab237bSDimitry Andric void RegAllocFast::handleThroughOperands(MachineInstr &MI,
740ffd1746dSEd Schouten SmallVectorImpl<unsigned> &VirtDead) {
7414ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Scanning for through registers:");
742ffd1746dSEd Schouten SmallSet<unsigned, 8> ThroughRegs;
7432cab237bSDimitry Andric for (const MachineOperand &MO : MI.operands()) {
744ffd1746dSEd Schouten if (!MO.isReg()) continue;
745ffd1746dSEd Schouten unsigned Reg = MO.getReg();
7462754fe60SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(Reg))
7472754fe60SDimitry Andric continue;
7482cab237bSDimitry Andric if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) ||
7492cab237bSDimitry Andric (MO.getSubReg() && MI.readsVirtualRegister(Reg))) {
75039d628a0SDimitry Andric if (ThroughRegs.insert(Reg).second)
7514ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << ' ' << printReg(Reg));
752ffd1746dSEd Schouten }
753ffd1746dSEd Schouten }
754ffd1746dSEd Schouten
755ffd1746dSEd Schouten // If any physreg defines collide with preallocated through registers,
756ffd1746dSEd Schouten // we must spill and reallocate.
7574ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
7582cab237bSDimitry Andric for (const MachineOperand &MO : MI.operands()) {
759ffd1746dSEd Schouten if (!MO.isReg() || !MO.isDef()) continue;
760ffd1746dSEd Schouten unsigned Reg = MO.getReg();
761ffd1746dSEd Schouten if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
762139f7f9bSDimitry Andric markRegUsedInInstr(Reg);
7637ae0e2c9SDimitry Andric for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
7647ae0e2c9SDimitry Andric if (ThroughRegs.count(PhysRegState[*AI]))
7652cab237bSDimitry Andric definePhysReg(MI, *AI, regFree);
766ffd1746dSEd Schouten }
767ffd1746dSEd Schouten }
768ffd1746dSEd Schouten
769ffd1746dSEd Schouten SmallVector<unsigned, 8> PartialDefs;
7704ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Allocating tied uses.\n");
7712cab237bSDimitry Andric for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
772*b5893f02SDimitry Andric MachineOperand &MO = MI.getOperand(I);
773ffd1746dSEd Schouten if (!MO.isReg()) continue;
774ffd1746dSEd Schouten unsigned Reg = MO.getReg();
7752754fe60SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
776ffd1746dSEd Schouten if (MO.isUse()) {
7772cab237bSDimitry Andric if (!MO.isTied()) continue;
7784ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO
7794ba319b5SDimitry Andric << ") is tied to operand " << MI.findTiedOperandIdx(I)
7804ba319b5SDimitry Andric << ".\n");
781*b5893f02SDimitry Andric LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
782*b5893f02SDimitry Andric MCPhysReg PhysReg = LR.PhysReg;
783*b5893f02SDimitry Andric setPhysReg(MI, MO, PhysReg);
784ffd1746dSEd Schouten // Note: we don't update the def operand yet. That would cause the normal
785ffd1746dSEd Schouten // def-scan to attempt spilling.
7862cab237bSDimitry Andric } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) {
787*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << '\n');
788ffd1746dSEd Schouten // Reload the register, but don't assign to the operand just yet.
789ffd1746dSEd Schouten // That would confuse the later phys-def processing pass.
790*b5893f02SDimitry Andric LiveReg &LR = reloadVirtReg(MI, I, Reg, 0);
791*b5893f02SDimitry Andric PartialDefs.push_back(LR.PhysReg);
792dff0c46cSDimitry Andric }
793dff0c46cSDimitry Andric }
794dff0c46cSDimitry Andric
7954ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n");
7962cab237bSDimitry Andric for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
7972cab237bSDimitry Andric const MachineOperand &MO = MI.getOperand(I);
798dff0c46cSDimitry Andric if (!MO.isReg()) continue;
799dff0c46cSDimitry Andric unsigned Reg = MO.getReg();
800dff0c46cSDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
801dff0c46cSDimitry Andric if (!MO.isEarlyClobber())
802dff0c46cSDimitry Andric continue;
803ffd1746dSEd Schouten // Note: defineVirtReg may invalidate MO.
804*b5893f02SDimitry Andric MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, 0);
805*b5893f02SDimitry Andric if (setPhysReg(MI, MI.getOperand(I), PhysReg))
806ffd1746dSEd Schouten VirtDead.push_back(Reg);
807ffd1746dSEd Schouten }
808ffd1746dSEd Schouten
809ffd1746dSEd Schouten // Restore UsedInInstr to a state usable for allocating normal virtual uses.
8103861d79fSDimitry Andric UsedInInstr.clear();
8112cab237bSDimitry Andric for (const MachineOperand &MO : MI.operands()) {
812ffd1746dSEd Schouten if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
813ffd1746dSEd Schouten unsigned Reg = MO.getReg();
814ffd1746dSEd Schouten if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
8154ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI)
81617a519f9SDimitry Andric << " as used in instr\n");
817139f7f9bSDimitry Andric markRegUsedInInstr(Reg);
818ffd1746dSEd Schouten }
819ffd1746dSEd Schouten
820ffd1746dSEd Schouten // Also mark PartialDefs as used to avoid reallocation.
8212cab237bSDimitry Andric for (unsigned PartialDef : PartialDefs)
8222cab237bSDimitry Andric markRegUsedInInstr(PartialDef);
823dff0c46cSDimitry Andric }
824dff0c46cSDimitry Andric
8252cab237bSDimitry Andric #ifndef NDEBUG
dumpState()8262cab237bSDimitry Andric void RegAllocFast::dumpState() {
827f22ef01cSRoman Divacky for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
828f22ef01cSRoman Divacky if (PhysRegState[Reg] == regDisabled) continue;
8292cab237bSDimitry Andric dbgs() << " " << printReg(Reg, TRI);
830f22ef01cSRoman Divacky switch(PhysRegState[Reg]) {
831f22ef01cSRoman Divacky case regFree:
832f22ef01cSRoman Divacky break;
833f22ef01cSRoman Divacky case regReserved:
834f22ef01cSRoman Divacky dbgs() << "*";
835f22ef01cSRoman Divacky break;
836dff0c46cSDimitry Andric default: {
8372cab237bSDimitry Andric dbgs() << '=' << printReg(PhysRegState[Reg]);
838*b5893f02SDimitry Andric LiveRegMap::iterator LRI = findLiveVirtReg(PhysRegState[Reg]);
839*b5893f02SDimitry Andric assert(LRI != LiveVirtRegs.end() && LRI->PhysReg &&
840*b5893f02SDimitry Andric "Missing VirtReg entry");
841*b5893f02SDimitry Andric if (LRI->Dirty)
842f22ef01cSRoman Divacky dbgs() << "*";
843*b5893f02SDimitry Andric assert(LRI->PhysReg == Reg && "Bad inverse map");
844f22ef01cSRoman Divacky break;
845f22ef01cSRoman Divacky }
846f22ef01cSRoman Divacky }
847dff0c46cSDimitry Andric }
848f22ef01cSRoman Divacky dbgs() << '\n';
849f22ef01cSRoman Divacky // Check that LiveVirtRegs is the inverse.
850f22ef01cSRoman Divacky for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
851f22ef01cSRoman Divacky e = LiveVirtRegs.end(); i != e; ++i) {
852*b5893f02SDimitry Andric if (!i->PhysReg)
853*b5893f02SDimitry Andric continue;
854dff0c46cSDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
855f22ef01cSRoman Divacky "Bad map key");
856dff0c46cSDimitry Andric assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
857f22ef01cSRoman Divacky "Bad map value");
858dff0c46cSDimitry Andric assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
859f22ef01cSRoman Divacky }
8602cab237bSDimitry Andric }
8612cab237bSDimitry Andric #endif
8622cab237bSDimitry Andric
allocateInstruction(MachineInstr & MI)863*b5893f02SDimitry Andric void RegAllocFast::allocateInstruction(MachineInstr &MI) {
8642cab237bSDimitry Andric const MCInstrDesc &MCID = MI.getDesc();
8654ba319b5SDimitry Andric
866f22ef01cSRoman Divacky // If this is a copy, we may be able to coalesce.
8672cab237bSDimitry Andric unsigned CopySrcReg = 0;
8682cab237bSDimitry Andric unsigned CopyDstReg = 0;
8692cab237bSDimitry Andric unsigned CopySrcSub = 0;
8702cab237bSDimitry Andric unsigned CopyDstSub = 0;
8712cab237bSDimitry Andric if (MI.isCopy()) {
8722cab237bSDimitry Andric CopyDstReg = MI.getOperand(0).getReg();
8732cab237bSDimitry Andric CopySrcReg = MI.getOperand(1).getReg();
8742cab237bSDimitry Andric CopyDstSub = MI.getOperand(0).getSubReg();
8752cab237bSDimitry Andric CopySrcSub = MI.getOperand(1).getSubReg();
876e580952dSDimitry Andric }
877f22ef01cSRoman Divacky
878f22ef01cSRoman Divacky // Track registers used by instruction.
8793861d79fSDimitry Andric UsedInInstr.clear();
880f22ef01cSRoman Divacky
881f22ef01cSRoman Divacky // First scan.
882f22ef01cSRoman Divacky // Mark physreg uses and early clobbers as used.
883f22ef01cSRoman Divacky // Find the end of the virtreg operands
884f22ef01cSRoman Divacky unsigned VirtOpEnd = 0;
885ffd1746dSEd Schouten bool hasTiedOps = false;
886ffd1746dSEd Schouten bool hasEarlyClobbers = false;
887ffd1746dSEd Schouten bool hasPartialRedefs = false;
888ffd1746dSEd Schouten bool hasPhysDefs = false;
8892cab237bSDimitry Andric for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
8902cab237bSDimitry Andric MachineOperand &MO = MI.getOperand(i);
8913861d79fSDimitry Andric // Make sure MRI knows about registers clobbered by regmasks.
8923861d79fSDimitry Andric if (MO.isRegMask()) {
8933861d79fSDimitry Andric MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
8943861d79fSDimitry Andric continue;
8953861d79fSDimitry Andric }
896f22ef01cSRoman Divacky if (!MO.isReg()) continue;
897f22ef01cSRoman Divacky unsigned Reg = MO.getReg();
898f22ef01cSRoman Divacky if (!Reg) continue;
899f22ef01cSRoman Divacky if (TargetRegisterInfo::isVirtualRegister(Reg)) {
900f22ef01cSRoman Divacky VirtOpEnd = i+1;
901ffd1746dSEd Schouten if (MO.isUse()) {
902ffd1746dSEd Schouten hasTiedOps = hasTiedOps ||
90317a519f9SDimitry Andric MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
904ffd1746dSEd Schouten } else {
905ffd1746dSEd Schouten if (MO.isEarlyClobber())
906ffd1746dSEd Schouten hasEarlyClobbers = true;
9072cab237bSDimitry Andric if (MO.getSubReg() && MI.readsVirtualRegister(Reg))
908ffd1746dSEd Schouten hasPartialRedefs = true;
909ffd1746dSEd Schouten }
910f22ef01cSRoman Divacky continue;
911f22ef01cSRoman Divacky }
9123861d79fSDimitry Andric if (!MRI->isAllocatable(Reg)) continue;
913f22ef01cSRoman Divacky if (MO.isUse()) {
914f22ef01cSRoman Divacky usePhysReg(MO);
915f22ef01cSRoman Divacky } else if (MO.isEarlyClobber()) {
9162cab237bSDimitry Andric definePhysReg(MI, Reg,
9173ca95b02SDimitry Andric (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
918ffd1746dSEd Schouten hasEarlyClobbers = true;
919ffd1746dSEd Schouten } else
920ffd1746dSEd Schouten hasPhysDefs = true;
921f22ef01cSRoman Divacky }
922ffd1746dSEd Schouten
923ffd1746dSEd Schouten // The instruction may have virtual register operands that must be allocated
924ffd1746dSEd Schouten // the same register at use-time and def-time: early clobbers and tied
925ffd1746dSEd Schouten // operands. If there are also physical defs, these registers must avoid
926ffd1746dSEd Schouten // both physical defs and uses, making them more constrained than normal
927ffd1746dSEd Schouten // operands.
928e580952dSDimitry Andric // Similarly, if there are multiple defs and tied operands, we must make
929e580952dSDimitry Andric // sure the same register is allocated to uses and defs.
930ffd1746dSEd Schouten // We didn't detect inline asm tied operands above, so just make this extra
931ffd1746dSEd Schouten // pass for all inline asm.
9322cab237bSDimitry Andric if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
93317a519f9SDimitry Andric (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
934ffd1746dSEd Schouten handleThroughOperands(MI, VirtDead);
935ffd1746dSEd Schouten // Don't attempt coalescing when we have funny stuff going on.
9362cab237bSDimitry Andric CopyDstReg = 0;
937e580952dSDimitry Andric // Pretend we have early clobbers so the use operands get marked below.
938e580952dSDimitry Andric // This is not necessary for the common case of a single tied use.
939e580952dSDimitry Andric hasEarlyClobbers = true;
940f22ef01cSRoman Divacky }
941f22ef01cSRoman Divacky
942f22ef01cSRoman Divacky // Second scan.
943ffd1746dSEd Schouten // Allocate virtreg uses.
9442cab237bSDimitry Andric for (unsigned I = 0; I != VirtOpEnd; ++I) {
945*b5893f02SDimitry Andric MachineOperand &MO = MI.getOperand(I);
946f22ef01cSRoman Divacky if (!MO.isReg()) continue;
947f22ef01cSRoman Divacky unsigned Reg = MO.getReg();
9482754fe60SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
949f22ef01cSRoman Divacky if (MO.isUse()) {
950*b5893f02SDimitry Andric LiveReg &LR = reloadVirtReg(MI, I, Reg, CopyDstReg);
951*b5893f02SDimitry Andric MCPhysReg PhysReg = LR.PhysReg;
9522cab237bSDimitry Andric CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0;
953*b5893f02SDimitry Andric if (setPhysReg(MI, MO, PhysReg))
954*b5893f02SDimitry Andric killVirtReg(LR);
955f22ef01cSRoman Divacky }
956f22ef01cSRoman Divacky }
957f22ef01cSRoman Divacky
958e580952dSDimitry Andric // Track registers defined by instruction - early clobbers and tied uses at
959e580952dSDimitry Andric // this point.
9603861d79fSDimitry Andric UsedInInstr.clear();
961ffd1746dSEd Schouten if (hasEarlyClobbers) {
9622cab237bSDimitry Andric for (const MachineOperand &MO : MI.operands()) {
963e580952dSDimitry Andric if (!MO.isReg()) continue;
964ffd1746dSEd Schouten unsigned Reg = MO.getReg();
965ffd1746dSEd Schouten if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
966e580952dSDimitry Andric // Look for physreg defs and tied uses.
9672cab237bSDimitry Andric if (!MO.isDef() && !MO.isTied()) continue;
968139f7f9bSDimitry Andric markRegUsedInInstr(Reg);
969ffd1746dSEd Schouten }
970f22ef01cSRoman Divacky }
971f22ef01cSRoman Divacky
9722cab237bSDimitry Andric unsigned DefOpEnd = MI.getNumOperands();
9732cab237bSDimitry Andric if (MI.isCall()) {
97409a17a1eSDimitry Andric // Spill all virtregs before a call. This serves one purpose: If an
975e580952dSDimitry Andric // exception is thrown, the landing pad is going to expect to find
97609a17a1eSDimitry Andric // registers in their spill slots.
97709a17a1eSDimitry Andric // Note: although this is appealing to just consider all definitions
97809a17a1eSDimitry Andric // as call-clobbered, this is not correct because some of those
97909a17a1eSDimitry Andric // definitions may be used later on and we do not want to reuse
98009a17a1eSDimitry Andric // those for virtual registers in between.
9814ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n");
982f22ef01cSRoman Divacky spillAll(MI);
983f22ef01cSRoman Divacky }
984f22ef01cSRoman Divacky
985f22ef01cSRoman Divacky // Third scan.
986f22ef01cSRoman Divacky // Allocate defs and collect dead defs.
9872cab237bSDimitry Andric for (unsigned I = 0; I != DefOpEnd; ++I) {
9882cab237bSDimitry Andric const MachineOperand &MO = MI.getOperand(I);
989ffd1746dSEd Schouten if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
990ffd1746dSEd Schouten continue;
991f22ef01cSRoman Divacky unsigned Reg = MO.getReg();
992f22ef01cSRoman Divacky
993f22ef01cSRoman Divacky if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
9943861d79fSDimitry Andric if (!MRI->isAllocatable(Reg)) continue;
9952cab237bSDimitry Andric definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved);
996f22ef01cSRoman Divacky continue;
997f22ef01cSRoman Divacky }
998*b5893f02SDimitry Andric MCPhysReg PhysReg = defineVirtReg(MI, I, Reg, CopySrcReg);
999*b5893f02SDimitry Andric if (setPhysReg(MI, MI.getOperand(I), PhysReg)) {
1000f22ef01cSRoman Divacky VirtDead.push_back(Reg);
10012cab237bSDimitry Andric CopyDstReg = 0; // cancel coalescing;
1002f22ef01cSRoman Divacky } else
10032cab237bSDimitry Andric CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0;
1004f22ef01cSRoman Divacky }
1005f22ef01cSRoman Divacky
1006f22ef01cSRoman Divacky // Kill dead defs after the scan to ensure that multiple defs of the same
1007f22ef01cSRoman Divacky // register are allocated identically. We didn't need to do this for uses
1008f22ef01cSRoman Divacky // because we are crerating our own kill flags, and they are always at the
1009f22ef01cSRoman Divacky // last use.
10102cab237bSDimitry Andric for (unsigned VirtReg : VirtDead)
10112cab237bSDimitry Andric killVirtReg(VirtReg);
1012f22ef01cSRoman Divacky VirtDead.clear();
1013f22ef01cSRoman Divacky
10144ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "<< " << MI);
1015*b5893f02SDimitry Andric if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) {
1016*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1017*b5893f02SDimitry Andric Coalesced.push_back(&MI);
1018f22ef01cSRoman Divacky }
1019f22ef01cSRoman Divacky }
1020f22ef01cSRoman Divacky
handleDebugValue(MachineInstr & MI)1021*b5893f02SDimitry Andric void RegAllocFast::handleDebugValue(MachineInstr &MI) {
1022*b5893f02SDimitry Andric MachineOperand &MO = MI.getOperand(0);
1023*b5893f02SDimitry Andric
1024*b5893f02SDimitry Andric // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1025*b5893f02SDimitry Andric // mostly constants and frame indices.
1026*b5893f02SDimitry Andric if (!MO.isReg())
1027*b5893f02SDimitry Andric return;
1028*b5893f02SDimitry Andric unsigned Reg = MO.getReg();
1029*b5893f02SDimitry Andric if (!TargetRegisterInfo::isVirtualRegister(Reg))
1030*b5893f02SDimitry Andric return;
1031*b5893f02SDimitry Andric
1032*b5893f02SDimitry Andric // See if this virtual register has already been allocated to a physical
1033*b5893f02SDimitry Andric // register or spilled to a stack slot.
1034*b5893f02SDimitry Andric LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1035*b5893f02SDimitry Andric if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1036*b5893f02SDimitry Andric setPhysReg(MI, MO, LRI->PhysReg);
1037*b5893f02SDimitry Andric } else {
1038*b5893f02SDimitry Andric int SS = StackSlotForVirtReg[Reg];
1039*b5893f02SDimitry Andric if (SS != -1) {
1040*b5893f02SDimitry Andric // Modify DBG_VALUE now that the value is in a spill slot.
1041*b5893f02SDimitry Andric updateDbgValueForSpill(MI, SS);
1042*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:" << "\t" << MI);
1043*b5893f02SDimitry Andric return;
1044*b5893f02SDimitry Andric }
1045*b5893f02SDimitry Andric
1046*b5893f02SDimitry Andric // We can't allocate a physreg for a DebugValue, sorry!
1047*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
1048*b5893f02SDimitry Andric MO.setReg(0);
1049*b5893f02SDimitry Andric }
1050*b5893f02SDimitry Andric
1051*b5893f02SDimitry Andric // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1052*b5893f02SDimitry Andric // that future spills of Reg will have DBG_VALUEs.
1053*b5893f02SDimitry Andric LiveDbgValueMap[Reg].push_back(&MI);
1054*b5893f02SDimitry Andric }
1055*b5893f02SDimitry Andric
allocateBasicBlock(MachineBasicBlock & MBB)1056*b5893f02SDimitry Andric void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
1057*b5893f02SDimitry Andric this->MBB = &MBB;
1058*b5893f02SDimitry Andric LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
1059*b5893f02SDimitry Andric
1060*b5893f02SDimitry Andric PhysRegState.assign(TRI->getNumRegs(), regDisabled);
1061*b5893f02SDimitry Andric assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
1062*b5893f02SDimitry Andric
1063*b5893f02SDimitry Andric MachineBasicBlock::iterator MII = MBB.begin();
1064*b5893f02SDimitry Andric
1065*b5893f02SDimitry Andric // Add live-in registers as live.
1066*b5893f02SDimitry Andric for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins())
1067*b5893f02SDimitry Andric if (MRI->isAllocatable(LI.PhysReg))
1068*b5893f02SDimitry Andric definePhysReg(MII, LI.PhysReg, regReserved);
1069*b5893f02SDimitry Andric
1070*b5893f02SDimitry Andric VirtDead.clear();
1071*b5893f02SDimitry Andric Coalesced.clear();
1072*b5893f02SDimitry Andric
1073*b5893f02SDimitry Andric // Otherwise, sequentially allocate each instruction in the MBB.
1074*b5893f02SDimitry Andric for (MachineInstr &MI : MBB) {
1075*b5893f02SDimitry Andric LLVM_DEBUG(
1076*b5893f02SDimitry Andric dbgs() << "\n>> " << MI << "Regs:";
1077*b5893f02SDimitry Andric dumpState()
1078*b5893f02SDimitry Andric );
1079*b5893f02SDimitry Andric
1080*b5893f02SDimitry Andric // Special handling for debug values. Note that they are not allowed to
1081*b5893f02SDimitry Andric // affect codegen of the other instructions in any way.
1082*b5893f02SDimitry Andric if (MI.isDebugValue()) {
1083*b5893f02SDimitry Andric handleDebugValue(MI);
1084*b5893f02SDimitry Andric continue;
1085*b5893f02SDimitry Andric }
1086*b5893f02SDimitry Andric
1087*b5893f02SDimitry Andric allocateInstruction(MI);
1088*b5893f02SDimitry Andric }
1089*b5893f02SDimitry Andric
1090f22ef01cSRoman Divacky // Spill all physical registers holding virtual registers now.
10914ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n");
10922cab237bSDimitry Andric spillAll(MBB.getFirstTerminator());
1093f22ef01cSRoman Divacky
1094f22ef01cSRoman Divacky // Erase all the coalesced copies. We are delaying it until now because
1095f22ef01cSRoman Divacky // LiveVirtRegs might refer to the instrs.
10962cab237bSDimitry Andric for (MachineInstr *MI : Coalesced)
10972cab237bSDimitry Andric MBB.erase(MI);
1098*b5893f02SDimitry Andric NumCoalesced += Coalesced.size();
1099f22ef01cSRoman Divacky
11004ba319b5SDimitry Andric LLVM_DEBUG(MBB.dump());
1101f22ef01cSRoman Divacky }
1102f22ef01cSRoman Divacky
runOnMachineFunction(MachineFunction & MF)11032cab237bSDimitry Andric bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
11044ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
11052cab237bSDimitry Andric << "********** Function: " << MF.getName() << '\n');
11062cab237bSDimitry Andric MRI = &MF.getRegInfo();
11072cab237bSDimitry Andric const TargetSubtargetInfo &STI = MF.getSubtarget();
11082cab237bSDimitry Andric TRI = STI.getRegisterInfo();
11092cab237bSDimitry Andric TII = STI.getInstrInfo();
11102cab237bSDimitry Andric MFI = &MF.getFrameInfo();
11112cab237bSDimitry Andric MRI->freezeReservedRegs(MF);
11122cab237bSDimitry Andric RegClassInfo.runOnMachineFunction(MF);
11133861d79fSDimitry Andric UsedInInstr.clear();
1114139f7f9bSDimitry Andric UsedInInstr.setUniverse(TRI->getNumRegUnits());
1115f22ef01cSRoman Divacky
1116f22ef01cSRoman Divacky // initialize the virtual->physical register map to have a 'null'
1117f22ef01cSRoman Divacky // mapping for all virtual registers
11182cab237bSDimitry Andric unsigned NumVirtRegs = MRI->getNumVirtRegs();
11192cab237bSDimitry Andric StackSlotForVirtReg.resize(NumVirtRegs);
11202cab237bSDimitry Andric LiveVirtRegs.setUniverse(NumVirtRegs);
1121f22ef01cSRoman Divacky
1122f22ef01cSRoman Divacky // Loop over all of the basic blocks, eliminating virtual register references
11232cab237bSDimitry Andric for (MachineBasicBlock &MBB : MF)
11242cab237bSDimitry Andric allocateBasicBlock(MBB);
1125f22ef01cSRoman Divacky
1126dff0c46cSDimitry Andric // All machine operands and other references to virtual registers have been
1127dff0c46cSDimitry Andric // replaced. Remove the virtual registers.
1128dff0c46cSDimitry Andric MRI->clearVirtRegs();
1129dff0c46cSDimitry Andric
1130f22ef01cSRoman Divacky StackSlotForVirtReg.clear();
1131e580952dSDimitry Andric LiveDbgValueMap.clear();
1132f22ef01cSRoman Divacky return true;
1133f22ef01cSRoman Divacky }
1134f22ef01cSRoman Divacky
createFastRegisterAllocator()1135f22ef01cSRoman Divacky FunctionPass *llvm::createFastRegisterAllocator() {
11362cab237bSDimitry Andric return new RegAllocFast();
1137f22ef01cSRoman Divacky }
1138