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