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(nullptr, 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() != nullptr &&
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     /// Return the scaled distance from this index to the given one, where all
00222     /// slots on the same instruction have zero distance.
00223     int getInstrDistance(SlotIndex other) const {
00224       return (other.listEntry()->getIndex() - listEntry()->getIndex())
00225         / Slot_Count;
00226     }
00227 
00228     /// isBlock - Returns true if this is a block boundary slot.
00229     bool isBlock() const { return getSlot() == Slot_Block; }
00230 
00231     /// isEarlyClobber - Returns true if this is an early-clobber slot.
00232     bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
00233 
00234     /// isRegister - Returns true if this is a normal register use/def slot.
00235     /// Note that early-clobber slots may also be used for uses and defs.
00236     bool isRegister() const { return getSlot() == Slot_Register; }
00237 
00238     /// isDead - Returns true if this is a dead def kill slot.
00239     bool isDead() const { return getSlot() == Slot_Dead; }
00240 
00241     /// Returns the base index for associated with this index. The base index
00242     /// is the one associated with the Slot_Block slot for the instruction
00243     /// pointed to by this index.
00244     SlotIndex getBaseIndex() const {
00245       return SlotIndex(listEntry(), Slot_Block);
00246     }
00247 
00248     /// Returns the boundary index for associated with this index. The boundary
00249     /// index is the one associated with the Slot_Block slot for the instruction
00250     /// pointed to by this index.
00251     SlotIndex getBoundaryIndex() const {
00252       return SlotIndex(listEntry(), Slot_Dead);
00253     }
00254 
00255     /// Returns the register use/def slot in the current instruction for a
00256     /// normal or early-clobber def.
00257     SlotIndex getRegSlot(bool EC = false) const {
00258       return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
00259     }
00260 
00261     /// Returns the dead def kill slot for the current instruction.
00262     SlotIndex getDeadSlot() const {
00263       return SlotIndex(listEntry(), Slot_Dead);
00264     }
00265 
00266     /// Returns the next slot in the index list. This could be either the
00267     /// next slot for the instruction pointed to by this index or, if this
00268     /// index is a STORE, the first slot for the next instruction.
00269     /// WARNING: This method is considerably more expensive than the methods
00270     /// that return specific slots (getUseIndex(), etc). If you can - please
00271     /// use one of those methods.
00272     SlotIndex getNextSlot() const {
00273       Slot s = getSlot();
00274       if (s == Slot_Dead) {
00275         return SlotIndex(listEntry()->getNextNode(), Slot_Block);
00276       }
00277       return SlotIndex(listEntry(), s + 1);
00278     }
00279 
00280     /// Returns the next index. This is the index corresponding to the this
00281     /// index's slot, but for the next instruction.
00282     SlotIndex getNextIndex() const {
00283       return SlotIndex(listEntry()->getNextNode(), getSlot());
00284     }
00285 
00286     /// Returns the previous slot in the index list. This could be either the
00287     /// previous slot for the instruction pointed to by this index or, if this
00288     /// index is a Slot_Block, the last slot for the previous instruction.
00289     /// WARNING: This method is considerably more expensive than the methods
00290     /// that return specific slots (getUseIndex(), etc). If you can - please
00291     /// use one of those methods.
00292     SlotIndex getPrevSlot() const {
00293       Slot s = getSlot();
00294       if (s == Slot_Block) {
00295         return SlotIndex(listEntry()->getPrevNode(), Slot_Dead);
00296       }
00297       return SlotIndex(listEntry(), s - 1);
00298     }
00299 
00300     /// Returns the previous index. This is the index corresponding to this
00301     /// index's slot, but for the previous instruction.
00302     SlotIndex getPrevIndex() const {
00303       return SlotIndex(listEntry()->getPrevNode(), getSlot());
00304     }
00305 
00306   };
00307 
00308   template <> struct isPodLike<SlotIndex> { static const bool value = true; };
00309 
00310   inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
00311     li.print(os);
00312     return os;
00313   }
00314 
00315   typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
00316 
00317   inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
00318     return V < IM.first;
00319   }
00320 
00321   inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
00322     return IM.first < V;
00323   }
00324 
00325   struct Idx2MBBCompare {
00326     bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
00327       return LHS.first < RHS.first;
00328     }
00329   };
00330 
00331   /// SlotIndexes pass.
00332   ///
00333   /// This pass assigns indexes to each instruction.
00334   class SlotIndexes : public MachineFunctionPass {
00335   private:
00336 
00337     typedef ilist<IndexListEntry> IndexList;
00338     IndexList indexList;
00339 
00340 #ifdef EXPENSIVE_CHECKS
00341     IndexList graveyardList;
00342 #endif // EXPENSIVE_CHECKS
00343 
00344     MachineFunction *mf;
00345 
00346     typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
00347     Mi2IndexMap mi2iMap;
00348 
00349     /// MBBRanges - Map MBB number to (start, stop) indexes.
00350     SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
00351 
00352     /// Idx2MBBMap - Sorted list of pairs of index of first instruction
00353     /// and MBB id.
00354     SmallVector<IdxMBBPair, 8> idx2MBBMap;
00355 
00356     // IndexListEntry allocator.
00357     BumpPtrAllocator ileAllocator;
00358 
00359     IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
00360       IndexListEntry *entry =
00361         static_cast<IndexListEntry*>(
00362           ileAllocator.Allocate(sizeof(IndexListEntry),
00363           alignOf<IndexListEntry>()));
00364 
00365       new (entry) IndexListEntry(mi, index);
00366 
00367       return entry;
00368     }
00369 
00370     /// Renumber locally after inserting curItr.
00371     void renumberIndexes(IndexList::iterator curItr);
00372 
00373   public:
00374     static char ID;
00375 
00376     SlotIndexes() : MachineFunctionPass(ID) {
00377       initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
00378     }
00379 
00380     void getAnalysisUsage(AnalysisUsage &au) const override;
00381     void releaseMemory() override;
00382 
00383     bool runOnMachineFunction(MachineFunction &fn) override;
00384 
00385     /// Dump the indexes.
00386     void dump() const;
00387 
00388     /// Renumber the index list, providing space for new instructions.
00389     void renumberIndexes();
00390 
00391     /// Repair indexes after adding and removing instructions.
00392     void repairIndexesInRange(MachineBasicBlock *MBB,
00393                               MachineBasicBlock::iterator Begin,
00394                               MachineBasicBlock::iterator End);
00395 
00396     /// Returns the zero index for this analysis.
00397     SlotIndex getZeroIndex() {
00398       assert(indexList.front().getIndex() == 0 && "First index is not 0?");
00399       return SlotIndex(&indexList.front(), 0);
00400     }
00401 
00402     /// Returns the base index of the last slot in this analysis.
00403     SlotIndex getLastIndex() {
00404       return SlotIndex(&indexList.back(), 0);
00405     }
00406 
00407     /// Returns true if the given machine instr is mapped to an index,
00408     /// otherwise returns false.
00409     bool hasIndex(const MachineInstr *instr) const {
00410       return mi2iMap.count(instr);
00411     }
00412 
00413     /// Returns the base index for the given instruction.
00414     SlotIndex getInstructionIndex(const MachineInstr *MI) const {
00415       // Instructions inside a bundle have the same number as the bundle itself.
00416       Mi2IndexMap::const_iterator itr = mi2iMap.find(getBundleStart(MI));
00417       assert(itr != mi2iMap.end() && "Instruction not found in maps.");
00418       return itr->second;
00419     }
00420 
00421     /// Returns the instruction for the given index, or null if the given
00422     /// index has no instruction associated with it.
00423     MachineInstr* getInstructionFromIndex(SlotIndex index) const {
00424       return index.isValid() ? index.listEntry()->getInstr() : nullptr;
00425     }
00426 
00427     /// Returns the next non-null index, if one exists.
00428     /// Otherwise returns getLastIndex().
00429     SlotIndex getNextNonNullIndex(SlotIndex Index) {
00430       IndexList::iterator I = Index.listEntry();
00431       IndexList::iterator E = indexList.end();
00432       while (++I != E)
00433         if (I->getInstr())
00434           return SlotIndex(I, Index.getSlot());
00435       // We reached the end of the function.
00436       return getLastIndex();
00437     }
00438 
00439     /// getIndexBefore - Returns the index of the last indexed instruction
00440     /// before MI, or the start index of its basic block.
00441     /// MI is not required to have an index.
00442     SlotIndex getIndexBefore(const MachineInstr *MI) const {
00443       const MachineBasicBlock *MBB = MI->getParent();
00444       assert(MBB && "MI must be inserted inna basic block");
00445       MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
00446       for (;;) {
00447         if (I == B)
00448           return getMBBStartIdx(MBB);
00449         --I;
00450         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
00451         if (MapItr != mi2iMap.end())
00452           return MapItr->second;
00453       }
00454     }
00455 
00456     /// getIndexAfter - Returns the index of the first indexed instruction
00457     /// after MI, or the end index of its basic block.
00458     /// MI is not required to have an index.
00459     SlotIndex getIndexAfter(const MachineInstr *MI) const {
00460       const MachineBasicBlock *MBB = MI->getParent();
00461       assert(MBB && "MI must be inserted inna basic block");
00462       MachineBasicBlock::const_iterator I = MI, E = MBB->end();
00463       for (;;) {
00464         ++I;
00465         if (I == E)
00466           return getMBBEndIdx(MBB);
00467         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
00468         if (MapItr != mi2iMap.end())
00469           return MapItr->second;
00470       }
00471     }
00472 
00473     /// Return the (start,end) range of the given basic block number.
00474     const std::pair<SlotIndex, SlotIndex> &
00475     getMBBRange(unsigned Num) const {
00476       return MBBRanges[Num];
00477     }
00478 
00479     /// Return the (start,end) range of the given basic block.
00480     const std::pair<SlotIndex, SlotIndex> &
00481     getMBBRange(const MachineBasicBlock *MBB) const {
00482       return getMBBRange(MBB->getNumber());
00483     }
00484 
00485     /// Returns the first index in the given basic block number.
00486     SlotIndex getMBBStartIdx(unsigned Num) const {
00487       return getMBBRange(Num).first;
00488     }
00489 
00490     /// Returns the first index in the given basic block.
00491     SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
00492       return getMBBRange(mbb).first;
00493     }
00494 
00495     /// Returns the last index in the given basic block number.
00496     SlotIndex getMBBEndIdx(unsigned Num) const {
00497       return getMBBRange(Num).second;
00498     }
00499 
00500     /// Returns the last index in the given basic block.
00501     SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
00502       return getMBBRange(mbb).second;
00503     }
00504 
00505     /// Returns the basic block which the given index falls in.
00506     MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
00507       if (MachineInstr *MI = getInstructionFromIndex(index))
00508         return MI->getParent();
00509       SmallVectorImpl<IdxMBBPair>::const_iterator I =
00510         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
00511       // Take the pair containing the index
00512       SmallVectorImpl<IdxMBBPair>::const_iterator J =
00513         ((I != idx2MBBMap.end() && I->first > index) ||
00514          (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
00515 
00516       assert(J != idx2MBBMap.end() && J->first <= index &&
00517              index < getMBBEndIdx(J->second) &&
00518              "index does not correspond to an MBB");
00519       return J->second;
00520     }
00521 
00522     bool findLiveInMBBs(SlotIndex start, SlotIndex end,
00523                         SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
00524       SmallVectorImpl<IdxMBBPair>::const_iterator itr =
00525         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
00526       bool resVal = false;
00527 
00528       while (itr != idx2MBBMap.end()) {
00529         if (itr->first >= end)
00530           break;
00531         mbbs.push_back(itr->second);
00532         resVal = true;
00533         ++itr;
00534       }
00535       return resVal;
00536     }
00537 
00538     /// Returns the MBB covering the given range, or null if the range covers
00539     /// more than one basic block.
00540     MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
00541 
00542       assert(start < end && "Backwards ranges not allowed.");
00543 
00544       SmallVectorImpl<IdxMBBPair>::const_iterator itr =
00545         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
00546 
00547       if (itr == idx2MBBMap.end()) {
00548         itr = std::prev(itr);
00549         return itr->second;
00550       }
00551 
00552       // Check that we don't cross the boundary into this block.
00553       if (itr->first < end)
00554         return nullptr;
00555 
00556       itr = std::prev(itr);
00557 
00558       if (itr->first <= start)
00559         return itr->second;
00560 
00561       return nullptr;
00562     }
00563 
00564     /// Insert the given machine instruction into the mapping. Returns the
00565     /// assigned index.
00566     /// If Late is set and there are null indexes between mi's neighboring
00567     /// instructions, create the new index after the null indexes instead of
00568     /// before them.
00569     SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late = false) {
00570       assert(!mi->isInsideBundle() &&
00571              "Instructions inside bundles should use bundle start's slot.");
00572       assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
00573       // Numbering DBG_VALUE instructions could cause code generation to be
00574       // affected by debug information.
00575       assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
00576 
00577       assert(mi->getParent() != nullptr && "Instr must be added to function.");
00578 
00579       // Get the entries where mi should be inserted.
00580       IndexList::iterator prevItr, nextItr;
00581       if (Late) {
00582         // Insert mi's index immediately before the following instruction.
00583         nextItr = getIndexAfter(mi).listEntry();
00584         prevItr = std::prev(nextItr);
00585       } else {
00586         // Insert mi's index immediately after the preceding instruction.
00587         prevItr = getIndexBefore(mi).listEntry();
00588         nextItr = std::next(prevItr);
00589       }
00590 
00591       // Get a number for the new instr, or 0 if there's no room currently.
00592       // In the latter case we'll force a renumber later.
00593       unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
00594       unsigned newNumber = prevItr->getIndex() + dist;
00595 
00596       // Insert a new list entry for mi.
00597       IndexList::iterator newItr =
00598         indexList.insert(nextItr, createEntry(mi, newNumber));
00599 
00600       // Renumber locally if we need to.
00601       if (dist == 0)
00602         renumberIndexes(newItr);
00603 
00604       SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
00605       mi2iMap.insert(std::make_pair(mi, newIndex));
00606       return newIndex;
00607     }
00608 
00609     /// Remove the given machine instruction from the mapping.
00610     void removeMachineInstrFromMaps(MachineInstr *mi) {
00611       // remove index -> MachineInstr and
00612       // MachineInstr -> index mappings
00613       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
00614       if (mi2iItr != mi2iMap.end()) {
00615         IndexListEntry *miEntry(mi2iItr->second.listEntry());
00616         assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
00617         // FIXME: Eventually we want to actually delete these indexes.
00618         miEntry->setInstr(nullptr);
00619         mi2iMap.erase(mi2iItr);
00620       }
00621     }
00622 
00623     /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
00624     /// maps used by register allocator.
00625     void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
00626       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
00627       if (mi2iItr == mi2iMap.end())
00628         return;
00629       SlotIndex replaceBaseIndex = mi2iItr->second;
00630       IndexListEntry *miEntry(replaceBaseIndex.listEntry());
00631       assert(miEntry->getInstr() == mi &&
00632              "Mismatched instruction in index tables.");
00633       miEntry->setInstr(newMI);
00634       mi2iMap.erase(mi2iItr);
00635       mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
00636     }
00637 
00638     /// Add the given MachineBasicBlock into the maps.
00639     void insertMBBInMaps(MachineBasicBlock *mbb) {
00640       MachineFunction::iterator nextMBB =
00641         std::next(MachineFunction::iterator(mbb));
00642 
00643       IndexListEntry *startEntry = nullptr;
00644       IndexListEntry *endEntry = nullptr;
00645       IndexList::iterator newItr;
00646       if (nextMBB == mbb->getParent()->end()) {
00647         startEntry = &indexList.back();
00648         endEntry = createEntry(nullptr, 0);
00649         newItr = indexList.insertAfter(startEntry, endEntry);
00650       } else {
00651         startEntry = createEntry(nullptr, 0);
00652         endEntry = getMBBStartIdx(nextMBB).listEntry();
00653         newItr = indexList.insert(endEntry, startEntry);
00654       }
00655 
00656       SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
00657       SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
00658 
00659       MachineFunction::iterator prevMBB(mbb);
00660       assert(prevMBB != mbb->getParent()->end() &&
00661              "Can't insert a new block at the beginning of a function.");
00662       --prevMBB;
00663       MBBRanges[prevMBB->getNumber()].second = startIdx;
00664 
00665       assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
00666              "Blocks must be added in order");
00667       MBBRanges.push_back(std::make_pair(startIdx, endIdx));
00668       idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
00669 
00670       renumberIndexes(newItr);
00671       std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
00672     }
00673 
00674     /// \brief Free the resources that were required to maintain a SlotIndex.
00675     ///
00676     /// Once an index is no longer needed (for instance because the instruction
00677     /// at that index has been moved), the resources required to maintain the
00678     /// index can be relinquished to reduce memory use and improve renumbering
00679     /// performance. Any remaining SlotIndex objects that point to the same
00680     /// index are left 'dangling' (much the same as a dangling pointer to a
00681     /// freed object) and should not be accessed, except to destruct them.
00682     ///
00683     /// Like dangling pointers, access to dangling SlotIndexes can cause
00684     /// painful-to-track-down bugs, especially if the memory for the index
00685     /// previously pointed to has been re-used. To detect dangling SlotIndex
00686     /// bugs, build with EXPENSIVE_CHECKS=1. This will cause "erased" indexes to
00687     /// be retained in a graveyard instead of being freed. Operations on indexes
00688     /// in the graveyard will trigger an assertion.
00689     void eraseIndex(SlotIndex index) {
00690       IndexListEntry *entry = index.listEntry();
00691 #ifdef EXPENSIVE_CHECKS
00692       indexList.remove(entry);
00693       graveyardList.push_back(entry);
00694       entry->setPoison();
00695 #else
00696       indexList.erase(entry);
00697 #endif
00698     }
00699 
00700   };
00701 
00702 
00703   // Specialize IntervalMapInfo for half-open slot index intervals.
00704   template <>
00705   struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
00706   };
00707 
00708 }
00709 
00710 #endif // LLVM_CODEGEN_SLOTINDEXES_H