LLVM  4.0.0
SlotIndexes.cpp
Go to the documentation of this file.
1 //===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/Statistic.h"
13 #include "llvm/Support/Debug.h"
16 
17 using namespace llvm;
18 
19 #define DEBUG_TYPE "slotindexes"
20 
21 char SlotIndexes::ID = 0;
22 INITIALIZE_PASS(SlotIndexes, "slotindexes",
23  "Slot index numbering", false, false)
24 
25 STATISTIC(NumLocalRenum, "Number of local renumberings");
26 STATISTIC(NumGlobalRenum, "Number of global renumberings");
27 
28 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
29  au.setPreservesAll();
31 }
32 
34  mi2iMap.clear();
35  MBBRanges.clear();
36  idx2MBBMap.clear();
37  indexList.clear();
38  ileAllocator.Reset();
39 }
40 
42 
43  // Compute numbering as follows:
44  // Grab an iterator to the start of the index list.
45  // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
46  // iterator in lock-step (though skipping it over indexes which have
47  // null pointers in the instruction field).
48  // At each iteration assert that the instruction pointed to in the index
49  // is the same one pointed to by the MI iterator. This
50 
51  // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
52  // only need to be set up once after the first numbering is computed.
53 
54  mf = &fn;
55 
56  // Check that the list contains only the sentinal.
57  assert(indexList.empty() && "Index list non-empty at initial numbering?");
58  assert(idx2MBBMap.empty() &&
59  "Index -> MBB mapping non-empty at initial numbering?");
60  assert(MBBRanges.empty() &&
61  "MBB -> Index mapping non-empty at initial numbering?");
62  assert(mi2iMap.empty() &&
63  "MachineInstr -> Index mapping non-empty at initial numbering?");
64 
65  unsigned index = 0;
66  MBBRanges.resize(mf->getNumBlockIDs());
67  idx2MBBMap.reserve(mf->size());
68 
69  indexList.push_back(createEntry(nullptr, index));
70 
71  // Iterate over the function.
72  for (MachineBasicBlock &MBB : *mf) {
73  // Insert an index for the MBB start.
74  SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
75 
76  for (MachineInstr &MI : MBB) {
77  if (MI.isDebugValue())
78  continue;
79 
80  // Insert a store index for the instr.
81  indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
82 
83  // Save this base index in the maps.
84  mi2iMap.insert(std::make_pair(
85  &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
86  }
87 
88  // We insert one blank instructions between basic blocks.
89  indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
90 
91  MBBRanges[MBB.getNumber()].first = blockStartIndex;
92  MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
93  SlotIndex::Slot_Block);
94  idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
95  }
96 
97  // Sort the Idx2MBBMap
98  std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
99 
100  DEBUG(mf->print(dbgs(), this));
101 
102  // And we're done!
103  return false;
104 }
105 
107  // Renumber updates the index of every element of the index list.
108  DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
109  ++NumGlobalRenum;
110 
111  unsigned index = 0;
112 
113  for (IndexList::iterator I = indexList.begin(), E = indexList.end();
114  I != E; ++I) {
115  I->setIndex(index);
116  index += SlotIndex::InstrDist;
117  }
118 }
119 
120 // Renumber indexes locally after curItr was inserted, but failed to get a new
121 // index.
122 void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
123  // Number indexes with half the default spacing so we can catch up quickly.
124  const unsigned Space = SlotIndex::InstrDist/2;
125  static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
126 
127  IndexList::iterator startItr = std::prev(curItr);
128  unsigned index = startItr->getIndex();
129  do {
130  curItr->setIndex(index += Space);
131  ++curItr;
132  // If the next index is bigger, we have caught up.
133  } while (curItr != indexList.end() && curItr->getIndex() <= index);
134 
135  DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
136  << index << " ***\n");
137  ++NumLocalRenum;
138 }
139 
140 // Repair indexes after adding and removing instructions.
144  // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
145  // does the same thing.
146  // Find anchor points, which are at the beginning/end of blocks or at
147  // instructions that already have indexes.
148  while (Begin != MBB->begin() && !hasIndex(*Begin))
149  --Begin;
150  while (End != MBB->end() && !hasIndex(*End))
151  ++End;
152 
153  bool includeStart = (Begin == MBB->begin());
154  SlotIndex startIdx;
155  if (includeStart)
156  startIdx = getMBBStartIdx(MBB);
157  else
158  startIdx = getInstructionIndex(*Begin);
159 
160  SlotIndex endIdx;
161  if (End == MBB->end())
162  endIdx = getMBBEndIdx(MBB);
163  else
164  endIdx = getInstructionIndex(*End);
165 
166  // FIXME: Conceptually, this code is implementing an iterator on MBB that
167  // optionally includes an additional position prior to MBB->begin(), indicated
168  // by the includeStart flag. This is done so that we can iterate MIs in a MBB
169  // in parallel with SlotIndexes, but there should be a better way to do this.
170  IndexList::iterator ListB = startIdx.listEntry()->getIterator();
171  IndexList::iterator ListI = endIdx.listEntry()->getIterator();
173  bool pastStart = false;
174  while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
175  assert(ListI->getIndex() >= startIdx.getIndex() &&
176  (includeStart || !pastStart) &&
177  "Decremented past the beginning of region to repair.");
178 
179  MachineInstr *SlotMI = ListI->getInstr();
180  MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
181  bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
182 
183  if (SlotMI == MI && !MBBIAtBegin) {
184  --ListI;
185  if (MBBI != Begin)
186  --MBBI;
187  else
188  pastStart = true;
189  } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
190  if (MBBI != Begin)
191  --MBBI;
192  else
193  pastStart = true;
194  } else {
195  --ListI;
196  if (SlotMI)
198  }
199  }
200 
201  // In theory this could be combined with the previous loop, but it is tricky
202  // to update the IndexList while we are iterating it.
203  for (MachineBasicBlock::iterator I = End; I != Begin;) {
204  --I;
205  MachineInstr &MI = *I;
206  if (!MI.isDebugValue() && mi2iMap.find(&MI) == mi2iMap.end())
208  }
209 }
210 
211 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
213  for (IndexList::const_iterator itr = indexList.begin();
214  itr != indexList.end(); ++itr) {
215  dbgs() << itr->getIndex() << " ";
216 
217  if (itr->getInstr()) {
218  dbgs() << *itr->getInstr();
219  } else {
220  dbgs() << "\n";
221  }
222  }
223 
224  for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
225  dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
226  << MBBRanges[i].second << ")\n";
227 }
228 #endif
229 
230 // Print a SlotIndex to a raw_ostream.
231 void SlotIndex::print(raw_ostream &os) const {
232  if (isValid())
233  os << listEntry()->getIndex() << "Berd"[getSlot()];
234  else
235  os << "invalid";
236 }
237 
238 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
239 // Dump a SlotIndex to stderr.
241  print(dbgs());
242  dbgs() << "\n";
243 }
244 #endif
245 
void push_back(const T &Elt)
Definition: SmallVector.h:211
void renumberIndexes()
Renumber the index list, providing space for new instructions.
std::pair< SlotIndex, MachineBasicBlock * > IdxMBBPair
Definition: SlotIndexes.h:304
SlotIndex getInstructionIndex(const MachineInstr &MI) const
Returns the base index for the given instruction.
Definition: SlotIndexes.h:406
STATISTIC(NumFunctions,"Total number of functions")
size_t i
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
void removeMachineInstrFromMaps(MachineInstr &MI)
Remove the given machine instruction from the mapping.
Definition: SlotIndexes.h:606
void reserve(size_type N)
Definition: SmallVector.h:377
void dump() const
Dump this index to stderr.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:172
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it...
Definition: Allocator.h:192
void dump() const
Dump the indexes.
bool hasIndex(const MachineInstr &instr) const
Returns true if the given machine instr is mapped to an index, otherwise returns false.
Definition: SlotIndexes.h:401
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
SlotIndexes pass.
Definition: SlotIndexes.h:323
MachineBasicBlock * MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
bool isDebugValue() const
Definition: MachineInstr.h:777
SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late=false)
Insert the given machine instruction into the mapping.
Definition: SlotIndexes.h:565
unsigned getIndex() const
Definition: SlotIndexes.h:51
bool isValid() const
Returns true if this is a valid index.
Definition: SlotIndexes.h:144
The default distance between instructions as returned by distance().
Definition: SlotIndexes.h:130
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
Represent the analysis usage information of a pass.
static const unsigned End
self_iterator getIterator()
Definition: ilist_node.h:81
unsigned size() const
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
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
Definition: SlotIndexes.h:489
void push_back(pointer val)
Definition: ilist.h:326
void repairIndexesInRange(MachineBasicBlock *MBB, MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End)
Repair indexes after adding and removing instructions.
void print(raw_ostream &os) const
Print this index to the given raw_ostream.
Representation of each machine instruction.
Definition: MachineInstr.h:52
static char ID
Definition: SlotIndexes.h:361
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
bool runOnMachineFunction(MachineFunction &fn) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition: SlotIndexes.cpp:41
void clear()
Definition: ilist.h:322
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:33
#define I(x, y, z)
Definition: MD5.cpp:54
iterator end()
Definition: DenseMap.h:69
iterator find(const KeyT &Val)
Definition: DenseMap.h:127
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
Definition: SlotIndexes.h:479
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:80
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
#define DEBUG(X)
Definition: Debug.h:100
INITIALIZE_PASS(SlotIndexes,"slotindexes","Slot index numbering", false, false) STATISTIC(NumLocalRenum
IRTranslator LLVM IR MI
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:76
Number of local renumberings
Definition: SlotIndexes.cpp:25