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