blob: bf66367865d2c10d8d48ff89c9f87227bdf421f2 [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"
13#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/Support/Debug.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace llvm;
18
Lang Hames0368e502009-11-04 21:24:15 +000019std::auto_ptr<IndexListEntry> IndexListEntry::emptyKeyEntry,
20 IndexListEntry::tombstoneKeyEntry;
Lang Hames233a60e2009-11-03 23:52:08 +000021
22char SlotIndexes::ID = 0;
23static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering");
24
25void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
26 au.setPreservesAll();
27 MachineFunctionPass::getAnalysisUsage(au);
28}
29
30void SlotIndexes::releaseMemory() {
31 mi2iMap.clear();
32 mbb2IdxMap.clear();
33 idx2MBBMap.clear();
34 terminatorGaps.clear();
35 clearList();
36}
37
38bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
39
40 // Compute numbering as follows:
41 // Grab an iterator to the start of the index list.
42 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
43 // iterator in lock-step (though skipping it over indexes which have
44 // null pointers in the instruction field).
45 // At each iteration assert that the instruction pointed to in the index
46 // is the same one pointed to by the MI iterator. This
47
48 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
49 // only need to be set up once after the first numbering is computed.
50
51 mf = &fn;
52 initList();
53
Lang Hames233a60e2009-11-03 23:52:08 +000054 // Check that the list contains only the sentinal.
55 assert(indexListHead->getNext() == 0 &&
56 "Index list non-empty at initial numbering?");
57 assert(idx2MBBMap.empty() &&
58 "Index -> MBB mapping non-empty at initial numbering?");
59 assert(mbb2IdxMap.empty() &&
60 "MBB -> Index mapping non-empty at initial numbering?");
61 assert(mi2iMap.empty() &&
62 "MachineInstr -> Index mapping non-empty at initial numbering?");
63
64 functionSize = 0;
Lang Hames233a60e2009-11-03 23:52:08 +000065 unsigned index = 0;
66
67 // Iterate over the the function.
68 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
69 mbbItr != mbbEnd; ++mbbItr) {
70 MachineBasicBlock *mbb = &*mbbItr;
71
72 // Insert an index for the MBB start.
73 push_back(createEntry(0, index));
74 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
75
Lang Hamesfbb8fa22009-11-05 22:20:57 +000076 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +000077
78 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
79 miItr != miEnd; ++miItr) {
80 MachineInstr *mi = &*miItr;
81
82 if (miItr == mbb->getFirstTerminator()) {
83 push_back(createEntry(0, index));
84 terminatorGaps.insert(
85 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
Lang Hamesfbb8fa22009-11-05 22:20:57 +000086 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +000087 }
88
89 // Insert a store index for the instr.
90 push_back(createEntry(mi, index));
91
92 // Save this base index in the maps.
93 mi2iMap.insert(
94 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
95
96 ++functionSize;
97
98 unsigned Slots = mi->getDesc().getNumDefs();
99 if (Slots == 0)
100 Slots = 1;
101
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000102 index += (Slots + 1) * SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000103 }
104
105 if (mbb->getFirstTerminator() == mbb->end()) {
106 push_back(createEntry(0, index));
107 terminatorGaps.insert(
108 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000109 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000110 }
111
112 SlotIndex blockEndIndex(back(), SlotIndex::STORE);
113 mbb2IdxMap.insert(
114 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
115
116 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
117 }
118
119 // One blank instruction at the end.
120 push_back(createEntry(0, index));
121
122 // Sort the Idx2MBBMap
123 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
124
125 DEBUG(dump());
126
127 // And we're done!
128 return false;
129}
130
131void SlotIndexes::renumber() {
Lang Hames233a60e2009-11-03 23:52:08 +0000132
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000133 // Renumber updates the index of every element of the index list.
134 // If all instrs in the function have been allocated an index (which has been
135 // placed in the index list in the order of instruction iteration) then the
136 // resulting numbering will match what would have been generated by the
137 // pass during the initial numbering of the function if the new instructions
138 // had been present.
Lang Hames233a60e2009-11-03 23:52:08 +0000139
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000140 functionSize = 0;
141 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000142
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000143 for (IndexListEntry *curEntry = front(); curEntry != getTail();
144 curEntry = curEntry->getNext()) {
145
146 curEntry->setIndex(index);
147
148 if (curEntry->getInstr() == 0) {
149 // MBB start entry or terminator gap. Just step index by 1.
150 index += SlotIndex::NUM;
151 }
152 else {
153 ++functionSize;
154 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
155 if (Slots == 0)
156 Slots = 1;
157
158 index += (Slots + 1) * SlotIndex::NUM;
159
160 }
161 }
Lang Hames233a60e2009-11-03 23:52:08 +0000162}
163
164void SlotIndexes::dump() const {
165 for (const IndexListEntry *itr = front(); itr != getTail();
166 itr = itr->getNext()) {
167 errs() << itr->getIndex() << " ";
168
169 if (itr->getInstr() != 0) {
170 errs() << *itr->getInstr();
171 } else {
172 errs() << "\n";
173 }
174 }
175
176 for (MBB2IdxMap::iterator itr = mbb2IdxMap.begin();
177 itr != mbb2IdxMap.end(); ++itr) {
178 errs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
179 << itr->second.first << ", " << itr->second.second << "]\n";
180 }
181}
182
183// Print a SlotIndex to a raw_ostream.
184void SlotIndex::print(raw_ostream &os) const {
185 os << getIndex();
186 if (isPHI())
187 os << "*";
188}
189
190// Dump a SlotIndex to stderr.
191void SlotIndex::dump() const {
192 print(errs());
193 errs() << "\n";
194}
195