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