LCOV - code coverage report
Current view: top level - lib/CodeGen - SlotIndexes.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 69 75 92.0 %
Date: 2018-02-23 15:42:53 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      10             : #include "llvm/CodeGen/SlotIndexes.h"
      11             : #include "llvm/ADT/Statistic.h"
      12             : #include "llvm/CodeGen/MachineFunction.h"
      13             : #include "llvm/Support/Debug.h"
      14             : #include "llvm/Support/raw_ostream.h"
      15             : 
      16             : using namespace llvm;
      17             : 
      18             : #define DEBUG_TYPE "slotindexes"
      19             : 
      20             : char SlotIndexes::ID = 0;
      21      920624 : INITIALIZE_PASS(SlotIndexes, DEBUG_TYPE,
      22             :                 "Slot index numbering", false, false)
      23             : 
      24             : STATISTIC(NumLocalRenum,  "Number of local renumberings");
      25             : STATISTIC(NumGlobalRenum, "Number of global renumberings");
      26             : 
      27       37857 : void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const {
      28             :   au.setPreservesAll();
      29       37857 :   MachineFunctionPass::getAnalysisUsage(au);
      30       37857 : }
      31             : 
      32      345079 : void SlotIndexes::releaseMemory() {
      33      345079 :   mi2iMap.clear();
      34             :   MBBRanges.clear();
      35             :   idx2MBBMap.clear();
      36             :   indexList.clear();
      37      345079 :   ileAllocator.Reset();
      38      345079 : }
      39             : 
      40      345029 : bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) {
      41             : 
      42             :   // Compute numbering as follows:
      43             :   // Grab an iterator to the start of the index list.
      44             :   // Iterate over all MBBs, and within each MBB all MIs, keeping the MI
      45             :   // iterator in lock-step (though skipping it over indexes which have
      46             :   // null pointers in the instruction field).
      47             :   // At each iteration assert that the instruction pointed to in the index
      48             :   // is the same one pointed to by the MI iterator. This
      49             : 
      50             :   // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should
      51             :   // only need to be set up once after the first numbering is computed.
      52             : 
      53      345029 :   mf = &fn;
      54             : 
      55             :   // Check that the list contains only the sentinal.
      56             :   assert(indexList.empty() && "Index list non-empty at initial numbering?");
      57             :   assert(idx2MBBMap.empty() &&
      58             :          "Index -> MBB mapping non-empty at initial numbering?");
      59             :   assert(MBBRanges.empty() &&
      60             :          "MBB -> Index mapping non-empty at initial numbering?");
      61             :   assert(mi2iMap.empty() &&
      62             :          "MachineInstr -> Index mapping non-empty at initial numbering?");
      63             : 
      64             :   unsigned index = 0;
      65      690058 :   MBBRanges.resize(mf->getNumBlockIDs());
      66      690056 :   idx2MBBMap.reserve(mf->size());
      67             : 
      68             :   indexList.push_back(createEntry(nullptr, index));
      69             : 
      70             :   // Iterate over the function.
      71     1345390 :   for (MachineBasicBlock &MBB : *mf) {
      72             :     // Insert an index for the MBB start.
      73             :     SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block);
      74             : 
      75    10610659 :     for (MachineInstr &MI : MBB) {
      76     9299993 :       if (MI.isDebugValue())
      77       53491 :         continue;
      78             : 
      79             :       // Insert a store index for the instr.
      80     9246502 :       indexList.push_back(createEntry(&MI, index += SlotIndex::InstrDist));
      81             : 
      82             :       // Save this base index in the maps.
      83     9246502 :       mi2iMap.insert(std::make_pair(
      84    18493004 :           &MI, SlotIndex(&indexList.back(), SlotIndex::Slot_Block)));
      85             :     }
      86             : 
      87             :     // We insert one blank instructions between basic blocks.
      88      655333 :     indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist));
      89             : 
      90     1310664 :     MBBRanges[MBB.getNumber()].first = blockStartIndex;
      91     1310664 :     MBBRanges[MBB.getNumber()].second = SlotIndex(&indexList.back(),
      92             :                                                    SlotIndex::Slot_Block);
      93     1310664 :     idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, &MBB));
      94             :   }
      95             : 
      96             :   // Sort the Idx2MBBMap
      97             :   std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
      98             : 
      99             :   DEBUG(mf->print(dbgs(), this));
     100             : 
     101             :   // And we're done!
     102      345029 :   return false;
     103             : }
     104             : 
     105      937551 : void SlotIndexes::removeMachineInstrFromMaps(MachineInstr &MI) {
     106             :   assert(!MI.isBundledWithPred() &&
     107             :          "Use removeSingleMachineInstrFromMaps() instread");
     108      937551 :   Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
     109      937551 :   if (mi2iItr == mi2iMap.end())
     110           0 :     return;
     111             : 
     112      937551 :   SlotIndex MIIndex = mi2iItr->second;
     113             :   IndexListEntry &MIEntry = *MIIndex.listEntry();
     114             :   assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
     115             :   mi2iMap.erase(mi2iItr);
     116             :   // FIXME: Eventually we want to actually delete these indexes.
     117             :   MIEntry.setInstr(nullptr);
     118             : }
     119             : 
     120      608916 : void SlotIndexes::removeSingleMachineInstrFromMaps(MachineInstr &MI) {
     121      608916 :   Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
     122      608916 :   if (mi2iItr == mi2iMap.end())
     123           6 :     return;
     124             : 
     125      608916 :   SlotIndex MIIndex = mi2iItr->second;
     126             :   IndexListEntry &MIEntry = *MIIndex.listEntry();
     127             :   assert(MIEntry.getInstr() == &MI && "Instruction indexes broken.");
     128             :   mi2iMap.erase(mi2iItr);
     129             : 
     130             :   // When removing the first instruction of a bundle update mapping to next
     131             :   // instruction.
     132      608916 :   if (MI.isBundledWithSucc()) {
     133             :     // Only the first instruction of a bundle should have an index assigned.
     134             :     assert(!MI.isBundledWithPred() && "Should have first bundle isntruction");
     135             : 
     136           6 :     MachineBasicBlock::instr_iterator Next = std::next(MI.getIterator());
     137             :     MachineInstr &NextMI = *Next;
     138             :     MIEntry.setInstr(&NextMI);
     139           6 :     mi2iMap.insert(std::make_pair(&NextMI, MIIndex));
     140             :     return;
     141             :   } else {
     142             :     // FIXME: Eventually we want to actually delete these indexes.
     143             :     MIEntry.setInstr(nullptr);
     144             :   }
     145             : }
     146             : 
     147           0 : void SlotIndexes::renumberIndexes() {
     148             :   // Renumber updates the index of every element of the index list.
     149             :   DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n");
     150             :   ++NumGlobalRenum;
     151             : 
     152             :   unsigned index = 0;
     153             : 
     154             :   for (IndexList::iterator I = indexList.begin(), E = indexList.end();
     155           0 :        I != E; ++I) {
     156             :     I->setIndex(index);
     157           0 :     index += SlotIndex::InstrDist;
     158             :   }
     159           0 : }
     160             : 
     161             : // Renumber indexes locally after curItr was inserted, but failed to get a new
     162             : // index.
     163       52760 : void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
     164             :   // Number indexes with half the default spacing so we can catch up quickly.
     165             :   const unsigned Space = SlotIndex::InstrDist/2;
     166             :   static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
     167             : 
     168             :   IndexList::iterator startItr = std::prev(curItr);
     169       52760 :   unsigned index = startItr->getIndex();
     170             :   do {
     171     4539068 :     curItr->setIndex(index += Space);
     172             :     ++curItr;
     173             :     // If the next index is bigger, we have caught up.
     174     4539068 :   } while (curItr != indexList.end() && curItr->getIndex() <= index);
     175             : 
     176             :   DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-'
     177             :                << index << " ***\n");
     178             :   ++NumLocalRenum;
     179       52760 : }
     180             : 
     181             : // Repair indexes after adding and removing instructions.
     182          21 : void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB,
     183             :                                        MachineBasicBlock::iterator Begin,
     184             :                                        MachineBasicBlock::iterator End) {
     185             :   // FIXME: Is this really necessary? The only caller repairIntervalsForRange()
     186             :   // does the same thing.
     187             :   // Find anchor points, which are at the beginning/end of blocks or at
     188             :   // instructions that already have indexes.
     189          21 :   while (Begin != MBB->begin() && !hasIndex(*Begin))
     190             :     --Begin;
     191          21 :   while (End != MBB->end() && !hasIndex(*End))
     192             :     ++End;
     193             : 
     194             :   bool includeStart = (Begin == MBB->begin());
     195             :   SlotIndex startIdx;
     196          21 :   if (includeStart)
     197             :     startIdx = getMBBStartIdx(MBB);
     198             :   else
     199          16 :     startIdx = getInstructionIndex(*Begin);
     200             : 
     201             :   SlotIndex endIdx;
     202          21 :   if (End == MBB->end())
     203             :     endIdx = getMBBEndIdx(MBB);
     204             :   else
     205          21 :     endIdx = getInstructionIndex(*End);
     206             : 
     207             :   // FIXME: Conceptually, this code is implementing an iterator on MBB that
     208             :   // optionally includes an additional position prior to MBB->begin(), indicated
     209             :   // by the includeStart flag. This is done so that we can iterate MIs in a MBB
     210             :   // in parallel with SlotIndexes, but there should be a better way to do this.
     211          21 :   IndexList::iterator ListB = startIdx.listEntry()->getIterator();
     212          21 :   IndexList::iterator ListI = endIdx.listEntry()->getIterator();
     213          21 :   MachineBasicBlock::iterator MBBI = End;
     214             :   bool pastStart = false;
     215         192 :   while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) {
     216             :     assert(ListI->getIndex() >= startIdx.getIndex() &&
     217             :            (includeStart || !pastStart) &&
     218             :            "Decremented past the beginning of region to repair.");
     219             : 
     220         150 :     MachineInstr *SlotMI = ListI->getInstr();
     221         150 :     MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? &*MBBI : nullptr;
     222         150 :     bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart);
     223             : 
     224         150 :     if (SlotMI == MI && !MBBIAtBegin) {
     225             :       --ListI;
     226          26 :       if (MBBI != Begin)
     227             :         --MBBI;
     228             :       else
     229             :         pastStart = true;
     230         351 :     } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) {
     231         103 :       if (MBBI != Begin)
     232             :         --MBBI;
     233             :       else
     234             :         pastStart = true;
     235             :     } else {
     236             :       --ListI;
     237          21 :       if (SlotMI)
     238          21 :         removeMachineInstrFromMaps(*SlotMI);
     239             :     }
     240             :   }
     241             : 
     242             :   // In theory this could be combined with the previous loop, but it is tricky
     243             :   // to update the IndexList while we are iterating it.
     244         166 :   for (MachineBasicBlock::iterator I = End; I != Begin;) {
     245             :     --I;
     246             :     MachineInstr &MI = *I;
     247         248 :     if (!MI.isDebugValue() && mi2iMap.find(&MI) == mi2iMap.end())
     248         103 :       insertMachineInstrInMaps(MI);
     249             :   }
     250          21 : }
     251             : 
     252             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     253             : LLVM_DUMP_METHOD void SlotIndexes::dump() const {
     254             :   for (IndexList::const_iterator itr = indexList.begin();
     255             :        itr != indexList.end(); ++itr) {
     256             :     dbgs() << itr->getIndex() << " ";
     257             : 
     258             :     if (itr->getInstr()) {
     259             :       dbgs() << *itr->getInstr();
     260             :     } else {
     261             :       dbgs() << "\n";
     262             :     }
     263             :   }
     264             : 
     265             :   for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i)
     266             :     dbgs() << "%bb." << i << "\t[" << MBBRanges[i].first << ';'
     267             :            << MBBRanges[i].second << ")\n";
     268             : }
     269             : #endif
     270             : 
     271             : // Print a SlotIndex to a raw_ostream.
     272        6204 : void SlotIndex::print(raw_ostream &os) const {
     273        6204 :   if (isValid())
     274       12408 :     os << listEntry()->getIndex() << "Berd"[getSlot()];
     275             :   else
     276           0 :     os << "invalid";
     277        6204 : }
     278             : 
     279             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     280             : // Dump a SlotIndex to stderr.
     281             : LLVM_DUMP_METHOD void SlotIndex::dump() const {
     282             :   print(dbgs());
     283             :   dbgs() << "\n";
     284             : }
     285             : #endif
     286             : 

Generated by: LCOV version 1.13