xref: /aosp_15_r20/external/llvm/lib/CodeGen/RegAllocFast.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- RegAllocFast.cpp - A fast register allocator for debug code -------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This register allocator allocates registers to a basic block at a time,
11*9880d681SAndroid Build Coastguard Worker // attempting to keep values in registers and reusing registers as appropriate.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DenseMap.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/IndexedMap.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SparseSet.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegAllocRegistry.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/RegisterClassInfo.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
35*9880d681SAndroid Build Coastguard Worker #include <algorithm>
36*9880d681SAndroid Build Coastguard Worker using namespace llvm;
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "regalloc"
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker STATISTIC(NumStores, "Number of stores added");
41*9880d681SAndroid Build Coastguard Worker STATISTIC(NumLoads , "Number of loads added");
42*9880d681SAndroid Build Coastguard Worker STATISTIC(NumCopies, "Number of copies coalesced");
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker static RegisterRegAlloc
45*9880d681SAndroid Build Coastguard Worker   fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
46*9880d681SAndroid Build Coastguard Worker 
47*9880d681SAndroid Build Coastguard Worker namespace {
48*9880d681SAndroid Build Coastguard Worker   class RAFast : public MachineFunctionPass {
49*9880d681SAndroid Build Coastguard Worker   public:
50*9880d681SAndroid Build Coastguard Worker     static char ID;
RAFast()51*9880d681SAndroid Build Coastguard Worker     RAFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1),
52*9880d681SAndroid Build Coastguard Worker                isBulkSpilling(false) {}
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker   private:
55*9880d681SAndroid Build Coastguard Worker     MachineFunction *MF;
56*9880d681SAndroid Build Coastguard Worker     MachineRegisterInfo *MRI;
57*9880d681SAndroid Build Coastguard Worker     const TargetRegisterInfo *TRI;
58*9880d681SAndroid Build Coastguard Worker     const TargetInstrInfo *TII;
59*9880d681SAndroid Build Coastguard Worker     RegisterClassInfo RegClassInfo;
60*9880d681SAndroid Build Coastguard Worker 
61*9880d681SAndroid Build Coastguard Worker     // Basic block currently being allocated.
62*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *MBB;
63*9880d681SAndroid Build Coastguard Worker 
64*9880d681SAndroid Build Coastguard Worker     // StackSlotForVirtReg - Maps virtual regs to the frame index where these
65*9880d681SAndroid Build Coastguard Worker     // values are spilled.
66*9880d681SAndroid Build Coastguard Worker     IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker     // Everything we know about a live virtual register.
69*9880d681SAndroid Build Coastguard Worker     struct LiveReg {
70*9880d681SAndroid Build Coastguard Worker       MachineInstr *LastUse;    // Last instr to use reg.
71*9880d681SAndroid Build Coastguard Worker       unsigned VirtReg;         // Virtual register number.
72*9880d681SAndroid Build Coastguard Worker       unsigned PhysReg;         // Currently held here.
73*9880d681SAndroid Build Coastguard Worker       unsigned short LastOpNum; // OpNum on LastUse.
74*9880d681SAndroid Build Coastguard Worker       bool Dirty;               // Register needs spill.
75*9880d681SAndroid Build Coastguard Worker 
LiveReg__anon497b06830111::RAFast::LiveReg76*9880d681SAndroid Build Coastguard Worker       explicit LiveReg(unsigned v)
77*9880d681SAndroid Build Coastguard Worker         : LastUse(nullptr), VirtReg(v), PhysReg(0), LastOpNum(0), Dirty(false){}
78*9880d681SAndroid Build Coastguard Worker 
getSparseSetIndex__anon497b06830111::RAFast::LiveReg79*9880d681SAndroid Build Coastguard Worker       unsigned getSparseSetIndex() const {
80*9880d681SAndroid Build Coastguard Worker         return TargetRegisterInfo::virtReg2Index(VirtReg);
81*9880d681SAndroid Build Coastguard Worker       }
82*9880d681SAndroid Build Coastguard Worker     };
83*9880d681SAndroid Build Coastguard Worker 
84*9880d681SAndroid Build Coastguard Worker     typedef SparseSet<LiveReg> LiveRegMap;
85*9880d681SAndroid Build Coastguard Worker 
86*9880d681SAndroid Build Coastguard Worker     // LiveVirtRegs - This map contains entries for each virtual register
87*9880d681SAndroid Build Coastguard Worker     // that is currently available in a physical register.
88*9880d681SAndroid Build Coastguard Worker     LiveRegMap LiveVirtRegs;
89*9880d681SAndroid Build Coastguard Worker 
90*9880d681SAndroid Build Coastguard Worker     DenseMap<unsigned, SmallVector<MachineInstr *, 4> > LiveDbgValueMap;
91*9880d681SAndroid Build Coastguard Worker 
92*9880d681SAndroid Build Coastguard Worker     // RegState - Track the state of a physical register.
93*9880d681SAndroid Build Coastguard Worker     enum RegState {
94*9880d681SAndroid Build Coastguard Worker       // A disabled register is not available for allocation, but an alias may
95*9880d681SAndroid Build Coastguard Worker       // be in use. A register can only be moved out of the disabled state if
96*9880d681SAndroid Build Coastguard Worker       // all aliases are disabled.
97*9880d681SAndroid Build Coastguard Worker       regDisabled,
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker       // A free register is not currently in use and can be allocated
100*9880d681SAndroid Build Coastguard Worker       // immediately without checking aliases.
101*9880d681SAndroid Build Coastguard Worker       regFree,
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker       // A reserved register has been assigned explicitly (e.g., setting up a
104*9880d681SAndroid Build Coastguard Worker       // call parameter), and it remains reserved until it is used.
105*9880d681SAndroid Build Coastguard Worker       regReserved
106*9880d681SAndroid Build Coastguard Worker 
107*9880d681SAndroid Build Coastguard Worker       // A register state may also be a virtual register number, indication that
108*9880d681SAndroid Build Coastguard Worker       // the physical register is currently allocated to a virtual register. In
109*9880d681SAndroid Build Coastguard Worker       // that case, LiveVirtRegs contains the inverse mapping.
110*9880d681SAndroid Build Coastguard Worker     };
111*9880d681SAndroid Build Coastguard Worker 
112*9880d681SAndroid Build Coastguard Worker     // PhysRegState - One of the RegState enums, or a virtreg.
113*9880d681SAndroid Build Coastguard Worker     std::vector<unsigned> PhysRegState;
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker     // Set of register units.
116*9880d681SAndroid Build Coastguard Worker     typedef SparseSet<unsigned> UsedInInstrSet;
117*9880d681SAndroid Build Coastguard Worker 
118*9880d681SAndroid Build Coastguard Worker     // Set of register units that are used in the current instruction, and so
119*9880d681SAndroid Build Coastguard Worker     // cannot be allocated.
120*9880d681SAndroid Build Coastguard Worker     UsedInInstrSet UsedInInstr;
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker     // Mark a physreg as used in this instruction.
markRegUsedInInstr(unsigned PhysReg)123*9880d681SAndroid Build Coastguard Worker     void markRegUsedInInstr(unsigned PhysReg) {
124*9880d681SAndroid Build Coastguard Worker       for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
125*9880d681SAndroid Build Coastguard Worker         UsedInInstr.insert(*Units);
126*9880d681SAndroid Build Coastguard Worker     }
127*9880d681SAndroid Build Coastguard Worker 
128*9880d681SAndroid Build Coastguard Worker     // Check if a physreg or any of its aliases are used in this instruction.
isRegUsedInInstr(unsigned PhysReg) const129*9880d681SAndroid Build Coastguard Worker     bool isRegUsedInInstr(unsigned PhysReg) const {
130*9880d681SAndroid Build Coastguard Worker       for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
131*9880d681SAndroid Build Coastguard Worker         if (UsedInInstr.count(*Units))
132*9880d681SAndroid Build Coastguard Worker           return true;
133*9880d681SAndroid Build Coastguard Worker       return false;
134*9880d681SAndroid Build Coastguard Worker     }
135*9880d681SAndroid Build Coastguard Worker 
136*9880d681SAndroid Build Coastguard Worker     // SkippedInstrs - Descriptors of instructions whose clobber list was
137*9880d681SAndroid Build Coastguard Worker     // ignored because all registers were spilled. It is still necessary to
138*9880d681SAndroid Build Coastguard Worker     // mark all the clobbered registers as used by the function.
139*9880d681SAndroid Build Coastguard Worker     SmallPtrSet<const MCInstrDesc*, 4> SkippedInstrs;
140*9880d681SAndroid Build Coastguard Worker 
141*9880d681SAndroid Build Coastguard Worker     // isBulkSpilling - This flag is set when LiveRegMap will be cleared
142*9880d681SAndroid Build Coastguard Worker     // completely after spilling all live registers. LiveRegMap entries should
143*9880d681SAndroid Build Coastguard Worker     // not be erased.
144*9880d681SAndroid Build Coastguard Worker     bool isBulkSpilling;
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker     enum : unsigned {
147*9880d681SAndroid Build Coastguard Worker       spillClean = 1,
148*9880d681SAndroid Build Coastguard Worker       spillDirty = 100,
149*9880d681SAndroid Build Coastguard Worker       spillImpossible = ~0u
150*9880d681SAndroid Build Coastguard Worker     };
151*9880d681SAndroid Build Coastguard Worker   public:
getPassName() const152*9880d681SAndroid Build Coastguard Worker     const char *getPassName() const override {
153*9880d681SAndroid Build Coastguard Worker       return "Fast Register Allocator";
154*9880d681SAndroid Build Coastguard Worker     }
155*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage(AnalysisUsage & AU) const156*9880d681SAndroid Build Coastguard Worker     void getAnalysisUsage(AnalysisUsage &AU) const override {
157*9880d681SAndroid Build Coastguard Worker       AU.setPreservesCFG();
158*9880d681SAndroid Build Coastguard Worker       MachineFunctionPass::getAnalysisUsage(AU);
159*9880d681SAndroid Build Coastguard Worker     }
160*9880d681SAndroid Build Coastguard Worker 
getSetProperties() const161*9880d681SAndroid Build Coastguard Worker     MachineFunctionProperties getSetProperties() const override {
162*9880d681SAndroid Build Coastguard Worker       return MachineFunctionProperties().set(
163*9880d681SAndroid Build Coastguard Worker           MachineFunctionProperties::Property::AllVRegsAllocated);
164*9880d681SAndroid Build Coastguard Worker     }
165*9880d681SAndroid Build Coastguard Worker 
166*9880d681SAndroid Build Coastguard Worker   private:
167*9880d681SAndroid Build Coastguard Worker     bool runOnMachineFunction(MachineFunction &Fn) override;
168*9880d681SAndroid Build Coastguard Worker     void AllocateBasicBlock();
169*9880d681SAndroid Build Coastguard Worker     void handleThroughOperands(MachineInstr *MI,
170*9880d681SAndroid Build Coastguard Worker                                SmallVectorImpl<unsigned> &VirtDead);
171*9880d681SAndroid Build Coastguard Worker     int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
172*9880d681SAndroid Build Coastguard Worker     bool isLastUseOfLocalReg(MachineOperand&);
173*9880d681SAndroid Build Coastguard Worker 
174*9880d681SAndroid Build Coastguard Worker     void addKillFlag(const LiveReg&);
175*9880d681SAndroid Build Coastguard Worker     void killVirtReg(LiveRegMap::iterator);
176*9880d681SAndroid Build Coastguard Worker     void killVirtReg(unsigned VirtReg);
177*9880d681SAndroid Build Coastguard Worker     void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator);
178*9880d681SAndroid Build Coastguard Worker     void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg);
179*9880d681SAndroid Build Coastguard Worker 
180*9880d681SAndroid Build Coastguard Worker     void usePhysReg(MachineOperand&);
181*9880d681SAndroid Build Coastguard Worker     void definePhysReg(MachineInstr &MI, unsigned PhysReg, RegState NewState);
182*9880d681SAndroid Build Coastguard Worker     unsigned calcSpillCost(unsigned PhysReg) const;
183*9880d681SAndroid Build Coastguard Worker     void assignVirtToPhysReg(LiveReg&, unsigned PhysReg);
findLiveVirtReg(unsigned VirtReg)184*9880d681SAndroid Build Coastguard Worker     LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) {
185*9880d681SAndroid Build Coastguard Worker       return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
186*9880d681SAndroid Build Coastguard Worker     }
findLiveVirtReg(unsigned VirtReg) const187*9880d681SAndroid Build Coastguard Worker     LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const {
188*9880d681SAndroid Build Coastguard Worker       return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg));
189*9880d681SAndroid Build Coastguard Worker     }
190*9880d681SAndroid Build Coastguard Worker     LiveRegMap::iterator assignVirtToPhysReg(unsigned VReg, unsigned PhysReg);
191*9880d681SAndroid Build Coastguard Worker     LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator,
192*9880d681SAndroid Build Coastguard Worker                                       unsigned Hint);
193*9880d681SAndroid Build Coastguard Worker     LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum,
194*9880d681SAndroid Build Coastguard Worker                                        unsigned VirtReg, unsigned Hint);
195*9880d681SAndroid Build Coastguard Worker     LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum,
196*9880d681SAndroid Build Coastguard Worker                                        unsigned VirtReg, unsigned Hint);
197*9880d681SAndroid Build Coastguard Worker     void spillAll(MachineBasicBlock::iterator MI);
198*9880d681SAndroid Build Coastguard Worker     bool setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg);
199*9880d681SAndroid Build Coastguard Worker   };
200*9880d681SAndroid Build Coastguard Worker   char RAFast::ID = 0;
201*9880d681SAndroid Build Coastguard Worker }
202*9880d681SAndroid Build Coastguard Worker 
203*9880d681SAndroid Build Coastguard Worker /// getStackSpaceFor - This allocates space for the specified virtual register
204*9880d681SAndroid Build Coastguard Worker /// to be held on the stack.
getStackSpaceFor(unsigned VirtReg,const TargetRegisterClass * RC)205*9880d681SAndroid Build Coastguard Worker int RAFast::getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC) {
206*9880d681SAndroid Build Coastguard Worker   // Find the location Reg would belong...
207*9880d681SAndroid Build Coastguard Worker   int SS = StackSlotForVirtReg[VirtReg];
208*9880d681SAndroid Build Coastguard Worker   if (SS != -1)
209*9880d681SAndroid Build Coastguard Worker     return SS;          // Already has space allocated?
210*9880d681SAndroid Build Coastguard Worker 
211*9880d681SAndroid Build Coastguard Worker   // Allocate a new stack object for this spill location...
212*9880d681SAndroid Build Coastguard Worker   int FrameIdx = MF->getFrameInfo()->CreateSpillStackObject(RC->getSize(),
213*9880d681SAndroid Build Coastguard Worker                                                             RC->getAlignment());
214*9880d681SAndroid Build Coastguard Worker 
215*9880d681SAndroid Build Coastguard Worker   // Assign the slot.
216*9880d681SAndroid Build Coastguard Worker   StackSlotForVirtReg[VirtReg] = FrameIdx;
217*9880d681SAndroid Build Coastguard Worker   return FrameIdx;
218*9880d681SAndroid Build Coastguard Worker }
219*9880d681SAndroid Build Coastguard Worker 
220*9880d681SAndroid Build Coastguard Worker /// isLastUseOfLocalReg - Return true if MO is the only remaining reference to
221*9880d681SAndroid Build Coastguard Worker /// its virtual register, and it is guaranteed to be a block-local register.
222*9880d681SAndroid Build Coastguard Worker ///
isLastUseOfLocalReg(MachineOperand & MO)223*9880d681SAndroid Build Coastguard Worker bool RAFast::isLastUseOfLocalReg(MachineOperand &MO) {
224*9880d681SAndroid Build Coastguard Worker   // If the register has ever been spilled or reloaded, we conservatively assume
225*9880d681SAndroid Build Coastguard Worker   // it is a global register used in multiple blocks.
226*9880d681SAndroid Build Coastguard Worker   if (StackSlotForVirtReg[MO.getReg()] != -1)
227*9880d681SAndroid Build Coastguard Worker     return false;
228*9880d681SAndroid Build Coastguard Worker 
229*9880d681SAndroid Build Coastguard Worker   // Check that the use/def chain has exactly one operand - MO.
230*9880d681SAndroid Build Coastguard Worker   MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg());
231*9880d681SAndroid Build Coastguard Worker   if (&*I != &MO)
232*9880d681SAndroid Build Coastguard Worker     return false;
233*9880d681SAndroid Build Coastguard Worker   return ++I == MRI->reg_nodbg_end();
234*9880d681SAndroid Build Coastguard Worker }
235*9880d681SAndroid Build Coastguard Worker 
236*9880d681SAndroid Build Coastguard Worker /// addKillFlag - Set kill flags on last use of a virtual register.
addKillFlag(const LiveReg & LR)237*9880d681SAndroid Build Coastguard Worker void RAFast::addKillFlag(const LiveReg &LR) {
238*9880d681SAndroid Build Coastguard Worker   if (!LR.LastUse) return;
239*9880d681SAndroid Build Coastguard Worker   MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum);
240*9880d681SAndroid Build Coastguard Worker   if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) {
241*9880d681SAndroid Build Coastguard Worker     if (MO.getReg() == LR.PhysReg)
242*9880d681SAndroid Build Coastguard Worker       MO.setIsKill();
243*9880d681SAndroid Build Coastguard Worker     else
244*9880d681SAndroid Build Coastguard Worker       LR.LastUse->addRegisterKilled(LR.PhysReg, TRI, true);
245*9880d681SAndroid Build Coastguard Worker   }
246*9880d681SAndroid Build Coastguard Worker }
247*9880d681SAndroid Build Coastguard Worker 
248*9880d681SAndroid Build Coastguard Worker /// killVirtReg - Mark virtreg as no longer available.
killVirtReg(LiveRegMap::iterator LRI)249*9880d681SAndroid Build Coastguard Worker void RAFast::killVirtReg(LiveRegMap::iterator LRI) {
250*9880d681SAndroid Build Coastguard Worker   addKillFlag(*LRI);
251*9880d681SAndroid Build Coastguard Worker   assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg &&
252*9880d681SAndroid Build Coastguard Worker          "Broken RegState mapping");
253*9880d681SAndroid Build Coastguard Worker   PhysRegState[LRI->PhysReg] = regFree;
254*9880d681SAndroid Build Coastguard Worker   // Erase from LiveVirtRegs unless we're spilling in bulk.
255*9880d681SAndroid Build Coastguard Worker   if (!isBulkSpilling)
256*9880d681SAndroid Build Coastguard Worker     LiveVirtRegs.erase(LRI);
257*9880d681SAndroid Build Coastguard Worker }
258*9880d681SAndroid Build Coastguard Worker 
259*9880d681SAndroid Build Coastguard Worker /// killVirtReg - Mark virtreg as no longer available.
killVirtReg(unsigned VirtReg)260*9880d681SAndroid Build Coastguard Worker void RAFast::killVirtReg(unsigned VirtReg) {
261*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
262*9880d681SAndroid Build Coastguard Worker          "killVirtReg needs a virtual register");
263*9880d681SAndroid Build Coastguard Worker   LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
264*9880d681SAndroid Build Coastguard Worker   if (LRI != LiveVirtRegs.end())
265*9880d681SAndroid Build Coastguard Worker     killVirtReg(LRI);
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker /// spillVirtReg - This method spills the value specified by VirtReg into the
269*9880d681SAndroid Build Coastguard Worker /// corresponding stack slot if needed.
spillVirtReg(MachineBasicBlock::iterator MI,unsigned VirtReg)270*9880d681SAndroid Build Coastguard Worker void RAFast::spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg) {
271*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
272*9880d681SAndroid Build Coastguard Worker          "Spilling a physical register is illegal!");
273*9880d681SAndroid Build Coastguard Worker   LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
274*9880d681SAndroid Build Coastguard Worker   assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register");
275*9880d681SAndroid Build Coastguard Worker   spillVirtReg(MI, LRI);
276*9880d681SAndroid Build Coastguard Worker }
277*9880d681SAndroid Build Coastguard Worker 
278*9880d681SAndroid Build Coastguard Worker /// spillVirtReg - Do the actual work of spilling.
spillVirtReg(MachineBasicBlock::iterator MI,LiveRegMap::iterator LRI)279*9880d681SAndroid Build Coastguard Worker void RAFast::spillVirtReg(MachineBasicBlock::iterator MI,
280*9880d681SAndroid Build Coastguard Worker                           LiveRegMap::iterator LRI) {
281*9880d681SAndroid Build Coastguard Worker   LiveReg &LR = *LRI;
282*9880d681SAndroid Build Coastguard Worker   assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping");
283*9880d681SAndroid Build Coastguard Worker 
284*9880d681SAndroid Build Coastguard Worker   if (LR.Dirty) {
285*9880d681SAndroid Build Coastguard Worker     // If this physreg is used by the instruction, we want to kill it on the
286*9880d681SAndroid Build Coastguard Worker     // instruction, not on the spill.
287*9880d681SAndroid Build Coastguard Worker     bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI;
288*9880d681SAndroid Build Coastguard Worker     LR.Dirty = false;
289*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Spilling " << PrintReg(LRI->VirtReg, TRI)
290*9880d681SAndroid Build Coastguard Worker                  << " in " << PrintReg(LR.PhysReg, TRI));
291*9880d681SAndroid Build Coastguard Worker     const TargetRegisterClass *RC = MRI->getRegClass(LRI->VirtReg);
292*9880d681SAndroid Build Coastguard Worker     int FI = getStackSpaceFor(LRI->VirtReg, RC);
293*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << " to stack slot #" << FI << "\n");
294*9880d681SAndroid Build Coastguard Worker     TII->storeRegToStackSlot(*MBB, MI, LR.PhysReg, SpillKill, FI, RC, TRI);
295*9880d681SAndroid Build Coastguard Worker     ++NumStores;   // Update statistics
296*9880d681SAndroid Build Coastguard Worker 
297*9880d681SAndroid Build Coastguard Worker     // If this register is used by DBG_VALUE then insert new DBG_VALUE to
298*9880d681SAndroid Build Coastguard Worker     // identify spilled location as the place to find corresponding variable's
299*9880d681SAndroid Build Coastguard Worker     // value.
300*9880d681SAndroid Build Coastguard Worker     SmallVectorImpl<MachineInstr *> &LRIDbgValues =
301*9880d681SAndroid Build Coastguard Worker       LiveDbgValueMap[LRI->VirtReg];
302*9880d681SAndroid Build Coastguard Worker     for (unsigned li = 0, le = LRIDbgValues.size(); li != le; ++li) {
303*9880d681SAndroid Build Coastguard Worker       MachineInstr *DBG = LRIDbgValues[li];
304*9880d681SAndroid Build Coastguard Worker       const MDNode *Var = DBG->getDebugVariable();
305*9880d681SAndroid Build Coastguard Worker       const MDNode *Expr = DBG->getDebugExpression();
306*9880d681SAndroid Build Coastguard Worker       bool IsIndirect = DBG->isIndirectDebugValue();
307*9880d681SAndroid Build Coastguard Worker       uint64_t Offset = IsIndirect ? DBG->getOperand(1).getImm() : 0;
308*9880d681SAndroid Build Coastguard Worker       DebugLoc DL = DBG->getDebugLoc();
309*9880d681SAndroid Build Coastguard Worker       assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
310*9880d681SAndroid Build Coastguard Worker              "Expected inlined-at fields to agree");
311*9880d681SAndroid Build Coastguard Worker       MachineInstr *NewDV =
312*9880d681SAndroid Build Coastguard Worker           BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::DBG_VALUE))
313*9880d681SAndroid Build Coastguard Worker               .addFrameIndex(FI)
314*9880d681SAndroid Build Coastguard Worker               .addImm(Offset)
315*9880d681SAndroid Build Coastguard Worker               .addMetadata(Var)
316*9880d681SAndroid Build Coastguard Worker               .addMetadata(Expr);
317*9880d681SAndroid Build Coastguard Worker       assert(NewDV->getParent() == MBB && "dangling parent pointer");
318*9880d681SAndroid Build Coastguard Worker       (void)NewDV;
319*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Inserting debug info due to spill:" << "\n" << *NewDV);
320*9880d681SAndroid Build Coastguard Worker     }
321*9880d681SAndroid Build Coastguard Worker     // Now this register is spilled there is should not be any DBG_VALUE
322*9880d681SAndroid Build Coastguard Worker     // pointing to this register because they are all pointing to spilled value
323*9880d681SAndroid Build Coastguard Worker     // now.
324*9880d681SAndroid Build Coastguard Worker     LRIDbgValues.clear();
325*9880d681SAndroid Build Coastguard Worker     if (SpillKill)
326*9880d681SAndroid Build Coastguard Worker       LR.LastUse = nullptr; // Don't kill register again
327*9880d681SAndroid Build Coastguard Worker   }
328*9880d681SAndroid Build Coastguard Worker   killVirtReg(LRI);
329*9880d681SAndroid Build Coastguard Worker }
330*9880d681SAndroid Build Coastguard Worker 
331*9880d681SAndroid Build Coastguard Worker /// spillAll - Spill all dirty virtregs without killing them.
spillAll(MachineBasicBlock::iterator MI)332*9880d681SAndroid Build Coastguard Worker void RAFast::spillAll(MachineBasicBlock::iterator MI) {
333*9880d681SAndroid Build Coastguard Worker   if (LiveVirtRegs.empty()) return;
334*9880d681SAndroid Build Coastguard Worker   isBulkSpilling = true;
335*9880d681SAndroid Build Coastguard Worker   // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
336*9880d681SAndroid Build Coastguard Worker   // of spilling here is deterministic, if arbitrary.
337*9880d681SAndroid Build Coastguard Worker   for (LiveRegMap::iterator i = LiveVirtRegs.begin(), e = LiveVirtRegs.end();
338*9880d681SAndroid Build Coastguard Worker        i != e; ++i)
339*9880d681SAndroid Build Coastguard Worker     spillVirtReg(MI, i);
340*9880d681SAndroid Build Coastguard Worker   LiveVirtRegs.clear();
341*9880d681SAndroid Build Coastguard Worker   isBulkSpilling = false;
342*9880d681SAndroid Build Coastguard Worker }
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker /// usePhysReg - Handle the direct use of a physical register.
345*9880d681SAndroid Build Coastguard Worker /// Check that the register is not used by a virtreg.
346*9880d681SAndroid Build Coastguard Worker /// Kill the physreg, marking it free.
347*9880d681SAndroid Build Coastguard Worker /// This may add implicit kills to MO->getParent() and invalidate MO.
usePhysReg(MachineOperand & MO)348*9880d681SAndroid Build Coastguard Worker void RAFast::usePhysReg(MachineOperand &MO) {
349*9880d681SAndroid Build Coastguard Worker   unsigned PhysReg = MO.getReg();
350*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) &&
351*9880d681SAndroid Build Coastguard Worker          "Bad usePhysReg operand");
352*9880d681SAndroid Build Coastguard Worker 
353*9880d681SAndroid Build Coastguard Worker   // Ignore undef uses.
354*9880d681SAndroid Build Coastguard Worker   if (MO.isUndef())
355*9880d681SAndroid Build Coastguard Worker     return;
356*9880d681SAndroid Build Coastguard Worker 
357*9880d681SAndroid Build Coastguard Worker   markRegUsedInInstr(PhysReg);
358*9880d681SAndroid Build Coastguard Worker   switch (PhysRegState[PhysReg]) {
359*9880d681SAndroid Build Coastguard Worker   case regDisabled:
360*9880d681SAndroid Build Coastguard Worker     break;
361*9880d681SAndroid Build Coastguard Worker   case regReserved:
362*9880d681SAndroid Build Coastguard Worker     PhysRegState[PhysReg] = regFree;
363*9880d681SAndroid Build Coastguard Worker     // Fall through
364*9880d681SAndroid Build Coastguard Worker   case regFree:
365*9880d681SAndroid Build Coastguard Worker     MO.setIsKill();
366*9880d681SAndroid Build Coastguard Worker     return;
367*9880d681SAndroid Build Coastguard Worker   default:
368*9880d681SAndroid Build Coastguard Worker     // The physreg was allocated to a virtual register. That means the value we
369*9880d681SAndroid Build Coastguard Worker     // wanted has been clobbered.
370*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Instruction uses an allocated register");
371*9880d681SAndroid Build Coastguard Worker   }
372*9880d681SAndroid Build Coastguard Worker 
373*9880d681SAndroid Build Coastguard Worker   // Maybe a superregister is reserved?
374*9880d681SAndroid Build Coastguard Worker   for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
375*9880d681SAndroid Build Coastguard Worker     unsigned Alias = *AI;
376*9880d681SAndroid Build Coastguard Worker     switch (PhysRegState[Alias]) {
377*9880d681SAndroid Build Coastguard Worker     case regDisabled:
378*9880d681SAndroid Build Coastguard Worker       break;
379*9880d681SAndroid Build Coastguard Worker     case regReserved:
380*9880d681SAndroid Build Coastguard Worker       // Either PhysReg is a subregister of Alias and we mark the
381*9880d681SAndroid Build Coastguard Worker       // whole register as free, or PhysReg is the superregister of
382*9880d681SAndroid Build Coastguard Worker       // Alias and we mark all the aliases as disabled before freeing
383*9880d681SAndroid Build Coastguard Worker       // PhysReg.
384*9880d681SAndroid Build Coastguard Worker       // In the latter case, since PhysReg was disabled, this means that
385*9880d681SAndroid Build Coastguard Worker       // its value is defined only by physical sub-registers. This check
386*9880d681SAndroid Build Coastguard Worker       // is performed by the assert of the default case in this loop.
387*9880d681SAndroid Build Coastguard Worker       // Note: The value of the superregister may only be partial
388*9880d681SAndroid Build Coastguard Worker       // defined, that is why regDisabled is a valid state for aliases.
389*9880d681SAndroid Build Coastguard Worker       assert((TRI->isSuperRegister(PhysReg, Alias) ||
390*9880d681SAndroid Build Coastguard Worker               TRI->isSuperRegister(Alias, PhysReg)) &&
391*9880d681SAndroid Build Coastguard Worker              "Instruction is not using a subregister of a reserved register");
392*9880d681SAndroid Build Coastguard Worker       // Fall through.
393*9880d681SAndroid Build Coastguard Worker     case regFree:
394*9880d681SAndroid Build Coastguard Worker       if (TRI->isSuperRegister(PhysReg, Alias)) {
395*9880d681SAndroid Build Coastguard Worker         // Leave the superregister in the working set.
396*9880d681SAndroid Build Coastguard Worker         PhysRegState[Alias] = regFree;
397*9880d681SAndroid Build Coastguard Worker         MO.getParent()->addRegisterKilled(Alias, TRI, true);
398*9880d681SAndroid Build Coastguard Worker         return;
399*9880d681SAndroid Build Coastguard Worker       }
400*9880d681SAndroid Build Coastguard Worker       // Some other alias was in the working set - clear it.
401*9880d681SAndroid Build Coastguard Worker       PhysRegState[Alias] = regDisabled;
402*9880d681SAndroid Build Coastguard Worker       break;
403*9880d681SAndroid Build Coastguard Worker     default:
404*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("Instruction uses an alias of an allocated register");
405*9880d681SAndroid Build Coastguard Worker     }
406*9880d681SAndroid Build Coastguard Worker   }
407*9880d681SAndroid Build Coastguard Worker 
408*9880d681SAndroid Build Coastguard Worker   // All aliases are disabled, bring register into working set.
409*9880d681SAndroid Build Coastguard Worker   PhysRegState[PhysReg] = regFree;
410*9880d681SAndroid Build Coastguard Worker   MO.setIsKill();
411*9880d681SAndroid Build Coastguard Worker }
412*9880d681SAndroid Build Coastguard Worker 
413*9880d681SAndroid Build Coastguard Worker /// definePhysReg - Mark PhysReg as reserved or free after spilling any
414*9880d681SAndroid Build Coastguard Worker /// virtregs. This is very similar to defineVirtReg except the physreg is
415*9880d681SAndroid Build Coastguard Worker /// reserved instead of allocated.
definePhysReg(MachineInstr & MI,unsigned PhysReg,RegState NewState)416*9880d681SAndroid Build Coastguard Worker void RAFast::definePhysReg(MachineInstr &MI, unsigned PhysReg,
417*9880d681SAndroid Build Coastguard Worker                            RegState NewState) {
418*9880d681SAndroid Build Coastguard Worker   markRegUsedInInstr(PhysReg);
419*9880d681SAndroid Build Coastguard Worker   switch (unsigned VirtReg = PhysRegState[PhysReg]) {
420*9880d681SAndroid Build Coastguard Worker   case regDisabled:
421*9880d681SAndroid Build Coastguard Worker     break;
422*9880d681SAndroid Build Coastguard Worker   default:
423*9880d681SAndroid Build Coastguard Worker     spillVirtReg(MI, VirtReg);
424*9880d681SAndroid Build Coastguard Worker     // Fall through.
425*9880d681SAndroid Build Coastguard Worker   case regFree:
426*9880d681SAndroid Build Coastguard Worker   case regReserved:
427*9880d681SAndroid Build Coastguard Worker     PhysRegState[PhysReg] = NewState;
428*9880d681SAndroid Build Coastguard Worker     return;
429*9880d681SAndroid Build Coastguard Worker   }
430*9880d681SAndroid Build Coastguard Worker 
431*9880d681SAndroid Build Coastguard Worker   // This is a disabled register, disable all aliases.
432*9880d681SAndroid Build Coastguard Worker   PhysRegState[PhysReg] = NewState;
433*9880d681SAndroid Build Coastguard Worker   for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
434*9880d681SAndroid Build Coastguard Worker     unsigned Alias = *AI;
435*9880d681SAndroid Build Coastguard Worker     switch (unsigned VirtReg = PhysRegState[Alias]) {
436*9880d681SAndroid Build Coastguard Worker     case regDisabled:
437*9880d681SAndroid Build Coastguard Worker       break;
438*9880d681SAndroid Build Coastguard Worker     default:
439*9880d681SAndroid Build Coastguard Worker       spillVirtReg(MI, VirtReg);
440*9880d681SAndroid Build Coastguard Worker       // Fall through.
441*9880d681SAndroid Build Coastguard Worker     case regFree:
442*9880d681SAndroid Build Coastguard Worker     case regReserved:
443*9880d681SAndroid Build Coastguard Worker       PhysRegState[Alias] = regDisabled;
444*9880d681SAndroid Build Coastguard Worker       if (TRI->isSuperRegister(PhysReg, Alias))
445*9880d681SAndroid Build Coastguard Worker         return;
446*9880d681SAndroid Build Coastguard Worker       break;
447*9880d681SAndroid Build Coastguard Worker     }
448*9880d681SAndroid Build Coastguard Worker   }
449*9880d681SAndroid Build Coastguard Worker }
450*9880d681SAndroid Build Coastguard Worker 
451*9880d681SAndroid Build Coastguard Worker 
452*9880d681SAndroid Build Coastguard Worker // calcSpillCost - Return the cost of spilling clearing out PhysReg and
453*9880d681SAndroid Build Coastguard Worker // aliases so it is free for allocation.
454*9880d681SAndroid Build Coastguard Worker // Returns 0 when PhysReg is free or disabled with all aliases disabled - it
455*9880d681SAndroid Build Coastguard Worker // can be allocated directly.
456*9880d681SAndroid Build Coastguard Worker // Returns spillImpossible when PhysReg or an alias can't be spilled.
calcSpillCost(unsigned PhysReg) const457*9880d681SAndroid Build Coastguard Worker unsigned RAFast::calcSpillCost(unsigned PhysReg) const {
458*9880d681SAndroid Build Coastguard Worker   if (isRegUsedInInstr(PhysReg)) {
459*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is already used in instr.\n");
460*9880d681SAndroid Build Coastguard Worker     return spillImpossible;
461*9880d681SAndroid Build Coastguard Worker   }
462*9880d681SAndroid Build Coastguard Worker   switch (unsigned VirtReg = PhysRegState[PhysReg]) {
463*9880d681SAndroid Build Coastguard Worker   case regDisabled:
464*9880d681SAndroid Build Coastguard Worker     break;
465*9880d681SAndroid Build Coastguard Worker   case regFree:
466*9880d681SAndroid Build Coastguard Worker     return 0;
467*9880d681SAndroid Build Coastguard Worker   case regReserved:
468*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << PrintReg(VirtReg, TRI) << " corresponding "
469*9880d681SAndroid Build Coastguard Worker                  << PrintReg(PhysReg, TRI) << " is reserved already.\n");
470*9880d681SAndroid Build Coastguard Worker     return spillImpossible;
471*9880d681SAndroid Build Coastguard Worker   default: {
472*9880d681SAndroid Build Coastguard Worker     LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
473*9880d681SAndroid Build Coastguard Worker     assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
474*9880d681SAndroid Build Coastguard Worker     return I->Dirty ? spillDirty : spillClean;
475*9880d681SAndroid Build Coastguard Worker   }
476*9880d681SAndroid Build Coastguard Worker   }
477*9880d681SAndroid Build Coastguard Worker 
478*9880d681SAndroid Build Coastguard Worker   // This is a disabled register, add up cost of aliases.
479*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is disabled.\n");
480*9880d681SAndroid Build Coastguard Worker   unsigned Cost = 0;
481*9880d681SAndroid Build Coastguard Worker   for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) {
482*9880d681SAndroid Build Coastguard Worker     unsigned Alias = *AI;
483*9880d681SAndroid Build Coastguard Worker     switch (unsigned VirtReg = PhysRegState[Alias]) {
484*9880d681SAndroid Build Coastguard Worker     case regDisabled:
485*9880d681SAndroid Build Coastguard Worker       break;
486*9880d681SAndroid Build Coastguard Worker     case regFree:
487*9880d681SAndroid Build Coastguard Worker       ++Cost;
488*9880d681SAndroid Build Coastguard Worker       break;
489*9880d681SAndroid Build Coastguard Worker     case regReserved:
490*9880d681SAndroid Build Coastguard Worker       return spillImpossible;
491*9880d681SAndroid Build Coastguard Worker     default: {
492*9880d681SAndroid Build Coastguard Worker       LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
493*9880d681SAndroid Build Coastguard Worker       assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
494*9880d681SAndroid Build Coastguard Worker       Cost += I->Dirty ? spillDirty : spillClean;
495*9880d681SAndroid Build Coastguard Worker       break;
496*9880d681SAndroid Build Coastguard Worker     }
497*9880d681SAndroid Build Coastguard Worker     }
498*9880d681SAndroid Build Coastguard Worker   }
499*9880d681SAndroid Build Coastguard Worker   return Cost;
500*9880d681SAndroid Build Coastguard Worker }
501*9880d681SAndroid Build Coastguard Worker 
502*9880d681SAndroid Build Coastguard Worker 
503*9880d681SAndroid Build Coastguard Worker /// assignVirtToPhysReg - This method updates local state so that we know
504*9880d681SAndroid Build Coastguard Worker /// that PhysReg is the proper container for VirtReg now.  The physical
505*9880d681SAndroid Build Coastguard Worker /// register must not be used for anything else when this is called.
506*9880d681SAndroid Build Coastguard Worker ///
assignVirtToPhysReg(LiveReg & LR,unsigned PhysReg)507*9880d681SAndroid Build Coastguard Worker void RAFast::assignVirtToPhysReg(LiveReg &LR, unsigned PhysReg) {
508*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Assigning " << PrintReg(LR.VirtReg, TRI) << " to "
509*9880d681SAndroid Build Coastguard Worker                << PrintReg(PhysReg, TRI) << "\n");
510*9880d681SAndroid Build Coastguard Worker   PhysRegState[PhysReg] = LR.VirtReg;
511*9880d681SAndroid Build Coastguard Worker   assert(!LR.PhysReg && "Already assigned a physreg");
512*9880d681SAndroid Build Coastguard Worker   LR.PhysReg = PhysReg;
513*9880d681SAndroid Build Coastguard Worker }
514*9880d681SAndroid Build Coastguard Worker 
515*9880d681SAndroid Build Coastguard Worker RAFast::LiveRegMap::iterator
assignVirtToPhysReg(unsigned VirtReg,unsigned PhysReg)516*9880d681SAndroid Build Coastguard Worker RAFast::assignVirtToPhysReg(unsigned VirtReg, unsigned PhysReg) {
517*9880d681SAndroid Build Coastguard Worker   LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
518*9880d681SAndroid Build Coastguard Worker   assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared");
519*9880d681SAndroid Build Coastguard Worker   assignVirtToPhysReg(*LRI, PhysReg);
520*9880d681SAndroid Build Coastguard Worker   return LRI;
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker 
523*9880d681SAndroid Build Coastguard Worker /// allocVirtReg - Allocate a physical register for VirtReg.
allocVirtReg(MachineInstr & MI,LiveRegMap::iterator LRI,unsigned Hint)524*9880d681SAndroid Build Coastguard Worker RAFast::LiveRegMap::iterator RAFast::allocVirtReg(MachineInstr &MI,
525*9880d681SAndroid Build Coastguard Worker                                                   LiveRegMap::iterator LRI,
526*9880d681SAndroid Build Coastguard Worker                                                   unsigned Hint) {
527*9880d681SAndroid Build Coastguard Worker   const unsigned VirtReg = LRI->VirtReg;
528*9880d681SAndroid Build Coastguard Worker 
529*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
530*9880d681SAndroid Build Coastguard Worker          "Can only allocate virtual registers");
531*9880d681SAndroid Build Coastguard Worker 
532*9880d681SAndroid Build Coastguard Worker   const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
533*9880d681SAndroid Build Coastguard Worker 
534*9880d681SAndroid Build Coastguard Worker   // Ignore invalid hints.
535*9880d681SAndroid Build Coastguard Worker   if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
536*9880d681SAndroid Build Coastguard Worker                !RC->contains(Hint) || !MRI->isAllocatable(Hint)))
537*9880d681SAndroid Build Coastguard Worker     Hint = 0;
538*9880d681SAndroid Build Coastguard Worker 
539*9880d681SAndroid Build Coastguard Worker   // Take hint when possible.
540*9880d681SAndroid Build Coastguard Worker   if (Hint) {
541*9880d681SAndroid Build Coastguard Worker     // Ignore the hint if we would have to spill a dirty register.
542*9880d681SAndroid Build Coastguard Worker     unsigned Cost = calcSpillCost(Hint);
543*9880d681SAndroid Build Coastguard Worker     if (Cost < spillDirty) {
544*9880d681SAndroid Build Coastguard Worker       if (Cost)
545*9880d681SAndroid Build Coastguard Worker         definePhysReg(MI, Hint, regFree);
546*9880d681SAndroid Build Coastguard Worker       // definePhysReg may kill virtual registers and modify LiveVirtRegs.
547*9880d681SAndroid Build Coastguard Worker       // That invalidates LRI, so run a new lookup for VirtReg.
548*9880d681SAndroid Build Coastguard Worker       return assignVirtToPhysReg(VirtReg, Hint);
549*9880d681SAndroid Build Coastguard Worker     }
550*9880d681SAndroid Build Coastguard Worker   }
551*9880d681SAndroid Build Coastguard Worker 
552*9880d681SAndroid Build Coastguard Worker   ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(RC);
553*9880d681SAndroid Build Coastguard Worker 
554*9880d681SAndroid Build Coastguard Worker   // First try to find a completely free register.
555*9880d681SAndroid Build Coastguard Worker   for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
556*9880d681SAndroid Build Coastguard Worker     unsigned PhysReg = *I;
557*9880d681SAndroid Build Coastguard Worker     if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) {
558*9880d681SAndroid Build Coastguard Worker       assignVirtToPhysReg(*LRI, PhysReg);
559*9880d681SAndroid Build Coastguard Worker       return LRI;
560*9880d681SAndroid Build Coastguard Worker     }
561*9880d681SAndroid Build Coastguard Worker   }
562*9880d681SAndroid Build Coastguard Worker 
563*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Allocating " << PrintReg(VirtReg) << " from "
564*9880d681SAndroid Build Coastguard Worker                << TRI->getRegClassName(RC) << "\n");
565*9880d681SAndroid Build Coastguard Worker 
566*9880d681SAndroid Build Coastguard Worker   unsigned BestReg = 0, BestCost = spillImpossible;
567*9880d681SAndroid Build Coastguard Worker   for (ArrayRef<MCPhysReg>::iterator I = AO.begin(), E = AO.end(); I != E; ++I){
568*9880d681SAndroid Build Coastguard Worker     unsigned Cost = calcSpillCost(*I);
569*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "\tRegister: " << PrintReg(*I, TRI) << "\n");
570*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "\tCost: " << Cost << "\n");
571*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n");
572*9880d681SAndroid Build Coastguard Worker     // Cost is 0 when all aliases are already disabled.
573*9880d681SAndroid Build Coastguard Worker     if (Cost == 0) {
574*9880d681SAndroid Build Coastguard Worker       assignVirtToPhysReg(*LRI, *I);
575*9880d681SAndroid Build Coastguard Worker       return LRI;
576*9880d681SAndroid Build Coastguard Worker     }
577*9880d681SAndroid Build Coastguard Worker     if (Cost < BestCost)
578*9880d681SAndroid Build Coastguard Worker       BestReg = *I, BestCost = Cost;
579*9880d681SAndroid Build Coastguard Worker   }
580*9880d681SAndroid Build Coastguard Worker 
581*9880d681SAndroid Build Coastguard Worker   if (BestReg) {
582*9880d681SAndroid Build Coastguard Worker     definePhysReg(MI, BestReg, regFree);
583*9880d681SAndroid Build Coastguard Worker     // definePhysReg may kill virtual registers and modify LiveVirtRegs.
584*9880d681SAndroid Build Coastguard Worker     // That invalidates LRI, so run a new lookup for VirtReg.
585*9880d681SAndroid Build Coastguard Worker     return assignVirtToPhysReg(VirtReg, BestReg);
586*9880d681SAndroid Build Coastguard Worker   }
587*9880d681SAndroid Build Coastguard Worker 
588*9880d681SAndroid Build Coastguard Worker   // Nothing we can do. Report an error and keep going with a bad allocation.
589*9880d681SAndroid Build Coastguard Worker   if (MI.isInlineAsm())
590*9880d681SAndroid Build Coastguard Worker     MI.emitError("inline assembly requires more registers than available");
591*9880d681SAndroid Build Coastguard Worker   else
592*9880d681SAndroid Build Coastguard Worker     MI.emitError("ran out of registers during register allocation");
593*9880d681SAndroid Build Coastguard Worker   definePhysReg(MI, *AO.begin(), regFree);
594*9880d681SAndroid Build Coastguard Worker   return assignVirtToPhysReg(VirtReg, *AO.begin());
595*9880d681SAndroid Build Coastguard Worker }
596*9880d681SAndroid Build Coastguard Worker 
597*9880d681SAndroid Build Coastguard Worker /// defineVirtReg - Allocate a register for VirtReg and mark it as dirty.
defineVirtReg(MachineInstr & MI,unsigned OpNum,unsigned VirtReg,unsigned Hint)598*9880d681SAndroid Build Coastguard Worker RAFast::LiveRegMap::iterator RAFast::defineVirtReg(MachineInstr &MI,
599*9880d681SAndroid Build Coastguard Worker                                                    unsigned OpNum,
600*9880d681SAndroid Build Coastguard Worker                                                    unsigned VirtReg,
601*9880d681SAndroid Build Coastguard Worker                                                    unsigned Hint) {
602*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
603*9880d681SAndroid Build Coastguard Worker          "Not a virtual register");
604*9880d681SAndroid Build Coastguard Worker   LiveRegMap::iterator LRI;
605*9880d681SAndroid Build Coastguard Worker   bool New;
606*9880d681SAndroid Build Coastguard Worker   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
607*9880d681SAndroid Build Coastguard Worker   if (New) {
608*9880d681SAndroid Build Coastguard Worker     // If there is no hint, peek at the only use of this register.
609*9880d681SAndroid Build Coastguard Worker     if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) &&
610*9880d681SAndroid Build Coastguard Worker         MRI->hasOneNonDBGUse(VirtReg)) {
611*9880d681SAndroid Build Coastguard Worker       const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg);
612*9880d681SAndroid Build Coastguard Worker       // It's a copy, use the destination register as a hint.
613*9880d681SAndroid Build Coastguard Worker       if (UseMI.isCopyLike())
614*9880d681SAndroid Build Coastguard Worker         Hint = UseMI.getOperand(0).getReg();
615*9880d681SAndroid Build Coastguard Worker     }
616*9880d681SAndroid Build Coastguard Worker     LRI = allocVirtReg(MI, LRI, Hint);
617*9880d681SAndroid Build Coastguard Worker   } else if (LRI->LastUse) {
618*9880d681SAndroid Build Coastguard Worker     // Redefining a live register - kill at the last use, unless it is this
619*9880d681SAndroid Build Coastguard Worker     // instruction defining VirtReg multiple times.
620*9880d681SAndroid Build Coastguard Worker     if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse())
621*9880d681SAndroid Build Coastguard Worker       addKillFlag(*LRI);
622*9880d681SAndroid Build Coastguard Worker   }
623*9880d681SAndroid Build Coastguard Worker   assert(LRI->PhysReg && "Register not assigned");
624*9880d681SAndroid Build Coastguard Worker   LRI->LastUse = &MI;
625*9880d681SAndroid Build Coastguard Worker   LRI->LastOpNum = OpNum;
626*9880d681SAndroid Build Coastguard Worker   LRI->Dirty = true;
627*9880d681SAndroid Build Coastguard Worker   markRegUsedInInstr(LRI->PhysReg);
628*9880d681SAndroid Build Coastguard Worker   return LRI;
629*9880d681SAndroid Build Coastguard Worker }
630*9880d681SAndroid Build Coastguard Worker 
631*9880d681SAndroid Build Coastguard Worker /// reloadVirtReg - Make sure VirtReg is available in a physreg and return it.
reloadVirtReg(MachineInstr & MI,unsigned OpNum,unsigned VirtReg,unsigned Hint)632*9880d681SAndroid Build Coastguard Worker RAFast::LiveRegMap::iterator RAFast::reloadVirtReg(MachineInstr &MI,
633*9880d681SAndroid Build Coastguard Worker                                                    unsigned OpNum,
634*9880d681SAndroid Build Coastguard Worker                                                    unsigned VirtReg,
635*9880d681SAndroid Build Coastguard Worker                                                    unsigned Hint) {
636*9880d681SAndroid Build Coastguard Worker   assert(TargetRegisterInfo::isVirtualRegister(VirtReg) &&
637*9880d681SAndroid Build Coastguard Worker          "Not a virtual register");
638*9880d681SAndroid Build Coastguard Worker   LiveRegMap::iterator LRI;
639*9880d681SAndroid Build Coastguard Worker   bool New;
640*9880d681SAndroid Build Coastguard Worker   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
641*9880d681SAndroid Build Coastguard Worker   MachineOperand &MO = MI.getOperand(OpNum);
642*9880d681SAndroid Build Coastguard Worker   if (New) {
643*9880d681SAndroid Build Coastguard Worker     LRI = allocVirtReg(MI, LRI, Hint);
644*9880d681SAndroid Build Coastguard Worker     const TargetRegisterClass *RC = MRI->getRegClass(VirtReg);
645*9880d681SAndroid Build Coastguard Worker     int FrameIndex = getStackSpaceFor(VirtReg, RC);
646*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Reloading " << PrintReg(VirtReg, TRI) << " into "
647*9880d681SAndroid Build Coastguard Worker                  << PrintReg(LRI->PhysReg, TRI) << "\n");
648*9880d681SAndroid Build Coastguard Worker     TII->loadRegFromStackSlot(*MBB, MI, LRI->PhysReg, FrameIndex, RC, TRI);
649*9880d681SAndroid Build Coastguard Worker     ++NumLoads;
650*9880d681SAndroid Build Coastguard Worker   } else if (LRI->Dirty) {
651*9880d681SAndroid Build Coastguard Worker     if (isLastUseOfLocalReg(MO)) {
652*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Killing last use: " << MO << "\n");
653*9880d681SAndroid Build Coastguard Worker       if (MO.isUse())
654*9880d681SAndroid Build Coastguard Worker         MO.setIsKill();
655*9880d681SAndroid Build Coastguard Worker       else
656*9880d681SAndroid Build Coastguard Worker         MO.setIsDead();
657*9880d681SAndroid Build Coastguard Worker     } else if (MO.isKill()) {
658*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n");
659*9880d681SAndroid Build Coastguard Worker       MO.setIsKill(false);
660*9880d681SAndroid Build Coastguard Worker     } else if (MO.isDead()) {
661*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n");
662*9880d681SAndroid Build Coastguard Worker       MO.setIsDead(false);
663*9880d681SAndroid Build Coastguard Worker     }
664*9880d681SAndroid Build Coastguard Worker   } else if (MO.isKill()) {
665*9880d681SAndroid Build Coastguard Worker     // We must remove kill flags from uses of reloaded registers because the
666*9880d681SAndroid Build Coastguard Worker     // register would be killed immediately, and there might be a second use:
667*9880d681SAndroid Build Coastguard Worker     //   %foo = OR %x<kill>, %x
668*9880d681SAndroid Build Coastguard Worker     // This would cause a second reload of %x into a different register.
669*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n");
670*9880d681SAndroid Build Coastguard Worker     MO.setIsKill(false);
671*9880d681SAndroid Build Coastguard Worker   } else if (MO.isDead()) {
672*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n");
673*9880d681SAndroid Build Coastguard Worker     MO.setIsDead(false);
674*9880d681SAndroid Build Coastguard Worker   }
675*9880d681SAndroid Build Coastguard Worker   assert(LRI->PhysReg && "Register not assigned");
676*9880d681SAndroid Build Coastguard Worker   LRI->LastUse = &MI;
677*9880d681SAndroid Build Coastguard Worker   LRI->LastOpNum = OpNum;
678*9880d681SAndroid Build Coastguard Worker   markRegUsedInInstr(LRI->PhysReg);
679*9880d681SAndroid Build Coastguard Worker   return LRI;
680*9880d681SAndroid Build Coastguard Worker }
681*9880d681SAndroid Build Coastguard Worker 
682*9880d681SAndroid Build Coastguard Worker // setPhysReg - Change operand OpNum in MI the refer the PhysReg, considering
683*9880d681SAndroid Build Coastguard Worker // subregs. This may invalidate any operand pointers.
684*9880d681SAndroid Build Coastguard Worker // Return true if the operand kills its register.
setPhysReg(MachineInstr * MI,unsigned OpNum,unsigned PhysReg)685*9880d681SAndroid Build Coastguard Worker bool RAFast::setPhysReg(MachineInstr *MI, unsigned OpNum, unsigned PhysReg) {
686*9880d681SAndroid Build Coastguard Worker   MachineOperand &MO = MI->getOperand(OpNum);
687*9880d681SAndroid Build Coastguard Worker   bool Dead = MO.isDead();
688*9880d681SAndroid Build Coastguard Worker   if (!MO.getSubReg()) {
689*9880d681SAndroid Build Coastguard Worker     MO.setReg(PhysReg);
690*9880d681SAndroid Build Coastguard Worker     return MO.isKill() || Dead;
691*9880d681SAndroid Build Coastguard Worker   }
692*9880d681SAndroid Build Coastguard Worker 
693*9880d681SAndroid Build Coastguard Worker   // Handle subregister index.
694*9880d681SAndroid Build Coastguard Worker   MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0);
695*9880d681SAndroid Build Coastguard Worker   MO.setSubReg(0);
696*9880d681SAndroid Build Coastguard Worker 
697*9880d681SAndroid Build Coastguard Worker   // A kill flag implies killing the full register. Add corresponding super
698*9880d681SAndroid Build Coastguard Worker   // register kill.
699*9880d681SAndroid Build Coastguard Worker   if (MO.isKill()) {
700*9880d681SAndroid Build Coastguard Worker     MI->addRegisterKilled(PhysReg, TRI, true);
701*9880d681SAndroid Build Coastguard Worker     return true;
702*9880d681SAndroid Build Coastguard Worker   }
703*9880d681SAndroid Build Coastguard Worker 
704*9880d681SAndroid Build Coastguard Worker   // A <def,read-undef> of a sub-register requires an implicit def of the full
705*9880d681SAndroid Build Coastguard Worker   // register.
706*9880d681SAndroid Build Coastguard Worker   if (MO.isDef() && MO.isUndef())
707*9880d681SAndroid Build Coastguard Worker     MI->addRegisterDefined(PhysReg, TRI);
708*9880d681SAndroid Build Coastguard Worker 
709*9880d681SAndroid Build Coastguard Worker   return Dead;
710*9880d681SAndroid Build Coastguard Worker }
711*9880d681SAndroid Build Coastguard Worker 
712*9880d681SAndroid Build Coastguard Worker // Handle special instruction operand like early clobbers and tied ops when
713*9880d681SAndroid Build Coastguard Worker // there are additional physreg defines.
handleThroughOperands(MachineInstr * MI,SmallVectorImpl<unsigned> & VirtDead)714*9880d681SAndroid Build Coastguard Worker void RAFast::handleThroughOperands(MachineInstr *MI,
715*9880d681SAndroid Build Coastguard Worker                                    SmallVectorImpl<unsigned> &VirtDead) {
716*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Scanning for through registers:");
717*9880d681SAndroid Build Coastguard Worker   SmallSet<unsigned, 8> ThroughRegs;
718*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
719*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI->getOperand(i);
720*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg()) continue;
721*9880d681SAndroid Build Coastguard Worker     unsigned Reg = MO.getReg();
722*9880d681SAndroid Build Coastguard Worker     if (!TargetRegisterInfo::isVirtualRegister(Reg))
723*9880d681SAndroid Build Coastguard Worker       continue;
724*9880d681SAndroid Build Coastguard Worker     if (MO.isEarlyClobber() || MI->isRegTiedToDefOperand(i) ||
725*9880d681SAndroid Build Coastguard Worker         (MO.getSubReg() && MI->readsVirtualRegister(Reg))) {
726*9880d681SAndroid Build Coastguard Worker       if (ThroughRegs.insert(Reg).second)
727*9880d681SAndroid Build Coastguard Worker         DEBUG(dbgs() << ' ' << PrintReg(Reg));
728*9880d681SAndroid Build Coastguard Worker     }
729*9880d681SAndroid Build Coastguard Worker   }
730*9880d681SAndroid Build Coastguard Worker 
731*9880d681SAndroid Build Coastguard Worker   // If any physreg defines collide with preallocated through registers,
732*9880d681SAndroid Build Coastguard Worker   // we must spill and reallocate.
733*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "\nChecking for physdef collisions.\n");
734*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
735*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI->getOperand(i);
736*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg() || !MO.isDef()) continue;
737*9880d681SAndroid Build Coastguard Worker     unsigned Reg = MO.getReg();
738*9880d681SAndroid Build Coastguard Worker     if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
739*9880d681SAndroid Build Coastguard Worker     markRegUsedInInstr(Reg);
740*9880d681SAndroid Build Coastguard Worker     for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
741*9880d681SAndroid Build Coastguard Worker       if (ThroughRegs.count(PhysRegState[*AI]))
742*9880d681SAndroid Build Coastguard Worker         definePhysReg(*MI, *AI, regFree);
743*9880d681SAndroid Build Coastguard Worker     }
744*9880d681SAndroid Build Coastguard Worker   }
745*9880d681SAndroid Build Coastguard Worker 
746*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 8> PartialDefs;
747*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Allocating tied uses.\n");
748*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
749*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI->getOperand(i);
750*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg()) continue;
751*9880d681SAndroid Build Coastguard Worker     unsigned Reg = MO.getReg();
752*9880d681SAndroid Build Coastguard Worker     if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
753*9880d681SAndroid Build Coastguard Worker     if (MO.isUse()) {
754*9880d681SAndroid Build Coastguard Worker       unsigned DefIdx = 0;
755*9880d681SAndroid Build Coastguard Worker       if (!MI->isRegTiedToDefOperand(i, &DefIdx)) continue;
756*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Operand " << i << "("<< MO << ") is tied to operand "
757*9880d681SAndroid Build Coastguard Worker         << DefIdx << ".\n");
758*9880d681SAndroid Build Coastguard Worker       LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
759*9880d681SAndroid Build Coastguard Worker       unsigned PhysReg = LRI->PhysReg;
760*9880d681SAndroid Build Coastguard Worker       setPhysReg(MI, i, PhysReg);
761*9880d681SAndroid Build Coastguard Worker       // Note: we don't update the def operand yet. That would cause the normal
762*9880d681SAndroid Build Coastguard Worker       // def-scan to attempt spilling.
763*9880d681SAndroid Build Coastguard Worker     } else if (MO.getSubReg() && MI->readsVirtualRegister(Reg)) {
764*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "Partial redefine: " << MO << "\n");
765*9880d681SAndroid Build Coastguard Worker       // Reload the register, but don't assign to the operand just yet.
766*9880d681SAndroid Build Coastguard Worker       // That would confuse the later phys-def processing pass.
767*9880d681SAndroid Build Coastguard Worker       LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, 0);
768*9880d681SAndroid Build Coastguard Worker       PartialDefs.push_back(LRI->PhysReg);
769*9880d681SAndroid Build Coastguard Worker     }
770*9880d681SAndroid Build Coastguard Worker   }
771*9880d681SAndroid Build Coastguard Worker 
772*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Allocating early clobbers.\n");
773*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
774*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI->getOperand(i);
775*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg()) continue;
776*9880d681SAndroid Build Coastguard Worker     unsigned Reg = MO.getReg();
777*9880d681SAndroid Build Coastguard Worker     if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
778*9880d681SAndroid Build Coastguard Worker     if (!MO.isEarlyClobber())
779*9880d681SAndroid Build Coastguard Worker       continue;
780*9880d681SAndroid Build Coastguard Worker     // Note: defineVirtReg may invalidate MO.
781*9880d681SAndroid Build Coastguard Worker     LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, 0);
782*9880d681SAndroid Build Coastguard Worker     unsigned PhysReg = LRI->PhysReg;
783*9880d681SAndroid Build Coastguard Worker     if (setPhysReg(MI, i, PhysReg))
784*9880d681SAndroid Build Coastguard Worker       VirtDead.push_back(Reg);
785*9880d681SAndroid Build Coastguard Worker   }
786*9880d681SAndroid Build Coastguard Worker 
787*9880d681SAndroid Build Coastguard Worker   // Restore UsedInInstr to a state usable for allocating normal virtual uses.
788*9880d681SAndroid Build Coastguard Worker   UsedInInstr.clear();
789*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
790*9880d681SAndroid Build Coastguard Worker     MachineOperand &MO = MI->getOperand(i);
791*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue;
792*9880d681SAndroid Build Coastguard Worker     unsigned Reg = MO.getReg();
793*9880d681SAndroid Build Coastguard Worker     if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
794*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "\tSetting " << PrintReg(Reg, TRI)
795*9880d681SAndroid Build Coastguard Worker                  << " as used in instr\n");
796*9880d681SAndroid Build Coastguard Worker     markRegUsedInInstr(Reg);
797*9880d681SAndroid Build Coastguard Worker   }
798*9880d681SAndroid Build Coastguard Worker 
799*9880d681SAndroid Build Coastguard Worker   // Also mark PartialDefs as used to avoid reallocation.
800*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = PartialDefs.size(); i != e; ++i)
801*9880d681SAndroid Build Coastguard Worker     markRegUsedInInstr(PartialDefs[i]);
802*9880d681SAndroid Build Coastguard Worker }
803*9880d681SAndroid Build Coastguard Worker 
AllocateBasicBlock()804*9880d681SAndroid Build Coastguard Worker void RAFast::AllocateBasicBlock() {
805*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "\nAllocating " << *MBB);
806*9880d681SAndroid Build Coastguard Worker 
807*9880d681SAndroid Build Coastguard Worker   PhysRegState.assign(TRI->getNumRegs(), regDisabled);
808*9880d681SAndroid Build Coastguard Worker   assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
809*9880d681SAndroid Build Coastguard Worker 
810*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock::iterator MII = MBB->begin();
811*9880d681SAndroid Build Coastguard Worker 
812*9880d681SAndroid Build Coastguard Worker   // Add live-in registers as live.
813*9880d681SAndroid Build Coastguard Worker   for (const auto &LI : MBB->liveins())
814*9880d681SAndroid Build Coastguard Worker     if (MRI->isAllocatable(LI.PhysReg))
815*9880d681SAndroid Build Coastguard Worker       definePhysReg(*MII, LI.PhysReg, regReserved);
816*9880d681SAndroid Build Coastguard Worker 
817*9880d681SAndroid Build Coastguard Worker   SmallVector<unsigned, 8> VirtDead;
818*9880d681SAndroid Build Coastguard Worker   SmallVector<MachineInstr*, 32> Coalesced;
819*9880d681SAndroid Build Coastguard Worker 
820*9880d681SAndroid Build Coastguard Worker   // Otherwise, sequentially allocate each instruction in the MBB.
821*9880d681SAndroid Build Coastguard Worker   while (MII != MBB->end()) {
822*9880d681SAndroid Build Coastguard Worker     MachineInstr *MI = &*MII++;
823*9880d681SAndroid Build Coastguard Worker     const MCInstrDesc &MCID = MI->getDesc();
824*9880d681SAndroid Build Coastguard Worker     DEBUG({
825*9880d681SAndroid Build Coastguard Worker         dbgs() << "\n>> " << *MI << "Regs:";
826*9880d681SAndroid Build Coastguard Worker         for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) {
827*9880d681SAndroid Build Coastguard Worker           if (PhysRegState[Reg] == regDisabled) continue;
828*9880d681SAndroid Build Coastguard Worker           dbgs() << " " << TRI->getName(Reg);
829*9880d681SAndroid Build Coastguard Worker           switch(PhysRegState[Reg]) {
830*9880d681SAndroid Build Coastguard Worker           case regFree:
831*9880d681SAndroid Build Coastguard Worker             break;
832*9880d681SAndroid Build Coastguard Worker           case regReserved:
833*9880d681SAndroid Build Coastguard Worker             dbgs() << "*";
834*9880d681SAndroid Build Coastguard Worker             break;
835*9880d681SAndroid Build Coastguard Worker           default: {
836*9880d681SAndroid Build Coastguard Worker             dbgs() << '=' << PrintReg(PhysRegState[Reg]);
837*9880d681SAndroid Build Coastguard Worker             LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]);
838*9880d681SAndroid Build Coastguard Worker             assert(I != LiveVirtRegs.end() && "Missing VirtReg entry");
839*9880d681SAndroid Build Coastguard Worker             if (I->Dirty)
840*9880d681SAndroid Build Coastguard Worker               dbgs() << "*";
841*9880d681SAndroid Build Coastguard Worker             assert(I->PhysReg == Reg && "Bad inverse map");
842*9880d681SAndroid Build Coastguard Worker             break;
843*9880d681SAndroid Build Coastguard Worker           }
844*9880d681SAndroid Build Coastguard Worker           }
845*9880d681SAndroid Build Coastguard Worker         }
846*9880d681SAndroid Build Coastguard Worker         dbgs() << '\n';
847*9880d681SAndroid Build Coastguard Worker         // Check that LiveVirtRegs is the inverse.
848*9880d681SAndroid Build Coastguard Worker         for (LiveRegMap::iterator i = LiveVirtRegs.begin(),
849*9880d681SAndroid Build Coastguard Worker              e = LiveVirtRegs.end(); i != e; ++i) {
850*9880d681SAndroid Build Coastguard Worker            assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) &&
851*9880d681SAndroid Build Coastguard Worker                   "Bad map key");
852*9880d681SAndroid Build Coastguard Worker            assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) &&
853*9880d681SAndroid Build Coastguard Worker                   "Bad map value");
854*9880d681SAndroid Build Coastguard Worker            assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map");
855*9880d681SAndroid Build Coastguard Worker         }
856*9880d681SAndroid Build Coastguard Worker       });
857*9880d681SAndroid Build Coastguard Worker 
858*9880d681SAndroid Build Coastguard Worker     // Debug values are not allowed to change codegen in any way.
859*9880d681SAndroid Build Coastguard Worker     if (MI->isDebugValue()) {
860*9880d681SAndroid Build Coastguard Worker       bool ScanDbgValue = true;
861*9880d681SAndroid Build Coastguard Worker       while (ScanDbgValue) {
862*9880d681SAndroid Build Coastguard Worker         ScanDbgValue = false;
863*9880d681SAndroid Build Coastguard Worker         for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
864*9880d681SAndroid Build Coastguard Worker           MachineOperand &MO = MI->getOperand(i);
865*9880d681SAndroid Build Coastguard Worker           if (!MO.isReg()) continue;
866*9880d681SAndroid Build Coastguard Worker           unsigned Reg = MO.getReg();
867*9880d681SAndroid Build Coastguard Worker           if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
868*9880d681SAndroid Build Coastguard Worker           LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
869*9880d681SAndroid Build Coastguard Worker           if (LRI != LiveVirtRegs.end())
870*9880d681SAndroid Build Coastguard Worker             setPhysReg(MI, i, LRI->PhysReg);
871*9880d681SAndroid Build Coastguard Worker           else {
872*9880d681SAndroid Build Coastguard Worker             int SS = StackSlotForVirtReg[Reg];
873*9880d681SAndroid Build Coastguard Worker             if (SS == -1) {
874*9880d681SAndroid Build Coastguard Worker               // We can't allocate a physreg for a DebugValue, sorry!
875*9880d681SAndroid Build Coastguard Worker               DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE");
876*9880d681SAndroid Build Coastguard Worker               MO.setReg(0);
877*9880d681SAndroid Build Coastguard Worker             }
878*9880d681SAndroid Build Coastguard Worker             else {
879*9880d681SAndroid Build Coastguard Worker               // Modify DBG_VALUE now that the value is in a spill slot.
880*9880d681SAndroid Build Coastguard Worker               bool IsIndirect = MI->isIndirectDebugValue();
881*9880d681SAndroid Build Coastguard Worker               uint64_t Offset = IsIndirect ? MI->getOperand(1).getImm() : 0;
882*9880d681SAndroid Build Coastguard Worker               const MDNode *Var = MI->getDebugVariable();
883*9880d681SAndroid Build Coastguard Worker               const MDNode *Expr = MI->getDebugExpression();
884*9880d681SAndroid Build Coastguard Worker               DebugLoc DL = MI->getDebugLoc();
885*9880d681SAndroid Build Coastguard Worker               MachineBasicBlock *MBB = MI->getParent();
886*9880d681SAndroid Build Coastguard Worker               assert(
887*9880d681SAndroid Build Coastguard Worker                   cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
888*9880d681SAndroid Build Coastguard Worker                   "Expected inlined-at fields to agree");
889*9880d681SAndroid Build Coastguard Worker               MachineInstr *NewDV = BuildMI(*MBB, MBB->erase(MI), DL,
890*9880d681SAndroid Build Coastguard Worker                                             TII->get(TargetOpcode::DBG_VALUE))
891*9880d681SAndroid Build Coastguard Worker                                         .addFrameIndex(SS)
892*9880d681SAndroid Build Coastguard Worker                                         .addImm(Offset)
893*9880d681SAndroid Build Coastguard Worker                                         .addMetadata(Var)
894*9880d681SAndroid Build Coastguard Worker                                         .addMetadata(Expr);
895*9880d681SAndroid Build Coastguard Worker               DEBUG(dbgs() << "Modifying debug info due to spill:"
896*9880d681SAndroid Build Coastguard Worker                            << "\t" << *NewDV);
897*9880d681SAndroid Build Coastguard Worker               // Scan NewDV operands from the beginning.
898*9880d681SAndroid Build Coastguard Worker               MI = NewDV;
899*9880d681SAndroid Build Coastguard Worker               ScanDbgValue = true;
900*9880d681SAndroid Build Coastguard Worker               break;
901*9880d681SAndroid Build Coastguard Worker             }
902*9880d681SAndroid Build Coastguard Worker           }
903*9880d681SAndroid Build Coastguard Worker           LiveDbgValueMap[Reg].push_back(MI);
904*9880d681SAndroid Build Coastguard Worker         }
905*9880d681SAndroid Build Coastguard Worker       }
906*9880d681SAndroid Build Coastguard Worker       // Next instruction.
907*9880d681SAndroid Build Coastguard Worker       continue;
908*9880d681SAndroid Build Coastguard Worker     }
909*9880d681SAndroid Build Coastguard Worker 
910*9880d681SAndroid Build Coastguard Worker     // If this is a copy, we may be able to coalesce.
911*9880d681SAndroid Build Coastguard Worker     unsigned CopySrc = 0, CopyDst = 0, CopySrcSub = 0, CopyDstSub = 0;
912*9880d681SAndroid Build Coastguard Worker     if (MI->isCopy()) {
913*9880d681SAndroid Build Coastguard Worker       CopyDst = MI->getOperand(0).getReg();
914*9880d681SAndroid Build Coastguard Worker       CopySrc = MI->getOperand(1).getReg();
915*9880d681SAndroid Build Coastguard Worker       CopyDstSub = MI->getOperand(0).getSubReg();
916*9880d681SAndroid Build Coastguard Worker       CopySrcSub = MI->getOperand(1).getSubReg();
917*9880d681SAndroid Build Coastguard Worker     }
918*9880d681SAndroid Build Coastguard Worker 
919*9880d681SAndroid Build Coastguard Worker     // Track registers used by instruction.
920*9880d681SAndroid Build Coastguard Worker     UsedInInstr.clear();
921*9880d681SAndroid Build Coastguard Worker 
922*9880d681SAndroid Build Coastguard Worker     // First scan.
923*9880d681SAndroid Build Coastguard Worker     // Mark physreg uses and early clobbers as used.
924*9880d681SAndroid Build Coastguard Worker     // Find the end of the virtreg operands
925*9880d681SAndroid Build Coastguard Worker     unsigned VirtOpEnd = 0;
926*9880d681SAndroid Build Coastguard Worker     bool hasTiedOps = false;
927*9880d681SAndroid Build Coastguard Worker     bool hasEarlyClobbers = false;
928*9880d681SAndroid Build Coastguard Worker     bool hasPartialRedefs = false;
929*9880d681SAndroid Build Coastguard Worker     bool hasPhysDefs = false;
930*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
931*9880d681SAndroid Build Coastguard Worker       MachineOperand &MO = MI->getOperand(i);
932*9880d681SAndroid Build Coastguard Worker       // Make sure MRI knows about registers clobbered by regmasks.
933*9880d681SAndroid Build Coastguard Worker       if (MO.isRegMask()) {
934*9880d681SAndroid Build Coastguard Worker         MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
935*9880d681SAndroid Build Coastguard Worker         continue;
936*9880d681SAndroid Build Coastguard Worker       }
937*9880d681SAndroid Build Coastguard Worker       if (!MO.isReg()) continue;
938*9880d681SAndroid Build Coastguard Worker       unsigned Reg = MO.getReg();
939*9880d681SAndroid Build Coastguard Worker       if (!Reg) continue;
940*9880d681SAndroid Build Coastguard Worker       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
941*9880d681SAndroid Build Coastguard Worker         VirtOpEnd = i+1;
942*9880d681SAndroid Build Coastguard Worker         if (MO.isUse()) {
943*9880d681SAndroid Build Coastguard Worker           hasTiedOps = hasTiedOps ||
944*9880d681SAndroid Build Coastguard Worker                               MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1;
945*9880d681SAndroid Build Coastguard Worker         } else {
946*9880d681SAndroid Build Coastguard Worker           if (MO.isEarlyClobber())
947*9880d681SAndroid Build Coastguard Worker             hasEarlyClobbers = true;
948*9880d681SAndroid Build Coastguard Worker           if (MO.getSubReg() && MI->readsVirtualRegister(Reg))
949*9880d681SAndroid Build Coastguard Worker             hasPartialRedefs = true;
950*9880d681SAndroid Build Coastguard Worker         }
951*9880d681SAndroid Build Coastguard Worker         continue;
952*9880d681SAndroid Build Coastguard Worker       }
953*9880d681SAndroid Build Coastguard Worker       if (!MRI->isAllocatable(Reg)) continue;
954*9880d681SAndroid Build Coastguard Worker       if (MO.isUse()) {
955*9880d681SAndroid Build Coastguard Worker         usePhysReg(MO);
956*9880d681SAndroid Build Coastguard Worker       } else if (MO.isEarlyClobber()) {
957*9880d681SAndroid Build Coastguard Worker         definePhysReg(*MI, Reg,
958*9880d681SAndroid Build Coastguard Worker                       (MO.isImplicit() || MO.isDead()) ? regFree : regReserved);
959*9880d681SAndroid Build Coastguard Worker         hasEarlyClobbers = true;
960*9880d681SAndroid Build Coastguard Worker       } else
961*9880d681SAndroid Build Coastguard Worker         hasPhysDefs = true;
962*9880d681SAndroid Build Coastguard Worker     }
963*9880d681SAndroid Build Coastguard Worker 
964*9880d681SAndroid Build Coastguard Worker     // The instruction may have virtual register operands that must be allocated
965*9880d681SAndroid Build Coastguard Worker     // the same register at use-time and def-time: early clobbers and tied
966*9880d681SAndroid Build Coastguard Worker     // operands. If there are also physical defs, these registers must avoid
967*9880d681SAndroid Build Coastguard Worker     // both physical defs and uses, making them more constrained than normal
968*9880d681SAndroid Build Coastguard Worker     // operands.
969*9880d681SAndroid Build Coastguard Worker     // Similarly, if there are multiple defs and tied operands, we must make
970*9880d681SAndroid Build Coastguard Worker     // sure the same register is allocated to uses and defs.
971*9880d681SAndroid Build Coastguard Worker     // We didn't detect inline asm tied operands above, so just make this extra
972*9880d681SAndroid Build Coastguard Worker     // pass for all inline asm.
973*9880d681SAndroid Build Coastguard Worker     if (MI->isInlineAsm() || hasEarlyClobbers || hasPartialRedefs ||
974*9880d681SAndroid Build Coastguard Worker         (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) {
975*9880d681SAndroid Build Coastguard Worker       handleThroughOperands(MI, VirtDead);
976*9880d681SAndroid Build Coastguard Worker       // Don't attempt coalescing when we have funny stuff going on.
977*9880d681SAndroid Build Coastguard Worker       CopyDst = 0;
978*9880d681SAndroid Build Coastguard Worker       // Pretend we have early clobbers so the use operands get marked below.
979*9880d681SAndroid Build Coastguard Worker       // This is not necessary for the common case of a single tied use.
980*9880d681SAndroid Build Coastguard Worker       hasEarlyClobbers = true;
981*9880d681SAndroid Build Coastguard Worker     }
982*9880d681SAndroid Build Coastguard Worker 
983*9880d681SAndroid Build Coastguard Worker     // Second scan.
984*9880d681SAndroid Build Coastguard Worker     // Allocate virtreg uses.
985*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i != VirtOpEnd; ++i) {
986*9880d681SAndroid Build Coastguard Worker       MachineOperand &MO = MI->getOperand(i);
987*9880d681SAndroid Build Coastguard Worker       if (!MO.isReg()) continue;
988*9880d681SAndroid Build Coastguard Worker       unsigned Reg = MO.getReg();
989*9880d681SAndroid Build Coastguard Worker       if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue;
990*9880d681SAndroid Build Coastguard Worker       if (MO.isUse()) {
991*9880d681SAndroid Build Coastguard Worker         LiveRegMap::iterator LRI = reloadVirtReg(*MI, i, Reg, CopyDst);
992*9880d681SAndroid Build Coastguard Worker         unsigned PhysReg = LRI->PhysReg;
993*9880d681SAndroid Build Coastguard Worker         CopySrc = (CopySrc == Reg || CopySrc == PhysReg) ? PhysReg : 0;
994*9880d681SAndroid Build Coastguard Worker         if (setPhysReg(MI, i, PhysReg))
995*9880d681SAndroid Build Coastguard Worker           killVirtReg(LRI);
996*9880d681SAndroid Build Coastguard Worker       }
997*9880d681SAndroid Build Coastguard Worker     }
998*9880d681SAndroid Build Coastguard Worker 
999*9880d681SAndroid Build Coastguard Worker     // Track registers defined by instruction - early clobbers and tied uses at
1000*9880d681SAndroid Build Coastguard Worker     // this point.
1001*9880d681SAndroid Build Coastguard Worker     UsedInInstr.clear();
1002*9880d681SAndroid Build Coastguard Worker     if (hasEarlyClobbers) {
1003*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1004*9880d681SAndroid Build Coastguard Worker         MachineOperand &MO = MI->getOperand(i);
1005*9880d681SAndroid Build Coastguard Worker         if (!MO.isReg()) continue;
1006*9880d681SAndroid Build Coastguard Worker         unsigned Reg = MO.getReg();
1007*9880d681SAndroid Build Coastguard Worker         if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue;
1008*9880d681SAndroid Build Coastguard Worker         // Look for physreg defs and tied uses.
1009*9880d681SAndroid Build Coastguard Worker         if (!MO.isDef() && !MI->isRegTiedToDefOperand(i)) continue;
1010*9880d681SAndroid Build Coastguard Worker         markRegUsedInInstr(Reg);
1011*9880d681SAndroid Build Coastguard Worker       }
1012*9880d681SAndroid Build Coastguard Worker     }
1013*9880d681SAndroid Build Coastguard Worker 
1014*9880d681SAndroid Build Coastguard Worker     unsigned DefOpEnd = MI->getNumOperands();
1015*9880d681SAndroid Build Coastguard Worker     if (MI->isCall()) {
1016*9880d681SAndroid Build Coastguard Worker       // Spill all virtregs before a call. This serves one purpose: If an
1017*9880d681SAndroid Build Coastguard Worker       // exception is thrown, the landing pad is going to expect to find
1018*9880d681SAndroid Build Coastguard Worker       // registers in their spill slots.
1019*9880d681SAndroid Build Coastguard Worker       // Note: although this is appealing to just consider all definitions
1020*9880d681SAndroid Build Coastguard Worker       // as call-clobbered, this is not correct because some of those
1021*9880d681SAndroid Build Coastguard Worker       // definitions may be used later on and we do not want to reuse
1022*9880d681SAndroid Build Coastguard Worker       // those for virtual registers in between.
1023*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "  Spilling remaining registers before call.\n");
1024*9880d681SAndroid Build Coastguard Worker       spillAll(MI);
1025*9880d681SAndroid Build Coastguard Worker 
1026*9880d681SAndroid Build Coastguard Worker       // The imp-defs are skipped below, but we still need to mark those
1027*9880d681SAndroid Build Coastguard Worker       // registers as used by the function.
1028*9880d681SAndroid Build Coastguard Worker       SkippedInstrs.insert(&MCID);
1029*9880d681SAndroid Build Coastguard Worker     }
1030*9880d681SAndroid Build Coastguard Worker 
1031*9880d681SAndroid Build Coastguard Worker     // Third scan.
1032*9880d681SAndroid Build Coastguard Worker     // Allocate defs and collect dead defs.
1033*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i != DefOpEnd; ++i) {
1034*9880d681SAndroid Build Coastguard Worker       MachineOperand &MO = MI->getOperand(i);
1035*9880d681SAndroid Build Coastguard Worker       if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber())
1036*9880d681SAndroid Build Coastguard Worker         continue;
1037*9880d681SAndroid Build Coastguard Worker       unsigned Reg = MO.getReg();
1038*9880d681SAndroid Build Coastguard Worker 
1039*9880d681SAndroid Build Coastguard Worker       if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1040*9880d681SAndroid Build Coastguard Worker         if (!MRI->isAllocatable(Reg)) continue;
1041*9880d681SAndroid Build Coastguard Worker         definePhysReg(*MI, Reg, MO.isDead() ? regFree : regReserved);
1042*9880d681SAndroid Build Coastguard Worker         continue;
1043*9880d681SAndroid Build Coastguard Worker       }
1044*9880d681SAndroid Build Coastguard Worker       LiveRegMap::iterator LRI = defineVirtReg(*MI, i, Reg, CopySrc);
1045*9880d681SAndroid Build Coastguard Worker       unsigned PhysReg = LRI->PhysReg;
1046*9880d681SAndroid Build Coastguard Worker       if (setPhysReg(MI, i, PhysReg)) {
1047*9880d681SAndroid Build Coastguard Worker         VirtDead.push_back(Reg);
1048*9880d681SAndroid Build Coastguard Worker         CopyDst = 0; // cancel coalescing;
1049*9880d681SAndroid Build Coastguard Worker       } else
1050*9880d681SAndroid Build Coastguard Worker         CopyDst = (CopyDst == Reg || CopyDst == PhysReg) ? PhysReg : 0;
1051*9880d681SAndroid Build Coastguard Worker     }
1052*9880d681SAndroid Build Coastguard Worker 
1053*9880d681SAndroid Build Coastguard Worker     // Kill dead defs after the scan to ensure that multiple defs of the same
1054*9880d681SAndroid Build Coastguard Worker     // register are allocated identically. We didn't need to do this for uses
1055*9880d681SAndroid Build Coastguard Worker     // because we are crerating our own kill flags, and they are always at the
1056*9880d681SAndroid Build Coastguard Worker     // last use.
1057*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = VirtDead.size(); i != e; ++i)
1058*9880d681SAndroid Build Coastguard Worker       killVirtReg(VirtDead[i]);
1059*9880d681SAndroid Build Coastguard Worker     VirtDead.clear();
1060*9880d681SAndroid Build Coastguard Worker 
1061*9880d681SAndroid Build Coastguard Worker     if (CopyDst && CopyDst == CopySrc && CopyDstSub == CopySrcSub) {
1062*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "-- coalescing: " << *MI);
1063*9880d681SAndroid Build Coastguard Worker       Coalesced.push_back(MI);
1064*9880d681SAndroid Build Coastguard Worker     } else {
1065*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "<< " << *MI);
1066*9880d681SAndroid Build Coastguard Worker     }
1067*9880d681SAndroid Build Coastguard Worker   }
1068*9880d681SAndroid Build Coastguard Worker 
1069*9880d681SAndroid Build Coastguard Worker   // Spill all physical registers holding virtual registers now.
1070*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Spilling live registers at end of block.\n");
1071*9880d681SAndroid Build Coastguard Worker   spillAll(MBB->getFirstTerminator());
1072*9880d681SAndroid Build Coastguard Worker 
1073*9880d681SAndroid Build Coastguard Worker   // Erase all the coalesced copies. We are delaying it until now because
1074*9880d681SAndroid Build Coastguard Worker   // LiveVirtRegs might refer to the instrs.
1075*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Coalesced.size(); i != e; ++i)
1076*9880d681SAndroid Build Coastguard Worker     MBB->erase(Coalesced[i]);
1077*9880d681SAndroid Build Coastguard Worker   NumCopies += Coalesced.size();
1078*9880d681SAndroid Build Coastguard Worker 
1079*9880d681SAndroid Build Coastguard Worker   DEBUG(MBB->dump());
1080*9880d681SAndroid Build Coastguard Worker }
1081*9880d681SAndroid Build Coastguard Worker 
1082*9880d681SAndroid Build Coastguard Worker /// runOnMachineFunction - Register allocate the whole function
1083*9880d681SAndroid Build Coastguard Worker ///
runOnMachineFunction(MachineFunction & Fn)1084*9880d681SAndroid Build Coastguard Worker bool RAFast::runOnMachineFunction(MachineFunction &Fn) {
1085*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1086*9880d681SAndroid Build Coastguard Worker                << "********** Function: " << Fn.getName() << '\n');
1087*9880d681SAndroid Build Coastguard Worker   MF = &Fn;
1088*9880d681SAndroid Build Coastguard Worker   MRI = &MF->getRegInfo();
1089*9880d681SAndroid Build Coastguard Worker   TRI = MF->getSubtarget().getRegisterInfo();
1090*9880d681SAndroid Build Coastguard Worker   TII = MF->getSubtarget().getInstrInfo();
1091*9880d681SAndroid Build Coastguard Worker   MRI->freezeReservedRegs(Fn);
1092*9880d681SAndroid Build Coastguard Worker   RegClassInfo.runOnMachineFunction(Fn);
1093*9880d681SAndroid Build Coastguard Worker   UsedInInstr.clear();
1094*9880d681SAndroid Build Coastguard Worker   UsedInInstr.setUniverse(TRI->getNumRegUnits());
1095*9880d681SAndroid Build Coastguard Worker 
1096*9880d681SAndroid Build Coastguard Worker   assert(!MRI->isSSA() && "regalloc requires leaving SSA");
1097*9880d681SAndroid Build Coastguard Worker 
1098*9880d681SAndroid Build Coastguard Worker   // initialize the virtual->physical register map to have a 'null'
1099*9880d681SAndroid Build Coastguard Worker   // mapping for all virtual registers
1100*9880d681SAndroid Build Coastguard Worker   StackSlotForVirtReg.resize(MRI->getNumVirtRegs());
1101*9880d681SAndroid Build Coastguard Worker   LiveVirtRegs.setUniverse(MRI->getNumVirtRegs());
1102*9880d681SAndroid Build Coastguard Worker 
1103*9880d681SAndroid Build Coastguard Worker   // Loop over all of the basic blocks, eliminating virtual register references
1104*9880d681SAndroid Build Coastguard Worker   for (MachineFunction::iterator MBBi = Fn.begin(), MBBe = Fn.end();
1105*9880d681SAndroid Build Coastguard Worker        MBBi != MBBe; ++MBBi) {
1106*9880d681SAndroid Build Coastguard Worker     MBB = &*MBBi;
1107*9880d681SAndroid Build Coastguard Worker     AllocateBasicBlock();
1108*9880d681SAndroid Build Coastguard Worker   }
1109*9880d681SAndroid Build Coastguard Worker 
1110*9880d681SAndroid Build Coastguard Worker   // All machine operands and other references to virtual registers have been
1111*9880d681SAndroid Build Coastguard Worker   // replaced. Remove the virtual registers.
1112*9880d681SAndroid Build Coastguard Worker   MRI->clearVirtRegs();
1113*9880d681SAndroid Build Coastguard Worker 
1114*9880d681SAndroid Build Coastguard Worker   SkippedInstrs.clear();
1115*9880d681SAndroid Build Coastguard Worker   StackSlotForVirtReg.clear();
1116*9880d681SAndroid Build Coastguard Worker   LiveDbgValueMap.clear();
1117*9880d681SAndroid Build Coastguard Worker   return true;
1118*9880d681SAndroid Build Coastguard Worker }
1119*9880d681SAndroid Build Coastguard Worker 
createFastRegisterAllocator()1120*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createFastRegisterAllocator() {
1121*9880d681SAndroid Build Coastguard Worker   return new RAFast();
1122*9880d681SAndroid Build Coastguard Worker }
1123