LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
10 #define DEBUG_TYPE "slotindexes"
11 
13 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Support/Debug.h"
18 
19 using namespace llvm;
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(0, index));
70 
71  // Iterate over the function.
72  for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
73  mbbItr != mbbEnd; ++mbbItr) {
74  MachineBasicBlock *mbb = &*mbbItr;
75 
76  // Insert an index for the MBB start.
77  SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
78 
79  for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
80  miItr != miEnd; ++miItr) {
81  MachineInstr *mi = miItr;
82  if (mi->isDebugValue())
83  continue;
84 
85  // Insert a store index for the instr.
86  indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist));
87 
88  // Save this base index in the maps.
89  mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(),
90  SlotIndex::Slot_Block)));
91  }
92 
93  // We insert one blank instructions between basic blocks.
94  indexList.push_back(createEntry(0, index += SlotIndex::InstrDist));
95 
96  MBBRanges[mbb->getNumber()].first = blockStartIndex;
97  MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(),
98  SlotIndex::Slot_Block);
99  idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
100  }
101 
102  // Sort the Idx2MBBMap
103  std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
104 
105  DEBUG(mf->print(dbgs(), this));
106 
107  // And we're done!
108  return false;
109 }
110 
112  // Renumber updates the index of every element of the index list.
113  DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
114  ++NumGlobalRenum;
115 
116  unsigned index = 0;
117 
118  for (IndexList::iterator I = indexList.begin(), E = indexList.end();
119  I != E; ++I) {
120  I->setIndex(index);
121  index += SlotIndex::InstrDist;
122  }
123 }
124 
125 // Renumber indexes locally after curItr was inserted, but failed to get a new
126 // index.
127 void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
128  // Number indexes with half the default spacing so we can catch up quickly.
129  const unsigned Space = SlotIndex::InstrDist/2;
130  assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM");
131 
132  IndexList::iterator startItr = prior(curItr);
133  unsigned index = startItr->getIndex();
134  do {
135  curItr->setIndex(index += Space);
136  ++curItr;
137  // If the next index is bigger, we have caught up.
138  } while (curItr != indexList.end() && curItr->getIndex() <= index);
139 
140  DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
141  << index << " ***\n");
142  ++NumLocalRenum;
143 }
144 
145 // Repair indexes after adding and removing instructions.
149  // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
150  // does the same thing.
151  // Find anchor points, which are at the beginning/end of blocks or at
152  // instructions that already have indexes.
153  while (Begin != MBB->begin() && !hasIndex(Begin))
154  --Begin;
155  while (End != MBB->end() && !hasIndex(End))
156  ++End;
157 
158  bool includeStart = (Begin == MBB->begin());
159  SlotIndex startIdx;
160  if (includeStart)
161  startIdx = getMBBStartIdx(MBB);
162  else
163  startIdx = getInstructionIndex(Begin);
164 
165  SlotIndex endIdx;
166  if (End == MBB->end())
167  endIdx = getMBBEndIdx(MBB);
168  else
169  endIdx = getInstructionIndex(End);
170 
171  // FIXME: Conceptually, this code is implementing an iterator on MBB that
172  // optionally includes an additional position prior to MBB->begin(), indicated
173  // by the includeStart flag. This is done so that we can iterate MIs in a MBB
174  // in parallel with SlotIndexes, but there should be a better way to do this.
175  IndexList::iterator ListB = startIdx.listEntry();
176  IndexList::iterator ListI = endIdx.listEntry();
177  MachineBasicBlock::iterator MBBI = End;
178  bool pastStart = false;
179  while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
180  assert(ListI->getIndex() >= startIdx.getIndex() &&
181  (includeStart || !pastStart) &&
182  "Decremented past the beginning of region to repair.");
183 
184  MachineInstr *SlotMI = ListI->getInstr();
185  MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? MBBI : 0;
186  bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
187 
188  if (SlotMI == MI && !MBBIAtBegin) {
189  --ListI;
190  if (MBBI != Begin)
191  --MBBI;
192  else
193  pastStart = true;
194  } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
195  if (MBBI != Begin)
196  --MBBI;
197  else
198  pastStart = true;
199  } else {
200  --ListI;
201  if (SlotMI)
203  }
204  }
205 
206  // In theory this could be combined with the previous loop, but it is tricky
207  // to update the IndexList while we are iterating it.
208  for (MachineBasicBlock::iterator I = End; I != Begin;) {
209  --I;
210  MachineInstr *MI = I;
211  if (!MI->isDebugValue() && mi2iMap.find(MI) == mi2iMap.end())
213  }
214 }
215 
216 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
217 void SlotIndexes::dump() const {
218  for (IndexList::const_iterator itr = indexList.begin();
219  itr != indexList.end(); ++itr) {
220  dbgs() << itr->getIndex() << " ";
221 
222  if (itr->getInstr() != 0) {
223  dbgs() << *itr->getInstr();
224  } else {
225  dbgs() << "\n";
226  }
227  }
228 
229  for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
230  dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
231  << MBBRanges[i].second << ")\n";
232 }
233 #endif
234 
235 // Print a SlotIndex to a raw_ostream.
236 void SlotIndex::print(raw_ostream &os) const {
237  if (isValid())
238  os << listEntry()->getIndex() << "Berd"[getSlot()];
239  else
240  os << "invalid";
241 }
242 
243 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
244 // Dump a SlotIndex to stderr.
245 void SlotIndex::dump() const {
246  print(dbgs());
247  dbgs() << "\n";
248 }
249 #endif
250 
void push_back(const T &Elt)
Definition: SmallVector.h:236
void renumberIndexes()
Renumber the index list, providing space for new instructions.
void reserve(unsigned N)
Definition: SmallVector.h:425
std::pair< SlotIndex, MachineBasicBlock * > IdxMBBPair
Definition: SlotIndexes.h:315
iplist< IndexListEntry >::iterator iterator
Definition: ilist.h:642
virtual bool runOnMachineFunction(MachineFunction &fn)
Definition: SlotIndexes.cpp:41
void print(raw_ostream &OS, SlotIndexes *=0) const
iterator begin()
Definition: ilist.h:359
unsigned getNumBlockIDs() const
virtual void releaseMemory()
Definition: SlotIndexes.cpp:33
void clear()
Definition: ilist.h:550
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
bool isDebugValue() const
Definition: MachineInstr.h:639
bundle_iterator< MachineInstr, instr_iterator > iterator
unsigned getIndex() const
Definition: SlotIndexes.h:51
bool isValid() const
Definition: SlotIndexes.h:160
void dump() const
Dump the indexes.
iterator end()
Definition: DenseMap.h:57
unsigned size() const
SlotIndex getInstructionIndex(const MachineInstr *MI) const
Returns the base index for the given instruction.
Definition: SlotIndexes.h:414
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:153
bool hasIndex(const MachineInstr *instr) const
Definition: SlotIndexes.h:409
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: ilist.h:385
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
SlotIndex getMBBEndIdx(unsigned Num) const
Returns the last index in the given basic block number.
Definition: SlotIndexes.h:496
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.
static char ID
Definition: SlotIndexes.h:374
virtual void getAnalysisUsage(AnalysisUsage &AU) const
void dump() const
Dump this index to stderr.
#define I(x, y, z)
Definition: MD5.cpp:54
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: DenseMap.h:67
STATISTIC(NumGlobalRenum,"Number of global renumberings")
void removeMachineInstrFromMaps(MachineInstr *mi)
Remove the given machine instruction from the mapping.
Definition: SlotIndexes.h:610
SlotIndex getMBBStartIdx(unsigned Num) const
Returns the first index in the given basic block number.
Definition: SlotIndexes.h:486
reference back()
Definition: ilist.h:398
iterator end()
Definition: ilist.h:367
BasicBlockListType::iterator iterator
ItTy prior(ItTy it, Dist n)
Definition: STLExtras.h:167
#define DEBUG(X)
Definition: Debug.h:97
INITIALIZE_PASS(SlotIndexes,"slotindexes","Slot index numbering", false, false) STATISTIC(NumLocalRenum
iterator find(const KeyT &Val)
Definition: DenseMap.h:108
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:92
SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late=false)
Definition: SlotIndexes.h:569
Number of local renumberings
Definition: SlotIndexes.cpp:25
void push_back(const NodeTy &val)
Definition: ilist.h:671