Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:36

0001 //===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements SlotIndex and related classes. The purpose of SlotIndex
0010 // is to describe a position at which a register can become live, or cease to
0011 // be live.
0012 //
0013 // SlotIndex is mostly a proxy for entries of the SlotIndexList, a class which
0014 // is held is LiveIntervals and provides the real numbering. This allows
0015 // LiveIntervals to perform largely transparent renumbering.
0016 //===----------------------------------------------------------------------===//
0017 
0018 #ifndef LLVM_CODEGEN_SLOTINDEXES_H
0019 #define LLVM_CODEGEN_SLOTINDEXES_H
0020 
0021 #include "llvm/ADT/DenseMap.h"
0022 #include "llvm/ADT/IntervalMap.h"
0023 #include "llvm/ADT/PointerIntPair.h"
0024 #include "llvm/ADT/SmallVector.h"
0025 #include "llvm/ADT/simple_ilist.h"
0026 #include "llvm/CodeGen/MachineBasicBlock.h"
0027 #include "llvm/CodeGen/MachineFunction.h"
0028 #include "llvm/CodeGen/MachineFunctionPass.h"
0029 #include "llvm/CodeGen/MachineInstr.h"
0030 #include "llvm/CodeGen/MachineInstrBundle.h"
0031 #include "llvm/CodeGen/MachinePassManager.h"
0032 #include "llvm/Support/Allocator.h"
0033 #include <algorithm>
0034 #include <cassert>
0035 #include <iterator>
0036 #include <utility>
0037 
0038 namespace llvm {
0039 
0040 class raw_ostream;
0041 
0042   /// This class represents an entry in the slot index list held in the
0043   /// SlotIndexes pass. It should not be used directly. See the
0044   /// SlotIndex & SlotIndexes classes for the public interface to this
0045   /// information.
0046   class IndexListEntry : public ilist_node<IndexListEntry> {
0047     MachineInstr *mi;
0048     unsigned index;
0049 
0050   public:
0051     IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
0052 
0053     MachineInstr* getInstr() const { return mi; }
0054     void setInstr(MachineInstr *mi) {
0055       this->mi = mi;
0056     }
0057 
0058     unsigned getIndex() const { return index; }
0059     void setIndex(unsigned index) {
0060       this->index = index;
0061     }
0062   };
0063 
0064   /// SlotIndex - An opaque wrapper around machine indexes.
0065   class SlotIndex {
0066     friend class SlotIndexes;
0067 
0068     enum Slot {
0069       /// Basic block boundary.  Used for live ranges entering and leaving a
0070       /// block without being live in the layout neighbor.  Also used as the
0071       /// def slot of PHI-defs.
0072       Slot_Block,
0073 
0074       /// Early-clobber register use/def slot.  A live range defined at
0075       /// Slot_EarlyClobber interferes with normal live ranges killed at
0076       /// Slot_Register.  Also used as the kill slot for live ranges tied to an
0077       /// early-clobber def.
0078       Slot_EarlyClobber,
0079 
0080       /// Normal register use/def slot.  Normal instructions kill and define
0081       /// register live ranges at this slot.
0082       Slot_Register,
0083 
0084       /// Dead def kill point.  Kill slot for a live range that is defined by
0085       /// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't
0086       /// used anywhere.
0087       Slot_Dead,
0088 
0089       Slot_Count
0090     };
0091 
0092     PointerIntPair<IndexListEntry*, 2, unsigned> lie;
0093 
0094     IndexListEntry* listEntry() const {
0095       assert(isValid() && "Attempt to compare reserved index.");
0096       return lie.getPointer();
0097     }
0098 
0099     unsigned getIndex() const {
0100       return listEntry()->getIndex() | getSlot();
0101     }
0102 
0103     /// Returns the slot for this SlotIndex.
0104     Slot getSlot() const {
0105       return static_cast<Slot>(lie.getInt());
0106     }
0107 
0108   public:
0109     enum {
0110       /// The default distance between instructions as returned by distance().
0111       /// This may vary as instructions are inserted and removed.
0112       InstrDist = 4 * Slot_Count
0113     };
0114 
0115     /// Construct an invalid index.
0116     SlotIndex() = default;
0117 
0118     // Creates a SlotIndex from an IndexListEntry and a slot. Generally should
0119     // not be used. This method is only public to facilitate writing certain
0120     // unit tests.
0121     SlotIndex(IndexListEntry *entry, unsigned slot) : lie(entry, slot) {}
0122 
0123     // Construct a new slot index from the given one, and set the slot.
0124     SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) {
0125       assert(isValid() && "Attempt to construct index with 0 pointer.");
0126     }
0127 
0128     /// Returns true if this is a valid index. Invalid indices do
0129     /// not point into an index table, and cannot be compared.
0130     bool isValid() const {
0131       return lie.getPointer();
0132     }
0133 
0134     /// Return true for a valid index.
0135     explicit operator bool() const { return isValid(); }
0136 
0137     /// Print this index to the given raw_ostream.
0138     void print(raw_ostream &os) const;
0139 
0140     /// Dump this index to stderr.
0141     void dump() const;
0142 
0143     /// Compare two SlotIndex objects for equality.
0144     bool operator==(SlotIndex other) const {
0145       return lie == other.lie;
0146     }
0147     /// Compare two SlotIndex objects for inequality.
0148     bool operator!=(SlotIndex other) const {
0149       return lie != other.lie;
0150     }
0151 
0152     /// Compare two SlotIndex objects. Return true if the first index
0153     /// is strictly lower than the second.
0154     bool operator<(SlotIndex other) const {
0155       return getIndex() < other.getIndex();
0156     }
0157     /// Compare two SlotIndex objects. Return true if the first index
0158     /// is lower than, or equal to, the second.
0159     bool operator<=(SlotIndex other) const {
0160       return getIndex() <= other.getIndex();
0161     }
0162 
0163     /// Compare two SlotIndex objects. Return true if the first index
0164     /// is greater than the second.
0165     bool operator>(SlotIndex other) const {
0166       return getIndex() > other.getIndex();
0167     }
0168 
0169     /// Compare two SlotIndex objects. Return true if the first index
0170     /// is greater than, or equal to, the second.
0171     bool operator>=(SlotIndex other) const {
0172       return getIndex() >= other.getIndex();
0173     }
0174 
0175     /// isSameInstr - Return true if A and B refer to the same instruction.
0176     static bool isSameInstr(SlotIndex A, SlotIndex B) {
0177       return A.listEntry() == B.listEntry();
0178     }
0179 
0180     /// isEarlierInstr - Return true if A refers to an instruction earlier than
0181     /// B. This is equivalent to A < B && !isSameInstr(A, B).
0182     static bool isEarlierInstr(SlotIndex A, SlotIndex B) {
0183       return A.listEntry()->getIndex() < B.listEntry()->getIndex();
0184     }
0185 
0186     /// Return true if A refers to the same instruction as B or an earlier one.
0187     /// This is equivalent to !isEarlierInstr(B, A).
0188     static bool isEarlierEqualInstr(SlotIndex A, SlotIndex B) {
0189       return !isEarlierInstr(B, A);
0190     }
0191 
0192     /// Return the distance from this index to the given one.
0193     int distance(SlotIndex other) const {
0194       return other.getIndex() - getIndex();
0195     }
0196 
0197     /// Return the scaled distance from this index to the given one, where all
0198     /// slots on the same instruction have zero distance, assuming that the slot
0199     /// indices are packed as densely as possible. There are normally gaps
0200     /// between instructions, so this assumption often doesn't hold. This
0201     /// results in this function often returning a value greater than the actual
0202     /// instruction distance.
0203     int getApproxInstrDistance(SlotIndex other) const {
0204       return (other.listEntry()->getIndex() - listEntry()->getIndex())
0205         / Slot_Count;
0206     }
0207 
0208     /// isBlock - Returns true if this is a block boundary slot.
0209     bool isBlock() const { return getSlot() == Slot_Block; }
0210 
0211     /// isEarlyClobber - Returns true if this is an early-clobber slot.
0212     bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
0213 
0214     /// isRegister - Returns true if this is a normal register use/def slot.
0215     /// Note that early-clobber slots may also be used for uses and defs.
0216     bool isRegister() const { return getSlot() == Slot_Register; }
0217 
0218     /// isDead - Returns true if this is a dead def kill slot.
0219     bool isDead() const { return getSlot() == Slot_Dead; }
0220 
0221     /// Returns the base index for associated with this index. The base index
0222     /// is the one associated with the Slot_Block slot for the instruction
0223     /// pointed to by this index.
0224     SlotIndex getBaseIndex() const {
0225       return SlotIndex(listEntry(), Slot_Block);
0226     }
0227 
0228     /// Returns the boundary index for associated with this index. The boundary
0229     /// index is the one associated with the Slot_Block slot for the instruction
0230     /// pointed to by this index.
0231     SlotIndex getBoundaryIndex() const {
0232       return SlotIndex(listEntry(), Slot_Dead);
0233     }
0234 
0235     /// Returns the register use/def slot in the current instruction for a
0236     /// normal or early-clobber def.
0237     SlotIndex getRegSlot(bool EC = false) const {
0238       return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
0239     }
0240 
0241     /// Returns the dead def kill slot for the current instruction.
0242     SlotIndex getDeadSlot() const {
0243       return SlotIndex(listEntry(), Slot_Dead);
0244     }
0245 
0246     /// Returns the next slot in the index list. This could be either the
0247     /// next slot for the instruction pointed to by this index or, if this
0248     /// index is a STORE, the first slot for the next instruction.
0249     /// WARNING: This method is considerably more expensive than the methods
0250     /// that return specific slots (getUseIndex(), etc). If you can - please
0251     /// use one of those methods.
0252     SlotIndex getNextSlot() const {
0253       Slot s = getSlot();
0254       if (s == Slot_Dead) {
0255         return SlotIndex(&*++listEntry()->getIterator(), Slot_Block);
0256       }
0257       return SlotIndex(listEntry(), s + 1);
0258     }
0259 
0260     /// Returns the next index. This is the index corresponding to the this
0261     /// index's slot, but for the next instruction.
0262     SlotIndex getNextIndex() const {
0263       return SlotIndex(&*++listEntry()->getIterator(), getSlot());
0264     }
0265 
0266     /// Returns the previous slot in the index list. This could be either the
0267     /// previous slot for the instruction pointed to by this index or, if this
0268     /// index is a Slot_Block, the last slot for the previous instruction.
0269     /// WARNING: This method is considerably more expensive than the methods
0270     /// that return specific slots (getUseIndex(), etc). If you can - please
0271     /// use one of those methods.
0272     SlotIndex getPrevSlot() const {
0273       Slot s = getSlot();
0274       if (s == Slot_Block) {
0275         return SlotIndex(&*--listEntry()->getIterator(), Slot_Dead);
0276       }
0277       return SlotIndex(listEntry(), s - 1);
0278     }
0279 
0280     /// Returns the previous index. This is the index corresponding to this
0281     /// index's slot, but for the previous instruction.
0282     SlotIndex getPrevIndex() const {
0283       return SlotIndex(&*--listEntry()->getIterator(), getSlot());
0284     }
0285   };
0286 
0287   inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
0288     li.print(os);
0289     return os;
0290   }
0291 
0292   using IdxMBBPair = std::pair<SlotIndex, MachineBasicBlock *>;
0293 
0294   /// SlotIndexes pass.
0295   ///
0296   /// This pass assigns indexes to each instruction.
0297   class SlotIndexes {
0298     friend class SlotIndexesWrapperPass;
0299 
0300   private:
0301     // IndexListEntry allocator.
0302     BumpPtrAllocator ileAllocator;
0303 
0304     using IndexList = simple_ilist<IndexListEntry>;
0305     IndexList indexList;
0306 
0307     MachineFunction *mf = nullptr;
0308 
0309     using Mi2IndexMap = DenseMap<const MachineInstr *, SlotIndex>;
0310     Mi2IndexMap mi2iMap;
0311 
0312     /// MBBRanges - Map MBB number to (start, stop) indexes.
0313     SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
0314 
0315     /// Idx2MBBMap - Sorted list of pairs of index of first instruction
0316     /// and MBB id.
0317     SmallVector<IdxMBBPair, 8> idx2MBBMap;
0318 
0319     // For legacy pass manager.
0320     SlotIndexes() = default;
0321 
0322     void clear();
0323 
0324     void analyze(MachineFunction &MF);
0325 
0326     IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
0327       IndexListEntry *entry =
0328           static_cast<IndexListEntry *>(ileAllocator.Allocate(
0329               sizeof(IndexListEntry), alignof(IndexListEntry)));
0330 
0331       new (entry) IndexListEntry(mi, index);
0332 
0333       return entry;
0334     }
0335 
0336     /// Renumber locally after inserting curItr.
0337     void renumberIndexes(IndexList::iterator curItr);
0338 
0339   public:
0340     SlotIndexes(SlotIndexes &&) = default;
0341 
0342     SlotIndexes(MachineFunction &MF) { analyze(MF); }
0343 
0344     ~SlotIndexes();
0345 
0346     void reanalyze(MachineFunction &MF) {
0347       clear();
0348       analyze(MF);
0349     }
0350 
0351     void print(raw_ostream &OS) const;
0352 
0353     /// Dump the indexes.
0354     void dump() const;
0355 
0356     /// Repair indexes after adding and removing instructions.
0357     void repairIndexesInRange(MachineBasicBlock *MBB,
0358                               MachineBasicBlock::iterator Begin,
0359                               MachineBasicBlock::iterator End);
0360 
0361     /// Returns the zero index for this analysis.
0362     SlotIndex getZeroIndex() {
0363       assert(indexList.front().getIndex() == 0 && "First index is not 0?");
0364       return SlotIndex(&indexList.front(), 0);
0365     }
0366 
0367     /// Returns the base index of the last slot in this analysis.
0368     SlotIndex getLastIndex() {
0369       return SlotIndex(&indexList.back(), 0);
0370     }
0371 
0372     /// Returns true if the given machine instr is mapped to an index,
0373     /// otherwise returns false.
0374     bool hasIndex(const MachineInstr &instr) const {
0375       return mi2iMap.count(&instr);
0376     }
0377 
0378     /// Returns the base index for the given instruction.
0379     SlotIndex getInstructionIndex(const MachineInstr &MI,
0380                                   bool IgnoreBundle = false) const {
0381       // Instructions inside a bundle have the same number as the bundle itself.
0382       auto BundleStart = getBundleStart(MI.getIterator());
0383       auto BundleEnd = getBundleEnd(MI.getIterator());
0384       // Use the first non-debug instruction in the bundle to get SlotIndex.
0385       const MachineInstr &BundleNonDebug =
0386           IgnoreBundle ? MI
0387                        : *skipDebugInstructionsForward(BundleStart, BundleEnd);
0388       assert(!BundleNonDebug.isDebugInstr() &&
0389              "Could not use a debug instruction to query mi2iMap.");
0390       Mi2IndexMap::const_iterator itr = mi2iMap.find(&BundleNonDebug);
0391       assert(itr != mi2iMap.end() && "Instruction not found in maps.");
0392       return itr->second;
0393     }
0394 
0395     /// Returns the instruction for the given index, or null if the given
0396     /// index has no instruction associated with it.
0397     MachineInstr* getInstructionFromIndex(SlotIndex index) const {
0398       return index.listEntry()->getInstr();
0399     }
0400 
0401     /// Returns the next non-null index, if one exists.
0402     /// Otherwise returns getLastIndex().
0403     SlotIndex getNextNonNullIndex(SlotIndex Index) {
0404       IndexList::iterator I = Index.listEntry()->getIterator();
0405       IndexList::iterator E = indexList.end();
0406       while (++I != E)
0407         if (I->getInstr())
0408           return SlotIndex(&*I, Index.getSlot());
0409       // We reached the end of the function.
0410       return getLastIndex();
0411     }
0412 
0413     /// getIndexBefore - Returns the index of the last indexed instruction
0414     /// before MI, or the start index of its basic block.
0415     /// MI is not required to have an index.
0416     SlotIndex getIndexBefore(const MachineInstr &MI) const {
0417       const MachineBasicBlock *MBB = MI.getParent();
0418       assert(MBB && "MI must be inserted in a basic block");
0419       MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
0420       while (true) {
0421         if (I == B)
0422           return getMBBStartIdx(MBB);
0423         --I;
0424         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I);
0425         if (MapItr != mi2iMap.end())
0426           return MapItr->second;
0427       }
0428     }
0429 
0430     /// getIndexAfter - Returns the index of the first indexed instruction
0431     /// after MI, or the end index of its basic block.
0432     /// MI is not required to have an index.
0433     SlotIndex getIndexAfter(const MachineInstr &MI) const {
0434       const MachineBasicBlock *MBB = MI.getParent();
0435       assert(MBB && "MI must be inserted in a basic block");
0436       MachineBasicBlock::const_iterator I = MI, E = MBB->end();
0437       while (true) {
0438         ++I;
0439         if (I == E)
0440           return getMBBEndIdx(MBB);
0441         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I);
0442         if (MapItr != mi2iMap.end())
0443           return MapItr->second;
0444       }
0445     }
0446 
0447     /// Return the (start,end) range of the given basic block number.
0448     const std::pair<SlotIndex, SlotIndex> &
0449     getMBBRange(unsigned Num) const {
0450       return MBBRanges[Num];
0451     }
0452 
0453     /// Return the (start,end) range of the given basic block.
0454     const std::pair<SlotIndex, SlotIndex> &
0455     getMBBRange(const MachineBasicBlock *MBB) const {
0456       return getMBBRange(MBB->getNumber());
0457     }
0458 
0459     /// Returns the first index in the given basic block number.
0460     SlotIndex getMBBStartIdx(unsigned Num) const {
0461       return getMBBRange(Num).first;
0462     }
0463 
0464     /// Returns the first index in the given basic block.
0465     SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
0466       return getMBBRange(mbb).first;
0467     }
0468 
0469     /// Returns the last index in the given basic block number.
0470     SlotIndex getMBBEndIdx(unsigned Num) const {
0471       return getMBBRange(Num).second;
0472     }
0473 
0474     /// Returns the last index in the given basic block.
0475     SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
0476       return getMBBRange(mbb).second;
0477     }
0478 
0479     /// Iterator over the idx2MBBMap (sorted pairs of slot index of basic block
0480     /// begin and basic block)
0481     using MBBIndexIterator = SmallVectorImpl<IdxMBBPair>::const_iterator;
0482 
0483     /// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater
0484     /// than or equal to \p Idx. If \p Start is provided, only search the range
0485     /// from \p Start to the end of the function.
0486     MBBIndexIterator getMBBLowerBound(MBBIndexIterator Start,
0487                                       SlotIndex Idx) const {
0488       return std::lower_bound(
0489           Start, MBBIndexEnd(), Idx,
0490           [](const IdxMBBPair &IM, SlotIndex Idx) { return IM.first < Idx; });
0491     }
0492     MBBIndexIterator getMBBLowerBound(SlotIndex Idx) const {
0493       return getMBBLowerBound(MBBIndexBegin(), Idx);
0494     }
0495 
0496     /// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater
0497     /// than \p Idx.
0498     MBBIndexIterator getMBBUpperBound(SlotIndex Idx) const {
0499       return std::upper_bound(
0500           MBBIndexBegin(), MBBIndexEnd(), Idx,
0501           [](SlotIndex Idx, const IdxMBBPair &IM) { return Idx < IM.first; });
0502     }
0503 
0504     /// Returns an iterator for the begin of the idx2MBBMap.
0505     MBBIndexIterator MBBIndexBegin() const {
0506       return idx2MBBMap.begin();
0507     }
0508 
0509     /// Return an iterator for the end of the idx2MBBMap.
0510     MBBIndexIterator MBBIndexEnd() const {
0511       return idx2MBBMap.end();
0512     }
0513 
0514     /// Returns the basic block which the given index falls in.
0515     MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
0516       if (MachineInstr *MI = getInstructionFromIndex(index))
0517         return MI->getParent();
0518 
0519       MBBIndexIterator I = std::prev(getMBBUpperBound(index));
0520       assert(I != MBBIndexEnd() && I->first <= index &&
0521              index < getMBBEndIdx(I->second) &&
0522              "index does not correspond to an MBB");
0523       return I->second;
0524     }
0525 
0526     /// Insert the given machine instruction into the mapping. Returns the
0527     /// assigned index.
0528     /// If Late is set and there are null indexes between mi's neighboring
0529     /// instructions, create the new index after the null indexes instead of
0530     /// before them.
0531     SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late = false) {
0532       assert(!MI.isInsideBundle() &&
0533              "Instructions inside bundles should use bundle start's slot.");
0534       assert(!mi2iMap.contains(&MI) && "Instr already indexed.");
0535       // Numbering debug instructions could cause code generation to be
0536       // affected by debug information.
0537       assert(!MI.isDebugInstr() && "Cannot number debug instructions.");
0538 
0539       assert(MI.getParent() != nullptr && "Instr must be added to function.");
0540 
0541       // Get the entries where MI should be inserted.
0542       IndexList::iterator prevItr, nextItr;
0543       if (Late) {
0544         // Insert MI's index immediately before the following instruction.
0545         nextItr = getIndexAfter(MI).listEntry()->getIterator();
0546         prevItr = std::prev(nextItr);
0547       } else {
0548         // Insert MI's index immediately after the preceding instruction.
0549         prevItr = getIndexBefore(MI).listEntry()->getIterator();
0550         nextItr = std::next(prevItr);
0551       }
0552 
0553       // Get a number for the new instr, or 0 if there's no room currently.
0554       // In the latter case we'll force a renumber later.
0555       unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
0556       unsigned newNumber = prevItr->getIndex() + dist;
0557 
0558       // Insert a new list entry for MI.
0559       IndexList::iterator newItr =
0560           indexList.insert(nextItr, *createEntry(&MI, newNumber));
0561 
0562       // Renumber locally if we need to.
0563       if (dist == 0)
0564         renumberIndexes(newItr);
0565 
0566       SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
0567       mi2iMap.insert(std::make_pair(&MI, newIndex));
0568       return newIndex;
0569     }
0570 
0571     /// Removes machine instruction (bundle) \p MI from the mapping.
0572     /// This should be called before MachineInstr::eraseFromParent() is used to
0573     /// remove a whole bundle or an unbundled instruction.
0574     /// If \p AllowBundled is set then this can be used on a bundled
0575     /// instruction; however, this exists to support handleMoveIntoBundle,
0576     /// and in general removeSingleMachineInstrFromMaps should be used instead.
0577     void removeMachineInstrFromMaps(MachineInstr &MI,
0578                                     bool AllowBundled = false);
0579 
0580     /// Removes a single machine instruction \p MI from the mapping.
0581     /// This should be called before MachineInstr::eraseFromBundle() is used to
0582     /// remove a single instruction (out of a bundle).
0583     void removeSingleMachineInstrFromMaps(MachineInstr &MI);
0584 
0585     /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
0586     /// maps used by register allocator. \returns the index where the new
0587     /// instruction was inserted.
0588     SlotIndex replaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI) {
0589       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI);
0590       if (mi2iItr == mi2iMap.end())
0591         return SlotIndex();
0592       SlotIndex replaceBaseIndex = mi2iItr->second;
0593       IndexListEntry *miEntry(replaceBaseIndex.listEntry());
0594       assert(miEntry->getInstr() == &MI &&
0595              "Mismatched instruction in index tables.");
0596       miEntry->setInstr(&NewMI);
0597       mi2iMap.erase(mi2iItr);
0598       mi2iMap.insert(std::make_pair(&NewMI, replaceBaseIndex));
0599       return replaceBaseIndex;
0600     }
0601 
0602     /// Add the given MachineBasicBlock into the maps.
0603     /// If it contains any instructions then they must already be in the maps.
0604     /// This is used after a block has been split by moving some suffix of its
0605     /// instructions into a newly created block.
0606     void insertMBBInMaps(MachineBasicBlock *mbb) {
0607       assert(mbb != &mbb->getParent()->front() &&
0608              "Can't insert a new block at the beginning of a function.");
0609       auto prevMBB = std::prev(MachineFunction::iterator(mbb));
0610 
0611       // Create a new entry to be used for the start of mbb and the end of
0612       // prevMBB.
0613       IndexListEntry *startEntry = createEntry(nullptr, 0);
0614       IndexListEntry *endEntry = getMBBEndIdx(&*prevMBB).listEntry();
0615       IndexListEntry *insEntry =
0616           mbb->empty() ? endEntry
0617                        : getInstructionIndex(mbb->front()).listEntry();
0618       IndexList::iterator newItr =
0619           indexList.insert(insEntry->getIterator(), *startEntry);
0620 
0621       SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
0622       SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
0623 
0624       MBBRanges[prevMBB->getNumber()].second = startIdx;
0625 
0626       assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
0627              "Blocks must be added in order");
0628       MBBRanges.push_back(std::make_pair(startIdx, endIdx));
0629       idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
0630 
0631       renumberIndexes(newItr);
0632       llvm::sort(idx2MBBMap, less_first());
0633     }
0634 
0635     /// Renumber all indexes using the default instruction distance.
0636     void packIndexes();
0637   };
0638 
0639   // Specialize IntervalMapInfo for half-open slot index intervals.
0640   template <>
0641   struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
0642   };
0643 
0644   class SlotIndexesAnalysis : public AnalysisInfoMixin<SlotIndexesAnalysis> {
0645     friend AnalysisInfoMixin<SlotIndexesAnalysis>;
0646     static AnalysisKey Key;
0647 
0648   public:
0649     using Result = SlotIndexes;
0650     Result run(MachineFunction &MF, MachineFunctionAnalysisManager &);
0651   };
0652 
0653   class SlotIndexesPrinterPass : public PassInfoMixin<SlotIndexesPrinterPass> {
0654     raw_ostream &OS;
0655 
0656   public:
0657     explicit SlotIndexesPrinterPass(raw_ostream &OS) : OS(OS) {}
0658     PreservedAnalyses run(MachineFunction &MF,
0659                           MachineFunctionAnalysisManager &MFAM);
0660     static bool isRequired() { return true; }
0661   };
0662 
0663   class SlotIndexesWrapperPass : public MachineFunctionPass {
0664     SlotIndexes SI;
0665 
0666   public:
0667     static char ID;
0668 
0669     SlotIndexesWrapperPass();
0670 
0671     void getAnalysisUsage(AnalysisUsage &au) const override;
0672     void releaseMemory() override { SI.clear(); }
0673 
0674     bool runOnMachineFunction(MachineFunction &fn) override {
0675       SI.analyze(fn);
0676       return false;
0677     }
0678 
0679     SlotIndexes &getSI() { return SI; }
0680   };
0681 
0682 } // end namespace llvm
0683 
0684 #endif // LLVM_CODEGEN_SLOTINDEXES_H