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"
20 // Yep - these are thread safe. See the header for details.
21 ManagedStatic<EmptyIndexListEntry> IndexListEntry::emptyKeyEntry;
22 ManagedStatic<TombstoneIndexListEntry> IndexListEntry::tombstoneKeyEntry;
24 char SlotIndexes::ID = 0;
25 static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering");
27 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
29 MachineFunctionPass::getAnalysisUsage(au);
32 void SlotIndexes::releaseMemory() {
36 terminatorGaps.clear();
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 // Iterate over the the function.
70 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
71 mbbItr != mbbEnd; ++mbbItr) {
72 MachineBasicBlock *mbb = &*mbbItr;
74 // Insert an index for the MBB start.
75 push_back(createEntry(0, index));
76 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
78 index += SlotIndex::NUM;
80 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
81 miItr != miEnd; ++miItr) {
82 MachineInstr *mi = &*miItr;
84 if (miItr == mbb->getFirstTerminator()) {
85 push_back(createEntry(0, index));
86 terminatorGaps.insert(
87 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
88 index += SlotIndex::NUM;
91 // Insert a store index for the instr.
92 push_back(createEntry(mi, index));
94 // Save this base index in the maps.
96 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
100 unsigned Slots = mi->getDesc().getNumDefs();
104 index += (Slots + 1) * SlotIndex::NUM;
107 if (mbb->getFirstTerminator() == mbb->end()) {
108 push_back(createEntry(0, index));
109 terminatorGaps.insert(
110 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
111 index += SlotIndex::NUM;
114 SlotIndex blockEndIndex(back(), SlotIndex::STORE);
116 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
118 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
121 // One blank instruction at the end.
122 push_back(createEntry(0, index));
124 // Sort the Idx2MBBMap
125 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
133 void SlotIndexes::renumber() {
135 // Renumber updates the index of every element of the index list.
136 // If all instrs in the function have been allocated an index (which has been
137 // placed in the index list in the order of instruction iteration) then the
138 // resulting numbering will match what would have been generated by the
139 // pass during the initial numbering of the function if the new instructions
145 for (IndexListEntry *curEntry = front(); curEntry != getTail();
146 curEntry = curEntry->getNext()) {
148 curEntry->setIndex(index);
150 if (curEntry->getInstr() == 0) {
151 // MBB start entry or terminator gap. Just step index by 1.
152 index += SlotIndex::NUM;
156 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
160 index += (Slots + 1) * SlotIndex::NUM;
166 void SlotIndexes::dump() const {
167 for (const IndexListEntry *itr = front(); itr != getTail();
168 itr = itr->getNext()) {
169 errs() << itr->getIndex() << " ";
171 if (itr->getInstr() != 0) {
172 errs() << *itr->getInstr();
178 for (MBB2IdxMap::iterator itr = mbb2IdxMap.begin();
179 itr != mbb2IdxMap.end(); ++itr) {
180 errs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
181 << itr->second.first << ", " << itr->second.second << "]\n";
185 // Print a SlotIndex to a raw_ostream.
186 void SlotIndex::print(raw_ostream &os) const {
192 // Dump a SlotIndex to stderr.
193 void SlotIndex::dump() const {