Keep track of the start of MBB's in a separate map from instructions. This

is faster and is needed for future improvements.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30383 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2006-09-15 03:57:23 +00:00
parent c45a2c72cc
commit 428b92eb83
2 changed files with 62 additions and 40 deletions

View File

@ -38,6 +38,10 @@ namespace llvm {
const TargetInstrInfo* tii_; const TargetInstrInfo* tii_;
LiveVariables* lv_; LiveVariables* lv_;
/// MBB2IdxMap - The index of the first instruction in the specified basic
/// block.
std::vector<unsigned> MBB2IdxMap;
typedef std::map<MachineInstr*, unsigned> Mi2IndexMap; typedef std::map<MachineInstr*, unsigned> Mi2IndexMap;
Mi2IndexMap mi2iMap_; Mi2IndexMap mi2iMap_;
@ -113,6 +117,17 @@ namespace llvm {
return I->second; return I->second;
} }
/// getMBBStartIdx - Return the base index of the first instruction in the
/// specified MachineBasicBlock.
unsigned getMBBStartIdx(MachineBasicBlock *MBB) const {
return getMBBStartIdx(MBB->getNumber());
}
unsigned getMBBStartIdx(unsigned MBBNo) const {
assert(MBBNo < MBB2IdxMap.size() && "Invalid MBB number!");
return MBB2IdxMap[MBBNo];
}
/// getInstructionIndex - returns the base index of instr /// getInstructionIndex - returns the base index of instr
unsigned getInstructionIndex(MachineInstr* instr) const { unsigned getInstructionIndex(MachineInstr* instr) const {
Mi2IndexMap::const_iterator it = mi2iMap_.find(instr); Mi2IndexMap::const_iterator it = mi2iMap_.find(instr);
@ -155,7 +170,7 @@ namespace llvm {
} }
} }
/// computeIntervals - compute live intervals /// computeIntervals - Compute live intervals.
void computeIntervals(); void computeIntervals();
/// joinIntervals - join compatible live intervals /// joinIntervals - join compatible live intervals

View File

