2009-11-03 23:52:08 +00:00
|
|
|
//===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "slotindexes"
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
2011-03-04 18:08:29 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-11-03 23:52:08 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-01-22 22:38:21 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
char SlotIndexes::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(SlotIndexes, "slotindexes",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Slot index numbering", false, false)
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2011-03-04 19:43:38 +00:00
|
|
|
STATISTIC(NumLocalRenum, "Number of local renumberings");
|
|
|
|
STATISTIC(NumGlobalRenum, "Number of global renumberings");
|
2011-03-04 18:08:29 +00:00
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
|
|
|
|
au.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(au);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlotIndexes::releaseMemory() {
|
|
|
|
mi2iMap.clear();
|
2011-04-02 06:03:31 +00:00
|
|
|
MBBRanges.clear();
|
2009-11-03 23:52:08 +00:00
|
|
|
idx2MBBMap.clear();
|
|
|
|
clearList();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
|
|
|
|
|
|
|
|
// Compute numbering as follows:
|
|
|
|
// Grab an iterator to the start of the index list.
|
|
|
|
// Iterate over all MBBs, and within each MBB all MIs, keeping the MI
|
|
|
|
// iterator in lock-step (though skipping it over indexes which have
|
|
|
|
// null pointers in the instruction field).
|
|
|
|
// At each iteration assert that the instruction pointed to in the index
|
|
|
|
// is the same one pointed to by the MI iterator. This
|
|
|
|
|
|
|
|
// FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
|
|
|
|
// only need to be set up once after the first numbering is computed.
|
|
|
|
|
|
|
|
mf = &fn;
|
|
|
|
initList();
|
|
|
|
|
|
|
|
// Check that the list contains only the sentinal.
|
|
|
|
assert(indexListHead->getNext() == 0 &&
|
|
|
|
"Index list non-empty at initial numbering?");
|
|
|
|
assert(idx2MBBMap.empty() &&
|
|
|
|
"Index -> MBB mapping non-empty at initial numbering?");
|
2011-04-02 06:03:31 +00:00
|
|
|
assert(MBBRanges.empty() &&
|
2009-11-03 23:52:08 +00:00
|
|
|
"MBB -> Index mapping non-empty at initial numbering?");
|
|
|
|
assert(mi2iMap.empty() &&
|
|
|
|
"MachineInstr -> Index mapping non-empty at initial numbering?");
|
|
|
|
|
|
|
|
functionSize = 0;
|
|
|
|
unsigned index = 0;
|
2011-04-02 06:03:31 +00:00
|
|
|
MBBRanges.resize(mf->getNumBlockIDs());
|
|
|
|
idx2MBBMap.reserve(mf->size());
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2009-12-22 00:11:50 +00:00
|
|
|
push_back(createEntry(0, index));
|
|
|
|
|
2010-02-10 16:03:48 +00:00
|
|
|
// Iterate over the function.
|
2009-11-03 23:52:08 +00:00
|
|
|
for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
|
|
|
|
mbbItr != mbbEnd; ++mbbItr) {
|
|
|
|
MachineBasicBlock *mbb = &*mbbItr;
|
|
|
|
|
|
|
|
// Insert an index for the MBB start.
|
|
|
|
SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
|
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
|
|
|
|
miItr != miEnd; ++miItr) {
|
2010-02-09 19:54:29 +00:00
|
|
|
MachineInstr *mi = miItr;
|
|
|
|
if (mi->isDebugValue())
|
2010-01-22 22:38:21 +00:00
|
|
|
continue;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
// Insert a store index for the instr.
|
2011-03-04 18:51:09 +00:00
|
|
|
push_back(createEntry(mi, index += SlotIndex::InstrDist));
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
// Save this base index in the maps.
|
2011-03-04 18:51:09 +00:00
|
|
|
mi2iMap.insert(std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
++functionSize;
|
|
|
|
}
|
|
|
|
|
2011-03-04 18:51:09 +00:00
|
|
|
// We insert one blank instructions between basic blocks.
|
|
|
|
push_back(createEntry(0, index += SlotIndex::InstrDist));
|
2009-12-22 00:11:50 +00:00
|
|
|
|
2011-04-02 06:03:31 +00:00
|
|
|
MBBRanges[mbb->getNumber()].first = blockStartIndex;
|
|
|
|
MBBRanges[mbb->getNumber()].second = SlotIndex(back(), SlotIndex::LOAD);
|
2009-11-03 23:52:08 +00:00
|
|
|
idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the Idx2MBBMap
|
|
|
|
std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
|
|
|
|
|
|
|
|
DEBUG(dump());
|
|
|
|
|
|
|
|
// And we're done!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-14 00:02:51 +00:00
|
|
|
void SlotIndexes::renumberIndexes() {
|
2009-11-05 22:20:57 +00:00
|
|
|
// Renumber updates the index of every element of the index list.
|
2011-02-03 20:29:41 +00:00
|
|
|
DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
|
2011-03-04 19:43:38 +00:00
|
|
|
++NumGlobalRenum;
|
2009-11-03 23:52:08 +00:00
|
|
|
|
2009-11-05 22:20:57 +00:00
|
|
|
unsigned index = 0;
|
|
|
|
|
|
|
|
for (IndexListEntry *curEntry = front(); curEntry != getTail();
|
|
|
|
curEntry = curEntry->getNext()) {
|
|
|
|
curEntry->setIndex(index);
|
2011-03-04 18:51:09 +00:00
|
|
|
index += SlotIndex::InstrDist;
|
2009-11-05 22:20:57 +00:00
|
|
|
}
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
2011-03-04 19:43:38 +00:00
|
|
|
// Renumber indexes locally after curEntry was inserted, but failed to get a new
|
|
|
|
// index.
|
|
|
|
void SlotIndexes::renumberIndexes(IndexListEntry *curEntry) {
|
|
|
|
// Number indexes with half the default spacing so we can catch up quickly.
|
|
|
|
const unsigned Space = SlotIndex::InstrDist/2;
|
|
|
|
assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM");
|
|
|
|
|
|
|
|
IndexListEntry *start = curEntry->getPrev();
|
|
|
|
unsigned index = start->getIndex();
|
|
|
|
IndexListEntry *tail = getTail();
|
|
|
|
do {
|
|
|
|
curEntry->setIndex(index += Space);
|
|
|
|
curEntry = curEntry->getNext();
|
|
|
|
// If the next index is bigger, we have caught up.
|
|
|
|
} while (curEntry != tail && curEntry->getIndex() <= index);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << start->getIndex() << '-'
|
|
|
|
<< index << " ***\n");
|
|
|
|
++NumLocalRenum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-03 23:52:08 +00:00
|
|
|
void SlotIndexes::dump() const {
|
|
|
|
for (const IndexListEntry *itr = front(); itr != getTail();
|
|
|
|
itr = itr->getNext()) {
|
2010-01-05 01:25:50 +00:00
|
|
|
dbgs() << itr->getIndex() << " ";
|
2009-11-03 23:52:08 +00:00
|
|
|
|
|
|
|
if (itr->getInstr() != 0) {
|
2010-01-05 01:25:50 +00:00
|
|
|
dbgs() << *itr->getInstr();
|
2009-11-03 23:52:08 +00:00
|
|
|
} else {
|
2010-01-05 01:25:50 +00:00
|
|
|
dbgs() << "\n";
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 06:03:31 +00:00
|
|
|
for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
|
|
|
|
dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
|
|
|
|
<< MBBRanges[i].second << ")\n";
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print a SlotIndex to a raw_ostream.
|
|
|
|
void SlotIndex::print(raw_ostream &os) const {
|
2011-02-03 20:29:41 +00:00
|
|
|
if (isValid())
|
|
|
|
os << entry().getIndex() << "LudS"[getSlot()];
|
|
|
|
else
|
|
|
|
os << "invalid";
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump a SlotIndex to stderr.
|
|
|
|
void SlotIndex::dump() const {
|
2010-01-05 01:25:50 +00:00
|
|
|
print(dbgs());
|
|
|
|
dbgs() << "\n";
|
2009-11-03 23:52:08 +00:00
|
|
|
}
|
|
|
|
|