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" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
Lang Hames | 0368e50 | 2009-11-04 21:24:15 +0000 | [diff] [blame] | 19 | std::auto_ptr<IndexListEntry> IndexListEntry::emptyKeyEntry, |
| 20 | IndexListEntry::tombstoneKeyEntry; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 21 | |
| 22 | char SlotIndexes::ID = 0; |
| 23 | static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering"); |
| 24 | |
| 25 | void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const { |
| 26 | au.setPreservesAll(); |
| 27 | MachineFunctionPass::getAnalysisUsage(au); |
| 28 | } |
| 29 | |
| 30 | void SlotIndexes::releaseMemory() { |
| 31 | mi2iMap.clear(); |
| 32 | mbb2IdxMap.clear(); |
| 33 | idx2MBBMap.clear(); |
| 34 | terminatorGaps.clear(); |
| 35 | clearList(); |
| 36 | } |
| 37 | |
| 38 | bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) { |
| 39 | |
| 40 | // Compute numbering as follows: |
| 41 | // Grab an iterator to the start of the index list. |
| 42 | // Iterate over all MBBs, and within each MBB all MIs, keeping the MI |
| 43 | // iterator in lock-step (though skipping it over indexes which have |
| 44 | // null pointers in the instruction field). |
| 45 | // At each iteration assert that the instruction pointed to in the index |
| 46 | // is the same one pointed to by the MI iterator. This |
| 47 | |
| 48 | // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should |
| 49 | // only need to be set up once after the first numbering is computed. |
| 50 | |
| 51 | mf = &fn; |
| 52 | initList(); |
| 53 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 54 | // Check that the list contains only the sentinal. |
| 55 | assert(indexListHead->getNext() == 0 && |
| 56 | "Index list non-empty at initial numbering?"); |
| 57 | assert(idx2MBBMap.empty() && |
| 58 | "Index -> MBB mapping non-empty at initial numbering?"); |
| 59 | assert(mbb2IdxMap.empty() && |
| 60 | "MBB -> Index mapping non-empty at initial numbering?"); |
| 61 | assert(mi2iMap.empty() && |
| 62 | "MachineInstr -> Index mapping non-empty at initial numbering?"); |
| 63 | |
| 64 | functionSize = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 65 | unsigned index = 0; |
| 66 | |
| 67 | // Iterate over the the function. |
| 68 | for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end(); |
| 69 | mbbItr != mbbEnd; ++mbbItr) { |
| 70 | MachineBasicBlock *mbb = &*mbbItr; |
| 71 | |
| 72 | // Insert an index for the MBB start. |
| 73 | push_back(createEntry(0, index)); |
| 74 | SlotIndex blockStartIndex(back(), SlotIndex::LOAD); |
| 75 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame^] | 76 | index += SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 77 | |
| 78 | for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end(); |
| 79 | miItr != miEnd; ++miItr) { |
| 80 | MachineInstr *mi = &*miItr; |
| 81 | |
| 82 | if (miItr == mbb->getFirstTerminator()) { |
| 83 | push_back(createEntry(0, index)); |
| 84 | terminatorGaps.insert( |
| 85 | std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT))); |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame^] | 86 | index += SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Insert a store index for the instr. |
| 90 | push_back(createEntry(mi, index)); |
| 91 | |
| 92 | // Save this base index in the maps. |
| 93 | mi2iMap.insert( |
| 94 | std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD))); |
| 95 | |
| 96 | ++functionSize; |
| 97 | |
| 98 | unsigned Slots = mi->getDesc().getNumDefs(); |
| 99 | if (Slots == 0) |
| 100 | Slots = 1; |
| 101 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame^] | 102 | index += (Slots + 1) * SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if (mbb->getFirstTerminator() == mbb->end()) { |
| 106 | push_back(createEntry(0, index)); |
| 107 | terminatorGaps.insert( |
| 108 | std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT))); |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame^] | 109 | index += SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | SlotIndex blockEndIndex(back(), SlotIndex::STORE); |
| 113 | mbb2IdxMap.insert( |
| 114 | std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex))); |
| 115 | |
| 116 | idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); |
| 117 | } |
| 118 | |
| 119 | // One blank instruction at the end. |
| 120 | push_back(createEntry(0, index)); |
| 121 | |
| 122 | // Sort the Idx2MBBMap |
| 123 | std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); |
| 124 | |
| 125 | DEBUG(dump()); |
| 126 | |
| 127 | // And we're done! |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | void SlotIndexes::renumber() { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 132 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame^] | 133 | // Renumber updates the index of every element of the index list. |
| 134 | // If all instrs in the function have been allocated an index (which has been |
| 135 | // placed in the index list in the order of instruction iteration) then the |
| 136 | // resulting numbering will match what would have been generated by the |
| 137 | // pass during the initial numbering of the function if the new instructions |
| 138 | // had been present. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 139 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame^] | 140 | functionSize = 0; |
| 141 | unsigned index = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 142 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame^] | 143 | for (IndexListEntry *curEntry = front(); curEntry != getTail(); |
| 144 | curEntry = curEntry->getNext()) { |
| 145 | |
| 146 | curEntry->setIndex(index); |
| 147 | |
| 148 | if (curEntry->getInstr() == 0) { |
| 149 | // MBB start entry or terminator gap. Just step index by 1. |
| 150 | index += SlotIndex::NUM; |
| 151 | } |
| 152 | else { |
| 153 | ++functionSize; |
| 154 | unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs(); |
| 155 | if (Slots == 0) |
| 156 | Slots = 1; |
| 157 | |
| 158 | index += (Slots + 1) * SlotIndex::NUM; |
| 159 | |
| 160 | } |
| 161 | } |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void SlotIndexes::dump() const { |
| 165 | for (const IndexListEntry *itr = front(); itr != getTail(); |
| 166 | itr = itr->getNext()) { |
| 167 | errs() << itr->getIndex() << " "; |
| 168 | |
| 169 | if (itr->getInstr() != 0) { |
| 170 | errs() << *itr->getInstr(); |
| 171 | } else { |
| 172 | errs() << "\n"; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | for (MBB2IdxMap::iterator itr = mbb2IdxMap.begin(); |
| 177 | itr != mbb2IdxMap.end(); ++itr) { |
| 178 | errs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - [" |
| 179 | << itr->second.first << ", " << itr->second.second << "]\n"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Print a SlotIndex to a raw_ostream. |
| 184 | void SlotIndex::print(raw_ostream &os) const { |
| 185 | os << getIndex(); |
| 186 | if (isPHI()) |
| 187 | os << "*"; |
| 188 | } |
| 189 | |
| 190 | // Dump a SlotIndex to stderr. |
| 191 | void SlotIndex::dump() const { |
| 192 | print(errs()); |
| 193 | errs() << "\n"; |
| 194 | } |
| 195 | |