19 #define DEBUG_TYPE "regalloc"
42 STATISTIC(NumSpillSlots,
"Number of spill slots allocated");
43 STATISTIC(NumIdCopies,
"Number of identity moves eliminated after rewriting");
54 MRI = &mf.getRegInfo();
55 TII = mf.getTarget().getInstrInfo();
60 Virt2StackSlotMap.clear();
61 Virt2SplitMap.clear();
69 Virt2PhysMap.resize(NumRegs);
70 Virt2StackSlotMap.resize(NumRegs);
71 Virt2SplitMap.resize(NumRegs);
82 unsigned Hint =
MRI->getSimpleHint(VirtReg);
87 return getPhys(VirtReg) == Hint;
91 std::pair<unsigned, unsigned> Hint =
MRI->getRegAllocationHint(VirtReg);
102 "attempt to assign stack slot to already spilled register");
104 return Virt2StackSlotMap[virtReg] = createSpillSlot(RC);
110 "attempt to assign stack slot to already spilled register");
113 "illegal fixed frame index");
114 Virt2StackSlotMap[virtReg] = SS;
118 OS <<
"********** REGISTER MAP **********\n";
119 for (
unsigned i = 0, e =
MRI->getNumVirtRegs(); i != e; ++i) {
122 OS <<
'[' <<
PrintReg(Reg, TRI) <<
" -> "
123 <<
PrintReg(Virt2PhysMap[Reg], TRI) <<
"] "
128 for (
unsigned i = 0, e =
MRI->getNumVirtRegs(); i != e; ++i) {
131 OS <<
'[' <<
PrintReg(Reg, TRI) <<
" -> fi#" << Virt2StackSlotMap[
Reg]
138 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
165 void addMBBLiveIns();
179 "Virtual Register Rewriter",
false,
false)
188 char VirtRegRewriter::
ID = 0;
190 void VirtRegRewriter::getAnalysisUsage(
AnalysisUsage &AU)
const {
191 AU.setPreservesCFG();
204 TM = &MF->getTarget();
205 TRI =
TM->getRegisterInfo();
206 TII =
TM->getInstrInfo();
207 MRI = &MF->getRegInfo();
208 Indexes = &getAnalysis<SlotIndexes>();
209 LIS = &getAnalysis<LiveIntervals>();
210 VRM = &getAnalysis<VirtRegMap>();
211 DEBUG(
dbgs() <<
"********** REWRITE VIRTUAL REGISTERS **********\n"
212 <<
"********** Function: "
213 << MF->getName() <<
'\n');
217 LIS->addKillFlags(VRM);
226 getAnalysis<LiveDebugVariables>().emitDebugValues(VRM);
231 MRI->clearVirtRegs();
237 void VirtRegRewriter::addMBBLiveIns() {
239 for (
unsigned Idx = 0, IdxE =
MRI->getNumVirtRegs(); Idx != IdxE; ++Idx) {
241 if (
MRI->reg_nodbg_empty(VirtReg))
244 if (LI.
empty() || LIS->intervalIsInOneMBB(LI))
248 unsigned PhysReg = VRM->getPhys(VirtReg);
254 if (!Indexes->findLiveInMBBs(
I->start,
I->end, LiveIn))
256 for (
unsigned i = 0, e = LiveIn.
size(); i != e; ++i)
257 if (!LiveIn[i]->isLiveIn(PhysReg))
264 void VirtRegRewriter::rewrite() {
271 MBBI != MBBE; ++MBBI) {
273 bool IsExitBB = MBBI->succ_empty();
275 MII = MBBI->instr_begin(), MIE = MBBI->instr_end();
MII != MIE;) {
281 if (IsExitBB && MI->
isCall())
308 unsigned VirtReg = MO.
getReg();
309 unsigned PhysReg = VRM->getPhys(VirtReg);
311 "Instruction uses unmapped VirtReg");
312 assert(!
MRI->isReserved(PhysReg) &&
"Reserved register assignment");
337 PhysReg = TRI->getSubReg(PhysReg, MO.
getSubReg());
338 assert(PhysReg &&
"Invalid SubReg for physical register");
348 while (!SuperKills.
empty())
351 while (!SuperDeads.
empty())
354 while (!SuperDefs.
empty())
363 DEBUG(
dbgs() <<
"Deleting identity copy.\n");
365 Indexes->removeMachineInstrFromMaps(MI);
371 DEBUG(
dbgs() <<
"Identity copy: " << *MI);
378 if (NoReturnInsts.
empty()) {
379 for (
unsigned Reg = 1, RegE = TRI->getNumRegs();
Reg != RegE; ++
Reg)
380 if (!
MRI->reg_nodbg_empty(
Reg))
383 for (
unsigned Reg = 1, RegE = TRI->getNumRegs();
Reg != RegE; ++
Reg) {
384 if (
MRI->reg_nodbg_empty(
Reg))
390 MRI->reg_nodbg_begin(
Reg),
391 EndIt =
MRI->reg_nodbg_end(); It != EndIt; ++It) {
392 if (!NoReturnInsts.
count(&(*It))) {
bool hasPhys(unsigned virtReg) const
returns true if the specified virtual register is mapped to a physical register
void push_back(const T &Elt)
mop_iterator operands_end()
const GlobalValue * getGlobal() const
static unsigned index2VirtReg(unsigned Index)
The main container class for the LLVM Intermediate Representation.
bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void setIsUndef(bool Val=true)
static bool isVirtualRegister(unsigned Reg)
Instructions::iterator instr_iterator
LoopInfoBase< BlockT, LoopT > * LI
unsigned getNumVirtRegs() const
#define INITIALIZE_PASS_DEPENDENCY(depName)
const HexagonInstrInfo * TII
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
static unsigned addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const TargetRegisterClass * getRegClass(unsigned Reg) const
void print(raw_ostream &OS, const Module *M=0) const
ID
LLVM Calling Convention Representation.
unsigned getNumOperands() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
int getObjectIndexBegin() const
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
unsigned getAlignment() const
const MCRegisterClass & getRegClass(unsigned i) const
Returns the register class associated with the enumeration value. See class MCOperandInfo.
bool hasPreferredPhys(unsigned VirtReg)
returns true if VirtReg is assigned to its preferred physreg.
Function doesn't unwind stack.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Mark the function as not returning.
bool hasKnownPreference(unsigned VirtReg)
returns true if VirtReg has a known preferred register. This returns false if VirtReg has a preferenc...
unsigned getSubReg() const
int CreateSpillStackObject(uint64_t Size, unsigned Alignment)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const char * getName() const
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
Segments::const_iterator const_iterator
void setDesc(const MCInstrDesc &tid)
const uint32_t * getRegMask() const
Promote Memory to Register
bool isIdentityCopy() const
isIdentityCopy - Return true is the instruction is an identity copy.
MachineFrameInfo * getFrameInfo()
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Virtual Register Rewriter
static bool isPhysicalRegister(unsigned Reg)
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
MachineRegisterInfo & getRegInfo()
virtual void getAnalysisUsage(AnalysisUsage &AU) const
void setReg(unsigned Reg)
bool isCall(QueryType Type=AnyInBundle) const
void setSubReg(unsigned subReg)
STATISTIC(NumSpillSlots,"Number of spill slots allocated")
INITIALIZE_PASS_BEGIN(VirtRegRewriter,"virtregrewriter","Virtual Register Rewriter", false, false) INITIALIZE_PASS_END(VirtRegRewriter
unsigned getReg() const
getReg - Returns the register number.
virtual const HexagonRegisterInfo & getRegisterInfo() const
mop_iterator operands_begin()
BasicBlockListType::iterator iterator
void addRegisterDefined(unsigned Reg, const TargetRegisterInfo *RegInfo=0)
int assignVirt2StackSlot(unsigned virtReg)
create a mapping for the specifed virtual register to the next available stack slot ...
bool addRegisterKilled(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
unsigned getPhys(unsigned virtReg) const
returns the physical register mapped to the specified virtual register
const MCRegisterInfo & MRI