LLVM 24.0.0git
SlotIndexes.cpp
Go to the documentation of this file.
1//===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/Statistic.h"
12#include "llvm/Config/llvm-config.h"
14#include "llvm/Support/Debug.h"
16
17using namespace llvm;
18
19#define DEBUG_TYPE "slot-indexes"
20
21AnalysisKey SlotIndexesAnalysis::Key;
22
28
32 OS << "Slot indexes in machine function: " << MF.getName() << '\n';
35}
37
39
41 // The indexList's nodes are all allocated in the BumpPtrAllocator.
42 indexList.clear();
43}
44
46 false, false)
47
48STATISTIC(NumLocalRenum, "Number of local renumberings");
49
54
55void SlotIndexes::clear() {
56 mi2iMap.clear();
57 MBBRanges.clear();
58 idx2MBBMap.clear();
59 indexList.clear();
60 ileAllocator.Reset();
61}
62
63void SlotIndexes::analyze(MachineFunction &fn) {
64
65 // Compute numbering as follows:
66 // Grab an iterator to the start of the index list.
67 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
68 // iterator in lock-step (though skipping it over indexes which have
69 // null pointers in the instruction field).
70 // At each iteration assert that the instruction pointed to in the index
71 // is the same one pointed to by the MI iterator. This
72
73 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
74 // only need to be set up once after the first numbering is computed.
75
76 mf = &fn;
77
78 // Check that the list contains only the sentinel.
79 assert(indexList.empty() && "Index list non-empty at initial numbering?");
80 assert(idx2MBBMap.empty() &&
81 "Index -> MBB mapping non-empty at initial numbering?");
82 assert(MBBRanges.empty() &&
83 "MBB -> Index mapping non-empty at initial numbering?");
84 assert(mi2iMap.empty() &&
85 "MachineInstr -> Index mapping non-empty at initial numbering?");
86
87 unsigned index = 0;
88 MBBRanges.resize(mf->getMaxAnalysisBlockNumber());
89 idx2MBBMap.reserve(mf->size());
90
91 indexList.push_back(*createEntry(nullptr, index));
92
93 // Iterate over the function.
94 for (MachineBasicBlock &MBB : *mf) {
95 // Insert an index for the MBB start.
96 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
97
98 for (MachineInstr &MI : MBB) {
99 if (MI.isDebugOrPseudoInstr())
100 continue;
101
102 // Insert a store index for the instr.
103 indexList.push_back(*createEntry(&MI, index += SlotIndex::InstrDist));
104
105 // Save this base index in the maps.
106 mi2iMap.insert(std::make_pair(
107 &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
108 }
109
110 // We insert one blank instructions between basic blocks.
111 indexList.push_back(*createEntry(nullptr, index += SlotIndex::InstrDist));
112
113 MBBRanges[MBB.getAnalysisNumber()].first = blockStartIndex;
114 MBBRanges[MBB.getAnalysisNumber()].second =
115 SlotIndex(&indexList.back(), SlotIndex::Slot_Block);
116 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
117 }
118
119 // Sort the Idx2MBBMap
120 llvm::sort(idx2MBBMap, less_first());
121
122 LLVM_DEBUG(mf->print(dbgs(), this));
123}
124
127 return false;
128
129 // Adjacent blocks share a boundary entry, so a boundary is either the start
130 // index of the block Idx falls in or the end index of the last block. A block
131 // dropped by removeMBBFromMaps() is gone from idx2MBBMap, so its start entry
132 // classifies as stale.
133 assert(!idx2MBBMap.empty() && "Index -> MBB mapping is empty");
134 SlotIndex Base = Idx.getBaseIndex();
136 return Base == I->first || Base == getMBBEndIdx(I->second);
137}
138
140 if (!isStaleIndex(Idx))
141 return Idx;
142
143 // The block start is a boundary entry, so it bounds the walk.
144 SlotIndex BlockStart = getMBBStartIdx(getMBBFromIndex(Idx));
145 IndexList::iterator I = Idx.listEntry()->getIterator();
146 while (&*I != BlockStart.listEntry()) {
147 --I;
148 if (I->getInstr())
149 return SlotIndex(&*I, SlotIndex::Slot_Register);
150 }
151 return BlockStart;
152}
153
155 bool AllowBundled) {
156 assert((AllowBundled || !MI.isBundledWithPred()) &&
157 "Use removeSingleMachineInstrFromMaps() instead");
158 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
159 if (mi2iItr == mi2iMap.end())
160 return;
161
162 SlotIndex MIIndex = mi2iItr->second;
163 IndexListEntry &MIEntry = *MIIndex.listEntry();
164 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
165 mi2iMap.erase(mi2iItr);
166 // FIXME: Eventually we want to actually delete these indexes.
167 MIEntry.setInstr(nullptr);
168}
169
171 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
172 if (mi2iItr == mi2iMap.end())
173 return;
174
175 SlotIndex MIIndex = mi2iItr->second;
176 IndexListEntry &MIEntry = *MIIndex.listEntry();
177 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
178 mi2iMap.erase(mi2iItr);
179
180 // When removing the first instruction of a bundle update mapping to next
181 // instruction.
182 if (MI.isBundledWithSucc()) {
183 // Only the first instruction of a bundle should have an index assigned.
184 assert(!MI.isBundledWithPred() && "Should be first bundle instruction");
185
186 MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
187 MachineInstr &NextMI = *Next;
188 MIEntry.setInstr(&NextMI);
189 mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
190 return;
191 } else {
192 // FIXME: Eventually we want to actually delete these indexes.
193 MIEntry.setInstr(nullptr);
194 }
195}
196
198 assert(&MBB != &MBB.getParent()->front() &&
199 "Can't remove the first block of a function.");
200
201 unsigned Num = MBB.getAnalysisNumber();
202 SlotIndex StartIdx = MBBRanges[Num].first;
203 SlotIndex EndIdx = MBBRanges[Num].second;
204
205 // Give MBB's slot range to its layout predecessor so blocks stay contiguous.
206 auto PrevMBB = std::prev(MBB.getIterator());
207 MBBRanges[PrevMBB->getAnalysisNumber()].second = EndIdx;
208
209 // Drop MBB's index -> MBB entry, which would dangle once MBB is erased.
210 auto It = getMBBLowerBound(StartIdx);
211 assert(It != MBBIndexEnd() && It->first == StartIdx && It->second == &MBB &&
212 "MBB not found in index -> MBB map");
213 idx2MBBMap.erase(It);
214
215 // Clear the block-start boundary entry. MBBRanges is never renumbered, so
216 // MBB's now-stale slot is simply left in place.
217 StartIdx.listEntry()->setInstr(nullptr);
218}
219
220// Renumber indexes locally after curItr was inserted, but failed to get a new
221// index.
222void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
223 // Number indexes with half the default spacing so we can catch up quickly.
224 const unsigned Space = SlotIndex::InstrDist/2;
225 static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
226
227 IndexList::iterator startItr = std::prev(curItr);
228 unsigned index = startItr->getIndex();
229 unsigned BeginIndex = index;
230 do {
231 curItr->setIndex(index += Space);
232 ++curItr;
233 // If the next index is bigger, we have caught up.
234 } while (curItr != indexList.end() && curItr->getIndex() <= index);
235
236 LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
237 << '-' << index << " ***\n");
238
239 // If we repack more than 20% of a function, add spacing in between the
240 // instructions so that future renumberings are able to catch up
241 // without also renumbering so much.
242 if (index - BeginIndex >
243 (getLastIndex().getIndex() - getZeroIndex().getIndex()) / 5)
244 packIndexes();
245
246 ++NumLocalRenum;
247}
248
249// Repair indexes after adding and removing instructions.
253 bool includeStart = (Begin == MBB->begin());
254 SlotIndex startIdx;
255 if (includeStart)
256 startIdx = getMBBStartIdx(MBB);
257 else
258 startIdx = getInstructionIndex(*--Begin);
259
260 SlotIndex endIdx;
261 if (End == MBB->end())
262 endIdx = getMBBEndIdx(MBB);
263 else
264 endIdx = getInstructionIndex(*End);
265
266 // FIXME: Conceptually, this code is implementing an iterator on MBB that
267 // optionally includes an additional position prior to MBB->begin(), indicated
268 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
269 // in parallel with SlotIndexes, but there should be a better way to do this.
270 IndexList::iterator ListB = startIdx.listEntry()->getIterator();
271 IndexList::iterator ListI = endIdx.listEntry()->getIterator();
273 bool pastStart = false;
274 bool OldIndexesRemoved = false;
275 while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
276 assert(ListI->getIndex() >= startIdx.getIndex() &&
277 (includeStart || !pastStart) &&
278 "Decremented past the beginning of region to repair.");
279
280 MachineInstr *SlotMI = ListI->getInstr();
281 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
282 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
283 bool MIIndexNotFound = MI && !mi2iMap.contains(MI);
284 bool SlotMIRemoved = false;
285
286 if (SlotMI == MI && !MBBIAtBegin) {
287 --ListI;
288 if (MBBI != Begin)
289 --MBBI;
290 else
291 pastStart = true;
292 } else if (MIIndexNotFound || OldIndexesRemoved) {
293 if (MBBI != Begin)
294 --MBBI;
295 else
296 pastStart = true;
297 } else {
298 // We ran through all the indexes on the interval
299 // -> The only thing left is to go through all the
300 // remaining MBB instructions and update their indexes
301 if (ListI == ListB)
302 OldIndexesRemoved = true;
303 else
304 --ListI;
305 if (SlotMI) {
307 SlotMIRemoved = true;
308 }
309 }
310
311 MachineInstr *InstrToInsert = SlotMIRemoved ? SlotMI : MI;
312
313 // Insert instruction back into the maps after passing it/removing the index
314 if ((MIIndexNotFound || SlotMIRemoved) && InstrToInsert->getParent() &&
315 !InstrToInsert->isDebugOrPseudoInstr())
316 insertMachineInstrInMaps(*InstrToInsert);
317 }
318}
319
321 for (auto [Index, Entry] : enumerate(indexList))
322 Entry.setIndex(Index * SlotIndex::InstrDist);
323}
324
326 for (const IndexListEntry &ILE : indexList) {
327 OS << ILE.getIndex() << ' ';
328
329 if (ILE.getInstr())
330 OS << *ILE.getInstr();
331 else
332 OS << '\n';
333 }
334
335 for (const MachineBasicBlock &MBB : *mf)
336 OS << printMBBReference(MBB) << "\t[" << getMBBStartIdx(&MBB) << ';'
337 << getMBBEndIdx(&MBB) << ")\n";
338}
339
340#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
342#endif
343
344// Print a SlotIndex to a raw_ostream.
346 if (isValid())
347 os << listEntry()->getIndex() << "Berd"[getSlot()];
348 else
349 os << "invalid";
350}
351
352#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
353// Dump a SlotIndex to stderr.
355 print(dbgs());
356 dbgs() << "\n";
357}
358#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:686
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
bool empty() const
Definition DenseMap.h:717
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:679
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:828
This class represents an entry in the slot index list held in the SlotIndexes pass.
Definition SlotIndexes.h:47
void setInstr(MachineInstr *mi)
Definition SlotIndexes.h:55
MachineInstr * getInstr() const
Definition SlotIndexes.h:54
int getAnalysisNumber() const
For analyses, blocks have a more stable number.
Instructions::iterator instr_iterator
MachineInstrBundleIterator< MachineInstr > iterator
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
unsigned getMaxAnalysisBlockNumber() const
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
bool isDebugOrPseudoInstr() const
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
@ InstrDist
The default distance between instructions as returned by distance().
bool isValid() const
Returns true if this is a valid index.
LLVM_ABI void dump() const
Dump this index to stderr.
LLVM_ABI void print(raw_ostream &os) const
Print this index to the given raw_ostream.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
void getAnalysisUsage(AnalysisUsage &au) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
LLVM_ABI bool isBlockBoundaryIndex(SlotIndex Idx) const
Returns true if Idx refers to an entry created to mark a basic block boundary.
SlotIndex getLastIndex()
Returns the base index of the last slot in this analysis.
SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late=false)
Insert the given machine instruction into the mapping.
LLVM_ABI void removeMachineInstrFromMaps(MachineInstr &MI, bool AllowBundled=false)
Removes machine instruction (bundle) MI from the mapping.
LLVM_ABI SlotIndex canonicalizeIndex(SlotIndex Idx) const
Returns the register slot of the closest instruction preceding a stale Idx, or the start index of its...
MachineBasicBlock * getMBBFromIndex(SlotIndex index) const
Returns the basic block which the given index falls in.
LLVM_ABI void dump() const
Dump the indexes.
LLVM_ABI void repairIndexesInRange(MachineBasicBlock *MBB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End)
Repair indexes after adding and removing instructions.
MBBIndexIterator getMBBLowerBound(MBBIndexIterator Start, SlotIndex Idx) const
Get an iterator pointing to the first IdxMBBPair with SlotIndex greater than or equal to Idx.
LLVM_ABI void removeSingleMachineInstrFromMaps(MachineInstr &MI)
Removes a single machine instruction MI from the mapping.
MBBIndexIterator MBBIndexEnd() const
Return an iterator for the end of the idx2MBBMap.
SmallVectorImpl< IdxMBBPair >::const_iterator MBBIndexIterator
Iterator over the idx2MBBMap (sorted pairs of slot index of basic block begin and basic block)
MBBIndexIterator getMBBUpperBound(SlotIndex Idx) const
Get an iterator pointing to the first IdxMBBPair with SlotIndex greater than Idx.
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
LLVM_ABI ~SlotIndexes()
bool isStaleIndex(SlotIndex Idx) const
Returns true if Idx refers to an instruction that has been erased.
LLVM_ABI void packIndexes()
Renumber all indexes using the default instruction distance.
LLVM_ABI void removeMBBFromMaps(MachineBasicBlock &MBB)
Inverse of insertMBBInMaps: merge MBB's slot range into its layout predecessor and drop it from the m...
LLVM_ABI void print(raw_ostream &OS) const
SlotIndex getZeroIndex()
Returns the zero index for this analysis.
SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const
Returns the index past the last valid index in the given basic block.
SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const
Returns the first index in the given basic block.
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction for the given index, or null if the given index has no instruction associated...
void reserve(size_type N)
self_iterator getIterator()
Definition ilist_node.h:123
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
void clear()
Clear the list; never deletes.
bool empty() const
Check if the list is empty in constant time.
ilist_select_iterator_type< OptionsT, false, false > iterator
void push_back(reference Node)
Insert a node at the back; never copies.
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
std::pair< SlotIndex, MachineBasicBlock * > IdxMBBPair
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29