10b57cec5SDimitry Andric //===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 100b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 110b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 120b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 13480093f4SDimitry Andric #include "llvm/InitializePasses.h" 140b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 150b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric using namespace llvm; 180b57cec5SDimitry Andric 190b57cec5SDimitry Andric #define DEBUG_TYPE "slotindexes" 200b57cec5SDimitry Andric 21*0fca6ea1SDimitry Andric AnalysisKey SlotIndexesAnalysis::Key; 22480093f4SDimitry Andric 23*0fca6ea1SDimitry Andric SlotIndexesAnalysis::Result 24*0fca6ea1SDimitry Andric SlotIndexesAnalysis::run(MachineFunction &MF, 25*0fca6ea1SDimitry Andric MachineFunctionAnalysisManager &) { 26*0fca6ea1SDimitry Andric return Result(MF); 27*0fca6ea1SDimitry Andric } 28*0fca6ea1SDimitry Andric 29*0fca6ea1SDimitry Andric PreservedAnalyses 30*0fca6ea1SDimitry Andric SlotIndexesPrinterPass::run(MachineFunction &MF, 31*0fca6ea1SDimitry Andric MachineFunctionAnalysisManager &MFAM) { 32*0fca6ea1SDimitry Andric OS << "Slot indexes in machine function: " << MF.getName() << '\n'; 33*0fca6ea1SDimitry Andric MFAM.getResult<SlotIndexesAnalysis>(MF).print(OS); 34*0fca6ea1SDimitry Andric return PreservedAnalyses::all(); 35*0fca6ea1SDimitry Andric } 36*0fca6ea1SDimitry Andric char SlotIndexesWrapperPass::ID = 0; 37*0fca6ea1SDimitry Andric 38*0fca6ea1SDimitry Andric SlotIndexesWrapperPass::SlotIndexesWrapperPass() : MachineFunctionPass(ID) { 39*0fca6ea1SDimitry Andric initializeSlotIndexesWrapperPassPass(*PassRegistry::getPassRegistry()); 40480093f4SDimitry Andric } 41480093f4SDimitry Andric 42480093f4SDimitry Andric SlotIndexes::~SlotIndexes() { 43480093f4SDimitry Andric // The indexList's nodes are all allocated in the BumpPtrAllocator. 44*0fca6ea1SDimitry Andric indexList.clear(); 45480093f4SDimitry Andric } 46480093f4SDimitry Andric 47*0fca6ea1SDimitry Andric INITIALIZE_PASS(SlotIndexesWrapperPass, DEBUG_TYPE, "Slot index numbering", 48*0fca6ea1SDimitry Andric false, false) 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric STATISTIC(NumLocalRenum, "Number of local renumberings"); 510b57cec5SDimitry Andric 52*0fca6ea1SDimitry Andric void SlotIndexesWrapperPass::getAnalysisUsage(AnalysisUsage &au) const { 530b57cec5SDimitry Andric au.setPreservesAll(); 540b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(au); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 57*0fca6ea1SDimitry Andric void SlotIndexes::clear() { 580b57cec5SDimitry Andric mi2iMap.clear(); 590b57cec5SDimitry Andric MBBRanges.clear(); 600b57cec5SDimitry Andric idx2MBBMap.clear(); 610b57cec5SDimitry Andric indexList.clear(); 620b57cec5SDimitry Andric ileAllocator.Reset(); 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 65*0fca6ea1SDimitry Andric void SlotIndexes::analyze(MachineFunction &fn) { 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric // Compute numbering as follows: 680b57cec5SDimitry Andric // Grab an iterator to the start of the index list. 690b57cec5SDimitry Andric // Iterate over all MBBs, and within each MBB all MIs, keeping the MI 700b57cec5SDimitry Andric // iterator in lock-step (though skipping it over indexes which have 710b57cec5SDimitry Andric // null pointers in the instruction field). 720b57cec5SDimitry Andric // At each iteration assert that the instruction pointed to in the index 730b57cec5SDimitry Andric // is the same one pointed to by the MI iterator. This 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should 760b57cec5SDimitry Andric // only need to be set up once after the first numbering is computed. 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric mf = &fn; 790b57cec5SDimitry Andric 807a6dacacSDimitry Andric // Check that the list contains only the sentinel. 810b57cec5SDimitry Andric assert(indexList.empty() && "Index list non-empty at initial numbering?"); 820b57cec5SDimitry Andric assert(idx2MBBMap.empty() && 830b57cec5SDimitry Andric "Index -> MBB mapping non-empty at initial numbering?"); 840b57cec5SDimitry Andric assert(MBBRanges.empty() && 850b57cec5SDimitry Andric "MBB -> Index mapping non-empty at initial numbering?"); 860b57cec5SDimitry Andric assert(mi2iMap.empty() && 870b57cec5SDimitry Andric "MachineInstr -> Index mapping non-empty at initial numbering?"); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric unsigned index = 0; 900b57cec5SDimitry Andric MBBRanges.resize(mf->getNumBlockIDs()); 910b57cec5SDimitry Andric idx2MBBMap.reserve(mf->size()); 920b57cec5SDimitry Andric 93*0fca6ea1SDimitry Andric indexList.push_back(*createEntry(nullptr, index)); 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric // Iterate over the function. 960b57cec5SDimitry Andric for (MachineBasicBlock &MBB : *mf) { 970b57cec5SDimitry Andric // Insert an index for the MBB start. 980b57cec5SDimitry Andric SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block); 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 101fe6060f1SDimitry Andric if (MI.isDebugOrPseudoInstr()) 1020b57cec5SDimitry Andric continue; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric // Insert a store index for the instr. 105*0fca6ea1SDimitry Andric indexList.push_back(*createEntry(&MI, index += SlotIndex::InstrDist)); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Save this base index in the maps. 1080b57cec5SDimitry Andric mi2iMap.insert(std::make_pair( 1090b57cec5SDimitry Andric &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block))); 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric // We insert one blank instructions between basic blocks. 113*0fca6ea1SDimitry Andric indexList.push_back(*createEntry(nullptr, index += SlotIndex::InstrDist)); 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric MBBRanges[MBB.getNumber()].first = blockStartIndex; 1160b57cec5SDimitry Andric MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(), 1170b57cec5SDimitry Andric SlotIndex::Slot_Block); 1180b57cec5SDimitry Andric idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB)); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // Sort the Idx2MBBMap 1220b57cec5SDimitry Andric llvm::sort(idx2MBBMap, less_first()); 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric LLVM_DEBUG(mf->print(dbgs(), this)); 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric 1275ffd83dbSDimitry Andric void SlotIndexes::removeMachineInstrFromMaps(MachineInstr &MI, 1285ffd83dbSDimitry Andric bool AllowBundled) { 1295ffd83dbSDimitry Andric assert((AllowBundled || !MI.isBundledWithPred()) && 1305ffd83dbSDimitry Andric "Use removeSingleMachineInstrFromMaps() instead"); 1310b57cec5SDimitry Andric Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI); 1320b57cec5SDimitry Andric if (mi2iItr == mi2iMap.end()) 1330b57cec5SDimitry Andric return; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric SlotIndex MIIndex = mi2iItr->second; 1360b57cec5SDimitry Andric IndexListEntry &MIEntry = *MIIndex.listEntry(); 1370b57cec5SDimitry Andric assert(MIEntry.getInstr() == &MI && "Instruction indexes broken."); 1380b57cec5SDimitry Andric mi2iMap.erase(mi2iItr); 1390b57cec5SDimitry Andric // FIXME: Eventually we want to actually delete these indexes. 1400b57cec5SDimitry Andric MIEntry.setInstr(nullptr); 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) { 1440b57cec5SDimitry Andric Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI); 1450b57cec5SDimitry Andric if (mi2iItr == mi2iMap.end()) 1460b57cec5SDimitry Andric return; 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric SlotIndex MIIndex = mi2iItr->second; 1490b57cec5SDimitry Andric IndexListEntry &MIEntry = *MIIndex.listEntry(); 1500b57cec5SDimitry Andric assert(MIEntry.getInstr() == &MI && "Instruction indexes broken."); 1510b57cec5SDimitry Andric mi2iMap.erase(mi2iItr); 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric // When removing the first instruction of a bundle update mapping to next 1540b57cec5SDimitry Andric // instruction. 1550b57cec5SDimitry Andric if (MI.isBundledWithSucc()) { 1560b57cec5SDimitry Andric // Only the first instruction of a bundle should have an index assigned. 1575ffd83dbSDimitry Andric assert(!MI.isBundledWithPred() && "Should be first bundle instruction"); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator()); 1600b57cec5SDimitry Andric MachineInstr &NextMI = *Next; 1610b57cec5SDimitry Andric MIEntry.setInstr(&NextMI); 1620b57cec5SDimitry Andric mi2iMap.insert(std::make_pair(&NextMI, MIIndex)); 1630b57cec5SDimitry Andric return; 1640b57cec5SDimitry Andric } else { 1650b57cec5SDimitry Andric // FIXME: Eventually we want to actually delete these indexes. 1660b57cec5SDimitry Andric MIEntry.setInstr(nullptr); 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric // Renumber indexes locally after curItr was inserted, but failed to get a new 1710b57cec5SDimitry Andric // index. 1720b57cec5SDimitry Andric void SlotIndexes::renumberIndexes(IndexList::iterator curItr) { 1730b57cec5SDimitry Andric // Number indexes with half the default spacing so we can catch up quickly. 1740b57cec5SDimitry Andric const unsigned Space = SlotIndex::InstrDist/2; 1750b57cec5SDimitry Andric static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM"); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric IndexList::iterator startItr = std::prev(curItr); 1780b57cec5SDimitry Andric unsigned index = startItr->getIndex(); 1790b57cec5SDimitry Andric do { 1800b57cec5SDimitry Andric curItr->setIndex(index += Space); 1810b57cec5SDimitry Andric ++curItr; 1820b57cec5SDimitry Andric // If the next index is bigger, we have caught up. 1830b57cec5SDimitry Andric } while (curItr != indexList.end() && curItr->getIndex() <= index); 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() 1860b57cec5SDimitry Andric << '-' << index << " ***\n"); 1870b57cec5SDimitry Andric ++NumLocalRenum; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric // Repair indexes after adding and removing instructions. 1910b57cec5SDimitry Andric void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB, 1920b57cec5SDimitry Andric MachineBasicBlock::iterator Begin, 1930b57cec5SDimitry Andric MachineBasicBlock::iterator End) { 1940b57cec5SDimitry Andric bool includeStart = (Begin == MBB->begin()); 1950b57cec5SDimitry Andric SlotIndex startIdx; 1960b57cec5SDimitry Andric if (includeStart) 1970b57cec5SDimitry Andric startIdx = getMBBStartIdx(MBB); 1980b57cec5SDimitry Andric else 199fcaf7f86SDimitry Andric startIdx = getInstructionIndex(*--Begin); 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric SlotIndex endIdx; 2020b57cec5SDimitry Andric if (End == MBB->end()) 2030b57cec5SDimitry Andric endIdx = getMBBEndIdx(MBB); 2040b57cec5SDimitry Andric else 2050b57cec5SDimitry Andric endIdx = getInstructionIndex(*End); 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric // FIXME: Conceptually, this code is implementing an iterator on MBB that 2080b57cec5SDimitry Andric // optionally includes an additional position prior to MBB->begin(), indicated 2090b57cec5SDimitry Andric // by the includeStart flag. This is done so that we can iterate MIs in a MBB 2100b57cec5SDimitry Andric // in parallel with SlotIndexes, but there should be a better way to do this. 2110b57cec5SDimitry Andric IndexList::iterator ListB = startIdx.listEntry()->getIterator(); 2120b57cec5SDimitry Andric IndexList::iterator ListI = endIdx.listEntry()->getIterator(); 2130b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = End; 2140b57cec5SDimitry Andric bool pastStart = false; 2150b57cec5SDimitry Andric while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) { 2160b57cec5SDimitry Andric assert(ListI->getIndex() >= startIdx.getIndex() && 2170b57cec5SDimitry Andric (includeStart || !pastStart) && 2180b57cec5SDimitry Andric "Decremented past the beginning of region to repair."); 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric MachineInstr *SlotMI = ListI->getInstr(); 2210b57cec5SDimitry Andric MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr; 2220b57cec5SDimitry Andric bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric if (SlotMI == MI && !MBBIAtBegin) { 2250b57cec5SDimitry Andric --ListI; 2260b57cec5SDimitry Andric if (MBBI != Begin) 2270b57cec5SDimitry Andric --MBBI; 2280b57cec5SDimitry Andric else 2290b57cec5SDimitry Andric pastStart = true; 23006c3fb27SDimitry Andric } else if (MI && !mi2iMap.contains(MI)) { 2310b57cec5SDimitry Andric if (MBBI != Begin) 2320b57cec5SDimitry Andric --MBBI; 2330b57cec5SDimitry Andric else 2340b57cec5SDimitry Andric pastStart = true; 2350b57cec5SDimitry Andric } else { 2360b57cec5SDimitry Andric --ListI; 2370b57cec5SDimitry Andric if (SlotMI) 2380b57cec5SDimitry Andric removeMachineInstrFromMaps(*SlotMI); 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric // In theory this could be combined with the previous loop, but it is tricky 2430b57cec5SDimitry Andric // to update the IndexList while we are iterating it. 2440b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = End; I != Begin;) { 2450b57cec5SDimitry Andric --I; 2460b57cec5SDimitry Andric MachineInstr &MI = *I; 24706c3fb27SDimitry Andric if (!MI.isDebugOrPseudoInstr() && !mi2iMap.contains(&MI)) 2480b57cec5SDimitry Andric insertMachineInstrInMaps(MI); 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric 2525f757f3fSDimitry Andric void SlotIndexes::packIndexes() { 2535f757f3fSDimitry Andric for (auto [Index, Entry] : enumerate(indexList)) 2545f757f3fSDimitry Andric Entry.setIndex(Index * SlotIndex::InstrDist); 2555f757f3fSDimitry Andric } 2565f757f3fSDimitry Andric 257*0fca6ea1SDimitry Andric void SlotIndexes::print(raw_ostream &OS) const { 258fe6060f1SDimitry Andric for (const IndexListEntry &ILE : indexList) { 259*0fca6ea1SDimitry Andric OS << ILE.getIndex() << ' '; 2600b57cec5SDimitry Andric 261*0fca6ea1SDimitry Andric if (ILE.getInstr()) 262*0fca6ea1SDimitry Andric OS << *ILE.getInstr(); 263*0fca6ea1SDimitry Andric else 264*0fca6ea1SDimitry Andric OS << '\n'; 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i) 268*0fca6ea1SDimitry Andric OS << "%bb." << i << "\t[" << MBBRanges[i].first << ';' 2690b57cec5SDimitry Andric << MBBRanges[i].second << ")\n"; 2700b57cec5SDimitry Andric } 271*0fca6ea1SDimitry Andric 272*0fca6ea1SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 273*0fca6ea1SDimitry Andric LLVM_DUMP_METHOD void SlotIndexes::dump() const { print(dbgs()); } 2740b57cec5SDimitry Andric #endif 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric // Print a SlotIndex to a raw_ostream. 2770b57cec5SDimitry Andric void SlotIndex::print(raw_ostream &os) const { 2780b57cec5SDimitry Andric if (isValid()) 2790b57cec5SDimitry Andric os << listEntry()->getIndex() << "Berd"[getSlot()]; 2800b57cec5SDimitry Andric else 2810b57cec5SDimitry Andric os << "invalid"; 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2850b57cec5SDimitry Andric // Dump a SlotIndex to stderr. 2860b57cec5SDimitry Andric LLVM_DUMP_METHOD void SlotIndex::dump() const { 2870b57cec5SDimitry Andric print(dbgs()); 2880b57cec5SDimitry Andric dbgs() << "\n"; 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric #endif 291