Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 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" |
Lang Hames | 16dcaf5 | 2009-11-08 08:49:59 +0000 | [diff] [blame] | 16 | #include "llvm/Support/ManagedStatic.h" |
Dale Johannesen | 1caedd0 | 2010-01-22 22:38:21 +0000 | [diff] [blame^] | 17 | #include "llvm/Target/TargetInstrInfo.h" |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
Lang Hames | d6ef7fa | 2009-11-07 05:50:28 +0000 | [diff] [blame] | 21 | |
| 22 | // Yep - these are thread safe. See the header for details. |
Lang Hames | 16dcaf5 | 2009-11-08 08:49:59 +0000 | [diff] [blame] | 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 | } |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 41 | |
| 42 | char SlotIndexes::ID = 0; |
| 43 | static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering"); |
| 44 | |
Lang Hames | 16dcaf5 | 2009-11-08 08:49:59 +0000 | [diff] [blame] | 45 | IndexListEntry* IndexListEntry::getEmptyKeyEntry() { |
| 46 | return &*IndexListEntryEmptyKey; |
| 47 | } |
| 48 | |
| 49 | IndexListEntry* IndexListEntry::getTombstoneKeyEntry() { |
| 50 | return &*IndexListEntryTombstoneKey; |
| 51 | } |
| 52 | |
| 53 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 54 | void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const { |
| 55 | au.setPreservesAll(); |
| 56 | MachineFunctionPass::getAnalysisUsage(au); |
| 57 | } |
| 58 | |
| 59 | void SlotIndexes::releaseMemory() { |
| 60 | mi2iMap.clear(); |
| 61 | mbb2IdxMap.clear(); |
| 62 | idx2MBBMap.clear(); |
| 63 | terminatorGaps.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 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 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; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 94 | unsigned index = 0; |
| 95 | |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 96 | push_back(createEntry(0, index)); |
| 97 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 98 | // Iterate over the 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. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 104 | SlotIndex blockStartIndex(back(), SlotIndex::LOAD); |
| 105 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 106 | index += SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 107 | |
| 108 | for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end(); |
| 109 | miItr != miEnd; ++miItr) { |
| 110 | MachineInstr *mi = &*miItr; |
Dale Johannesen | 1caedd0 | 2010-01-22 22:38:21 +0000 | [diff] [blame^] | 111 | if (mi->getOpcode()==TargetInstrInfo::DEBUG_VALUE) |
| 112 | continue; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 113 | |
| 114 | if (miItr == mbb->getFirstTerminator()) { |
| 115 | push_back(createEntry(0, index)); |
| 116 | terminatorGaps.insert( |
| 117 | std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT))); |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 118 | index += SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Insert a store index for the instr. |
| 122 | push_back(createEntry(mi, index)); |
| 123 | |
| 124 | // Save this base index in the maps. |
| 125 | mi2iMap.insert( |
| 126 | std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD))); |
| 127 | |
| 128 | ++functionSize; |
| 129 | |
| 130 | unsigned Slots = mi->getDesc().getNumDefs(); |
| 131 | if (Slots == 0) |
| 132 | Slots = 1; |
| 133 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 134 | index += (Slots + 1) * SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 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))); |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 141 | index += SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 144 | // One blank instruction at the end. |
| 145 | push_back(createEntry(0, index)); |
| 146 | |
| 147 | SlotIndex blockEndIndex(back(), SlotIndex::LOAD); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 148 | mbb2IdxMap.insert( |
| 149 | std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex))); |
| 150 | |
| 151 | idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); |
| 152 | } |
| 153 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 154 | // Sort the Idx2MBBMap |
| 155 | std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); |
| 156 | |
| 157 | DEBUG(dump()); |
| 158 | |
| 159 | // And we're done! |
| 160 | return false; |
| 161 | } |
| 162 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 163 | void SlotIndexes::renumberIndexes() { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 164 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 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 |
| 170 | // had been present. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 171 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 172 | functionSize = 0; |
| 173 | unsigned index = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 174 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 175 | for (IndexListEntry *curEntry = front(); curEntry != getTail(); |
| 176 | curEntry = curEntry->getNext()) { |
| 177 | |
| 178 | curEntry->setIndex(index); |
| 179 | |
| 180 | if (curEntry->getInstr() == 0) { |
| 181 | // MBB start entry or terminator gap. Just step index by 1. |
| 182 | index += SlotIndex::NUM; |
| 183 | } |
| 184 | else { |
| 185 | ++functionSize; |
| 186 | unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs(); |
| 187 | if (Slots == 0) |
| 188 | Slots = 1; |
| 189 | |
| 190 | index += (Slots + 1) * SlotIndex::NUM; |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void SlotIndexes::dump() const { |
| 196 | for (const IndexListEntry *itr = front(); itr != getTail(); |
| 197 | itr = itr->getNext()) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 198 | dbgs() << itr->getIndex() << " "; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 199 | |
| 200 | if (itr->getInstr() != 0) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 201 | dbgs() << *itr->getInstr(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 202 | } else { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 203 | dbgs() << "\n"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 207 | for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 208 | itr != mbb2IdxMap.end(); ++itr) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 209 | dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - [" |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 210 | << itr->second.first << ", " << itr->second.second << "]\n"; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Print a SlotIndex to a raw_ostream. |
| 215 | void SlotIndex::print(raw_ostream &os) const { |
| 216 | os << getIndex(); |
| 217 | if (isPHI()) |
| 218 | os << "*"; |
| 219 | } |
| 220 | |
| 221 | // Dump a SlotIndex to stderr. |
| 222 | void SlotIndex::dump() const { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 223 | print(dbgs()); |
| 224 | dbgs() << "\n"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 225 | } |
| 226 | |