LLVM  5.0.0
RegAllocBase.cpp
Go to the documentation of this file.
1 //===-- RegAllocBase.cpp - Register Allocator Base Class ------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the RegAllocBase class which provides common functionality
11 // for LiveIntervalUnion-based register allocators.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "RegAllocBase.h"
16 #include "Spiller.h"
17 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Timer.h"
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "regalloc"
34 
35 STATISTIC(NumNewQueued , "Number of new live ranges queued");
36 
37 // Temporary verification option until we can put verification inside
38 // MachineVerifier.
41  cl::desc("Verify during register allocation"));
42 
43 const char RegAllocBase::TimerGroupName[] = "regalloc";
44 const char RegAllocBase::TimerGroupDescription[] = "Register Allocation";
45 bool RegAllocBase::VerifyEnabled = false;
46 
47 //===----------------------------------------------------------------------===//
48 // RegAllocBase Implementation
49 //===----------------------------------------------------------------------===//
50 
51 // Pin the vtable to this file.
52 void RegAllocBase::anchor() {}
53 
55  LiveIntervals &lis,
56  LiveRegMatrix &mat) {
57  TRI = &vrm.getTargetRegInfo();
58  MRI = &vrm.getRegInfo();
59  VRM = &vrm;
60  LIS = &lis;
61  Matrix = &mat;
64 }
65 
66 // Visit all the live registers. If they are already assigned to a physical
67 // register, unify them with the corresponding LiveIntervalUnion, otherwise push
68 // them on the priority queue for later assignment.
69 void RegAllocBase::seedLiveRegs() {
70  NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
72  for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
74  if (MRI->reg_nodbg_empty(Reg))
75  continue;
76  enqueue(&LIS->getInterval(Reg));
77  }
78 }
79 
80 // Top-level driver to manage the queue of unassigned VirtRegs and call the
81 // selectOrSplit implementation.
83  seedLiveRegs();
84 
85  // Continue assigning vregs one at a time to available physical registers.
86  while (LiveInterval *VirtReg = dequeue()) {
87  assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
88 
89  // Unused registers can appear when the spiller coalesces snippets.
90  if (MRI->reg_nodbg_empty(VirtReg->reg)) {
91  DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
92  aboutToRemoveInterval(*VirtReg);
93  LIS->removeInterval(VirtReg->reg);
94  continue;
95  }
96 
97  // Invalidate all interference queries, live ranges could have changed.
99 
100  // selectOrSplit requests the allocator to return an available physical
101  // register if possible and populate a list of new live intervals that
102  // result from splitting.
103  DEBUG(dbgs() << "\nselectOrSplit "
104  << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg))
105  << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
106  typedef SmallVector<unsigned, 4> VirtRegVec;
107  VirtRegVec SplitVRegs;
108  unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
109 
110  if (AvailablePhysReg == ~0u) {
111  // selectOrSplit failed to find a register!
112  // Probably caused by an inline asm.
113  MachineInstr *MI = nullptr;
115  I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
116  I != E; ) {
117  MachineInstr *TmpMI = &*(I++);
118  if (TmpMI->isInlineAsm()) {
119  MI = TmpMI;
120  break;
121  }
122  }
123  if (MI)
124  MI->emitError("inline assembly requires more registers than available");
125  else
126  report_fatal_error("ran out of registers during register allocation");
127  // Keep going after reporting the error.
128  VRM->assignVirt2Phys(VirtReg->reg,
129  RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
130  continue;
131  }
132 
133  if (AvailablePhysReg)
134  Matrix->assign(*VirtReg, AvailablePhysReg);
135 
136  for (unsigned Reg : SplitVRegs) {
137  assert(LIS->hasInterval(Reg));
138 
139  LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
140  assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
141  if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
142  assert(SplitVirtReg->empty() && "Non-empty but used interval");
143  DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
144  aboutToRemoveInterval(*SplitVirtReg);
145  LIS->removeInterval(SplitVirtReg->reg);
146  continue;
147  }
148  DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
149  assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
150  "expect split value in virtual register");
151  enqueue(SplitVirtReg);
152  ++NumNewQueued;
153  }
154  }
155 }
156 
159  for (auto DeadInst : DeadRemats) {
160  LIS->RemoveMachineInstrFromMaps(*DeadInst);
161  DeadInst->eraseFromParent();
162  }
163  DeadRemats.clear();
164 }
bool hasPhys(unsigned virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition: VirtRegMap.h:92
static const char TimerGroupDescription[]
Definition: RegAllocBase.h:108
STATISTIC(NumFunctions,"Total number of functions")
void RemoveMachineInstrFromMaps(MachineInstr &MI)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
MachineFunction & getMachineFunction() const
Definition: VirtRegMap.h:80
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:638
LiveRegMatrix * Matrix
Definition: RegAllocBase.h:65
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
virtual unsigned selectOrSplit(LiveInterval &VirtReg, SmallVectorImpl< unsigned > &splitLVRs)=0
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...
Definition: RegAllocBase.h:72
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
This class is basically a combination of TimeRegion and Timer.
Definition: Timer.h:160
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
void freezeReservedRegs(const MachineFunction &)
freezeReservedRegs - Called by the register allocator to freeze the set of reserved registers before ...
Reg
All possible values of the reg field in the ModR/M byte.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
static reg_instr_iterator reg_instr_end()
defusechain_iterator - This class provides iterator support for machine operands in the function that...
void assign(LiveInterval &VirtReg, unsigned PhysReg)
Assign VirtReg to PhysReg.
#define T
const char * getRegClassName(const TargetRegisterClass *Class) const
Returns the name of the register class.
void invalidateVirtRegs()
Invalidate cached interference queries after modifying virtual register live ranges.
Definition: LiveRegMatrix.h:82
VirtRegMap * VRM
Definition: RegAllocBase.h:63
void removeInterval(unsigned Reg)
Interval removal.
const TargetRegisterInfo & getTargetRegInfo() const
Definition: VirtRegMap.h:86
reg_instr_iterator reg_instr_begin(unsigned RegNo) const
void runOnMachineFunction(const MachineFunction &MF)
runOnFunction - Prepare to answer questions about MF.
void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat)
static const char TimerGroupName[]
Definition: RegAllocBase.h:107
LiveIntervals * LIS
Definition: RegAllocBase.h:64
void emitError(StringRef Msg) const
Emit an error referring to the source location of this instruction.
virtual void enqueue(LiveInterval *LI)=0
enqueue - Add VirtReg to the priority queue of unassigned registers.
#define E
Definition: LargeTest.cpp:27
void assignVirt2Phys(unsigned virtReg, MCPhysReg physReg)
creates a mapping for the specified virtual register to the specified physical register ...
Definition: VirtRegMap.cpp:75
bool isInlineAsm() const
Definition: MachineInstr.h:798
LiveInterval & getInterval(unsigned Reg)
static cl::opt< bool, true > VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled), cl::desc("Verify during register allocation"))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
virtual void aboutToRemoveInterval(LiveInterval &LI)
Method called when the allocator is about to remove a LiveInterval.
Definition: RegAllocBase.h:111
Representation of each machine instruction.
Definition: MachineInstr.h:59
static bool VerifyEnabled
VerifyEnabled - True when -verify-regalloc is given.
Definition: RegAllocBase.h:115
const TargetRegisterInfo * TRI
Definition: RegAllocBase.h:61
#define I(x, y, z)
Definition: MD5.cpp:58
virtual void postOptimization()
Definition: Spiller.h:32
virtual void postOptimization()
bool hasInterval(unsigned Reg) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineRegisterInfo & getRegInfo() const
Definition: VirtRegMap.h:85
#define DEBUG(X)
Definition: Debug.h:118
RegisterClassInfo RegClassInfo
Definition: RegAllocBase.h:66
IRTranslator LLVM IR MI
bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
Definition: IRReader.cpp:26
MachineRegisterInfo * MRI
Definition: RegAllocBase.h:62
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:417
bool reg_nodbg_empty(unsigned RegNo) const
reg_nodbg_empty - Return true if the only instructions using or defining Reg are Debug instructions...
virtual Spiller & spiller()=0
virtual LiveInterval * dequeue()=0
dequeue - Return the next unassigned register, or NULL.