SlotIndexes.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
  147. // does the same thing.
  148. // Find anchor points, which are at the beginning/end of blocks or at
  149. // instructions that already have indexes.
  150. while (Begin != MBB->begin() && !hasIndex(*Begin))
  151. --Begin;
  152. while (End != MBB->end() && !hasIndex(*End))
  153. ++End;
  154. bool includeStart = (Begin == MBB->begin());
  155. SlotIndex startIdx;
  156. if (includeStart)
  157. startIdx = getMBBStartIdx(MBB);
  158. else
  159. startIdx = getInstructionIndex(*Begin);
  160. SlotIndex endIdx;
  161. if (End == MBB->end())
  162. endIdx = getMBBEndIdx(MBB);
  163. else
  164. endIdx = getInstructionIndex(*End);
  165. // FIXME: Conceptually, this code is implementing an iterator on MBB that
  166. // optionally includes an additional position prior to MBB->begin(), indicated
  167. // by the includeStart flag. This is done so that we can iterate MIs in a MBB
  168. // in parallel with SlotIndexes, but there should be a better way to do this.
  169. IndexList::iterator ListB = startIdx.listEntry()->getIterator();
  170. IndexList::iterator ListI = endIdx.listEntry()->getIterator();
  171. MachineBasicBlock::iterator MBBI = End;
  172. bool pastStart = false;
  173. while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
  174. assert(ListI->getIndex() >= startIdx.getIndex() &&
  175. (includeStart || !pastStart) &&
  176. "Decremented past the beginning of region to repair.");
  177. MachineInstr *SlotMI = ListI->getInstr();
  178. MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
  179. bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
  180. if (SlotMI == MI && !MBBIAtBegin) {
  181. --ListI;
  182. if (MBBI != Begin)
  183. --MBBI;
  184. else
  185. pastStart = true;
  186. } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
  187. if (MBBI != Begin)
  188. --MBBI;
  189. else
  190. pastStart = true;
  191. } else {
  192. --ListI;
  193. if (SlotMI)
  194. removeMachineInstrFromMaps(*SlotMI);
  195. }
  196. }
  197. // In theory this could be combined with the previous loop, but it is tricky
  198. // to update the IndexList while we are iterating it.
  199. for (MachineBasicBlock::iterator I = End; I != Begin;) {
  200. --I;
  201. MachineInstr &MI = *I;
  202. if (!MI.isDebugOrPseudoInstr() && mi2iMap.find(&MI) == mi2iMap.end())
  203. insertMachineInstrInMaps(MI);
  204. }
  205. }
  206. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  207. LLVM_DUMP_METHOD void SlotIndexes::dump() const {
  208. for (const IndexListEntry &ILE : indexList) {
  209. dbgs() << ILE.getIndex() << " ";
  210. if (ILE.getInstr()) {
  211. dbgs() << *ILE.getInstr();
  212. } else {
  213. dbgs() << "\n";
  214. }
  215. }
  216. for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
  217. dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
  218. << MBBRanges[i].second << ")\n";
  219. }
  220. #endif
  221. // Print a SlotIndex to a raw_ostream.
  222. void SlotIndex::print(raw_ostream &os) const {
  223. if (isValid())
  224. os << listEntry()->getIndex() << "Berd"[getSlot()];
  225. else
  226. os << "invalid";
  227. }
  228. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  229. // Dump a SlotIndex to stderr.
  230. LLVM_DUMP_METHOD void SlotIndex::dump() const {
  231. print(dbgs());
  232. dbgs() << "\n";
  233. }
  234. #endif