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(); |
Jakob Stoklund Olesen | a122eaa | 2011-04-02 06:03:31 +0000 | [diff] [blame] | 35 | MBBRanges.clear(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 36 | idx2MBBMap.clear(); |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 37 | indexList.clear(); |
| 38 | ileAllocator.Reset(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) { |
| 42 | |
| 43 | // Compute numbering as follows: |
| 44 | // Grab an iterator to the start of the index list. |
| 45 | // Iterate over all MBBs, and within each MBB all MIs, keeping the MI |
| 46 | // iterator in lock-step (though skipping it over indexes which have |
| 47 | // null pointers in the instruction field). |
| 48 | // At each iteration assert that the instruction pointed to in the index |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 49 | // is the same one pointed to by the MI iterator. This |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 50 | |
| 51 | // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should |
| 52 | // only need to be set up once after the first numbering is computed. |
| 53 | |
| 54 | mf = &fn; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 55 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 56 | // Check that the list contains only the sentinal. |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 57 | assert(indexList.empty() && "Index list non-empty at initial numbering?"); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 58 | assert(idx2MBBMap.empty() && |
| 59 | "Index -> MBB mapping non-empty at initial numbering?"); |
Jakob Stoklund Olesen | a122eaa | 2011-04-02 06:03:31 +0000 | [diff] [blame] | 60 | assert(MBBRanges.empty() && |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 61 | "MBB -> Index mapping non-empty at initial numbering?"); |
| 62 | assert(mi2iMap.empty() && |
| 63 | "MachineInstr -> Index mapping non-empty at initial numbering?"); |
| 64 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 65 | unsigned index = 0; |
Jakob Stoklund Olesen | a122eaa | 2011-04-02 06:03:31 +0000 | [diff] [blame] | 66 | MBBRanges.resize(mf->getNumBlockIDs()); |
| 67 | idx2MBBMap.reserve(mf->size()); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 68 | |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 69 | indexList.push_back(createEntry(0, index)); |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 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 | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 77 | SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 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. |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 86 | indexList.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. |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 89 | mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(), |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 90 | SlotIndex::Slot_Block))); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Jakob Stoklund Olesen | f0cf2d3 | 2011-03-04 18:51:09 +0000 | [diff] [blame] | 93 | // We insert one blank instructions between basic blocks. |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 94 | indexList.push_back(createEntry(0, index += SlotIndex::InstrDist)); |
Lang Hames | 74ab5ee | 2009-12-22 00:11:50 +0000 | [diff] [blame] | 95 | |
Jakob Stoklund Olesen | a122eaa | 2011-04-02 06:03:31 +0000 | [diff] [blame] | 96 | MBBRanges[mbb->getNumber()].first = blockStartIndex; |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 97 | MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(), |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 98 | SlotIndex::Slot_Block); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 99 | idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); |
| 100 | } |
| 101 | |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 102 | // Sort the Idx2MBBMap |
| 103 | std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); |
| 104 | |
Jakob Stoklund Olesen | 1ce6a36 | 2012-01-24 23:28:38 +0000 | [diff] [blame] | 105 | DEBUG(mf->print(dbgs(), this)); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 106 | |
| 107 | // And we're done! |
| 108 | return false; |
| 109 | } |
| 110 | |
Lang Hames | b366158 | 2009-11-14 00:02:51 +0000 | [diff] [blame] | 111 | void SlotIndexes::renumberIndexes() { |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 112 | // Renumber updates the index of every element of the index list. |
Jakob Stoklund Olesen | 97af986 | 2011-02-03 20:29:41 +0000 | [diff] [blame] | 113 | DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n"); |
Jakob Stoklund Olesen | 979869c | 2011-03-04 19:43:38 +0000 | [diff] [blame] | 114 | ++NumGlobalRenum; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 115 | |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 116 | unsigned index = 0; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 117 | |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 118 | for (IndexList::iterator I = indexList.begin(), E = indexList.end(); |
| 119 | I != E; ++I) { |
| 120 | I->setIndex(index); |
Jakob Stoklund Olesen | f0cf2d3 | 2011-03-04 18:51:09 +0000 | [diff] [blame] | 121 | index += SlotIndex::InstrDist; |
Lang Hames | fbb8fa2 | 2009-11-05 22:20:57 +0000 | [diff] [blame] | 122 | } |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 125 | // Renumber indexes locally after curItr was inserted, but failed to get a new |
Jakob Stoklund Olesen | 979869c | 2011-03-04 19:43:38 +0000 | [diff] [blame] | 126 | // index. |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 127 | void SlotIndexes::renumberIndexes(IndexList::iterator curItr) { |
Jakob Stoklund Olesen | 979869c | 2011-03-04 19:43:38 +0000 | [diff] [blame] | 128 | // Number indexes with half the default spacing so we can catch up quickly. |
| 129 | const unsigned Space = SlotIndex::InstrDist/2; |
| 130 | assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM"); |
| 131 | |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 132 | IndexList::iterator startItr = prior(curItr); |
| 133 | unsigned index = startItr->getIndex(); |
Jakob Stoklund Olesen | 979869c | 2011-03-04 19:43:38 +0000 | [diff] [blame] | 134 | do { |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 135 | curItr->setIndex(index += Space); |
| 136 | ++curItr; |
Jakob Stoklund Olesen | 979869c | 2011-03-04 19:43:38 +0000 | [diff] [blame] | 137 | // If the next index is bigger, we have caught up. |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 138 | } while (curItr != indexList.end() && curItr->getIndex() <= index); |
Jakob Stoklund Olesen | 979869c | 2011-03-04 19:43:38 +0000 | [diff] [blame] | 139 | |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 140 | DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-' |
Jakob Stoklund Olesen | 979869c | 2011-03-04 19:43:38 +0000 | [diff] [blame] | 141 | << index << " ***\n"); |
| 142 | ++NumLocalRenum; |
| 143 | } |
| 144 | |
| 145 | |
Manman Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame^] | 146 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 147 | void SlotIndexes::dump() const { |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 148 | for (IndexList::const_iterator itr = indexList.begin(); |
| 149 | itr != indexList.end(); ++itr) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 150 | dbgs() << itr->getIndex() << " "; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 151 | |
| 152 | if (itr->getInstr() != 0) { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 153 | dbgs() << *itr->getInstr(); |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 154 | } else { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 155 | dbgs() << "\n"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Jakob Stoklund Olesen | a122eaa | 2011-04-02 06:03:31 +0000 | [diff] [blame] | 159 | for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i) |
| 160 | dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';' |
| 161 | << MBBRanges[i].second << ")\n"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 162 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 163 | #endif |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 164 | |
| 165 | // Print a SlotIndex to a raw_ostream. |
| 166 | void SlotIndex::print(raw_ostream &os) const { |
Jakob Stoklund Olesen | 97af986 | 2011-02-03 20:29:41 +0000 | [diff] [blame] | 167 | if (isValid()) |
Lang Hames | 613dfb2 | 2012-04-17 04:15:51 +0000 | [diff] [blame] | 168 | os << listEntry()->getIndex() << "Berd"[getSlot()]; |
Jakob Stoklund Olesen | 97af986 | 2011-02-03 20:29:41 +0000 | [diff] [blame] | 169 | else |
| 170 | os << "invalid"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Manman Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame^] | 173 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 174 | // Dump a SlotIndex to stderr. |
| 175 | void SlotIndex::dump() const { |
David Greene | 87b0efc | 2010-01-05 01:25:50 +0000 | [diff] [blame] | 176 | print(dbgs()); |
| 177 | dbgs() << "\n"; |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 178 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 179 | #endif |
Lang Hames | 233a60e | 2009-11-03 23:52:08 +0000 | [diff] [blame] | 180 | |