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