063ac9900d991b05dd8fe7922b86e23e4e26a6be
[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 = 2*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   public:
431     static char ID;
432
433     SlotIndexes() : MachineFunctionPass(ID), indexListHead(0) {
434       initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
435     }
436
437     virtual void getAnalysisUsage(AnalysisUsage &au) const;
438     virtual void releaseMemory(); 
439
440     virtual bool runOnMachineFunction(MachineFunction &fn);
441
442     /// Dump the indexes.
443     void dump() const;
444
445     /// Renumber the index list, providing space for new instructions.
446     void renumberIndexes();
447
448     /// Returns the zero index for this analysis.
449     SlotIndex getZeroIndex() {
450       assert(front()->getIndex() == 0 && "First index is not 0?");
451       return SlotIndex(front(), 0);
452     }
453
454     /// Returns the base index of the last slot in this analysis.
455     SlotIndex getLastIndex() {
456       return SlotIndex(back(), 0);
457     }
458
459     /// Returns the invalid index marker for this analysis.
460     SlotIndex getInvalidIndex() {
461       return getZeroIndex();
462     }
463
464     /// Returns the distance between the highest and lowest indexes allocated
465     /// so far.
466     unsigned getIndexesLength() const {
467       assert(front()->getIndex() == 0 &&
468              "Initial index isn't zero?");
469
470       return back()->getIndex();
471     }
472
473     /// Returns the number of instructions in the function.
474     unsigned getFunctionSize() const {
475       return functionSize;
476     }
477
478     /// Returns true if the given machine instr is mapped to an index,
479     /// otherwise returns false.
480     bool hasIndex(const MachineInstr *instr) const {
481       return (mi2iMap.find(instr) != mi2iMap.end());
482     }
483
484     /// Returns the base index for the given instruction.
485     SlotIndex getInstructionIndex(const MachineInstr *instr) const {
486       Mi2IndexMap::const_iterator itr = mi2iMap.find(instr);
487       assert(itr != mi2iMap.end() && "Instruction not found in maps.");
488       return itr->second;
489     }
490
491     /// Returns the instruction for the given index, or null if the given
492     /// index has no instruction associated with it.
493     MachineInstr* getInstructionFromIndex(SlotIndex index) const {
494       return index.isValid() ? index.entry().getInstr() : 0;
495     }
496
497     /// Returns the next non-null index.
498     SlotIndex getNextNonNullIndex(SlotIndex index) {
499       SlotIndex nextNonNull = index.getNextIndex();
500
501       while (&nextNonNull.entry() != getTail() &&
502              getInstructionFromIndex(nextNonNull) == 0) {
503         nextNonNull = nextNonNull.getNextIndex();
504       }
505
506       return nextNonNull;
507     }
508
509     /// Return the (start,end) range of the given basic block.
510     const std::pair<SlotIndex, SlotIndex> &
511     getMBBRange(const MachineBasicBlock *mbb) const {
512       MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
513       assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
514       return itr->second;
515     }
516
517     /// Returns the first index in the given basic block.
518     SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
519       return getMBBRange(mbb).first;
520     }
521
522     /// Returns the last index in the given basic block.
523     SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
524       return getMBBRange(mbb).second;
525     }
526
527     /// Returns the basic block which the given index falls in.
528     MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
529       std::vector<IdxMBBPair>::const_iterator I =
530         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
531       // Take the pair containing the index
532       std::vector<IdxMBBPair>::const_iterator J =
533         ((I != idx2MBBMap.end() && I->first > index) ||
534          (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
535
536       assert(J != idx2MBBMap.end() && J->first <= index &&
537              index < getMBBEndIdx(J->second) &&
538              "index does not correspond to an MBB");
539       return J->second;
540     }
541
542     bool findLiveInMBBs(SlotIndex start, SlotIndex end,
543                         SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
544       std::vector<IdxMBBPair>::const_iterator itr =
545         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
546       bool resVal = false;
547
548       while (itr != idx2MBBMap.end()) {
549         if (itr->first >= end)
550           break;
551         mbbs.push_back(itr->second);
552         resVal = true;
553         ++itr;
554       }
555       return resVal;
556     }
557
558     /// Returns the MBB covering the given range, or null if the range covers
559     /// more than one basic block.
560     MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
561
562       assert(start < end && "Backwards ranges not allowed.");
563
564       std::vector<IdxMBBPair>::const_iterator itr =
565         std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
566
567       if (itr == idx2MBBMap.end()) {
568         itr = prior(itr);
569         return itr->second;
570       }
571
572       // Check that we don't cross the boundary into this block.
573       if (itr->first < end)
574         return 0;
575
576       itr = prior(itr);
577
578       if (itr->first <= start)
579         return itr->second;
580
581       return 0;
582     }
583
584     /// Insert the given machine instruction into the mapping. Returns the
585     /// assigned index.
586     SlotIndex insertMachineInstrInMaps(MachineInstr *mi) {
587       assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
588       // Numbering DBG_VALUE instructions could cause code generation to be
589       // affected by debug information.
590       assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
591
592       MachineBasicBlock *mbb = mi->getParent();
593
594       assert(mbb != 0 && "Instr must be added to function.");
595
596       MBB2IdxMap::iterator mbbRangeItr = mbb2IdxMap.find(mbb);
597
598       assert(mbbRangeItr != mbb2IdxMap.end() &&
599              "Instruction's parent MBB has not been added to SlotIndexes.");
600
601       MachineBasicBlock::iterator miItr(mi);
602       bool needRenumber = false;
603       IndexListEntry *newEntry;
604       // Get previous index, considering that not all instructions are indexed.
605       IndexListEntry *prevEntry;
606       for (;;) {
607         // If mi is at the mbb beginning, get the prev index from the mbb.
608         if (miItr == mbb->begin()) {
609           prevEntry = &mbbRangeItr->second.first.entry();
610           break;
611         }
612         // Otherwise rewind until we find a mapped instruction.
613         Mi2IndexMap::const_iterator itr = mi2iMap.find(--miItr);
614         if (itr != mi2iMap.end()) {
615           prevEntry = &itr->second.entry();
616           break;
617         }
618       }
619
620       // Get next entry from previous entry.
621       IndexListEntry *nextEntry = prevEntry->getNext();
622
623       // Get a number for the new instr, or 0 if there's no room currently.
624       // In the latter case we'll force a renumber later.
625       unsigned dist = nextEntry->getIndex() - prevEntry->getIndex();
626       unsigned newNumber = dist > SlotIndex::NUM ?
627         prevEntry->getIndex() + ((dist >> 1) & ~3U) : 0;
628
629       if (newNumber == 0) {
630         needRenumber = true;
631       }
632
633       // Insert a new list entry for mi.
634       newEntry = createEntry(mi, newNumber);
635       insert(nextEntry, newEntry);
636   
637       SlotIndex newIndex(newEntry, SlotIndex::LOAD);
638       mi2iMap.insert(std::make_pair(mi, newIndex));
639
640       if (miItr == mbb->end()) {
641         // If this is the last instr in the MBB then we need to fix up the bb
642         // range:
643         mbbRangeItr->second.second = SlotIndex(newEntry, SlotIndex::STORE);
644       }
645
646       // Renumber if we need to.
647       if (needRenumber)
648         renumberIndexes();
649
650       return newIndex;
651     }
652
653     /// Remove the given machine instruction from the mapping.
654     void removeMachineInstrFromMaps(MachineInstr *mi) {
655       // remove index -> MachineInstr and
656       // MachineInstr -> index mappings
657       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
658       if (mi2iItr != mi2iMap.end()) {
659         IndexListEntry *miEntry(&mi2iItr->second.entry());        
660         assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
661         // FIXME: Eventually we want to actually delete these indexes.
662         miEntry->setInstr(0);
663         mi2iMap.erase(mi2iItr);
664       }
665     }
666
667     /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
668     /// maps used by register allocator.
669     void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
670       Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
671       if (mi2iItr == mi2iMap.end())
672         return;
673       SlotIndex replaceBaseIndex = mi2iItr->second;
674       IndexListEntry *miEntry(&replaceBaseIndex.entry());
675       assert(miEntry->getInstr() == mi &&
676              "Mismatched instruction in index tables.");
677       miEntry->setInstr(newMI);
678       mi2iMap.erase(mi2iItr);
679       mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
680     }
681
682     /// Add the given MachineBasicBlock into the maps.
683     void insertMBBInMaps(MachineBasicBlock *mbb) {
684       MachineFunction::iterator nextMBB =
685         llvm::next(MachineFunction::iterator(mbb));
686       IndexListEntry *startEntry = createEntry(0, 0);
687       IndexListEntry *stopEntry = createEntry(0, 0);
688       IndexListEntry *nextEntry = 0;
689
690       if (nextMBB == mbb->getParent()->end()) {
691         nextEntry = getTail();
692       } else {
693         nextEntry = &getMBBStartIdx(nextMBB).entry();
694       }
695
696       insert(nextEntry, startEntry);
697       insert(nextEntry, stopEntry);
698
699       SlotIndex startIdx(startEntry, SlotIndex::LOAD);
700       SlotIndex endIdx(nextEntry, SlotIndex::LOAD);
701
702       mbb2IdxMap.insert(
703         std::make_pair(mbb, std::make_pair(startIdx, endIdx)));
704
705       idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
706
707       if (MachineFunction::iterator(mbb) != mbb->getParent()->begin()) {
708         // Have to update the end index of the previous block.
709         MachineBasicBlock *priorMBB =
710           llvm::prior(MachineFunction::iterator(mbb));
711         mbb2IdxMap[priorMBB].second = startIdx;
712       }
713
714       renumberIndexes();
715       std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
716
717     }
718
719   };
720
721
722   // Specialize IntervalMapInfo for half-open slot index intervals.
723   template <typename> struct IntervalMapInfo;
724   template <> struct IntervalMapInfo<SlotIndex> {
725     static inline bool startLess(const SlotIndex &x, const SlotIndex &a) {
726       return x < a;
727     }
728     static inline bool stopLess(const SlotIndex &b, const SlotIndex &x) {
729       return b <= x;
730     }
731     static inline bool adjacent(const SlotIndex &a, const SlotIndex &b) {
732       return a == b;
733     }
734   };
735
736 }
737
738 #endif // LLVM_CODEGEN_LIVEINDEX_H