12#include "llvm/Config/llvm-config.h"
19#define DEBUG_TYPE "slot-indexes"
32 OS <<
"Slot indexes in machine function: " << MF.
getName() <<
'\n';
48STATISTIC(NumLocalRenum,
"Number of local renumberings");
50 "Number of index list entries erased for deleted instructions");
57void SlotIndexes::clear() {
81 assert(indexList.
empty() &&
"Index list non-empty at initial numbering?");
83 "Index -> MBB mapping non-empty at initial numbering?");
84 assert(MBBRanges.empty() &&
85 "MBB -> Index mapping non-empty at initial numbering?");
86 assert(MIEntries.empty() &&
87 "MachineInstr -> Index mapping non-empty at initial numbering?");
93 indexList.
push_back(*createEntry(
nullptr, index));
98 SlotIndex blockStartIndex(&indexList.
back(), SlotIndex::Slot_Block);
101 if (
MI.isBundledWithPred() ||
MI.isDebugOrPseudoInstr()) {
102 MI.SlotIndexHandle = 0;
110 setMIEntry(
MI, &indexList.back());
118 SlotIndex(&indexList.back(), SlotIndex::Slot_Block);
136 assert(!idx2MBBMap.empty() &&
"Index -> MBB mapping is empty");
149 while (&*
I != BlockStart.listEntry()) {
152 return SlotIndex(&*
I, SlotIndex::Slot_Register);
159 assert((AllowBundled || !
MI.isBundledWithPred()) &&
160 "Use removeSingleMachineInstrFromMaps() instead");
167 MI.SlotIndexHandle = 0;
179 if (
MI.isBundledWithSucc()) {
181 assert(!
MI.isBundledWithPred() &&
"Should be first bundle instruction");
186 NextMI.SlotIndexHandle =
MI.SlotIndexHandle;
187 MI.SlotIndexHandle = 0;
192 MI.SlotIndexHandle = 0;
198 "Can't remove the first block of a function.");
200 unsigned Num =
MBB.getAnalysisNumber();
201 SlotIndex StartIdx = MBBRanges[Num].first;
202 SlotIndex EndIdx = MBBRanges[Num].second;
205 auto PrevMBB = std::prev(
MBB.getIterator());
206 MBBRanges[PrevMBB->getAnalysisNumber()].second = EndIdx;
211 "MBB not found in index -> MBB map");
212 idx2MBBMap.erase(It);
216 StartIdx.listEntry()->
setInstr(
nullptr);
221void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
224 static_assert((Space & 3) == 0,
"InstrDist must be a multiple of 2*NUM");
227 unsigned index = startItr->getIndex();
228 unsigned BeginIndex = index;
230 curItr->setIndex(index += Space);
233 }
while (curItr != indexList.
end() && curItr->getIndex() <= index);
235 LLVM_DEBUG(
dbgs() <<
"\n*** Renumbered SlotIndexes " << startItr->getIndex()
236 <<
'-' << index <<
" ***\n");
241 if (index - BeginIndex >
252 bool includeStart = (Begin ==
MBB->begin());
272 bool pastStart =
false;
273 bool OldIndexesRemoved =
false;
274 while (ListI != ListB ||
MBBI != Begin || (includeStart && !pastStart)) {
275 assert(ListI->getIndex() >= startIdx.getIndex() &&
276 (includeStart || !pastStart) &&
277 "Decremented past the beginning of region to repair.");
281 bool MBBIAtBegin =
MBBI == Begin && (!includeStart || pastStart);
283 bool SlotMIRemoved =
false;
285 if (SlotMI ==
MI && !MBBIAtBegin) {
291 }
else if (MIIndexNotFound || OldIndexesRemoved) {
301 OldIndexesRemoved =
true;
306 SlotMIRemoved =
true;
313 if ((MIIndexNotFound || SlotMIRemoved) && InstrToInsert->
getParent() &&
320 for (
auto [Index, Entry] :
enumerate(indexList))
325 if (indexList.empty())
331 Keep.insert(&indexList.front());
332 Keep.insert(&indexList.back());
334 for (
const auto &
Range : MBBRanges) {
335 if (
Range.first.isValid())
337 if (
Range.second.isValid())
344 for (
auto It = indexList.begin(), E = indexList.end(); It != E;) {
346 if (Entry.getInstr() ||
Keep.contains(&Entry)) {
347 Entry.setIndex(Index);
352 assert(Index != 0 &&
"First entry is always kept");
355 indexList.remove(Entry);
361 NumDeadEntriesErased += Erased.
size();
362 return Erased.
size();
367 OS << ILE.getIndex() <<
' ';
370 OS << *ILE.getInstr();
380#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
387 os << listEntry()->getIndex() <<
"Berd"[getSlot()];
392#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator MBBI
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
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.
This class represents an entry in the slot index list held in the SlotIndexes pass.
void setInstr(MachineInstr *mi)
int getAnalysisNumber() const
For analyses, blocks have a more stable number.
Instructions::iterator instr_iterator
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass(char &ID)
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.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
SlotIndex - An opaque wrapper around machine indexes.
@ 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.
LLVM_ABI unsigned compactIndexes()
Erase index list entries left behind by deleted instructions, then renumber.
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.
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...
bool hasIndex(const MachineInstr &instr) const
Returns true if the given machine instr is mapped to an index, otherwise returns false.
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...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
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,...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
std::pair< SlotIndex, MachineBasicBlock * > IdxMBBPair
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
@ End
Run the machine verifier once, at the end of the pipeline.
@ Keep
No function return thunk.
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...