Fix typo, reported by Eitan Adler!
[oota-llvm.git] / include / llvm / CodeGen / SlotIndexes.h
1 //===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements SlotIndex and related classes. The purpose of SlotIndex
11 // is to describe a position at which a register can become live, or cease to
12 // be live.
13 //
14 // SlotIndex is mostly a proxy for entries of the SlotIndexList, a class which
15 // is held is LiveIntervals and provides the real numbering. This allows
16 // LiveIntervals to perform largely transparent renumbering.
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_CODEGEN_SLOTINDEXES_H
20 #define LLVM_CODEGEN_SLOTINDEXES_H
21
22 #include "llvm/CodeGen/MachineBasicBlock.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/ADT/PointerIntPair.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/Support/Allocator.h"
29
30 namespace llvm {
31
32   /// This class represents an entry in the slot index list held in the
33   /// SlotIndexes pass. It should not be used directly. See the
34   /// SlotIndex & SlotIndexes classes for the public interface to this
35   /// information.
36   class IndexListEntry {
37     IndexListEntry *next, *prev;
38     MachineInstr *mi;
39     unsigned index;
40
41   public:
42
43     IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
44
45     MachineInstr* getInstr() const { return mi; }
46     void setInstr(MachineInstr *mi) {
47       this->mi = mi;
48     }
49
50     unsigned getIndex() const { return index; }
51     void setIndex(unsigned index) {
52       this->index = index;
53     }
54     
55     IndexListEntry* getNext() { return next; }
56     const IndexListEntry* getNext() const { return next; }
57     void setNext(IndexListEntry *next) {
58       this->next = next;
59     }
60
61     IndexListEntry* getPrev() { return prev; }
62     const IndexListEntry* getPrev() const { return prev; }
63     void setPrev(IndexListEntry *prev) {
64       this->prev = prev;
65     }
66   };
67
68   // Specialize PointerLikeTypeTraits for IndexListEntry.
69   template <>
70   class PointerLikeTypeTraits<IndexListEntry*> { 
71   public:
72     static inline void* getAsVoidPointer(IndexListEntry *p) {
73       return p;
74     }
75     static inline IndexListEntry* getFromVoidPointer(void *p) {
76       return static_cast<IndexListEntry*>(p);
77     }
78     enum { NumLowBitsAvailable = 3 };
79   };
80
81   /// SlotIndex - An opaque wrapper around machine indexes.
82   class SlotIndex {
83     friend class SlotIndexes;
84     friend struct DenseMapInfo<SlotIndex>;
85
86     enum Slot {
87       /// Basic block boundary.  Used for live ranges entering and leaving a
88       /// block without being live in the layout neighbor.  Also used as the
89       /// def slot of PHI-defs.
90       Slot_Block,
91
92       /// Early-clobber register use/def slot.  A live range defined at
93       /// Slot_EarlyCLobber interferes with normal live ranges killed at
94       /// Slot_Register.  Also used as the kill slot for live ranges tied to an
95       /// early-clobber def.
96       Slot_EarlyClobber,
97
98       /// Normal register use/def slot.  Normal instructions kill and define
99       /// register live ranges at this slot.
100       Slot_Register,
101
102       /// Dead def kill point.  Kill slot for a live range that is defined by
103       /// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't
104       /// used anywhere.
105       Slot_Dead,
106
107       Slot_Count
108     };
109
110     PointerIntPair<IndexListEntry*, 2, unsigned> lie;
111
112     SlotIndex(IndexListEntry *entry, unsigned slot)
113       : lie(entry, slot) {}
114
115     IndexListEntry& entry() const {
116       assert(isValid() && "Attempt to compare reserved index.");
117       return *lie.getPointer();
118     }
119
120     int getIndex() const {
121       return entry().getIndex() | getSlot();
122     }
123
124     /// Returns the slot for this SlotIndex.
125     Slot getSlot() const {
126       return static_cast<Slot>(lie.getInt());
127     }
128
129     static inline unsigned getHashValue(const SlotIndex &v) {
130       void *ptrVal = v.lie.getOpaqueValue();
131       return (unsigned((intptr_t)ptrVal)) ^ (unsigned((intptr_t)ptrVal) >> 9);
132     }
133
134   public:
135     enum {
136       /// The default distance between instructions as returned by distance().
137       /// This may vary as instructions are inserted and removed.
138       InstrDist = 4 * Slot_Count
139     };
140
141     static inline SlotIndex getEmptyKey() {
142       return SlotIndex(0, 1);
143     }
144
145     static inline SlotIndex getTombstoneKey() {
146       return SlotIndex(0, 2);
147     }
148
149     /// Construct an invalid index.
150     SlotIndex() : lie(0, 0) {}
151
152     // Construct a new slot index from the given one, and set the slot.
153     SlotIndex(const SlotIndex &li, Slot s)
154       : lie(&li.entry(), unsigned(s)) {
155       assert(lie.getPointer() != 0 &&
156              "Attempt to construct index with 0 pointer.");
157     }
158
159     /// Returns true if this is a valid index. Invalid indicies do
160     /// not point into an index table, and cannot be compared.
161     bool isValid() const {
162       return lie.getPointer();
163     }
164
165     /// Return true for a valid index.
166     operator bool() const { return isValid(); }
167
168     /// Print this index to the given raw_ostream.
169     void print(raw_ostream &os) const;
170
171     /// Dump this index to stderr.
172     void dump() const;
173
174     /// Compare two SlotIndex objects for equality.
175     bool operator==(SlotIndex other) const {
176       return lie == other.lie;
177     }
178     /// Compare two SlotIndex objects for inequality.
179     bool operator!=(SlotIndex other) const {
180       return lie != other.lie;
181     }
182    
183     /// Compare two SlotIndex objects. Return true if the first index
184     /// is strictly lower than the second.
185     bool operator<(SlotIndex other) const {
186       return getIndex() < other.getIndex();
187     }
188     /// Compare two SlotIndex objects. Return true if the first index
189     /// is lower than, or equal to, the second.
190     bool operator<=(SlotIndex other) const {
191       return getIndex() <= other.getIndex();
192     }
193
194     /// Compare two SlotIndex objects. Return true if the first index
195     /// is greater than the second.
196     bool operator>(SlotIndex other) const {
197       return getIndex() > other.getIndex();
198     }
199
200     /// Compare two SlotIndex objects. Return true if the first index
201     /// is greater than, or equal to, the second.
202     bool operator>=(SlotIndex other) const {
203       return getIndex() >= other.getIndex();
204     }
205
206     /// isSameInstr - Return true if A and B refer to the same instruction.
207     static bool isSameInstr(SlotIndex A, SlotIndex B) {
208       return A.lie.getPointer() == B.lie.getPointer();
209     }
210
211     /// Return the distance from this index to the given one.
212     int distance(SlotIndex other) const {
213       return other.getIndex() - getIndex();
214     }
215
216     /// isBlock - Returns true if this is a block boundary slot.
217     bool isBlock() const { return getSlot() == Slot_Block; }
218
219     /// isEarlyClobber - Returns true if this is an early-clobber slot.
220     bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
221
222     /// isRegister - Returns true if this is a normal register use/def slot.
223     /// Note that early-clobber slots may also be used for uses and defs.
224     bool isRegister() const { return getSlot() == Slot_Register; }
225
226     /// isDead - Returns true if this is a dead def kill slot.
227     bool isDead() const { return getSlot() == Slot_Dead; }
228
229     /// Returns the base index for associated with this index. The base index
230     /// is the one associated with the Slot_Block slot for the instruction
231     /// pointed to by this index.
232     SlotIndex getBaseIndex() const {
233       return SlotIndex(&entry(), Slot_Block);
234     }
235
236     /// Returns the boundary index for associated with this index. The boundary
237     /// index is the one associated with the Slot_Block slot for the instruction
238     /// pointed to by this index.
239     SlotIndex getBoundaryIndex() const {
240       return SlotIndex(&entry(), Slot_Dead);
241     }
242
243     /// Returns the register use/def slot in the current instruction for a
244     /// normal or early-clobber def.
245     SlotIndex getRegSlot(bool EC = false) const {
246       return SlotIndex(&entry(), EC ? Slot_EarlyClobber : Slot_Register);
247     }
248
249     /// Returns the dead def kill slot for the current instruction.
250     SlotIndex getDeadSlot() const {
251       return SlotIndex(&entry(), Slot_Dead);
252     }
253
254     /// Returns the next slot in the index list. This could be either the
255     /// next slot for the instruction pointed to by this index or, if this
256     /// index is a STORE, the first slot for the next instruction.
257     /// WARNING: This method is considerably more expensive than the methods
258     /// that return specific slots (getUseIndex(), etc). If you can - please
259     /// use one of those methods.
260     SlotIndex getNextSlot() const {
261       Slot s = getSlot();
262       if (s == Slot_Dead) {
263         return SlotIndex(entry().getNext(), Slot_Block);
264       }
265       return SlotIndex(&entry(), s + 1);
266     }
267
268     /// Returns the next index. This is the index corresponding to the this
269     /// index's slot, but for the next instruction.
270     SlotIndex getNextIndex() const {
271       return SlotIndex(entry().getNext(), getSlot());
272     }
273
274     /// Returns the previous slot in the index list. This could be either the
275     /// previous slot for the instruction pointed to by this index or, if this
276     /// index is a Slot_Block, the last slot for the previous instruction.
277     /// WARNING: This method is considerably more expensive than the methods
278     /// that return specific slots (getUseIndex(), etc). If you can - please
279     /// use one of those methods.
280     SlotIndex getPrevSlot() const {
281       Slot s = getSlot();
282       if (s == Slot_Block) {
283         return SlotIndex(entry().getPrev(), Slot_Dead);
284       }
285       return SlotIndex(&entry(), s - 1);
286     }
287
288     /// Returns the previous index. This is the index corresponding to this
289     /// index's slot, but for the previous instruction.
290     SlotIndex getPrevIndex() const {
291       return SlotIndex(entry().getPrev(), getSlot());
292     }
293
294   };
295
296   /// DenseMapInfo specialization for SlotIndex.
297   template <>
298   struct DenseMapInfo<SlotIndex> {
299     static inline SlotIndex getEmptyKey() {
300       return SlotIndex::getEmptyKey();
301     }
302     static inline SlotIndex getTombstoneKey() {
303       return SlotIndex::getTombstoneKey();
304     }
305     static inline unsigned getHashValue(const SlotIndex &v) {
306       return SlotIndex::getHashValue(v);
307     }
308     static inline bool isEqual(const SlotIndex &LHS, const SlotIndex &RHS) {
309       return (LHS == RHS);
310     }
311   };
312   
313   template <> struct isPodLike<SlotIndex> { static const bool value = true; };
314
315
316   inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
317     li.print(os);
318     return os;
319   }
320
321   typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
322
323   inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
324     return V < IM.first;
325   }
326
327   inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
328     return IM.first < V;
329   }
330
331   struct Idx2MBBCompare {
332     bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
333       return LHS.first < RHS.first;
334     }
335   };
336
337   /// SlotIndexes pass.
338   ///
339   /// This pass assigns indexes to each instruction.
340   class SlotIndexes : public MachineFunctionPass {
341   private:
342
343     MachineFunction *mf;
344     IndexListEntry *indexListHead;
345     unsigned functionSize;
346
347     typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
348     Mi2IndexMap mi2iMap;
349
350     /// MBBRanges - Map MBB number to (start, stop) indexes.
351     SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
352
353     /// Idx2MBBMap - Sorted list of pairs of index of first instruction
354     /// and MBB id.
355     SmallVector<IdxMBBPair, 8> idx2MBBMap;
356
357     // IndexListEntry allocator.
358     BumpPtrAllocator ileAllocator;
359
360     IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
361       IndexListEntry *entry =
362         static_cast<IndexListEntry*>(
363           ileAllocator.Allocate(sizeof(IndexListEntry),
364           alignOf<IndexListEntry>()));
365
366       new (entry) IndexListEntry(mi, index);
367
368       return entry;
369     }
370
371     void initList() {
372       assert(indexListHead == 0 && "Zero entry non-null at initialisation.");
373       indexListHead = createEntry(0, ~0U);
374       indexListHead->setNext(0);
375       indexListHead->setPrev(indexListHead);
376     }
377
378     void clearList() {
379       indexListHead = 0;
380       ileAllocator.Reset();
381     }
382
383     IndexListEntry* getTail() {
384       assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
385       return indexListHead->getPrev();
386     }
387
388     const IndexListEntry* getTail() const {
389       assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
390       return indexListHead->getPrev();
391     }
392
393     // Returns true if the index list is empty.
394     bool empty() const { return (indexListHead == getTail()); }
395
396     IndexListEntry* front() {
397       assert(!empty() && "front() called on empty index list.");
398       return indexListHead;
399     }
400
401     const IndexListEntry* front() const {
402       assert(!empty() && "front() called on empty index list.");
403       return indexListHead;
404     }
405
406     IndexListEntry* back() {
407       assert(!empty() && "back() called on empty index list.");
408       return getTail()->getPrev();
409     }
410
411     const IndexListEntry* back() const {
412       assert(!empty() && "back() called on empty index list.");
413       return getTail()->getPrev();
414     }
415
416     /// Insert a new entry before itr.
417     void insert(IndexListEntry *itr, IndexListEntry *val) {
418       assert(itr != 0 && "itr should not be null.");
419       IndexListEntry *prev = itr->getPrev();
420       val->setNext(itr);
421       val->setPrev(prev);
422       
423       if (itr != indexListHead) {
424         prev->setNext(val);
425       }
426       else {
427         indexListHead = val;
428       }
429       itr->setPrev(val);
430     }
431
432     /// Push a new entry on to the end of the list.
433     void push_back(IndexListEntry *val) {
434       insert(getTail(), val);
435     }
436
437     /// Renumber locally after inserting newEntry.
438     void renumberIndexes(IndexListEntry *newEntry);
439
440   public:
441     static char ID;
442
443     SlotIndexes() : MachineFunctionPass(ID), indexListHead(0) {
444       initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
445     }
446
447     virtual void getAnalysisUsage(AnalysisUsage &au) const;
448     virtual void releaseMemory(); 
449
450     virtual bool runOnMachineFunction(MachineFunction &fn);
451
452     /// Dump the indexes.
453     void dump() const;
454
455     /// Renumber the index list, providing space for new instructions.
456     void renumberIndexes();
457
458     /// Returns the zero index for this analysis.
459     SlotIndex getZeroIndex() {
460       assert(front()->getIndex() == 0 && "First index is not 0?");
461       return SlotIndex(front(), 0);
462     }
463
464     /// Returns the base index of the last slot in this analysis.
465     SlotIndex getLastIndex() {
466       return SlotIndex(back(), 0);
467     }
468
469     /// Returns the invalid index marker for this analysis.
470     SlotIndex getInvalidIndex() {
471       return getZeroIndex();
472     }
473
474     /// Returns the distance between the highest and lowest indexes allocated
475     /// so far.
476     unsigned getIndexesLength() const {
477       assert(front()->getIndex() == 0 &&
478              "Initial index isn't zero?");
479
480       return back()->getIndex();
481     }
482
483     /// Returns the number of instructions in the function.
484     unsigned getFunctionSize() const {
485       return functionSize;
486     }
487
488     /// Returns true if the given machine instr is mapped to an index,
489     /// otherwise returns false.
490     bool hasIndex(const MachineInstr *instr) const {
491       return (mi2iMap.find(instr) != mi2iMap.end());
492     }
493
494     /// Returns the base index for the given instruction.
495     SlotIndex getInstructionIndex(const MachineInstr *instr) const {
496       Mi2IndexMap::const_iterator itr = mi2iMap.find(instr);
497       assert(itr != mi2iMap.end() && "Instruction not found in maps.");
498       return itr->second;
499     }
500
501     /// Returns the instruction for the given index, or null if the given
502     /// index has no instruction associated with it.
503     MachineInstr* getInstructionFromIndex(SlotIndex index) const {
504       return index.isValid() ? index.entry().getInstr() : 0;
505     }
506
507     /// Returns the next non-null index.
508     SlotIndex getNextNonNullIndex(SlotIndex index) {
509       SlotIndex nextNonNull = index.getNextIndex();
510
511       while (&nextNonNull.entry() != getTail() &&
512              getInstructionFromIndex(nextNonNull) == 0) {
513         nextNonNull = nextNonNull.getNextIndex();
514       }
515
516       return nextNonNull;
517     }
518
519     /// getIndexBefore - Returns the index of the last indexed instruction
520     /// before MI, or the the start index of its basic block.
521     /// MI is not required to have an index.
522     SlotIndex getIndexBefore(const MachineInstr *MI) const {
523       const MachineBasicBlock *MBB = MI->getParent();
524       assert(MBB && "MI must be inserted inna basic block");
525       MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
526       for (;;) {
527         if (I == B)
528           return getMBBStartIdx(MBB);
529         --I;
530         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
531         if (MapItr != mi2iMap.end())
532           return MapItr->second;
533       }
534     }
535
536     /// getIndexAfter - Returns the index of the first indexed instruction
537     /// after MI, or the end index of its basic block.
538     /// MI is not required to have an index.
539     SlotIndex getIndexAfter(const MachineInstr *MI) const {
540       const MachineBasicBlock *MBB = MI->getParent();
541       assert(MBB && "MI must be inserted inna basic block");
542       MachineBasicBlock::const_iterator I = MI, E = MBB->end();
543       for (;;) {
544         ++I;
545         if (I == E)
546           return getMBBEndIdx(MBB);
547         Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
548         if (MapItr != mi2iMap.end())
549           return MapItr->second;
550       }
551     }
552
553     /// Return the (start,end) range of the given basic block number.
554     const std::pair<SlotIndex, SlotIndex> &
555     getMBBRange(unsigned Num) const {
556       return MBBRanges[Num];
557     }
558
559     /// Return the (start,end) range of the given basic block.
560     const std::pair<SlotIndex, SlotIndex> &
561     getMBBRange(const MachineBasicBlock *MBB) const {
562       return getMBBRange(MBB->getNumber());
563     }
564
565     /// Returns the first index in the given basic block number.
566     SlotIndex getMBBStartIdx(unsigned Num) const {
567       return getMBBRange(Num).first;
568     }
569
570     /// Returns the first index in the given basic block.
571     SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
572       return getMBBRange(mbb).first;
573     }
574
575     /// Returns the last index in the given basic block number.
576     SlotIndex getMBBEndIdx(unsigned Num) const {
577       return getMBBRange(Num).second;
578     }
579
580     /// Returns the last index in the given basic block.
581     SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
582       return getMBBRange(mbb).second;
583     }
584
585     /// Returns the basic block which the given index falls in.
586     MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
587       if (MachineInstr *MI = getInstructionFromIndex(index))
588         return MI->getParent();
589       SmallVectorImpl<IdxMBBPair>::const_iterator I =
590         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
591       // Take the pair containing the index
592       SmallVectorImpl<IdxMBBPair>::const_iterator J =
593         ((I != idx2MBBMap.end() && I->first > index) ||
594          (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
595
596       assert(J != idx2MBBMap.end() && J->first <= index &&
597              index < getMBBEndIdx(J->second) &&
598              "index does not correspond to an MBB");
599       return J->second;
600     }
601
602     bool findLiveInMBBs(SlotIndex start, SlotIndex end,
603                         SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
604       SmallVectorImpl<IdxMBBPair>::const_iterator itr =
605         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
606       bool resVal = false;
607
608       while (itr != idx2MBBMap.end()) {
609         if (itr->first >= end)
610           break;
611         mbbs.push_back(itr->second);
612         resVal = true;
613         ++itr;
614       }
615       return resVal;
616     }
617
618     /// Returns the MBB covering the given range, or null if the range covers
619     /// more than one basic block.
620     MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
621
622       assert(start < end && "Backwards ranges not allowed.");
623
624       SmallVectorImpl<IdxMBBPair>::const_iterator itr =
625         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
626
627       if (itr == idx2MBBMap.end()) {
628         itr = prior(itr);
629         return itr->second;
630       }
631
632       // Check that we don't cross the boundary into this block.
633       if (itr->first < end)
634         return 0;
635
636       itr = prior(itr);
637
638       if (itr->first <= start)
639         return itr->second;
640
641       return 0;
642     }
643
644     /// Insert the given machine instruction into the mapping. Returns the
645     /// assigned index.
646     /// If Late is set and there are null indexes between mi's neighboring
647     /// instructions, create the new index after the null indexes instead of
648     /// before them.
649     SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late = false) {
650       assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
651       // Numbering DBG_VALUE instructions could cause code generation to be
652       // affected by debug information.
653       assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
654
655       assert(mi->getParent() != 0 && "Instr must be added to function.");
656
657       // Get the entries where mi should be inserted.
658       IndexListEntry *prevEntry, *nextEntry;
659       if (Late) {
660         // Insert mi's index immediately before the following instruction.
661         nextEntry = &getIndexAfter(mi).entry();
662         prevEntry = nextEntry->getPrev();
663       } else {
664         // Insert mi's index immediately after the preceeding instruction.
665         prevEntry = &getIndexBefore(mi).entry();
666         nextEntry = prevEntry->getNext();
667       }
668
669       // Get a number for the new instr, or 0 if there's no room currently.
670       // In the latter case we'll force a renumber later.
671       unsigned dist = ((nextEntry->getIndex() - prevEntry->getIndex())/2) & ~3u;
672       unsigned newNumber = prevEntry->getIndex() + dist;
673
674       // Insert a new list entry for mi.
675       IndexListEntry *newEntry = createEntry(mi, newNumber);
676       insert(nextEntry, newEntry);
677
678       // Renumber locally if we need to.
679       if (dist == 0)
680         renumberIndexes(newEntry);
681
682       SlotIndex newIndex(newEntry, SlotIndex::Slot_Block);
683       mi2iMap.insert(std::make_pair(mi, newIndex));
684       return newIndex;
685     }
686
687     /// Remove the given machine instruction from the mapping.
688     void removeMachineInstrFromMaps(MachineInstr *mi) {
689       // remove index -> MachineInstr and
690       // MachineInstr -> index mappings
691       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
692       if (mi2iItr != mi2iMap.end()) {
693         IndexListEntry *miEntry(&mi2iItr->second.entry());        
694         assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
695         // FIXME: Eventually we want to actually delete these indexes.
696         miEntry->setInstr(0);
697         mi2iMap.erase(mi2iItr);
698       }
699     }
700
701     /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
702     /// maps used by register allocator.
703     void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
704       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
705       if (mi2iItr == mi2iMap.end())
706         return;
707       SlotIndex replaceBaseIndex = mi2iItr->second;
708       IndexListEntry *miEntry(&replaceBaseIndex.entry());
709       assert(miEntry->getInstr() == mi &&
710              "Mismatched instruction in index tables.");
711       miEntry->setInstr(newMI);
712       mi2iMap.erase(mi2iItr);
713       mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
714     }
715
716     /// Add the given MachineBasicBlock into the maps.
717     void insertMBBInMaps(MachineBasicBlock *mbb) {
718       MachineFunction::iterator nextMBB =
719         llvm::next(MachineFunction::iterator(mbb));
720       IndexListEntry *startEntry = createEntry(0, 0);
721       IndexListEntry *stopEntry = createEntry(0, 0);
722       IndexListEntry *nextEntry = 0;
723
724       if (nextMBB == mbb->getParent()->end()) {
725         nextEntry = getTail();
726       } else {
727         nextEntry = &getMBBStartIdx(nextMBB).entry();
728       }
729
730       insert(nextEntry, startEntry);
731       insert(nextEntry, stopEntry);
732
733       SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
734       SlotIndex endIdx(nextEntry, SlotIndex::Slot_Block);
735
736       assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
737              "Blocks must be added in order");
738       MBBRanges.push_back(std::make_pair(startIdx, endIdx));
739
740       idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
741
742       renumberIndexes();
743       std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
744     }
745
746   };
747
748
749   // Specialize IntervalMapInfo for half-open slot index intervals.
750   template <typename> struct IntervalMapInfo;
751   template <> struct IntervalMapInfo<SlotIndex> {
752     static inline bool startLess(const SlotIndex &x, const SlotIndex &a) {
753       return x < a;
754     }
755     static inline bool stopLess(const SlotIndex &b, const SlotIndex &x) {
756       return b <= x;
757     }
758     static inline bool adjacent(const SlotIndex &a, const SlotIndex &b) {
759       return a == b;
760     }
761   };
762
763 }
764
765 #endif // LLVM_CODEGEN_LIVEINDEX_H