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/ADT/Statistic.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/Target/TargetInstrInfo.h"
21 char SlotIndexes::ID = 0;
22 INITIALIZE_PASS(SlotIndexes, "slotindexes",
23 "Slot index numbering", false, false)
25 STATISTIC(NumRenumPasses, "Number of slot index renumber passes");
27 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
29 MachineFunctionPass::getAnalysisUsage(au);
32 void SlotIndexes::releaseMemory() {
39 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
41 // Compute numbering as follows:
42 // Grab an iterator to the start of the index list.
43 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
44 // iterator in lock-step (though skipping it over indexes which have
45 // null pointers in the instruction field).
46 // At each iteration assert that the instruction pointed to in the index
47 // is the same one pointed to by the MI iterator. This
49 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
50 // only need to be set up once after the first numbering is computed.
55 // Check that the list contains only the sentinal.
56 assert(indexListHead->getNext() == 0 &&
57 "Index list non-empty at initial numbering?");
58 assert(idx2MBBMap.empty() &&
59 "Index -> MBB mapping non-empty at initial numbering?");
60 assert(mbb2IdxMap.empty() &&
61 "MBB -> Index mapping non-empty at initial numbering?");
62 assert(mi2iMap.empty() &&
63 "MachineInstr -> Index mapping non-empty at initial numbering?");
68 push_back(createEntry(0, index));
70 // Iterate over the function.
71 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
72 mbbItr != mbbEnd; ++mbbItr) {
73 MachineBasicBlock *mbb = &*mbbItr;
75 // Insert an index for the MBB start.
76 SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
78 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
79 miItr != miEnd; ++miItr) {
80 MachineInstr *mi = miItr;
81 if (mi->isDebugValue())
84 // Insert a store index for the instr.
85 push_back(createEntry(mi, index += SlotIndex::InstrDist));
87 // Save this base index in the maps.
88 mi2iMap.insert(std::make_pair(mi, SlotIndex(back(), SlotIndex::LOAD)));
93 // We insert one blank instructions between basic blocks.
94 push_back(createEntry(0, index += SlotIndex::InstrDist));
96 SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
98 std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
100 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
103 // Sort the Idx2MBBMap
104 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
112 void SlotIndexes::renumberIndexes() {
113 // Renumber updates the index of every element of the index list.
114 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
119 for (IndexListEntry *curEntry = front(); curEntry != getTail();
120 curEntry = curEntry->getNext()) {
121 curEntry->setIndex(index);
122 index += SlotIndex::InstrDist;
126 void SlotIndexes::dump() const {
127 for (const IndexListEntry *itr = front(); itr != getTail();
128 itr = itr->getNext()) {
129 dbgs() << itr->getIndex() << " ";
131 if (itr->getInstr() != 0) {
132 dbgs() << *itr->getInstr();
138 for (MBB2IdxMap::const_iterator itr = mbb2IdxMap.begin();
139 itr != mbb2IdxMap.end(); ++itr) {
140 dbgs() << "MBB " << itr->first->getNumber() << " (" << itr->first << ") - ["
141 << itr->second.first << ", " << itr->second.second << "]\n";
145 // Print a SlotIndex to a raw_ostream.
146 void SlotIndex::print(raw_ostream &os) const {
148 os << entry().getIndex() << "LudS"[getSlot()];
153 // Dump a SlotIndex to stderr.
154 void SlotIndex::dump() const {