Use basic block numbers as indexes when mapping slot index ranges.

This is more compact and faster than using DenseMap.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@128763 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jakob Stoklund Olesen 2011-04-02 06:03:31 +00:00
parent 4071a71112
commit a122eaaee2
2 changed files with 28 additions and 41 deletions

View File

@ -337,15 +337,12 @@ namespace llvm {
typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap; typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
Mi2IndexMap mi2iMap; Mi2IndexMap mi2iMap;
/// MBB2IdxMap - The indexes of the first and last instructions in the /// MBBRanges - Map MBB number to (start, stop) indexes.
/// specified basic block. SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
typedef DenseMap<const MachineBasicBlock*,
std::pair<SlotIndex, SlotIndex> > MBB2IdxMap;
MBB2IdxMap mbb2IdxMap;
/// Idx2MBBMap - Sorted list of pairs of index of first instruction /// Idx2MBBMap - Sorted list of pairs of index of first instruction
/// and MBB id. /// and MBB id.
std::vector<IdxMBBPair> idx2MBBMap; SmallVector<IdxMBBPair, 8> idx2MBBMap;
// IndexListEntry allocator. // IndexListEntry allocator.
BumpPtrAllocator ileAllocator; BumpPtrAllocator ileAllocator;
@ -509,12 +506,16 @@ namespace llvm {
return nextNonNull; return nextNonNull;
} }
/// Return the (start,end) range of the given basic block number.
const std::pair<SlotIndex, SlotIndex> &
getMBBRange(unsigned Num) const {
return MBBRanges[Num];
}
/// Return the (start,end) range of the given basic block. /// Return the (start,end) range of the given basic block.
const std::pair<SlotIndex, SlotIndex> & const std::pair<SlotIndex, SlotIndex> &
getMBBRange(const MachineBasicBlock *mbb) const { getMBBRange(const MachineBasicBlock *MBB) const {
MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb); return getMBBRange(MBB->getNumber());
assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
return itr->second;
} }
/// Returns the first index in the given basic block. /// Returns the first index in the given basic block.
@ -529,10 +530,10 @@ namespace llvm {
/// Returns the basic block which the given index falls in. /// Returns the basic block which the given index falls in.
MachineBasicBlock* getMBBFromIndex(SlotIndex index) const { MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
std::vector<IdxMBBPair>::const_iterator I = SmallVectorImpl<IdxMBBPair>::const_iterator I =
std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index); std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
// Take the pair containing the index // Take the pair containing the index
std::vector<IdxMBBPair>::const_iterator J = SmallVectorImpl<IdxMBBPair>::const_iterator J =
((I != idx2MBBMap.end() && I->first > index) || ((I != idx2MBBMap.end() && I->first > index) ||
(I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I; (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
@ -544,7 +545,7 @@ namespace llvm {
bool findLiveInMBBs(SlotIndex start, SlotIndex end, bool findLiveInMBBs(SlotIndex start, SlotIndex end,
SmallVectorImpl<MachineBasicBlock*> &mbbs) const { SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
std::vector<IdxMBBPair>::const_iterator itr = SmallVectorImpl<IdxMBBPair>::const_iterator itr =
std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start); std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
bool resVal = false; bool resVal = false;
@ -564,7 +565,7 @@ namespace llvm {
assert(start < end && "Backwards ranges not allowed."); assert(start < end && "Backwards ranges not allowed.");
std::vector<IdxMBBPair>::const_iterator itr = SmallVectorImpl<IdxMBBPair>::const_iterator itr =
std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start); std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
if (itr == idx2MBBMap.end()) { if (itr == idx2MBBMap.end()) {
@ -596,11 +597,6 @@ namespace llvm {
assert(mbb != 0 && "Instr must be added to function."); assert(mbb != 0 && "Instr must be added to function.");
MBB2IdxMap::iterator mbbRangeItr = mbb2IdxMap.find(mbb);
assert(mbbRangeItr != mbb2IdxMap.end() &&
"Instruction's parent MBB has not been added to SlotIndexes.");
MachineBasicBlock::iterator miItr(mi); MachineBasicBlock::iterator miItr(mi);
IndexListEntry *newEntry; IndexListEntry *newEntry;
// Get previous index, considering that not all instructions are indexed. // Get previous index, considering that not all instructions are indexed.
@ -608,7 +604,7 @@ namespace llvm {
for (;;) { for (;;) {
// If mi is at the mbb beginning, get the prev index from the mbb. // If mi is at the mbb beginning, get the prev index from the mbb.
if (miItr == mbb->begin()) { if (miItr == mbb->begin()) {
prevEntry = &mbbRangeItr->second.first.entry(); prevEntry = &getMBBStartIdx(mbb).entry();
break; break;
} }
// Otherwise rewind until we find a mapped instruction. // Otherwise rewind until we find a mapped instruction.
@ -689,21 +685,14 @@ namespace llvm {
SlotIndex startIdx(startEntry, SlotIndex::LOAD); SlotIndex startIdx(startEntry, SlotIndex::LOAD);
SlotIndex endIdx(nextEntry, SlotIndex::LOAD); SlotIndex endIdx(nextEntry, SlotIndex::LOAD);
mbb2IdxMap.insert( assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
std::make_pair(mbb, std::make_pair(startIdx, endIdx))); "Blocks must be added in order");
MBBRanges.push_back(std::make_pair(startIdx, endIdx));
idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb)); idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
if (MachineFunction::iterator(mbb) != mbb->getParent()->begin()) {
// Have to update the end index of the previous block.
MachineBasicBlock *priorMBB =
llvm::prior(MachineFunction::iterator(mbb));
mbb2IdxMap[priorMBB].second = startIdx;
}
renumberIndexes(); renumberIndexes();
std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
} }
}; };

View File

@ -32,7 +32,7 @@ void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
void SlotIndexes::releaseMemory() { void SlotIndexes::releaseMemory() {
mi2iMap.clear(); mi2iMap.clear();
mbb2IdxMap.clear(); MBBRanges.clear();
idx2MBBMap.clear(); idx2MBBMap.clear();
clearList(); clearList();
} }
@ -58,13 +58,15 @@ bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
"Index list non-empty at initial numbering?"); "Index list non-empty at initial numbering?");
assert(idx2MBBMap.empty() && assert(idx2MBBMap.empty() &&
"Index -> MBB mapping non-empty at initial numbering?"); "Index -> MBB mapping non-empty at initial numbering?");
assert(mbb2IdxMap.empty() && assert(MBBRanges.empty() &&
"MBB -> Index mapping non-empty at initial numbering?"); "MBB -> Index mapping non-empty at initial numbering?");
assert(mi2iMap.empty() && assert(mi2iMap.empty() &&
"MachineInstr -> Index mapping non-empty at initial numbering?"); "MachineInstr -> Index mapping non-empty at initial numbering?");
functionSize = 0; functionSize = 0;
unsigned index = 0; unsigned index = 0;
MBBRanges.resize(mf->getNumBlockIDs());
idx2MBBMap.reserve(mf->size());
push_back(createEntry(0, index)); push_back(createEntry(0, index));
@ -94,10 +96,8 @@ bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
// We insert one blank instructions between basic blocks. // We insert one blank instructions between basic blocks.
push_back(createEntry(0, index += SlotIndex::InstrDist)); push_back(createEntry(0, index += SlotIndex::InstrDist));
SlotIndex blockEndIndex(back(), SlotIndex::LOAD); MBBRanges[mbb->getNumber()].first = blockStartIndex;
mbb2IdxMap.insert( MBBRanges[mbb->getNumber()].second = SlotIndex(back(), SlotIndex::LOAD);
std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
} }
@ -158,11 +158,9 @@ void SlotIndexes::dump() const {
} }
} }
for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin(); for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
itr != mbb2IdxMap.end(); ++itr) { dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - [" << MBBRanges[i].second << ")\n";
<< itr->second.first << ", " << itr->second.second << "]\n";
}
} }
// Print a SlotIndex to a raw_ostream. // Print a SlotIndex to a raw_ostream.