xref: /llvm-project/llvm/lib/CodeGen/RegAllocFast.cpp (revision 61f99a1c75e9dc84b70d6f2a660e99c1ac182e5b)
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(MCPhysReg 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(MCPhysReg 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     MCPhysReg 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     MCRegister 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   MCRegister 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::const_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     // FIXME: This can happen, and should fall back to a reserved entry in RC.
993     assert(!AllocationOrder.empty() && "Allocation order must not be empty");
994     PhysReg = AllocationOrder[0];
995   }
996 
997   unsigned SubRegIdx = MO.getSubReg();
998   if (SubRegIdx != 0) {
999     PhysReg = TRI->getSubReg(PhysReg, SubRegIdx);
1000     MO.setSubReg(0);
1001   }
1002   MO.setReg(PhysReg);
1003   MO.setIsRenamable(true);
1004 }
1005 
1006 /// Variation of defineVirtReg() with special handling for livethrough regs
1007 /// (tied or earlyclobber) that may interfere with preassigned uses.
1008 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1009 bool RegAllocFastImpl::defineLiveThroughVirtReg(MachineInstr &MI,
1010                                                 unsigned OpNum,
1011                                                 Register VirtReg) {
1012   if (!shouldAllocateRegister(VirtReg))
1013     return false;
1014   LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg);
1015   if (LRI != LiveVirtRegs.end()) {
1016     MCPhysReg PrevReg = LRI->PhysReg;
1017     if (PrevReg != 0 && isRegUsedInInstr(PrevReg, true)) {
1018       LLVM_DEBUG(dbgs() << "Need new assignment for " << printReg(PrevReg, TRI)
1019                         << " (tied/earlyclobber resolution)\n");
1020       freePhysReg(PrevReg);
1021       LRI->PhysReg = 0;
1022       allocVirtReg(MI, *LRI, 0, true);
1023       MachineBasicBlock::iterator InsertBefore =
1024           std::next((MachineBasicBlock::iterator)MI.getIterator());
1025       LLVM_DEBUG(dbgs() << "Copy " << printReg(LRI->PhysReg, TRI) << " to "
1026                         << printReg(PrevReg, TRI) << '\n');
1027       BuildMI(*MBB, InsertBefore, MI.getDebugLoc(),
1028               TII->get(TargetOpcode::COPY), PrevReg)
1029           .addReg(LRI->PhysReg, llvm::RegState::Kill);
1030     }
1031     MachineOperand &MO = MI.getOperand(OpNum);
1032     if (MO.getSubReg() && !MO.isUndef()) {
1033       LRI->LastUse = &MI;
1034     }
1035   }
1036   return defineVirtReg(MI, OpNum, VirtReg, true);
1037 }
1038 
1039 /// Allocates a register for VirtReg definition. Typically the register is
1040 /// already assigned from a use of the virtreg, however we still need to
1041 /// perform an allocation if:
1042 /// - It is a dead definition without any uses.
1043 /// - The value is live out and all uses are in different basic blocks.
1044 ///
1045 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1046 bool RegAllocFastImpl::defineVirtReg(MachineInstr &MI, unsigned OpNum,
1047                                      Register VirtReg, bool LookAtPhysRegUses) {
1048   assert(VirtReg.isVirtual() && "Not a virtual register");
1049   if (!shouldAllocateRegister(VirtReg))
1050     return false;
1051   MachineOperand &MO = MI.getOperand(OpNum);
1052   LiveRegMap::iterator LRI;
1053   bool New;
1054   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
1055   if (New) {
1056     if (!MO.isDead()) {
1057       if (mayLiveOut(VirtReg)) {
1058         LRI->LiveOut = true;
1059       } else {
1060         // It is a dead def without the dead flag; add the flag now.
1061         MO.setIsDead(true);
1062       }
1063     }
1064   }
1065   if (LRI->PhysReg == 0) {
1066     allocVirtReg(MI, *LRI, 0, LookAtPhysRegUses);
1067   } else {
1068     assert((!isRegUsedInInstr(LRI->PhysReg, LookAtPhysRegUses) || LRI->Error) &&
1069            "TODO: preassign mismatch");
1070     LLVM_DEBUG(dbgs() << "In def of " << printReg(VirtReg, TRI)
1071                       << " use existing assignment to "
1072                       << printReg(LRI->PhysReg, TRI) << '\n');
1073   }
1074 
1075   MCPhysReg PhysReg = LRI->PhysReg;
1076   if (LRI->Reloaded || LRI->LiveOut) {
1077     if (!MI.isImplicitDef()) {
1078       MachineBasicBlock::iterator SpillBefore =
1079           std::next((MachineBasicBlock::iterator)MI.getIterator());
1080       LLVM_DEBUG(dbgs() << "Spill Reason: LO: " << LRI->LiveOut
1081                         << " RL: " << LRI->Reloaded << '\n');
1082       bool Kill = LRI->LastUse == nullptr;
1083       spill(SpillBefore, VirtReg, PhysReg, Kill, LRI->LiveOut);
1084 
1085       // We need to place additional spills for each indirect destination of an
1086       // INLINEASM_BR.
1087       if (MI.getOpcode() == TargetOpcode::INLINEASM_BR) {
1088         int FI = StackSlotForVirtReg[VirtReg];
1089         const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg);
1090         for (MachineOperand &MO : MI.operands()) {
1091           if (MO.isMBB()) {
1092             MachineBasicBlock *Succ = MO.getMBB();
1093             TII->storeRegToStackSlot(*Succ, Succ->begin(), PhysReg, Kill, FI,
1094                                      &RC, TRI, VirtReg);
1095             ++NumStores;
1096             Succ->addLiveIn(PhysReg);
1097           }
1098         }
1099       }
1100 
1101       LRI->LastUse = nullptr;
1102     }
1103     LRI->LiveOut = false;
1104     LRI->Reloaded = false;
1105   }
1106   if (MI.getOpcode() == TargetOpcode::BUNDLE) {
1107     BundleVirtRegsMap[VirtReg] = PhysReg;
1108   }
1109   markRegUsedInInstr(PhysReg);
1110   return setPhysReg(MI, MO, PhysReg);
1111 }
1112 
1113 /// Allocates a register for a VirtReg use.
1114 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1115 bool RegAllocFastImpl::useVirtReg(MachineInstr &MI, MachineOperand &MO,
1116                                   Register VirtReg) {
1117   assert(VirtReg.isVirtual() && "Not a virtual register");
1118   if (!shouldAllocateRegister(VirtReg))
1119     return false;
1120   LiveRegMap::iterator LRI;
1121   bool New;
1122   std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg));
1123   if (New) {
1124     if (!MO.isKill()) {
1125       if (mayLiveOut(VirtReg)) {
1126         LRI->LiveOut = true;
1127       } else {
1128         // It is a last (killing) use without the kill flag; add the flag now.
1129         MO.setIsKill(true);
1130       }
1131     }
1132   } else {
1133     assert((!MO.isKill() || LRI->LastUse == &MI) && "Invalid kill flag");
1134   }
1135 
1136   // If necessary allocate a register.
1137   if (LRI->PhysReg == 0) {
1138     assert(!MO.isTied() && "tied op should be allocated");
1139     Register Hint;
1140     if (MI.isCopy() && MI.getOperand(1).getSubReg() == 0) {
1141       Hint = MI.getOperand(0).getReg();
1142       if (Hint.isVirtual()) {
1143         assert(!shouldAllocateRegister(Hint));
1144         Hint = Register();
1145       } else {
1146         assert(Hint.isPhysical() &&
1147                "Copy destination should already be assigned");
1148       }
1149     }
1150     allocVirtReg(MI, *LRI, Hint, false);
1151   }
1152 
1153   LRI->LastUse = &MI;
1154 
1155   if (MI.getOpcode() == TargetOpcode::BUNDLE) {
1156     BundleVirtRegsMap[VirtReg] = LRI->PhysReg;
1157   }
1158   markRegUsedInInstr(LRI->PhysReg);
1159   return setPhysReg(MI, MO, LRI->PhysReg);
1160 }
1161 
1162 /// Query a physical register to use as a filler in contexts where the
1163 /// allocation has failed. This will raise an error, but not abort the
1164 /// compilation.
1165 MCPhysReg RegAllocFastImpl::getErrorAssignment(const LiveReg &LR,
1166                                                MachineInstr &MI,
1167                                                const TargetRegisterClass &RC) {
1168   MachineFunction &MF = *MI.getMF();
1169 
1170   // Avoid repeating the error every time a register is used.
1171   bool EmitError = !MF.getProperties().hasProperty(
1172       MachineFunctionProperties::Property::FailedRegAlloc);
1173   if (EmitError)
1174     MF.getProperties().set(MachineFunctionProperties::Property::FailedRegAlloc);
1175 
1176   // If the allocation order was empty, all registers in the class were
1177   // probably reserved. Fall back to taking the first register in the class,
1178   // even if it's reserved.
1179   ArrayRef<MCPhysReg> AllocationOrder = RegClassInfo.getOrder(&RC);
1180   if (AllocationOrder.empty()) {
1181     const Function &Fn = MF.getFunction();
1182     if (EmitError) {
1183       DiagnosticInfoRegAllocFailure DI(
1184           "no registers from class available to allocate", Fn,
1185           MI.getDebugLoc());
1186       Fn.getContext().diagnose(DI);
1187     }
1188 
1189     ArrayRef<MCPhysReg> RawRegs = RC.getRegisters();
1190     assert(!RawRegs.empty() && "register classes cannot have no registers");
1191     return RawRegs.front();
1192   }
1193 
1194   if (!LR.Error && EmitError) {
1195     // Nothing we can do: Report an error and keep going with an invalid
1196     // allocation.
1197     if (MI.isInlineAsm()) {
1198       MI.emitInlineAsmError(
1199           "inline assembly requires more registers than available");
1200     } else {
1201       const Function &Fn = MBB->getParent()->getFunction();
1202       DiagnosticInfoRegAllocFailure DI(
1203           "ran out of registers during register allocation", Fn,
1204           MI.getDebugLoc());
1205       Fn.getContext().diagnose(DI);
1206     }
1207   }
1208 
1209   return AllocationOrder.front();
1210 }
1211 
1212 /// Changes operand OpNum in MI the refer the PhysReg, considering subregs.
1213 /// \return true if MI's MachineOperands were re-arranged/invalidated.
1214 bool RegAllocFastImpl::setPhysReg(MachineInstr &MI, MachineOperand &MO,
1215                                   MCPhysReg PhysReg) {
1216   if (!MO.getSubReg()) {
1217     MO.setReg(PhysReg);
1218     MO.setIsRenamable(true);
1219     return false;
1220   }
1221 
1222   // Handle subregister index.
1223   MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : MCRegister());
1224   MO.setIsRenamable(true);
1225   // Note: We leave the subreg number around a little longer in case of defs.
1226   // This is so that the register freeing logic in allocateInstruction can still
1227   // recognize this as subregister defs. The code there will clear the number.
1228   if (!MO.isDef())
1229     MO.setSubReg(0);
1230 
1231   // A kill flag implies killing the full register. Add corresponding super
1232   // register kill.
1233   if (MO.isKill()) {
1234     MI.addRegisterKilled(PhysReg, TRI, true);
1235     // Conservatively assume implicit MOs were re-arranged
1236     return true;
1237   }
1238 
1239   // A <def,read-undef> of a sub-register requires an implicit def of the full
1240   // register.
1241   if (MO.isDef() && MO.isUndef()) {
1242     if (MO.isDead())
1243       MI.addRegisterDead(PhysReg, TRI, true);
1244     else
1245       MI.addRegisterDefined(PhysReg, TRI);
1246     // Conservatively assume implicit MOs were re-arranged
1247     return true;
1248   }
1249   return false;
1250 }
1251 
1252 #ifndef NDEBUG
1253 
1254 void RegAllocFastImpl::dumpState() const {
1255   for (unsigned Unit = 1, UnitE = TRI->getNumRegUnits(); Unit != UnitE;
1256        ++Unit) {
1257     switch (unsigned VirtReg = RegUnitStates[Unit]) {
1258     case regFree:
1259       break;
1260     case regPreAssigned:
1261       dbgs() << " " << printRegUnit(Unit, TRI) << "[P]";
1262       break;
1263     case regLiveIn:
1264       llvm_unreachable("Should not have regLiveIn in map");
1265     default: {
1266       dbgs() << ' ' << printRegUnit(Unit, TRI) << '=' << printReg(VirtReg);
1267       LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg);
1268       assert(I != LiveVirtRegs.end() && "have LiveVirtRegs entry");
1269       if (I->LiveOut || I->Reloaded) {
1270         dbgs() << '[';
1271         if (I->LiveOut)
1272           dbgs() << 'O';
1273         if (I->Reloaded)
1274           dbgs() << 'R';
1275         dbgs() << ']';
1276       }
1277       assert(TRI->hasRegUnit(I->PhysReg, Unit) && "inverse mapping present");
1278       break;
1279     }
1280     }
1281   }
1282   dbgs() << '\n';
1283   // Check that LiveVirtRegs is the inverse.
1284   for (const LiveReg &LR : LiveVirtRegs) {
1285     Register VirtReg = LR.VirtReg;
1286     assert(VirtReg.isVirtual() && "Bad map key");
1287     MCPhysReg PhysReg = LR.PhysReg;
1288     if (PhysReg != 0) {
1289       assert(Register::isPhysicalRegister(PhysReg) && "mapped to physreg");
1290       for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
1291         assert(RegUnitStates[Unit] == VirtReg && "inverse map valid");
1292       }
1293     }
1294   }
1295 }
1296 #endif
1297 
1298 /// Count number of defs consumed from each register class by \p Reg
1299 void RegAllocFastImpl::addRegClassDefCounts(
1300     MutableArrayRef<unsigned> RegClassDefCounts, Register Reg) const {
1301   assert(RegClassDefCounts.size() == TRI->getNumRegClasses());
1302 
1303   if (Reg.isVirtual()) {
1304     if (!shouldAllocateRegister(Reg))
1305       return;
1306     const TargetRegisterClass *OpRC = MRI->getRegClass(Reg);
1307     for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
1308          RCIdx != RCIdxEnd; ++RCIdx) {
1309       const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
1310       // FIXME: Consider aliasing sub/super registers.
1311       if (OpRC->hasSubClassEq(IdxRC))
1312         ++RegClassDefCounts[RCIdx];
1313     }
1314 
1315     return;
1316   }
1317 
1318   for (unsigned RCIdx = 0, RCIdxEnd = TRI->getNumRegClasses();
1319        RCIdx != RCIdxEnd; ++RCIdx) {
1320     const TargetRegisterClass *IdxRC = TRI->getRegClass(RCIdx);
1321     for (MCRegAliasIterator Alias(Reg, TRI, true); Alias.isValid(); ++Alias) {
1322       if (IdxRC->contains(*Alias)) {
1323         ++RegClassDefCounts[RCIdx];
1324         break;
1325       }
1326     }
1327   }
1328 }
1329 
1330 /// Compute \ref DefOperandIndexes so it contains the indices of "def" operands
1331 /// that are to be allocated. Those are ordered in a way that small classes,
1332 /// early clobbers and livethroughs are allocated first.
1333 void RegAllocFastImpl::findAndSortDefOperandIndexes(const MachineInstr &MI) {
1334   DefOperandIndexes.clear();
1335 
1336   LLVM_DEBUG(dbgs() << "Need to assign livethroughs\n");
1337   for (unsigned I = 0, E = MI.getNumOperands(); I < E; ++I) {
1338     const MachineOperand &MO = MI.getOperand(I);
1339     if (!MO.isReg())
1340       continue;
1341     Register Reg = MO.getReg();
1342     if (MO.readsReg()) {
1343       if (Reg.isPhysical()) {
1344         LLVM_DEBUG(dbgs() << "mark extra used: " << printReg(Reg, TRI) << '\n');
1345         markPhysRegUsedInInstr(Reg);
1346       }
1347     }
1348 
1349     if (MO.isDef() && Reg.isVirtual() && shouldAllocateRegister(Reg))
1350       DefOperandIndexes.push_back(I);
1351   }
1352 
1353   // Most instructions only have one virtual def, so there's no point in
1354   // computing the possible number of defs for every register class.
1355   if (DefOperandIndexes.size() <= 1)
1356     return;
1357 
1358   // Track number of defs which may consume a register from the class. This is
1359   // used to assign registers for possibly-too-small classes first. Example:
1360   // defs are eax, 3 * gr32_abcd, 2 * gr32 => we want to assign the gr32_abcd
1361   // registers first so that the gr32 don't use the gr32_abcd registers before
1362   // we assign these.
1363   SmallVector<unsigned> RegClassDefCounts(TRI->getNumRegClasses(), 0);
1364 
1365   for (const MachineOperand &MO : MI.all_defs())
1366     addRegClassDefCounts(RegClassDefCounts, MO.getReg());
1367 
1368   llvm::sort(DefOperandIndexes, [&](unsigned I0, unsigned I1) {
1369     const MachineOperand &MO0 = MI.getOperand(I0);
1370     const MachineOperand &MO1 = MI.getOperand(I1);
1371     Register Reg0 = MO0.getReg();
1372     Register Reg1 = MO1.getReg();
1373     const TargetRegisterClass &RC0 = *MRI->getRegClass(Reg0);
1374     const TargetRegisterClass &RC1 = *MRI->getRegClass(Reg1);
1375 
1376     // Identify regclass that are easy to use up completely just in this
1377     // instruction.
1378     unsigned ClassSize0 = RegClassInfo.getOrder(&RC0).size();
1379     unsigned ClassSize1 = RegClassInfo.getOrder(&RC1).size();
1380 
1381     bool SmallClass0 = ClassSize0 < RegClassDefCounts[RC0.getID()];
1382     bool SmallClass1 = ClassSize1 < RegClassDefCounts[RC1.getID()];
1383     if (SmallClass0 > SmallClass1)
1384       return true;
1385     if (SmallClass0 < SmallClass1)
1386       return false;
1387 
1388     // Allocate early clobbers and livethrough operands first.
1389     bool Livethrough0 = MO0.isEarlyClobber() || MO0.isTied() ||
1390                         (MO0.getSubReg() == 0 && !MO0.isUndef());
1391     bool Livethrough1 = MO1.isEarlyClobber() || MO1.isTied() ||
1392                         (MO1.getSubReg() == 0 && !MO1.isUndef());
1393     if (Livethrough0 > Livethrough1)
1394       return true;
1395     if (Livethrough0 < Livethrough1)
1396       return false;
1397 
1398     // Tie-break rule: operand index.
1399     return I0 < I1;
1400   });
1401 }
1402 
1403 // Returns true if MO is tied and the operand it's tied to is not Undef (not
1404 // Undef is not the same thing as Def).
1405 static bool isTiedToNotUndef(const MachineOperand &MO) {
1406   if (!MO.isTied())
1407     return false;
1408   const MachineInstr &MI = *MO.getParent();
1409   unsigned TiedIdx = MI.findTiedOperandIdx(MI.getOperandNo(&MO));
1410   const MachineOperand &TiedMO = MI.getOperand(TiedIdx);
1411   return !TiedMO.isUndef();
1412 }
1413 
1414 void RegAllocFastImpl::allocateInstruction(MachineInstr &MI) {
1415   // The basic algorithm here is:
1416   // 1. Mark registers of def operands as free
1417   // 2. Allocate registers to use operands and place reload instructions for
1418   //    registers displaced by the allocation.
1419   //
1420   // However we need to handle some corner cases:
1421   // - pre-assigned defs and uses need to be handled before the other def/use
1422   //   operands are processed to avoid the allocation heuristics clashing with
1423   //   the pre-assignment.
1424   // - The "free def operands" step has to come last instead of first for tied
1425   //   operands and early-clobbers.
1426 
1427   InstrGen += 2;
1428   // In the event we ever get more than 2**31 instructions...
1429   if (LLVM_UNLIKELY(InstrGen == 0)) {
1430     UsedInInstr.assign(UsedInInstr.size(), 0);
1431     InstrGen = 2;
1432   }
1433   RegMasks.clear();
1434   BundleVirtRegsMap.clear();
1435 
1436   // Scan for special cases; Apply pre-assigned register defs to state.
1437   bool HasPhysRegUse = false;
1438   bool HasRegMask = false;
1439   bool HasVRegDef = false;
1440   bool HasDef = false;
1441   bool HasEarlyClobber = false;
1442   bool NeedToAssignLiveThroughs = false;
1443   for (MachineOperand &MO : MI.operands()) {
1444     if (MO.isReg()) {
1445       Register Reg = MO.getReg();
1446       if (Reg.isVirtual()) {
1447         if (!shouldAllocateRegister(Reg))
1448           continue;
1449         if (MO.isDef()) {
1450           HasDef = true;
1451           HasVRegDef = true;
1452           if (MO.isEarlyClobber()) {
1453             HasEarlyClobber = true;
1454             NeedToAssignLiveThroughs = true;
1455           }
1456           if (isTiedToNotUndef(MO) || (MO.getSubReg() != 0 && !MO.isUndef()))
1457             NeedToAssignLiveThroughs = true;
1458         }
1459       } else if (Reg.isPhysical()) {
1460         if (!MRI->isReserved(Reg)) {
1461           if (MO.isDef()) {
1462             HasDef = true;
1463             bool displacedAny = definePhysReg(MI, Reg);
1464             if (MO.isEarlyClobber())
1465               HasEarlyClobber = true;
1466             if (!displacedAny)
1467               MO.setIsDead(true);
1468           }
1469           if (MO.readsReg())
1470             HasPhysRegUse = true;
1471         }
1472       }
1473     } else if (MO.isRegMask()) {
1474       HasRegMask = true;
1475       RegMasks.push_back(MO.getRegMask());
1476     }
1477   }
1478 
1479   // Allocate virtreg defs.
1480   if (HasDef) {
1481     if (HasVRegDef) {
1482       // Note that Implicit MOs can get re-arranged by defineVirtReg(), so loop
1483       // multiple times to ensure no operand is missed.
1484       bool ReArrangedImplicitOps = true;
1485 
1486       // Special handling for early clobbers, tied operands or subregister defs:
1487       // Compared to "normal" defs these:
1488       // - Must not use a register that is pre-assigned for a use operand.
1489       // - In order to solve tricky inline assembly constraints we change the
1490       //   heuristic to figure out a good operand order before doing
1491       //   assignments.
1492       if (NeedToAssignLiveThroughs) {
1493         while (ReArrangedImplicitOps) {
1494           ReArrangedImplicitOps = false;
1495           findAndSortDefOperandIndexes(MI);
1496           for (unsigned OpIdx : DefOperandIndexes) {
1497             MachineOperand &MO = MI.getOperand(OpIdx);
1498             LLVM_DEBUG(dbgs() << "Allocating " << MO << '\n');
1499             Register Reg = MO.getReg();
1500             if (MO.isEarlyClobber() || isTiedToNotUndef(MO) ||
1501                 (MO.getSubReg() && !MO.isUndef())) {
1502               ReArrangedImplicitOps = defineLiveThroughVirtReg(MI, OpIdx, Reg);
1503             } else {
1504               ReArrangedImplicitOps = defineVirtReg(MI, OpIdx, Reg);
1505             }
1506             // Implicit operands of MI were re-arranged,
1507             // re-compute DefOperandIndexes.
1508             if (ReArrangedImplicitOps)
1509               break;
1510           }
1511         }
1512       } else {
1513         // Assign virtual register defs.
1514         while (ReArrangedImplicitOps) {
1515           ReArrangedImplicitOps = false;
1516           for (MachineOperand &MO : MI.all_defs()) {
1517             Register Reg = MO.getReg();
1518             if (Reg.isVirtual()) {
1519               ReArrangedImplicitOps =
1520                   defineVirtReg(MI, MI.getOperandNo(&MO), Reg);
1521               if (ReArrangedImplicitOps)
1522                 break;
1523             }
1524           }
1525         }
1526       }
1527     }
1528 
1529     // Free registers occupied by defs.
1530     // Iterate operands in reverse order, so we see the implicit super register
1531     // defs first (we added them earlier in case of <def,read-undef>).
1532     for (MachineOperand &MO : reverse(MI.all_defs())) {
1533       Register Reg = MO.getReg();
1534 
1535       // subreg defs don't free the full register. We left the subreg number
1536       // around as a marker in setPhysReg() to recognize this case here.
1537       if (Reg.isPhysical() && MO.getSubReg() != 0) {
1538         MO.setSubReg(0);
1539         continue;
1540       }
1541 
1542       assert((!MO.isTied() || !isClobberedByRegMasks(MO.getReg())) &&
1543              "tied def assigned to clobbered register");
1544 
1545       // Do not free tied operands and early clobbers.
1546       if (isTiedToNotUndef(MO) || MO.isEarlyClobber())
1547         continue;
1548       if (!Reg)
1549         continue;
1550       if (Reg.isVirtual()) {
1551         assert(!shouldAllocateRegister(Reg));
1552         continue;
1553       }
1554       assert(Reg.isPhysical());
1555       if (MRI->isReserved(Reg))
1556         continue;
1557       freePhysReg(Reg);
1558       unmarkRegUsedInInstr(Reg);
1559     }
1560   }
1561 
1562   // Displace clobbered registers.
1563   if (HasRegMask) {
1564     assert(!RegMasks.empty() && "expected RegMask");
1565     // MRI bookkeeping.
1566     for (const auto *RM : RegMasks)
1567       MRI->addPhysRegsUsedFromRegMask(RM);
1568 
1569     // Displace clobbered registers.
1570     for (const LiveReg &LR : LiveVirtRegs) {
1571       MCPhysReg PhysReg = LR.PhysReg;
1572       if (PhysReg != 0 && isClobberedByRegMasks(PhysReg))
1573         displacePhysReg(MI, PhysReg);
1574     }
1575   }
1576 
1577   // Apply pre-assigned register uses to state.
1578   if (HasPhysRegUse) {
1579     for (MachineOperand &MO : MI.operands()) {
1580       if (!MO.isReg() || !MO.readsReg())
1581         continue;
1582       Register Reg = MO.getReg();
1583       if (!Reg.isPhysical())
1584         continue;
1585       if (MRI->isReserved(Reg))
1586         continue;
1587       if (!usePhysReg(MI, Reg))
1588         MO.setIsKill(true);
1589     }
1590   }
1591 
1592   // Allocate virtreg uses and insert reloads as necessary.
1593   // Implicit MOs can get moved/removed by useVirtReg(), so loop multiple
1594   // times to ensure no operand is missed.
1595   bool HasUndefUse = false;
1596   bool ReArrangedImplicitMOs = true;
1597   while (ReArrangedImplicitMOs) {
1598     ReArrangedImplicitMOs = false;
1599     for (MachineOperand &MO : MI.operands()) {
1600       if (!MO.isReg() || !MO.isUse())
1601         continue;
1602       Register Reg = MO.getReg();
1603       if (!Reg.isVirtual() || !shouldAllocateRegister(Reg))
1604         continue;
1605 
1606       if (MO.isUndef()) {
1607         HasUndefUse = true;
1608         continue;
1609       }
1610 
1611       // Populate MayLiveAcrossBlocks in case the use block is allocated before
1612       // the def block (removing the vreg uses).
1613       mayLiveIn(Reg);
1614 
1615       assert(!MO.isInternalRead() && "Bundles not supported");
1616       assert(MO.readsReg() && "reading use");
1617       ReArrangedImplicitMOs = useVirtReg(MI, MO, Reg);
1618       if (ReArrangedImplicitMOs)
1619         break;
1620     }
1621   }
1622 
1623   // Allocate undef operands. This is a separate step because in a situation
1624   // like  ` = OP undef %X, %X`    both operands need the same register assign
1625   // so we should perform the normal assignment first.
1626   if (HasUndefUse) {
1627     for (MachineOperand &MO : MI.all_uses()) {
1628       Register Reg = MO.getReg();
1629       if (!Reg.isVirtual() || !shouldAllocateRegister(Reg))
1630         continue;
1631 
1632       assert(MO.isUndef() && "Should only have undef virtreg uses left");
1633       allocVirtRegUndef(MO);
1634     }
1635   }
1636 
1637   // Free early clobbers.
1638   if (HasEarlyClobber) {
1639     for (MachineOperand &MO : reverse(MI.all_defs())) {
1640       if (!MO.isEarlyClobber())
1641         continue;
1642       assert(!MO.getSubReg() && "should be already handled in def processing");
1643 
1644       Register Reg = MO.getReg();
1645       if (!Reg)
1646         continue;
1647       if (Reg.isVirtual()) {
1648         assert(!shouldAllocateRegister(Reg));
1649         continue;
1650       }
1651       assert(Reg.isPhysical() && "should have register assigned");
1652 
1653       // We sometimes get odd situations like:
1654       //    early-clobber %x0 = INSTRUCTION %x0
1655       // which is semantically questionable as the early-clobber should
1656       // apply before the use. But in practice we consider the use to
1657       // happen before the early clobber now. Don't free the early clobber
1658       // register in this case.
1659       if (MI.readsRegister(Reg, TRI))
1660         continue;
1661 
1662       freePhysReg(Reg);
1663     }
1664   }
1665 
1666   LLVM_DEBUG(dbgs() << "<< " << MI);
1667   if (MI.isCopy() && MI.getOperand(0).getReg() == MI.getOperand(1).getReg() &&
1668       MI.getNumOperands() == 2) {
1669     LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1670     Coalesced.push_back(&MI);
1671   }
1672 }
1673 
1674 void RegAllocFastImpl::handleDebugValue(MachineInstr &MI) {
1675   // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1676   // mostly constants and frame indices.
1677   assert(MI.isDebugValue() && "not a DBG_VALUE*");
1678   for (const auto &MO : MI.debug_operands()) {
1679     if (!MO.isReg())
1680       continue;
1681     Register Reg = MO.getReg();
1682     if (!Reg.isVirtual())
1683       continue;
1684     if (!shouldAllocateRegister(Reg))
1685       continue;
1686 
1687     // Already spilled to a stackslot?
1688     int SS = StackSlotForVirtReg[Reg];
1689     if (SS != -1) {
1690       // Modify DBG_VALUE now that the value is in a spill slot.
1691       updateDbgValueForSpill(MI, SS, Reg);
1692       LLVM_DEBUG(dbgs() << "Rewrite DBG_VALUE for spilled memory: " << MI);
1693       continue;
1694     }
1695 
1696     // See if this virtual register has already been allocated to a physical
1697     // register or spilled to a stack slot.
1698     LiveRegMap::iterator LRI = findLiveVirtReg(Reg);
1699     SmallVector<MachineOperand *> DbgOps;
1700     for (MachineOperand &Op : MI.getDebugOperandsForReg(Reg))
1701       DbgOps.push_back(&Op);
1702 
1703     if (LRI != LiveVirtRegs.end() && LRI->PhysReg) {
1704       // Update every use of Reg within MI.
1705       for (auto &RegMO : DbgOps)
1706         setPhysReg(MI, *RegMO, LRI->PhysReg);
1707     } else {
1708       DanglingDbgValues[Reg].push_back(&MI);
1709     }
1710 
1711     // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1712     // that future spills of Reg will have DBG_VALUEs.
1713     LiveDbgValueMap[Reg].append(DbgOps.begin(), DbgOps.end());
1714   }
1715 }
1716 
1717 void RegAllocFastImpl::handleBundle(MachineInstr &MI) {
1718   MachineBasicBlock::instr_iterator BundledMI = MI.getIterator();
1719   ++BundledMI;
1720   while (BundledMI->isBundledWithPred()) {
1721     for (MachineOperand &MO : BundledMI->operands()) {
1722       if (!MO.isReg())
1723         continue;
1724 
1725       Register Reg = MO.getReg();
1726       if (!Reg.isVirtual() || !shouldAllocateRegister(Reg))
1727         continue;
1728 
1729       DenseMap<Register, MCPhysReg>::iterator DI;
1730       DI = BundleVirtRegsMap.find(Reg);
1731       assert(DI != BundleVirtRegsMap.end() && "Unassigned virtual register");
1732 
1733       setPhysReg(MI, MO, DI->second);
1734     }
1735 
1736     ++BundledMI;
1737   }
1738 }
1739 
1740 void RegAllocFastImpl::allocateBasicBlock(MachineBasicBlock &MBB) {
1741   this->MBB = &MBB;
1742   LLVM_DEBUG(dbgs() << "\nAllocating " << MBB);
1743 
1744   PosIndexes.unsetInitialized();
1745   RegUnitStates.assign(TRI->getNumRegUnits(), regFree);
1746   assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?");
1747 
1748   for (const auto &LiveReg : MBB.liveouts())
1749     setPhysRegState(LiveReg.PhysReg, regPreAssigned);
1750 
1751   Coalesced.clear();
1752 
1753   // Traverse block in reverse order allocating instructions one by one.
1754   for (MachineInstr &MI : reverse(MBB)) {
1755     LLVM_DEBUG(dbgs() << "\n>> " << MI << "Regs:"; dumpState());
1756 
1757     // Special handling for debug values. Note that they are not allowed to
1758     // affect codegen of the other instructions in any way.
1759     if (MI.isDebugValue()) {
1760       handleDebugValue(MI);
1761       continue;
1762     }
1763 
1764     allocateInstruction(MI);
1765 
1766     // Once BUNDLE header is assigned registers, same assignments need to be
1767     // done for bundled MIs.
1768     if (MI.getOpcode() == TargetOpcode::BUNDLE) {
1769       handleBundle(MI);
1770     }
1771   }
1772 
1773   LLVM_DEBUG(dbgs() << "Begin Regs:"; dumpState());
1774 
1775   // Spill all physical registers holding virtual registers now.
1776   LLVM_DEBUG(dbgs() << "Loading live registers at begin of block.\n");
1777   reloadAtBegin(MBB);
1778 
1779   // Erase all the coalesced copies. We are delaying it until now because
1780   // LiveVirtRegs might refer to the instrs.
1781   for (MachineInstr *MI : Coalesced)
1782     MBB.erase(MI);
1783   NumCoalesced += Coalesced.size();
1784 
1785   for (auto &UDBGPair : DanglingDbgValues) {
1786     for (MachineInstr *DbgValue : UDBGPair.second) {
1787       assert(DbgValue->isDebugValue() && "expected DBG_VALUE");
1788       // Nothing to do if the vreg was spilled in the meantime.
1789       if (!DbgValue->hasDebugOperandForReg(UDBGPair.first))
1790         continue;
1791       LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
1792                         << '\n');
1793       DbgValue->setDebugValueUndef();
1794     }
1795   }
1796   DanglingDbgValues.clear();
1797 
1798   LLVM_DEBUG(MBB.dump());
1799 }
1800 
1801 bool RegAllocFastImpl::runOnMachineFunction(MachineFunction &MF) {
1802   LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1803                     << "********** Function: " << MF.getName() << '\n');
1804   MRI = &MF.getRegInfo();
1805   const TargetSubtargetInfo &STI = MF.getSubtarget();
1806   TRI = STI.getRegisterInfo();
1807   TII = STI.getInstrInfo();
1808   MFI = &MF.getFrameInfo();
1809   MRI->freezeReservedRegs();
1810   RegClassInfo.runOnMachineFunction(MF);
1811   unsigned NumRegUnits = TRI->getNumRegUnits();
1812   InstrGen = 0;
1813   UsedInInstr.assign(NumRegUnits, 0);
1814 
1815   // initialize the virtual->physical register map to have a 'null'
1816   // mapping for all virtual registers
1817   unsigned NumVirtRegs = MRI->getNumVirtRegs();
1818   StackSlotForVirtReg.resize(NumVirtRegs);
1819   LiveVirtRegs.setUniverse(NumVirtRegs);
1820   MayLiveAcrossBlocks.clear();
1821   MayLiveAcrossBlocks.resize(NumVirtRegs);
1822 
1823   // Loop over all of the basic blocks, eliminating virtual register references
1824   for (MachineBasicBlock &MBB : MF)
1825     allocateBasicBlock(MBB);
1826 
1827   if (ClearVirtRegs) {
1828     // All machine operands and other references to virtual registers have been
1829     // replaced. Remove the virtual registers.
1830     MRI->clearVirtRegs();
1831   }
1832 
1833   StackSlotForVirtReg.clear();
1834   LiveDbgValueMap.clear();
1835   return true;
1836 }
1837 
1838 PreservedAnalyses RegAllocFastPass::run(MachineFunction &MF,
1839                                         MachineFunctionAnalysisManager &) {
1840   MFPropsModifier _(*this, MF);
1841   RegAllocFastImpl Impl(Opts.Filter, Opts.ClearVRegs);
1842   bool Changed = Impl.runOnMachineFunction(MF);
1843   if (!Changed)
1844     return PreservedAnalyses::all();
1845   auto PA = getMachineFunctionPassPreservedAnalyses();
1846   PA.preserveSet<CFGAnalyses>();
1847   return PA;
1848 }
1849 
1850 void RegAllocFastPass::printPipeline(
1851     raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
1852   bool PrintFilterName = Opts.FilterName != "all";
1853   bool PrintNoClearVRegs = !Opts.ClearVRegs;
1854   bool PrintSemicolon = PrintFilterName && PrintNoClearVRegs;
1855 
1856   OS << "regallocfast";
1857   if (PrintFilterName || PrintNoClearVRegs) {
1858     OS << '<';
1859     if (PrintFilterName)
1860       OS << "filter=" << Opts.FilterName;
1861     if (PrintSemicolon)
1862       OS << ';';
1863     if (PrintNoClearVRegs)
1864       OS << "no-clear-vregs";
1865     OS << '>';
1866   }
1867 }
1868 
1869 FunctionPass *llvm::createFastRegisterAllocator() { return new RegAllocFast(); }
1870 
1871 FunctionPass *llvm::createFastRegisterAllocator(RegAllocFilterFunc Ftor,
1872                                                 bool ClearVirtRegs) {
1873   return new RegAllocFast(Ftor, ClearVirtRegs);
1874 }
1875