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 INITIALIZE_PASS(SlotIndexes, "slotindexes",
44 "Slot index numbering", false, false);
46 IndexListEntry* IndexListEntry::getEmptyKeyEntry() {
47 return &*IndexListEntryEmptyKey;
50 IndexListEntry* IndexListEntry::getTombstoneKeyEntry() {
51 return &*IndexListEntryTombstoneKey;
55 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
57 MachineFunctionPass::getAnalysisUsage(au);
60 void SlotIndexes::releaseMemory() {
64 terminatorGaps.clear();
68 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
70 // Compute numbering as follows:
71 // Grab an iterator to the start of the index list.
72 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
73 // iterator in lock-step (though skipping it over indexes which have
74 // null pointers in the instruction field).
75 // At each iteration assert that the instruction pointed to in the index
76 // is the same one pointed to by the MI iterator. This
78 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
79 // only need to be set up once after the first numbering is computed.
84 // Check that the list contains only the sentinal.
85 assert(indexListHead->getNext() == 0 &&
86 "Index list non-empty at initial numbering?");
87 assert(idx2MBBMap.empty() &&
88 "Index -> MBB mapping non-empty at initial numbering?");
89 assert(mbb2IdxMap.empty() &&
90 "MBB -> Index mapping non-empty at initial numbering?");
91 assert(mi2iMap.empty() &&
92 "MachineInstr -> Index mapping non-empty at initial numbering?");
97 push_back(createEntry(0, index));
99 // Iterate over the function.
100 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
101 mbbItr != mbbEnd; ++mbbItr) {
102 MachineBasicBlock *mbb = &*mbbItr;
104 // Insert an index for the MBB start.
105 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
107 index += SlotIndex::NUM;
109 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
110 miItr != miEnd; ++miItr) {
111 MachineInstr *mi = miItr;
112 if (mi->isDebugValue())
115 if (miItr == mbb->getFirstTerminator()) {
116 push_back(createEntry(0, index));
117 terminatorGaps.insert(
118 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
119 index += SlotIndex::NUM;
122 // Insert a store index for the instr.
123 push_back(createEntry(mi, index));
125 // Save this base index in the maps.
127 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
131 unsigned Slots = mi->getDesc().getNumDefs();
135 index += (Slots + 1) * SlotIndex::NUM;
138 if (mbb->getFirstTerminator() == mbb->end()) {
139 push_back(createEntry(0, index));
140 terminatorGaps.insert(
141 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
142 index += SlotIndex::NUM;
145 // One blank instruction at the end.
146 push_back(createEntry(0, index));
148 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
150 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
152 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
155 // Sort the Idx2MBBMap
156 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
164 void SlotIndexes::renumberIndexes() {
166 // Renumber updates the index of every element of the index list.
167 // If all instrs in the function have been allocated an index (which has been
168 // placed in the index list in the order of instruction iteration) then the
169 // resulting numbering will match what would have been generated by the
170 // pass during the initial numbering of the function if the new instructions
176 for (IndexListEntry *curEntry = front(); curEntry != getTail();
177 curEntry = curEntry->getNext()) {
179 curEntry->setIndex(index);
181 if (curEntry->getInstr() == 0) {
182 // MBB start entry or terminator gap. Just step index by 1.
183 index += SlotIndex::NUM;
187 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
191 index += (Slots + 1) * SlotIndex::NUM;
196 void SlotIndexes::dump() const {
197 for (const IndexListEntry *itr = front(); itr != getTail();
198 itr = itr->getNext()) {
199 dbgs() << itr->getIndex() << " ";
201 if (itr->getInstr() != 0) {
202 dbgs() << *itr->getInstr();
208 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
209 itr != mbb2IdxMap.end(); ++itr) {
210 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
211 << itr->second.first << ", " << itr->second.second << "]\n";
215 // Print a SlotIndex to a raw_ostream.
216 void SlotIndex::print(raw_ostream &os) const {
217 os << entry().getIndex();
221 os << "LudS"[getSlot()];
224 // Dump a SlotIndex to stderr.
225 void SlotIndex::dump() const {