SlotIndexes.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "llvm/CodeGen/SlotIndexes.h"
  9. #include "llvm/ADT/Statistic.h"
  10. #include "llvm/CodeGen/MachineFunction.h"
  11. #include "llvm/Config/llvm-config.h"
  12. #include "llvm/InitializePasses.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. using namespace llvm;
  16. #define DEBUG_TYPE "slotindexes"
  17. char SlotIndexes::ID = 0;
  18. SlotIndexes::SlotIndexes() : MachineFunctionPass(ID) {
  19. initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
  20. }
  21. SlotIndexes::~SlotIndexes() {
  22. // The indexList's nodes are all allocated in the BumpPtrAllocator.
  23. indexList.clearAndLeakNodesUnsafely();
  24. }
  25. INITIALIZE_PASS(SlotIndexes, DEBUG_TYPE,
  26. "Slot index numbering", false, false)
  27. STATISTIC(NumLocalRenum, "Number of local renumberings");
  28. void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
  29. au.setPreservesAll();
  30. MachineFunctionPass::getAnalysisUsage(au);
  31. }
  32. void SlotIndexes::releaseMemory() {
  33. mi2iMap.clear();
  34. MBBRanges.clear();
  35. idx2MBBMap.clear();
  36. indexList.clear();
  37. ileAllocator.Reset();
  38. }
  39. bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
  40. // Compute numbering as follows:
  41. // Grab an iterator to the start of the index list.
  42. // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
  43. // iterator in lock-step (though skipping it over indexes which have
  44. // null pointers in the instruction field).
  45. // At each iteration assert that the instruction pointed to in the index
  46. // is the same one pointed to by the MI iterator. This
  47. // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
  48. // only need to be set up once after the first numbering is computed.
  49. mf = &fn;
  50. // Check that the list contains only the sentinal.
  51. assert(indexList.empty() && "Index list non-empty at initial numbering?");
  52. assert(idx2MBBMap.empty() &&
  53. "Index -> MBB mapping non-empty at initial numbering?");
  54. assert(MBBRanges.empty() &&
  55. "MBB -> Index mapping non-empty at initial numbering?");
  56. assert(mi2iMap.empty() &&
  57. "MachineInstr -> Index mapping non-empty at initial numbering?");
  58. unsigned index = 0;
  59. MBBRanges.resize(mf->getNumBlockIDs());
  60. idx2MBBMap.reserve(mf->size());
  61. indexList.push_back(createEntry(nullptr, index));
  62. // Iterate over the function.
  63. for (MachineBasicBlock &MBB : *mf) {
  64. // Insert an index for the MBB start.
  65. SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
  66. for (MachineInstr &MI : MBB) {
  67. if (MI.isDebugOrPseudoInstr())
  68. continue;
  69. // Insert a store index for the instr.
  70. indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
  71. // Save this base index in the maps.
  72. mi2iMap.insert(std::make_pair(
  73. &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
  74. }
  75. // We insert one blank instructions between basic blocks.
  76. indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
  77. MBBRanges[MBB.getNumber()].first = blockStartIndex;
  78. MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
  79. SlotIndex::Slot_Block);
  80. idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
  81. }
  82. // Sort the Idx2MBBMap
  83. llvm::sort(idx2MBBMap, less_first());
  84. LLVM_DEBUG(mf->print(dbgs(), this));
  85. // And we're done!
  86. return false;
  87. }
  88. void SlotIndexes::removeMachineInstrFromMaps(MachineInstr &MI,
  89. bool AllowBundled) {
  90. assert((AllowBundled || !MI.isBundledWithPred()) &&
  91. "Use removeSingleMachineInstrFromMaps() instead");
  92. Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
  93. if (mi2iItr == mi2iMap.end())
  94. return;
  95. SlotIndex MIIndex = mi2iItr->second;
  96. IndexListEntry &MIEntry = *MIIndex.listEntry();
  97. assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
  98. mi2iMap.erase(mi2iItr);
  99. // FIXME: Eventually we want to actually delete these indexes.
  100. MIEntry.setInstr(nullptr);
  101. }
  102. void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) {
  103. Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
  104. if (mi2iItr == mi2iMap.end())
  105. return;
  106. SlotIndex MIIndex = mi2iItr->second;
  107. IndexListEntry &MIEntry = *MIIndex.listEntry();
  108. assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
  109. mi2iMap.erase(mi2iItr);
  110. // When removing the first instruction of a bundle update mapping to next
  111. // instruction.
  112. if (MI.isBundledWithSucc()) {
  113. // Only the first instruction of a bundle should have an index assigned.
  114. assert(!MI.isBundledWithPred() && "Should be first bundle instruction");
  115. MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
  116. MachineInstr &NextMI = *Next;
  117. MIEntry.setInstr(&NextMI);
  118. mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
  119. return;
  120. } else {
  121. // FIXME: Eventually we want to actually delete these indexes.
  122. MIEntry.setInstr(nullptr);
  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. static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
  131. IndexList::iterator startItr = std::prev(curItr);
  132. unsigned index = startItr->getIndex();
  133. do {
  134. curItr->setIndex(index += Space);
  135. ++curItr;
  136. // If the next index is bigger, we have caught up.
  137. } while (curItr != indexList.end() && curItr->getIndex() <= index);
  138. LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex()
  139. << '-' << index << " ***\n");
  140. ++NumLocalRenum;
  141. }
  142. // Repair indexes after adding and removing instructions.
  143. void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
  144. MachineBasicBlock::iterator Begin,
  145. MachineBasicBlock::iterator End) {
  146. bool includeStart = (Begin == MBB->begin());
  147. SlotIndex startIdx;
  148. if (includeStart)
  149. startIdx = getMBBStartIdx(MBB);
  150. else
  151. startIdx = getInstructionIndex(*--Begin);
  152. SlotIndex endIdx;
  153. if (End == MBB->end())
  154. endIdx = getMBBEndIdx(MBB);
  155. else
  156. endIdx = getInstructionIndex(*End);
  157. // FIXME: Conceptually, this code is implementing an iterator on MBB that
  158. // optionally includes an additional position prior to MBB->begin(), indicated
  159. // by the includeStart flag. This is done so that we can iterate MIs in a MBB
  160. // in parallel with SlotIndexes, but there should be a better way to do this.
  161. IndexList::iterator ListB = startIdx.listEntry()->getIterator();
  162. IndexList::iterator ListI = endIdx.listEntry()->getIterator();
  163. MachineBasicBlock::iterator MBBI = End;
  164. bool pastStart = false;
  165. while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
  166. assert(ListI->getIndex() >= startIdx.getIndex() &&
  167. (includeStart || !pastStart) &&
  168. "Decremented past the beginning of region to repair.");
  169. MachineInstr *SlotMI = ListI->getInstr();
  170. MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
  171. bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
  172. if (SlotMI == MI && !MBBIAtBegin) {
  173. --ListI;
  174. if (MBBI != Begin)
  175. --MBBI;
  176. else
  177. pastStart = true;
  178. } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
  179. if (MBBI != Begin)
  180. --MBBI;
  181. else
  182. pastStart = true;
  183. } else {
  184. --ListI;
  185. if (SlotMI)
  186. removeMachineInstrFromMaps(*SlotMI);
  187. }
  188. }
  189. // In theory this could be combined with the previous loop, but it is tricky
  190. // to update the IndexList while we are iterating it.
  191. for (MachineBasicBlock::iterator I = End; I != Begin;) {
  192. --I;
  193. MachineInstr &MI = *I;
  194. if (!MI.isDebugOrPseudoInstr() && mi2iMap.find(&MI) == mi2iMap.end())
  195. insertMachineInstrInMaps(MI);
  196. }
  197. }
  198. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  199. LLVM_DUMP_METHOD void SlotIndexes::dump() const {
  200. for (const IndexListEntry &ILE : indexList) {
  201. dbgs() << ILE.getIndex() << " ";
  202. if (ILE.getInstr()) {
  203. dbgs() << *ILE.getInstr();
  204. } else {
  205. dbgs() << "\n";
  206. }
  207. }
  208. for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
  209. dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
  210. << MBBRanges[i].second << ")\n";
  211. }
  212. #endif
  213. // Print a SlotIndex to a raw_ostream.
  214. void SlotIndex::print(raw_ostream &os) const {
  215. if (isValid())
  216. os << listEntry()->getIndex() << "Berd"[getSlot()];
  217. else
  218. os << "invalid";
  219. }
  220. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  221. // Dump a SlotIndex to stderr.
  222. LLVM_DUMP_METHOD void SlotIndex::dump() const {
  223. print(dbgs());
  224. dbgs() << "\n";
  225. }
  226. #endif