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