LLVM API Documentation
00001 //===-- SlotIndexes.cpp - Slot Indexes Pass ------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm/CodeGen/SlotIndexes.h" 00011 #include "llvm/ADT/Statistic.h" 00012 #include "llvm/CodeGen/MachineFunction.h" 00013 #include "llvm/Support/Debug.h" 00014 #include "llvm/Support/raw_ostream.h" 00015 #include "llvm/Target/TargetInstrInfo.h" 00016 00017 using namespace llvm; 00018 00019 #define DEBUG_TYPE "slotindexes" 00020 00021 char SlotIndexes::ID = 0; 00022 INITIALIZE_PASS(SlotIndexes, "slotindexes", 00023 "Slot index numbering", false, false) 00024 00025 STATISTIC(NumLocalRenum, "Number of local renumberings"); 00026 STATISTIC(NumGlobalRenum, "Number of global renumberings"); 00027 00028 void SlotIndexes::getAnalysisUsage(AnalysisUsage &au) const { 00029 au.setPreservesAll(); 00030 MachineFunctionPass::getAnalysisUsage(au); 00031 } 00032 00033 void SlotIndexes::releaseMemory() { 00034 mi2iMap.clear(); 00035 MBBRanges.clear(); 00036 idx2MBBMap.clear(); 00037 indexList.clear(); 00038 ileAllocator.Reset(); 00039 } 00040 00041 bool SlotIndexes::runOnMachineFunction(MachineFunction &fn) { 00042 00043 // Compute numbering as follows: 00044 // Grab an iterator to the start of the index list. 00045 // Iterate over all MBBs, and within each MBB all MIs, keeping the MI 00046 // iterator in lock-step (though skipping it over indexes which have 00047 // null pointers in the instruction field). 00048 // At each iteration assert that the instruction pointed to in the index 00049 // is the same one pointed to by the MI iterator. This 00050 00051 // FIXME: This can be simplified. The mi2iMap_, Idx2MBBMap, etc. should 00052 // only need to be set up once after the first numbering is computed. 00053 00054 mf = &fn; 00055 00056 // Check that the list contains only the sentinal. 00057 assert(indexList.empty() && "Index list non-empty at initial numbering?"); 00058 assert(idx2MBBMap.empty() && 00059 "Index -> MBB mapping non-empty at initial numbering?"); 00060 assert(MBBRanges.empty() && 00061 "MBB -> Index mapping non-empty at initial numbering?"); 00062 assert(mi2iMap.empty() && 00063 "MachineInstr -> Index mapping non-empty at initial numbering?"); 00064 00065 unsigned index = 0; 00066 MBBRanges.resize(mf->getNumBlockIDs()); 00067 idx2MBBMap.reserve(mf->size()); 00068 00069 indexList.push_back(createEntry(nullptr, index)); 00070 00071 // Iterate over the function. 00072 for (MachineFunction::iterator mbbItr = mf->begin(), mbbEnd = mf->end(); 00073 mbbItr != mbbEnd; ++mbbItr) { 00074 MachineBasicBlock *mbb = &*mbbItr; 00075 00076 // Insert an index for the MBB start. 00077 SlotIndex blockStartIndex(&indexList.back(), SlotIndex::Slot_Block); 00078 00079 for (MachineBasicBlock::iterator miItr = mbb->begin(), miEnd = mbb->end(); 00080 miItr != miEnd; ++miItr) { 00081 MachineInstr *mi = miItr; 00082 if (mi->isDebugValue()) 00083 continue; 00084 00085 // Insert a store index for the instr. 00086 indexList.push_back(createEntry(mi, index += SlotIndex::InstrDist)); 00087 00088 // Save this base index in the maps. 00089 mi2iMap.insert(std::make_pair(mi, SlotIndex(&indexList.back(), 00090 SlotIndex::Slot_Block))); 00091 } 00092 00093 // We insert one blank instructions between basic blocks. 00094 indexList.push_back(createEntry(nullptr, index += SlotIndex::InstrDist)); 00095 00096 MBBRanges[mbb->getNumber()].first = blockStartIndex; 00097 MBBRanges[mbb->getNumber()].second = SlotIndex(&indexList.back(), 00098 SlotIndex::Slot_Block); 00099 idx2MBBMap.push_back(IdxMBBPair(blockStartIndex, mbb)); 00100 } 00101 00102 // Sort the Idx2MBBMap 00103 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare()); 00104 00105 DEBUG(mf->print(dbgs(), this)); 00106 00107 // And we're done! 00108 return false; 00109 } 00110 00111 void SlotIndexes::renumberIndexes() { 00112 // Renumber updates the index of every element of the index list. 00113 DEBUG(dbgs() << "\n*** Renumbering SlotIndexes ***\n"); 00114 ++NumGlobalRenum; 00115 00116 unsigned index = 0; 00117 00118 for (IndexList::iterator I = indexList.begin(), E = indexList.end(); 00119 I != E; ++I) { 00120 I->setIndex(index); 00121 index += SlotIndex::InstrDist; 00122 } 00123 } 00124 00125 // Renumber indexes locally after curItr was inserted, but failed to get a new 00126 // index. 00127 void SlotIndexes::renumberIndexes(IndexList::iterator curItr) { 00128 // Number indexes with half the default spacing so we can catch up quickly. 00129 const unsigned Space = SlotIndex::InstrDist/2; 00130 assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM"); 00131 00132 IndexList::iterator startItr = std::prev(curItr); 00133 unsigned index = startItr->getIndex(); 00134 do { 00135 curItr->setIndex(index += Space); 00136 ++curItr; 00137 // If the next index is bigger, we have caught up. 00138 } while (curItr != indexList.end() && curItr->getIndex() <= index); 00139 00140 DEBUG(dbgs() << "\n*** Renumbered SlotIndexes " << startItr->getIndex() << '-' 00141 << index << " ***\n"); 00142 ++NumLocalRenum; 00143 } 00144 00145 // Repair indexes after adding and removing instructions. 00146 void SlotIndexes::repairIndexesInRange(MachineBasicBlock *MBB, 00147 MachineBasicBlock::iterator Begin, 00148 MachineBasicBlock::iterator End) { 00149 // FIXME: Is this really necessary? The only caller repairIntervalsForRange() 00150 // does the same thing. 00151 // Find anchor points, which are at the beginning/end of blocks or at 00152 // instructions that already have indexes. 00153 while (Begin != MBB->begin() && !hasIndex(Begin)) 00154 --Begin; 00155 while (End != MBB->end() && !hasIndex(End)) 00156 ++End; 00157 00158 bool includeStart = (Begin == MBB->begin()); 00159 SlotIndex startIdx; 00160 if (includeStart) 00161 startIdx = getMBBStartIdx(MBB); 00162 else 00163 startIdx = getInstructionIndex(Begin); 00164 00165 SlotIndex endIdx; 00166 if (End == MBB->end()) 00167 endIdx = getMBBEndIdx(MBB); 00168 else 00169 endIdx = getInstructionIndex(End); 00170 00171 // FIXME: Conceptually, this code is implementing an iterator on MBB that 00172 // optionally includes an additional position prior to MBB->begin(), indicated 00173 // by the includeStart flag. This is done so that we can iterate MIs in a MBB 00174 // in parallel with SlotIndexes, but there should be a better way to do this. 00175 IndexList::iterator ListB = startIdx.listEntry(); 00176 IndexList::iterator ListI = endIdx.listEntry(); 00177 MachineBasicBlock::iterator MBBI = End; 00178 bool pastStart = false; 00179 while (ListI != ListB || MBBI != Begin || (includeStart && !pastStart)) { 00180 assert(ListI->getIndex() >= startIdx.getIndex() && 00181 (includeStart || !pastStart) && 00182 "Decremented past the beginning of region to repair."); 00183 00184 MachineInstr *SlotMI = ListI->getInstr(); 00185 MachineInstr *MI = (MBBI != MBB->end() && !pastStart) ? MBBI : nullptr; 00186 bool MBBIAtBegin = MBBI == Begin && (!includeStart || pastStart); 00187 00188 if (SlotMI == MI && !MBBIAtBegin) { 00189 --ListI; 00190 if (MBBI != Begin) 00191 --MBBI; 00192 else 00193 pastStart = true; 00194 } else if (MI && mi2iMap.find(MI) == mi2iMap.end()) { 00195 if (MBBI != Begin) 00196 --MBBI; 00197 else 00198 pastStart = true; 00199 } else { 00200 --ListI; 00201 if (SlotMI) 00202 removeMachineInstrFromMaps(SlotMI); 00203 } 00204 } 00205 00206 // In theory this could be combined with the previous loop, but it is tricky 00207 // to update the IndexList while we are iterating it. 00208 for (MachineBasicBlock::iterator I = End; I != Begin;) { 00209 --I; 00210 MachineInstr *MI = I; 00211 if (!MI->isDebugValue() && mi2iMap.find(MI) == mi2iMap.end()) 00212 insertMachineInstrInMaps(MI); 00213 } 00214 } 00215 00216 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 00217 void SlotIndexes::dump() const { 00218 for (IndexList::const_iterator itr = indexList.begin(); 00219 itr != indexList.end(); ++itr) { 00220 dbgs() << itr->getIndex() << " "; 00221 00222 if (itr->getInstr()) { 00223 dbgs() << *itr->getInstr(); 00224 } else { 00225 dbgs() << "\n"; 00226 } 00227 } 00228 00229 for (unsigned i = 0, e = MBBRanges.size(); i != e; ++i) 00230 dbgs() << "BB#" << i << "\t[" << MBBRanges[i].first << ';' 00231 << MBBRanges[i].second << ")\n"; 00232 } 00233 #endif 00234 00235 // Print a SlotIndex to a raw_ostream. 00236 void SlotIndex::print(raw_ostream &os) const { 00237 if (isValid()) 00238 os << listEntry()->getIndex() << "Berd"[getSlot()]; 00239 else 00240 os << "invalid"; 00241 } 00242 00243 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 00244 // Dump a SlotIndex to stderr. 00245 void SlotIndex::dump() const { 00246 print(dbgs()); 00247 dbgs() << "\n"; 00248 } 00249 #endif 00250