1 //===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "slotindexes"
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 #include "llvm/Target/TargetInstrInfo.h"
20 char SlotIndexes::ID = 0;
21 INITIALIZE_PASS(SlotIndexes, "slotindexes",
22 "Slot index numbering", false, false)
24 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
26 MachineFunctionPass::getAnalysisUsage(au);
29 void SlotIndexes::releaseMemory() {
36 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
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
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.
52 // 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?");
65 push_back(createEntry(0, index));
67 // Iterate over the function.
68 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
69 mbbItr != mbbEnd; ++mbbItr) {
70 MachineBasicBlock *mbb = &*mbbItr;
72 // Insert an index for the MBB start.
73 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
75 index += SlotIndex::NUM;
77 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
78 miItr != miEnd; ++miItr) {
79 MachineInstr *mi = miItr;
80 if (mi->isDebugValue())
83 // Insert a store index for the instr.
84 push_back(createEntry(mi, index));
86 // Save this base index in the maps.
88 std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
92 unsigned Slots = mi->getDesc().getNumDefs();
96 index += (Slots + 1) * SlotIndex::NUM;
99 // 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;
104 push_back(createEntry(0, index));
106 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
108 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
110 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
113 // Sort the Idx2MBBMap
114 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
122 void SlotIndexes::renumberIndexes() {
124 // 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
130 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
134 for (IndexListEntry *curEntry = front(); curEntry != getTail();
135 curEntry = curEntry->getNext()) {
136 curEntry->setIndex(index);
137 index += 4*SlotIndex::NUM;
141 void SlotIndexes::dump() const {
142 for (const IndexListEntry *itr = front(); itr != getTail();
143 itr = itr->getNext()) {
144 dbgs() << itr->getIndex() << " ";
146 if (itr->getInstr() != 0) {
147 dbgs() << *itr->getInstr();
153 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
154 itr != mbb2IdxMap.end(); ++itr) {
155 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
156 << itr->second.first << ", " << itr->second.second << "]\n";
160 // Print a SlotIndex to a raw_ostream.
161 void SlotIndex::print(raw_ostream &os) const {
163 os << entry().getIndex() << "LudS"[getSlot()];
168 // Dump a SlotIndex to stderr.
169 void SlotIndex::dump() const {