2009-11-04 14:58:56 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
2011-05-02 19:34:44 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-11-04 14:58:56 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-01-23 11:09:33 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2009-11-04 14:58:56 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
#define DEBUG_TYPE "slotindexes"
|
|
|
|
|
2009-11-04 14:58:56 +00:00
|
|
|
char SlotIndexes::ID = 0;
|
2010-09-17 15:48:55 +00:00
|
|
|
INITIALIZE_PASS(SlotIndexes, "slotindexes",
|
2011-02-20 12:57:14 +00:00
|
|
|
"Slot index numbering", false, false)
|
2009-11-04 14:58:56 +00:00
|
|
|
|
2011-05-02 19:34:44 +00:00
|
|
|
STATISTIC(NumLocalRenum, "Number of local renumberings");
|
|
|
|
STATISTIC(NumGlobalRenum, "Number of global renumberings");
|
2009-11-18 14:58:34 +00:00
|
|
|
|
2009-11-04 14:58:56 +00:00
|
|
|
void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
|
|
|
|
au.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(au);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlotIndexes::releaseMemory() {
|
|
|
|
mi2iMap.clear();
|
2011-05-02 19:34:44 +00:00
|
|
|
MBBRanges.clear();
|
2009-11-04 14:58:56 +00:00
|
|
|
idx2MBBMap.clear();
|
2012-05-03 16:50:55 +00:00
|
|
|
indexList.clear();
|
|
|
|
ileAllocator.Reset();
|
2009-11-04 14:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2012-05-03 16:50:55 +00:00
|
|
|
// is the same one pointed to by the MI iterator. This
|
2009-11-04 14:58:56 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Check that the list contains only the sentinal.
|
2012-05-03 16:50:55 +00:00
|
|
|
assert(indexList.empty() && "Index list non-empty at initial numbering?");
|
2009-11-04 14:58:56 +00:00
|
|
|
assert(idx2MBBMap.empty() &&
|
|
|
|
"Index -> MBB mapping non-empty at initial numbering?");
|
2011-05-02 19:34:44 +00:00
|
|
|
assert(MBBRanges.empty() &&
|
2009-11-04 14:58:56 +00:00
|
|
|
"MBB -> Index mapping non-empty at initial numbering?");
|
|
|
|
assert(mi2iMap.empty() &&
|
|
|
|
"MachineInstr -> Index mapping non-empty at initial numbering?");
|
|
|
|
|
|
|
|
unsigned index = 0;
|
2011-05-02 19:34:44 +00:00
|
|
|
MBBRanges.resize(mf->getNumBlockIDs());
|
|
|
|
idx2MBBMap.reserve(mf->size());
|
2009-11-04 14:58:56 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
indexList.push_back(createEntry(nullptr, index));
|
2010-01-01 10:31:22 +00:00
|
|
|
|
2010-02-16 09:30:23 +00:00
|
|
|
// Iterate over the function.
|
2009-11-04 14:58:56 +00:00
|
|
|
for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
|
|
|
|
mbbItr != mbbEnd; ++mbbItr) {
|
|
|
|
MachineBasicBlock *mbb = &*mbbItr;
|
|
|
|
|
|
|
|
// Insert an index for the MBB start.
|
2012-05-03 16:50:55 +00:00
|
|
|
SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
|
2009-11-04 14:58:56 +00:00
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
|
|
|
|
miItr != miEnd; ++miItr) {
|
2010-02-16 09:30:23 +00:00
|
|
|
MachineInstr *mi = miItr;
|
|
|
|
if (mi->isDebugValue())
|
2010-01-23 11:09:33 +00:00
|
|
|
continue;
|
2009-11-04 14:58:56 +00:00
|
|
|
|
|
|
|
// Insert a store index for the instr.
|
2012-05-03 16:50:55 +00:00
|
|
|
indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist));
|
2009-11-04 14:58:56 +00:00
|
|
|
|
|
|
|
// Save this base index in the maps.
|
2012-05-03 16:50:55 +00:00
|
|
|
mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(),
|
2012-04-14 13:54:10 +00:00
|
|
|
SlotIndex::Slot_Block)));
|
2009-11-04 14:58:56 +00:00
|
|
|
}
|
|
|
|
|
2011-05-02 19:34:44 +00:00
|
|
|
// We insert one blank instructions between basic blocks.
|
2014-11-24 09:08:18 +00:00
|
|
|
indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
|
2009-11-04 14:58:56 +00:00
|
|
|
|
2011-05-02 19:34:44 +00:00
|
|
|
MBBRanges[mbb->getNumber()].first = blockStartIndex;
|
2012-05-03 16:50:55 +00:00
|
|
|
MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(),
|
2012-04-14 13:54:10 +00:00
|
|
|
SlotIndex::Slot_Block);
|
2009-11-04 14:58:56 +00:00
|
|
|
idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the Idx2MBBMap
|
|
|
|
std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
|
|
|
|
|
2012-04-14 13:54:10 +00:00
|
|
|
DEBUG(mf->print(dbgs(), this));
|
2009-11-04 14:58:56 +00:00
|
|
|
|
|
|
|
// And we're done!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-18 14:58:34 +00:00
|
|
|
void SlotIndexes::renumberIndexes() {
|
|
|
|
// Renumber updates the index of every element of the index list.
|
2011-02-20 12:57:14 +00:00
|
|
|
DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
|
2011-05-02 19:34:44 +00:00
|
|
|
++NumGlobalRenum;
|
2009-11-04 14:58:56 +00:00
|
|
|
|
2009-11-18 14:58:34 +00:00
|
|
|
unsigned index = 0;
|
|
|
|
|
2012-05-03 16:50:55 +00:00
|
|
|
for (IndexList::iterator I = indexList.begin(), E = indexList.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
I->setIndex(index);
|
2011-05-02 19:34:44 +00:00
|
|
|
index += SlotIndex::InstrDist;
|
2009-11-18 14:58:34 +00:00
|
|
|
}
|
2009-11-04 14:58:56 +00:00
|
|
|
}
|
|
|
|
|
2012-05-03 16:50:55 +00:00
|
|
|
// Renumber indexes locally after curItr was inserted, but failed to get a new
|
2011-05-02 19:34:44 +00:00
|
|
|
// index.
|
2012-05-03 16:50:55 +00:00
|
|
|
void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
|
2011-05-02 19:34:44 +00:00
|
|
|
// Number indexes with half the default spacing so we can catch up quickly.
|
|
|
|
const unsigned Space = SlotIndex::InstrDist/2;
|
2015-05-27 18:44:32 +00:00
|
|
|
static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
|
2011-05-02 19:34:44 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
IndexList::iterator startItr = std::prev(curItr);
|
2012-05-03 16:50:55 +00:00
|
|
|
unsigned index = startItr->getIndex();
|
2011-05-02 19:34:44 +00:00
|
|
|
do {
|
2012-05-03 16:50:55 +00:00
|
|
|
curItr->setIndex(index += Space);
|
|
|
|
++curItr;
|
2011-05-02 19:34:44 +00:00
|
|
|
// If the next index is bigger, we have caught up.
|
2012-05-03 16:50:55 +00:00
|
|
|
} while (curItr != indexList.end() && curItr->getIndex() <= index);
|
2011-05-02 19:34:44 +00:00
|
|
|
|
2012-05-03 16:50:55 +00:00
|
|
|
DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
|
2011-05-02 19:34:44 +00:00
|
|
|
<< index << " ***\n");
|
|
|
|
++NumLocalRenum;
|
|
|
|
}
|
|
|
|
|
2013-04-08 18:41:23 +00:00
|
|
|
// Repair indexes after adding and removing instructions.
|
|
|
|
void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
|
|
|
|
MachineBasicBlock::iterator Begin,
|
|
|
|
MachineBasicBlock::iterator End) {
|
|
|
|
// FIXME: Is this really necessary? The only caller repairIntervalsForRange()
|
|
|
|
// does the same thing.
|
|
|
|
// Find anchor points, which are at the beginning/end of blocks or at
|
|
|
|
// instructions that already have indexes.
|
|
|
|
while (Begin != MBB->begin() && !hasIndex(Begin))
|
|
|
|
--Begin;
|
|
|
|
while (End != MBB->end() && !hasIndex(End))
|
|
|
|
++End;
|
|
|
|
|
|
|
|
bool includeStart = (Begin == MBB->begin());
|
|
|
|
SlotIndex startIdx;
|
|
|
|
if (includeStart)
|
|
|
|
startIdx = getMBBStartIdx(MBB);
|
|
|
|
else
|
|
|
|
startIdx = getInstructionIndex(Begin);
|
|
|
|
|
|
|
|
SlotIndex endIdx;
|
|
|
|
if (End == MBB->end())
|
|
|
|
endIdx = getMBBEndIdx(MBB);
|
|
|
|
else
|
|
|
|
endIdx = getInstructionIndex(End);
|
|
|
|
|
|
|
|
// FIXME: Conceptually, this code is implementing an iterator on MBB that
|
|
|
|
// optionally includes an additional position prior to MBB->begin(), indicated
|
|
|
|
// by the includeStart flag. This is done so that we can iterate MIs in a MBB
|
|
|
|
// in parallel with SlotIndexes, but there should be a better way to do this.
|
|
|
|
IndexList::iterator ListB = startIdx.listEntry();
|
|
|
|
IndexList::iterator ListI = endIdx.listEntry();
|
|
|
|
MachineBasicBlock::iterator MBBI = End;
|
|
|
|
bool pastStart = false;
|
|
|
|
while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
|
|
|
|
assert(ListI->getIndex() >= startIdx.getIndex() &&
|
|
|
|
(includeStart || !pastStart) &&
|
|
|
|
"Decremented past the beginning of region to repair.");
|
|
|
|
|
|
|
|
MachineInstr *SlotMI = ListI->getInstr();
|
2014-11-24 09:08:18 +00:00
|
|
|
MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? MBBI : nullptr;
|
2013-04-08 18:41:23 +00:00
|
|
|
bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
|
|
|
|
|
|
|
|
if (SlotMI == MI && !MBBIAtBegin) {
|
|
|
|
--ListI;
|
|
|
|
if (MBBI != Begin)
|
|
|
|
--MBBI;
|
|
|
|
else
|
|
|
|
pastStart = true;
|
|
|
|
} else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
|
|
|
|
if (MBBI != Begin)
|
|
|
|
--MBBI;
|
|
|
|
else
|
|
|
|
pastStart = true;
|
|
|
|
} else {
|
|
|
|
--ListI;
|
|
|
|
if (SlotMI)
|
|
|
|
removeMachineInstrFromMaps(SlotMI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In theory this could be combined with the previous loop, but it is tricky
|
|
|
|
// to update the IndexList while we are iterating it.
|
|
|
|
for (MachineBasicBlock::iterator I = End; I != Begin;) {
|
|
|
|
--I;
|
|
|
|
MachineInstr *MI = I;
|
|
|
|
if (!MI->isDebugValue() && mi2iMap.find(MI) == mi2iMap.end())
|
|
|
|
insertMachineInstrInMaps(MI);
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 19:34:44 +00:00
|
|
|
|
2012-12-02 13:10:19 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2009-11-04 14:58:56 +00:00
|
|
|
void SlotIndexes::dump() const {
|
2012-05-03 16:50:55 +00:00
|
|
|
for (IndexList::const_iterator itr = indexList.begin();
|
|
|
|
itr != indexList.end(); ++itr) {
|
2010-01-15 15:37:28 +00:00
|
|
|
dbgs() << itr->getIndex() << " ";
|
2009-11-04 14:58:56 +00:00
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
if (itr->getInstr()) {
|
2010-01-15 15:37:28 +00:00
|
|
|
dbgs() << *itr->getInstr();
|
2009-11-04 14:58:56 +00:00
|
|
|
} else {
|
2010-01-15 15:37:28 +00:00
|
|
|
dbgs() << "\n";
|
2009-11-04 14:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-02 19:34:44 +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-04 14:58:56 +00:00
|
|
|
}
|
2012-12-02 13:10:19 +00:00
|
|
|
#endif
|
2009-11-04 14:58:56 +00:00
|
|
|
|
|
|
|
// Print a SlotIndex to a raw_ostream.
|
|
|
|
void SlotIndex::print(raw_ostream &os) const {
|
2011-02-20 12:57:14 +00:00
|
|
|
if (isValid())
|
2012-05-03 16:50:55 +00:00
|
|
|
os << listEntry()->getIndex() << "Berd"[getSlot()];
|
2010-07-13 17:19:57 +00:00
|
|
|
else
|
2011-02-20 12:57:14 +00:00
|
|
|
os << "invalid";
|
2009-11-04 14:58:56 +00:00
|
|
|
}
|
|
|
|
|
2012-12-02 13:10:19 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2009-11-04 14:58:56 +00:00
|
|
|
// Dump a SlotIndex to stderr.
|
|
|
|
void SlotIndex::dump() const {
|
2010-01-15 15:37:28 +00:00
|
|
|
print(dbgs());
|
|
|
|
dbgs() << "\n";
|
2009-11-04 14:58:56 +00:00
|
|
|
}
|
2012-12-02 13:10:19 +00:00
|
|
|
#endif
|
2009-11-04 14:58:56 +00:00
|
|
|
|