LLVM 24.0.0git
RegAllocBase.cpp
Go to the documentation of this file.
1//===- RegAllocBase.cpp - Register Allocator Base Class -------------------===//
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// This file defines the RegAllocBase class which provides common functionality
10// for LiveIntervalUnion-based register allocators.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RegAllocBase.h"
16#include "llvm/ADT/Statistic.h"
27#include "llvm/IR/Module.h"
29#include "llvm/Pass.h"
31#include "llvm/Support/Debug.h"
33#include "llvm/Support/Timer.h"
35#include <cassert>
36
37using namespace llvm;
38
39#define DEBUG_TYPE "regalloc"
40
41STATISTIC(NumNewQueued, "Number of new live ranges queued");
42
43// Temporary verification option until we can put verification inside
44// MachineVerifier.
47 cl::Hidden, cl::desc("Verify during register allocation"));
48
49const char RegAllocBase::TimerGroupName[] = "regalloc";
50const char RegAllocBase::TimerGroupDescription[] = "Register Allocation";
52
53//===----------------------------------------------------------------------===//
54// RegAllocBase Implementation
55//===----------------------------------------------------------------------===//
56
57// Pin the vtable to this file.
58void RegAllocBase::anchor() {}
59
61 RegisterClassInfo &rci) {
62 TRI = &vrm.getTargetRegInfo();
63 MRI = &vrm.getRegInfo();
64 VRM = &vrm;
65 LIS = &lis;
66 Matrix = &mat;
67 RegClassInfo = &rci;
68 MRI->freezeReservedRegs();
69 // Keep the shared analysis in step with the set just frozen.
70 RegClassInfo->updateReservedRegs(MRI->getReservedRegs());
71 FailedVRegs.clear();
72}
73
74// Visit all the live registers. If they are already assigned to a physical
75// register, unify them with the corresponding LiveIntervalUnion, otherwise push
76// them on the priority queue for later assignment.
77void RegAllocBase::seedLiveRegs() {
78 NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
80 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
82 if (MRI->reg_nodbg_empty(Reg))
83 continue;
84 enqueue(&LIS->getInterval(Reg));
85 }
86}
87
88// Top-level driver to manage the queue of unassigned VirtRegs and call the
89// selectOrSplit implementation.
91 seedLiveRegs();
92
93 // Continue assigning vregs one at a time to available physical registers.
94 while (const LiveInterval *VirtReg = dequeue()) {
95 assert(!VRM->hasPhys(VirtReg->reg()) && "Register already assigned");
96
97 // Unused registers can appear when the spiller coalesces snippets.
98 if (MRI->reg_nodbg_empty(VirtReg->reg())) {
99 LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
100 aboutToRemoveInterval(*VirtReg);
101 LIS->removeInterval(VirtReg->reg());
102 continue;
103 }
104
105 // Invalidate all interference queries, live ranges could have changed.
106 Matrix->invalidateVirtRegs();
107
108 // selectOrSplit requests the allocator to return an available physical
109 // register if possible and populate a list of new live intervals that
110 // result from splitting.
111 LLVM_DEBUG(dbgs() << "\nselectOrSplit "
112 << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg()))
113 << ':' << *VirtReg << '\n');
114
115 using VirtRegVec = SmallVector<Register, 4>;
116
117 VirtRegVec SplitVRegs;
118 MCRegister AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
119
120 if (AvailablePhysReg == ~0u) {
121 // selectOrSplit failed to find a register!
122 // Probably caused by an inline asm.
123 MachineInstr *MI = nullptr;
124 for (MachineInstr &MIR : MRI->reg_instructions(VirtReg->reg())) {
125 MI = &MIR;
126 if (MI->isInlineAsm())
127 break;
128 }
129
130 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg->reg());
131 AvailablePhysReg = getErrorAssignment(*RC, MI);
132
133 // Keep going after reporting the error.
134 cleanupFailedVReg(VirtReg->reg(), AvailablePhysReg, SplitVRegs);
135 } else if (AvailablePhysReg)
136 Matrix->assign(*VirtReg, AvailablePhysReg);
137
138 for (Register Reg : SplitVRegs) {
139 assert(LIS->hasInterval(Reg));
140
141 LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
142 assert(!VRM->hasPhys(SplitVirtReg->reg()) && "Register already assigned");
143 if (MRI->reg_nodbg_empty(SplitVirtReg->reg())) {
144 assert(SplitVirtReg->empty() && "Non-empty but used interval");
145 LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
146 aboutToRemoveInterval(*SplitVirtReg);
147 LIS->removeInterval(SplitVirtReg->reg());
148 continue;
149 }
150 LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
151 assert(SplitVirtReg->reg().isVirtual() &&
152 "expect split value in virtual register");
153 enqueue(SplitVirtReg);
154 ++NumNewQueued;
155 }
156 }
157}
158
161
162 // Verify LiveRegMatrix after spilling (no dangling pointers).
163 assert(Matrix->isValid() && "LiveRegMatrix validation failed");
164
165 for (auto *DeadInst : DeadRemats) {
166 LIS->RemoveMachineInstrFromMaps(*DeadInst);
167 DeadInst->eraseFromParent();
168 }
169 DeadRemats.clear();
170}
171
173 SmallVectorImpl<Register> &SplitRegs) {
174 // We still should produce valid IR. Kill all the uses and reduce the live
175 // ranges so that we don't think it's possible to introduce kill flags later
176 // which will fail the verifier.
177 for (MachineOperand &MO : MRI->reg_operands(FailedReg)) {
178 if (MO.readsReg())
179 MO.setIsUndef(true);
180 if (MO.isDef())
181 cleanupFailedDefs(*MO.getParent(), FailedReg, PhysReg);
182 }
183
184 if (!MRI->isReserved(PhysReg)) {
185 // Physical liveness for any aliasing registers is now unreliable, so delete
186 // the uses.
187 for (MCRegAliasIterator Aliases(PhysReg, TRI, true); Aliases.isValid();
188 ++Aliases) {
189 for (MachineOperand &MO : MRI->reg_operands(*Aliases)) {
190 if (MO.readsReg())
191 MO.setIsUndef(true);
192 }
193 }
194 }
195
196 // Directly perform the rewrite, and do not leave it to VirtRegRewriter as
197 // usual. This avoids trying to manage illegal overlapping assignments in
198 // LiveRegMatrix.
199 MRI->replaceRegWith(FailedReg, PhysReg);
200 LIS->removeInterval(FailedReg);
201}
202
204 MCRegister PhysReg) {
205 auto IsErrorDef = [=](const MachineOperand &MO) {
206 return MO.getReg() == FailedReg || MO.getReg() == PhysReg;
207 };
208
209 SlotIndex Idx = LIS->getInstructionIndex(MI);
210 std::optional<bool> AssignedIsDead;
211 bool AnyLive = false;
212 for (const MachineOperand &MO : MI.all_defs()) {
213 Register Reg = MO.getReg();
214 if (IsErrorDef(MO)) {
215 AnyLive |= !MO.isDead();
216 if (MRI->isReserved(PhysReg))
217 continue;
218
219 // Add the def to the PhysReg regunit ranges so it interferes with later
220 // assignments. Ranges computed later will include the rewritten def.
221 MCRegister DefReg = PhysReg;
222 if (MO.getSubReg())
223 DefReg = TRI->getSubReg(PhysReg, MO.getSubReg());
224
225 SlotIndex DefIdx = Idx.getRegSlot(MO.isEarlyClobber());
226 for (MCRegUnit Unit : TRI->regunits(DefReg)) {
227 LiveRange *LR = LIS->getCachedRegUnit(Unit);
228 if (LR && !LR->liveAt(DefIdx))
229 LR->createDeadDef(DefIdx, LIS->getVNInfoAllocator());
230 }
231 } else if (Reg.isVirtual() && VRM->hasPhys(Reg)) {
232 MCRegister AssignedReg = VRM->getPhys(Reg);
233 if (MO.getSubReg())
234 AssignedReg = TRI->getSubReg(AssignedReg, MO.getSubReg());
235 if (AssignedReg == PhysReg)
236 AssignedIsDead = MO.isDead();
237 }
238 }
239
240 // Match a virtual register def assigned PhysReg, since its dead flag must
241 // agree with its live interval. Otherwise, keep PhysReg live if any def is.
242 bool IsDead = AssignedIsDead.value_or(!AnyLive);
243 for (MachineOperand &MO : MI.all_defs()) {
244 if (IsErrorDef(MO))
245 MO.setIsDead(IsDead);
246 }
247}
248
250 const Register Reg = LI->reg();
251
252 assert(Reg.isVirtual() && "Can only enqueue virtual registers");
253
254 if (VRM->hasPhys(Reg))
255 return;
256
257 if (shouldAllocateRegister(Reg)) {
258 LLVM_DEBUG(dbgs() << "Enqueuing " << printReg(Reg, TRI) << '\n');
259 enqueueImpl(LI);
260 } else {
261 LLVM_DEBUG(dbgs() << "Not enqueueing " << printReg(Reg, TRI)
262 << " in skipped register class\n");
263 }
264}
265
267 const MachineInstr *CtxMI) {
268 MachineFunction &MF = VRM->getMachineFunction();
269
270 // Avoid printing the error for every single instance of the register. It
271 // would be better if this were per register class.
272 bool EmitError = !MF.getProperties().hasFailedRegAlloc();
273 if (EmitError)
274 MF.getProperties().setFailedRegAlloc();
275
276 const Function &Fn = MF.getFunction();
277 LLVMContext &Context = Fn.getContext();
278
279 ArrayRef<MCPhysReg> AllocOrder = RegClassInfo->getOrder(&RC);
280 if (AllocOrder.empty()) {
281 // If the allocation order is empty, it likely means all registers in the
282 // class are reserved. We still to need to pick something, so look at the
283 // underlying class.
284 ArrayRef<MCPhysReg> RawRegs = RC.getRegisters();
285
286 if (EmitError) {
287 Context.diagnose(DiagnosticInfoRegAllocFailure(
288 "no registers from class available to allocate", Fn,
289 CtxMI ? CtxMI->getDebugLoc() : DiagnosticLocation()));
290 }
291
292 assert(!RawRegs.empty() && "register classes cannot have no registers");
293 return RawRegs.front();
294 }
295
296 if (EmitError) {
297 if (CtxMI && CtxMI->isInlineAsm()) {
298 CtxMI->emitInlineAsmError(
299 "inline assembly requires more registers than available");
300 } else {
301 Context.diagnose(DiagnosticInfoRegAllocFailure(
302 "ran out of registers during register allocation", Fn,
303 CtxMI ? CtxMI->getDebugLoc() : DiagnosticLocation()));
304 }
305 }
306
307 return AllocOrder.front();
308}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define T
This header defines classes/functions to handle pass execution timing information with interfaces for...
MachineRegisterInfo & MRI
static cl::opt< bool, true > VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled), cl::Hidden, cl::desc("Verify during register allocation"))
bool IsDead
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:352
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
LiveInterval - This class represents the liveness of a register, or stack slot.
Register reg() const
LiveInterval & getInterval(Register Reg)
This class represents the liveness of a register, stack slot, etc.
bool liveAt(SlotIndex index) const
LLVM_ABI VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc)
createDeadDef - Make sure the range has a value defined at Def.
bool empty() const
MCRegAliasIterator enumerates all registers aliasing Reg.
ArrayRef< MCPhysReg > getRegisters() const
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineFunctionProperties & getProperties() const
Get the function properties.
Representation of each machine instruction.
bool isInlineAsm() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void emitInlineAsmError(const Twine &ErrMsg) const
Emit an error referring to the source location of this instruction.
MachineOperand class - Representation of each machine instruction operand.
virtual void aboutToRemoveInterval(const LiveInterval &LI)
Method called when the allocator is about to remove a LiveInterval.
virtual MCRegister selectOrSplit(const LiveInterval &VirtReg, SmallVectorImpl< Register > &splitLVRs)=0
MCPhysReg getErrorAssignment(const TargetRegisterClass &RC, const MachineInstr *CtxMI=nullptr)
Query a physical register to use as a filler in contexts where the allocation has failed.
void enqueue(const LiveInterval *LI)
enqueue - Add VirtReg to the priority queue of unassigned registers.
SmallPtrSet< MachineInstr *, 32 > DeadRemats
Inst which is a def of an original reg and whose defs are already all dead after remat is saved in De...
void cleanupFailedVReg(Register FailedVReg, MCRegister PhysReg, SmallVectorImpl< Register > &SplitRegs)
Perform cleanups on registers that failed to allocate.
SmallSet< Register, 2 > FailedVRegs
void cleanupFailedDefs(MachineInstr &MI, Register FailedReg, MCRegister PhysReg)
Add the defs of FailedReg on MI to the PhysReg regunit live ranges, and make the defs of PhysReg on M...
virtual Spiller & spiller()=0
const TargetRegisterInfo * TRI
LiveIntervals * LIS
static const char TimerGroupName[]
static const char TimerGroupDescription[]
void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat, RegisterClassInfo &rci)
LiveRegMatrix * Matrix
virtual const LiveInterval * dequeue()=0
dequeue - Return the next unassigned register, or NULL.
virtual void postOptimization()
VirtRegMap * VRM
MachineRegisterInfo * MRI
virtual void enqueueImpl(const LiveInterval *LI)=0
enqueue - Add VirtReg to the priority queue of unassigned registers.
bool shouldAllocateRegister(Register Reg)
Get whether a given register should be allocated.
RegisterClassInfo * RegClassInfo
static bool VerifyEnabled
VerifyEnabled - True when -verify-regalloc is given.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:70
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
virtual void postOptimization()
Definition Spiller.h:49
MachineRegisterInfo & getRegInfo() const
Definition VirtRegMap.h:80
const TargetRegisterInfo & getTargetRegInfo() const
Definition VirtRegMap.h:81
LocationClass< Ty > location(Ty &L)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
This class is basically a combination of TimeRegion and Timer.
Definition Timer.h:175