SlotIndexes.cpp 9.0 KB

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