blob: 6110ef5d2f058b084dc9f368ac7799b5300db004 [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"
Lang Hames16dcaf52009-11-08 08:49:59 +000016#include "llvm/Support/ManagedStatic.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 Hamesd6ef7fa2009-11-07 05:50:28 +000021
22// Yep - these are thread safe. See the header for details.
Lang Hames16dcaf52009-11-08 08:49:59 +000023namespace {
24
25
26 class EmptyIndexListEntry : public IndexListEntry {
27 public:
28 EmptyIndexListEntry() : IndexListEntry(EMPTY_KEY) {}
29 };
30
31 class TombstoneIndexListEntry : public IndexListEntry {
32 public:
33 TombstoneIndexListEntry() : IndexListEntry(TOMBSTONE_KEY) {}
34 };
35
36 // The following statics are thread safe. They're read only, and you
37 // can't step from them to any other list entries.
38 ManagedStatic<EmptyIndexListEntry> IndexListEntryEmptyKey;
39 ManagedStatic<TombstoneIndexListEntry> IndexListEntryTombstoneKey;
40}
Lang Hames233a60e2009-11-03 23:52:08 +000041
42char SlotIndexes::ID = 0;
43static RegisterPass<SlotIndexes> X("slotindexes", "Slot index numbering");
44
Lang Hames16dcaf52009-11-08 08:49:59 +000045IndexListEntry* IndexListEntry::getEmptyKeyEntry() {
46 return &*IndexListEntryEmptyKey;
47}
48
49IndexListEntry* IndexListEntry::getTombstoneKeyEntry() {
50 return &*IndexListEntryTombstoneKey;
51}
52
53
Lang Hames233a60e2009-11-03 23:52:08 +000054void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
55 au.setPreservesAll();
56 MachineFunctionPass::getAnalysisUsage(au);
57}
58
59void SlotIndexes::releaseMemory() {
60 mi2iMap.clear();
61 mbb2IdxMap.clear();
62 idx2MBBMap.clear();
63 terminatorGaps.clear();
64 clearList();
65}
66
67bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
68
69 // Compute numbering as follows:
70 // Grab an iterator to the start of the index list.
71 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
72 // iterator in lock-step (though skipping it over indexes which have
73 // null pointers in the instruction field).
74 // At each iteration assert that the instruction pointed to in the index
75 // is the same one pointed to by the MI iterator. This
76
77 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
78 // only need to be set up once after the first numbering is computed.
79
80 mf = &fn;
81 initList();
82
Lang Hames233a60e2009-11-03 23:52:08 +000083 // Check that the list contains only the sentinal.
84 assert(indexListHead->getNext() == 0 &&
85 "Index list non-empty at initial numbering?");
86 assert(idx2MBBMap.empty() &&
87 "Index -> MBB mapping non-empty at initial numbering?");
88 assert(mbb2IdxMap.empty() &&
89 "MBB -> Index mapping non-empty at initial numbering?");
90 assert(mi2iMap.empty() &&
91 "MachineInstr -> Index mapping non-empty at initial numbering?");
92
93 functionSize = 0;
Lang Hames233a60e2009-11-03 23:52:08 +000094 unsigned index = 0;
95
Lang Hames74ab5ee2009-12-22 00:11:50 +000096 push_back(createEntry(0, index));
97
Dan Gohmanf451cb82010-02-10 16:03:48 +000098 // Iterate over the function.
Lang Hames233a60e2009-11-03 23:52:08 +000099 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
100 mbbItr != mbbEnd; ++mbbItr) {
101 MachineBasicBlock *mbb = &*mbbItr;
102
103 // Insert an index for the MBB start.
Lang Hames233a60e2009-11-03 23:52:08 +0000104 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
105
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000106 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000107
108 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
109 miItr != miEnd; ++miItr) {
Chris Lattner518bb532010-02-09 19:54:29 +0000110 MachineInstr *mi = miItr;
111 if (mi->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +0000112 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000113
114 if (miItr == mbb->getFirstTerminator()) {
115 push_back(createEntry(0, index));
116 terminatorGaps.insert(
117 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000118 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000119 }
120
121 // Insert a store index for the instr.
122 push_back(createEntry(mi, index));
123
124 // Save this base index in the maps.
125 mi2iMap.insert(
126 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
127
128 ++functionSize;
129
130 unsigned Slots = mi->getDesc().getNumDefs();
131 if (Slots == 0)
132 Slots = 1;
133
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000134 index += (Slots + 1) * SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000135 }
136
137 if (mbb->getFirstTerminator() == mbb->end()) {
138 push_back(createEntry(0, index));
139 terminatorGaps.insert(
140 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000141 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000142 }
143
Lang Hames74ab5ee2009-12-22 00:11:50 +0000144 // One blank instruction at the end.
145 push_back(createEntry(0, index));
146
147 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
Lang Hames233a60e2009-11-03 23:52:08 +0000148 mbb2IdxMap.insert(
149 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
150
151 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
152 }
153
Lang Hames233a60e2009-11-03 23:52:08 +0000154 // Sort the Idx2MBBMap
155 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
156
157 DEBUG(dump());
158
159 // And we're done!
160 return false;
161}
162
Lang Hamesb3661582009-11-14 00:02:51 +0000163void SlotIndexes::renumberIndexes() {
Lang Hames233a60e2009-11-03 23:52:08 +0000164
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000165 // Renumber updates the index of every element of the index list.
166 // If all instrs in the function have been allocated an index (which has been
167 // placed in the index list in the order of instruction iteration) then the
168 // resulting numbering will match what would have been generated by the
169 // pass during the initial numbering of the function if the new instructions
170 // had been present.
Lang Hames233a60e2009-11-03 23:52:08 +0000171
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000172 functionSize = 0;
173 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000174
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000175 for (IndexListEntry *curEntry = front(); curEntry != getTail();
176 curEntry = curEntry->getNext()) {
177
178 curEntry->setIndex(index);
179
180 if (curEntry->getInstr() == 0) {
181 // MBB start entry or terminator gap. Just step index by 1.
182 index += SlotIndex::NUM;
183 }
184 else {
185 ++functionSize;
186 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
187 if (Slots == 0)
188 Slots = 1;
189
190 index += (Slots + 1) * SlotIndex::NUM;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000191 }
192 }
Lang Hames233a60e2009-11-03 23:52:08 +0000193}
194
195void SlotIndexes::dump() const {
196 for (const IndexListEntry *itr = front(); itr != getTail();
197 itr = itr->getNext()) {
David Greene87b0efc2010-01-05 01:25:50 +0000198 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000199
200 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000201 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000202 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000203 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000204 }
205 }
206
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000207 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000208 itr != mbb2IdxMap.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000209 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
Lang Hames233a60e2009-11-03 23:52:08 +0000210 << itr->second.first << ", " << itr->second.second << "]\n";
211 }
212}
213
214// Print a SlotIndex to a raw_ostream.
215void SlotIndex::print(raw_ostream &os) const {
216 os << getIndex();
217 if (isPHI())
218 os << "*";
219}
220
221// Dump a SlotIndex to stderr.
222void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000223 print(dbgs());
224 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000225}
226