LLVM API Documentation

SlotIndexes.h
Go to the documentation of this file.
00001 //===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===//
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 // This file implements SlotIndex and related classes. The purpose of SlotIndex
00011 // is to describe a position at which a register can become live, or cease to
00012 // be live.
00013 //
00014 // SlotIndex is mostly a proxy for entries of the SlotIndexList, a class which
00015 // is held is LiveIntervals and provides the real numbering. This allows
00016 // LiveIntervals to perform largely transparent renumbering.
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_CODEGEN_SLOTINDEXES_H
00020 #define LLVM_CODEGEN_SLOTINDEXES_H
00021 
00022 #include "llvm/ADT/DenseMap.h"
00023 #include "llvm/ADT/IntervalMap.h"
00024 #include "llvm/ADT/PointerIntPair.h"
00025 #include "llvm/ADT/SmallVector.h"
00026 #include "llvm/ADT/ilist.h"
00027 #include "llvm/CodeGen/MachineFunction.h"
00028 #include "llvm/CodeGen/MachineFunctionPass.h"
00029 #include "llvm/CodeGen/MachineInstrBundle.h"
00030 #include "llvm/Support/Allocator.h"
00031 
00032 namespace llvm {
00033 
00034   /// This class represents an entry in the slot index list held in the
00035   /// SlotIndexes pass. It should not be used directly. See the
00036   /// SlotIndex & SlotIndexes classes for the public interface to this
00037   /// information.
00038   class IndexListEntry : public ilist_node<IndexListEntry> {
00039     MachineInstr *mi;
00040     unsigned index;
00041 
00042   public:
00043 
00044     IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
00045 
00046     MachineInstr* getInstr() const { return mi; }
00047     void setInstr(MachineInstr *mi) {
00048       this->mi = mi;
00049     }
00050 
00051     unsigned getIndex() const { return index; }
00052     void setIndex(unsigned index) {
00053       this->index = index;
00054     }
00055 
00056 #ifdef EXPENSIVE_CHECKS
00057     // When EXPENSIVE_CHECKS is defined, "erased" index list entries will
00058     // actually be moved to a "graveyard" list, and have their pointers
00059     // poisoned, so that dangling SlotIndex access can be reliably detected.
00060     void setPoison() {
00061       intptr_t tmp = reinterpret_cast<intptr_t>(mi);
00062       assert(((tmp & 0x1) == 0x0) && "Pointer already poisoned?");  
00063       tmp |= 0x1;
00064       mi = reinterpret_cast<MachineInstr*>(tmp);
00065     }
00066 
00067     bool isPoisoned() const { return (reinterpret_cast<intptr_t>(mi) & 0x1) == 0x1; }
00068 #endif // EXPENSIVE_CHECKS
00069 
00070   };
00071 
00072   template <>
00073   struct ilist_traits<IndexListEntry> : public ilist_default_traits<IndexListEntry> {
00074   private:
00075     mutable ilist_half_node<IndexListEntry> Sentinel;
00076   public:
00077     IndexListEntry *createSentinel() const {
00078       return static_cast<IndexListEntry*>(&Sentinel);
00079     }
00080     void destroySentinel(IndexListEntry *) const {}
00081 
00082     IndexListEntry *provideInitialHead() const { return createSentinel(); }
00083     IndexListEntry *ensureHead(IndexListEntry*) const { return createSentinel(); }
00084     static void noteHead(IndexListEntry*, IndexListEntry*) {}
00085     void deleteNode(IndexListEntry *N) {}
00086 
00087   private:
00088     void createNode(const IndexListEntry &);
00089   };
00090 
00091   /// SlotIndex - An opaque wrapper around machine indexes.
00092   class SlotIndex {
00093     friend class SlotIndexes;
00094 
00095     enum Slot {
00096       /// Basic block boundary.  Used for live ranges entering and leaving a
00097       /// block without being live in the layout neighbor.  Also used as the
00098       /// def slot of PHI-defs.
00099       Slot_Block,
00100 
00101       /// Early-clobber register use/def slot.  A live range defined at
00102       /// Slot_EarlyCLobber interferes with normal live ranges killed at
00103       /// Slot_Register.  Also used as the kill slot for live ranges tied to an
00104       /// early-clobber def.
00105       Slot_EarlyClobber,
00106 
00107       /// Normal register use/def slot.  Normal instructions kill and define
00108       /// register live ranges at this slot.
00109       Slot_Register,
00110 
00111       /// Dead def kill point.  Kill slot for a live range that is defined by
00112       /// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't
00113       /// used anywhere.
00114       Slot_Dead,
00115 
00116       Slot_Count
00117     };
00118 
00119     PointerIntPair<IndexListEntry*, 2, unsigned> lie;
00120 
00121     SlotIndex(IndexListEntry *entry, unsigned slot)
00122       : lie(entry, slot) {}
00123 
00124     IndexListEntry* listEntry() const {
00125       assert(isValid() && "Attempt to compare reserved index.");
00126 #ifdef EXPENSIVE_CHECKS
00127       assert(!lie.getPointer()->isPoisoned() &&
00128              "Attempt to access deleted list-entry.");
00129 #endif // EXPENSIVE_CHECKS
00130       return lie.getPointer();
00131     }
00132 
00133     unsigned getIndex() const {
00134       return listEntry()->getIndex() | getSlot();
00135     }
00136 
00137     /// Returns the slot for this SlotIndex.
00138     Slot getSlot() const {
00139       return static_cast<Slot>(lie.getInt());
00140     }
00141 
00142   public:
00143     enum {
00144       /// The default distance between instructions as returned by distance().
00145       /// This may vary as instructions are inserted and removed.
00146       InstrDist = 4 * Slot_Count
00147     };
00148 
00149     /// Construct an invalid index.
00150     SlotIndex() : lie(0, 0) {}
00151 
00152     // Construct a new slot index from the given one, and set the slot.
00153     SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) {
00154       assert(lie.getPointer() != 0 &&
00155              "Attempt to construct index with 0 pointer.");
00156     }
00157 
00158     /// Returns true if this is a valid index. Invalid indicies do
00159     /// not point into an index table, and cannot be compared.
00160     bool isValid() const {
00161       return lie.getPointer();
00162     }
00163 
00164     /// Return true for a valid index.
00165     LLVM_EXPLICIT operator bool() const { return isValid(); }
00166 
00167     /// Print this index to the given raw_ostream.
00168     void print(raw_ostream &os) const;
00169 
00170     /// Dump this index to stderr.
00171     void dump() const;
00172 
00173     /// Compare two SlotIndex objects for equality.
00174     bool operator==(SlotIndex other) const {
00175       return lie == other.lie;
00176     }
00177     /// Compare two SlotIndex objects for inequality.
00178     bool operator!=(SlotIndex other) const {
00179       return lie != other.lie;
00180     }
00181 
00182     /// Compare two SlotIndex objects. Return true if the first index
00183     /// is strictly lower than the second.
00184     bool operator<(SlotIndex other) const {
00185       return getIndex() < other.getIndex();
00186     }
00187     /// Compare two SlotIndex objects. Return true if the first index
00188     /// is lower than, or equal to, the second.
00189     bool operator<=(SlotIndex other) const {
00190       return getIndex() <= other.getIndex();
00191     }
00192 
00193     /// Compare two SlotIndex objects. Return true if the first index
00194     /// is greater than the second.
00195     bool operator>(SlotIndex other) const {
00196       return getIndex() > other.getIndex();
00197     }
00198 
00199     /// Compare two SlotIndex objects. Return true if the first index
00200     /// is greater than, or equal to, the second.
00201     bool operator>=(SlotIndex other) const {
00202       return getIndex() >= other.getIndex();
00203     }
00204 
00205     /// isSameInstr - Return true if A and B refer to the same instruction.
00206     static bool isSameInstr(SlotIndex A, SlotIndex B) {
00207       return A.lie.getPointer() == B.lie.getPointer();
00208     }
00209 
00210     /// isEarlierInstr - Return true if A refers to an instruction earlier than
00211     /// B. This is equivalent to A < B && !isSameInstr(A, B).
00212     static bool isEarlierInstr(SlotIndex A, SlotIndex B) {
00213       return A.listEntry()->getIndex() < B.listEntry()->getIndex();
00214     }
00215 
00216     /// Return the distance from this index to the given one.
00217     int distance(SlotIndex other) const {
00218       return other.getIndex() - getIndex();
00219     }
00220 
00221     /// isBlock - Returns true if this is a block boundary slot.
00222     bool isBlock() const { return getSlot() == Slot_Block; }
00223 
00224     /// isEarlyClobber - Returns true if this is an early-clobber slot.
00225     bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
00226 
00227     /// isRegister - Returns true if this is a normal register use/def slot.
00228     /// Note that early-clobber slots may also be used for uses and defs.
00229     bool isRegister() const { return getSlot() == Slot_Register; }
00230 
00231     /// isDead - Returns true if this is a dead def kill slot.
00232     bool isDead() const { return getSlot() == Slot_Dead; }
00233 
00234     /// Returns the base index for associated with this index. The base index
00235     /// is the one associated with the Slot_Block slot for the instruction
00236     /// pointed to by this index.
00237     SlotIndex getBaseIndex() const {
00238       return SlotIndex(listEntry(), Slot_Block);
00239     }
00240 
00241     /// Returns the boundary index for associated with this index. The boundary
00242     /// index is the one associated with the Slot_Block slot for the instruction
00243     /// pointed to by this index.
00244     SlotIndex getBoundaryIndex() const {
00245       return SlotIndex(listEntry(), Slot_Dead);
00246     }
00247 
00248     /// Returns the register use/def slot in the current instruction for a
00249     /// normal or early-clobber def.
00250     SlotIndex getRegSlot(bool EC = false) const {
00251       return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
00252     }
00253 
00254     /// Returns the dead def kill slot for the current instruction.
00255     SlotIndex getDeadSlot() const {
00256       return SlotIndex(listEntry(), Slot_Dead);
00257     }
00258 
00259     /// Returns the next slot in the index list. This could be either the
00260     /// next slot for the instruction pointed to by this index or, if this
00261     /// index is a STORE, the first slot for the next instruction.
00262     /// WARNING: This method is considerably more expensive than the methods
00263     /// that return specific slots (getUseIndex(), etc). If you can - please
00264     /// use one of those methods.
00265     SlotIndex getNextSlot() const {
00266       Slot s = getSlot();
00267       if (s == Slot_Dead) {
00268         return SlotIndex(listEntry()->getNextNode(), Slot_Block);
00269       }
00270       return SlotIndex(listEntry(), s + 1);
00271     }
00272 
00273     /// Returns the next index. This is the index corresponding to the this
00274     /// index's slot, but for the next instruction.
00275     SlotIndex getNextIndex() const {
00276       return SlotIndex(listEntry()->getNextNode(), getSlot());
00277     }
00278 
00279     /// Returns the previous slot in the index list. This could be either the
00280     /// previous slot for the instruction pointed to by this index or, if this
00281     /// index is a Slot_Block, the last slot for the previous instruction.
00282     /// WARNING: This method is considerably more expensive than the methods
00283     /// that return specific slots (getUseIndex(), etc). If you can - please
00284     /// use one of those methods.
00285     SlotIndex getPrevSlot() const {
00286       Slot s = getSlot();
00287       if (s == Slot_Block) {
00288         return SlotIndex(listEntry()->getPrevNode(), Slot_Dead);
00289       }
00290       return SlotIndex(listEntry(), s - 1);
00291     }
00292 
00293     /// Returns the previous index. This is the index corresponding to this
00294     /// index's slot, but for the previous instruction.
00295     SlotIndex getPrevIndex() const {
00296       return SlotIndex(listEntry()->getPrevNode(), getSlot());
00297     }
00298 
00299   };
00300 
00301   template <> struct isPodLike<SlotIndex> { static const bool value = true; };
00302 
00303   inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
00304     li.print(os);
00305     return os;
00306   }
00307 
00308   typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
00309 
00310   inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
00311     return V < IM.first;
00312   }
00313 
00314   inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
00315     return IM.first < V;
00316   }
00317 
00318   struct Idx2MBBCompare {
00319     bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
00320       return LHS.first < RHS.first;
00321     }
00322   };
00323 
00324   /// SlotIndexes pass.
00325   ///
00326   /// This pass assigns indexes to each instruction.
00327   class SlotIndexes : public MachineFunctionPass {
00328   private:
00329 
00330     typedef ilist<IndexListEntry> IndexList;
00331     IndexList indexList;
00332 
00333 #ifdef EXPENSIVE_CHECKS
00334     IndexList graveyardList;
00335 #endif // EXPENSIVE_CHECKS
00336 
00337     MachineFunction *mf;
00338 
00339     typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
00340     Mi2IndexMap mi2iMap;
00341 
00342     /// MBBRanges - Map MBB number to (start, stop) indexes.
00343     SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
00344 
00345     /// Idx2MBBMap - Sorted list of pairs of index of first instruction
00346     /// and MBB id.
00347     SmallVector<IdxMBBPair, 8> idx2MBBMap;
00348 
00349     // IndexListEntry allocator.
00350     BumpPtrAllocator ileAllocator;
00351 
00352     IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
00353       IndexListEntry *entry =
00354         static_cast<IndexListEntry*>(
00355           ileAllocator.Allocate(sizeof(IndexListEntry),
00356           alignOf<IndexListEntry>()));
00357 
00358       new (entry) IndexListEntry(mi, index);
00359 
00360       return entry;
00361     }
00362 
00363     /// Renumber locally after inserting curItr.
00364     void renumberIndexes(IndexList::iterator curItr);
00365 
00366   public:
00367     static char ID;
00368 
00369     SlotIndexes() : MachineFunctionPass(ID) {
00370       initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
00371     }
00372 
00373     virtual void getAnalysisUsage(AnalysisUsage &au) const;
00374     virtual void releaseMemory();
00375 
00376     virtual bool runOnMachineFunction(MachineFunction &fn);
00377 
00378     /// Dump the indexes.
00379     void dump() const;
00380 
00381     /// Renumber the index list, providing space for new instructions.
00382     void renumberIndexes();
00383 
00384     /// Repair indexes after adding and removing instructions.
00385     void repairIndexesInRange(MachineBasicBlock *MBB,
00386                               MachineBasicBlock::iterator Begin,
00387                               MachineBasicBlock::iterator End);
00388 
00389     /// Returns the zero index for this analysis.
00390     SlotIndex getZeroIndex() {
00391       assert(indexList.front().getIndex() == 0 && "First index is not 0?");
00392       return SlotIndex(&indexList.front(), 0);
00393     }
00394 
00395     /// Returns the base index of the last slot in this analysis.
00396     SlotIndex getLastIndex() {
00397       return SlotIndex(&indexList.back(), 0);
00398     }
00399 
00400     /// Returns true if the given machine instr is mapped to an index,
00401     /// otherwise returns false.
00402     bool hasIndex(const MachineInstr *instr) const {
00403       return mi2iMap.count(instr);
00404     }
00405 
00406     /// Returns the base index for the given instruction.
00407     SlotIndex getInstructionIndex(const MachineInstr *MI) const {
00408       // Instructions inside a bundle have the same number as the bundle itself.
00409       Mi2IndexMap::const_iterator itr = mi2iMap.find(getBundleStart(MI));
00410       assert(itr != mi2iMap.end() && "Instruction not found in maps.");
00411       return itr->second;
00412     }
00413 
00414     /// Returns the instruction for the given index, or null if the given
00415     /// index has no instruction associated with it.
00416     MachineInstr* getInstructionFromIndex(SlotIndex index) const {
00417       return index.isValid() ? index.listEntry()->getInstr() : 0;
00418     }
00419 
00420     /// Returns the next non-null index, if one exists.
00421     /// Otherwise returns getLastIndex().
00422     SlotIndex getNextNonNullIndex(SlotIndex Index) {
00423       IndexList::iterator I = Index.listEntry();
00424       IndexList::iterator E = indexList.end();
00425       while (++I != E)
00426         if (I->getInstr())
00427           return SlotIndex(I, Index.getSlot());
00428       // We reached the end of the function.
00429       return getLastIndex();
00430     }
00431 
00432     /// getIndexBefore - Returns the index of the last indexed instruction
00433     /// before MI, or the start index of its basic block.
00434     /// MI is not required to have an index.
00435     SlotIndex getIndexBefore(const MachineInstr *MI) const {
00436       const MachineBasicBlock *MBB = MI->getParent();
00437       assert(MBB && "MI must be inserted inna basic block");
00438       MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
00439       for (;;) {
00440         if (I == B)
00441           return getMBBStartIdx(MBB);
00442         --I;
00443         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
00444         if (MapItr != mi2iMap.end())
00445           return MapItr->second;
00446       }
00447     }
00448 
00449     /// getIndexAfter - Returns the index of the first indexed instruction
00450     /// after MI, or the end index of its basic block.
00451     /// MI is not required to have an index.
00452     SlotIndex getIndexAfter(const MachineInstr *MI) const {
00453       const MachineBasicBlock *MBB = MI->getParent();
00454       assert(MBB && "MI must be inserted inna basic block");
00455       MachineBasicBlock::const_iterator I = MI, E = MBB->end();
00456       for (;;) {
00457         ++I;
00458         if (I == E)
00459           return getMBBEndIdx(MBB);
00460         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
00461         if (MapItr != mi2iMap.end())
00462           return MapItr->second;
00463       }
00464     }
00465 
00466     /// Return the (start,end) range of the given basic block number.
00467     const std::pair<SlotIndex, SlotIndex> &
00468     getMBBRange(unsigned Num) const {
00469       return MBBRanges[Num];
00470     }
00471 
00472     /// Return the (start,end) range of the given basic block.
00473     const std::pair<SlotIndex, SlotIndex> &
00474     getMBBRange(const MachineBasicBlock *MBB) const {
00475       return getMBBRange(MBB->getNumber());
00476     }
00477 
00478     /// Returns the first index in the given basic block number.
00479     SlotIndex getMBBStartIdx(unsigned Num) const {
00480       return getMBBRange(Num).first;
00481     }
00482 
00483     /// Returns the first index in the given basic block.
00484     SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
00485       return getMBBRange(mbb).first;
00486     }
00487 
00488     /// Returns the last index in the given basic block number.
00489     SlotIndex getMBBEndIdx(unsigned Num) const {
00490       return getMBBRange(Num).second;
00491     }
00492 
00493     /// Returns the last index in the given basic block.
00494     SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
00495       return getMBBRange(mbb).second;
00496     }
00497 
00498     /// Returns the basic block which the given index falls in.
00499     MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
00500       if (MachineInstr *MI = getInstructionFromIndex(index))
00501         return MI->getParent();
00502       SmallVectorImpl<IdxMBBPair>::const_iterator I =
00503         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
00504       // Take the pair containing the index
00505       SmallVectorImpl<IdxMBBPair>::const_iterator J =
00506         ((I != idx2MBBMap.end() && I->first > index) ||
00507          (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
00508 
00509       assert(J != idx2MBBMap.end() && J->first <= index &&
00510              index < getMBBEndIdx(J->second) &&
00511              "index does not correspond to an MBB");
00512       return J->second;
00513     }
00514 
00515     bool findLiveInMBBs(SlotIndex start, SlotIndex end,
00516                         SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
00517       SmallVectorImpl<IdxMBBPair>::const_iterator itr =
00518         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
00519       bool resVal = false;
00520 
00521       while (itr != idx2MBBMap.end()) {
00522         if (itr->first >= end)
00523           break;
00524         mbbs.push_back(itr->second);
00525         resVal = true;
00526         ++itr;
00527       }
00528       return resVal;
00529     }
00530 
00531     /// Returns the MBB covering the given range, or null if the range covers
00532     /// more than one basic block.
00533     MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
00534 
00535       assert(start < end && "Backwards ranges not allowed.");
00536 
00537       SmallVectorImpl<IdxMBBPair>::const_iterator itr =
00538         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
00539 
00540       if (itr == idx2MBBMap.end()) {
00541         itr = prior(itr);
00542         return itr->second;
00543       }
00544 
00545       // Check that we don't cross the boundary into this block.
00546       if (itr->first < end)
00547         return 0;
00548 
00549       itr = prior(itr);
00550 
00551       if (itr->first <= start)
00552         return itr->second;
00553 
00554       return 0;
00555     }
00556 
00557     /// Insert the given machine instruction into the mapping. Returns the
00558     /// assigned index.
00559     /// If Late is set and there are null indexes between mi's neighboring
00560     /// instructions, create the new index after the null indexes instead of
00561     /// before them.
00562     SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late = false) {
00563       assert(!mi->isInsideBundle() &&
00564              "Instructions inside bundles should use bundle start's slot.");
00565       assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
00566       // Numbering DBG_VALUE instructions could cause code generation to be
00567       // affected by debug information.
00568       assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
00569 
00570       assert(mi->getParent() != 0 && "Instr must be added to function.");
00571 
00572       // Get the entries where mi should be inserted.
00573       IndexList::iterator prevItr, nextItr;
00574       if (Late) {
00575         // Insert mi's index immediately before the following instruction.
00576         nextItr = getIndexAfter(mi).listEntry();
00577         prevItr = prior(nextItr);
00578       } else {
00579         // Insert mi's index immediately after the preceding instruction.
00580         prevItr = getIndexBefore(mi).listEntry();
00581         nextItr = llvm::next(prevItr);
00582       }
00583 
00584       // Get a number for the new instr, or 0 if there's no room currently.
00585       // In the latter case we'll force a renumber later.
00586       unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
00587       unsigned newNumber = prevItr->getIndex() + dist;
00588 
00589       // Insert a new list entry for mi.
00590       IndexList::iterator newItr =
00591         indexList.insert(nextItr, createEntry(mi, newNumber));
00592 
00593       // Renumber locally if we need to.
00594       if (dist == 0)
00595         renumberIndexes(newItr);
00596 
00597       SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
00598       mi2iMap.insert(std::make_pair(mi, newIndex));
00599       return newIndex;
00600     }
00601 
00602     /// Remove the given machine instruction from the mapping.
00603     void removeMachineInstrFromMaps(MachineInstr *mi) {
00604       // remove index -> MachineInstr and
00605       // MachineInstr -> index mappings
00606       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
00607       if (mi2iItr != mi2iMap.end()) {
00608         IndexListEntry *miEntry(mi2iItr->second.listEntry());
00609         assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
00610         // FIXME: Eventually we want to actually delete these indexes.
00611         miEntry->setInstr(0);
00612         mi2iMap.erase(mi2iItr);
00613       }
00614     }
00615 
00616     /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
00617     /// maps used by register allocator.
00618     void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
00619       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
00620       if (mi2iItr == mi2iMap.end())
00621         return;
00622       SlotIndex replaceBaseIndex = mi2iItr->second;
00623       IndexListEntry *miEntry(replaceBaseIndex.listEntry());
00624       assert(miEntry->getInstr() == mi &&
00625              "Mismatched instruction in index tables.");
00626       miEntry->setInstr(newMI);
00627       mi2iMap.erase(mi2iItr);
00628       mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
00629     }
00630 
00631     /// Add the given MachineBasicBlock into the maps.
00632     void insertMBBInMaps(MachineBasicBlock *mbb) {
00633       MachineFunction::iterator nextMBB =
00634         llvm::next(MachineFunction::iterator(mbb));
00635 
00636       IndexListEntry *startEntry = 0;
00637       IndexListEntry *endEntry = 0;
00638       IndexList::iterator newItr;
00639       if (nextMBB == mbb->getParent()->end()) {
00640         startEntry = &indexList.back();
00641         endEntry = createEntry(0, 0);
00642         newItr = indexList.insertAfter(startEntry, endEntry);
00643       } else {
00644         startEntry = createEntry(0, 0);
00645         endEntry = getMBBStartIdx(nextMBB).listEntry();
00646         newItr = indexList.insert(endEntry, startEntry);
00647       }
00648 
00649       SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
00650       SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
00651 
00652       MachineFunction::iterator prevMBB(mbb);
00653       assert(prevMBB != mbb->getParent()->end() &&
00654              "Can't insert a new block at the beginning of a function.");
00655       --prevMBB;
00656       MBBRanges[prevMBB->getNumber()].second = startIdx;
00657 
00658       assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
00659              "Blocks must be added in order");
00660       MBBRanges.push_back(std::make_pair(startIdx, endIdx));
00661       idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
00662 
00663       renumberIndexes(newItr);
00664       std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
00665     }
00666 
00667     /// \brief Free the resources that were required to maintain a SlotIndex.
00668     ///
00669     /// Once an index is no longer needed (for instance because the instruction
00670     /// at that index has been moved), the resources required to maintain the
00671     /// index can be relinquished to reduce memory use and improve renumbering
00672     /// performance. Any remaining SlotIndex objects that point to the same
00673     /// index are left 'dangling' (much the same as a dangling pointer to a
00674     /// freed object) and should not be accessed, except to destruct them.
00675     /// 
00676     /// Like dangling pointers, access to dangling SlotIndexes can cause
00677     /// painful-to-track-down bugs, especially if the memory for the index
00678     /// previously pointed to has been re-used. To detect dangling SlotIndex
00679     /// bugs, build with EXPENSIVE_CHECKS=1. This will cause "erased" indexes to
00680     /// be retained in a graveyard instead of being freed. Operations on indexes
00681     /// in the graveyard will trigger an assertion.
00682     void eraseIndex(SlotIndex index) {
00683       IndexListEntry *entry = index.listEntry();
00684 #ifdef EXPENSIVE_CHECKS
00685       indexList.remove(entry);
00686       graveyardList.push_back(entry);
00687       entry->setPoison();
00688 #else
00689       indexList.erase(entry);
00690 #endif
00691     }
00692 
00693   };
00694 
00695 
00696   // Specialize IntervalMapInfo for half-open slot index intervals.
00697   template <>
00698   struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
00699   };
00700 
00701 }
00702 
00703 #endif // LLVM_CODEGEN_SLOTINDEXES_H