@ -57,7 +57,7 @@ namespace {
static cl::opt<bool> static cl::opt<bool>
EnableJoining("join-liveintervals", EnableJoining("join-liveintervals",
cl::desc("Join compatible live intervals"), cl::desc("Coallesce copies (default=true)"),
cl::init(true)); cl::init(true));
} }
@ -120,16 +120,23 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
"copyRetToReg didn't insert anything!"); "copyRetToReg didn't insert anything!");
} }
// number MachineInstrs // Number MachineInstrs and MachineBasicBlocks.
unsigned miIndex = 0; // Initialize MBB indexes to a sentinal.
for (MachineFunction::iterator mbb = mf_->begin(), mbbEnd = mf_->end(); MBB2IdxMap.resize(mf_->getNumBlockIDs(), ~0U);
mbb != mbbEnd; ++mbb)
for (MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end(); unsigned MIIndex = 0;
mi != miEnd; ++mi) { for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
bool inserted = mi2iMap_.insert(std::make_pair(mi, miIndex)).second; MBB != E; ++MBB) {
// Set the MBB2IdxMap entry for this MBB.
MBB2IdxMap[MBB->getNumber()] = MIIndex;
for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
I != E; ++I) {
bool inserted = mi2iMap_.insert(std::make_pair(I, MIIndex)).second;
assert(inserted && "multiple MachineInstr -> index mappings"); assert(inserted && "multiple MachineInstr -> index mappings");
i2miMap_.push_back(mi); i2miMap_.push_back(I);
miIndex += InstrSlots::NUM; MIIndex += InstrSlots::NUM;
}
} }
// Note intervals due to live-in values. // Note intervals due to live-in values.
@ -155,11 +162,12 @@ bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
std::cerr << "\n"; std::cerr << "\n";
}); });
// join intervals if requested // Join (coallesce) intervals if requested.
if (EnableJoining) joinIntervals(); if (EnableJoining) joinIntervals();
numIntervalsAfter += getNumIntervals(); numIntervalsAfter += getNumIntervals();
// perform a final pass over the instructions and compute spill // perform a final pass over the instructions and compute spill
// weights, coalesce virtual registers and remove identity moves. // weights, coalesce virtual registers and remove identity moves.
const LoopInfo &loopInfo = getAnalysis<LoopInfo>(); const LoopInfo &loopInfo = getAnalysis<LoopInfo>();
@ -420,10 +428,10 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
// live interval. // live interval.
for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) { for (unsigned i = 0, e = vi.AliveBlocks.size(); i != e; ++i) {
if (vi.AliveBlocks[i]) { if (vi.AliveBlocks[i]) {
MachineBasicBlock* mbb = mf_->getBlockNumbered(i); MachineBasicBlock *MBB = mf_->getBlockNumbered(i);
if (!mbb->empty()) { if (!MBB->empty()) {
LiveRange LR(getInstructionIndex(&mbb->front()), LiveRange LR(getMBBStartIdx(i),
getInstructionIndex(&mbb->back()) + InstrSlots::NUM, getInstructionIndex(&MBB->back()) + InstrSlots::NUM,
ValNum); ValNum);
interval.addRange(LR); interval.addRange(LR);
DEBUG(std::cerr << " +" << LR); DEBUG(std::cerr << " +" << LR);
@ -435,7 +443,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
// block to the 'use' slot of the killing instruction. // block to the 'use' slot of the killing instruction.
for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) { for (unsigned i = 0, e = vi.Kills.size(); i != e; ++i) {
MachineInstr *Kill = vi.Kills[i]; MachineInstr *Kill = vi.Kills[i];
LiveRange LR(getInstructionIndex(Kill->getParent()->begin()), LiveRange LR(getMBBStartIdx(Kill->getParent()),
getUseIndex(getInstructionIndex(Kill))+1, getUseIndex(getInstructionIndex(Kill))+1,
ValNum); ValNum);
interval.addRange(LR); interval.addRange(LR);
@ -498,7 +506,7 @@ void LiveIntervals::handleVirtualRegisterDef(MachineBasicBlock *mbb,
// Remove the old range that we now know has an incorrect number. // Remove the old range that we now know has an incorrect number.
MachineInstr *Killer = vi.Kills[0]; MachineInstr *Killer = vi.Kills[0];
unsigned Start = getInstructionIndex(Killer->getParent()->begin()); unsigned Start = getMBBStartIdx(Killer->getParent());
unsigned End = getUseIndex(getInstructionIndex(Killer))+1; unsigned End = getUseIndex(getInstructionIndex(Killer))+1;
DEBUG(std::cerr << "Removing [" << Start << "," << End << "] from: "; DEBUG(std::cerr << "Removing [" << Start << "," << End << "] from: ";
interval.print(std::cerr, mri_); std::cerr << "\n"); interval.print(std::cerr, mri_); std::cerr << "\n");
@ -613,35 +621,34 @@ void LiveIntervals::computeIntervals() {
// Track the index of the current machine instr. // Track the index of the current machine instr.
unsigned MIIndex = 0; unsigned MIIndex = 0;
for (MachineFunction::iterator I = mf_->begin(), E = mf_->end(); for (MachineFunction::iterator MBBI = mf_->begin(), E = mf_->end();
I != E; ++I) { MBBI != E; ++MBBI) {
MachineBasicBlock* mbb = I; MachineBasicBlock *MBB = MBBI;
DEBUG(std::cerr << ((Value*)mbb->getBasicBlock())->getName() << ":\n"); DEBUG(std::cerr << ((Value*)MBB->getBasicBlock())->getName() << ":\n");
MachineBasicBlock::iterator mi = mbb->begin(), miEnd = mbb->end(); MachineBasicBlock::iterator MI = MBB->begin(), miEnd = MBB->end();
if (IgnoreFirstInstr) { if (IgnoreFirstInstr) {
++mi; ++MI;
IgnoreFirstInstr = false; IgnoreFirstInstr = false;
MIIndex += InstrSlots::NUM; MIIndex += InstrSlots::NUM;
} }
for (; mi != miEnd; ++mi) { for (; MI != miEnd; ++MI) {
const TargetInstrDescriptor& tid = const TargetInstrDescriptor &TID = tii_->get(MI->getOpcode());
tm_->getInstrInfo()->get(mi->getOpcode()); DEBUG(std::cerr << MIIndex << "\t" << *MI);
DEBUG(std::cerr << MIIndex << "\t" << *mi);
// handle implicit defs // Handle implicit defs.
if (tid.ImplicitDefs) { if (TID.ImplicitDefs) {
for (const unsigned* id = tid.ImplicitDefs; *id; ++id) for (const unsigned *ImpDef = TID.ImplicitDefs; *ImpDef; ++ImpDef)
handleRegisterDef(mbb, mi, MIIndex, *id); handleRegisterDef(MBB, MI, MIIndex, *ImpDef);
} }
// handle explicit defs // Handle explicit defs.
for (int i = mi->getNumOperands() - 1; i >= 0; --i) { for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
MachineOperand& mop = mi->getOperand(i); MachineOperand &MO = MI->getOperand(i);
// handle register defs - build intervals // handle register defs - build intervals
if (mop.isRegister() && mop.getReg() && mop.isDef()) if (MO.isRegister() && MO.getReg() && MO.isDef())
handleRegisterDef(mbb, mi, MIIndex, mop.getReg()); handleRegisterDef(MBB, MI, MIIndex, MO.getReg());
} }
MIIndex += InstrSlots::NUM; MIIndex += InstrSlots::NUM;