1 //===- RegAllocFast.cpp - A fast register allocator for debug code --------===//
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
7 //===----------------------------------------------------------------------===//
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
13 //===----------------------------------------------------------------------===//
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/IR/DebugLoc.h"
39 #include "llvm/IR/Metadata.h"
40 #include "llvm/InitializePasses.h"
41 #include "llvm/MC/MCInstrDesc.h"
42 #include "llvm/MC/MCRegisterInfo.h"
43 #include "llvm/Pass.h"
44 #include "llvm/Support/Casting.h"
45 #include "llvm/Support/Compiler.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/raw_ostream.h"
55 #define DEBUG_TYPE "regalloc"
57 STATISTIC(NumStores
, "Number of stores added");
58 STATISTIC(NumLoads
, "Number of loads added");
59 STATISTIC(NumCoalesced
, "Number of copies coalesced");
61 // FIXME: Remove this switch when all testcases are fixed!
62 static cl::opt
<bool> IgnoreMissingDefs("rafast-ignore-missing-defs",
65 static RegisterRegAlloc
66 fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator
);
70 class RegAllocFast
: public MachineFunctionPass
{
74 RegAllocFast(const RegClassFilterFunc F
= allocateAllRegClasses
,
75 bool ClearVirtRegs_
= true) :
76 MachineFunctionPass(ID
),
77 ShouldAllocateClass(F
),
78 StackSlotForVirtReg(-1),
79 ClearVirtRegs(ClearVirtRegs_
) {
83 MachineFrameInfo
*MFI
;
84 MachineRegisterInfo
*MRI
;
85 const TargetRegisterInfo
*TRI
;
86 const TargetInstrInfo
*TII
;
87 RegisterClassInfo RegClassInfo
;
88 const RegClassFilterFunc ShouldAllocateClass
;
90 /// Basic block currently being allocated.
91 MachineBasicBlock
*MBB
;
93 /// Maps virtual regs to the frame index where these values are spilled.
94 IndexedMap
<int, VirtReg2IndexFunctor
> StackSlotForVirtReg
;
98 /// Everything we know about a live virtual register.
100 MachineInstr
*LastUse
= nullptr; ///< Last instr to use reg.
101 Register VirtReg
; ///< Virtual register number.
102 MCPhysReg PhysReg
= 0; ///< Currently held here.
103 bool LiveOut
= false; ///< Register is possibly live out.
104 bool Reloaded
= false; ///< Register was reloaded.
105 bool Error
= false; ///< Could not allocate.
107 explicit LiveReg(Register VirtReg
) : VirtReg(VirtReg
) {}
109 unsigned getSparseSetIndex() const {
110 return Register::virtReg2Index(VirtReg
);
114 using LiveRegMap
= SparseSet
<LiveReg
>;
115 /// This map contains entries for each virtual register that is currently
116 /// available in a physical register.
117 LiveRegMap LiveVirtRegs
;
119 /// Stores assigned virtual registers present in the bundle MI.
120 DenseMap
<Register
, MCPhysReg
> BundleVirtRegsMap
;
122 DenseMap
<unsigned, SmallVector
<MachineOperand
*, 2>> LiveDbgValueMap
;
123 /// List of DBG_VALUE that we encountered without the vreg being assigned
124 /// because they were placed after the last use of the vreg.
125 DenseMap
<unsigned, SmallVector
<MachineInstr
*, 1>> DanglingDbgValues
;
127 /// Has a bit set for every virtual register for which it was determined
128 /// that it is alive across blocks.
129 BitVector MayLiveAcrossBlocks
;
131 /// State of a register unit.
133 /// A free register is not currently in use and can be allocated
134 /// immediately without checking aliases.
137 /// A pre-assigned register has been assigned before register allocation
138 /// (e.g., setting up a call parameter).
141 /// Used temporarily in reloadAtBegin() to mark register units that are
142 /// live-in to the basic block.
145 /// A register state may also be a virtual register number, indication
146 /// that the physical register is currently allocated to a virtual
147 /// register. In that case, LiveVirtRegs contains the inverse mapping.
150 /// Maps each physical register to a RegUnitState enum or virtual register.
151 std::vector
<unsigned> RegUnitStates
;
153 SmallVector
<MachineInstr
*, 32> Coalesced
;
155 using RegUnitSet
= SparseSet
<uint16_t, identity
<uint16_t>>;
156 /// Set of register units that are used in the current instruction, and so
157 /// cannot be allocated.
158 RegUnitSet UsedInInstr
;
159 RegUnitSet PhysRegUses
;
160 SmallVector
<uint16_t, 8> DefOperandIndexes
;
161 // Register masks attached to the current instruction.
162 SmallVector
<const uint32_t *> RegMasks
;
164 void setPhysRegState(MCPhysReg PhysReg
, unsigned NewState
);
165 bool isPhysRegFree(MCPhysReg PhysReg
) const;
167 /// Mark a physreg as used in this instruction.
168 void markRegUsedInInstr(MCPhysReg PhysReg
) {
169 for (MCRegUnitIterator
Units(PhysReg
, TRI
); Units
.isValid(); ++Units
)
170 UsedInInstr
.insert(*Units
);
173 // Check if physreg is clobbered by instruction's regmask(s).
174 bool isClobberedByRegMasks(MCPhysReg PhysReg
) const {
175 return llvm::any_of(RegMasks
, [PhysReg
](const uint32_t *Mask
) {
176 return MachineOperand::clobbersPhysReg(Mask
, PhysReg
);
180 /// Check if a physreg or any of its aliases are used in this instruction.
181 bool isRegUsedInInstr(MCPhysReg PhysReg
, bool LookAtPhysRegUses
) const {
182 if (LookAtPhysRegUses
&& isClobberedByRegMasks(PhysReg
))
184 for (MCRegUnitIterator
Units(PhysReg
, TRI
); Units
.isValid(); ++Units
) {
185 if (UsedInInstr
.count(*Units
))
187 if (LookAtPhysRegUses
&& PhysRegUses
.count(*Units
))
193 /// Mark physical register as being used in a register use operand.
194 /// This is only used by the special livethrough handling code.
195 void markPhysRegUsedInInstr(MCPhysReg PhysReg
) {
196 for (MCRegUnitIterator
Units(PhysReg
, TRI
); Units
.isValid(); ++Units
)
197 PhysRegUses
.insert(*Units
);
200 /// Remove mark of physical register being used in the instruction.
201 void unmarkRegUsedInInstr(MCPhysReg PhysReg
) {
202 for (MCRegUnitIterator
Units(PhysReg
, TRI
); Units
.isValid(); ++Units
)
203 UsedInInstr
.erase(*Units
);
210 spillImpossible
= ~0u
214 StringRef
getPassName() const override
{ return "Fast Register Allocator"; }
216 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
217 AU
.setPreservesCFG();
218 MachineFunctionPass::getAnalysisUsage(AU
);
221 MachineFunctionProperties
getRequiredProperties() const override
{
222 return MachineFunctionProperties().set(
223 MachineFunctionProperties::Property::NoPHIs
);
226 MachineFunctionProperties
getSetProperties() const override
{
228 return MachineFunctionProperties().set(
229 MachineFunctionProperties::Property::NoVRegs
);
232 return MachineFunctionProperties();
235 MachineFunctionProperties
getClearedProperties() const override
{
236 return MachineFunctionProperties().set(
237 MachineFunctionProperties::Property::IsSSA
);
241 bool runOnMachineFunction(MachineFunction
&MF
) override
;
243 void allocateBasicBlock(MachineBasicBlock
&MBB
);
245 void addRegClassDefCounts(std::vector
<unsigned> &RegClassDefCounts
,
248 void allocateInstruction(MachineInstr
&MI
);
249 void handleDebugValue(MachineInstr
&MI
);
250 void handleBundle(MachineInstr
&MI
);
252 bool usePhysReg(MachineInstr
&MI
, MCPhysReg PhysReg
);
253 bool definePhysReg(MachineInstr
&MI
, MCPhysReg PhysReg
);
254 bool displacePhysReg(MachineInstr
&MI
, MCPhysReg PhysReg
);
255 void freePhysReg(MCPhysReg PhysReg
);
257 unsigned calcSpillCost(MCPhysReg PhysReg
) const;
259 LiveRegMap::iterator
findLiveVirtReg(Register VirtReg
) {
260 return LiveVirtRegs
.find(Register::virtReg2Index(VirtReg
));
263 LiveRegMap::const_iterator
findLiveVirtReg(Register VirtReg
) const {
264 return LiveVirtRegs
.find(Register::virtReg2Index(VirtReg
));
267 void assignVirtToPhysReg(MachineInstr
&MI
, LiveReg
&, MCPhysReg PhysReg
);
268 void allocVirtReg(MachineInstr
&MI
, LiveReg
&LR
, Register Hint
,
269 bool LookAtPhysRegUses
= false);
270 void allocVirtRegUndef(MachineOperand
&MO
);
271 void assignDanglingDebugValues(MachineInstr
&Def
, Register VirtReg
,
273 void defineLiveThroughVirtReg(MachineInstr
&MI
, unsigned OpNum
,
275 void defineVirtReg(MachineInstr
&MI
, unsigned OpNum
, Register VirtReg
,
276 bool LookAtPhysRegUses
= false);
277 void useVirtReg(MachineInstr
&MI
, unsigned OpNum
, Register VirtReg
);
279 MachineBasicBlock::iterator
280 getMBBBeginInsertionPoint(MachineBasicBlock
&MBB
,
281 SmallSet
<Register
, 2> &PrologLiveIns
) const;
283 void reloadAtBegin(MachineBasicBlock
&MBB
);
284 void setPhysReg(MachineInstr
&MI
, MachineOperand
&MO
, MCPhysReg PhysReg
);
286 Register
traceCopies(Register VirtReg
) const;
287 Register
traceCopyChain(Register Reg
) const;
289 int getStackSpaceFor(Register VirtReg
);
290 void spill(MachineBasicBlock::iterator Before
, Register VirtReg
,
291 MCPhysReg AssignedReg
, bool Kill
, bool LiveOut
);
292 void reload(MachineBasicBlock::iterator Before
, Register VirtReg
,
295 bool mayLiveOut(Register VirtReg
);
296 bool mayLiveIn(Register VirtReg
);
298 void dumpState() const;
301 } // end anonymous namespace
303 char RegAllocFast::ID
= 0;
305 INITIALIZE_PASS(RegAllocFast
, "regallocfast", "Fast Register Allocator", false,
308 void RegAllocFast::setPhysRegState(MCPhysReg PhysReg
, unsigned NewState
) {
309 for (MCRegUnitIterator
UI(PhysReg
, TRI
); UI
.isValid(); ++UI
)
310 RegUnitStates
[*UI
] = NewState
;
313 bool RegAllocFast::isPhysRegFree(MCPhysReg PhysReg
) const {
314 for (MCRegUnitIterator
UI(PhysReg
, TRI
); UI
.isValid(); ++UI
) {
315 if (RegUnitStates
[*UI
] != regFree
)
321 /// This allocates space for the specified virtual register to be held on the
323 int RegAllocFast::getStackSpaceFor(Register VirtReg
) {
324 // Find the location Reg would belong...
325 int SS
= StackSlotForVirtReg
[VirtReg
];
326 // Already has space allocated?
330 // Allocate a new stack object for this spill location...
331 const TargetRegisterClass
&RC
= *MRI
->getRegClass(VirtReg
);
332 unsigned Size
= TRI
->getSpillSize(RC
);
333 Align Alignment
= TRI
->getSpillAlign(RC
);
334 int FrameIdx
= MFI
->CreateSpillStackObject(Size
, Alignment
);
337 StackSlotForVirtReg
[VirtReg
] = FrameIdx
;
341 static bool dominates(MachineBasicBlock
&MBB
,
342 MachineBasicBlock::const_iterator A
,
343 MachineBasicBlock::const_iterator B
) {
344 auto MBBEnd
= MBB
.end();
348 MachineBasicBlock::const_iterator I
= MBB
.begin();
349 for (; &*I
!= A
&& &*I
!= B
; ++I
)
355 /// Returns false if \p VirtReg is known to not live out of the current block.
356 bool RegAllocFast::mayLiveOut(Register VirtReg
) {
357 if (MayLiveAcrossBlocks
.test(Register::virtReg2Index(VirtReg
))) {
358 // Cannot be live-out if there are no successors.
359 return !MBB
->succ_empty();
362 const MachineInstr
*SelfLoopDef
= nullptr;
364 // If this block loops back to itself, it is necessary to check whether the
365 // use comes after the def.
366 if (MBB
->isSuccessor(MBB
)) {
367 SelfLoopDef
= MRI
->getUniqueVRegDef(VirtReg
);
369 MayLiveAcrossBlocks
.set(Register::virtReg2Index(VirtReg
));
374 // See if the first \p Limit uses of the register are all in the current
376 static const unsigned Limit
= 8;
378 for (const MachineInstr
&UseInst
: MRI
->use_nodbg_instructions(VirtReg
)) {
379 if (UseInst
.getParent() != MBB
|| ++C
>= Limit
) {
380 MayLiveAcrossBlocks
.set(Register::virtReg2Index(VirtReg
));
381 // Cannot be live-out if there are no successors.
382 return !MBB
->succ_empty();
386 // Try to handle some simple cases to avoid spilling and reloading every
387 // value inside a self looping block.
388 if (SelfLoopDef
== &UseInst
||
389 !dominates(*MBB
, SelfLoopDef
->getIterator(), UseInst
.getIterator())) {
390 MayLiveAcrossBlocks
.set(Register::virtReg2Index(VirtReg
));
399 /// Returns false if \p VirtReg is known to not be live into the current block.
400 bool RegAllocFast::mayLiveIn(Register VirtReg
) {
401 if (MayLiveAcrossBlocks
.test(Register::virtReg2Index(VirtReg
)))
402 return !MBB
->pred_empty();
404 // See if the first \p Limit def of the register are all in the current block.
405 static const unsigned Limit
= 8;
407 for (const MachineInstr
&DefInst
: MRI
->def_instructions(VirtReg
)) {
408 if (DefInst
.getParent() != MBB
|| ++C
>= Limit
) {
409 MayLiveAcrossBlocks
.set(Register::virtReg2Index(VirtReg
));
410 return !MBB
->pred_empty();
417 /// Insert spill instruction for \p AssignedReg before \p Before. Update
418 /// DBG_VALUEs with \p VirtReg operands with the stack slot.
419 void RegAllocFast::spill(MachineBasicBlock::iterator Before
, Register VirtReg
,
420 MCPhysReg AssignedReg
, bool Kill
, bool LiveOut
) {
421 LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg
, TRI
)
422 << " in " << printReg(AssignedReg
, TRI
));
423 int FI
= getStackSpaceFor(VirtReg
);
424 LLVM_DEBUG(dbgs() << " to stack slot #" << FI
<< '\n');
426 const TargetRegisterClass
&RC
= *MRI
->getRegClass(VirtReg
);
427 TII
->storeRegToStackSlot(*MBB
, Before
, AssignedReg
, Kill
, FI
, &RC
, TRI
);
430 MachineBasicBlock::iterator FirstTerm
= MBB
->getFirstTerminator();
432 // When we spill a virtual register, we will have spill instructions behind
433 // every definition of it, meaning we can switch all the DBG_VALUEs over
434 // to just reference the stack slot.
435 SmallVectorImpl
<MachineOperand
*> &LRIDbgOperands
= LiveDbgValueMap
[VirtReg
];
436 SmallMapVector
<MachineInstr
*, SmallVector
<const MachineOperand
*>, 2>
438 for (MachineOperand
*MO
: LRIDbgOperands
)
439 SpilledOperandsMap
[MO
->getParent()].push_back(MO
);
440 for (auto MISpilledOperands
: SpilledOperandsMap
) {
441 MachineInstr
&DBG
= *MISpilledOperands
.first
;
442 MachineInstr
*NewDV
= buildDbgValueForSpill(
443 *MBB
, Before
, *MISpilledOperands
.first
, FI
, MISpilledOperands
.second
);
444 assert(NewDV
->getParent() == MBB
&& "dangling parent pointer");
446 LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV
);
449 // We need to insert a DBG_VALUE at the end of the block if the spill slot
450 // is live out, but there is another use of the value after the
451 // spill. This will allow LiveDebugValues to see the correct live out
452 // value to propagate to the successors.
453 MachineInstr
*ClonedDV
= MBB
->getParent()->CloneMachineInstr(NewDV
);
454 MBB
->insert(FirstTerm
, ClonedDV
);
455 LLVM_DEBUG(dbgs() << "Cloning debug info due to live out spill\n");
458 // Rewrite unassigned dbg_values to use the stack slot.
459 // TODO We can potentially do this for list debug values as well if we know
460 // how the dbg_values are getting unassigned.
461 if (DBG
.isNonListDebugValue()) {
462 MachineOperand
&MO
= DBG
.getDebugOperand(0);
463 if (MO
.isReg() && MO
.getReg() == 0) {
464 updateDbgValueForSpill(DBG
, FI
, 0);
468 // Now this register is spilled there is should not be any DBG_VALUE
469 // pointing to this register because they are all pointing to spilled value
471 LRIDbgOperands
.clear();
474 /// Insert reload instruction for \p PhysReg before \p Before.
475 void RegAllocFast::reload(MachineBasicBlock::iterator Before
, Register VirtReg
,
477 LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg
, TRI
) << " into "
478 << printReg(PhysReg
, TRI
) << '\n');
479 int FI
= getStackSpaceFor(VirtReg
);
480 const TargetRegisterClass
&RC
= *MRI
->getRegClass(VirtReg
);
481 TII
->loadRegFromStackSlot(*MBB
, Before
, PhysReg
, FI
, &RC
, TRI
);
485 /// Get basic block begin insertion point.
486 /// This is not just MBB.begin() because surprisingly we have EH_LABEL
487 /// instructions marking the begin of a basic block. This means we must insert
488 /// new instructions after such labels...
489 MachineBasicBlock::iterator
490 RegAllocFast::getMBBBeginInsertionPoint(
491 MachineBasicBlock
&MBB
, SmallSet
<Register
, 2> &PrologLiveIns
) const {
492 MachineBasicBlock::iterator I
= MBB
.begin();
493 while (I
!= MBB
.end()) {
499 // Most reloads should be inserted after prolog instructions.
500 if (!TII
->isBasicBlockPrologue(*I
))
503 // However if a prolog instruction reads a register that needs to be
504 // reloaded, the reload should be inserted before the prolog.
505 for (MachineOperand
&MO
: I
->operands()) {
507 PrologLiveIns
.insert(MO
.getReg());
516 /// Reload all currently assigned virtual registers.
517 void RegAllocFast::reloadAtBegin(MachineBasicBlock
&MBB
) {
518 if (LiveVirtRegs
.empty())
521 for (MachineBasicBlock::RegisterMaskPair P
: MBB
.liveins()) {
522 MCPhysReg Reg
= P
.PhysReg
;
523 // Set state to live-in. This possibly overrides mappings to virtual
524 // registers but we don't care anymore at this point.
525 setPhysRegState(Reg
, regLiveIn
);
529 SmallSet
<Register
, 2> PrologLiveIns
;
531 // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order
532 // of spilling here is deterministic, if arbitrary.
533 MachineBasicBlock::iterator InsertBefore
534 = getMBBBeginInsertionPoint(MBB
, PrologLiveIns
);
535 for (const LiveReg
&LR
: LiveVirtRegs
) {
536 MCPhysReg PhysReg
= LR
.PhysReg
;
540 MCRegister FirstUnit
= *MCRegUnitIterator(PhysReg
, TRI
);
541 if (RegUnitStates
[FirstUnit
] == regLiveIn
)
544 assert((&MBB
!= &MBB
.getParent()->front() || IgnoreMissingDefs
) &&
545 "no reload in start block. Missing vreg def?");
547 if (PrologLiveIns
.count(PhysReg
)) {
548 // FIXME: Theoretically this should use an insert point skipping labels
549 // but I'm not sure how labels should interact with prolog instruction
550 // that need reloads.
551 reload(MBB
.begin(), LR
.VirtReg
, PhysReg
);
553 reload(InsertBefore
, LR
.VirtReg
, PhysReg
);
555 LiveVirtRegs
.clear();
558 /// Handle the direct use of a physical register. Check that the register is
559 /// not used by a virtreg. Kill the physreg, marking it free. This may add
560 /// implicit kills to MO->getParent() and invalidate MO.
561 bool RegAllocFast::usePhysReg(MachineInstr
&MI
, MCPhysReg Reg
) {
562 assert(Register::isPhysicalRegister(Reg
) && "expected physreg");
563 bool displacedAny
= displacePhysReg(MI
, Reg
);
564 setPhysRegState(Reg
, regPreAssigned
);
565 markRegUsedInInstr(Reg
);
569 bool RegAllocFast::definePhysReg(MachineInstr
&MI
, MCPhysReg Reg
) {
570 bool displacedAny
= displacePhysReg(MI
, Reg
);
571 setPhysRegState(Reg
, regPreAssigned
);
575 /// Mark PhysReg as reserved or free after spilling any virtregs. This is very
576 /// similar to defineVirtReg except the physreg is reserved instead of
578 bool RegAllocFast::displacePhysReg(MachineInstr
&MI
, MCPhysReg PhysReg
) {
579 bool displacedAny
= false;
581 for (MCRegUnitIterator
UI(PhysReg
, TRI
); UI
.isValid(); ++UI
) {
583 switch (unsigned VirtReg
= RegUnitStates
[Unit
]) {
585 LiveRegMap::iterator LRI
= findLiveVirtReg(VirtReg
);
586 assert(LRI
!= LiveVirtRegs
.end() && "datastructures in sync");
587 MachineBasicBlock::iterator ReloadBefore
=
588 std::next((MachineBasicBlock::iterator
)MI
.getIterator());
589 reload(ReloadBefore
, VirtReg
, LRI
->PhysReg
);
591 setPhysRegState(LRI
->PhysReg
, regFree
);
593 LRI
->Reloaded
= true;
598 RegUnitStates
[Unit
] = regFree
;
608 void RegAllocFast::freePhysReg(MCPhysReg PhysReg
) {
609 LLVM_DEBUG(dbgs() << "Freeing " << printReg(PhysReg
, TRI
) << ':');
611 MCRegister FirstUnit
= *MCRegUnitIterator(PhysReg
, TRI
);
612 switch (unsigned VirtReg
= RegUnitStates
[FirstUnit
]) {
614 LLVM_DEBUG(dbgs() << '\n');
617 LLVM_DEBUG(dbgs() << '\n');
618 setPhysRegState(PhysReg
, regFree
);
621 LiveRegMap::iterator LRI
= findLiveVirtReg(VirtReg
);
622 assert(LRI
!= LiveVirtRegs
.end());
623 LLVM_DEBUG(dbgs() << ' ' << printReg(LRI
->VirtReg
, TRI
) << '\n');
624 setPhysRegState(LRI
->PhysReg
, regFree
);
631 /// Return the cost of spilling clearing out PhysReg and aliases so it is free
632 /// for allocation. Returns 0 when PhysReg is free or disabled with all aliases
633 /// disabled - it can be allocated directly.
634 /// \returns spillImpossible when PhysReg or an alias can't be spilled.
635 unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg
) const {
636 for (MCRegUnitIterator
UI(PhysReg
, TRI
); UI
.isValid(); ++UI
) {
637 switch (unsigned VirtReg
= RegUnitStates
[*UI
]) {
641 LLVM_DEBUG(dbgs() << "Cannot spill pre-assigned "
642 << printReg(PhysReg
, TRI
) << '\n');
643 return spillImpossible
;
645 bool SureSpill
= StackSlotForVirtReg
[VirtReg
] != -1 ||
646 findLiveVirtReg(VirtReg
)->LiveOut
;
647 return SureSpill
? spillClean
: spillDirty
;
654 void RegAllocFast::assignDanglingDebugValues(MachineInstr
&Definition
,
655 Register VirtReg
, MCPhysReg Reg
) {
656 auto UDBGValIter
= DanglingDbgValues
.find(VirtReg
);
657 if (UDBGValIter
== DanglingDbgValues
.end())
660 SmallVectorImpl
<MachineInstr
*> &Dangling
= UDBGValIter
->second
;
661 for (MachineInstr
*DbgValue
: Dangling
) {
662 assert(DbgValue
->isDebugValue());
663 if (!DbgValue
->hasDebugOperandForReg(VirtReg
))
666 // Test whether the physreg survives from the definition to the DBG_VALUE.
667 MCPhysReg SetToReg
= Reg
;
669 for (MachineBasicBlock::iterator I
= std::next(Definition
.getIterator()),
670 E
= DbgValue
->getIterator(); I
!= E
; ++I
) {
671 if (I
->modifiesRegister(Reg
, TRI
) || --Limit
== 0) {
672 LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
678 for (MachineOperand
&MO
: DbgValue
->getDebugOperandsForReg(VirtReg
)) {
687 /// This method updates local state so that we know that PhysReg is the
688 /// proper container for VirtReg now. The physical register must not be used
689 /// for anything else when this is called.
690 void RegAllocFast::assignVirtToPhysReg(MachineInstr
&AtMI
, LiveReg
&LR
,
692 Register VirtReg
= LR
.VirtReg
;
693 LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg
, TRI
) << " to "
694 << printReg(PhysReg
, TRI
) << '\n');
695 assert(LR
.PhysReg
== 0 && "Already assigned a physreg");
696 assert(PhysReg
!= 0 && "Trying to assign no register");
697 LR
.PhysReg
= PhysReg
;
698 setPhysRegState(PhysReg
, VirtReg
);
700 assignDanglingDebugValues(AtMI
, VirtReg
, PhysReg
);
703 static bool isCoalescable(const MachineInstr
&MI
) {
704 return MI
.isFullCopy();
707 Register
RegAllocFast::traceCopyChain(Register Reg
) const {
708 static const unsigned ChainLengthLimit
= 3;
711 if (Reg
.isPhysical())
713 assert(Reg
.isVirtual());
715 MachineInstr
*VRegDef
= MRI
->getUniqueVRegDef(Reg
);
716 if (!VRegDef
|| !isCoalescable(*VRegDef
))
718 Reg
= VRegDef
->getOperand(1).getReg();
719 } while (++C
<= ChainLengthLimit
);
723 /// Check if any of \p VirtReg's definitions is a copy. If it is follow the
724 /// chain of copies to check whether we reach a physical register we can
726 Register
RegAllocFast::traceCopies(Register VirtReg
) const {
727 static const unsigned DefLimit
= 3;
729 for (const MachineInstr
&MI
: MRI
->def_instructions(VirtReg
)) {
730 if (isCoalescable(MI
)) {
731 Register Reg
= MI
.getOperand(1).getReg();
732 Reg
= traceCopyChain(Reg
);
743 /// Allocates a physical register for VirtReg.
744 void RegAllocFast::allocVirtReg(MachineInstr
&MI
, LiveReg
&LR
,
745 Register Hint0
, bool LookAtPhysRegUses
) {
746 const Register VirtReg
= LR
.VirtReg
;
747 assert(LR
.PhysReg
== 0);
749 const TargetRegisterClass
&RC
= *MRI
->getRegClass(VirtReg
);
750 LLVM_DEBUG(dbgs() << "Search register for " << printReg(VirtReg
)
751 << " in class " << TRI
->getRegClassName(&RC
)
752 << " with hint " << printReg(Hint0
, TRI
) << '\n');
754 // Take hint when possible.
755 if (Hint0
.isPhysical() && MRI
->isAllocatable(Hint0
) && RC
.contains(Hint0
) &&
756 !isRegUsedInInstr(Hint0
, LookAtPhysRegUses
)) {
757 // Take hint if the register is currently free.
758 if (isPhysRegFree(Hint0
)) {
759 LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint0
, TRI
)
761 assignVirtToPhysReg(MI
, LR
, Hint0
);
764 LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint0
, TRI
)
773 Register Hint1
= traceCopies(VirtReg
);
774 if (Hint1
.isPhysical() && MRI
->isAllocatable(Hint1
) && RC
.contains(Hint1
) &&
775 !isRegUsedInInstr(Hint1
, LookAtPhysRegUses
)) {
776 // Take hint if the register is currently free.
777 if (isPhysRegFree(Hint1
)) {
778 LLVM_DEBUG(dbgs() << "\tPreferred Register 0: " << printReg(Hint1
, TRI
)
780 assignVirtToPhysReg(MI
, LR
, Hint1
);
783 LLVM_DEBUG(dbgs() << "\tPreferred Register 1: " << printReg(Hint1
, TRI
)
790 MCPhysReg BestReg
= 0;
791 unsigned BestCost
= spillImpossible
;
792 ArrayRef
<MCPhysReg
> AllocationOrder
= RegClassInfo
.getOrder(&RC
);
793 for (MCPhysReg PhysReg
: AllocationOrder
) {
794 LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg
, TRI
) << ' ');
795 if (isRegUsedInInstr(PhysReg
, LookAtPhysRegUses
)) {
796 LLVM_DEBUG(dbgs() << "already used in instr.\n");
800 unsigned Cost
= calcSpillCost(PhysReg
);
801 LLVM_DEBUG(dbgs() << "Cost: " << Cost
<< " BestCost: " << BestCost
<< '\n');
802 // Immediate take a register with cost 0.
804 assignVirtToPhysReg(MI
, LR
, PhysReg
);
808 if (PhysReg
== Hint0
|| PhysReg
== Hint1
)
809 Cost
-= spillPrefBonus
;
811 if (Cost
< BestCost
) {
818 // Nothing we can do: Report an error and keep going with an invalid
820 if (MI
.isInlineAsm())
821 MI
.emitError("inline assembly requires more registers than available");
823 MI
.emitError("ran out of registers during register allocation");
830 displacePhysReg(MI
, BestReg
);
831 assignVirtToPhysReg(MI
, LR
, BestReg
);
834 void RegAllocFast::allocVirtRegUndef(MachineOperand
&MO
) {
835 assert(MO
.isUndef() && "expected undef use");
836 Register VirtReg
= MO
.getReg();
837 assert(Register::isVirtualRegister(VirtReg
) && "Expected virtreg");
839 LiveRegMap::const_iterator LRI
= findLiveVirtReg(VirtReg
);
841 if (LRI
!= LiveVirtRegs
.end() && LRI
->PhysReg
) {
842 PhysReg
= LRI
->PhysReg
;
844 const TargetRegisterClass
&RC
= *MRI
->getRegClass(VirtReg
);
845 ArrayRef
<MCPhysReg
> AllocationOrder
= RegClassInfo
.getOrder(&RC
);
846 assert(!AllocationOrder
.empty() && "Allocation order must not be empty");
847 PhysReg
= AllocationOrder
[0];
850 unsigned SubRegIdx
= MO
.getSubReg();
851 if (SubRegIdx
!= 0) {
852 PhysReg
= TRI
->getSubReg(PhysReg
, SubRegIdx
);
856 MO
.setIsRenamable(true);
859 /// Variation of defineVirtReg() with special handling for livethrough regs
860 /// (tied or earlyclobber) that may interfere with preassigned uses.
861 void RegAllocFast::defineLiveThroughVirtReg(MachineInstr
&MI
, unsigned OpNum
,
863 LiveRegMap::iterator LRI
= findLiveVirtReg(VirtReg
);
864 if (LRI
!= LiveVirtRegs
.end()) {
865 MCPhysReg PrevReg
= LRI
->PhysReg
;
866 if (PrevReg
!= 0 && isRegUsedInInstr(PrevReg
, true)) {
867 LLVM_DEBUG(dbgs() << "Need new assignment for " << printReg(PrevReg
, TRI
)
868 << " (tied/earlyclobber resolution)\n");
869 freePhysReg(PrevReg
);
871 allocVirtReg(MI
, *LRI
, 0, true);
872 MachineBasicBlock::iterator InsertBefore
=
873 std::next((MachineBasicBlock::iterator
)MI
.getIterator());
874 LLVM_DEBUG(dbgs() << "Copy " << printReg(LRI
->PhysReg
, TRI
) << " to "
875 << printReg(PrevReg
, TRI
) << '\n');
876 BuildMI(*MBB
, InsertBefore
, MI
.getDebugLoc(),
877 TII
->get(TargetOpcode::COPY
), PrevReg
)
878 .addReg(LRI
->PhysReg
, llvm::RegState::Kill
);
880 MachineOperand
&MO
= MI
.getOperand(OpNum
);
881 if (MO
.getSubReg() && !MO
.isUndef()) {
885 return defineVirtReg(MI
, OpNum
, VirtReg
, true);
888 /// Allocates a register for VirtReg definition. Typically the register is
889 /// already assigned from a use of the virtreg, however we still need to
890 /// perform an allocation if:
891 /// - It is a dead definition without any uses.
892 /// - The value is live out and all uses are in different basic blocks.
893 void RegAllocFast::defineVirtReg(MachineInstr
&MI
, unsigned OpNum
,
894 Register VirtReg
, bool LookAtPhysRegUses
) {
895 assert(VirtReg
.isVirtual() && "Not a virtual register");
896 MachineOperand
&MO
= MI
.getOperand(OpNum
);
897 LiveRegMap::iterator LRI
;
899 std::tie(LRI
, New
) = LiveVirtRegs
.insert(LiveReg(VirtReg
));
902 if (mayLiveOut(VirtReg
)) {
905 // It is a dead def without the dead flag; add the flag now.
910 if (LRI
->PhysReg
== 0)
911 allocVirtReg(MI
, *LRI
, 0, LookAtPhysRegUses
);
913 assert(!isRegUsedInInstr(LRI
->PhysReg
, LookAtPhysRegUses
) &&
914 "TODO: preassign mismatch");
915 LLVM_DEBUG(dbgs() << "In def of " << printReg(VirtReg
, TRI
)
916 << " use existing assignment to "
917 << printReg(LRI
->PhysReg
, TRI
) << '\n');
920 MCPhysReg PhysReg
= LRI
->PhysReg
;
921 assert(PhysReg
!= 0 && "Register not assigned");
922 if (LRI
->Reloaded
|| LRI
->LiveOut
) {
923 if (!MI
.isImplicitDef()) {
924 MachineBasicBlock::iterator SpillBefore
=
925 std::next((MachineBasicBlock::iterator
)MI
.getIterator());
926 LLVM_DEBUG(dbgs() << "Spill Reason: LO: " << LRI
->LiveOut
<< " RL: "
927 << LRI
->Reloaded
<< '\n');
928 bool Kill
= LRI
->LastUse
== nullptr;
929 spill(SpillBefore
, VirtReg
, PhysReg
, Kill
, LRI
->LiveOut
);
930 LRI
->LastUse
= nullptr;
932 LRI
->LiveOut
= false;
933 LRI
->Reloaded
= false;
935 if (MI
.getOpcode() == TargetOpcode::BUNDLE
) {
936 BundleVirtRegsMap
[VirtReg
] = PhysReg
;
938 markRegUsedInInstr(PhysReg
);
939 setPhysReg(MI
, MO
, PhysReg
);
942 /// Allocates a register for a VirtReg use.
943 void RegAllocFast::useVirtReg(MachineInstr
&MI
, unsigned OpNum
,
945 assert(VirtReg
.isVirtual() && "Not a virtual register");
946 MachineOperand
&MO
= MI
.getOperand(OpNum
);
947 LiveRegMap::iterator LRI
;
949 std::tie(LRI
, New
) = LiveVirtRegs
.insert(LiveReg(VirtReg
));
951 MachineOperand
&MO
= MI
.getOperand(OpNum
);
953 if (mayLiveOut(VirtReg
)) {
956 // It is a last (killing) use without the kill flag; add the flag now.
961 assert((!MO
.isKill() || LRI
->LastUse
== &MI
) && "Invalid kill flag");
964 // If necessary allocate a register.
965 if (LRI
->PhysReg
== 0) {
966 assert(!MO
.isTied() && "tied op should be allocated");
968 if (MI
.isCopy() && MI
.getOperand(1).getSubReg() == 0) {
969 Hint
= MI
.getOperand(0).getReg();
970 assert(Hint
.isPhysical() &&
971 "Copy destination should already be assigned");
973 allocVirtReg(MI
, *LRI
, Hint
, false);
975 const TargetRegisterClass
&RC
= *MRI
->getRegClass(VirtReg
);
976 ArrayRef
<MCPhysReg
> AllocationOrder
= RegClassInfo
.getOrder(&RC
);
977 setPhysReg(MI
, MO
, *AllocationOrder
.begin());
984 if (MI
.getOpcode() == TargetOpcode::BUNDLE
) {
985 BundleVirtRegsMap
[VirtReg
] = LRI
->PhysReg
;
987 markRegUsedInInstr(LRI
->PhysReg
);
988 setPhysReg(MI
, MO
, LRI
->PhysReg
);
991 /// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This
992 /// may invalidate any operand pointers. Return true if the operand kills its
994 void RegAllocFast::setPhysReg(MachineInstr
&MI
, MachineOperand
&MO
,
996 if (!MO
.getSubReg()) {
998 MO
.setIsRenamable(true);
1002 // Handle subregister index.
1003 MO
.setReg(PhysReg
? TRI
->getSubReg(PhysReg
, MO
.getSubReg()) : MCRegister());
1004 MO
.setIsRenamable(true);
1005 // Note: We leave the subreg number around a little longer in case of defs.
1006 // This is so that the register freeing logic in allocateInstruction can still
1007 // recognize this as subregister defs. The code there will clear the number.
1011 // A kill flag implies killing the full register. Add corresponding super
1014 MI
.addRegisterKilled(PhysReg
, TRI
, true);
1018 // A <def,read-undef> of a sub-register requires an implicit def of the full
1020 if (MO
.isDef() && MO
.isUndef()) {
1022 MI
.addRegisterDead(PhysReg
, TRI
, true);
1024 MI
.addRegisterDefined(PhysReg
, TRI
);
1030 void RegAllocFast::dumpState() const {
1031 for (unsigned Unit
= 1, UnitE
= TRI
->getNumRegUnits(); Unit
!= UnitE
;
1033 switch (unsigned VirtReg
= RegUnitStates
[Unit
]) {
1036 case regPreAssigned
:
1037 dbgs() << " " << printRegUnit(Unit
, TRI
) << "[P]";
1040 llvm_unreachable("Should not have regLiveIn in map");
1042 dbgs() << ' ' << printRegUnit(Unit
, TRI
) << '=' << printReg(VirtReg
);
1043 LiveRegMap::const_iterator I
= findLiveVirtReg(VirtReg
);
1044 assert(I
!= LiveVirtRegs
.end() && "have LiveVirtRegs entry");
1045 if (I
->LiveOut
|| I
->Reloaded
) {
1047 if (I
->LiveOut
) dbgs() << 'O';
1048 if (I
->Reloaded
) dbgs() << 'R';
1051 assert(TRI
->hasRegUnit(I
->PhysReg
, Unit
) && "inverse mapping present");
1057 // Check that LiveVirtRegs is the inverse.
1058 for (const LiveReg
&LR
: LiveVirtRegs
) {
1059 Register VirtReg
= LR
.VirtReg
;
1060 assert(VirtReg
.isVirtual() && "Bad map key");
1061 MCPhysReg PhysReg
= LR
.PhysReg
;
1063 assert(Register::isPhysicalRegister(PhysReg
) &&
1064 "mapped to physreg");
1065 for (MCRegUnitIterator
UI(PhysReg
, TRI
); UI
.isValid(); ++UI
) {
1066 assert(RegUnitStates
[*UI
] == VirtReg
&& "inverse map valid");
1073 /// Count number of defs consumed from each register class by \p Reg
1074 void RegAllocFast::addRegClassDefCounts(std::vector
<unsigned> &RegClassDefCounts
,
1075 Register Reg
) const {
1076 assert(RegClassDefCounts
.size() == TRI
->getNumRegClasses());
1078 if (Reg
.isVirtual()) {
1079 const TargetRegisterClass
*OpRC
= MRI
->getRegClass(Reg
);
1080 for (unsigned RCIdx
= 0, RCIdxEnd
= TRI
->getNumRegClasses();
1081 RCIdx
!= RCIdxEnd
; ++RCIdx
) {
1082 const TargetRegisterClass
*IdxRC
= TRI
->getRegClass(RCIdx
);
1083 // FIXME: Consider aliasing sub/super registers.
1084 if (OpRC
->hasSubClassEq(IdxRC
))
1085 ++RegClassDefCounts
[RCIdx
];
1091 for (unsigned RCIdx
= 0, RCIdxEnd
= TRI
->getNumRegClasses();
1092 RCIdx
!= RCIdxEnd
; ++RCIdx
) {
1093 const TargetRegisterClass
*IdxRC
= TRI
->getRegClass(RCIdx
);
1094 for (MCRegAliasIterator
Alias(Reg
, TRI
, true); Alias
.isValid(); ++Alias
) {
1095 if (IdxRC
->contains(*Alias
)) {
1096 ++RegClassDefCounts
[RCIdx
];
1103 void RegAllocFast::allocateInstruction(MachineInstr
&MI
) {
1104 // The basic algorithm here is:
1105 // 1. Mark registers of def operands as free
1106 // 2. Allocate registers to use operands and place reload instructions for
1107 // registers displaced by the allocation.
1109 // However we need to handle some corner cases:
1110 // - pre-assigned defs and uses need to be handled before the other def/use
1111 // operands are processed to avoid the allocation heuristics clashing with
1112 // the pre-assignment.
1113 // - The "free def operands" step has to come last instead of first for tied
1114 // operands and early-clobbers.
1116 UsedInInstr
.clear();
1118 BundleVirtRegsMap
.clear();
1120 // Scan for special cases; Apply pre-assigned register defs to state.
1121 bool HasPhysRegUse
= false;
1122 bool HasRegMask
= false;
1123 bool HasVRegDef
= false;
1124 bool HasDef
= false;
1125 bool HasEarlyClobber
= false;
1126 bool NeedToAssignLiveThroughs
= false;
1127 for (MachineOperand
&MO
: MI
.operands()) {
1129 Register Reg
= MO
.getReg();
1130 if (Reg
.isVirtual()) {
1134 if (MO
.isEarlyClobber()) {
1135 HasEarlyClobber
= true;
1136 NeedToAssignLiveThroughs
= true;
1138 if (MO
.isTied() || (MO
.getSubReg() != 0 && !MO
.isUndef()))
1139 NeedToAssignLiveThroughs
= true;
1141 } else if (Reg
.isPhysical()) {
1142 if (!MRI
->isReserved(Reg
)) {
1145 bool displacedAny
= definePhysReg(MI
, Reg
);
1146 if (MO
.isEarlyClobber())
1147 HasEarlyClobber
= true;
1152 HasPhysRegUse
= true;
1155 } else if (MO
.isRegMask()) {
1157 RegMasks
.push_back(MO
.getRegMask());
1161 // Allocate virtreg defs.
1164 // Special handling for early clobbers, tied operands or subregister defs:
1165 // Compared to "normal" defs these:
1166 // - Must not use a register that is pre-assigned for a use operand.
1167 // - In order to solve tricky inline assembly constraints we change the
1168 // heuristic to figure out a good operand order before doing
1170 if (NeedToAssignLiveThroughs
) {
1171 DefOperandIndexes
.clear();
1172 PhysRegUses
.clear();
1174 // Track number of defs which may consume a register from the class.
1175 std::vector
<unsigned> RegClassDefCounts(TRI
->getNumRegClasses(), 0);
1176 assert(RegClassDefCounts
[0] == 0);
1178 LLVM_DEBUG(dbgs() << "Need to assign livethroughs\n");
1179 for (unsigned I
= 0, E
= MI
.getNumOperands(); I
< E
; ++I
) {
1180 const MachineOperand
&MO
= MI
.getOperand(I
);
1183 Register Reg
= MO
.getReg();
1184 if (MO
.readsReg()) {
1185 if (Reg
.isPhysical()) {
1186 LLVM_DEBUG(dbgs() << "mark extra used: " << printReg(Reg
, TRI
)
1188 markPhysRegUsedInInstr(Reg
);
1193 if (Reg
.isVirtual())
1194 DefOperandIndexes
.push_back(I
);
1196 addRegClassDefCounts(RegClassDefCounts
, Reg
);
1200 llvm::sort(DefOperandIndexes
, [&](uint16_t I0
, uint16_t I1
) {
1201 const MachineOperand
&MO0
= MI
.getOperand(I0
);
1202 const MachineOperand
&MO1
= MI
.getOperand(I1
);
1203 Register Reg0
= MO0
.getReg();
1204 Register Reg1
= MO1
.getReg();
1205 const TargetRegisterClass
&RC0
= *MRI
->getRegClass(Reg0
);
1206 const TargetRegisterClass
&RC1
= *MRI
->getRegClass(Reg1
);
1208 // Identify regclass that are easy to use up completely just in this
1210 unsigned ClassSize0
= RegClassInfo
.getOrder(&RC0
).size();
1211 unsigned ClassSize1
= RegClassInfo
.getOrder(&RC1
).size();
1213 bool SmallClass0
= ClassSize0
< RegClassDefCounts
[RC0
.getID()];
1214 bool SmallClass1
= ClassSize1
< RegClassDefCounts
[RC1
.getID()];
1215 if (SmallClass0
> SmallClass1
)
1217 if (SmallClass0
< SmallClass1
)
1220 // Allocate early clobbers and livethrough operands first.
1221 bool Livethrough0
= MO0
.isEarlyClobber() || MO0
.isTied() ||
1222 (MO0
.getSubReg() == 0 && !MO0
.isUndef());
1223 bool Livethrough1
= MO1
.isEarlyClobber() || MO1
.isTied() ||
1224 (MO1
.getSubReg() == 0 && !MO1
.isUndef());
1225 if (Livethrough0
> Livethrough1
)
1227 if (Livethrough0
< Livethrough1
)
1230 // Tie-break rule: operand index.
1234 for (uint16_t OpIdx
: DefOperandIndexes
) {
1235 MachineOperand
&MO
= MI
.getOperand(OpIdx
);
1236 LLVM_DEBUG(dbgs() << "Allocating " << MO
<< '\n');
1237 unsigned Reg
= MO
.getReg();
1238 if (MO
.isEarlyClobber() || MO
.isTied() ||
1239 (MO
.getSubReg() && !MO
.isUndef())) {
1240 defineLiveThroughVirtReg(MI
, OpIdx
, Reg
);
1242 defineVirtReg(MI
, OpIdx
, Reg
);
1246 // Assign virtual register defs.
1247 for (unsigned I
= 0, E
= MI
.getNumOperands(); I
< E
; ++I
) {
1248 MachineOperand
&MO
= MI
.getOperand(I
);
1249 if (!MO
.isReg() || !MO
.isDef())
1251 Register Reg
= MO
.getReg();
1252 if (Reg
.isVirtual())
1253 defineVirtReg(MI
, I
, Reg
);
1258 // Free registers occupied by defs.
1259 // Iterate operands in reverse order, so we see the implicit super register
1260 // defs first (we added them earlier in case of <def,read-undef>).
1261 for (MachineOperand
&MO
: llvm::reverse(MI
.operands())) {
1262 if (!MO
.isReg() || !MO
.isDef())
1265 // subreg defs don't free the full register. We left the subreg number
1266 // around as a marker in setPhysReg() to recognize this case here.
1267 if (MO
.getSubReg() != 0) {
1272 assert((!MO
.isTied() || !isClobberedByRegMasks(MO
.getReg())) &&
1273 "tied def assigned to clobbered register");
1275 // Do not free tied operands and early clobbers.
1276 if (MO
.isTied() || MO
.isEarlyClobber())
1278 Register Reg
= MO
.getReg();
1281 assert(Reg
.isPhysical());
1282 if (MRI
->isReserved(Reg
))
1285 unmarkRegUsedInInstr(Reg
);
1289 // Displace clobbered registers.
1291 assert(!RegMasks
.empty() && "expected RegMask");
1293 for (const auto *RM
: RegMasks
)
1294 MRI
->addPhysRegsUsedFromRegMask(RM
);
1296 // Displace clobbered registers.
1297 for (const LiveReg
&LR
: LiveVirtRegs
) {
1298 MCPhysReg PhysReg
= LR
.PhysReg
;
1299 if (PhysReg
!= 0 && isClobberedByRegMasks(PhysReg
))
1300 displacePhysReg(MI
, PhysReg
);
1304 // Apply pre-assigned register uses to state.
1305 if (HasPhysRegUse
) {
1306 for (MachineOperand
&MO
: MI
.operands()) {
1307 if (!MO
.isReg() || !MO
.readsReg())
1309 Register Reg
= MO
.getReg();
1310 if (!Reg
.isPhysical())
1312 if (MRI
->isReserved(Reg
))
1314 bool displacedAny
= usePhysReg(MI
, Reg
);
1315 if (!displacedAny
&& !MRI
->isReserved(Reg
))
1320 // Allocate virtreg uses and insert reloads as necessary.
1321 bool HasUndefUse
= false;
1322 for (unsigned I
= 0; I
< MI
.getNumOperands(); ++I
) {
1323 MachineOperand
&MO
= MI
.getOperand(I
);
1324 if (!MO
.isReg() || !MO
.isUse())
1326 Register Reg
= MO
.getReg();
1327 if (!Reg
.isVirtual())
1336 // Populate MayLiveAcrossBlocks in case the use block is allocated before
1337 // the def block (removing the vreg uses).
1341 assert(!MO
.isInternalRead() && "Bundles not supported");
1342 assert(MO
.readsReg() && "reading use");
1343 useVirtReg(MI
, I
, Reg
);
1346 // Allocate undef operands. This is a separate step because in a situation
1347 // like ` = OP undef %X, %X` both operands need the same register assign
1348 // so we should perform the normal assignment first.
1350 for (MachineOperand
&MO
: MI
.uses()) {
1351 if (!MO
.isReg() || !MO
.isUse())
1353 Register Reg
= MO
.getReg();
1354 if (!Reg
.isVirtual())
1357 assert(MO
.isUndef() && "Should only have undef virtreg uses left");
1358 allocVirtRegUndef(MO
);
1362 // Free early clobbers.
1363 if (HasEarlyClobber
) {
1364 for (MachineOperand
&MO
: llvm::reverse(MI
.operands())) {
1365 if (!MO
.isReg() || !MO
.isDef() || !MO
.isEarlyClobber())
1367 // subreg defs don't free the full register. We left the subreg number
1368 // around as a marker in setPhysReg() to recognize this case here.
1369 if (MO
.getSubReg() != 0) {
1374 Register Reg
= MO
.getReg();
1377 assert(Reg
.isPhysical() && "should have register assigned");
1379 // We sometimes get odd situations like:
1380 // early-clobber %x0 = INSTRUCTION %x0
1381 // which is semantically questionable as the early-clobber should
1382 // apply before the use. But in practice we consider the use to
1383 // happen before the early clobber now. Don't free the early clobber
1384 // register in this case.
1385 if (MI
.readsRegister(Reg
, TRI
))
1392 LLVM_DEBUG(dbgs() << "<< " << MI
);
1393 if (MI
.isCopy() && MI
.getOperand(0).getReg() == MI
.getOperand(1).getReg() &&
1394 MI
.getNumOperands() == 2) {
1395 LLVM_DEBUG(dbgs() << "Mark identity copy for removal\n");
1396 Coalesced
.push_back(&MI
);
1400 void RegAllocFast::handleDebugValue(MachineInstr
&MI
) {
1401 // Ignore DBG_VALUEs that aren't based on virtual registers. These are
1402 // mostly constants and frame indices.
1403 for (Register Reg
: MI
.getUsedDebugRegs()) {
1404 if (!Register::isVirtualRegister(Reg
))
1407 // Already spilled to a stackslot?
1408 int SS
= StackSlotForVirtReg
[Reg
];
1410 // Modify DBG_VALUE now that the value is in a spill slot.
1411 updateDbgValueForSpill(MI
, SS
, Reg
);
1412 LLVM_DEBUG(dbgs() << "Rewrite DBG_VALUE for spilled memory: " << MI
);
1416 // See if this virtual register has already been allocated to a physical
1417 // register or spilled to a stack slot.
1418 LiveRegMap::iterator LRI
= findLiveVirtReg(Reg
);
1419 SmallVector
<MachineOperand
*> DbgOps
;
1420 for (MachineOperand
&Op
: MI
.getDebugOperandsForReg(Reg
))
1421 DbgOps
.push_back(&Op
);
1423 if (LRI
!= LiveVirtRegs
.end() && LRI
->PhysReg
) {
1424 // Update every use of Reg within MI.
1425 for (auto &RegMO
: DbgOps
)
1426 setPhysReg(MI
, *RegMO
, LRI
->PhysReg
);
1428 DanglingDbgValues
[Reg
].push_back(&MI
);
1431 // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so
1432 // that future spills of Reg will have DBG_VALUEs.
1433 LiveDbgValueMap
[Reg
].append(DbgOps
.begin(), DbgOps
.end());
1437 void RegAllocFast::handleBundle(MachineInstr
&MI
) {
1438 MachineBasicBlock::instr_iterator BundledMI
= MI
.getIterator();
1440 while (BundledMI
->isBundledWithPred()) {
1441 for (MachineOperand
&MO
: BundledMI
->operands()) {
1445 Register Reg
= MO
.getReg();
1446 if (!Reg
.isVirtual())
1449 DenseMap
<Register
, MCPhysReg
>::iterator DI
;
1450 DI
= BundleVirtRegsMap
.find(Reg
);
1451 assert(DI
!= BundleVirtRegsMap
.end() && "Unassigned virtual register");
1453 setPhysReg(MI
, MO
, DI
->second
);
1460 void RegAllocFast::allocateBasicBlock(MachineBasicBlock
&MBB
) {
1462 LLVM_DEBUG(dbgs() << "\nAllocating " << MBB
);
1464 RegUnitStates
.assign(TRI
->getNumRegUnits(), regFree
);
1465 assert(LiveVirtRegs
.empty() && "Mapping not cleared from last block?");
1467 for (auto &LiveReg
: MBB
.liveouts())
1468 setPhysRegState(LiveReg
.PhysReg
, regPreAssigned
);
1472 // Traverse block in reverse order allocating instructions one by one.
1473 for (MachineInstr
&MI
: reverse(MBB
)) {
1475 dbgs() << "\n>> " << MI
<< "Regs:";
1479 // Special handling for debug values. Note that they are not allowed to
1480 // affect codegen of the other instructions in any way.
1481 if (MI
.isDebugValue()) {
1482 handleDebugValue(MI
);
1486 allocateInstruction(MI
);
1488 // Once BUNDLE header is assigned registers, same assignments need to be
1489 // done for bundled MIs.
1490 if (MI
.getOpcode() == TargetOpcode::BUNDLE
) {
1496 dbgs() << "Begin Regs:";
1500 // Spill all physical registers holding virtual registers now.
1501 LLVM_DEBUG(dbgs() << "Loading live registers at begin of block.\n");
1504 // Erase all the coalesced copies. We are delaying it until now because
1505 // LiveVirtRegs might refer to the instrs.
1506 for (MachineInstr
*MI
: Coalesced
)
1508 NumCoalesced
+= Coalesced
.size();
1510 for (auto &UDBGPair
: DanglingDbgValues
) {
1511 for (MachineInstr
*DbgValue
: UDBGPair
.second
) {
1512 assert(DbgValue
->isDebugValue() && "expected DBG_VALUE");
1513 // Nothing to do if the vreg was spilled in the meantime.
1514 if (!DbgValue
->hasDebugOperandForReg(UDBGPair
.first
))
1516 LLVM_DEBUG(dbgs() << "Register did not survive for " << *DbgValue
1518 DbgValue
->setDebugValueUndef();
1521 DanglingDbgValues
.clear();
1523 LLVM_DEBUG(MBB
.dump());
1526 bool RegAllocFast::runOnMachineFunction(MachineFunction
&MF
) {
1527 LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n"
1528 << "********** Function: " << MF
.getName() << '\n');
1529 MRI
= &MF
.getRegInfo();
1530 const TargetSubtargetInfo
&STI
= MF
.getSubtarget();
1531 TRI
= STI
.getRegisterInfo();
1532 TII
= STI
.getInstrInfo();
1533 MFI
= &MF
.getFrameInfo();
1534 MRI
->freezeReservedRegs(MF
);
1535 RegClassInfo
.runOnMachineFunction(MF
);
1536 unsigned NumRegUnits
= TRI
->getNumRegUnits();
1537 UsedInInstr
.clear();
1538 UsedInInstr
.setUniverse(NumRegUnits
);
1539 PhysRegUses
.clear();
1540 PhysRegUses
.setUniverse(NumRegUnits
);
1542 // initialize the virtual->physical register map to have a 'null'
1543 // mapping for all virtual registers
1544 unsigned NumVirtRegs
= MRI
->getNumVirtRegs();
1545 StackSlotForVirtReg
.resize(NumVirtRegs
);
1546 LiveVirtRegs
.setUniverse(NumVirtRegs
);
1547 MayLiveAcrossBlocks
.clear();
1548 MayLiveAcrossBlocks
.resize(NumVirtRegs
);
1550 // Loop over all of the basic blocks, eliminating virtual register references
1551 for (MachineBasicBlock
&MBB
: MF
)
1552 allocateBasicBlock(MBB
);
1554 if (ClearVirtRegs
) {
1555 // All machine operands and other references to virtual registers have been
1556 // replaced. Remove the virtual registers.
1557 MRI
->clearVirtRegs();
1560 StackSlotForVirtReg
.clear();
1561 LiveDbgValueMap
.clear();
1565 FunctionPass
*llvm::createFastRegisterAllocator() {
1566 return new RegAllocFast();
1569 FunctionPass
*llvm::createFastRegisterAllocator(
1570 std::function
<bool(const TargetRegisterInfo
&TRI
,
1571 const TargetRegisterClass
&RC
)> Ftor
, bool ClearVirtRegs
) {
1572 return new RegAllocFast(Ftor
, ClearVirtRegs
);