SlotIndexes.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include "llvm/CodeGen/SlotIndexes.h"
  10. #include "llvm/ADT/Statistic.h"
  11. #include "llvm/CodeGen/MachineFunction.h"
  12. #include "llvm/Support/Debug.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. #include "llvm/Target/TargetInstrInfo.h"
  15. using namespace llvm;
  16. #define DEBUG_TYPE "slotindexes"
  17. char SlotIndexes::ID = 0;
  18. INITIALIZE_PASS(SlotIndexes, "slotindexes",
  19. "Slot index numbering", false, false)
  20. STATISTIC(NumLocalRenum, "Number of local renumberings");
  21. STATISTIC(NumGlobalRenum, "Number of global renumberings");
  22. void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
  23. au.setPreservesAll();
  24. MachineFunctionPass::getAnalysisUsage(au);
  25. }
  26. void SlotIndexes::releaseMemory() {
  27. mi2iMap.clear();
  28. MBBRanges.clear();
  29. idx2MBBMap.clear();
  30. indexList.clear();
  31. ileAllocator.Reset();
  32. }
  33. bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
  34. // Compute numbering as follows:
  35. // Grab an iterator to the start of the index list.
  36. // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
  37. // iterator in lock-step (though skipping it over indexes which have
  38. // null pointers in the instruction field).
  39. // At each iteration assert that the instruction pointed to in the index
  40. // is the same one pointed to by the MI iterator. This
  41. // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
  42. // only need to be set up once after the first numbering is computed.
  43. mf = &fn;
  44. // Check that the list contains only the sentinal.
  45. assert(indexList.empty() && "Index list non-empty at initial numbering?");
  46. assert(idx2MBBMap.empty() &&
  47. "Index -> MBB mapping non-empty at initial numbering?");
  48. assert(MBBRanges.empty() &&
  49. "MBB -> Index mapping non-empty at initial numbering?");
  50. assert(mi2iMap.empty() &&
  51. "MachineInstr -> Index mapping non-empty at initial numbering?");
  52. unsigned index = 0;
  53. MBBRanges.resize(mf->getNumBlockIDs());
  54. idx2MBBMap.reserve(mf->size());
  55. indexList.push_back(createEntry(nullptr, index));
  56. // Iterate over the function.
  57. for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end();
  58. mbbItr != mbbEnd; ++mbbItr) {
  59. MachineBasicBlock *mbb = &*mbbItr;
  60. // Insert an index for the MBB start.
  61. SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
  62. for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end();
  63. miItr != miEnd; ++miItr) {
  64. MachineInstr *mi = miItr;
  65. if (mi->isDebugValue())
  66. continue;
  67. // Insert a store index for the instr.
  68. indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist));
  69. // Save this base index in the maps.
  70. mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(),
  71. SlotIndex::Slot_Block)));
  72. }
  73. // We insert one blank instructions between basic blocks.
  74. indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
  75. MBBRanges[mbb->getNumber()].first = blockStartIndex;
  76. MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(),
  77. SlotIndex::Slot_Block);
  78. idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb));
  79. }
  80. // Sort the Idx2MBBMap
  81. std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
  82. DEBUG(mf->print(dbgs(), this));
  83. // And we're done!
  84. return false;
  85. }
  86. void SlotIndexes::renumberIndexes() {
  87. // Renumber updates the index of every element of the index list.
  88. DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
  89. ++NumGlobalRenum;
  90. unsigned index = 0;
  91. for (IndexList::iterator I = indexList.begin(), E = indexList.end();
  92. I != E; ++I) {
  93. I->setIndex(index);
  94. index += SlotIndex::InstrDist;
  95. }
  96. }
  97. // Renumber indexes locally after curItr was inserted, but failed to get a new
  98. // index.
  99. void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
  100. // Number indexes with half the default spacing so we can catch up quickly.
  101. const unsigned Space = SlotIndex::InstrDist/2;
  102. static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
  103. IndexList::iterator startItr = std::prev(curItr);
  104. unsigned index = startItr->getIndex();
  105. do {
  106. curItr->setIndex(index += Space);
  107. ++curItr;
  108. // If the next index is bigger, we have caught up.
  109. } while (curItr != indexList.end() && curItr->getIndex() <= index);
  110. DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
  111. << index << " ***\n");
  112. ++NumLocalRenum;
  113. }
  114. // Repair indexes after adding and removing instructions.
  115. void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
  116. MachineBasicBlock::iterator Begin,
  117. MachineBasicBlock::iterator End) {
  118. // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
  119. // does the same thing.
  120. // Find anchor points, which are at the beginning/end of blocks or at
  121. // instructions that already have indexes.
  122. while (Begin != MBB->begin() && !hasIndex(Begin))
  123. --Begin;
  124. while (End != MBB->end() && !hasIndex(End))
  125. ++End;
  126. bool includeStart = (Begin == MBB->begin());
  127. SlotIndex startIdx;
  128. if (includeStart)
  129. startIdx = getMBBStartIdx(MBB);
  130. else
  131. startIdx = getInstructionIndex(Begin);
  132. SlotIndex endIdx;
  133. if (End == MBB->end())
  134. endIdx = getMBBEndIdx(MBB);
  135. else
  136. endIdx = getInstructionIndex(End);
  137. // FIXME: Conceptually, this code is implementing an iterator on MBB that
  138. // optionally includes an additional position prior to MBB->begin(), indicated
  139. // by the includeStart flag. This is done so that we can iterate MIs in a MBB
  140. // in parallel with SlotIndexes, but there should be a better way to do this.
  141. IndexList::iterator ListB = startIdx.listEntry();
  142. IndexList::iterator ListI = endIdx.listEntry();
  143. MachineBasicBlock::iterator MBBI = End;
  144. bool pastStart = false;
  145. while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
  146. assert(ListI->getIndex() >= startIdx.getIndex() &&
  147. (includeStart || !pastStart) &&
  148. "Decremented past the beginning of region to repair.");
  149. MachineInstr *SlotMI = ListI->getInstr();
  150. MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? MBBI : nullptr;
  151. bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
  152. if (SlotMI == MI && !MBBIAtBegin) {
  153. --ListI;
  154. if (MBBI != Begin)
  155. --MBBI;
  156. else
  157. pastStart = true;
  158. } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
  159. if (MBBI != Begin)
  160. --MBBI;
  161. else
  162. pastStart = true;
  163. } else {
  164. --ListI;
  165. if (SlotMI)
  166. removeMachineInstrFromMaps(SlotMI);
  167. }
  168. }
  169. // In theory this could be combined with the previous loop, but it is tricky
  170. // to update the IndexList while we are iterating it.
  171. for (MachineBasicBlock::iterator I = End; I != Begin;) {
  172. --I;
  173. MachineInstr *MI = I;
  174. if (!MI->isDebugValue() && mi2iMap.find(MI) == mi2iMap.end())
  175. insertMachineInstrInMaps(MI);
  176. }
  177. }
  178. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  179. void SlotIndexes::dump() const {
  180. for (IndexList::const_iterator itr = indexList.begin();
  181. itr != indexList.end(); ++itr) {
  182. dbgs() << itr->getIndex() << " ";
  183. if (itr->getInstr()) {
  184. dbgs() << *itr->getInstr();
  185. } else {
  186. dbgs() << "\n";
  187. }
  188. }
  189. for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
  190. dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';'
  191. << MBBRanges[i].second << ")\n";
  192. }
  193. #endif
  194. // Print a SlotIndex to a raw_ostream.
  195. void SlotIndex::print(raw_ostream &os) const {
  196. if (isValid())
  197. os << listEntry()->getIndex() << "Berd"[getSlot()];
  198. else
  199. os << "invalid";
  200. }
  201. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  202. // Dump a SlotIndex to stderr.
  203. void SlotIndex::dump() const {
  204. print(dbgs());
  205. dbgs() << "\n";
  206. }
  207. #endif