73adb92101d7447e5283e1c6f40cbfc0d5b5f939
[oota-llvm.git] / lib / CodeGen / SlotIndexes.cpp
1 //===-- 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"
16 #include "llvm/Target/TargetInstrInfo.h"
17
18 using namespace llvm;
19
20 char SlotIndexes::ID = 0;
21 INITIALIZE_PASS(SlotIndexes, "slotindexes",
22                 "Slot index numbering", false, false)
23
24 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
25   au.setPreservesAll();
26   MachineFunctionPass::getAnalysisUsage(au);
27 }
28
29 void SlotIndexes::releaseMemory() {
30   mi2iMap.clear();
31   mbb2IdxMap.clear();
32   idx2MBBMap.clear();
33   clearList();
34 }
35
36 bool 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
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?");
61
62   functionSize = 0;
63   unsigned index = 0;
64
65   push_back(createEntry(0, index));
66
67   // Iterate over the function.
68   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.
73     SlotIndex blockStartIndex(back(), SlotIndex::LOAD);
74
75     index += SlotIndex::NUM;
76
77     for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
78          miItr != miEnd; ++miItr) {
79       MachineInstr *mi = miItr;
80       if (mi->isDebugValue())
81         continue;
82
83       // 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
96       index += (Slots + 1) * SlotIndex::NUM;
97     }
98
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;
103
104     push_back(createEntry(0, index));
105
106     SlotIndex blockEndIndex(back(), SlotIndex::LOAD);
107     mbb2IdxMap.insert(
108       std::make_pair(mbb, std::make_pair(blockStartIndex, blockEndIndex)));
109
110     idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
111   }
112
113   // 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
122 void SlotIndexes::renumberIndexes() {
123
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
129   // had been present.
130   DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
131
132   unsigned index = 0;
133
134   for (IndexListEntry *curEntry = front(); curEntry != getTail();
135        curEntry = curEntry->getNext()) {
136     curEntry->setIndex(index);
137     index += 4*SlotIndex::NUM;
138   }
139 }
140
141 void SlotIndexes::dump() const {
142   for (const IndexListEntry *itr = front(); itr != getTail();
143        itr = itr->getNext()) {
144     dbgs() << itr->getIndex() << " ";
145
146     if (itr->getInstr() != 0) {
147       dbgs() << *itr->getInstr();
148     } else {
149       dbgs() << "\n";
150     }
151   }
152
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";
157   }
158 }
159
160 // Print a SlotIndex to a raw_ostream.
161 void SlotIndex::print(raw_ostream &os) const {
162   if (isValid())
163     os << entry().getIndex() << "LudS"[getSlot()];
164   else
165     os << "invalid";
166 }
167
168 // Dump a SlotIndex to stderr.
169 void SlotIndex::dump() const {
170   print(dbgs());
171   dbgs() << "\n";
172 }
173