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/CodeGen/MachineFunction.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Target/TargetInstrInfo.h"
22 // Yep - these are thread safe. See the header for details.
26 class EmptyIndexListEntry : public IndexListEntry {
28 EmptyIndexListEntry() : IndexListEntry(EMPTY_KEY) {}
31 class TombstoneIndexListEntry : public IndexListEntry {
33 TombstoneIndexListEntry() : IndexListEntry(TOMBSTONE_KEY) {}
36 // The following statics are thread safe. They're read only, and you
37 // can't step from them to any other list entries.
38 ManagedStatic<EmptyIndexListEntry> IndexListEntryEmptyKey;
39 ManagedStatic<TombstoneIndexListEntry> IndexListEntryTombstoneKey;
42 char SlotIndexes::ID = 0;
43 static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering");
45 IndexListEntry* IndexListEntry::getEmptyKeyEntry() {
46 return &*IndexListEntryEmptyKey;
49 IndexListEntry* IndexListEntry::getTombstoneKeyEntry() {
50 return &*IndexListEntryTombstoneKey;
54 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
56 MachineFunctionPass::getAnalysisUsage(au);
59 void SlotIndexes::releaseMemory() {
63 terminatorGaps.clear();
67 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
69 // Compute numbering as follows:
70 // Grab an iterator to the start of the index list.
71 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
72 // iterator in lock-step (though skipping it over indexes which have
73 // null pointers in the instruction field).
74 // At each iteration assert that the instruction pointed to in the index
75 // is the same one pointed to by the MI iterator. This
77 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
78 // only need to be set up once after the first numbering is computed.
83 // Check that the list contains only the sentinal.
84 assert(indexListHead->getNext() == 0 &&
85 "Index list non-empty at initial numbering?");
86 assert(idx2MBBMap.empty() &&
87 "Index -> MBB mapping non-empty at initial numbering?");
88 assert(mbb2IdxMap.empty() &&
89 "MBB -> Index mapping non-empty at initial numbering?");
90 assert(mi2iMap.empty() &&
91 "MachineInstr -> Index mapping non-empty at initial numbering?");
96 push_back(createEntry(0, index));
98 // Iterate over the function.
99 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
100 mbbItr != mbbEnd; ++mbbItr) {
101 MachineBasicBlock *mbb = &*mbbItr;
103 // Insert an index for the MBB start.
104 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
106 index += SlotIndex::NUM;
108 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
109 miItr != miEnd; ++miItr) {
110 MachineInstr *mi = miItr;
111 if (mi->isDebugValue())
114 if (miItr == mbb->getFirstTerminator()) {
115 push_back(createEntry(0, index));
116 terminatorGaps.insert(
117 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
118 index += SlotIndex::NUM;
121 // Insert a store index for the instr.
122 push_back(createEntry(mi, index));
124 // Save this base index in the maps.
126 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
130 unsigned Slots = mi->getDesc().getNumDefs();
134 index += (Slots + 1) * SlotIndex::NUM;
137 if (mbb->getFirstTerminator() == mbb->end()) {
138 push_back(createEntry(0, index));
139 terminatorGaps.insert(
140 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
141 index += SlotIndex::NUM;
144 // One blank instruction at the end.
145 push_back(createEntry(0, index));
147 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
149 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
151 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
154 // Sort the Idx2MBBMap
155 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
163 void SlotIndexes::renumberIndexes() {
165 // Renumber updates the index of every element of the index list.
166 // If all instrs in the function have been allocated an index (which has been
167 // placed in the index list in the order of instruction iteration) then the
168 // resulting numbering will match what would have been generated by the
169 // pass during the initial numbering of the function if the new instructions
175 for (IndexListEntry *curEntry = front(); curEntry != getTail();
176 curEntry = curEntry->getNext()) {
178 curEntry->setIndex(index);
180 if (curEntry->getInstr() == 0) {
181 // MBB start entry or terminator gap. Just step index by 1.
182 index += SlotIndex::NUM;
186 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
190 index += (Slots + 1) * SlotIndex::NUM;
195 void SlotIndexes::dump() const {
196 for (const IndexListEntry *itr = front(); itr != getTail();
197 itr = itr->getNext()) {
198 dbgs() << itr->getIndex() << " ";
200 if (itr->getInstr() != 0) {
201 dbgs() << *itr->getInstr();
207 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
208 itr != mbb2IdxMap.end(); ++itr) {
209 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
210 << itr->second.first << ", " << itr->second.second << "]\n";
214 // Print a SlotIndex to a raw_ostream.
215 void SlotIndex::print(raw_ostream &os) const {
216 os << entry().getIndex();
220 os << "LudS"[getSlot()];
223 // Dump a SlotIndex to stderr.
224 void SlotIndex::dump() const {