LLVM 22.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 "slotindexes"
20
21AnalysisKey SlotIndexesAnalysis::Key;
22
28
32 OS << "Slot indexes in machine function: " << MF.getName() << '\n';
35}
37
41
43 // The indexList's nodes are all allocated in the BumpPtrAllocator.
44 indexList.clear();
45}
46
48 false, false)
49
50STATISTIC(NumLocalRenum, "Number of local renumberings");
51
56
57void SlotIndexes::clear() {
58 mi2iMap.clear();
59 MBBRanges.clear();
60 idx2MBBMap.clear();
61 indexList.clear();
62 ileAllocator.Reset();
63}
64
65void SlotIndexes::analyze(MachineFunction &fn) {
66
67 // Compute numbering as follows:
68 // Grab an iterator to the start of the index list.
69 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
70 // iterator in lock-step (though skipping it over indexes which have
71 // null pointers in the instruction field).
72 // At each iteration assert that the instruction pointed to in the index
73 // is the same one pointed to by the MI iterator. This
74
75 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
76 // only need to be set up once after the first numbering is computed.
77
78 mf = &fn;
79
80 // Check that the list contains only the sentinel.
81 assert(indexList.empty() && "Index list non-empty at initial numbering?");
82 assert(idx2MBBMap.empty() &&
83 "Index -> MBB mapping non-empty at initial numbering?");
84 assert(MBBRanges.empty() &&
85 "MBB -> Index mapping non-empty at initial numbering?");
86 assert(mi2iMap.empty() &&
87 "MachineInstr -> Index mapping non-empty at initial numbering?");
88
89 unsigned index = 0;
90 MBBRanges.resize(mf->getNumBlockIDs());
91 idx2MBBMap.reserve(mf->size());
92
93 indexList.push_back(*createEntry(nullptr, index));
94
95 // Iterate over the function.
96 for (MachineBasicBlock &MBB : *mf) {
97 // Insert an index for the MBB start.
98 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
99
100 for (MachineInstr &MI : MBB) {
101 if (MI.isDebugOrPseudoInstr())
102 continue;
103
104 // Insert a store index for the instr.
105 indexList.push_back(*createEntry(&MI, index += SlotIndex::InstrDist));
106
107 // Save this base index in the maps.
108 mi2iMap.insert(std::make_pair(
109 &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
110 }
111
112 // We insert one blank instructions between basic blocks.
113 indexList.push_back(*createEntry(nullptr, index += SlotIndex::InstrDist));
114
115 MBBRanges[MBB.getNumber()].first = blockStartIndex;
116 MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
117 SlotIndex::Slot_Block);
118 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
119 }
120
121 // Sort the Idx2MBBMap
122 llvm::sort(idx2MBBMap, less_first());
123
124 LLVM_DEBUG(mf->print(dbgs(), this));
125}
126
128 bool AllowBundled) {
129 assert((AllowBundled || !MI.isBundledWithPred()) &&
130 "Use removeSingleMachineInstrFromMaps() instead");
131 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
132 if (mi2iItr == mi2iMap.end())
133 return;
134
135 SlotIndex MIIndex = mi2iItr->second;
136 IndexListEntry &MIEntry = *MIIndex.listEntry();
137 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
138 mi2iMap.erase(mi2iItr);
139 // FIXME: Eventually we want to actually delete these indexes.
140 MIEntry.setInstr(nullptr);
141}
142
144 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
145 if (mi2iItr == mi2iMap.end())
146 return;
147
148 SlotIndex MIIndex = mi2iItr->second;
149 IndexListEntry &MIEntry = *MIIndex.listEntry();
150 assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
151 mi2iMap.erase(mi2iItr);
152
153 // When removing the first instruction of a bundle update mapping to next
154 // instruction.
155 if (MI.isBundledWithSucc()) {
156 // Only the first instruction of a bundle should have an index assigned.
157 assert(!MI.isBundledWithPred() && "Should be first bundle instruction");
158
159 MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
160 MachineInstr &NextMI = *Next;
161 MIEntry.setInstr(&NextMI);
162 mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
163 return;
164 } else {
165 // FIXME: Eventually we want to actually delete these indexes.
166 MIEntry.setInstr(nullptr);
167 }
168}
169
170// Renumber indexes locally after curItr was inserted, but failed to get a new
171// index.
172void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
173 // Number indexes with half the default spacing so we can catch up quickly.
174 const unsigned Space = SlotIndex::InstrDist/2;
175 static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
176
177 IndexList::iterator startItr = std::prev(curItr);
178 unsigned index = startItr->getIndex();
179 do {
180 curItr->setIndex(index += Space);
181 ++curItr;
182 // If the next index is bigger, we have caught up.
183 } while (curItr != indexList.end() && curItr->getIndex() <= index);
184
185 LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
186 << '-' << index << " ***\n");
187 ++NumLocalRenum;
188}
189
190// Repair indexes after adding and removing instructions.
194 bool includeStart = (Begin == MBB->begin());
195 SlotIndex startIdx;
196 if (includeStart)
197 startIdx = getMBBStartIdx(MBB);
198 else
199 startIdx = getInstructionIndex(*--Begin);
200
201 SlotIndex endIdx;
202 if (End == MBB->end())
203 endIdx = getMBBEndIdx(MBB);
204 else
205 endIdx = getInstructionIndex(*End);
206
207 // FIXME: Conceptually, this code is implementing an iterator on MBB that
208 // optionally includes an additional position prior to MBB->begin(), indicated
209 // by the includeStart flag. This is done so that we can iterate MIs in a MBB
210 // in parallel with SlotIndexes, but there should be a better way to do this.
211 IndexList::iterator ListB = startIdx.listEntry()->getIterator();
212 IndexList::iterator ListI = endIdx.listEntry()->getIterator();
214 bool pastStart = false;
215 bool OldIndexesRemoved = 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.");
220
221 MachineInstr *SlotMI = ListI->getInstr();
222 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
223 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
224 bool MIIndexNotFound = MI && !mi2iMap.contains(MI);
225 bool SlotMIRemoved = false;
226
227 if (SlotMI == MI && !MBBIAtBegin) {
228 --ListI;
229 if (MBBI != Begin)
230 --MBBI;
231 else
232 pastStart = true;
233 } else if (MIIndexNotFound || OldIndexesRemoved) {
234 if (MBBI != Begin)
235 --MBBI;
236 else
237 pastStart = true;
238 } else {
239 // We ran through all the indexes on the interval
240 // -> The only thing left is to go through all the
241 // remaining MBB instructions and update their indexes
242 if (ListI == ListB)
243 OldIndexesRemoved = true;
244 else
245 --ListI;
246 if (SlotMI) {
248 SlotMIRemoved = true;
249 }
250 }
251
252 MachineInstr *InstrToInsert = SlotMIRemoved ? SlotMI : MI;
253
254 // Insert instruction back into the maps after passing it/removing the index
255 if ((MIIndexNotFound || SlotMIRemoved) && InstrToInsert->getParent() &&
256 !InstrToInsert->isDebugOrPseudoInstr())
257 insertMachineInstrInMaps(*InstrToInsert);
258 }
259}
260
262 for (auto [Index, Entry] : enumerate(indexList))
263 Entry.setIndex(Index * SlotIndex::InstrDist);
264}
265
267 for (const IndexListEntry &ILE : indexList) {
268 OS << ILE.getIndex() << ' ';
269
270 if (ILE.getInstr())
271 OS << *ILE.getInstr();
272 else
273 OS << '\n';
274 }
275
276 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
277 OS << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
278 << MBBRanges[i].second << ")\n";
279}
280
281#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
283#endif
284
285// Print a SlotIndex to a raw_ostream.
287 if (isValid())
288 os << listEntry()->getIndex() << "Berd"[getSlot()];
289 else
290 os << "invalid";
291}
292
293#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
294// Dump a SlotIndex to stderr.
296 print(dbgs());
297 dbgs() << "\n";
298}
299#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:638
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#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:114
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.
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition Allocator.h:124
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
bool empty() const
Definition DenseMap.h:107
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:214
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 getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
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 size() const
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
bool isDebugOrPseudoInstr() const
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
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 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.
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
LLVM_ABI void removeSingleMachineInstrFromMaps(MachineInstr &MI)
Removes a single machine instruction MI from the mapping.
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
LLVM_ABI ~SlotIndexes()
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
LLVM_ABI void packIndexes()
Renumber all indexes using the default instruction distance.
LLVM_ABI void print(raw_ostream &OS) const
void reserve(size_type N)
self_iterator getIterator()
Definition ilist_node.h:130
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.
typename ilist_select_iterator_type< OptionsT::has_iterator_bits, OptionsT, false, false >::type iterator
bool empty() const
Check if the list is empty in constant time.
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:2452
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1624
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
std::pair< SlotIndex, MachineBasicBlock * > IdxMBBPair
LLVM_ABI void initializeSlotIndexesWrapperPassPass(PassRegistry &)
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29