xref: /llvm-project/llvm/lib/CodeGen/RegAllocFast.cpp (revision f5f32cef617c0796a7d980a464786949cbf21227)
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/CodeGen/RegAllocFast.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/IndexedMap.h"
19 #include "llvm/ADT/MapVector.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/SparseSet.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/CodeGen/MachineBasicBlock.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/RegAllocCommon.h"
33 #include "llvm/CodeGen/RegAllocRegistry.h"
34 #include "llvm/CodeGen/RegisterClassInfo.h"
35 #include "llvm/CodeGen/TargetInstrInfo.h"
36 #include "llvm/CodeGen/TargetOpcodes.h"
37 #include "llvm/CodeGen/TargetRegisterInfo.h"
38 #include "llvm/CodeGen/TargetSubtargetInfo.h"
39 #include "llvm/InitializePasses.h"
40 #include "llvm/MC/MCRegisterInfo.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/ErrorHandling.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include <cassert>
46 #include <tuple>
47 #include <vector>
48 
49 using namespace llvm;
50 
51 #define DEBUG_TYPE "regalloc"
52 
53 STATISTIC(NumStores, "Number of stores added");
54 STATISTIC(NumLoads, "Number of loads added");
55 STATISTIC(NumCoalesced, "Number of copies coalesced");
56 
57 // FIXME: Remove this switch when all testcases are fixed!
58 static cl::opt<bool> IgnoreMissingDefs("rafast-ignore-missing-defs",
59                                        cl::Hidden);
60 
61 static RegisterRegAlloc fastRegAlloc("fast", "fast register allocator",
62                                      createFastRegisterAllocator);
63 
64 namespace {
65 
66 /// Assign ascending index for instructions in machine basic block. The index
67 /// can be used to determine dominance between instructions in same MBB.
68 class InstrPosIndexes {
69 public:
70   void unsetInitialized() { IsInitialized = false; }
71 
72   void init(const MachineBasicBlock &MBB) {
73     CurMBB = &MBB;
74     Instr2PosIndex.clear();
75     uint64_t LastIndex = 0;
76     for (const MachineInstr &MI : MBB) {
77       LastIndex += InstrDist;
78       Instr2PosIndex[&MI] = LastIndex;
79     }
80   }
81 
82   /// Set \p Index to index of \p MI. If \p MI is new inserted, it try to assign
83   /// index without affecting existing instruction's index. Return true if all
84   /// instructions index has been reassigned.
85   bool getIndex(const MachineInstr &MI, uint64_t &Index) {
86     if (!IsInitialized) {
87       init(*MI.getParent());
88       IsInitialized = true;
89       Index = Instr2PosIndex.at(&MI);
90       return true;
91     }
92 
93     assert(MI.getParent() == CurMBB && "MI is not in CurMBB");
94     auto It = Instr2PosIndex.find(&MI);
95     if (It != Instr2PosIndex.end()) {
96       Index = It->second;
97       return false;
98     }
99 
100     // Distance is the number of consecutive unassigned instructions including
101     // MI. Start is the first instruction of them. End is the next of last
102     // instruction of them.
103     // e.g.
104     // |Instruction|  A   |  B   |  C   |  MI  |  D   |  E   |
105     // |   Index   | 1024 |      |      |      |      | 2048 |
106     //
107     // In this case, B, C, MI, D are unassigned. Distance is 4, Start is B, End
108     // is E.
109     unsigned Distance = 1;
110     MachineBasicBlock::const_iterator Start = MI.getIterator(),
111                                       End = std::next(Start);
112     while (Start != CurMBB->begin() &&
113            !Instr2PosIndex.count(&*std::prev(Start))) {
114       --Start;
115       ++Distance;
116     }
117     while (End != CurMBB->end() && !Instr2PosIndex.count(&*(End))) {
118       ++End;
119       ++Distance;
120     }
121 
122     // LastIndex is initialized to last used index prior to MI or zero.
123     // In previous example, LastIndex is 1024, EndIndex is 2048;
124     uint64_t LastIndex =
125         Start == CurMBB->begin() ? 0 : Instr2PosIndex.at(&*std::prev(Start));
126     uint64_t Step;
127     if (End == CurMBB->end())
128       Step = static_cast<uint64_t>(InstrDist);
129     else {
130       // No instruction uses index zero.
131       uint64_t EndIndex = Instr2PosIndex.at(&*End);
132       assert(EndIndex > LastIndex && "Index must be ascending order");
133       unsigned NumAvailableIndexes = EndIndex - LastIndex - 1;
134       // We want index gap between two adjacent MI is as same as possible. Given
135       // total A available indexes, D is number of consecutive unassigned
136       // instructions, S is the step.
137       // |<- S-1 -> MI <- S-1 -> MI <- A-S*D ->|
138       // There're S-1 available indexes between unassigned instruction and its
139       // predecessor. There're A-S*D available indexes between the last
140       // unassigned instruction and its successor.
141       // Ideally, we want
142       //    S-1 = A-S*D
143       // then
144       //    S = (A+1)/(D+1)
145       // An valid S must be integer greater than zero, so
146       //    S <= (A+1)/(D+1)
147       // =>
148       //    A-S*D >= 0
149       // That means we can safely use (A+1)/(D+1) as step.
150       // In previous example, Step is 204, Index of B, C, MI, D is 1228, 1432,
151       // 1636, 1840.
152       Step = (NumAvailableIndexes + 1) / (Distance + 1);
153     }
154 
155     // Reassign index for all instructions if number of new inserted
156     // instructions exceed slot or all instructions are new.
157     if (LLVM_UNLIKELY(!Step || (!LastIndex && Step == InstrDist))) {
158       init(*CurMBB);
159       Index = Instr2PosIndex.at(&MI);
160       return true;
161     }
162 
163     for (auto I = Start; I != End; ++I) {
164       LastIndex += Step;
165       Instr2PosIndex[&*I] = LastIndex;
166     }
167     Index = Instr2PosIndex.at(&MI);
168     return false;
169   }
170 
171 private:
172   bool IsInitialized = false;
173   enum { InstrDist = 1024 };
174   const MachineBasicBlock *CurMBB = nullptr;
175   DenseMap<const MachineInstr *, uint64_t> Instr2PosIndex;
176 };
177 
178 class RegAllocFastImpl {
179 public:
180   RegAllocFastImpl(const RegAllocFilterFunc F = nullptr,
181                    bool ClearVirtRegs_ = true)
182       : ShouldAllocateRegisterImpl(F), StackSlotForVirtReg(-1),
183         ClearVirtRegs(ClearVirtRegs_) {}
184 
185 private:
186   MachineFrameInfo *MFI = nullptr;
187   MachineRegisterInfo *MRI = nullptr;
188   const TargetRegisterInfo *TRI = nullptr;
189   const TargetInstrInfo *TII = nullptr;
190   RegisterClassInfo RegClassInfo;
191   const RegAllocFilterFunc ShouldAllocateRegisterImpl;
192 
193   /// Basic block currently being allocated.
194   MachineBasicBlock *MBB = nullptr;
195 
196   /// Maps virtual regs to the frame index where these values are spilled.
197   IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg;
198 
199   /// Everything we know about a live virtual register.
200   struct LiveReg {
201     MachineInstr *LastUse = nullptr; ///< Last instr to use reg.
202     Register VirtReg;                ///< Virtual register number.
203     MCPhysReg PhysReg = 0;           ///< Currently held here.
204     bool LiveOut = false;            ///< Register is possibly live out.
205     bool Reloaded = false;           ///< Register was reloaded.
206     bool Error = false;              ///< Could not allocate.
207 
208     explicit LiveReg(Register VirtReg) : VirtReg(VirtReg) {}
209 
210     unsigned getSparseSetIndex() const {
211       return Register::virtReg2Index(VirtReg);
212     }
213   };
214 
215   using LiveRegMap = SparseSet<LiveReg, identity<unsigned>, uint16_t>;
216   /// This map contains entries for each virtual register that is currently
217   /// available in a physical register.
218   LiveRegMap LiveVirtRegs;
219 
220   /// Stores assigned virtual registers present in the bundle MI.
221   DenseMap<Register, MCPhysReg> BundleVirtRegsMap;
222 
223   DenseMap<unsigned, SmallVector<MachineOperand *, 2>> LiveDbgValueMap;
224   /// List of DBG_VALUE that we encountered without the vreg being assigned
225   /// because they were placed after the last use of the vreg.
226   DenseMap<unsigned, SmallVector<MachineInstr *, 1>> DanglingDbgValues;
227 
228   /// Has a bit set for every virtual register for which it was determined
229   /// that it is alive across blocks.
230   BitVector MayLiveAcrossBlocks;
231 
232   /// State of a register unit.
233   enum RegUnitState {
234     /// A free register is not currently in use and can be allocated
235     /// immediately without checking aliases.
236     regFree,
237 
238     /// A pre-assigned register has been assigned before register allocation
239     /// (e.g., setting up a call parameter).
240     regPreAssigned,
241 
242     /// Used temporarily in reloadAtBegin() to mark register units that are
243     /// live-in to the basic block.
244     regLiveIn,
245 
246     /// A register state may also be a virtual register number, indication
247     /// that the physical register is currently allocated to a virtual
248     /// register. In that case, LiveVirtRegs contains the inverse mapping.
249   };
250 
251   /// Maps each physical register to a RegUnitState enum or virtual register.
252   std::vector<unsigned> RegUnitStates;
253 
254   SmallVector<MachineInstr *, 32> Coalesced;
255 
256   /// Track register units that are used in the current instruction, and so
257   /// cannot be allocated.
258   ///
259   /// In the first phase (tied defs/early clobber), we consider also physical
260   /// uses, afterwards, we don't. If the lowest bit isn't set, it's a solely
261   /// physical use (markPhysRegUsedInInstr), otherwise, it's a normal use. To
262   /// avoid resetting the entire vector after every instruction, we track the
263   /// instruction "generation" in the remaining 31 bits -- this means, that if
264   /// UsedInInstr[Idx] < InstrGen, the register unit is unused. InstrGen is
265   /// never zero and always incremented by two.
266   ///
267   /// Don't allocate inline storage: the number of register units is typically
268   /// quite large (e.g., AArch64 > 100, X86 > 200, AMDGPU > 1000).
269   uint32_t InstrGen;
270   SmallVector<unsigned, 0> UsedInInstr;
271 
272   SmallVector<unsigned, 8> DefOperandIndexes;
273   // Register masks attached to the current instruction.
274   SmallVector<const uint32_t *> RegMasks;
275 
276   // Assign index for each instruction to quickly determine dominance.
277   InstrPosIndexes PosIndexes;
278 
279   void setPhysRegState(MCRegister PhysReg, unsigned NewState);
280   bool isPhysRegFree(MCPhysReg PhysReg) const;
281 
282   /// Mark a physreg as used in this instruction.
283   void markRegUsedInInstr(MCPhysReg PhysReg) {
284     for (MCRegUnit Unit : TRI->regunits(PhysReg))
285       UsedInInstr[Unit] = InstrGen | 1;
286   }
287 
288   // Check if physreg is clobbered by instruction's regmask(s).
289   bool isClobberedByRegMasks(MCPhysReg PhysReg) const {
290     return llvm::any_of(RegMasks, [PhysReg](const uint32_t *Mask) {
291       return MachineOperand::clobbersPhysReg(Mask, PhysReg);
292     });
293   }
294 
295   /// Check if a physreg or any of its aliases are used in this instruction.
296   bool isRegUsedInInstr(MCPhysReg PhysReg, bool LookAtPhysRegUses) const {
297     if (LookAtPhysRegUses && isClobberedByRegMasks(PhysReg))
298       return true;
299     for (MCRegUnit Unit : TRI->regunits(PhysReg))
300       if (UsedInInstr[Unit] >= (InstrGen | !LookAtPhysRegUses))
301         return true;
302     return false;
303   }
304 
305   /// Mark physical register as being used in a register use operand.
306   /// This is only used by the special livethrough handling code.
307   void markPhysRegUsedInInstr(MCPhysReg PhysReg) {
308     for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
309       assert(UsedInInstr[Unit] <= InstrGen && "non-phys use before phys use?");
310       UsedInInstr[Unit] = InstrGen;
311     }
312   }
313 
314   /// Remove mark of physical register being used in the instruction.
315   void unmarkRegUsedInInstr(MCPhysReg PhysReg) {
316     for (MCRegUnit Unit : TRI->regunits(PhysReg))
317       UsedInInstr[Unit] = 0;
318   }
319 
320   enum : unsigned {
321     spillClean = 50,
322     spillDirty = 100,
323     spillPrefBonus = 20,
324     spillImpossible = ~0u
325   };
326 
327 public:
328   bool ClearVirtRegs;
329 
330   bool runOnMachineFunction(MachineFunction &MF);
331 
332 private:
333   void allocateBasicBlock(MachineBasicBlock &MBB);
334 
335   void addRegClassDefCounts(MutableArrayRef<unsigned> RegClassDefCounts,
336                             Register Reg) const;
337 
338   void findAndSortDefOperandIndexes(const MachineInstr &MI);
339 
340   void allocateInstruction(MachineInstr &MI);
341   void handleDebugValue(MachineInstr &MI);
342   void handleBundle(MachineInstr &MI);
343 
344   bool usePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
345   bool definePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
346   bool displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg);
347   void freePhysReg(MCPhysReg PhysReg);
348 
349   unsigned calcSpillCost(MCPhysReg PhysReg) const;
350 
351   LiveRegMap::iterator findLiveVirtReg(Register VirtReg) {
352     return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
353   }
354 
355   LiveRegMap::const_iterator findLiveVirtReg(Register VirtReg) const {
356     return LiveVirtRegs.find(Register::virtReg2Index(VirtReg));
357   }
358 
359   void assignVirtToPhysReg(MachineInstr &MI, LiveReg &, MCPhysReg PhysReg);
360   void allocVirtReg(MachineInstr &MI, LiveReg &LR, Register Hint,
361                     bool LookAtPhysRegUses = false);
362   void allocVirtRegUndef(MachineOperand &MO);
363   void assignDanglingDebugValues(MachineInstr &Def, Register VirtReg,
364                                  MCPhysReg Reg);
365   bool defineLiveThroughVirtReg(MachineInstr &MI, unsigned OpNum,
366                                 Register VirtReg);
367   bool defineVirtReg(MachineInstr &MI, unsigned OpNum, Register VirtReg,
368                      bool LookAtPhysRegUses = false);
369   bool useVirtReg(MachineInstr &MI, MachineOperand &MO, Register VirtReg);
370 
371   MCPhysReg getErrorAssignment(const LiveReg &LR, MachineInstr &MI,
372                                const TargetRegisterClass &RC);
373 
374   MachineBasicBlock::iterator
375   getMBBBeginInsertionPoint(MachineBasicBlock &MBB,
376                             SmallSet<Register, 2> &PrologLiveIns) const;
377 
378   void reloadAtBegin(MachineBasicBlock &MBB);
379   bool setPhysReg(MachineInstr &MI, MachineOperand &MO, MCPhysReg PhysReg);
380 
381   Register traceCopies(Register VirtReg) const;
382   Register traceCopyChain(Register Reg) const;
383 
384   bool shouldAllocateRegister(const Register Reg) const;
385   int getStackSpaceFor(Register VirtReg);
386   void spill(MachineBasicBlock::iterator Before, Register VirtReg,
387              MCPhysReg AssignedReg, bool Kill, bool LiveOut);
388   void reload(MachineBasicBlock::iterator Before, Register VirtReg,
389               MCPhysReg PhysReg);
390 
391   bool mayLiveOut(Register VirtReg);
392   bool mayLiveIn(Register VirtReg);
393 
394   void dumpState() const;
395 };
396 
397 class RegAllocFast : public MachineFunctionPass {
398   RegAllocFastImpl Impl;
399 
400 public:
401   static char ID;
402 
403   RegAllocFast(const RegAllocFilterFunc F = nullptr, bool ClearVirtRegs_ = true)
404       : MachineFunctionPass(ID), Impl(F, ClearVirtRegs_) {}
405 
406   bool runOnMachineFunction(MachineFunction &MF) override {
407     return Impl.runOnMachineFunction(MF);
408   }
409 
410   StringRef getPassName() const override { return "Fast Register Allocator"; }
411 
412   void getAnalysisUsage(AnalysisUsage &AU) const override {
413     AU.setPreservesCFG();
414     MachineFunctionPass::getAnalysisUsage(AU);
415   }
416 
417   MachineFunctionProperties getRequiredProperties() const override {
418     return MachineFunctionProperties().set(
419         MachineFunctionProperties::Property::NoPHIs);
420   }
421 
422   MachineFunctionProperties getSetProperties() const override {
423     if (Impl.ClearVirtRegs) {
424       return MachineFunctionProperties().set(
425           MachineFunctionProperties::Property::NoVRegs);
426     }
427 
428     return MachineFunctionProperties();
429   }
430 
431   MachineFunctionProperties getClearedProperties() const override {
432     return MachineFunctionProperties().set(
433         MachineFunctionProperties::Property::IsSSA);
434   }
435 };
436 
437 } // end anonymous namespace
438 
439 char RegAllocFast::ID = 0;
440 
441 INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false,
442                 false)
443 
444 bool RegAllocFastImpl::shouldAllocateRegister(const Register Reg) const {
445   assert(Reg.isVirtual());
446   if (!ShouldAllocateRegisterImpl)
447     return true;
448 
449   return ShouldAllocateRegisterImpl(*TRI, *MRI, Reg);
450 }
451 
452 void RegAllocFastImpl::setPhysRegState(MCRegister PhysReg, unsigned NewState) {
453   for (MCRegUnit Unit : TRI->regunits(PhysReg))
454     RegUnitStates[Unit] = NewState;
455 }
456 
457 bool RegAllocFastImpl::isPhysRegFree(MCPhysReg PhysReg) const {
458   for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
459     if (RegUnitStates[Unit] != regFree)
460       return false;
461   }
462   return true;
463 }
464 
465 /// This allocates space for the specified virtual register to be held on the
466 /// stack.
467 int RegAllocFastImpl::getStackSpaceFor(Register VirtReg) {
468   // Find the location Reg would belong...
469   int SS = StackSlotForVirtReg[VirtReg];
470   // Already has space allocated?
471   if (SS != -1)
472     return SS;
473 
474   // Allocate a new stack object for this spill location...
475   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
476   unsigned Size = TRI->getSpillSize(RC);
477   Align Alignment = TRI->getSpillAlign(RC);
478   int FrameIdx = MFI->CreateSpillStackObject(Size, Alignment);
479 
480   // Assign the slot.
481   StackSlotForVirtReg[VirtReg] = FrameIdx;
482   return FrameIdx;
483 }
484 
485 static bool dominates(InstrPosIndexes &PosIndexes, const MachineInstr &A,
486                       const MachineInstr &B) {
487   uint64_t IndexA, IndexB;
488   PosIndexes.getIndex(A, IndexA);
489   if (LLVM_UNLIKELY(PosIndexes.getIndex(B, IndexB)))
490     PosIndexes.getIndex(A, IndexA);
491   return IndexA < IndexB;
492 }
493 
494 /// Returns false if \p VirtReg is known to not live out of the current block.
495 bool RegAllocFastImpl::mayLiveOut(Register VirtReg) {
496   if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg))) {
497     // Cannot be live-out if there are no successors.
498     return !MBB->succ_empty();
499   }
500 
501   const MachineInstr *SelfLoopDef = nullptr;
502 
503   // If this block loops back to itself, it is necessary to check whether the
504   // use comes after the def.
505   if (MBB->isSuccessor(MBB)) {
506     // Find the first def in the self loop MBB.
507     for (const MachineInstr &DefInst : MRI->def_instructions(VirtReg)) {
508       if (DefInst.getParent() != MBB) {
509         MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
510         return true;
511       } else {
512         if (!SelfLoopDef || dominates(PosIndexes, DefInst, *SelfLoopDef))
513           SelfLoopDef = &DefInst;
514       }
515     }
516     if (!SelfLoopDef) {
517       MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
518       return true;
519     }
520   }
521 
522   // See if the first \p Limit uses of the register are all in the current
523   // block.
524   static const unsigned Limit = 8;
525   unsigned C = 0;
526   for (const MachineInstr &UseInst : MRI->use_nodbg_instructions(VirtReg)) {
527     if (UseInst.getParent() != MBB || ++C >= Limit) {
528       MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
529       // Cannot be live-out if there are no successors.
530       return !MBB->succ_empty();
531     }
532 
533     if (SelfLoopDef) {
534       // Try to handle some simple cases to avoid spilling and reloading every
535       // value inside a self looping block.
536       if (SelfLoopDef == &UseInst ||
537           !dominates(PosIndexes, *SelfLoopDef, UseInst)) {
538         MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
539         return true;
540       }
541     }
542   }
543 
544   return false;
545 }
546 
547 /// Returns false if \p VirtReg is known to not be live into the current block.
548 bool RegAllocFastImpl::mayLiveIn(Register VirtReg) {
549   if (MayLiveAcrossBlocks.test(Register::virtReg2Index(VirtReg)))
550     return !MBB->pred_empty();
551 
552   // See if the first \p Limit def of the register are all in the current block.
553   static const unsigned Limit = 8;
554   unsigned C = 0;
555   for (const MachineInstr &DefInst : MRI->def_instructions(VirtReg)) {
556     if (DefInst.getParent() != MBB || ++C >= Limit) {
557       MayLiveAcrossBlocks.set(Register::virtReg2Index(VirtReg));
558       return !MBB->pred_empty();
559     }
560   }
561 
562   return false;
563 }
564 
565 /// Insert spill instruction for \p AssignedReg before \p Before. Update
566 /// DBG_VALUEs with \p VirtReg operands with the stack slot.
567 void RegAllocFastImpl::spill(MachineBasicBlock::iterator Before,
568                              Register VirtReg, MCPhysReg AssignedReg, bool Kill,
569                              bool LiveOut) {
570   LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI) << " in "
571                     << printReg(AssignedReg, TRI));
572   int FI = getStackSpaceFor(VirtReg);
573   LLVM_DEBUG(dbgs() << " to stack slot #" << FI << '\n');
574 
575   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
576   TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI,
577                            VirtReg);
578   ++NumStores;
579 
580   MachineBasicBlock::iterator FirstTerm = MBB->getFirstTerminator();
581 
582   // When we spill a virtual register, we will have spill instructions behind
583   // every definition of it, meaning we can switch all the DBG_VALUEs over
584   // to just reference the stack slot.
585   SmallVectorImpl<MachineOperand *> &LRIDbgOperands = LiveDbgValueMap[VirtReg];
586   SmallMapVector<MachineInstr *, SmallVector<const MachineOperand *>, 2>
587       SpilledOperandsMap;
588   for (MachineOperand *MO : LRIDbgOperands)
589     SpilledOperandsMap[MO->getParent()].push_back(MO);
590   for (const auto &MISpilledOperands : SpilledOperandsMap) {
591     MachineInstr &DBG = *MISpilledOperands.first;
592     // We don't have enough support for tracking operands of DBG_VALUE_LISTs.
593     if (DBG.isDebugValueList())
594       continue;
595     MachineInstr *NewDV = buildDbgValueForSpill(
596         *MBB, Before, *MISpilledOperands.first, FI, MISpilledOperands.second);
597     assert(NewDV->getParent() == MBB && "dangling parent pointer");
598     (void)NewDV;
599     LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV);
600 
601     if (LiveOut) {
602       // We need to insert a DBG_VALUE at the end of the block if the spill slot
603       // is live out, but there is another use of the value after the
604       // spill. This will allow LiveDebugValues to see the correct live out
605       // value to propagate to the successors.
606       MachineInstr *ClonedDV = MBB->getParent()->CloneMachineInstr(NewDV);
607       MBB->insert(FirstTerm, ClonedDV);
608       LLVM_DEBUG(dbgs() << "Cloning debug info due to live out spill\n");
609     }
610 
611     // Rewrite unassigned dbg_values to use the stack slot.
612     // TODO We can potentially do this for list debug values as well if we know
613     // how the dbg_values are getting unassigned.
614     if (DBG.isNonListDebugValue()) {
615       MachineOperand &MO = DBG.getDebugOperand(0);
616       if (MO.isReg() && MO.getReg() == 0) {
617         updateDbgValueForSpill(DBG, FI, 0);
618       }
619     }
620   }
621   // Now this register is spilled there is should not be any DBG_VALUE
622   // pointing to this register because they are all pointing to spilled value
623   // now.
624   LRIDbgOperands.clear();
625 }
626 
627 /// Insert reload instruction for \p PhysReg before \p Before.
628 void RegAllocFastImpl::reload(MachineBasicBlock::iterator Before,
629                               Register VirtReg, MCPhysReg PhysReg) {
630   LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into "
631                     << printReg(PhysReg, TRI) << '\n');
632   int FI = getStackSpaceFor(VirtReg);
633   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
634   TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI, VirtReg);
635   ++NumLoads;
636 }
637 
638 /// Get basic block begin insertion point.
639 /// This is not just MBB.begin() because surprisingly we have EH_LABEL
640 /// instructions marking the begin of a basic block. This means we must insert
641 /// new instructions after such labels...
642 MachineBasicBlock::iterator RegAllocFastImpl::getMBBBeginInsertionPoint(
643     MachineBasicBlock &MBB, SmallSet<Register, 2> &PrologLiveIns) const {
644   MachineBasicBlock::iterator I = MBB.begin();
645   while (I != MBB.end()) {
646     if (I->isLabel()) {
647       ++I;
648       continue;
649     }
650 
651     // Most reloads should be inserted after prolog instructions.
652     if (!TII->isBasicBlockPrologue(*I))
653       break;
654 
655     // However if a prolog instruction reads a register that needs to be
656     // reloaded, the reload should be inserted before the prolog.
657     for (MachineOperand &MO : I->operands()) {
658       if (MO.isReg())
659         PrologLiveIns.insert(MO.getReg());
660     }
661 
662     ++I;
663   }
664 
665   return I;
666 }
667 
668 /// Reload all currently assigned virtual registers.
669 void RegAllocFastImpl::reloadAtBegin(MachineBasicBlock &MBB) {
670   if (LiveVirtRegs.empty())
671     return;
672 
673   for (MachineBasicBlock::RegisterMaskPair P : MBB.liveins()) {
674     MCRegister Reg = P.PhysReg;
675     // Set state to live-in. This possibly overrides mappings to virtual
676     // registers but we don't care anymore at this point.
677     setPhysRegState(Reg, regLiveIn);
678   }
679 
680   SmallSet<Register, 2> PrologLiveIns;
681 
682   // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
683   // of spilling here is deterministic, if arbitrary.
684   MachineBasicBlock::iterator InsertBefore =
685       getMBBBeginInsertionPoint(MBB, PrologLiveIns);
686   for (const LiveReg &LR : LiveVirtRegs) {
687     MCPhysReg PhysReg = LR.PhysReg;
688     if (PhysReg == 0 || LR.Error)
689       continue;
690 
691     MCRegUnit FirstUnit = *TRI->regunits(PhysReg).begin();
692     if (RegUnitStates[FirstUnit] == regLiveIn)
693       continue;
694 
695     assert((&MBB != &MBB.getParent()->front() || IgnoreMissingDefs) &&
696            "no reload in start block. Missing vreg def?");
697 
698     if (PrologLiveIns.count(PhysReg)) {
699       // FIXME: Theoretically this should use an insert point skipping labels
700       // but I'm not sure how labels should interact with prolog instruction
701       // that need reloads.
702       reload(MBB.begin(), LR.VirtReg, PhysReg);
703     } else
704       reload(InsertBefore, LR.VirtReg, PhysReg);
705   }
706   LiveVirtRegs.clear();
707 }
708 
709 /// Handle the direct use of a physical register.  Check that the register is
710 /// not used by a virtreg. Kill the physreg, marking it free. This may add
711 /// implicit kills to MO->getParent() and invalidate MO.
712 bool RegAllocFastImpl::usePhysReg(MachineInstr &MI, MCPhysReg Reg) {
713   assert(Register::isPhysicalRegister(Reg) && "expected physreg");
714   bool displacedAny = displacePhysReg(MI, Reg);
715   setPhysRegState(Reg, regPreAssigned);
716   markRegUsedInInstr(Reg);
717   return displacedAny;
718 }
719 
720 bool RegAllocFastImpl::definePhysReg(MachineInstr &MI, MCPhysReg Reg) {
721   bool displacedAny = displacePhysReg(MI, Reg);
722   setPhysRegState(Reg, regPreAssigned);
723   return displacedAny;
724 }
725 
726 /// Mark PhysReg as reserved or free after spilling any virtregs. This is very
727 /// similar to defineVirtReg except the physreg is reserved instead of
728 /// allocated.
729 bool RegAllocFastImpl::displacePhysReg(MachineInstr &MI, MCPhysReg PhysReg) {
730   bool displacedAny = false;
731 
732   for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
733     switch (unsigned VirtReg = RegUnitStates[Unit]) {
734     default: {
735       LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
736       assert(LRI != LiveVirtRegs.end() && "datastructures in sync");
737       MachineBasicBlock::iterator ReloadBefore =
738           std::next((MachineBasicBlock::iterator)MI.getIterator());
739       reload(ReloadBefore, VirtReg, LRI->PhysReg);
740 
741       setPhysRegState(LRI->PhysReg, regFree);
742       LRI->PhysReg = 0;
743       LRI->Reloaded = true;
744       displacedAny = true;
745       break;
746     }
747     case regPreAssigned:
748       RegUnitStates[Unit] = regFree;
749       displacedAny = true;
750       break;
751     case regFree:
752       break;
753     }
754   }
755   return displacedAny;
756 }
757 
758 void RegAllocFastImpl::freePhysReg(MCPhysReg PhysReg) {
759   LLVM_DEBUG(dbgs() << "Freeing " << printReg(PhysReg, TRI) << ':');
760 
761   MCRegUnit FirstUnit = *TRI->regunits(PhysReg).begin();
762   switch (unsigned VirtReg = RegUnitStates[FirstUnit]) {
763   case regFree:
764     LLVM_DEBUG(dbgs() << '\n');
765     return;
766   case regPreAssigned:
767     LLVM_DEBUG(dbgs() << '\n');
768     setPhysRegState(PhysReg, regFree);
769     return;
770   default: {
771     LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
772     assert(LRI != LiveVirtRegs.end());
773     LLVM_DEBUG(dbgs() << ' ' << printReg(LRI->VirtReg, TRI) << '\n');
774     setPhysRegState(LRI->PhysReg, regFree);
775     LRI->PhysReg = 0;
776   }
777     return;
778   }
779 }
780 
781 /// Return the cost of spilling clearing out PhysReg and aliases so it is free
782 /// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
783 /// disabled - it can be allocated directly.
784 /// \returns spillImpossible when PhysReg or an alias can't be spilled.
785 unsigned RegAllocFastImpl::calcSpillCost(MCPhysReg PhysReg) const {
786   for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
787     switch (unsigned VirtReg = RegUnitStates[Unit]) {
788     case regFree:
789       break;
790     case regPreAssigned:
791       LLVM_DEBUG(dbgs() << "Cannot spill pre-assigned "
792                         << printReg(PhysReg, TRI) << '\n');
793       return spillImpossible;
794     default: {
795       bool SureSpill = StackSlotForVirtReg[VirtReg] != -1 ||
796                        findLiveVirtReg(VirtReg)->LiveOut;
797       return SureSpill ? spillClean : spillDirty;
798     }
799     }
800   }
801   return 0;
802 }
803 
804 void RegAllocFastImpl::assignDanglingDebugValues(MachineInstr &Definition,
805                                                  Register VirtReg,
806                                                  MCPhysReg Reg) {
807   auto UDBGValIter = DanglingDbgValues.find(VirtReg);
808   if (UDBGValIter == DanglingDbgValues.end())
809     return;
810 
811   SmallVectorImpl<MachineInstr *> &Dangling = UDBGValIter->second;
812   for (MachineInstr *DbgValue : Dangling) {
813     assert(DbgValue->isDebugValue());
814     if (!DbgValue->hasDebugOperandForReg(VirtReg))
815       continue;
816 
817     // Test whether the physreg survives from the definition to the DBG_VALUE.
818     MCPhysReg SetToReg = Reg;
819     unsigned Limit = 20;
820     for (MachineBasicBlock::iterator I = std::next(Definition.getIterator()),
821                                      E = DbgValue->getIterator();
822          I != E; ++I) {
823       if (I->modifiesRegister(Reg, TRI) || --Limit == 0) {
824         LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
825                           << '\n');
826         SetToReg = 0;
827         break;
828       }
829     }
830     for (MachineOperand &MO : DbgValue->getDebugOperandsForReg(VirtReg)) {
831       MO.setReg(SetToReg);
832       if (SetToReg != 0)
833         MO.setIsRenamable();
834     }
835   }
836   Dangling.clear();
837 }
838 
839 /// This method updates local state so that we know that PhysReg is the
840 /// proper container for VirtReg now.  The physical register must not be used
841 /// for anything else when this is called.
842 void RegAllocFastImpl::assignVirtToPhysReg(MachineInstr &AtMI, LiveReg &LR,
843                                            MCPhysReg PhysReg) {
844   Register VirtReg = LR.VirtReg;
845   LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to "
846                     << printReg(PhysReg, TRI) << '\n');
847   assert(LR.PhysReg == 0 && "Already assigned a physreg");
848   assert(PhysReg != 0 && "Trying to assign no register");
849   LR.PhysReg = PhysReg;
850   setPhysRegState(PhysReg, VirtReg);
851 
852   assignDanglingDebugValues(AtMI, VirtReg, PhysReg);
853 }
854 
855 static bool isCoalescable(const MachineInstr &MI) { return MI.isFullCopy(); }
856 
857 Register RegAllocFastImpl::traceCopyChain(Register Reg) const {
858   static const unsigned ChainLengthLimit = 3;
859   unsigned C = 0;
860   do {
861     if (Reg.isPhysical())
862       return Reg;
863     assert(Reg.isVirtual());
864 
865     MachineInstr *VRegDef = MRI->getUniqueVRegDef(Reg);
866     if (!VRegDef || !isCoalescable(*VRegDef))
867       return 0;
868     Reg = VRegDef->getOperand(1).getReg();
869   } while (++C <= ChainLengthLimit);
870   return 0;
871 }
872 
873 /// Check if any of \p VirtReg's definitions is a copy. If it is follow the
874 /// chain of copies to check whether we reach a physical register we can
875 /// coalesce with.
876 Register RegAllocFastImpl::traceCopies(Register VirtReg) const {
877   static const unsigned DefLimit = 3;
878   unsigned C = 0;
879   for (const MachineInstr &MI : MRI->def_instructions(VirtReg)) {
880     if (isCoalescable(MI)) {
881       Register Reg = MI.getOperand(1).getReg();
882       Reg = traceCopyChain(Reg);
883       if (Reg.isValid())
884         return Reg;
885     }
886 
887     if (++C >= DefLimit)
888       break;
889   }
890   return Register();
891 }
892 
893 /// Allocates a physical register for VirtReg.
894 void RegAllocFastImpl::allocVirtReg(MachineInstr &MI, LiveReg &LR,
895                                     Register Hint0, bool LookAtPhysRegUses) {
896   const Register VirtReg = LR.VirtReg;
897   assert(LR.PhysReg == 0);
898 
899   const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
900   LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg)
901                     << " in class " << TRI->getRegClassName(&RC)
902                     << " with hint " << printReg(Hint0, TRI) << '\n');
903 
904   // Take hint when possible.
905   if (Hint0.isPhysical() && MRI->isAllocatable(Hint0) && RC.contains(Hint0) &&
906       !isRegUsedInInstr(Hint0, LookAtPhysRegUses)) {
907     // Take hint if the register is currently free.
908     if (isPhysRegFree(Hint0)) {
909       LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0, TRI)
910                         << '\n');
911       assignVirtToPhysReg(MI, LR, Hint0);
912       return;
913     } else {
914       LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint0, TRI)
915                         << " occupied\n");
916     }
917   } else {
918     Hint0 = Register();
919   }
920 
921   // Try other hint.
922   Register Hint1 = traceCopies(VirtReg);
923   if (Hint1.isPhysical() && MRI->isAllocatable(Hint1) && RC.contains(Hint1) &&
924       !isRegUsedInInstr(Hint1, LookAtPhysRegUses)) {
925     // Take hint if the register is currently free.
926     if (isPhysRegFree(Hint1)) {
927       LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1, TRI)
928                         << '\n');
929       assignVirtToPhysReg(MI, LR, Hint1);
930       return;
931     } else {
932       LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint1, TRI)
933                         << " occupied\n");
934     }
935   } else {
936     Hint1 = Register();
937   }
938 
939   MCPhysReg BestReg = 0;
940   unsigned BestCost = spillImpossible;
941   ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
942   for (MCPhysReg PhysReg : AllocationOrder) {
943     LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' ');
944     if (isRegUsedInInstr(PhysReg, LookAtPhysRegUses)) {
945       LLVM_DEBUG(dbgs() << "already used in instr.\n");
946       continue;
947     }
948 
949     unsigned Cost = calcSpillCost(PhysReg);
950     LLVM_DEBUG(dbgs() << "Cost: " << Cost << " BestCost: " << BestCost << '\n');
951     // Immediate take a register with cost 0.
952     if (Cost == 0) {
953       assignVirtToPhysReg(MI, LR, PhysReg);
954       return;
955     }
956 
957     if (PhysReg == Hint0 || PhysReg == Hint1)
958       Cost -= spillPrefBonus;
959 
960     if (Cost < BestCost) {
961       BestReg = PhysReg;
962       BestCost = Cost;
963     }
964   }
965 
966   if (!BestReg) {
967     // Nothing we can do: Report an error and keep going with an invalid
968     // allocation.
969     LR.PhysReg = getErrorAssignment(LR, MI, RC);
970     LR.Error = true;
971     return;
972   }
973 
974   displacePhysReg(MI, BestReg);
975   assignVirtToPhysReg(MI, LR, BestReg);
976 }
977 
978 void RegAllocFastImpl::allocVirtRegUndef(MachineOperand &MO) {
979   assert(MO.isUndef() && "expected undef use");
980   Register VirtReg = MO.getReg();
981   assert(VirtReg.isVirtual() && "Expected virtreg");
982   if (!shouldAllocateRegister(VirtReg))
983     return;
984 
985   LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
986   MCPhysReg PhysReg;
987   if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
988     PhysReg = LRI->PhysReg;
989   } else {
990     const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
991     ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
992     if (AllocationOrder.empty()) {
993       // All registers in the class were reserved.
994       //
995       // It might be OK to take any entry from the class as this is an undef
996       // use, but accepting this would give different behavior than greedy and
997       // basic.
998       PhysReg = getErrorAssignment(*LRI, *MO.getParent(), RC);
999       LRI->Error = true;
1000     } else
1001       PhysReg = AllocationOrder.front();
1002   }
1003 
1004   unsigned SubRegIdx = MO.getSubReg();
1005   if (SubRegIdx != 0) {
1006     PhysReg = TRI->getSubReg(PhysReg, SubRegIdx);
1007     MO.setSubReg(0);
1008   }
1009   MO.setReg(PhysReg);
1010   MO.setIsRenamable(true);
1011 }
1012 
1013 /// Variation of defineVirtReg() with special handling for livethrough regs
1014 /// (tied or earlyclobber) that may interfere with preassigned uses.
1015 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1016 bool RegAllocFastImpl::defineLiveThroughVirtReg(MachineInstr &MI,
1017                                                 unsigned OpNum,
1018                                                 Register VirtReg) {
1019   if (!shouldAllocateRegister(VirtReg))
1020     return false;
1021   LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
1022   if (LRI != LiveVirtRegs.end()) {
1023     MCPhysReg PrevReg = LRI->PhysReg;
1024     if (PrevReg != 0 && isRegUsedInInstr(PrevReg, true)) {
1025       LLVM_DEBUG(dbgs() << "Need new assignment for " << printReg(PrevReg, TRI)
1026                         << " (tied/earlyclobber resolution)\n");
1027       freePhysReg(PrevReg);
1028       LRI->PhysReg = 0;
1029       allocVirtReg(MI, *LRI, 0, true);
1030       MachineBasicBlock::iterator InsertBefore =
1031           std::next((MachineBasicBlock::iterator)MI.getIterator());
1032       LLVM_DEBUG(dbgs() << "Copy " << printReg(LRI->PhysReg, TRI) << " to "
1033                         << printReg(PrevReg, TRI) << '\n');
1034       BuildMI(*MBB, InsertBefore, MI.getDebugLoc(),
1035               TII->get(TargetOpcode::COPY), PrevReg)
1036           .addReg(LRI->PhysReg, llvm::RegState::Kill);
1037     }
1038     MachineOperand &MO = MI.getOperand(OpNum);
1039     if (MO.getSubReg() && !MO.isUndef()) {
1040       LRI->LastUse = &MI;
1041     }
1042   }
1043   return defineVirtReg(MI, OpNum, VirtReg, true);
1044 }
1045 
1046 /// Allocates a register for VirtReg definition. Typically the register is
1047 /// already assigned from a use of the virtreg, however we still need to
1048 /// perform an allocation if:
1049 /// - It is a dead definition without any uses.
1050 /// - The value is live out and all uses are in different basic blocks.
1051 ///
1052 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1053 bool RegAllocFastImpl::defineVirtReg(MachineInstr &MI, unsigned OpNum,
1054                                      Register VirtReg, bool LookAtPhysRegUses) {
1055   assert(VirtReg.isVirtual() && "Not a virtual register");
1056   if (!shouldAllocateRegister(VirtReg))
1057     return false;
1058   MachineOperand &MO = MI.getOperand(OpNum);
1059   LiveRegMap::iterator LRI;
1060   bool New;
1061   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
1062   if (New) {
1063     if (!MO.isDead()) {
1064       if (mayLiveOut(VirtReg)) {
1065         LRI->LiveOut = true;
1066       } else {
1067         // It is a dead def without the dead flag; add the flag now.
1068         MO.setIsDead(true);
1069       }
1070     }
1071   }
1072   if (LRI->PhysReg == 0) {
1073     allocVirtReg(MI, *LRI, 0, LookAtPhysRegUses);
1074   } else {
1075     assert((!isRegUsedInInstr(LRI->PhysReg, LookAtPhysRegUses) || LRI->Error) &&
1076            "TODO: preassign mismatch");
1077     LLVM_DEBUG(dbgs() << "In def of " << printReg(VirtReg, TRI)
1078                       << " use existing assignment to "
1079                       << printReg(LRI->PhysReg, TRI) << '\n');
1080   }
1081 
1082   MCPhysReg PhysReg = LRI->PhysReg;
1083   if (LRI->Reloaded || LRI->LiveOut) {
1084     if (!MI.isImplicitDef()) {
1085       MachineBasicBlock::iterator SpillBefore =
1086           std::next((MachineBasicBlock::iterator)MI.getIterator());
1087       LLVM_DEBUG(dbgs() << "Spill Reason: LO: " << LRI->LiveOut
1088                         << " RL: " << LRI->Reloaded << '\n');
1089       bool Kill = LRI->LastUse == nullptr;
1090       spill(SpillBefore, VirtReg, PhysReg, Kill, LRI->LiveOut);
1091 
1092       // We need to place additional spills for each indirect destination of an
1093       // INLINEASM_BR.
1094       if (MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
1095         int FI = StackSlotForVirtReg[VirtReg];
1096         const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
1097         for (MachineOperand &MO : MI.operands()) {
1098           if (MO.isMBB()) {
1099             MachineBasicBlock *Succ = MO.getMBB();
1100             TII->storeRegToStackSlot(*Succ, Succ->begin(), PhysReg, Kill, FI,
1101                                      &RC, TRI, VirtReg);
1102             ++NumStores;
1103             Succ->addLiveIn(PhysReg);
1104           }
1105         }
1106       }
1107 
1108       LRI->LastUse = nullptr;
1109     }
1110     LRI->LiveOut = false;
1111     LRI->Reloaded = false;
1112   }
1113   if (MI.getOpcode() == TargetOpcode::BUNDLE) {
1114     BundleVirtRegsMap[VirtReg] = PhysReg;
1115   }
1116   markRegUsedInInstr(PhysReg);
1117   return setPhysReg(MI, MO, PhysReg);
1118 }
1119 
1120 /// Allocates a register for a VirtReg use.
1121 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1122 bool RegAllocFastImpl::useVirtReg(MachineInstr &MI, MachineOperand &MO,
1123                                   Register VirtReg) {
1124   assert(VirtReg.isVirtual() && "Not a virtual register");
1125   if (!shouldAllocateRegister(VirtReg))
1126     return false;
1127   LiveRegMap::iterator LRI;
1128   bool New;
1129   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
1130   if (New) {
1131     if (!MO.isKill()) {
1132       if (mayLiveOut(VirtReg)) {
1133         LRI->LiveOut = true;
1134       } else {
1135         // It is a last (killing) use without the kill flag; add the flag now.
1136         MO.setIsKill(true);
1137       }
1138     }
1139   } else {
1140     assert((!MO.isKill() || LRI->LastUse == &MI) && "Invalid kill flag");
1141   }
1142 
1143   // If necessary allocate a register.
1144   if (LRI->PhysReg == 0) {
1145     assert(!MO.isTied() && "tied op should be allocated");
1146     Register Hint;
1147     if (MI.isCopy() && MI.getOperand(1).getSubReg() == 0) {
1148       Hint = MI.getOperand(0).getReg();
1149       if (Hint.isVirtual()) {
1150         assert(!shouldAllocateRegister(Hint));
1151         Hint = Register();
1152       } else {
1153         assert(Hint.isPhysical() &&
1154                "Copy destination should already be assigned");
1155       }
1156     }
1157     allocVirtReg(MI, *LRI, Hint, false);
1158   }
1159 
1160   LRI->LastUse = &MI;
1161 
1162   if (MI.getOpcode() == TargetOpcode::BUNDLE) {
1163     BundleVirtRegsMap[VirtReg] = LRI->PhysReg;
1164   }
1165   markRegUsedInInstr(LRI->PhysReg);
1166   return setPhysReg(MI, MO, LRI->PhysReg);
1167 }
1168 
1169 /// Query a physical register to use as a filler in contexts where the
1170 /// allocation has failed. This will raise an error, but not abort the
1171 /// compilation.
1172 MCPhysReg RegAllocFastImpl::getErrorAssignment(const LiveReg &LR,
1173                                                MachineInstr &MI,
1174                                                const TargetRegisterClass &RC) {
1175   MachineFunction &MF = *MI.getMF();
1176 
1177   // Avoid repeating the error every time a register is used.
1178   bool EmitError = !MF.getProperties().hasProperty(
1179       MachineFunctionProperties::Property::FailedRegAlloc);
1180   if (EmitError)
1181     MF.getProperties().set(MachineFunctionProperties::Property::FailedRegAlloc);
1182 
1183   // If the allocation order was empty, all registers in the class were
1184   // probably reserved. Fall back to taking the first register in the class,
1185   // even if it's reserved.
1186   ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
1187   if (AllocationOrder.empty()) {
1188     const Function &Fn = MF.getFunction();
1189     if (EmitError) {
1190       Fn.getContext().diagnose(DiagnosticInfoRegAllocFailure(
1191           "no registers from class available to allocate", Fn,
1192           MI.getDebugLoc()));
1193     }
1194 
1195     ArrayRef<MCPhysReg> RawRegs = RC.getRegisters();
1196     assert(!RawRegs.empty() && "register classes cannot have no registers");
1197     return RawRegs.front();
1198   }
1199 
1200   if (!LR.Error && EmitError) {
1201     // Nothing we can do: Report an error and keep going with an invalid
1202     // allocation.
1203     if (MI.isInlineAsm()) {
1204       MI.emitInlineAsmError(
1205           "inline assembly requires more registers than available");
1206     } else {
1207       const Function &Fn = MBB->getParent()->getFunction();
1208       Fn.getContext().diagnose(DiagnosticInfoRegAllocFailure(
1209           "ran out of registers during register allocation", Fn,
1210           MI.getDebugLoc()));
1211     }
1212   }
1213 
1214   return AllocationOrder.front();
1215 }
1216 
1217 /// Changes operand OpNum in MI the refer the PhysReg, considering subregs.
1218 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1219 bool RegAllocFastImpl::setPhysReg(MachineInstr &MI, MachineOperand &MO,
1220                                   MCPhysReg PhysReg) {
1221   if (!MO.getSubReg()) {
1222     MO.setReg(PhysReg);
1223     MO.setIsRenamable(true);
1224     return false;
1225   }
1226 
1227   // Handle subregister index.
1228   MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : MCRegister());
1229   MO.setIsRenamable(true);
1230   // Note: We leave the subreg number around a little longer in case of defs.
1231   // This is so that the register freeing logic in allocateInstruction can still
1232   // recognize this as subregister defs. The code there will clear the number.
1233   if (!MO.isDef())
1234     MO.setSubReg(0);
1235 
1236   // A kill flag implies killing the full register. Add corresponding super
1237   // register kill.
1238   if (MO.isKill()) {
1239     MI.addRegisterKilled(PhysReg, TRI, true);
1240     // Conservatively assume implicit MOs were re-arranged
1241     return true;
1242   }
1243 
1244   // A <def,read-undef> of a sub-register requires an implicit def of the full
1245   // register.
1246   if (MO.isDef() && MO.isUndef()) {
1247     if (MO.isDead())
1248       MI.addRegisterDead(PhysReg, TRI, true);
1249     else
1250       MI.addRegisterDefined(PhysReg, TRI);
1251     // Conservatively assume implicit MOs were re-arranged
1252     return true;
1253   }
1254   return false;
1255 }
1256 
1257 #ifndef NDEBUG
1258 
1259 void RegAllocFastImpl::dumpState() const {
1260   for (unsigned Unit = 1, UnitE = TRI->getNumRegUnits(); Unit != UnitE;
1261        ++Unit) {
1262     switch (unsigned VirtReg = RegUnitStates[Unit]) {
1263     case regFree:
1264       break;
1265     case regPreAssigned:
1266       dbgs() << " " << printRegUnit(Unit, TRI) << "[P]";
1267       break;
1268     case regLiveIn:
1269       llvm_unreachable("Should not have regLiveIn in map");
1270     default: {
1271       dbgs() << ' ' << printRegUnit(Unit, TRI) << '=' << printReg(VirtReg);
1272       LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
1273       assert(I != LiveVirtRegs.end() && "have LiveVirtRegs entry");
1274       if (I->LiveOut || I->Reloaded) {
1275         dbgs() << '[';
1276         if (I->LiveOut)
1277           dbgs() << 'O';
1278         if (I->Reloaded)
1279           dbgs() << 'R';
1280         dbgs() << ']';
1281       }
1282       assert(TRI->hasRegUnit(I->PhysReg, Unit) && "inverse mapping present");
1283       break;
1284     }
1285     }
1286   }
1287   dbgs() << '\n';
1288   // Check that LiveVirtRegs is the inverse.
1289   for (const LiveReg &LR : LiveVirtRegs) {
1290     Register VirtReg = LR.VirtReg;
1291     assert(VirtReg.isVirtual() && "Bad map key");
1292     MCPhysReg PhysReg = LR.PhysReg;
1293     if (PhysReg != 0) {
1294       assert(Register::isPhysicalRegister(PhysReg) && "mapped to physreg");
1295       for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
1296         assert(RegUnitStates[Unit] == VirtReg && "inverse map valid");
1297       }
1298     }
1299   }
1300 }
1301 #endif
1302 
1303 /// Count number of defs consumed from each register class by \p Reg
1304 void RegAllocFastImpl::addRegClassDefCounts(
1305     MutableArrayRef<unsigned> RegClassDefCounts, Register Reg) const {
1306   assert(RegClassDefCounts.size() == TRI->getNumRegClasses());
1307 
1308   if (Reg.isVirtual()) {
1309     if (!shouldAllocateRegister(Reg))
1310       return;
1311     const TargetRegisterClass *OpRC = MRI->getRegClass(Reg);
1312     for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
1313          RCIdx != RCIdxEnd; ++RCIdx) {
1314       const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
1315       // FIXME: Consider aliasing sub/super registers.
1316       if (OpRC->hasSubClassEq(IdxRC))
1317         ++RegClassDefCounts[RCIdx];
1318     }
1319 
1320     return;
1321   }
1322 
1323   for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
1324        RCIdx != RCIdxEnd; ++RCIdx) {
1325     const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
1326     for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) {
1327       if (IdxRC->contains(*Alias)) {
1328         ++RegClassDefCounts[RCIdx];
1329         break;
1330       }
1331     }
1332   }
1333 }
1334 
1335 /// Compute \ref DefOperandIndexes so it contains the indices of "def" operands
1336 /// that are to be allocated. Those are ordered in a way that small classes,
1337 /// early clobbers and livethroughs are allocated first.
1338 void RegAllocFastImpl::findAndSortDefOperandIndexes(const MachineInstr &MI) {
1339   DefOperandIndexes.clear();
1340 
1341   LLVM_DEBUG(dbgs() << "Need to assign livethroughs\n");
1342   for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
1343     const MachineOperand &MO = MI.getOperand(I);
1344     if (!MO.isReg())
1345       continue;
1346     Register Reg = MO.getReg();
1347     if (MO.readsReg()) {
1348       if (Reg.isPhysical()) {
1349         LLVM_DEBUG(dbgs() << "mark extra used: " << printReg(Reg, TRI) << '\n');
1350         markPhysRegUsedInInstr(Reg);
1351       }
1352     }
1353 
1354     if (MO.isDef() && Reg.isVirtual() && shouldAllocateRegister(Reg))
1355       DefOperandIndexes.push_back(I);
1356   }
1357 
1358   // Most instructions only have one virtual def, so there's no point in
1359   // computing the possible number of defs for every register class.
1360   if (DefOperandIndexes.size() <= 1)
1361     return;
1362 
1363   // Track number of defs which may consume a register from the class. This is
1364   // used to assign registers for possibly-too-small classes first. Example:
1365   // defs are eax, 3 * gr32_abcd, 2 * gr32 => we want to assign the gr32_abcd
1366   // registers first so that the gr32 don't use the gr32_abcd registers before
1367   // we assign these.
1368   SmallVector<unsigned> RegClassDefCounts(TRI->getNumRegClasses(), 0);
1369 
1370   for (const MachineOperand &MO : MI.all_defs())
1371     addRegClassDefCounts(RegClassDefCounts, MO.getReg());
1372 
1373   llvm::sort(DefOperandIndexes, [&](unsigned I0, unsigned I1) {
1374     const MachineOperand &MO0 = MI.getOperand(I0);
1375     const MachineOperand &MO1 = MI.getOperand(I1);
1376     Register Reg0 = MO0.getReg();
1377     Register Reg1 = MO1.getReg();
1378     const TargetRegisterClass &RC0 = *MRI->getRegClass(Reg0);
1379     const TargetRegisterClass &RC1 = *MRI->getRegClass(Reg1);
1380 
1381     // Identify regclass that are easy to use up completely just in this
1382     // instruction.
1383     unsigned ClassSize0 = RegClassInfo.getOrder(&RC0).size();
1384     unsigned ClassSize1 = RegClassInfo.getOrder(&RC1).size();
1385 
1386     bool SmallClass0 = ClassSize0 < RegClassDefCounts[RC0.getID()];
1387     bool SmallClass1 = ClassSize1 < RegClassDefCounts[RC1.getID()];
1388     if (SmallClass0 > SmallClass1)
1389       return true;
1390     if (SmallClass0 < SmallClass1)
1391       return false;
1392 
1393     // Allocate early clobbers and livethrough operands first.
1394     bool Livethrough0 = MO0.isEarlyClobber() || MO0.isTied() ||
1395                         (MO0.getSubReg() == 0 && !MO0.isUndef());
1396     bool Livethrough1 = MO1.isEarlyClobber() || MO1.isTied() ||
1397                         (MO1.getSubReg() == 0 && !MO1.isUndef());
1398     if (Livethrough0 > Livethrough1)
1399       return true;
1400     if (Livethrough0 < Livethrough1)
1401       return false;
1402 
1403     // Tie-break rule: operand index.
1404     return I0 < I1;
1405   });
1406 }
1407 
1408 // Returns true if MO is tied and the operand it's tied to is not Undef (not
1409 // Undef is not the same thing as Def).
1410 static bool isTiedToNotUndef(const MachineOperand &MO) {
1411   if (!MO.isTied())
1412     return false;
1413   const MachineInstr &MI = *MO.getParent();
1414   unsigned TiedIdx = MI.findTiedOperandIdx(MI.getOperandNo(&MO));
1415   const MachineOperand &TiedMO = MI.getOperand(TiedIdx);
1416   return !TiedMO.isUndef();
1417 }
1418 
1419 void RegAllocFastImpl::allocateInstruction(MachineInstr &MI) {
1420   // The basic algorithm here is:
1421   // 1. Mark registers of def operands as free
1422   // 2. Allocate registers to use operands and place reload instructions for
1423   //    registers displaced by the allocation.
1424   //
1425   // However we need to handle some corner cases:
1426   // - pre-assigned defs and uses need to be handled before the other def/use
1427   //   operands are processed to avoid the allocation heuristics clashing with
1428   //   the pre-assignment.
1429   // - The "free def operands" step has to come last instead of first for tied
1430   //   operands and early-clobbers.
1431 
1432   InstrGen += 2;
1433   // In the event we ever get more than 2**31 instructions...
1434   if (LLVM_UNLIKELY(InstrGen == 0)) {
1435     UsedInInstr.assign(UsedInInstr.size(), 0);
1436     InstrGen = 2;
1437   }
1438   RegMasks.clear();
1439   BundleVirtRegsMap.clear();
1440 
1441   // Scan for special cases; Apply pre-assigned register defs to state.
1442   bool HasPhysRegUse = false;
1443   bool HasRegMask = false;
1444   bool HasVRegDef = false;
1445   bool HasDef = false;
1446   bool HasEarlyClobber = false;
1447   bool NeedToAssignLiveThroughs = false;
1448   for (MachineOperand &MO : MI.operands()) {
1449     if (MO.isReg()) {
1450       Register Reg = MO.getReg();
1451       if (Reg.isVirtual()) {
1452         if (!shouldAllocateRegister(Reg))
1453           continue;
1454         if (MO.isDef()) {
1455           HasDef = true;
1456           HasVRegDef = true;
1457           if (MO.isEarlyClobber()) {
1458             HasEarlyClobber = true;
1459             NeedToAssignLiveThroughs = true;
1460           }
1461           if (isTiedToNotUndef(MO) || (MO.getSubReg() != 0 && !MO.isUndef()))
1462             NeedToAssignLiveThroughs = true;
1463         }
1464       } else if (Reg.isPhysical()) {
1465         if (!MRI->isReserved(Reg)) {
1466           if (MO.isDef()) {
1467             HasDef = true;
1468             bool displacedAny = definePhysReg(MI, Reg);
1469             if (MO.isEarlyClobber())
1470               HasEarlyClobber = true;
1471             if (!displacedAny)
1472               MO.setIsDead(true);
1473           }
1474           if (MO.readsReg())
1475             HasPhysRegUse = true;
1476         }
1477       }
1478     } else if (MO.isRegMask()) {
1479       HasRegMask = true;
1480       RegMasks.push_back(MO.getRegMask());
1481     }
1482   }
1483 
1484   // Allocate virtreg defs.
1485   if (HasDef) {
1486     if (HasVRegDef) {
1487       // Note that Implicit MOs can get re-arranged by defineVirtReg(), so loop
1488       // multiple times to ensure no operand is missed.
1489       bool ReArrangedImplicitOps = true;
1490 
1491       // Special handling for early clobbers, tied operands or subregister defs:
1492       // Compared to "normal" defs these:
1493       // - Must not use a register that is pre-assigned for a use operand.
1494       // - In order to solve tricky inline assembly constraints we change the
1495       //   heuristic to figure out a good operand order before doing
1496       //   assignments.
1497       if (NeedToAssignLiveThroughs) {
1498         while (ReArrangedImplicitOps) {
1499           ReArrangedImplicitOps = false;
1500           findAndSortDefOperandIndexes(MI);
1501           for (unsigned OpIdx : DefOperandIndexes) {
1502             MachineOperand &MO = MI.getOperand(OpIdx);
1503             LLVM_DEBUG(dbgs() << "Allocating " << MO << '\n');
1504             Register Reg = MO.getReg();
1505             if (MO.isEarlyClobber() || isTiedToNotUndef(MO) ||
1506                 (MO.getSubReg() && !MO.isUndef())) {
1507               ReArrangedImplicitOps = defineLiveThroughVirtReg(MI, OpIdx, Reg);
1508             } else {
1509               ReArrangedImplicitOps = defineVirtReg(MI, OpIdx, Reg);
1510             }
1511             // Implicit operands of MI were re-arranged,
1512             // re-compute DefOperandIndexes.
1513             if (ReArrangedImplicitOps)
1514               break;
1515           }
1516         }
1517       } else {
1518         // Assign virtual register defs.
1519         while (ReArrangedImplicitOps) {
1520           ReArrangedImplicitOps = false;
1521           for (MachineOperand &MO : MI.all_defs()) {
1522             Register Reg = MO.getReg();
1523             if (Reg.isVirtual()) {
1524               ReArrangedImplicitOps =
1525                   defineVirtReg(MI, MI.getOperandNo(&MO), Reg);
1526               if (ReArrangedImplicitOps)
1527                 break;
1528             }
1529           }
1530         }
1531       }
1532     }
1533 
1534     // Free registers occupied by defs.
1535     // Iterate operands in reverse order, so we see the implicit super register
1536     // defs first (we added them earlier in case of <def,read-undef>).
1537     for (MachineOperand &MO : reverse(MI.all_defs())) {
1538       Register Reg = MO.getReg();
1539 
1540       // subreg defs don't free the full register. We left the subreg number
1541       // around as a marker in setPhysReg() to recognize this case here.
1542       if (Reg.isPhysical() && MO.getSubReg() != 0) {
1543         MO.setSubReg(0);
1544         continue;
1545       }
1546 
1547       assert((!MO.isTied() || !isClobberedByRegMasks(MO.getReg())) &&
1548              "tied def assigned to clobbered register");
1549 
1550       // Do not free tied operands and early clobbers.
1551       if (isTiedToNotUndef(MO) || MO.isEarlyClobber())
1552         continue;
1553       if (!Reg)
1554         continue;
1555       if (Reg.isVirtual()) {
1556         assert(!shouldAllocateRegister(Reg));
1557         continue;
1558       }
1559       assert(Reg.isPhysical());
1560       if (MRI->isReserved(Reg))
1561         continue;
1562       freePhysReg(Reg);
1563       unmarkRegUsedInInstr(Reg);
1564     }
1565   }
1566 
1567   // Displace clobbered registers.
1568   if (HasRegMask) {
1569     assert(!RegMasks.empty() && "expected RegMask");
1570     // MRI bookkeeping.
1571     for (const auto *RM : RegMasks)
1572       MRI->addPhysRegsUsedFromRegMask(RM);
1573 
1574     // Displace clobbered registers.
1575     for (const LiveReg &LR : LiveVirtRegs) {
1576       MCPhysReg PhysReg = LR.PhysReg;
1577       if (PhysReg != 0 && isClobberedByRegMasks(PhysReg))
1578         displacePhysReg(MI, PhysReg);
1579     }
1580   }
1581 
1582   // Apply pre-assigned register uses to state.
1583   if (HasPhysRegUse) {
1584     for (MachineOperand &MO : MI.operands()) {
1585       if (!MO.isReg() || !MO.readsReg())
1586         continue;
1587       Register Reg = MO.getReg();
1588       if (!Reg.isPhysical())
1589         continue;
1590       if (MRI->isReserved(Reg))
1591         continue;
1592       if (!usePhysReg(MI, Reg))
1593         MO.setIsKill(true);
1594     }
1595   }
1596 
1597   // Allocate virtreg uses and insert reloads as necessary.
1598   // Implicit MOs can get moved/removed by useVirtReg(), so loop multiple
1599   // times to ensure no operand is missed.
1600   bool HasUndefUse = false;
1601   bool ReArrangedImplicitMOs = true;
1602   while (ReArrangedImplicitMOs) {
1603     ReArrangedImplicitMOs = false;
1604     for (MachineOperand &MO : MI.operands()) {
1605       if (!MO.isReg() || !MO.isUse())
1606         continue;
1607       Register Reg = MO.getReg();
1608       if (!Reg.isVirtual() || !shouldAllocateRegister(Reg))
1609         continue;
1610 
1611       if (MO.isUndef()) {
1612         HasUndefUse = true;
1613         continue;
1614       }
1615 
1616       // Populate MayLiveAcrossBlocks in case the use block is allocated before
1617       // the def block (removing the vreg uses).
1618       mayLiveIn(Reg);
1619 
1620       assert(!MO.isInternalRead() && "Bundles not supported");
1621       assert(MO.readsReg() && "reading use");
1622       ReArrangedImplicitMOs = useVirtReg(MI, MO, Reg);
1623       if (ReArrangedImplicitMOs)
1624         break;
1625     }
1626   }
1627 
1628   // Allocate undef operands. This is a separate step because in a situation
1629   // like  ` = OP undef %X, %X`    both operands need the same register assign
1630   // so we should perform the normal assignment first.
1631   if (HasUndefUse) {
1632     for (MachineOperand &MO : MI.all_uses()) {
1633       Register Reg = MO.getReg();
1634       if (!Reg.isVirtual() || !shouldAllocateRegister(Reg))
1635         continue;
1636 
1637       assert(MO.isUndef() && "Should only have undef virtreg uses left");
1638       allocVirtRegUndef(MO);
1639     }
1640   }
1641 
1642   // Free early clobbers.
1643   if (HasEarlyClobber) {
1644     for (MachineOperand &MO : reverse(MI.all_defs())) {
1645       if (!MO.isEarlyClobber())
1646         continue;
1647       assert(!MO.getSubReg() && "should be already handled in def processing");
1648 
1649       Register Reg = MO.getReg();
1650       if (!Reg)
1651         continue;
1652       if (Reg.isVirtual()) {
1653         assert(!shouldAllocateRegister(Reg));
1654         continue;
1655       }
1656       assert(Reg.isPhysical() && "should have register assigned");
1657 
1658       // We sometimes get odd situations like:
1659       //    early-clobber %x0 = INSTRUCTION %x0
1660       // which is semantically questionable as the early-clobber should
1661       // apply before the use. But in practice we consider the use to
1662       // happen before the early clobber now. Don't free the early clobber
1663       // register in this case.
1664       if (MI.readsRegister(Reg, TRI))
1665         continue;
1666 
1667       freePhysReg(Reg);
1668     }
1669   }
1670 
1671   LLVM_DEBUG(dbgs() << "<< " << MI);
1672   if (MI.isCopy() && MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
1673       MI.getNumOperands() == 2) {
1674     LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1675     Coalesced.push_back(&MI);
1676   }
1677 }
1678 
1679 void RegAllocFastImpl::handleDebugValue(MachineInstr &MI) {
1680   // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1681   // mostly constants and frame indices.
1682   assert(MI.isDebugValue() && "not a DBG_VALUE*");
1683   for (const auto &MO : MI.debug_operands()) {
1684     if (!MO.isReg())
1685       continue;
1686     Register Reg = MO.getReg();
1687     if (!Reg.isVirtual())
1688       continue;
1689     if (!shouldAllocateRegister(Reg))
1690       continue;
1691 
1692     // Already spilled to a stackslot?
1693     int SS = StackSlotForVirtReg[Reg];
1694     if (SS != -1) {
1695       // Modify DBG_VALUE now that the value is in a spill slot.
1696       updateDbgValueForSpill(MI, SS, Reg);
1697       LLVM_DEBUG(dbgs() << "Rewrite DBG_VALUE for spilled memory: " << MI);
1698       continue;
1699     }
1700 
1701     // See if this virtual register has already been allocated to a physical
1702     // register or spilled to a stack slot.
1703     LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1704     SmallVector<MachineOperand *> DbgOps;
1705     for (MachineOperand &Op : MI.getDebugOperandsForReg(Reg))
1706       DbgOps.push_back(&Op);
1707 
1708     if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1709       // Update every use of Reg within MI.
1710       for (auto &RegMO : DbgOps)
1711         setPhysReg(MI, *RegMO, LRI->PhysReg);
1712     } else {
1713       DanglingDbgValues[Reg].push_back(&MI);
1714     }
1715 
1716     // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1717     // that future spills of Reg will have DBG_VALUEs.
1718     LiveDbgValueMap[Reg].append(DbgOps.begin(), DbgOps.end());
1719   }
1720 }
1721 
1722 void RegAllocFastImpl::handleBundle(MachineInstr &MI) {
1723   MachineBasicBlock::instr_iterator BundledMI = MI.getIterator();
1724   ++BundledMI;
1725   while (BundledMI->isBundledWithPred()) {
1726     for (MachineOperand &MO : BundledMI->operands()) {
1727       if (!MO.isReg())
1728         continue;
1729 
1730       Register Reg = MO.getReg();
1731       if (!Reg.isVirtual() || !shouldAllocateRegister(Reg))
1732         continue;
1733 
1734       DenseMap<Register, MCPhysReg>::iterator DI;
1735       DI = BundleVirtRegsMap.find(Reg);
1736       assert(DI != BundleVirtRegsMap.end() && "Unassigned virtual register");
1737 
1738       setPhysReg(MI, MO, DI->second);
1739     }
1740 
1741     ++BundledMI;
1742   }
1743 }
1744 
1745 void RegAllocFastImpl::allocateBasicBlock(MachineBasicBlock &MBB) {
1746   this->MBB = &MBB;
1747   LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
1748 
1749   PosIndexes.unsetInitialized();
1750   RegUnitStates.assign(TRI->getNumRegUnits(), regFree);
1751   assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
1752 
1753   for (const auto &LiveReg : MBB.liveouts())
1754     setPhysRegState(LiveReg.PhysReg, regPreAssigned);
1755 
1756   Coalesced.clear();
1757 
1758   // Traverse block in reverse order allocating instructions one by one.
1759   for (MachineInstr &MI : reverse(MBB)) {
1760     LLVM_DEBUG(dbgs() << "\n>> " << MI << "Regs:"; dumpState());
1761 
1762     // Special handling for debug values. Note that they are not allowed to
1763     // affect codegen of the other instructions in any way.
1764     if (MI.isDebugValue()) {
1765       handleDebugValue(MI);
1766       continue;
1767     }
1768 
1769     allocateInstruction(MI);
1770 
1771     // Once BUNDLE header is assigned registers, same assignments need to be
1772     // done for bundled MIs.
1773     if (MI.getOpcode() == TargetOpcode::BUNDLE) {
1774       handleBundle(MI);
1775     }
1776   }
1777 
1778   LLVM_DEBUG(dbgs() << "Begin Regs:"; dumpState());
1779 
1780   // Spill all physical registers holding virtual registers now.
1781   LLVM_DEBUG(dbgs() << "Loading live registers at begin of block.\n");
1782   reloadAtBegin(MBB);
1783 
1784   // Erase all the coalesced copies. We are delaying it until now because
1785   // LiveVirtRegs might refer to the instrs.
1786   for (MachineInstr *MI : Coalesced)
1787     MBB.erase(MI);
1788   NumCoalesced += Coalesced.size();
1789 
1790   for (auto &UDBGPair : DanglingDbgValues) {
1791     for (MachineInstr *DbgValue : UDBGPair.second) {
1792       assert(DbgValue->isDebugValue() && "expected DBG_VALUE");
1793       // Nothing to do if the vreg was spilled in the meantime.
1794       if (!DbgValue->hasDebugOperandForReg(UDBGPair.first))
1795         continue;
1796       LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
1797                         << '\n');
1798       DbgValue->setDebugValueUndef();
1799     }
1800   }
1801   DanglingDbgValues.clear();
1802 
1803   LLVM_DEBUG(MBB.dump());
1804 }
1805 
1806 bool RegAllocFastImpl::runOnMachineFunction(MachineFunction &MF) {
1807   LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1808                     << "********** Function: " << MF.getName() << '\n');
1809   MRI = &MF.getRegInfo();
1810   const TargetSubtargetInfo &STI = MF.getSubtarget();
1811   TRI = STI.getRegisterInfo();
1812   TII = STI.getInstrInfo();
1813   MFI = &MF.getFrameInfo();
1814   MRI->freezeReservedRegs();
1815   RegClassInfo.runOnMachineFunction(MF);
1816   unsigned NumRegUnits = TRI->getNumRegUnits();
1817   InstrGen = 0;
1818   UsedInInstr.assign(NumRegUnits, 0);
1819 
1820   // initialize the virtual->physical register map to have a 'null'
1821   // mapping for all virtual registers
1822   unsigned NumVirtRegs = MRI->getNumVirtRegs();
1823   StackSlotForVirtReg.resize(NumVirtRegs);
1824   LiveVirtRegs.setUniverse(NumVirtRegs);
1825   MayLiveAcrossBlocks.clear();
1826   MayLiveAcrossBlocks.resize(NumVirtRegs);
1827 
1828   // Loop over all of the basic blocks, eliminating virtual register references
1829   for (MachineBasicBlock &MBB : MF)
1830     allocateBasicBlock(MBB);
1831 
1832   if (ClearVirtRegs) {
1833     // All machine operands and other references to virtual registers have been
1834     // replaced. Remove the virtual registers.
1835     MRI->clearVirtRegs();
1836   }
1837 
1838   StackSlotForVirtReg.clear();
1839   LiveDbgValueMap.clear();
1840   return true;
1841 }
1842 
1843 PreservedAnalyses RegAllocFastPass::run(MachineFunction &MF,
1844                                         MachineFunctionAnalysisManager &) {
1845   MFPropsModifier _(*this, MF);
1846   RegAllocFastImpl Impl(Opts.Filter, Opts.ClearVRegs);
1847   bool Changed = Impl.runOnMachineFunction(MF);
1848   if (!Changed)
1849     return PreservedAnalyses::all();
1850   auto PA = getMachineFunctionPassPreservedAnalyses();
1851   PA.preserveSet<CFGAnalyses>();
1852   return PA;
1853 }
1854 
1855 void RegAllocFastPass::printPipeline(
1856     raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
1857   bool PrintFilterName = Opts.FilterName != "all";
1858   bool PrintNoClearVRegs = !Opts.ClearVRegs;
1859   bool PrintSemicolon = PrintFilterName && PrintNoClearVRegs;
1860 
1861   OS << "regallocfast";
1862   if (PrintFilterName || PrintNoClearVRegs) {
1863     OS << '<';
1864     if (PrintFilterName)
1865       OS << "filter=" << Opts.FilterName;
1866     if (PrintSemicolon)
1867       OS << ';';
1868     if (PrintNoClearVRegs)
1869       OS << "no-clear-vregs";
1870     OS << '>';
1871   }
1872 }
1873 
1874 FunctionPass *llvm::createFastRegisterAllocator() { return new RegAllocFast(); }
1875 
1876 FunctionPass *llvm::createFastRegisterAllocator(RegAllocFilterFunc Ftor,
1877                                                 bool ClearVirtRegs) {
1878   return new RegAllocFast(Ftor, ClearVirtRegs);
1879 }
1880