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 terminatorGaps.clear(); 65 clearList(); 66 } 67 68 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) { 69 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 77 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. 80 81 mf = &fn; 82 initList(); 83 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?"); 93 94 functionSize = 0; 95 unsigned index = 0; 96 97 push_back(createEntry(0, index)); 98 99 // Iterate over the function. 100 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end(); 101 mbbItr != mbbEnd; ++mbbItr) { 102 MachineBasicBlock *mbb = &*mbbItr; 103 104 // Insert an index for the MBB start. 105 SlotIndex blockStartIndex(back(), SlotIndex::LOAD); 106 107 index += SlotIndex::NUM; 108 109 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end(); 110 miItr != miEnd; ++miItr) { 111 MachineInstr *mi = miItr; 112 if (mi->isDebugValue()) 113 continue; 114 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; 120 } 121 122 // Insert a store index for the instr. 123 push_back(createEntry(mi, index)); 124 125 // Save this base index in the maps. 126 mi2iMap.insert( 127 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD))); 128 129 ++functionSize; 130 131 unsigned Slots = mi->getDesc().getNumDefs(); 132 if (Slots == 0) 133 Slots = 1; 134 135 index += (Slots + 1) * SlotIndex::NUM; 136 } 137 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; 143 } 144 145 // One blank instruction at the end. 146 push_back(createEntry(0, index)); 147 148 SlotIndex blockEndIndex(back(), SlotIndex::LOAD); 149 mbb2IdxMap.insert( 150 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex))); 151 152 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); 153 } 154 155 // Sort the Idx2MBBMap 156 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); 157 158 DEBUG(dump()); 159 160 // And we're done! 161 return false; 162 } 163 164 void SlotIndexes::renumberIndexes() { 165 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 171 // had been present. 172 173 functionSize = 0; 174 unsigned index = 0; 175 176 for (IndexListEntry *curEntry = front(); curEntry != getTail(); 177 curEntry = curEntry->getNext()) { 178 179 curEntry->setIndex(index); 180 181 if (curEntry->getInstr() == 0) { 182 // MBB start entry or terminator gap. Just step index by 1. 183 index += SlotIndex::NUM; 184 } 185 else { 186 ++functionSize; 187 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs(); 188 if (Slots == 0) 189 Slots = 1; 190 191 index += (Slots + 1) * SlotIndex::NUM; 192 } 193 } 194 } 195 196 void SlotIndexes::dump() const { 197 for (const IndexListEntry *itr = front(); itr != getTail(); 198 itr = itr->getNext()) { 199 dbgs() << itr->getIndex() << " "; 200 201 if (itr->getInstr() != 0) { 202 dbgs() << *itr->getInstr(); 203 } else { 204 dbgs() << "\n"; 205 } 206 } 207 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"; 212 } 213 } 214 215 // Print a SlotIndex to a raw_ostream. 216 void SlotIndex::print(raw_ostream &os) const { 217 os << entry().getIndex(); 218 if (isPHI()) 219 os << "*"; 220 else 221 os << "LudS"[getSlot()]; 222 } 223 224 // Dump a SlotIndex to stderr. 225 void SlotIndex::dump() const { 226 print(dbgs()); 227 dbgs() << "\n"; 228 } 229 230