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