blob: 28e3fb5c159d1d1c288891ab307af1bf8dcf2c59 [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 Olesen10c5f2d2011-03-04 18:08:29 +000025STATISTIC(NumRenumPasses, "Number of slot index renumber passes");
26
Lang Hames233a60e2009-11-03 23:52:08 +000027void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
28 au.setPreservesAll();
29 MachineFunctionPass::getAnalysisUsage(au);
30}
31
32void SlotIndexes::releaseMemory() {
33 mi2iMap.clear();
34 mbb2IdxMap.clear();
35 idx2MBBMap.clear();
Lang Hames233a60e2009-11-03 23:52:08 +000036 clearList();
37}
38
39bool 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 Hames233a60e2009-11-03 23:52:08 +000055 // 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 Hames233a60e2009-11-03 23:52:08 +000066 unsigned index = 0;
67
Lang Hames74ab5ee2009-12-22 00:11:50 +000068 push_back(createEntry(0, index));
69
Dan Gohmanf451cb82010-02-10 16:03:48 +000070 // Iterate over the function.
Lang Hames233a60e2009-11-03 23:52:08 +000071 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 Hames233a60e2009-11-03 23:52:08 +000076 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
77
Lang Hames233a60e2009-11-03 23:52:08 +000078 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
79 miItr != miEnd; ++miItr) {
Chris Lattner518bb532010-02-09 19:54:29 +000080 MachineInstr *mi = miItr;
81 if (mi->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +000082 continue;
Lang Hames233a60e2009-11-03 23:52:08 +000083
Lang Hames233a60e2009-11-03 23:52:08 +000084 // Insert a store index for the instr.
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +000085 push_back(createEntry(mi, index += SlotIndex::InstrDist));
Lang Hames233a60e2009-11-03 23:52:08 +000086
87 // Save this base index in the maps.
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +000088 mi2iMap.insert(std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
Lang Hames233a60e2009-11-03 23:52:08 +000089
90 ++functionSize;
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.
94 push_back(createEntry(0, index += SlotIndex::InstrDist));
Lang Hames74ab5ee2009-12-22 00:11:50 +000095
96 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
Lang Hames233a60e2009-11-03 23:52:08 +000097 mbb2IdxMap.insert(
98 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
99
100 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
101 }
102
Lang Hames233a60e2009-11-03 23:52:08 +0000103 // Sort the Idx2MBBMap
104 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
105
106 DEBUG(dump());
107
108 // And we're done!
109 return false;
110}
111
Lang Hamesb3661582009-11-14 00:02:51 +0000112void SlotIndexes::renumberIndexes() {
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000113 // Renumber updates the index of every element of the index list.
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000114 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Jakob Stoklund Olesen10c5f2d2011-03-04 18:08:29 +0000115 ++NumRenumPasses;
Lang Hames233a60e2009-11-03 23:52:08 +0000116
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000117 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000118
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000119 for (IndexListEntry *curEntry = front(); curEntry != getTail();
120 curEntry = curEntry->getNext()) {
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000121 curEntry->setIndex(index);
Jakob Stoklund Olesenf0cf2d32011-03-04 18:51:09 +0000122 index += SlotIndex::InstrDist;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000123 }
Lang Hames233a60e2009-11-03 23:52:08 +0000124}
125
126void SlotIndexes::dump() const {
127 for (const IndexListEntry *itr = front(); itr != getTail();
128 itr = itr->getNext()) {
David Greene87b0efc2010-01-05 01:25:50 +0000129 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000130
131 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000132 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000133 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000134 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000135 }
136 }
137
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000138 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000139 itr != mbb2IdxMap.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000140 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
Lang Hames233a60e2009-11-03 23:52:08 +0000141 << itr->second.first << ", " << itr->second.second << "]\n";
142 }
143}
144
145// Print a SlotIndex to a raw_ostream.
146void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000147 if (isValid())
148 os << entry().getIndex() << "LudS"[getSlot()];
149 else
150 os << "invalid";
Lang Hames233a60e2009-11-03 23:52:08 +0000151}
152
153// Dump a SlotIndex to stderr.
154void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000155 print(dbgs());
156 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000157}
158