blob: c8c3fb37ad7934c12a6158af519009ebe0ee6f0b [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
Lang Hames233a60e2009-11-03 23:52:08 +000065 unsigned index = 0;
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000066 MBBRanges.resize(mf->getNumBlockIDs());
67 idx2MBBMap.reserve(mf->size());
Lang Hames233a60e2009-11-03 23:52:08 +000068
Lang Hames613dfb22012-04-17 04:15:51 +000069 indexList.push_back(createEntry(0, index));
Lang Hames74ab5ee2009-12-22 00:11:50 +000070
Dan Gohmanf451cb82010-02-10 16:03:48 +000071 // Iterate over the function.
Lang Hames233a60e2009-11-03 23:52:08 +000072 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 Hames613dfb22012-04-17 04:15:51 +000077 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
Lang Hames233a60e2009-11-03 23:52:08 +000078
Lang Hames233a60e2009-11-03 23:52:08 +000079 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
80 miItr != miEnd; ++miItr) {
Chris Lattner518bb532010-02-09 19:54:29 +000081 MachineInstr *mi = miItr;
82 if (mi->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +000083 continue;
Lang Hames233a60e2009-11-03 23:52:08 +000084
Lang Hames233a60e2009-11-03 23:52:08 +000085 // Insert a store index for the instr.
Lang Hames613dfb22012-04-17 04:15:51 +000086 indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist));
Lang Hames233a60e2009-11-03 23:52:08 +000087
88 // Save this base index in the maps.
Lang Hames613dfb22012-04-17 04:15:51 +000089 mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(),
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +000090 SlotIndex::Slot_Block)));
Lang Hames233a60e2009-11-03 23:52:08 +000091 }
92
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +000093 // We insert one blank instructions between basic blocks.
Lang Hames613dfb22012-04-17 04:15:51 +000094 indexList.push_back(createEntry(0, index += SlotIndex::InstrDist));
Lang Hames74ab5ee2009-12-22 00:11:50 +000095
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +000096 MBBRanges[mbb->getNumber()].first = blockStartIndex;
Lang Hames613dfb22012-04-17 04:15:51 +000097 MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(),
Jakob Stoklund Olesen2debd482011-11-13 20:45:27 +000098 SlotIndex::Slot_Block);
Lang Hames233a60e2009-11-03 23:52:08 +000099 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
100 }
101
Lang Hames233a60e2009-11-03 23:52:08 +0000102 // Sort the Idx2MBBMap
103 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
104
Jakob Stoklund Olesen1ce6a362012-01-24 23:28:38 +0000105 DEBUG(mf->print(dbgs(), this));
Lang Hames233a60e2009-11-03 23:52:08 +0000106
107 // And we're done!
108 return false;
109}
110
Lang Hamesb3661582009-11-14 00:02:51 +0000111void SlotIndexes::renumberIndexes() {
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000112 // Renumber updates the index of every element of the index list.
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000113 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000114 ++NumGlobalRenum;
Lang Hames233a60e2009-11-03 23:52:08 +0000115
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000116 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000117
Lang Hames613dfb22012-04-17 04:15:51 +0000118 for (IndexList::iterator I = indexList.begin(), E = indexList.end();
119 I != E; ++I) {
120 I->setIndex(index);
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +0000121 index += SlotIndex::InstrDist;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000122 }
Lang Hames233a60e2009-11-03 23:52:08 +0000123}
124
Lang Hames613dfb22012-04-17 04:15:51 +0000125// Renumber indexes locally after curItr was inserted, but failed to get a new
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000126// index.
Lang Hames613dfb22012-04-17 04:15:51 +0000127void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000128 // 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 Hames613dfb22012-04-17 04:15:51 +0000132 IndexList::iterator startItr = prior(curItr);
133 unsigned index = startItr->getIndex();
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000134 do {
Lang Hames613dfb22012-04-17 04:15:51 +0000135 curItr->setIndex(index += Space);
136 ++curItr;
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000137 // If the next index is bigger, we have caught up.
Lang Hames613dfb22012-04-17 04:15:51 +0000138 } while (curItr != indexList.end() && curItr->getIndex() <= index);
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000139
Lang Hames613dfb22012-04-17 04:15:51 +0000140 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
Jakob Stoklund Olesen979869c2011-03-04 19:43:38 +0000141 << index << " ***\n");
142 ++NumLocalRenum;
143}
144
145
Lang Hames233a60e2009-11-03 23:52:08 +0000146void SlotIndexes::dump() const {
Lang Hames613dfb22012-04-17 04:15:51 +0000147 for (IndexList::const_iterator itr = indexList.begin();
148 itr != indexList.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000149 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000150
151 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000152 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000153 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000154 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000155 }
156 }
157
Jakob Stoklund Olesena122eaa2011-04-02 06:03:31 +0000158 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
159 dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
160 << MBBRanges[i].second << ")\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000161}
162
163// Print a SlotIndex to a raw_ostream.
164void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000165 if (isValid())
Lang Hames613dfb22012-04-17 04:15:51 +0000166 os << listEntry()->getIndex() << "Berd"[getSlot()];
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000167 else
168 os << "invalid";
Lang Hames233a60e2009-11-03 23:52:08 +0000169}
170
171// Dump a SlotIndex to stderr.
172void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000173 print(dbgs());
174 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000175}
176