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