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