blob: 1bc148f160bc86a3585fcd1d0a33edf6ff3adbca [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",
44 "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();
64 terminatorGaps.clear();
65 clearList();
66}
67
68bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
69
70 // Compute numbering as follows:
71 // Grab an iterator to the start of the index list.
72 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
73 // iterator in lock-step (though skipping it over indexes which have
74 // null pointers in the instruction field).
75 // At each iteration assert that the instruction pointed to in the index
76 // is the same one pointed to by the MI iterator. This
77
78 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
79 // only need to be set up once after the first numbering is computed.
80
81 mf = &fn;
82 initList();
83
Lang Hames233a60e2009-11-03 23:52:08 +000084 // Check that the list contains only the sentinal.
85 assert(indexListHead->getNext() == 0 &&
86 "Index list non-empty at initial numbering?");
87 assert(idx2MBBMap.empty() &&
88 "Index -> MBB mapping non-empty at initial numbering?");
89 assert(mbb2IdxMap.empty() &&
90 "MBB -> Index mapping non-empty at initial numbering?");
91 assert(mi2iMap.empty() &&
92 "MachineInstr -> Index mapping non-empty at initial numbering?");
93
94 functionSize = 0;
Lang Hames233a60e2009-11-03 23:52:08 +000095 unsigned index = 0;
96
Lang Hames74ab5ee2009-12-22 00:11:50 +000097 push_back(createEntry(0, index));
98
Dan Gohmanf451cb82010-02-10 16:03:48 +000099 // Iterate over the function.
Lang Hames233a60e2009-11-03 23:52:08 +0000100 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
101 mbbItr != mbbEnd; ++mbbItr) {
102 MachineBasicBlock *mbb = &*mbbItr;
103
104 // Insert an index for the MBB start.
Lang Hames233a60e2009-11-03 23:52:08 +0000105 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
106
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000107 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000108
109 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
110 miItr != miEnd; ++miItr) {
Chris Lattner518bb532010-02-09 19:54:29 +0000111 MachineInstr *mi = miItr;
112 if (mi->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +0000113 continue;
Lang Hames233a60e2009-11-03 23:52:08 +0000114
115 if (miItr == mbb->getFirstTerminator()) {
116 push_back(createEntry(0, index));
117 terminatorGaps.insert(
118 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000119 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000120 }
121
122 // Insert a store index for the instr.
123 push_back(createEntry(mi, index));
124
125 // Save this base index in the maps.
126 mi2iMap.insert(
127 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
128
129 ++functionSize;
130
131 unsigned Slots = mi->getDesc().getNumDefs();
132 if (Slots == 0)
133 Slots = 1;
134
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000135 index += (Slots + 1) * SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000136 }
137
138 if (mbb->getFirstTerminator() == mbb->end()) {
139 push_back(createEntry(0, index));
140 terminatorGaps.insert(
141 std::make_pair(mbb, SlotIndex(back(), SlotIndex::PHI_BIT)));
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000142 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +0000143 }
144
Lang Hames74ab5ee2009-12-22 00:11:50 +0000145 // One blank instruction at the end.
146 push_back(createEntry(0, index));
147
148 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
Lang Hames233a60e2009-11-03 23:52:08 +0000149 mbb2IdxMap.insert(
150 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
151
152 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
153 }
154
Lang Hames233a60e2009-11-03 23:52:08 +0000155 // Sort the Idx2MBBMap
156 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
157
158 DEBUG(dump());
159
160 // And we're done!
161 return false;
162}
163
Lang Hamesb3661582009-11-14 00:02:51 +0000164void SlotIndexes::renumberIndexes() {
Lang Hames233a60e2009-11-03 23:52:08 +0000165
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000166 // Renumber updates the index of every element of the index list.
167 // If all instrs in the function have been allocated an index (which has been
168 // placed in the index list in the order of instruction iteration) then the
169 // resulting numbering will match what would have been generated by the
170 // pass during the initial numbering of the function if the new instructions
171 // had been present.
Lang Hames233a60e2009-11-03 23:52:08 +0000172
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000173 functionSize = 0;
174 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000175
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000176 for (IndexListEntry *curEntry = front(); curEntry != getTail();
177 curEntry = curEntry->getNext()) {
178
179 curEntry->setIndex(index);
180
181 if (curEntry->getInstr() == 0) {
182 // MBB start entry or terminator gap. Just step index by 1.
183 index += SlotIndex::NUM;
184 }
185 else {
186 ++functionSize;
187 unsigned Slots = curEntry->getInstr()->getDesc().getNumDefs();
188 if (Slots == 0)
189 Slots = 1;
190
191 index += (Slots + 1) * SlotIndex::NUM;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000192 }
193 }
Lang Hames233a60e2009-11-03 23:52:08 +0000194}
195
196void SlotIndexes::dump() const {
197 for (const IndexListEntry *itr = front(); itr != getTail();
198 itr = itr->getNext()) {
David Greene87b0efc2010-01-05 01:25:50 +0000199 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000200
201 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000202 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000203 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000204 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000205 }
206 }
207
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000208 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000209 itr != mbb2IdxMap.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000210 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
Lang Hames233a60e2009-11-03 23:52:08 +0000211 << itr->second.first << ", " << itr->second.second << "]\n";
212 }
213}
214
215// Print a SlotIndex to a raw_ostream.
216void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Oleseneec418d2010-06-24 17:31:07 +0000217 os << entry().getIndex();
Lang Hames233a60e2009-11-03 23:52:08 +0000218 if (isPHI())
219 os << "*";
Jakob Stoklund Oleseneec418d2010-06-24 17:31:07 +0000220 else
221 os << "LudS"[getSlot()];
Lang Hames233a60e2009-11-03 23:52:08 +0000222}
223
224// Dump a SlotIndex to stderr.
225void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000226 print(dbgs());
227 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000228}
229