blob: 26cf25944162fff6be9de06833852b489405125d [file] [log] [blame]
Lang Hames233a60e2009-11-03 23:52:08 +00001//===-- 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 Olesen10c5f2d2011-03-04 18:08:29 +000013#include "llvm/ADT/Statistic.h"
Lang Hames233a60e2009-11-03 23:52:08 +000014#include "llvm/CodeGen/MachineFunction.h"
15#include "llvm/Support/Debug.h"
16#include "llvm/Support/raw_ostream.h"
Dale Johannesen1caedd02010-01-22 22:38:21 +000017#include "llvm/Target/TargetInstrInfo.h"
Lang Hames233a60e2009-11-03 23:52:08 +000018
19using namespace llvm;
20
Lang Hames233a60e2009-11-03 23:52:08 +000021char SlotIndexes::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000022INITIALIZE_PASS(SlotIndexes, "slotindexes",
Owen Andersonce665bd2010-10-07 22:25:06 +000023 "Slot index numbering", false, false)
Lang Hames233a60e2009-11-03 23:52:08 +000024
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +000025STATISTIC(NumLocalRenum, "Number of local renumberings");
26STATISTIC(NumGlobalRenum, "Number of global renumberings");
Jakob Stoklund Olesen10c5f2d2011-03-04 18:08:29 +000027
Lang Hames233a60e2009-11-03 23:52:08 +000028void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
29 au.setPreservesAll();
30 MachineFunctionPass::getAnalysisUsage(au);
31}
32
33void SlotIndexes::releaseMemory() {
34 mi2iMap.clear();
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000035 MBBRanges.clear();
Lang Hames233a60e2009-11-03 23:52:08 +000036 idx2MBBMap.clear();
Lang Hames613dfb22012-04-17 04:15:51 +000037 indexList.clear();
38 ileAllocator.Reset();
Lang Hames233a60e2009-11-03 23:52:08 +000039}
40
41bool 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 Hames613dfb22012-04-17 04:15:51 +000049 // is the same one pointed to by the MI iterator. This
Lang Hames233a60e2009-11-03 23:52:08 +000050
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 Hames233a60e2009-11-03 23:52:08 +000055
Lang Hames233a60e2009-11-03 23:52:08 +000056 // Check that the list contains only the sentinal.
Lang Hames613dfb22012-04-17 04:15:51 +000057 assert(indexList.empty() && "Index list non-empty at initial numbering?");
Lang Hames233a60e2009-11-03 23:52:08 +000058 assert(idx2MBBMap.empty() &&
59 "Index -> MBB mapping non-empty at initial numbering?");
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000060 assert(MBBRanges.empty() &&
Lang Hames233a60e2009-11-03 23:52:08 +000061 "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 Hames233a60e2009-11-03 23:52:08 +000066 unsigned index = 0;
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000067 MBBRanges.resize(mf->getNumBlockIDs());
68 idx2MBBMap.reserve(mf->size());
Lang Hames233a60e2009-11-03 23:52:08 +000069
Lang Hames613dfb22012-04-17 04:15:51 +000070 indexList.push_back(createEntry(0, index));
Lang Hames74ab5ee2009-12-22 00:11:50 +000071
Dan Gohmanf451cb82010-02-10 16:03:48 +000072 // Iterate over the function.
Lang Hames233a60e2009-11-03 23:52:08 +000073 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
74 mbbItr != mbbEnd; ++mbbItr) {
75 MachineBasicBlock *mbb = &*mbbItr;
76
77 // Insert an index for the MBB start.
Lang Hames613dfb22012-04-17 04:15:51 +000078 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
Lang Hames233a60e2009-11-03 23:52:08 +000079
Lang Hames233a60e2009-11-03 23:52:08 +000080 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
81 miItr != miEnd; ++miItr) {
Chris Lattner518bb532010-02-09 19:54:29 +000082 MachineInstr *mi = miItr;
83 if (mi->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +000084 continue;
Lang Hames233a60e2009-11-03 23:52:08 +000085
Lang Hames233a60e2009-11-03 23:52:08 +000086 // Insert a store index for the instr.
Lang Hames613dfb22012-04-17 04:15:51 +000087 indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist));
Lang Hames233a60e2009-11-03 23:52:08 +000088
89 // Save this base index in the maps.
Lang Hames613dfb22012-04-17 04:15:51 +000090 mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(),
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +000091 SlotIndex::Slot_Block)));
Lang Hames613dfb22012-04-17 04:15:51 +000092
Lang Hames233a60e2009-11-03 23:52:08 +000093 ++functionSize;
Lang Hames233a60e2009-11-03 23:52:08 +000094 }
95
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +000096 // We insert one blank instructions between basic blocks.
Lang Hames613dfb22012-04-17 04:15:51 +000097 indexList.push_back(createEntry(0, index += SlotIndex::InstrDist));
Lang Hames74ab5ee2009-12-22 00:11:50 +000098
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000099 MBBRanges[mbb->getNumber()].first = blockStartIndex;
Lang Hames613dfb22012-04-17 04:15:51 +0000100 MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(),
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +0000101 SlotIndex::Slot_Block);
Lang Hames233a60e2009-11-03 23:52:08 +0000102 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
103 }
104
Lang Hames233a60e2009-11-03 23:52:08 +0000105 // Sort the Idx2MBBMap
106 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
107
Jakob Stoklund Olesen1ce6a362012-01-24 23:28:38 +0000108 DEBUG(mf->print(dbgs(), this));
Lang Hames233a60e2009-11-03 23:52:08 +0000109
110 // And we're done!
111 return false;
112}
113
Lang Hamesb3661582009-11-14 00:02:51 +0000114void SlotIndexes::renumberIndexes() {
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000115 // Renumber updates the index of every element of the index list.
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000116 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000117 ++NumGlobalRenum;
Lang Hames233a60e2009-11-03 23:52:08 +0000118
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000119 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000120
Lang Hames613dfb22012-04-17 04:15:51 +0000121 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
122 I != E; ++I) {
123 I->setIndex(index);
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +0000124 index += SlotIndex::InstrDist;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000125 }
Lang Hames233a60e2009-11-03 23:52:08 +0000126}
127
Lang Hames613dfb22012-04-17 04:15:51 +0000128// Renumber indexes locally after curItr was inserted, but failed to get a new
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000129// index.
Lang Hames613dfb22012-04-17 04:15:51 +0000130void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000131 // Number indexes with half the default spacing so we can catch up quickly.
132 const unsigned Space = SlotIndex::InstrDist/2;
133 assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM");
134
Lang Hames613dfb22012-04-17 04:15:51 +0000135 IndexList::iterator startItr = prior(curItr);
136 unsigned index = startItr->getIndex();
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000137 do {
Lang Hames613dfb22012-04-17 04:15:51 +0000138 curItr->setIndex(index += Space);
139 ++curItr;
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000140 // If the next index is bigger, we have caught up.
Lang Hames613dfb22012-04-17 04:15:51 +0000141 } while (curItr != indexList.end() && curItr->getIndex() <= index);
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000142
Lang Hames613dfb22012-04-17 04:15:51 +0000143 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000144 << index << " ***\n");
145 ++NumLocalRenum;
146}
147
148
Lang Hames233a60e2009-11-03 23:52:08 +0000149void SlotIndexes::dump() const {
Lang Hames613dfb22012-04-17 04:15:51 +0000150 for (IndexList::const_iterator itr = indexList.begin();
151 itr != indexList.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000152 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000153
154 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000155 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000156 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000157 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000158 }
159 }
160
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +0000161 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
162 dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
163 << MBBRanges[i].second << ")\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000164}
165
166// Print a SlotIndex to a raw_ostream.
167void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000168 if (isValid())
Lang Hames613dfb22012-04-17 04:15:51 +0000169 os << listEntry()->getIndex() << "Berd"[getSlot()];
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000170 else
171 os << "invalid";
Lang Hames233a60e2009-11-03 23:52:08 +0000172}
173
174// Dump a SlotIndex to stderr.
175void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000176 print(dbgs());
177 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000178}
179