1 //===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "slotindexes"
12 #include "llvm/CodeGen/SlotIndexes.h"
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/Target/TargetInstrInfo.h"
21 char SlotIndexes::ID = 0;
22 INITIALIZE_PASS(SlotIndexes, "slotindexes",
23 "Slot index numbering", false, false)
25 STATISTIC(NumLocalRenum, "Number of local renumberings");
26 STATISTIC(NumGlobalRenum, "Number of global renumberings");
28 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
30 MachineFunctionPass::getAnalysisUsage(au);
33 void SlotIndexes::releaseMemory() {
40 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
42 // Compute numbering as follows:
43 // Grab an iterator to the start of the index list.
44 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
45 // iterator in lock-step (though skipping it over indexes which have
46 // null pointers in the instruction field).
47 // At each iteration assert that the instruction pointed to in the index
48 // is the same one pointed to by the MI iterator. This
50 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
51 // only need to be set up once after the first numbering is computed.
56 // Check that the list contains only the sentinal.
57 assert(indexListHead->getNext() == 0 &&
58 "Index list non-empty at initial numbering?");
59 assert(idx2MBBMap.empty() &&
60 "Index -> MBB mapping non-empty at initial numbering?");
61 assert(mbb2IdxMap.empty() &&
62 "MBB -> Index mapping non-empty at initial numbering?");
63 assert(mi2iMap.empty() &&
64 "MachineInstr -> Index mapping non-empty at initial numbering?");
69 push_back(createEntry(0, index));
71 // Iterate over the function.
72 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
73 mbbItr != mbbEnd; ++mbbItr) {
74 MachineBasicBlock *mbb = &*mbbItr;
76 // Insert an index for the MBB start.
77 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
79 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
80 miItr != miEnd; ++miItr) {
81 MachineInstr *mi = miItr;
82 if (mi->isDebugValue())
85 // Insert a store index for the instr.
86 push_back(createEntry(mi, index += SlotIndex::InstrDist));
88 // Save this base index in the maps.
89 mi2iMap.insert(std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
94 // We insert one blank instructions between basic blocks.
95 push_back(createEntry(0, index += SlotIndex::InstrDist));
97 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
99 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
101 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
104 // Sort the Idx2MBBMap
105 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
113 void SlotIndexes::renumberIndexes() {
114 // Renumber updates the index of every element of the index list.
115 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
120 for (IndexListEntry *curEntry = front(); curEntry != getTail();
121 curEntry = curEntry->getNext()) {
122 curEntry->setIndex(index);
123 index += SlotIndex::InstrDist;
127 // Renumber indexes locally after curEntry was inserted, but failed to get a new
129 void SlotIndexes::renumberIndexes(IndexListEntry *curEntry) {
130 // Number indexes with half the default spacing so we can catch up quickly.
131 const unsigned Space = SlotIndex::InstrDist/2;
132 assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM");
134 IndexListEntry *start = curEntry->getPrev();
135 unsigned index = start->getIndex();
136 IndexListEntry *tail = getTail();
138 curEntry->setIndex(index += Space);
139 curEntry = curEntry->getNext();
140 // If the next index is bigger, we have caught up.
141 } while (curEntry != tail && curEntry->getIndex() <= index);
143 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << start->getIndex() << '-'
144 << index << " ***\n");
149 void SlotIndexes::dump() const {
150 for (const IndexListEntry *itr = front(); itr != getTail();
151 itr = itr->getNext()) {
152 dbgs() << itr->getIndex() << " ";
154 if (itr->getInstr() != 0) {
155 dbgs() << *itr->getInstr();
161 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
162 itr != mbb2IdxMap.end(); ++itr) {
163 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
164 << itr->second.first << ", " << itr->second.second << "]\n";
168 // Print a SlotIndex to a raw_ostream.
169 void SlotIndex::print(raw_ostream &os) const {
171 os << entry().getIndex() << "LudS"[getSlot()];
176 // Dump a SlotIndex to stderr.
177 void SlotIndex::dump() const {