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 #include "llvm/CodeGen/SlotIndexes.h"
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
19 #define DEBUG_TYPE "slotindexes"
21 char SlotIndexes::ID
= 0;
22 INITIALIZE_PASS(SlotIndexes
, DEBUG_TYPE
,
23 "Slot index numbering", false, false)
25 STATISTIC(NumLocalRenum
, "Number of local renumberings");
26 STATISTIC(NumGlobalRenum
, "Number of global renumberings");
28 void SlotIndexes::getAnalysisUsage(AnalysisUsage
&au
) const {
30 MachineFunctionPass::getAnalysisUsage(au
);
33 void SlotIndexes::releaseMemory() {
41 bool SlotIndexes::runOnMachineFunction(MachineFunction
&fn
) {
43 // Compute numbering as follows:
44 // Grab an iterator to the start of the index list.
45 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
46 // iterator in lock-step (though skipping it over indexes which have
47 // null pointers in the instruction field).
48 // At each iteration assert that the instruction pointed to in the index
49 // is the same one pointed to by the MI iterator. This
51 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
52 // only need to be set up once after the first numbering is computed.
56 // Check that the list contains only the sentinal.
57 assert(indexList
.empty() && "Index list non-empty at initial numbering?");
58 assert(idx2MBBMap
.empty() &&
59 "Index -> MBB mapping non-empty at initial numbering?");
60 assert(MBBRanges
.empty() &&
61 "MBB -> Index mapping non-empty at initial numbering?");
62 assert(mi2iMap
.empty() &&
63 "MachineInstr -> Index mapping non-empty at initial numbering?");
66 MBBRanges
.resize(mf
->getNumBlockIDs());
67 idx2MBBMap
.reserve(mf
->size());
69 indexList
.push_back(createEntry(nullptr, index
));
71 // Iterate over the function.
72 for (MachineBasicBlock
&MBB
: *mf
) {
73 // Insert an index for the MBB start.
74 SlotIndex
blockStartIndex(&indexList
.back(), SlotIndex::Slot_Block
);
76 for (MachineInstr
&MI
: MBB
) {
77 if (MI
.isDebugInstr())
80 // Insert a store index for the instr.
81 indexList
.push_back(createEntry(&MI
, index
+= SlotIndex::InstrDist
));
83 // Save this base index in the maps.
84 mi2iMap
.insert(std::make_pair(
85 &MI
, SlotIndex(&indexList
.back(), SlotIndex::Slot_Block
)));
88 // We insert one blank instructions between basic blocks.
89 indexList
.push_back(createEntry(nullptr, index
+= SlotIndex::InstrDist
));
91 MBBRanges
[MBB
.getNumber()].first
= blockStartIndex
;
92 MBBRanges
[MBB
.getNumber()].second
= SlotIndex(&indexList
.back(),
93 SlotIndex::Slot_Block
);
94 idx2MBBMap
.push_back(IdxMBBPair(blockStartIndex
, &MBB
));
97 // Sort the Idx2MBBMap
98 llvm::sort(idx2MBBMap
, Idx2MBBCompare());
100 LLVM_DEBUG(mf
->print(dbgs(), this));
106 void SlotIndexes::removeMachineInstrFromMaps(MachineInstr
&MI
) {
107 assert(!MI
.isBundledWithPred() &&
108 "Use removeSingleMachineInstrFromMaps() instread");
109 Mi2IndexMap::iterator mi2iItr
= mi2iMap
.find(&MI
);
110 if (mi2iItr
== mi2iMap
.end())
113 SlotIndex MIIndex
= mi2iItr
->second
;
114 IndexListEntry
&MIEntry
= *MIIndex
.listEntry();
115 assert(MIEntry
.getInstr() == &MI
&& "Instruction indexes broken.");
116 mi2iMap
.erase(mi2iItr
);
117 // FIXME: Eventually we want to actually delete these indexes.
118 MIEntry
.setInstr(nullptr);
121 void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr
&MI
) {
122 Mi2IndexMap::iterator mi2iItr
= mi2iMap
.find(&MI
);
123 if (mi2iItr
== mi2iMap
.end())
126 SlotIndex MIIndex
= mi2iItr
->second
;
127 IndexListEntry
&MIEntry
= *MIIndex
.listEntry();
128 assert(MIEntry
.getInstr() == &MI
&& "Instruction indexes broken.");
129 mi2iMap
.erase(mi2iItr
);
131 // When removing the first instruction of a bundle update mapping to next
133 if (MI
.isBundledWithSucc()) {
134 // Only the first instruction of a bundle should have an index assigned.
135 assert(!MI
.isBundledWithPred() && "Should have first bundle isntruction");
137 MachineBasicBlock::instr_iterator Next
= std::next(MI
.getIterator());
138 MachineInstr
&NextMI
= *Next
;
139 MIEntry
.setInstr(&NextMI
);
140 mi2iMap
.insert(std::make_pair(&NextMI
, MIIndex
));
143 // FIXME: Eventually we want to actually delete these indexes.
144 MIEntry
.setInstr(nullptr);
148 void SlotIndexes::renumberIndexes() {
149 // Renumber updates the index of every element of the index list.
150 LLVM_DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
155 for (IndexList::iterator I
= indexList
.begin(), E
= indexList
.end();
158 index
+= SlotIndex::InstrDist
;
162 // Renumber indexes locally after curItr was inserted, but failed to get a new
164 void SlotIndexes::renumberIndexes(IndexList::iterator curItr
) {
165 // Number indexes with half the default spacing so we can catch up quickly.
166 const unsigned Space
= SlotIndex::InstrDist
/2;
167 static_assert((Space
& 3) == 0, "InstrDist must be a multiple of 2*NUM");
169 IndexList::iterator startItr
= std::prev(curItr
);
170 unsigned index
= startItr
->getIndex();
172 curItr
->setIndex(index
+= Space
);
174 // If the next index is bigger, we have caught up.
175 } while (curItr
!= indexList
.end() && curItr
->getIndex() <= index
);
177 LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr
->getIndex()
178 << '-' << index
<< " ***\n");
182 // Repair indexes after adding and removing instructions.
183 void SlotIndexes::repairIndexesInRange(MachineBasicBlock
*MBB
,
184 MachineBasicBlock::iterator Begin
,
185 MachineBasicBlock::iterator End
) {
186 // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
187 // does the same thing.
188 // Find anchor points, which are at the beginning/end of blocks or at
189 // instructions that already have indexes.
190 while (Begin
!= MBB
->begin() && !hasIndex(*Begin
))
192 while (End
!= MBB
->end() && !hasIndex(*End
))
195 bool includeStart
= (Begin
== MBB
->begin());
198 startIdx
= getMBBStartIdx(MBB
);
200 startIdx
= getInstructionIndex(*Begin
);
203 if (End
== MBB
->end())
204 endIdx
= getMBBEndIdx(MBB
);
206 endIdx
= getInstructionIndex(*End
);
208 // FIXME: Conceptually, this code is implementing an iterator on MBB that
209 // optionally includes an additional position prior to MBB->begin(), indicated
210 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
211 // in parallel with SlotIndexes, but there should be a better way to do this.
212 IndexList::iterator ListB
= startIdx
.listEntry()->getIterator();
213 IndexList::iterator ListI
= endIdx
.listEntry()->getIterator();
214 MachineBasicBlock::iterator MBBI
= End
;
215 bool pastStart
= false;
216 while (ListI
!= ListB
|| MBBI
!= Begin
|| (includeStart
&& !pastStart
)) {
217 assert(ListI
->getIndex() >= startIdx
.getIndex() &&
218 (includeStart
|| !pastStart
) &&
219 "Decremented past the beginning of region to repair.");
221 MachineInstr
*SlotMI
= ListI
->getInstr();
222 MachineInstr
*MI
= (MBBI
!= MBB
->end() && !pastStart
) ? &*MBBI
: nullptr;
223 bool MBBIAtBegin
= MBBI
== Begin
&& (!includeStart
|| pastStart
);
225 if (SlotMI
== MI
&& !MBBIAtBegin
) {
231 } else if (MI
&& mi2iMap
.find(MI
) == mi2iMap
.end()) {
239 removeMachineInstrFromMaps(*SlotMI
);
243 // In theory this could be combined with the previous loop, but it is tricky
244 // to update the IndexList while we are iterating it.
245 for (MachineBasicBlock::iterator I
= End
; I
!= Begin
;) {
247 MachineInstr
&MI
= *I
;
248 if (!MI
.isDebugInstr() && mi2iMap
.find(&MI
) == mi2iMap
.end())
249 insertMachineInstrInMaps(MI
);
253 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
254 LLVM_DUMP_METHOD
void SlotIndexes::dump() const {
255 for (IndexList::const_iterator itr
= indexList
.begin();
256 itr
!= indexList
.end(); ++itr
) {
257 dbgs() << itr
->getIndex() << " ";
259 if (itr
->getInstr()) {
260 dbgs() << *itr
->getInstr();
266 for (unsigned i
= 0, e
= MBBRanges
.size(); i
!= e
; ++i
)
267 dbgs() << "%bb." << i
<< "\t[" << MBBRanges
[i
].first
<< ';'
268 << MBBRanges
[i
].second
<< ")\n";
272 // Print a SlotIndex to a raw_ostream.
273 void SlotIndex::print(raw_ostream
&os
) const {
275 os
<< listEntry()->getIndex() << "Berd"[getSlot()];
280 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
281 // Dump a SlotIndex to stderr.
282 LLVM_DUMP_METHOD
void SlotIndex::dump() const {