1 //===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file This register allocator allocates registers to a basic block at a
10 /// time, attempting to keep values in registers and reusing registers as
11 /// appropriate.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/IndexedMap.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/SparseSet.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineOperand.h"
29 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/RegAllocRegistry.h"
31 #include "llvm/CodeGen/RegisterClassInfo.h"
32 #include "llvm/CodeGen/TargetInstrInfo.h"
33 #include "llvm/CodeGen/TargetOpcodes.h"
34 #include "llvm/CodeGen/TargetRegisterInfo.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/IR/DebugLoc.h"
37 #include "llvm/IR/Metadata.h"
38 #include "llvm/InitializePasses.h"
39 #include "llvm/MC/MCInstrDesc.h"
40 #include "llvm/MC/MCRegisterInfo.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/Compiler.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include <cassert>
48 #include <tuple>
49 #include <vector>
50 
51 using namespace llvm;
52 
53 #define DEBUG_TYPE "regalloc"
54 
55 STATISTIC(NumStores, "Number of stores added");
56 STATISTIC(NumLoads , "Number of loads added");
57 STATISTIC(NumCoalesced, "Number of copies coalesced");
58 
59 // FIXME: Remove this switch when all testcases are fixed!
60 static cl::opt<bool> IgnoreMissingDefs("rafast-ignore-missing-defs",
61                                        cl::Hidden);
62 
63 static RegisterRegAlloc
64   fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator);
65 
66 namespace {
67 
68   class RegAllocFast : public MachineFunctionPass {
69   public:
70     static char ID;
71 
72     RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {}
73 
74   private:
75     MachineFrameInfo *MFI;
76     MachineRegisterInfo *MRI;
77     const TargetRegisterInfo *TRI;
78     const TargetInstrInfo *TII;
79     RegisterClassInfo RegClassInfo;
80 
81     /// Basic block currently being allocated.
82     MachineBasicBlock *MBB;
83 
84     /// Maps virtual regs to the frame index where these values are spilled.
85     IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
86 
87     /// Everything we know about a live virtual register.
88     struct LiveReg {
89       MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
90       Register VirtReg;                ///< Virtual register number.
91       MCPhysReg PhysReg = 0;           ///< Currently held here.
92       bool LiveOut = false;            ///< Register is possibly live out.
93       bool Reloaded = false;           ///< Register was reloaded.
94       bool Error = false;              ///< Could not allocate.
95 
96       explicit LiveReg(Register VirtReg) : VirtReg(VirtReg) {}
97 
98       unsigned getSparseSetIndex() const {
99         return Register::virtReg2Index(VirtReg);
100       }
101     };
102 
103     using LiveRegMap = SparseSet<LiveReg>;
104     /// This map contains entries for each virtual register that is currently
105     /// available in a physical register.
106     LiveRegMap LiveVirtRegs;
107 
108     DenseMap<unsigned, SmallVector<MachineInstr *, 2>> LiveDbgValueMap;
109     /// List of DBG_VALUE that we encountered without the vreg being assigned
110     /// because they were placed after the last use of the vreg.
111     DenseMap<unsigned, SmallVector<MachineInstr *, 1>> DanglingDbgValues;
112 
113     /// Has a bit set for every virtual register for which it was determined
114     /// that it is alive across blocks.
115     BitVector MayLiveAcrossBlocks;
116 
117     /// State of a register unit.
118     enum RegUnitState {
119       /// A free register is not currently in use and can be allocated
120       /// immediately without checking aliases.
121       regFree,
122 
123       /// A pre-assigned register has been assigned before register allocation
124       /// (e.g., setting up a call parameter).
125       regPreAssigned,
126 
127       /// Used temporarily in reloadAtBegin() to mark register units that are
128       /// live-in to the basic block.
129       regLiveIn,
130 
131       /// A register state may also be a virtual register number, indication
132       /// that the physical register is currently allocated to a virtual
133       /// register. In that case, LiveVirtRegs contains the inverse mapping.
134     };
135 
136     /// Maps each physical register to a RegUnitState enum or virtual register.
137     std::vector<unsigned> RegUnitStates;
138 
139     SmallVector<MachineInstr *, 32> Coalesced;
140 
141     using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>;
142     /// Set of register units that are used in the current instruction, and so
143     /// cannot be allocated.
144     RegUnitSet UsedInInstr;
145     RegUnitSet PhysRegUses;
146     SmallVector<uint16_t, 8> DefOperandIndexes;
147 
148     void setPhysRegState(MCPhysReg PhysReg, unsigned NewState);
149     bool isPhysRegFree(MCPhysReg PhysReg) const;
150 
151     /// Mark a physreg as used in this instruction.
152     void markRegUsedInInstr(MCPhysReg PhysReg) {
153       for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
154         UsedInInstr.insert(*Units);
155     }
156 
157     /// Check if a physreg or any of its aliases are used in this instruction.
158     bool isRegUsedInInstr(MCPhysReg PhysReg, bool LookAtPhysRegUses) const {
159       for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
160         if (UsedInInstr.count(*Units))
161           return true;
162         if (LookAtPhysRegUses && PhysRegUses.count(*Units))
163           return true;
164       }
165       return false;
166     }
167 
168     /// Mark physical register as being used in a register use operand.
169     /// This is only used by the special livethrough handling code.
170     void markPhysRegUsedInInstr(MCPhysReg PhysReg) {
171       for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
172         PhysRegUses.insert(*Units);
173     }
174 
175     /// Remove mark of physical register being used in the instruction.
176     void unmarkRegUsedInInstr(MCPhysReg PhysReg) {
177       for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
178         UsedInInstr.erase(*Units);
179     }
180 
181     enum : unsigned {
182       spillClean = 50,
183       spillDirty = 100,
184       spillPrefBonus = 20,
185       spillImpossible = ~0u
186     };
187 
188   public:
189     StringRef getPassName() const override { return "Fast Register Allocator"; }
190 
191     void getAnalysisUsage(AnalysisUsage &AU) const override {
192       AU.setPreservesCFG();
193       MachineFunctionPass::getAnalysisUsage(AU);
194     }
195 
196     MachineFunctionProperties getRequiredProperties() const override {
197       return MachineFunctionProperties().set(
198           MachineFunctionProperties::Property::NoPHIs);
199     }
200 
201     MachineFunctionProperties getSetProperties() const override {
202       return MachineFunctionProperties().set(
203           MachineFunctionProperties::Property::NoVRegs);
204     }
205 
206   private:
207     bool runOnMachineFunction(MachineFunction &MF) override;
208 
209     void allocateBasicBlock(MachineBasicBlock &MBB);
210 
211     void addRegClassDefCounts(std::vector<unsigned> &RegClassDefCounts,
212                               Register Reg) const;
213 
214     void allocateInstruction(MachineInstr &MI);
215     void handleDebugValue(MachineInstr &MI);
216 #ifndef NDEBUG
217     bool verifyRegStateMapping(const LiveReg &LR) const;
218 #endif
219     bool usePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
220     bool definePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
221     bool displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
222     void freePhysReg(MCPhysReg PhysReg);
223 
224     unsigned calcSpillCost(MCPhysReg PhysReg) const;
225 
226     LiveRegMap::iterator findLiveVirtReg(Register VirtReg) {
227       return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
228     }
229 
230     LiveRegMap::const_iterator findLiveVirtReg(Register VirtReg) const {
231       return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
232     }
233 
234     void assignVirtToPhysReg(MachineInstr &MI, LiveReg &, MCPhysReg PhysReg);
235     void allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint,
236                       bool LookAtPhysRegUses = false);
237     void allocVirtRegUndef(MachineOperand &MO);
238     void assignDanglingDebugValues(MachineInstr &Def, Register VirtReg,
239                                    MCPhysReg Reg);
240     void defineLiveThroughVirtReg(MachineInstr &MI, unsigned OpNum,
241                                   Register VirtReg);
242     void defineVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg,
243                        bool LookAtPhysRegUses = false);
244     void useVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg);
245 
246     MachineBasicBlock::iterator
247     getMBBBeginInsertionPoint(MachineBasicBlock &MBB,
248                               SmallSet<Register, 2> &PrologLiveIns) const;
249 
250     void reloadAtBegin(MachineBasicBlock &MBB);
251     void setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
252 
253     Register traceCopies(Register VirtReg) const;
254     Register traceCopyChain(Register Reg) const;
255 
256     int getStackSpaceFor(Register VirtReg);
257     void spill(MachineBasicBlock::iterator Before, Register VirtReg,
258                MCPhysReg AssignedReg, bool Kill, bool LiveOut);
259     void reload(MachineBasicBlock::iterator Before, Register VirtReg,
260                 MCPhysReg PhysReg);
261 
262     bool mayLiveOut(Register VirtReg);
263     bool mayLiveIn(Register VirtReg);
264 
265     void dumpState() const;
266   };
267 
268 } // end anonymous namespace
269 
270 char RegAllocFast::ID = 0;
271 
272 INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
273                 false)
274 
275 void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) {
276   for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI)
277     RegUnitStates[*UI] = NewState;
278 }
279 
280 bool RegAllocFast::isPhysRegFree(MCPhysReg PhysReg) const {
281   for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
282     if (RegUnitStates[*UI] != regFree)
283       return false;
284   }
285   return true;
286 }
287 
288 /// This allocates space for the specified virtual register to be held on the
289 /// stack.
290 int RegAllocFast::getStackSpaceFor(Register VirtReg) {
291   // Find the location Reg would belong...
292   int SS = StackSlotForVirtReg[VirtReg];
293   // Already has space allocated?
294   if (SS != -1)
295     return SS;
296 
297   // Allocate a new stack object for this spill location...
298   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
299   unsigned Size = TRI->getSpillSize(RC);
300   Align Alignment = TRI->getSpillAlign(RC);
301   int FrameIdx = MFI->CreateSpillStackObject(Size, Alignment);
302 
303   // Assign the slot.
304   StackSlotForVirtReg[VirtReg] = FrameIdx;
305   return FrameIdx;
306 }
307 
308 static bool dominates(MachineBasicBlock &MBB,
309                       MachineBasicBlock::const_iterator A,
310                       MachineBasicBlock::const_iterator B) {
311   auto MBBEnd = MBB.end();
312   if (B == MBBEnd)
313     return true;
314 
315   MachineBasicBlock::const_iterator I = MBB.begin();
316   for (; &*I != A && &*I != B; ++I)
317     ;
318 
319   return &*I == A;
320 }
321 
322 /// Returns false if \p VirtReg is known to not live out of the current block.
323 bool RegAllocFast::mayLiveOut(Register VirtReg) {
324   if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg))) {
325     // Cannot be live-out if there are no successors.
326     return !MBB->succ_empty();
327   }
328 
329   const MachineInstr *SelfLoopDef = nullptr;
330 
331   // If this block loops back to itself, it is necessary to check whether the
332   // use comes after the def.
333   if (MBB->isSuccessor(MBB)) {
334     SelfLoopDef = MRI->getUniqueVRegDef(VirtReg);
335     if (!SelfLoopDef) {
336       MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
337       return true;
338     }
339   }
340 
341   // See if the first \p Limit uses of the register are all in the current
342   // block.
343   static const unsigned Limit = 8;
344   unsigned C = 0;
345   for (const MachineInstr &UseInst : MRI->use_nodbg_instructions(VirtReg)) {
346     if (UseInst.getParent() != MBB || ++C >= Limit) {
347       MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
348       // Cannot be live-out if there are no successors.
349       return !MBB->succ_empty();
350     }
351 
352     if (SelfLoopDef) {
353       // Try to handle some simple cases to avoid spilling and reloading every
354       // value inside a self looping block.
355       if (SelfLoopDef == &UseInst ||
356           !dominates(*MBB, SelfLoopDef->getIterator(), UseInst.getIterator())) {
357         MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
358         return true;
359       }
360     }
361   }
362 
363   return false;
364 }
365 
366 /// Returns false if \p VirtReg is known to not be live into the current block.
367 bool RegAllocFast::mayLiveIn(Register VirtReg) {
368   if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg)))
369     return !MBB->pred_empty();
370 
371   // See if the first \p Limit def of the register are all in the current block.
372   static const unsigned Limit = 8;
373   unsigned C = 0;
374   for (const MachineInstr &DefInst : MRI->def_instructions(VirtReg)) {
375     if (DefInst.getParent() != MBB || ++C >= Limit) {
376       MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
377       return !MBB->pred_empty();
378     }
379   }
380 
381   return false;
382 }
383 
384 /// Insert spill instruction for \p AssignedReg before \p Before. Update
385 /// DBG_VALUEs with \p VirtReg operands with the stack slot.
386 void RegAllocFast::spill(MachineBasicBlock::iterator Before, Register VirtReg,
387                          MCPhysReg AssignedReg, bool Kill, bool LiveOut) {
388   LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI)
389                     << " in " << printReg(AssignedReg, TRI));
390   int FI = getStackSpaceFor(VirtReg);
391   LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
392 
393   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
394   TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI);
395   ++NumStores;
396 
397   MachineBasicBlock::iterator FirstTerm = MBB->getFirstTerminator();
398 
399   // When we spill a virtual register, we will have spill instructions behind
400   // every definition of it, meaning we can switch all the DBG_VALUEs over
401   // to just reference the stack slot.
402   SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg];
403   for (MachineInstr *DBG : LRIDbgValues) {
404     MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI);
405     assert(NewDV->getParent() == MBB && "dangling parent pointer");
406     (void)NewDV;
407     LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
408 
409     if (LiveOut) {
410       // We need to insert a DBG_VALUE at the end of the block if the spill slot
411       // is live out, but there is another use of the value after the
412       // spill. This will allow LiveDebugValues to see the correct live out
413       // value to propagate to the successors.
414       MachineInstr *ClonedDV = MBB->getParent()->CloneMachineInstr(NewDV);
415       MBB->insert(FirstTerm, ClonedDV);
416       LLVM_DEBUG(dbgs() << "Cloning debug info due to live out spill\n");
417     }
418 
419     // Rewrite unassigned dbg_values to use the stack slot.
420     MachineOperand &MO = DBG->getOperand(0);
421     if (MO.isReg() && MO.getReg() == 0)
422       updateDbgValueForSpill(*DBG, FI);
423   }
424   // Now this register is spilled there is should not be any DBG_VALUE
425   // pointing to this register because they are all pointing to spilled value
426   // now.
427   LRIDbgValues.clear();
428 }
429 
430 /// Insert reload instruction for \p PhysReg before \p Before.
431 void RegAllocFast::reload(MachineBasicBlock::iterator Before, Register VirtReg,
432                           MCPhysReg PhysReg) {
433   LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
434                     << printReg(PhysReg, TRI) << '\n');
435   int FI = getStackSpaceFor(VirtReg);
436   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
437   TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI);
438   ++NumLoads;
439 }
440 
441 /// Get basic block begin insertion point.
442 /// This is not just MBB.begin() because surprisingly we have EH_LABEL
443 /// instructions marking the begin of a basic block. This means we must insert
444 /// new instructions after such labels...
445 MachineBasicBlock::iterator
446 RegAllocFast::getMBBBeginInsertionPoint(
447   MachineBasicBlock &MBB, SmallSet<Register, 2> &PrologLiveIns) const {
448   MachineBasicBlock::iterator I = MBB.begin();
449   while (I != MBB.end()) {
450     if (I->isLabel()) {
451       ++I;
452       continue;
453     }
454 
455     // Most reloads should be inserted after prolog instructions.
456     if (!TII->isBasicBlockPrologue(*I))
457       break;
458 
459     // However if a prolog instruction reads a register that needs to be
460     // reloaded, the reload should be inserted before the prolog.
461     for (MachineOperand &MO : I->operands()) {
462       if (MO.isReg())
463         PrologLiveIns.insert(MO.getReg());
464     }
465 
466     ++I;
467   }
468 
469   return I;
470 }
471 
472 /// Reload all currently assigned virtual registers.
473 void RegAllocFast::reloadAtBegin(MachineBasicBlock &MBB) {
474   if (LiveVirtRegs.empty())
475     return;
476 
477   for (MachineBasicBlock::RegisterMaskPair P : MBB.liveins()) {
478     MCPhysReg Reg = P.PhysReg;
479     // Set state to live-in. This possibly overrides mappings to virtual
480     // registers but we don't care anymore at this point.
481     setPhysRegState(Reg, regLiveIn);
482   }
483 
484 
485   SmallSet<Register, 2> PrologLiveIns;
486 
487   // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
488   // of spilling here is deterministic, if arbitrary.
489   MachineBasicBlock::iterator InsertBefore
490     = getMBBBeginInsertionPoint(MBB, PrologLiveIns);
491   for (const LiveReg &LR : LiveVirtRegs) {
492     MCPhysReg PhysReg = LR.PhysReg;
493     if (PhysReg == 0)
494       continue;
495 
496     unsigned FirstUnit = *MCRegUnitIterator(PhysReg, TRI);
497     if (RegUnitStates[FirstUnit] == regLiveIn)
498       continue;
499 
500     assert((&MBB != &MBB.getParent()->front() || IgnoreMissingDefs) &&
501            "no reload in start block. Missing vreg def?");
502 
503     if (PrologLiveIns.count(PhysReg)) {
504       // FIXME: Theoretically this should use an insert point skipping labels
505       // but I'm not sure how labels should interact with prolog instruction
506       // that need reloads.
507       reload(MBB.begin(), LR.VirtReg, PhysReg);
508     } else
509       reload(InsertBefore, LR.VirtReg, PhysReg);
510   }
511   LiveVirtRegs.clear();
512 }
513 
514 /// Handle the direct use of a physical register.  Check that the register is
515 /// not used by a virtreg. Kill the physreg, marking it free. This may add
516 /// implicit kills to MO->getParent() and invalidate MO.
517 bool RegAllocFast::usePhysReg(MachineInstr &MI, MCPhysReg Reg) {
518   assert(Register::isPhysicalRegister(Reg) && "expected physreg");
519   bool displacedAny = displacePhysReg(MI, Reg);
520   setPhysRegState(Reg, regPreAssigned);
521   markRegUsedInInstr(Reg);
522   return displacedAny;
523 }
524 
525 bool RegAllocFast::definePhysReg(MachineInstr &MI, MCPhysReg Reg) {
526   bool displacedAny = displacePhysReg(MI, Reg);
527   setPhysRegState(Reg, regPreAssigned);
528   return displacedAny;
529 }
530 
531 /// Mark PhysReg as reserved or free after spilling any virtregs. This is very
532 /// similar to defineVirtReg except the physreg is reserved instead of
533 /// allocated.
534 bool RegAllocFast::displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg) {
535   bool displacedAny = false;
536 
537   for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
538     unsigned Unit = *UI;
539     switch (unsigned VirtReg = RegUnitStates[Unit]) {
540     default: {
541       LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
542       assert(LRI != LiveVirtRegs.end() && "datastructures in sync");
543       MachineBasicBlock::iterator ReloadBefore =
544           std::next((MachineBasicBlock::iterator)MI.getIterator());
545       reload(ReloadBefore, VirtReg, LRI->PhysReg);
546 
547       setPhysRegState(LRI->PhysReg, regFree);
548       LRI->PhysReg = 0;
549       LRI->Reloaded = true;
550       displacedAny = true;
551       break;
552     }
553     case regPreAssigned:
554       RegUnitStates[Unit] = regFree;
555       displacedAny = true;
556       break;
557     case regFree:
558       break;
559     }
560   }
561   return displacedAny;
562 }
563 
564 void RegAllocFast::freePhysReg(MCPhysReg PhysReg) {
565   LLVM_DEBUG(dbgs() << "Freeing " << printReg(PhysReg, TRI) << ':');
566 
567   unsigned FirstUnit = *MCRegUnitIterator(PhysReg, TRI);
568   switch (unsigned VirtReg = RegUnitStates[FirstUnit]) {
569   case regFree:
570     LLVM_DEBUG(dbgs() << '\n');
571     return;
572   case regPreAssigned:
573     LLVM_DEBUG(dbgs() << '\n');
574     setPhysRegState(PhysReg, regFree);
575     return;
576   default: {
577       LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
578       assert(LRI != LiveVirtRegs.end());
579       LLVM_DEBUG(dbgs() << ' ' << printReg(LRI->VirtReg, TRI) << '\n');
580       setPhysRegState(LRI->PhysReg, regFree);
581       LRI->PhysReg = 0;
582     }
583     return;
584   }
585 }
586 
587 /// Return the cost of spilling clearing out PhysReg and aliases so it is free
588 /// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
589 /// disabled - it can be allocated directly.
590 /// \returns spillImpossible when PhysReg or an alias can't be spilled.
591 unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const {
592   for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
593     switch (unsigned VirtReg = RegUnitStates[*UI]) {
594     case regFree:
595       break;
596     case regPreAssigned:
597       LLVM_DEBUG(dbgs() << "Cannot spill pre-assigned "
598                         << printReg(PhysReg, TRI) << '\n');
599       return spillImpossible;
600     default: {
601       bool SureSpill = StackSlotForVirtReg[VirtReg] != -1 ||
602                        findLiveVirtReg(VirtReg)->LiveOut;
603       return SureSpill ? spillClean : spillDirty;
604     }
605     }
606   }
607   return 0;
608 }
609 
610 void RegAllocFast::assignDanglingDebugValues(MachineInstr &Definition,
611                                              Register VirtReg, MCPhysReg Reg) {
612   auto UDBGValIter = DanglingDbgValues.find(VirtReg);
613   if (UDBGValIter == DanglingDbgValues.end())
614     return;
615 
616   SmallVectorImpl<MachineInstr*> &Dangling = UDBGValIter->second;
617   for (MachineInstr *DbgValue : Dangling) {
618     assert(DbgValue->isDebugValue());
619     MachineOperand &MO = DbgValue->getOperand(0);
620     if (!MO.isReg())
621       continue;
622 
623     // Test whether the physreg survives from the definition to the DBG_VALUE.
624     MCPhysReg SetToReg = Reg;
625     unsigned Limit = 20;
626     for (MachineBasicBlock::iterator I = std::next(Definition.getIterator()),
627          E = DbgValue->getIterator(); I != E; ++I) {
628       if (I->modifiesRegister(Reg, TRI) || --Limit == 0) {
629         LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
630                    << '\n');
631         SetToReg = 0;
632         break;
633       }
634     }
635     MO.setReg(SetToReg);
636     if (SetToReg != 0)
637       MO.setIsRenamable();
638   }
639   Dangling.clear();
640 }
641 
642 /// This method updates local state so that we know that PhysReg is the
643 /// proper container for VirtReg now.  The physical register must not be used
644 /// for anything else when this is called.
645 void RegAllocFast::assignVirtToPhysReg(MachineInstr &AtMI, LiveReg &LR,
646                                        MCPhysReg PhysReg) {
647   Register VirtReg = LR.VirtReg;
648   LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
649                     << printReg(PhysReg, TRI) << '\n');
650   assert(LR.PhysReg == 0 && "Already assigned a physreg");
651   assert(PhysReg != 0 && "Trying to assign no register");
652   LR.PhysReg = PhysReg;
653   setPhysRegState(PhysReg, VirtReg);
654 
655   assignDanglingDebugValues(AtMI, VirtReg, PhysReg);
656 }
657 
658 static bool isCoalescable(const MachineInstr &MI) {
659   return MI.isFullCopy();
660 }
661 
662 Register RegAllocFast::traceCopyChain(Register Reg) const {
663   static const unsigned ChainLengthLimit = 3;
664   unsigned C = 0;
665   do {
666     if (Reg.isPhysical())
667       return Reg;
668     assert(Reg.isVirtual());
669 
670     MachineInstr *VRegDef = MRI->getUniqueVRegDef(Reg);
671     if (!VRegDef || !isCoalescable(*VRegDef))
672       return 0;
673     Reg = VRegDef->getOperand(1).getReg();
674   } while (++C <= ChainLengthLimit);
675   return 0;
676 }
677 
678 /// Check if any of \p VirtReg's definitions is a copy. If it is follow the
679 /// chain of copies to check whether we reach a physical register we can
680 /// coalesce with.
681 Register RegAllocFast::traceCopies(Register VirtReg) const {
682   static const unsigned DefLimit = 3;
683   unsigned C = 0;
684   for (const MachineInstr &MI : MRI->def_instructions(VirtReg)) {
685     if (isCoalescable(MI)) {
686       Register Reg = MI.getOperand(1).getReg();
687       Reg = traceCopyChain(Reg);
688       if (Reg.isValid())
689         return Reg;
690     }
691 
692     if (++C >= DefLimit)
693       break;
694   }
695   return Register();
696 }
697 
698 /// Allocates a physical register for VirtReg.
699 void RegAllocFast::allocVirtReg(MachineInstr &MI, LiveReg &LR,
700                                 Register Hint0, bool LookAtPhysRegUses) {
701   const Register VirtReg = LR.VirtReg;
702   assert(LR.PhysReg == 0);
703 
704   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
705   LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
706                     << " in class " << TRI->getRegClassName(&RC)
707                     << " with hint " << printReg(Hint0, TRI) << '\n');
708 
709   // Take hint when possible.
710   if (Hint0.isPhysical() && MRI->isAllocatable(Hint0) && RC.contains(Hint0) &&
711       !isRegUsedInInstr(Hint0, LookAtPhysRegUses)) {
712     // Take hint if the register is currently free.
713     if (isPhysRegFree(Hint0)) {
714       LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI)
715                         << '\n');
716       assignVirtToPhysReg(MI, LR, Hint0);
717       return;
718     } else {
719       LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint0, TRI)
720                         << " occupied\n");
721     }
722   } else {
723     Hint0 = Register();
724   }
725 
726 
727   // Try other hint.
728   Register Hint1 = traceCopies(VirtReg);
729   if (Hint1.isPhysical() && MRI->isAllocatable(Hint1) && RC.contains(Hint1) &&
730       !isRegUsedInInstr(Hint1, LookAtPhysRegUses)) {
731     // Take hint if the register is currently free.
732     if (isPhysRegFree(Hint1)) {
733       LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI)
734                  << '\n');
735       assignVirtToPhysReg(MI, LR, Hint1);
736       return;
737     } else {
738       LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint1, TRI)
739                  << " occupied\n");
740     }
741   } else {
742     Hint1 = Register();
743   }
744 
745   MCPhysReg BestReg = 0;
746   unsigned BestCost = spillImpossible;
747   ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
748   for (MCPhysReg PhysReg : AllocationOrder) {
749     LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
750     if (isRegUsedInInstr(PhysReg, LookAtPhysRegUses)) {
751       LLVM_DEBUG(dbgs() << "already used in instr.\n");
752       continue;
753     }
754 
755     unsigned Cost = calcSpillCost(PhysReg);
756     LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
757     // Immediate take a register with cost 0.
758     if (Cost == 0) {
759       assignVirtToPhysReg(MI, LR, PhysReg);
760       return;
761     }
762 
763     if (PhysReg == Hint0 || PhysReg == Hint1)
764       Cost -= spillPrefBonus;
765 
766     if (Cost < BestCost) {
767       BestReg = PhysReg;
768       BestCost = Cost;
769     }
770   }
771 
772   if (!BestReg) {
773     // Nothing we can do: Report an error and keep going with an invalid
774     // allocation.
775     if (MI.isInlineAsm())
776       MI.emitError("inline assembly requires more registers than available");
777     else
778       MI.emitError("ran out of registers during register allocation");
779 
780     LR.Error = true;
781     LR.PhysReg = 0;
782     return;
783   }
784 
785   displacePhysReg(MI, BestReg);
786   assignVirtToPhysReg(MI, LR, BestReg);
787 }
788 
789 void RegAllocFast::allocVirtRegUndef(MachineOperand &MO) {
790   assert(MO.isUndef() && "expected undef use");
791   Register VirtReg = MO.getReg();
792   assert(Register::isVirtualRegister(VirtReg) && "Expected virtreg");
793 
794   LiveRegMap::const_iterator LRI = findLiveVirtReg(VirtReg);
795   MCPhysReg PhysReg;
796   if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
797     PhysReg = LRI->PhysReg;
798   } else {
799     const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
800     ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
801     assert(!AllocationOrder.empty() && "Allocation order must not be empty");
802     PhysReg = AllocationOrder[0];
803   }
804 
805   unsigned SubRegIdx = MO.getSubReg();
806   if (SubRegIdx != 0) {
807     PhysReg = TRI->getSubReg(PhysReg, SubRegIdx);
808     MO.setSubReg(0);
809   }
810   MO.setReg(PhysReg);
811   MO.setIsRenamable(true);
812 }
813 
814 /// Variation of defineVirtReg() with special handling for livethrough regs
815 /// (tied or earlyclobber) that may interfere with preassigned uses.
816 void RegAllocFast::defineLiveThroughVirtReg(MachineInstr &MI, unsigned OpNum,
817                                             Register VirtReg) {
818   LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
819   if (LRI != LiveVirtRegs.end()) {
820     MCPhysReg PrevReg = LRI->PhysReg;
821     if (PrevReg != 0 && isRegUsedInInstr(PrevReg, true)) {
822       LLVM_DEBUG(dbgs() << "Need new assignment for " << printReg(PrevReg, TRI)
823                         << " (tied/earlyclobber resolution)\n");
824       freePhysReg(PrevReg);
825       LRI->PhysReg = 0;
826       allocVirtReg(MI, *LRI, 0, true);
827       MachineBasicBlock::iterator InsertBefore =
828         std::next((MachineBasicBlock::iterator)MI.getIterator());
829       LLVM_DEBUG(dbgs() << "Copy " << printReg(LRI->PhysReg, TRI) << " to "
830                         << printReg(PrevReg, TRI) << '\n');
831       BuildMI(*MBB, InsertBefore, MI.getDebugLoc(),
832               TII->get(TargetOpcode::COPY), PrevReg)
833         .addReg(LRI->PhysReg, llvm::RegState::Kill);
834     }
835     MachineOperand &MO = MI.getOperand(OpNum);
836     if (MO.getSubReg() && !MO.isUndef()) {
837       LRI->LastUse = &MI;
838     }
839   }
840   return defineVirtReg(MI, OpNum, VirtReg, true);
841 }
842 
843 /// Allocates a register for VirtReg definition. Typically the register is
844 /// already assigned from a use of the virtreg, however we still need to
845 /// perform an allocation if:
846 /// - It is a dead definition without any uses.
847 /// - The value is live out and all uses are in different basic blocks.
848 void RegAllocFast::defineVirtReg(MachineInstr &MI, unsigned OpNum,
849                                  Register VirtReg, bool LookAtPhysRegUses) {
850   assert(VirtReg.isVirtual() && "Not a virtual register");
851   MachineOperand &MO = MI.getOperand(OpNum);
852   LiveRegMap::iterator LRI;
853   bool New;
854   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
855   if (New) {
856     if (!MO.isDead()) {
857       if (mayLiveOut(VirtReg)) {
858         LRI->LiveOut = true;
859       } else {
860         // It is a dead def without the dead flag; add the flag now.
861         MO.setIsDead(true);
862       }
863     }
864   }
865   if (LRI->PhysReg == 0)
866     allocVirtReg(MI, *LRI, 0, LookAtPhysRegUses);
867   else {
868     assert(!isRegUsedInInstr(LRI->PhysReg, LookAtPhysRegUses) &&
869            "TODO: preassign mismatch");
870     LLVM_DEBUG(dbgs() << "In def of " << printReg(VirtReg, TRI)
871                       << " use existing assignment to "
872                       << printReg(LRI->PhysReg, TRI) << '\n');
873   }
874 
875   MCPhysReg PhysReg = LRI->PhysReg;
876   assert(PhysReg != 0 && "Register not assigned");
877   if (LRI->Reloaded || LRI->LiveOut) {
878     if (!MI.isImplicitDef()) {
879       MachineBasicBlock::iterator SpillBefore =
880           std::next((MachineBasicBlock::iterator)MI.getIterator());
881       LLVM_DEBUG(dbgs() << "Spill Reason: LO: " << LRI->LiveOut << " RL: "
882                         << LRI->Reloaded << '\n');
883       bool Kill = LRI->LastUse == nullptr;
884       spill(SpillBefore, VirtReg, PhysReg, Kill, LRI->LiveOut);
885       LRI->LastUse = nullptr;
886     }
887     LRI->LiveOut = false;
888     LRI->Reloaded = false;
889   }
890   markRegUsedInInstr(PhysReg);
891   setPhysReg(MI, MO, PhysReg);
892 }
893 
894 /// Allocates a register for a VirtReg use.
895 void RegAllocFast::useVirtReg(MachineInstr &MI, unsigned OpNum,
896                               Register VirtReg) {
897   assert(VirtReg.isVirtual() && "Not a virtual register");
898   MachineOperand &MO = MI.getOperand(OpNum);
899   LiveRegMap::iterator LRI;
900   bool New;
901   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
902   if (New) {
903     MachineOperand &MO = MI.getOperand(OpNum);
904     if (!MO.isKill()) {
905       if (mayLiveOut(VirtReg)) {
906         LRI->LiveOut = true;
907       } else {
908         // It is a last (killing) use without the kill flag; add the flag now.
909         MO.setIsKill(true);
910       }
911     }
912   } else {
913     assert((!MO.isKill() || LRI->LastUse == &MI) && "Invalid kill flag");
914   }
915 
916   // If necessary allocate a register.
917   if (LRI->PhysReg == 0) {
918     assert(!MO.isTied() && "tied op should be allocated");
919     Register Hint;
920     if (MI.isCopy() && MI.getOperand(1).getSubReg() == 0) {
921       Hint = MI.getOperand(0).getReg();
922       assert(Hint.isPhysical() &&
923              "Copy destination should already be assigned");
924     }
925     allocVirtReg(MI, *LRI, Hint, false);
926     if (LRI->Error) {
927       const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
928       ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
929       setPhysReg(MI, MO, *AllocationOrder.begin());
930       return;
931     }
932   }
933 
934   LRI->LastUse = &MI;
935   markRegUsedInInstr(LRI->PhysReg);
936   setPhysReg(MI, MO, LRI->PhysReg);
937 }
938 
939 /// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
940 /// may invalidate any operand pointers.  Return true if the operand kills its
941 /// register.
942 void RegAllocFast::setPhysReg(MachineInstr &MI, MachineOperand &MO,
943                               MCPhysReg PhysReg) {
944   if (!MO.getSubReg()) {
945     MO.setReg(PhysReg);
946     MO.setIsRenamable(true);
947     return;
948   }
949 
950   // Handle subregister index.
951   MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : Register());
952   MO.setIsRenamable(true);
953   // Note: We leave the subreg number around a little longer in case of defs.
954   // This is so that the register freeing logic in allocateInstruction can still
955   // recognize this as subregister defs. The code there will clear the number.
956   if (!MO.isDef())
957     MO.setSubReg(0);
958 
959   // A kill flag implies killing the full register. Add corresponding super
960   // register kill.
961   if (MO.isKill()) {
962     MI.addRegisterKilled(PhysReg, TRI, true);
963     return;
964   }
965 
966   // A <def,read-undef> of a sub-register requires an implicit def of the full
967   // register.
968   if (MO.isDef() && MO.isUndef()) {
969     if (MO.isDead())
970       MI.addRegisterDead(PhysReg, TRI, true);
971     else
972       MI.addRegisterDefined(PhysReg, TRI);
973   }
974 }
975 
976 #ifndef NDEBUG
977 
978 void RegAllocFast::dumpState() const {
979   for (unsigned Unit = 1, UnitE = TRI->getNumRegUnits(); Unit != UnitE;
980        ++Unit) {
981     switch (unsigned VirtReg = RegUnitStates[Unit]) {
982     case regFree:
983       break;
984     case regPreAssigned:
985       dbgs() << " " << printRegUnit(Unit, TRI) << "[P]";
986       break;
987     case regLiveIn:
988       llvm_unreachable("Should not have regLiveIn in map");
989     default: {
990       dbgs() << ' ' << printRegUnit(Unit, TRI) << '=' << printReg(VirtReg);
991       LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
992       assert(I != LiveVirtRegs.end() && "have LiveVirtRegs entry");
993       if (I->LiveOut || I->Reloaded) {
994         dbgs() << '[';
995         if (I->LiveOut) dbgs() << 'O';
996         if (I->Reloaded) dbgs() << 'R';
997         dbgs() << ']';
998       }
999       assert(TRI->hasRegUnit(I->PhysReg, Unit) && "inverse mapping present");
1000       break;
1001     }
1002     }
1003   }
1004   dbgs() << '\n';
1005   // Check that LiveVirtRegs is the inverse.
1006   for (const LiveReg &LR : LiveVirtRegs) {
1007     Register VirtReg = LR.VirtReg;
1008     assert(VirtReg.isVirtual() && "Bad map key");
1009     MCPhysReg PhysReg = LR.PhysReg;
1010     if (PhysReg != 0) {
1011       assert(Register::isPhysicalRegister(PhysReg) &&
1012              "mapped to physreg");
1013       for (MCRegUnitIterator UI(PhysReg, TRI); UI.isValid(); ++UI) {
1014         assert(RegUnitStates[*UI] == VirtReg && "inverse map valid");
1015       }
1016     }
1017   }
1018 }
1019 #endif
1020 
1021 /// Count number of defs consumed from each register class by \p Reg
1022 void RegAllocFast::addRegClassDefCounts(std::vector<unsigned> &RegClassDefCounts,
1023                                         Register Reg) const {
1024   assert(RegClassDefCounts.size() == TRI->getNumRegClasses());
1025 
1026   if (Reg.isVirtual()) {
1027     const TargetRegisterClass *OpRC = MRI->getRegClass(Reg);
1028     for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
1029          RCIdx != RCIdxEnd; ++RCIdx) {
1030       const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
1031       // FIXME: Consider aliasing sub/super registers.
1032       if (OpRC->hasSubClassEq(IdxRC))
1033         ++RegClassDefCounts[RCIdx];
1034     }
1035 
1036     return;
1037   }
1038 
1039   for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
1040        RCIdx != RCIdxEnd; ++RCIdx) {
1041     const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
1042     for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) {
1043       if (IdxRC->contains(*Alias)) {
1044         ++RegClassDefCounts[RCIdx];
1045         break;
1046       }
1047     }
1048   }
1049 }
1050 
1051 void RegAllocFast::allocateInstruction(MachineInstr &MI) {
1052   // The basic algorithm here is:
1053   // 1. Mark registers of def operands as free
1054   // 2. Allocate registers to use operands and place reload instructions for
1055   //    registers displaced by the allocation.
1056   //
1057   // However we need to handle some corner cases:
1058   // - pre-assigned defs and uses need to be handled before the other def/use
1059   //   operands are processed to avoid the allocation heuristics clashing with
1060   //   the pre-assignment.
1061   // - The "free def operands" step has to come last instead of first for tied
1062   //   operands and early-clobbers.
1063 
1064   UsedInInstr.clear();
1065 
1066   // Scan for special cases; Apply pre-assigned register defs to state.
1067   bool HasPhysRegUse = false;
1068   bool HasRegMask = false;
1069   bool HasVRegDef = false;
1070   bool HasDef = false;
1071   bool HasEarlyClobber = false;
1072   bool NeedToAssignLiveThroughs = false;
1073   for (MachineOperand &MO : MI.operands()) {
1074     if (MO.isReg()) {
1075       Register Reg = MO.getReg();
1076       if (Reg.isVirtual()) {
1077         if (MO.isDef()) {
1078           HasDef = true;
1079           HasVRegDef = true;
1080           if (MO.isEarlyClobber()) {
1081             HasEarlyClobber = true;
1082             NeedToAssignLiveThroughs = true;
1083           }
1084           if (MO.isTied() || (MO.getSubReg() != 0 && !MO.isUndef()))
1085             NeedToAssignLiveThroughs = true;
1086         }
1087       } else if (Reg.isPhysical()) {
1088         if (!MRI->isReserved(Reg)) {
1089           if (MO.isDef()) {
1090             HasDef = true;
1091             bool displacedAny = definePhysReg(MI, Reg);
1092             if (MO.isEarlyClobber())
1093               HasEarlyClobber = true;
1094             if (!displacedAny)
1095               MO.setIsDead(true);
1096           }
1097           if (MO.readsReg())
1098             HasPhysRegUse = true;
1099         }
1100       }
1101     } else if (MO.isRegMask()) {
1102       HasRegMask = true;
1103     }
1104   }
1105 
1106   // Allocate virtreg defs.
1107   if (HasDef) {
1108     if (HasVRegDef) {
1109       // Special handling for early clobbers, tied operands or subregister defs:
1110       // Compared to "normal" defs these:
1111       // - Must not use a register that is pre-assigned for a use operand.
1112       // - In order to solve tricky inline assembly constraints we change the
1113       //   heuristic to figure out a good operand order before doing
1114       //   assignments.
1115       if (NeedToAssignLiveThroughs) {
1116         DefOperandIndexes.clear();
1117         PhysRegUses.clear();
1118 
1119         // Track number of defs which may consume a register from the class.
1120         std::vector<unsigned> RegClassDefCounts(TRI->getNumRegClasses(), 0);
1121         assert(RegClassDefCounts[0] == 0);
1122 
1123         LLVM_DEBUG(dbgs() << "Need to assign livethroughs\n");
1124         for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
1125           const MachineOperand &MO = MI.getOperand(I);
1126           if (!MO.isReg())
1127             continue;
1128           Register Reg = MO.getReg();
1129           if (MO.readsReg()) {
1130             if (Reg.isPhysical()) {
1131               LLVM_DEBUG(dbgs() << "mark extra used: " << printReg(Reg, TRI)
1132                                 << '\n');
1133               markPhysRegUsedInInstr(Reg);
1134             }
1135           }
1136 
1137           if (MO.isDef()) {
1138             if (Reg.isVirtual())
1139               DefOperandIndexes.push_back(I);
1140 
1141             addRegClassDefCounts(RegClassDefCounts, Reg);
1142           }
1143         }
1144 
1145         llvm::sort(DefOperandIndexes.begin(), DefOperandIndexes.end(),
1146                    [&](uint16_t I0, uint16_t I1) {
1147           const MachineOperand &MO0 = MI.getOperand(I0);
1148           const MachineOperand &MO1 = MI.getOperand(I1);
1149           Register Reg0 = MO0.getReg();
1150           Register Reg1 = MO1.getReg();
1151           const TargetRegisterClass &RC0 = *MRI->getRegClass(Reg0);
1152           const TargetRegisterClass &RC1 = *MRI->getRegClass(Reg1);
1153 
1154           // Identify regclass that are easy to use up completely just in this
1155           // instruction.
1156           unsigned ClassSize0 = RegClassInfo.getOrder(&RC0).size();
1157           unsigned ClassSize1 = RegClassInfo.getOrder(&RC1).size();
1158 
1159           bool SmallClass0 = ClassSize0 < RegClassDefCounts[RC0.getID()];
1160           bool SmallClass1 = ClassSize1 < RegClassDefCounts[RC1.getID()];
1161           if (SmallClass0 > SmallClass1)
1162             return true;
1163           if (SmallClass0 < SmallClass1)
1164             return false;
1165 
1166           // Allocate early clobbers and livethrough operands first.
1167           bool Livethrough0 = MO0.isEarlyClobber() || MO0.isTied() ||
1168                               (MO0.getSubReg() == 0 && !MO0.isUndef());
1169           bool Livethrough1 = MO1.isEarlyClobber() || MO1.isTied() ||
1170                               (MO1.getSubReg() == 0 && !MO1.isUndef());
1171           if (Livethrough0 > Livethrough1)
1172             return true;
1173           if (Livethrough0 < Livethrough1)
1174             return false;
1175 
1176           // Tie-break rule: operand index.
1177           return I0 < I1;
1178         });
1179 
1180         for (uint16_t OpIdx : DefOperandIndexes) {
1181           MachineOperand &MO = MI.getOperand(OpIdx);
1182           LLVM_DEBUG(dbgs() << "Allocating " << MO << '\n');
1183           unsigned Reg = MO.getReg();
1184           if (MO.isEarlyClobber() || MO.isTied() ||
1185               (MO.getSubReg() && !MO.isUndef())) {
1186             defineLiveThroughVirtReg(MI, OpIdx, Reg);
1187           } else {
1188             defineVirtReg(MI, OpIdx, Reg);
1189           }
1190         }
1191       } else {
1192         // Assign virtual register defs.
1193         for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
1194           MachineOperand &MO = MI.getOperand(I);
1195           if (!MO.isReg() || !MO.isDef())
1196             continue;
1197           Register Reg = MO.getReg();
1198           if (Reg.isVirtual())
1199             defineVirtReg(MI, I, Reg);
1200         }
1201       }
1202     }
1203 
1204     // Free registers occupied by defs.
1205     // Iterate operands in reverse order, so we see the implicit super register
1206     // defs first (we added them earlier in case of <def,read-undef>).
1207     for (unsigned I = MI.getNumOperands(); I-- > 0;) {
1208       MachineOperand &MO = MI.getOperand(I);
1209       if (!MO.isReg() || !MO.isDef())
1210         continue;
1211 
1212       // subreg defs don't free the full register. We left the subreg number
1213       // around as a marker in setPhysReg() to recognize this case here.
1214       if (MO.getSubReg() != 0) {
1215         MO.setSubReg(0);
1216         continue;
1217       }
1218 
1219       // Do not free tied operands and early clobbers.
1220       if (MO.isTied() || MO.isEarlyClobber())
1221         continue;
1222       Register Reg = MO.getReg();
1223       if (!Reg)
1224         continue;
1225       assert(Reg.isPhysical());
1226       if (MRI->isReserved(Reg))
1227         continue;
1228       freePhysReg(Reg);
1229       unmarkRegUsedInInstr(Reg);
1230     }
1231   }
1232 
1233   // Displace clobbered registers.
1234   if (HasRegMask) {
1235     for (const MachineOperand &MO : MI.operands()) {
1236       if (MO.isRegMask()) {
1237         // MRI bookkeeping.
1238         MRI->addPhysRegsUsedFromRegMask(MO.getRegMask());
1239 
1240         // Displace clobbered registers.
1241         const uint32_t *Mask = MO.getRegMask();
1242         for (LiveRegMap::iterator LRI = LiveVirtRegs.begin(),
1243              LRIE = LiveVirtRegs.end(); LRI != LRIE; ++LRI) {
1244           MCPhysReg PhysReg = LRI->PhysReg;
1245           if (PhysReg != 0 && MachineOperand::clobbersPhysReg(Mask, PhysReg))
1246             displacePhysReg(MI, PhysReg);
1247         }
1248       }
1249     }
1250   }
1251 
1252   // Apply pre-assigned register uses to state.
1253   if (HasPhysRegUse) {
1254     for (MachineOperand &MO : MI.operands()) {
1255       if (!MO.isReg() || !MO.readsReg())
1256         continue;
1257       Register Reg = MO.getReg();
1258       if (!Reg.isPhysical())
1259         continue;
1260       if (MRI->isReserved(Reg))
1261         continue;
1262       bool displacedAny = usePhysReg(MI, Reg);
1263       if (!displacedAny && !MRI->isReserved(Reg))
1264         MO.setIsKill(true);
1265     }
1266   }
1267 
1268   // Allocate virtreg uses and insert reloads as necessary.
1269   bool HasUndefUse = false;
1270   for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
1271     MachineOperand &MO = MI.getOperand(I);
1272     if (!MO.isReg() || !MO.isUse())
1273       continue;
1274     Register Reg = MO.getReg();
1275     if (!Reg.isVirtual())
1276       continue;
1277 
1278     if (MO.isUndef()) {
1279       HasUndefUse = true;
1280       continue;
1281     }
1282 
1283 
1284     // Populate MayLiveAcrossBlocks in case the use block is allocated before
1285     // the def block (removing the vreg uses).
1286     mayLiveIn(Reg);
1287 
1288 
1289     assert(!MO.isInternalRead() && "Bundles not supported");
1290     assert(MO.readsReg() && "reading use");
1291     useVirtReg(MI, I, Reg);
1292   }
1293 
1294   // Allocate undef operands. This is a separate step because in a situation
1295   // like  ` = OP undef %X, %X`    both operands need the same register assign
1296   // so we should perform the normal assignment first.
1297   if (HasUndefUse) {
1298     for (MachineOperand &MO : MI.uses()) {
1299       if (!MO.isReg() || !MO.isUse())
1300         continue;
1301       Register Reg = MO.getReg();
1302       if (!Reg.isVirtual())
1303         continue;
1304 
1305       assert(MO.isUndef() && "Should only have undef virtreg uses left");
1306       allocVirtRegUndef(MO);
1307     }
1308   }
1309 
1310   // Free early clobbers.
1311   if (HasEarlyClobber) {
1312     for (unsigned I = MI.getNumOperands(); I-- > 0; ) {
1313       MachineOperand &MO = MI.getOperand(I);
1314       if (!MO.isReg() || !MO.isDef() || !MO.isEarlyClobber())
1315         continue;
1316       // subreg defs don't free the full register. We left the subreg number
1317       // around as a marker in setPhysReg() to recognize this case here.
1318       if (MO.getSubReg() != 0) {
1319         MO.setSubReg(0);
1320         continue;
1321       }
1322 
1323       Register Reg = MO.getReg();
1324       if (!Reg)
1325         continue;
1326       assert(Reg.isPhysical() && "should have register assigned");
1327 
1328       // We sometimes get odd situations like:
1329       //    early-clobber %x0 = INSTRUCTION %x0
1330       // which is semantically questionable as the early-clobber should
1331       // apply before the use. But in practice we consider the use to
1332       // happen before the early clobber now. Don't free the early clobber
1333       // register in this case.
1334       if (MI.readsRegister(Reg, TRI))
1335         continue;
1336 
1337       freePhysReg(Reg);
1338     }
1339   }
1340 
1341   LLVM_DEBUG(dbgs() << "<< " << MI);
1342   if (MI.isCopy() && MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
1343       MI.getNumOperands() == 2) {
1344     LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1345     Coalesced.push_back(&MI);
1346   }
1347 }
1348 
1349 void RegAllocFast::handleDebugValue(MachineInstr &MI) {
1350   MachineOperand &MO = MI.getDebugOperand(0);
1351 
1352   // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1353   // mostly constants and frame indices.
1354   if (!MO.isReg())
1355     return;
1356   Register Reg = MO.getReg();
1357   if (!Register::isVirtualRegister(Reg))
1358     return;
1359 
1360   // Already spilled to a stackslot?
1361   int SS = StackSlotForVirtReg[Reg];
1362   if (SS != -1) {
1363     // Modify DBG_VALUE now that the value is in a spill slot.
1364     updateDbgValueForSpill(MI, SS);
1365     LLVM_DEBUG(dbgs() << "Rewrite DBG_VALUE for spilled memory: " << MI);
1366     return;
1367   }
1368 
1369   // See if this virtual register has already been allocated to a physical
1370   // register or spilled to a stack slot.
1371   LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1372   if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1373     setPhysReg(MI, MO, LRI->PhysReg);
1374   } else {
1375     DanglingDbgValues[Reg].push_back(&MI);
1376   }
1377 
1378   // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1379   // that future spills of Reg will have DBG_VALUEs.
1380   LiveDbgValueMap[Reg].push_back(&MI);
1381 }
1382 
1383 #ifndef NDEBUG
1384 bool RegAllocFast::verifyRegStateMapping(const LiveReg &LR) const {
1385   for (MCRegUnitIterator UI(LR.PhysReg, TRI); UI.isValid(); ++UI) {
1386     if (RegUnitStates[*UI] != LR.VirtReg)
1387       return false;
1388   }
1389 
1390   return true;
1391 }
1392 #endif
1393 
1394 void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) {
1395   this->MBB = &MBB;
1396   LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
1397 
1398   RegUnitStates.assign(TRI->getNumRegUnits(), regFree);
1399   assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
1400 
1401   for (MachineBasicBlock *Succ : MBB.successors()) {
1402     for (const MachineBasicBlock::RegisterMaskPair &LI : Succ->liveins())
1403       setPhysRegState(LI.PhysReg, regPreAssigned);
1404   }
1405 
1406   Coalesced.clear();
1407 
1408   // Traverse block in reverse order allocating instructions one by one.
1409   for (MachineInstr &MI : reverse(MBB)) {
1410     LLVM_DEBUG(
1411       dbgs() << "\n>> " << MI << "Regs:";
1412       dumpState()
1413     );
1414 
1415     // Special handling for debug values. Note that they are not allowed to
1416     // affect codegen of the other instructions in any way.
1417     if (MI.isDebugValue()) {
1418       handleDebugValue(MI);
1419       continue;
1420     }
1421 
1422     allocateInstruction(MI);
1423   }
1424 
1425   LLVM_DEBUG(
1426     dbgs() << "Begin Regs:";
1427     dumpState()
1428   );
1429 
1430   // Spill all physical registers holding virtual registers now.
1431   LLVM_DEBUG(dbgs() << "Loading live registers at begin of block.\n");
1432   reloadAtBegin(MBB);
1433 
1434   // Erase all the coalesced copies. We are delaying it until now because
1435   // LiveVirtRegs might refer to the instrs.
1436   for (MachineInstr *MI : Coalesced)
1437     MBB.erase(MI);
1438   NumCoalesced += Coalesced.size();
1439 
1440   for (auto &UDBGPair : DanglingDbgValues) {
1441     for (MachineInstr *DbgValue : UDBGPair.second) {
1442       assert(DbgValue->isDebugValue() && "expected DBG_VALUE");
1443       MachineOperand &MO = DbgValue->getOperand(0);
1444       // Nothing to do if the vreg was spilled in the meantime.
1445       if (!MO.isReg())
1446         continue;
1447       LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
1448                  << '\n');
1449       MO.setReg(0);
1450     }
1451   }
1452   DanglingDbgValues.clear();
1453 
1454   LLVM_DEBUG(MBB.dump());
1455 }
1456 
1457 bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) {
1458   LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1459                     << "********** Function: " << MF.getName() << '\n');
1460   MRI = &MF.getRegInfo();
1461   const TargetSubtargetInfo &STI = MF.getSubtarget();
1462   TRI = STI.getRegisterInfo();
1463   TII = STI.getInstrInfo();
1464   MFI = &MF.getFrameInfo();
1465   MRI->freezeReservedRegs(MF);
1466   RegClassInfo.runOnMachineFunction(MF);
1467   unsigned NumRegUnits = TRI->getNumRegUnits();
1468   UsedInInstr.clear();
1469   UsedInInstr.setUniverse(NumRegUnits);
1470   PhysRegUses.clear();
1471   PhysRegUses.setUniverse(NumRegUnits);
1472 
1473   // initialize the virtual->physical register map to have a 'null'
1474   // mapping for all virtual registers
1475   unsigned NumVirtRegs = MRI->getNumVirtRegs();
1476   StackSlotForVirtReg.resize(NumVirtRegs);
1477   LiveVirtRegs.setUniverse(NumVirtRegs);
1478   MayLiveAcrossBlocks.clear();
1479   MayLiveAcrossBlocks.resize(NumVirtRegs);
1480 
1481   // Loop over all of the basic blocks, eliminating virtual register references
1482   for (MachineBasicBlock &MBB : MF)
1483     allocateBasicBlock(MBB);
1484 
1485   // All machine operands and other references to virtual registers have been
1486   // replaced. Remove the virtual registers.
1487   MRI->clearVirtRegs();
1488 
1489   StackSlotForVirtReg.clear();
1490   LiveDbgValueMap.clear();
1491   return true;
1492 }
1493 
1494 FunctionPass *llvm::createFastRegisterAllocator() {
1495   return new RegAllocFast();
1496 }
1497