1 //===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/SlotIndexes.h"
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/CodeGen/MachineFunction.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/InitializePasses.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
19 #define DEBUG_TYPE "slotindexes"
21 char SlotIndexes::ID
= 0;
23 SlotIndexes::SlotIndexes() : MachineFunctionPass(ID
) {
24 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
27 SlotIndexes::~SlotIndexes() {
28 // The indexList's nodes are all allocated in the BumpPtrAllocator.
29 indexList
.clearAndLeakNodesUnsafely();
32 INITIALIZE_PASS(SlotIndexes
, DEBUG_TYPE
,
33 "Slot index numbering", false, false)
35 STATISTIC(NumLocalRenum
, "Number of local renumberings");
37 void SlotIndexes::getAnalysisUsage(AnalysisUsage
&au
) const {
39 MachineFunctionPass::getAnalysisUsage(au
);
42 void SlotIndexes::releaseMemory() {
50 bool SlotIndexes::runOnMachineFunction(MachineFunction
&fn
) {
52 // Compute numbering as follows:
53 // Grab an iterator to the start of the index list.
54 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
55 // iterator in lock-step (though skipping it over indexes which have
56 // null pointers in the instruction field).
57 // At each iteration assert that the instruction pointed to in the index
58 // is the same one pointed to by the MI iterator. This
60 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
61 // only need to be set up once after the first numbering is computed.
65 // Check that the list contains only the sentinel.
66 assert(indexList
.empty() && "Index list non-empty at initial numbering?");
67 assert(idx2MBBMap
.empty() &&
68 "Index -> MBB mapping non-empty at initial numbering?");
69 assert(MBBRanges
.empty() &&
70 "MBB -> Index mapping non-empty at initial numbering?");
71 assert(mi2iMap
.empty() &&
72 "MachineInstr -> Index mapping non-empty at initial numbering?");
75 MBBRanges
.resize(mf
->getNumBlockIDs());
76 idx2MBBMap
.reserve(mf
->size());
78 indexList
.push_back(createEntry(nullptr, index
));
80 // Iterate over the function.
81 for (MachineBasicBlock
&MBB
: *mf
) {
82 // Insert an index for the MBB start.
83 SlotIndex
blockStartIndex(&indexList
.back(), SlotIndex::Slot_Block
);
85 for (MachineInstr
&MI
: MBB
) {
86 if (MI
.isDebugOrPseudoInstr())
89 // Insert a store index for the instr.
90 indexList
.push_back(createEntry(&MI
, index
+= SlotIndex::InstrDist
));
92 // Save this base index in the maps.
93 mi2iMap
.insert(std::make_pair(
94 &MI
, SlotIndex(&indexList
.back(), SlotIndex::Slot_Block
)));
97 // We insert one blank instructions between basic blocks.
98 indexList
.push_back(createEntry(nullptr, index
+= SlotIndex::InstrDist
));
100 MBBRanges
[MBB
.getNumber()].first
= blockStartIndex
;
101 MBBRanges
[MBB
.getNumber()].second
= SlotIndex(&indexList
.back(),
102 SlotIndex::Slot_Block
);
103 idx2MBBMap
.push_back(IdxMBBPair(blockStartIndex
, &MBB
));
106 // Sort the Idx2MBBMap
107 llvm::sort(idx2MBBMap
, less_first());
109 LLVM_DEBUG(mf
->print(dbgs(), this));
115 void SlotIndexes::removeMachineInstrFromMaps(MachineInstr
&MI
,
117 assert((AllowBundled
|| !MI
.isBundledWithPred()) &&
118 "Use removeSingleMachineInstrFromMaps() instead");
119 Mi2IndexMap::iterator mi2iItr
= mi2iMap
.find(&MI
);
120 if (mi2iItr
== mi2iMap
.end())
123 SlotIndex MIIndex
= mi2iItr
->second
;
124 IndexListEntry
&MIEntry
= *MIIndex
.listEntry();
125 assert(MIEntry
.getInstr() == &MI
&& "Instruction indexes broken.");
126 mi2iMap
.erase(mi2iItr
);
127 // FIXME: Eventually we want to actually delete these indexes.
128 MIEntry
.setInstr(nullptr);
131 void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr
&MI
) {
132 Mi2IndexMap::iterator mi2iItr
= mi2iMap
.find(&MI
);
133 if (mi2iItr
== mi2iMap
.end())
136 SlotIndex MIIndex
= mi2iItr
->second
;
137 IndexListEntry
&MIEntry
= *MIIndex
.listEntry();
138 assert(MIEntry
.getInstr() == &MI
&& "Instruction indexes broken.");
139 mi2iMap
.erase(mi2iItr
);
141 // When removing the first instruction of a bundle update mapping to next
143 if (MI
.isBundledWithSucc()) {
144 // Only the first instruction of a bundle should have an index assigned.
145 assert(!MI
.isBundledWithPred() && "Should be first bundle instruction");
147 MachineBasicBlock::instr_iterator Next
= std::next(MI
.getIterator());
148 MachineInstr
&NextMI
= *Next
;
149 MIEntry
.setInstr(&NextMI
);
150 mi2iMap
.insert(std::make_pair(&NextMI
, MIIndex
));
153 // FIXME: Eventually we want to actually delete these indexes.
154 MIEntry
.setInstr(nullptr);
158 // Renumber indexes locally after curItr was inserted, but failed to get a new
160 void SlotIndexes::renumberIndexes(IndexList::iterator curItr
) {
161 // Number indexes with half the default spacing so we can catch up quickly.
162 const unsigned Space
= SlotIndex::InstrDist
/2;
163 static_assert((Space
& 3) == 0, "InstrDist must be a multiple of 2*NUM");
165 IndexList::iterator startItr
= std::prev(curItr
);
166 unsigned index
= startItr
->getIndex();
168 curItr
->setIndex(index
+= Space
);
170 // If the next index is bigger, we have caught up.
171 } while (curItr
!= indexList
.end() && curItr
->getIndex() <= index
);
173 LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr
->getIndex()
174 << '-' << index
<< " ***\n");
178 // Repair indexes after adding and removing instructions.
179 void SlotIndexes::repairIndexesInRange(MachineBasicBlock
*MBB
,
180 MachineBasicBlock::iterator Begin
,
181 MachineBasicBlock::iterator End
) {
182 bool includeStart
= (Begin
== MBB
->begin());
185 startIdx
= getMBBStartIdx(MBB
);
187 startIdx
= getInstructionIndex(*--Begin
);
190 if (End
== MBB
->end())
191 endIdx
= getMBBEndIdx(MBB
);
193 endIdx
= getInstructionIndex(*End
);
195 // FIXME: Conceptually, this code is implementing an iterator on MBB that
196 // optionally includes an additional position prior to MBB->begin(), indicated
197 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
198 // in parallel with SlotIndexes, but there should be a better way to do this.
199 IndexList::iterator ListB
= startIdx
.listEntry()->getIterator();
200 IndexList::iterator ListI
= endIdx
.listEntry()->getIterator();
201 MachineBasicBlock::iterator MBBI
= End
;
202 bool pastStart
= false;
203 while (ListI
!= ListB
|| MBBI
!= Begin
|| (includeStart
&& !pastStart
)) {
204 assert(ListI
->getIndex() >= startIdx
.getIndex() &&
205 (includeStart
|| !pastStart
) &&
206 "Decremented past the beginning of region to repair.");
208 MachineInstr
*SlotMI
= ListI
->getInstr();
209 MachineInstr
*MI
= (MBBI
!= MBB
->end() && !pastStart
) ? &*MBBI
: nullptr;
210 bool MBBIAtBegin
= MBBI
== Begin
&& (!includeStart
|| pastStart
);
212 if (SlotMI
== MI
&& !MBBIAtBegin
) {
218 } else if (MI
&& !mi2iMap
.contains(MI
)) {
226 removeMachineInstrFromMaps(*SlotMI
);
230 // In theory this could be combined with the previous loop, but it is tricky
231 // to update the IndexList while we are iterating it.
232 for (MachineBasicBlock::iterator I
= End
; I
!= Begin
;) {
234 MachineInstr
&MI
= *I
;
235 if (!MI
.isDebugOrPseudoInstr() && !mi2iMap
.contains(&MI
))
236 insertMachineInstrInMaps(MI
);
240 void SlotIndexes::packIndexes() {
241 for (auto [Index
, Entry
] : enumerate(indexList
))
242 Entry
.setIndex(Index
* SlotIndex::InstrDist
);
245 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
246 LLVM_DUMP_METHOD
void SlotIndexes::dump() const {
247 for (const IndexListEntry
&ILE
: indexList
) {
248 dbgs() << ILE
.getIndex() << " ";
250 if (ILE
.getInstr()) {
251 dbgs() << *ILE
.getInstr();
257 for (unsigned i
= 0, e
= MBBRanges
.size(); i
!= e
; ++i
)
258 dbgs() << "%bb." << i
<< "\t[" << MBBRanges
[i
].first
<< ';'
259 << MBBRanges
[i
].second
<< ")\n";
263 // Print a SlotIndex to a raw_ostream.
264 void SlotIndex::print(raw_ostream
&os
) const {
266 os
<< listEntry()->getIndex() << "Berd"[getSlot()];
271 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
272 // Dump a SlotIndex to stderr.
273 LLVM_DUMP_METHOD
void SlotIndex::dump() const {