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