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