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" |
Jakob Stoklund Olesen | 10c5f2d | 2011-03-04 18:08:29 +0000 | [diff] [blame^] | 13 | #include "llvm/ADT/Statistic.h" |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFunction.h" |
| 15 | #include "llvm/Support/Debug.h" |
| 16 | #include "llvm/Support/raw_ostream.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 | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 21 | char SlotIndexes::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 22 | INITIALIZE_PASS(SlotIndexes, "slotindexes", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 23 | "Slot index numbering", false, false) |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 24 | |
Jakob Stoklund Olesen | 10c5f2d | 2011-03-04 18:08:29 +0000 | [diff] [blame^] | 25 | STATISTIC(NumRenumPasses, "Number of slot index renumber passes"); |
| 26 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 27 | void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const { |
| 28 | au.setPreservesAll(); |
| 29 | MachineFunctionPass::getAnalysisUsage(au); |
| 30 | } |
| 31 | |
| 32 | void SlotIndexes::releaseMemory() { |
| 33 | mi2iMap.clear(); |
| 34 | mbb2IdxMap.clear(); |
| 35 | idx2MBBMap.clear(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 36 | clearList(); |
| 37 | } |
| 38 | |
| 39 | bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) { |
| 40 | |
| 41 | // Compute numbering as follows: |
| 42 | // Grab an iterator to the start of the index list. |
| 43 | // Iterate over all MBBs, and within each MBB all MIs, keeping the MI |
| 44 | // iterator in lock-step (though skipping it over indexes which have |
| 45 | // null pointers in the instruction field). |
| 46 | // At each iteration assert that the instruction pointed to in the index |
| 47 | // is the same one pointed to by the MI iterator. This |
| 48 | |
| 49 | // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should |
| 50 | // only need to be set up once after the first numbering is computed. |
| 51 | |
| 52 | mf = &fn; |
| 53 | initList(); |
| 54 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 55 | // Check that the list contains only the sentinal. |
| 56 | assert(indexListHead->getNext() == 0 && |
| 57 | "Index list non-empty at initial numbering?"); |
| 58 | assert(idx2MBBMap.empty() && |
| 59 | "Index -> MBB mapping non-empty at initial numbering?"); |
| 60 | assert(mbb2IdxMap.empty() && |
| 61 | "MBB -> Index mapping non-empty at initial numbering?"); |
| 62 | assert(mi2iMap.empty() && |
| 63 | "MachineInstr -> Index mapping non-empty at initial numbering?"); |
| 64 | |
| 65 | functionSize = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 66 | unsigned index = 0; |
| 67 | |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 68 | push_back(createEntry(0, index)); |
| 69 | |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 70 | // Iterate over the function. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 71 | for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end(); |
| 72 | mbbItr != mbbEnd; ++mbbItr) { |
| 73 | MachineBasicBlock *mbb = &*mbbItr; |
| 74 | |
| 75 | // Insert an index for the MBB start. |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 76 | SlotIndex blockStartIndex(back(), SlotIndex::LOAD); |
| 77 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 78 | index += SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 79 | |
| 80 | for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end(); |
| 81 | miItr != miEnd; ++miItr) { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 82 | MachineInstr *mi = miItr; |
| 83 | if (mi->isDebugValue()) |
Dale Johannesen | 1caedd0 | 2010-01-22 22:38:21 +0000 | [diff] [blame] | 84 | continue; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 85 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 86 | // Insert a store index for the instr. |
| 87 | push_back(createEntry(mi, index)); |
| 88 | |
| 89 | // Save this base index in the maps. |
| 90 | mi2iMap.insert( |
| 91 | std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD))); |
| 92 | |
| 93 | ++functionSize; |
| 94 | |
| 95 | unsigned Slots = mi->getDesc().getNumDefs(); |
| 96 | if (Slots == 0) |
| 97 | Slots = 1; |
| 98 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 99 | index += (Slots + 1) * SlotIndex::NUM; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Jakob Stoklund Olesen | 1e8e72d | 2010-11-11 00:19:20 +0000 | [diff] [blame] | 102 | // We insert two blank instructions between basic blocks. |
| 103 | // One to represent live-out registers and one to represent live-ins. |
| 104 | push_back(createEntry(0, index)); |
| 105 | index += SlotIndex::NUM; |
| 106 | |
| 107 | push_back(createEntry(0, index)); |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 108 | |
| 109 | SlotIndex blockEndIndex(back(), SlotIndex::LOAD); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 110 | mbb2IdxMap.insert( |
| 111 | std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex))); |
| 112 | |
| 113 | idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); |
| 114 | } |
| 115 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 116 | // Sort the Idx2MBBMap |
| 117 | std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); |
| 118 | |
| 119 | DEBUG(dump()); |
| 120 | |
| 121 | // And we're done! |
| 122 | return false; |
| 123 | } |
| 124 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 125 | void SlotIndexes::renumberIndexes() { |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 126 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 127 | // Renumber updates the index of every element of the index list. |
| 128 | // If all instrs in the function have been allocated an index (which has been |
| 129 | // placed in the index list in the order of instruction iteration) then the |
| 130 | // resulting numbering will match what would have been generated by the |
| 131 | // pass during the initial numbering of the function if the new instructions |
| 132 | // had been present. |
Jakob Stoklund Olesen | 97af986 | 2011-02-03 20:29:41 +0000 | [diff] [blame] | 133 | DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n"); |
Jakob Stoklund Olesen | 10c5f2d | 2011-03-04 18:08:29 +0000 | [diff] [blame^] | 134 | ++NumRenumPasses; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 135 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 136 | unsigned index = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 137 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 138 | for (IndexListEntry *curEntry = front(); curEntry != getTail(); |
| 139 | curEntry = curEntry->getNext()) { |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 140 | curEntry->setIndex(index); |
Jakob Stoklund Olesen | 2c11eb3 | 2011-03-03 06:29:01 +0000 | [diff] [blame] | 141 | index += 4*SlotIndex::NUM; |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 142 | } |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void SlotIndexes::dump() const { |
| 146 | for (const IndexListEntry *itr = front(); itr != getTail(); |
| 147 | itr = itr->getNext()) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 148 | dbgs() << itr->getIndex() << " "; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 149 | |
| 150 | if (itr->getInstr() != 0) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 151 | dbgs() << *itr->getInstr(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 152 | } else { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 153 | dbgs() << "\n"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Jeffrey Yasskin | 81cf432 | 2009-11-10 01:02:17 +0000 | [diff] [blame] | 157 | for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 158 | itr != mbb2IdxMap.end(); ++itr) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 159 | dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - [" |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 160 | << itr->second.first << ", " << itr->second.second << "]\n"; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Print a SlotIndex to a raw_ostream. |
| 165 | void SlotIndex::print(raw_ostream &os) const { |
Jakob Stoklund Olesen | 97af986 | 2011-02-03 20:29:41 +0000 | [diff] [blame] | 166 | if (isValid()) |
| 167 | os << entry().getIndex() << "LudS"[getSlot()]; |
| 168 | else |
| 169 | os << "invalid"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // Dump a SlotIndex to stderr. |
| 173 | void SlotIndex::dump() const { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 174 | print(dbgs()); |
| 175 | dbgs() << "\n"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 176 | } |
| 177 | |