1 //===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #define DEBUG_TYPE "slotindexes" 11 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" 18 19 using namespace llvm; 20 21 22 // Yep - these are thread safe. See the header for details. 23 namespace { 24 25 26 class EmptyIndexListEntry : public IndexListEntry { 27 public: 28 EmptyIndexListEntry() : IndexListEntry(EMPTY_KEY) {} 29 }; 30 31 class TombstoneIndexListEntry : public IndexListEntry { 32 public: 33 TombstoneIndexListEntry() : IndexListEntry(TOMBSTONE_KEY) {} 34 }; 35 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; 40 } 41 42 char SlotIndexes::ID = 0; 43 INITIALIZE_PASS(SlotIndexes, "slotindexes", 44 "Slot index numbering", false, false) 45 46 IndexListEntry* IndexListEntry::getEmptyKeyEntry() { 47 return &*IndexListEntryEmptyKey; 48 } 49 50 IndexListEntry* IndexListEntry::getTombstoneKeyEntry() { 51 return &*IndexListEntryTombstoneKey; 52 } 53 54 55 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const { 56 au.setPreservesAll(); 57 MachineFunctionPass::getAnalysisUsage(au); 58 } 59 60 void SlotIndexes::releaseMemory() { 61 mi2iMap.clear(); 62 mbb2IdxMap.clear(); 63 idx2MBBMap.clear(); 64 clearList(); 65 } 66 67 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) { 68 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 76 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. 79 80 mf = &fn; 81 initList(); 82 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?"); 92 93 functionSize = 0; 94 unsigned index = 0; 95 96 push_back(createEntry(0, index)); 97 98 // Iterate over the function. 99 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end(); 100 mbbItr != mbbEnd; ++mbbItr) { 101 MachineBasicBlock *mbb = &*mbbItr; 102 103 // Insert an index for the MBB start. 104 SlotIndex blockStartIndex(back(), SlotIndex::LOAD); 105 106 index += SlotIndex::NUM; 107 108 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end(); 109 miItr != miEnd; ++miItr) { 110 MachineInstr *mi = miItr; 111 if (mi->isDebugValue()) 112 continue; 113 114 // Insert a store index for the instr. 115 push_back(createEntry(mi, index)); 116 117 // Save this base index in the maps. 118 mi2iMap.insert( 119 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD))); 120 121 ++functionSize; 122 123 unsigned Slots = mi->getDesc().getNumDefs(); 124 if (Slots == 0) 125 Slots = 1; 126 127 index += (Slots + 1) * SlotIndex::NUM; 128 } 129 130 // We insert two blank instructions between basic blocks. 131 // One to represent live-out registers and one to represent live-ins. 132 push_back(createEntry(0, index)); 133 index += SlotIndex::NUM; 134 135 push_back(createEntry(0, index)); 136 137 SlotIndex blockEndIndex(back(), SlotIndex::LOAD); 138 mbb2IdxMap.insert( 139 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex))); 140 141 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); 142 } 143 144 // Sort the Idx2MBBMap 145 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); 146 147 DEBUG(dump()); 148 149 // And we're done! 150 return false; 151 } 152 153 void SlotIndexes::renumberIndexes() { 154 155 // Renumber updates the index of every element of the index list. 156 // If all instrs in the function have been allocated an index (which has been 157 // placed in the index list in the order of instruction iteration) then the 158 // resulting numbering will match what would have been generated by the 159 // pass during the initial numbering of the function if the new instructions 160 // had been present. 161 162 functionSize = 0; 163 unsigned index = 0; 164 165 for (IndexListEntry *curEntry = front(); curEntry != getTail(); 166 curEntry = curEntry->getNext()) { 167 168 curEntry->setIndex(index); 169 170 if (curEntry->getInstr() == 0) { 171 // MBB start entry. Just step index by 1. 172 index += SlotIndex::NUM; 173 } 174 else { 175 ++functionSize; 176 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs(); 177 if (Slots == 0) 178 Slots = 1; 179 180 index += (Slots + 1) * SlotIndex::NUM; 181 } 182 } 183 } 184 185 void SlotIndexes::dump() const { 186 for (const IndexListEntry *itr = front(); itr != getTail(); 187 itr = itr->getNext()) { 188 dbgs() << itr->getIndex() << " "; 189 190 if (itr->getInstr() != 0) { 191 dbgs() << *itr->getInstr(); 192 } else { 193 dbgs() << "\n"; 194 } 195 } 196 197 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin(); 198 itr != mbb2IdxMap.end(); ++itr) { 199 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - [" 200 << itr->second.first << ", " << itr->second.second << "]\n"; 201 } 202 } 203 204 // Print a SlotIndex to a raw_ostream. 205 void SlotIndex::print(raw_ostream &os) const { 206 os << entry().getIndex() << "LudS"[getSlot()]; 207 } 208 209 // Dump a SlotIndex to stderr. 210 void SlotIndex::dump() const { 211 print(dbgs()); 212 dbgs() << "\n"; 213 } 214 215