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