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