blob: 6e3fa90e4341eb0caafda31a358f93f1412bd597 [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;
Owen Andersond13db2c2010-07-21 22:09:45 +000043INITIALIZE_PASS(SlotIndexes, "slotindexes",
Owen Andersonce665bd2010-10-07 22:25:06 +000044 "Slot index numbering", false, false)
Lang Hames233a60e2009-11-03 23:52:08 +000045
Lang Hames16dcaf52009-11-08 08:49:59 +000046IndexListEntry* IndexListEntry::getEmptyKeyEntry() {
47 return &*IndexListEntryEmptyKey;
48}
49
50IndexListEntry* IndexListEntry::getTombstoneKeyEntry() {
51 return &*IndexListEntryTombstoneKey;
52}
53
54
Lang Hames233a60e2009-11-03 23:52:08 +000055void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
56 au.setPreservesAll();
57 MachineFunctionPass::getAnalysisUsage(au);
58}
59
60void SlotIndexes::releaseMemory() {
61 mi2iMap.clear();
62 mbb2IdxMap.clear();
63 idx2MBBMap.clear();
Lang Hames233a60e2009-11-03 23:52:08 +000064 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
Lang Hames233a60e2009-11-03 23:52:08 +0000114 // Insert a store index for the instr.
115 push_back(createEntry(mi, index));
116
117 // Save this base index in the maps.
118 mi2iMap.insert(
119 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
120
121 ++functionSize;
122
123 unsigned Slots = mi->getDesc().getNumDefs();
124 if (Slots == 0)
125 Slots = 1;
126
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000127 index += (Slots + 1) * SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000128 }
129
Jakob Stoklund Olesen1e8e72d2010-11-11 00:19:20 +0000130 // We insert two blank instructions between basic blocks.
131 // One to represent live-out registers and one to represent live-ins.
132 push_back(createEntry(0, index));
133 index += SlotIndex::NUM;
134
135 push_back(createEntry(0, index));
Lang Hames74ab5ee2009-12-22 00:11:50 +0000136
137 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
Lang Hames233a60e2009-11-03 23:52:08 +0000138 mbb2IdxMap.insert(
139 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
140
141 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
142 }
143
Lang Hames233a60e2009-11-03 23:52:08 +0000144 // Sort the Idx2MBBMap
145 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
146
147 DEBUG(dump());
148
149 // And we're done!
150 return false;
151}
152
Lang Hamesb3661582009-11-14 00:02:51 +0000153void SlotIndexes::renumberIndexes() {
Lang Hames233a60e2009-11-03 23:52:08 +0000154
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000155 // Renumber updates the index of every element of the index list.
156 // If all instrs in the function have been allocated an index (which has been
157 // placed in the index list in the order of instruction iteration) then the
158 // resulting numbering will match what would have been generated by the
159 // pass during the initial numbering of the function if the new instructions
160 // had been present.
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000161 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Lang Hames233a60e2009-11-03 23:52:08 +0000162
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000163 functionSize = 0;
164 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000165
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000166 for (IndexListEntry *curEntry = front(); curEntry != getTail();
167 curEntry = curEntry->getNext()) {
168
169 curEntry->setIndex(index);
170
171 if (curEntry->getInstr() == 0) {
Jakob Stoklund Olesen1803b372010-09-24 23:58:56 +0000172 // MBB start entry. Just step index by 1.
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000173 index += SlotIndex::NUM;
174 }
175 else {
176 ++functionSize;
177 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
178 if (Slots == 0)
179 Slots = 1;
180
181 index += (Slots + 1) * SlotIndex::NUM;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000182 }
183 }
Lang Hames233a60e2009-11-03 23:52:08 +0000184}
185
186void SlotIndexes::dump() const {
187 for (const IndexListEntry *itr = front(); itr != getTail();
188 itr = itr->getNext()) {
David Greene87b0efc2010-01-05 01:25:50 +0000189 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000190
191 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000192 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000193 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000194 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000195 }
196 }
197
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000198 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000199 itr != mbb2IdxMap.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000200 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
Lang Hames233a60e2009-11-03 23:52:08 +0000201 << itr->second.first << ", " << itr->second.second << "]\n";
202 }
203}
204
205// Print a SlotIndex to a raw_ostream.
206void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000207 if (isValid())
208 os << entry().getIndex() << "LudS"[getSlot()];
209 else
210 os << "invalid";
Lang Hames233a60e2009-11-03 23:52:08 +0000211}
212
213// Dump a SlotIndex to stderr.
214void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000215 print(dbgs());
216 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000217}
218