LLVM  10.0.0
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"
13 #include "llvm/InitializePasses.h"
14 #include "llvm/Support/Debug.h"
16 
17 using namespace llvm;
18 
19 #define DEBUG_TYPE "slotindexes"
20 
21 char SlotIndexes::ID = 0;
22 
25 }
26 
28  // The indexList's nodes are all allocated in the BumpPtrAllocator.
29  indexList.clearAndLeakNodesUnsafely();
30 }
31 
33  "Slot index numbering", false, false)
34 
35 STATISTIC(NumLocalRenum, "Number of local renumberings");
36 
38  au.setPreservesAll();
40 }
41 
43  mi2iMap.clear();
44  MBBRanges.clear();
45  idx2MBBMap.clear();
46  indexList.clear();
47  ileAllocator.Reset();
48 }
49 
51 
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
59 
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.
62 
63  mf = &fn;
64 
65  // Check that the list contains only the sentinal.
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?");
73 
74  unsigned index = 0;
75  MBBRanges.resize(mf->getNumBlockIDs());
76  idx2MBBMap.reserve(mf->size());
77 
78  indexList.push_back(createEntry(nullptr, index));
79 
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);
84 
85  for (MachineInstr &MI : MBB) {
86  if (MI.isDebugInstr())
87  continue;
88 
89  // Insert a store index for the instr.
90  indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
91 
92  // Save this base index in the maps.
93  mi2iMap.insert(std::make_pair(
94  &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
95  }
96 
97  // We insert one blank instructions between basic blocks.
98  indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
99 
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));
104  }
105 
106  // Sort the Idx2MBBMap
107  llvm::sort(idx2MBBMap, less_first());
108 
109  LLVM_DEBUG(mf->print(dbgs(), this));
110 
111  // And we're done!
112  return false;
113 }
114 
116  assert(!MI.isBundledWithPred() &&
117  "Use removeSingleMachineInstrFromMaps() instread");
118  Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
119  if (mi2iItr == mi2iMap.end())
120  return;
121 
122  SlotIndex MIIndex = mi2iItr->second;
123  IndexListEntry &MIEntry = *MIIndex.listEntry();
124  assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
125  mi2iMap.erase(mi2iItr);
126  // FIXME: Eventually we want to actually delete these indexes.
127  MIEntry.setInstr(nullptr);
128 }
129 
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 
140  // When removing the first instruction of a bundle update mapping to next
141  // instruction.
142  if (MI.isBundledWithSucc()) {
143  // Only the first instruction of a bundle should have an index assigned.
144  assert(!MI.isBundledWithPred() && "Should have first bundle isntruction");
145 
146  MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
147  MachineInstr &NextMI = *Next;
148  MIEntry.setInstr(&NextMI);
149  mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
150  return;
151  } else {
152  // FIXME: Eventually we want to actually delete these indexes.
153  MIEntry.setInstr(nullptr);
154  }
155 }
156 
157 // Renumber indexes locally after curItr was inserted, but failed to get a new
158 // index.
159 void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
160  // Number indexes with half the default spacing so we can catch up quickly.
161  const unsigned Space = SlotIndex::InstrDist/2;
162  static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
163 
164  IndexList::iterator startItr = std::prev(curItr);
165  unsigned index = startItr->getIndex();
166  do {
167  curItr->setIndex(index += Space);
168  ++curItr;
169  // If the next index is bigger, we have caught up.
170  } while (curItr != indexList.end() && curItr->getIndex() <= index);
171 
172  LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
173  << '-' << index << " ***\n");
174  ++NumLocalRenum;
175 }
176 
177 // Repair indexes after adding and removing instructions.
181  // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
182  // does the same thing.
183  // Find anchor points, which are at the beginning/end of blocks or at
184  // instructions that already have indexes.
185  while (Begin != MBB->begin() && !hasIndex(*Begin))
186  --Begin;
187  while (End != MBB->end() && !hasIndex(*End))
188  ++End;
189 
190  bool includeStart = (Begin == MBB->begin());
191  SlotIndex startIdx;
192  if (includeStart)
193  startIdx = getMBBStartIdx(MBB);
194  else
195  startIdx = getInstructionIndex(*Begin);
196 
197  SlotIndex endIdx;
198  if (End == MBB->end())
199  endIdx = getMBBEndIdx(MBB);
200  else
201  endIdx = getInstructionIndex(*End);
202 
203  // FIXME: Conceptually, this code is implementing an iterator on MBB that
204  // optionally includes an additional position prior to MBB->begin(), indicated
205  // by the includeStart flag. This is done so that we can iterate MIs in a MBB
206  // in parallel with SlotIndexes, but there should be a better way to do this.
207  IndexList::iterator ListB = startIdx.listEntry()->getIterator();
208  IndexList::iterator ListI = endIdx.listEntry()->getIterator();
209  MachineBasicBlock::iterator MBBI = End;
210  bool pastStart = false;
211  while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
212  assert(ListI->getIndex() >= startIdx.getIndex() &&
213  (includeStart || !pastStart) &&
214  "Decremented past the beginning of region to repair.");
215 
216  MachineInstr *SlotMI = ListI->getInstr();
217  MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
218  bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
219 
220  if (SlotMI == MI && !MBBIAtBegin) {
221  --ListI;
222  if (MBBI != Begin)
223  --MBBI;
224  else
225  pastStart = true;
226  } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
227  if (MBBI != Begin)
228  --MBBI;
229  else
230  pastStart = true;
231  } else {
232  --ListI;
233  if (SlotMI)
235  }
236  }
237 
238  // In theory this could be combined with the previous loop, but it is tricky
239  // to update the IndexList while we are iterating it.
240  for (MachineBasicBlock::iterator I = End; I != Begin;) {
241  --I;
242  MachineInstr &MI = *I;
243  if (!MI.isDebugInstr() && mi2iMap.find(&MI) == mi2iMap.end())
245  }
246 }
247 
248 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
250  for (IndexList::const_iterator itr = indexList.begin();
251  itr != indexList.end(); ++itr) {
252  dbgs() << itr->getIndex() << " ";
253 
254  if (itr->getInstr()) {
255  dbgs() << *itr->getInstr();
256  } else {
257  dbgs() << "\n";
258  }
259  }
260 
261  for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
262  dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
263  << MBBRanges[i].second << ")\n";
264 }
265 #endif
266 
267 // Print a SlotIndex to a raw_ostream.
268 void SlotIndex::print(raw_ostream &os) const {
269  if (isValid())
270  os << listEntry()->getIndex() << "Berd"[getSlot()];
271  else
272  os << "invalid";
273 }
274 
275 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
276 // Dump a SlotIndex to stderr.
278  print(dbgs());
279  dbgs() << "\n";
280 }
281 #endif
282 
The default distance between instructions as returned by distance().
Definition: SlotIndexes.h:137
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:492
void removeSingleMachineInstrFromMaps(MachineInstr &MI)
Removes a single machine instruction MI from the mapping.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID&#39;s allocated.
bool isBundledWithPred() const
Return true if this instruction is part of a bundle, and it is not the first instruction in the bundl...
Definition: MachineInstr.h:379
unsigned size() const
void push_back(const T &Elt)
Definition: SmallVector.h:211
void removeMachineInstrFromMaps(MachineInstr &MI)
Removes machine instruction (bundle) MI from the mapping.
STATISTIC(NumFunctions, "Total number of functions")
void reserve(size_type N)
Definition: SmallVector.h:369
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:195
#define DEBUG_TYPE
Definition: SlotIndexes.cpp:19
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it...
Definition: Allocator.h:195
bool isBundledWithSucc() const
Return true if this instruction is part of a bundle, and it is not the last instruction in the bundle...
Definition: MachineInstr.h:383
SlotIndex getInstructionIndex(const MachineInstr &MI) const
Returns the base index for the given instruction.
Definition: SlotIndexes.h:385
SlotIndexes pass.
Definition: SlotIndexes.h:314
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
void dump() const
Dump this index to stderr.
SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late=false)
Insert the given machine instruction into the mapping.
Definition: SlotIndexes.h:533
MachineInstr * getInstr() const
Definition: SlotIndexes.h:53
bool erase(const KeyT &Val)
Definition: DenseMap.h:272
void initializeSlotIndexesPass(PassRegistry &)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool hasIndex(const MachineInstr &instr) const
Returns true if the given machine instr is mapped to an index, otherwise returns false.
Definition: SlotIndexes.h:380
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
Definition: SlotIndexes.h:464
Represent the analysis usage information of a pass.
std::pair< SlotIndex, MachineBasicBlock * > IdxMBBPair
Definition: SlotIndexes.h:309
constexpr double e
Definition: MathExtras.h:57
self_iterator getIterator()
Definition: ilist_node.h:81
void setInstr(MachineInstr *mi)
Definition: SlotIndexes.h:54
void dump() const
Dump the indexes.
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
Definition: SlotIndexes.h:474
bool isDebugInstr() const
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1114
Iterator for intrusive lists based on ilist_node.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void push_back(pointer val)
Definition: ilist.h:311
void repairIndexesInRange(MachineBasicBlock *MBB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End)
Repair indexes after adding and removing instructions.
void getAnalysisUsage(AnalysisUsage &au) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: SlotIndexes.cpp:37
Representation of each machine instruction.
Definition: MachineInstr.h:63
static char ID
Definition: SlotIndexes.h:348
This class represents an entry in the slot index list held in the SlotIndexes pass.
Definition: SlotIndexes.h:46
bool runOnMachineFunction(MachineFunction &fn) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: SlotIndexes.cpp:50
void clear()
Definition: ilist.h:307
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: SlotIndexes.cpp:42
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
#define I(x, y, z)
Definition: MD5.cpp:58
iterator end()
Definition: DenseMap.h:82
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:128
INITIALIZE_PASS(SlotIndexes, DEBUG_TYPE, "Slot index numbering", false, false) STATISTIC(NumLocalRenum
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:96
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void clearAndLeakNodesUnsafely()
Remove all nodes from the list like clear(), but do not call removeNodeFromList() or deleteNode()...
Definition: ilist.h:278
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
void print(raw_ostream &os) const
Print this index to the given raw_ostream.
#define LLVM_DEBUG(X)
Definition: Debug.h:122
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:83
Function object to check whether the first component of a std::pair compares less than the first comp...
Definition: STLExtras.h:965
Number of local renumberings
Definition: SlotIndexes.cpp:35
~SlotIndexes() override
Definition: SlotIndexes.cpp:27