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