#define DEBUG_TYPE "slotindexes"
#include "llvm/CodeGen/SlotIndexes.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Target/TargetInstrInfo.h"
using namespace llvm;
namespace {
class EmptyIndexListEntry : public IndexListEntry {
public:
EmptyIndexListEntry() : IndexListEntry(EMPTY_KEY) {}
};
class TombstoneIndexListEntry : public IndexListEntry {
public:
TombstoneIndexListEntry() : IndexListEntry(TOMBSTONE_KEY) {}
};
ManagedStatic<EmptyIndexListEntry> IndexListEntryEmptyKey;
ManagedStatic<TombstoneIndexListEntry> IndexListEntryTombstoneKey;
}
char SlotIndexes::ID = 0;
static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering");
IndexListEntry* IndexListEntry::getEmptyKeyEntry() {
return &*IndexListEntryEmptyKey;
}
IndexListEntry* IndexListEntry::getTombstoneKeyEntry() {
return &*IndexListEntryTombstoneKey;
}
void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
au.setPreservesAll();
MachineFunctionPass::getAnalysisUsage(au);
}
void SlotIndexes::releaseMemory() {
mi2iMap.clear();
mbb2IdxMap.clear();
idx2MBBMap.clear();
terminatorGaps.clear();
clearList();
}
bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
mf = &fn;
initList();
assert(indexListHead->getNext() == 0 &&
"Index list non-empty at initial numbering?");
assert(idx2MBBMap.empty() &&
"Index -> MBB mapping non-empty at initial numbering?");
assert(mbb2IdxMap.empty() &&
"MBB -> Index mapping non-empty at initial numbering?");
assert(mi2iMap.empty() &&
"MachineInstr -> Index mapping non-empty at initial numbering?");
functionSize = 0;
unsigned index = 0;
push_back(createEntry(0, index));
for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
mbbItr != mbbEnd; ++mbbItr) {
MachineBasicBlock *mbb = &*mbbItr;
SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
index += SlotIndex::NUM;
for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
miItr != miEnd; ++miItr) {
MachineInstr *mi = miItr;
if (mi->isDebugValue())
continue;
if (miItr == mbb->getFirstTerminator()) {
push_back(createEntry(0, index));
terminatorGaps.insert(
std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
index += SlotIndex::NUM;
}
push_back(createEntry(mi, index));
mi2iMap.insert(
std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
++functionSize;
unsigned Slots = mi->getDesc().getNumDefs();
if (Slots == 0)
Slots = 1;
index += (Slots + 1) * SlotIndex::NUM;
}
if (mbb->getFirstTerminator() == mbb->end()) {
push_back(createEntry(0, index));
terminatorGaps.insert(
std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
index += SlotIndex::NUM;
}
push_back(createEntry(0, index));
SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
mbb2IdxMap.insert(
std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
}
std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
DEBUG(dump());
return false;
}
void SlotIndexes::renumberIndexes() {
functionSize = 0;
unsigned index = 0;
for (IndexListEntry *curEntry = front(); curEntry != getTail();
curEntry = curEntry->getNext()) {
curEntry->setIndex(index);
if (curEntry->getInstr() == 0) {
index += SlotIndex::NUM;
}
else {
++functionSize;
unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
if (Slots == 0)
Slots = 1;
index += (Slots + 1) * SlotIndex::NUM;
}
}
}
void SlotIndexes::dump() const {
for (const IndexListEntry *itr = front(); itr != getTail();
itr = itr->getNext()) {
dbgs() << itr->getIndex() << " ";
if (itr->getInstr() != 0) {
dbgs() << *itr->getInstr();
} else {
dbgs() << "\n";
}
}
for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
itr != mbb2IdxMap.end(); ++itr) {
dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
<< itr->second.first << ", " << itr->second.second << "]\n";
}
}
void SlotIndex::print(raw_ostream &os) const {
os << getIndex();
if (isPHI())
os << "*";
}
void SlotIndex::dump() const {
print(dbgs());
dbgs() << "\n";
}