blob: 73adb92101d7447e5283e1c6f40cbfc0d5b5f939 [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"
Dale Johannesen1caedd02010-01-22 22:38:21 +000016#include "llvm/Target/TargetInstrInfo.h"
Lang Hames233a60e2009-11-03 23:52:08 +000017
18using namespace llvm;
19
Lang Hames233a60e2009-11-03 23:52:08 +000020char SlotIndexes::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000021INITIALIZE_PASS(SlotIndexes, "slotindexes",
Owen Andersonce665bd2010-10-07 22:25:06 +000022 "Slot index numbering", false, false)
Lang Hames233a60e2009-11-03 23:52:08 +000023
24void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
25 au.setPreservesAll();
26 MachineFunctionPass::getAnalysisUsage(au);
27}
28
29void SlotIndexes::releaseMemory() {
30 mi2iMap.clear();
31 mbb2IdxMap.clear();
32 idx2MBBMap.clear();
Lang Hames233a60e2009-11-03 23:52:08 +000033 clearList();
34}
35
36bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
37
38 // Compute numbering as follows:
39 // Grab an iterator to the start of the index list.
40 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
41 // iterator in lock-step (though skipping it over indexes which have
42 // null pointers in the instruction field).
43 // At each iteration assert that the instruction pointed to in the index
44 // is the same one pointed to by the MI iterator. This
45
46 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
47 // only need to be set up once after the first numbering is computed.
48
49 mf = &fn;
50 initList();
51
Lang Hames233a60e2009-11-03 23:52:08 +000052 // Check that the list contains only the sentinal.
53 assert(indexListHead->getNext() == 0 &&
54 "Index list non-empty at initial numbering?");
55 assert(idx2MBBMap.empty() &&
56 "Index -> MBB mapping non-empty at initial numbering?");
57 assert(mbb2IdxMap.empty() &&
58 "MBB -> Index mapping non-empty at initial numbering?");
59 assert(mi2iMap.empty() &&
60 "MachineInstr -> Index mapping non-empty at initial numbering?");
61
62 functionSize = 0;
Lang Hames233a60e2009-11-03 23:52:08 +000063 unsigned index = 0;
64
Lang Hames74ab5ee2009-12-22 00:11:50 +000065 push_back(createEntry(0, index));
66
Dan Gohmanf451cb82010-02-10 16:03:48 +000067 // Iterate over the function.
Lang Hames233a60e2009-11-03 23:52:08 +000068 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.
Lang Hames233a60e2009-11-03 23:52:08 +000073 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
74
Lang Hamesfbb8fa22009-11-05 22:20:57 +000075 index += SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +000076
77 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
78 miItr != miEnd; ++miItr) {
Chris Lattner518bb532010-02-09 19:54:29 +000079 MachineInstr *mi = miItr;
80 if (mi->isDebugValue())
Dale Johannesen1caedd02010-01-22 22:38:21 +000081 continue;
Lang Hames233a60e2009-11-03 23:52:08 +000082
Lang Hames233a60e2009-11-03 23:52:08 +000083 // Insert a store index for the instr.
84 push_back(createEntry(mi, index));
85
86 // Save this base index in the maps.
87 mi2iMap.insert(
88 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
89
90 ++functionSize;
91
92 unsigned Slots = mi->getDesc().getNumDefs();
93 if (Slots == 0)
94 Slots = 1;
95
Lang Hamesfbb8fa22009-11-05 22:20:57 +000096 index += (Slots + 1) * SlotIndex::NUM;
Lang Hames233a60e2009-11-03 23:52:08 +000097 }
98
Jakob Stoklund Olesen1e8e72d2010-11-11 00:19:20 +000099 // We insert two blank instructions between basic blocks.
100 // One to represent live-out registers and one to represent live-ins.
101 push_back(createEntry(0, index));
102 index += SlotIndex::NUM;
103
104 push_back(createEntry(0, index));
Lang Hames74ab5ee2009-12-22 00:11:50 +0000105
106 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
Lang Hames233a60e2009-11-03 23:52:08 +0000107 mbb2IdxMap.insert(
108 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
109
110 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
111 }
112
Lang Hames233a60e2009-11-03 23:52:08 +0000113 // Sort the Idx2MBBMap
114 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
115
116 DEBUG(dump());
117
118 // And we're done!
119 return false;
120}
121
Lang Hamesb3661582009-11-14 00:02:51 +0000122void SlotIndexes::renumberIndexes() {
Lang Hames233a60e2009-11-03 23:52:08 +0000123
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000124 // Renumber updates the index of every element of the index list.
125 // If all instrs in the function have been allocated an index (which has been
126 // placed in the index list in the order of instruction iteration) then the
127 // resulting numbering will match what would have been generated by the
128 // pass during the initial numbering of the function if the new instructions
129 // had been present.
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000130 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
Lang Hames233a60e2009-11-03 23:52:08 +0000131
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000132 unsigned index = 0;
Lang Hames233a60e2009-11-03 23:52:08 +0000133
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000134 for (IndexListEntry *curEntry = front(); curEntry != getTail();
135 curEntry = curEntry->getNext()) {
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000136 curEntry->setIndex(index);
Jakob Stoklund Olesen2c11eb32011-03-03 06:29:01 +0000137 index += 4*SlotIndex::NUM;
Lang Hamesfbb8fa22009-11-05 22:20:57 +0000138 }
Lang Hames233a60e2009-11-03 23:52:08 +0000139}
140
141void SlotIndexes::dump() const {
142 for (const IndexListEntry *itr = front(); itr != getTail();
143 itr = itr->getNext()) {
David Greene87b0efc2010-01-05 01:25:50 +0000144 dbgs() << itr->getIndex() << " ";
Lang Hames233a60e2009-11-03 23:52:08 +0000145
146 if (itr->getInstr() != 0) {
David Greene87b0efc2010-01-05 01:25:50 +0000147 dbgs() << *itr->getInstr();
Lang Hames233a60e2009-11-03 23:52:08 +0000148 } else {
David Greene87b0efc2010-01-05 01:25:50 +0000149 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000150 }
151 }
152
Jeffrey Yasskin81cf4322009-11-10 01:02:17 +0000153 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
Lang Hames233a60e2009-11-03 23:52:08 +0000154 itr != mbb2IdxMap.end(); ++itr) {
David Greene87b0efc2010-01-05 01:25:50 +0000155 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
Lang Hames233a60e2009-11-03 23:52:08 +0000156 << itr->second.first << ", " << itr->second.second << "]\n";
157 }
158}
159
160// Print a SlotIndex to a raw_ostream.
161void SlotIndex::print(raw_ostream &os) const {
Jakob Stoklund Olesen97af9862011-02-03 20:29:41 +0000162 if (isValid())
163 os << entry().getIndex() << "LudS"[getSlot()];
164 else
165 os << "invalid";
Lang Hames233a60e2009-11-03 23:52:08 +0000166}
167
168// Dump a SlotIndex to stderr.
169void SlotIndex::dump() const {
David Greene87b0efc2010-01-05 01:25:50 +0000170 print(dbgs());
171 dbgs() << "\n";
Lang Hames233a60e2009-11-03 23:52:08 +0000172}
173