1 //===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CODEGEN_SLOTINDEXES_H
20 #define LLVM_CODEGEN_SLOTINDEXES_H
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"
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
36 class IndexListEntry {
37 static const unsigned EMPTY_KEY_INDEX = ~0U & ~3U,
38 TOMBSTONE_KEY_INDEX = ~0U & ~7U;
40 IndexListEntry *next, *prev;
46 typedef enum { EMPTY_KEY, TOMBSTONE_KEY } ReservedEntryType;
48 // This constructor is only to be used by getEmptyKeyEntry
49 // & getTombstoneKeyEntry. It sets index to the given
50 // value and mi to zero.
51 IndexListEntry(ReservedEntryType r) : mi(0) {
53 case EMPTY_KEY: index = EMPTY_KEY_INDEX; break;
54 case TOMBSTONE_KEY: index = TOMBSTONE_KEY_INDEX; break;
55 default: assert(false && "Invalid value for constructor.");
63 IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {
64 assert(index != EMPTY_KEY_INDEX && index != TOMBSTONE_KEY_INDEX &&
65 "Attempt to create invalid index. "
66 "Available indexes may have been exhausted?.");
69 bool isValid() const {
70 return (index != EMPTY_KEY_INDEX && index != TOMBSTONE_KEY_INDEX);
73 MachineInstr* getInstr() const { return mi; }
74 void setInstr(MachineInstr *mi) {
75 assert(isValid() && "Attempt to modify reserved index.");
79 unsigned getIndex() const { return index; }
80 void setIndex(unsigned index) {
81 assert(index != EMPTY_KEY_INDEX && index != TOMBSTONE_KEY_INDEX &&
82 "Attempt to set index to invalid value.");
83 assert(isValid() && "Attempt to reset reserved index value.");
87 IndexListEntry* getNext() { return next; }
88 const IndexListEntry* getNext() const { return next; }
89 void setNext(IndexListEntry *next) {
90 assert(isValid() && "Attempt to modify reserved index.");
94 IndexListEntry* getPrev() { return prev; }
95 const IndexListEntry* getPrev() const { return prev; }
96 void setPrev(IndexListEntry *prev) {
97 assert(isValid() && "Attempt to modify reserved index.");
101 // This function returns the index list entry that is to be used for empty
103 static IndexListEntry* getEmptyKeyEntry();
105 // This function returns the index list entry that is to be used for
106 // tombstone SlotIndex keys.
107 static IndexListEntry* getTombstoneKeyEntry();
110 // Specialize PointerLikeTypeTraits for IndexListEntry.
112 class PointerLikeTypeTraits<IndexListEntry*> {
114 static inline void* getAsVoidPointer(IndexListEntry *p) {
117 static inline IndexListEntry* getFromVoidPointer(void *p) {
118 return static_cast<IndexListEntry*>(p);
120 enum { NumLowBitsAvailable = 3 };
123 /// SlotIndex - An opaque wrapper around machine indexes.
125 friend class SlotIndexes;
126 friend struct DenseMapInfo<SlotIndex>;
128 enum Slot { LOAD, USE, DEF, STORE, NUM };
130 PointerIntPair<IndexListEntry*, 2, unsigned> lie;
132 SlotIndex(IndexListEntry *entry, unsigned slot)
134 assert(entry != 0 && "Attempt to construct index with 0 pointer.");
137 IndexListEntry& entry() const {
138 return *lie.getPointer();
141 int getIndex() const {
142 return entry().getIndex() | getSlot();
145 /// Returns the slot for this SlotIndex.
146 Slot getSlot() const {
147 return static_cast<Slot>(lie.getInt());
150 static inline unsigned getHashValue(const SlotIndex &v) {
151 IndexListEntry *ptrVal = &v.entry();
152 return (unsigned((intptr_t)ptrVal) >> 4) ^
153 (unsigned((intptr_t)ptrVal) >> 9);
157 static inline SlotIndex getEmptyKey() {
158 return SlotIndex(IndexListEntry::getEmptyKeyEntry(), 0);
161 static inline SlotIndex getTombstoneKey() {
162 return SlotIndex(IndexListEntry::getTombstoneKeyEntry(), 0);
165 /// Construct an invalid index.
166 SlotIndex() : lie(IndexListEntry::getEmptyKeyEntry(), 0) {}
168 // Construct a new slot index from the given one, and set the slot.
169 SlotIndex(const SlotIndex &li, Slot s)
170 : lie(&li.entry(), unsigned(s)) {
171 assert(lie.getPointer() != 0 &&
172 "Attempt to construct index with 0 pointer.");
175 /// Returns true if this is a valid index. Invalid indicies do
176 /// not point into an index table, and cannot be compared.
177 bool isValid() const {
178 IndexListEntry *entry = lie.getPointer();
179 return ((entry!= 0) && (entry->isValid()));
182 /// Print this index to the given raw_ostream.
183 void print(raw_ostream &os) const;
185 /// Dump this index to stderr.
188 /// Compare two SlotIndex objects for equality.
189 bool operator==(SlotIndex other) const {
190 return getIndex() == other.getIndex();
192 /// Compare two SlotIndex objects for inequality.
193 bool operator!=(SlotIndex other) const {
194 return getIndex() != other.getIndex();
197 /// Compare two SlotIndex objects. Return true if the first index
198 /// is strictly lower than the second.
199 bool operator<(SlotIndex other) const {
200 return getIndex() < other.getIndex();
202 /// Compare two SlotIndex objects. Return true if the first index
203 /// is lower than, or equal to, the second.
204 bool operator<=(SlotIndex other) const {
205 return getIndex() <= other.getIndex();
208 /// Compare two SlotIndex objects. Return true if the first index
209 /// is greater than the second.
210 bool operator>(SlotIndex other) const {
211 return getIndex() > other.getIndex();
214 /// Compare two SlotIndex objects. Return true if the first index
215 /// is greater than, or equal to, the second.
216 bool operator>=(SlotIndex other) const {
217 return getIndex() >= other.getIndex();
220 /// Return the distance from this index to the given one.
221 int distance(SlotIndex other) const {
222 return other.getIndex() - getIndex();
225 /// isLoad - Return true if this is a LOAD slot.
226 bool isLoad() const {
227 return getSlot() == LOAD;
230 /// isDef - Return true if this is a DEF slot.
232 return getSlot() == DEF;
235 /// isUse - Return true if this is a USE slot.
237 return getSlot() == USE;
240 /// isStore - Return true if this is a STORE slot.
241 bool isStore() const {
242 return getSlot() == STORE;
245 /// Returns the base index for associated with this index. The base index
246 /// is the one associated with the LOAD slot for the instruction pointed to
248 SlotIndex getBaseIndex() const {
249 return getLoadIndex();
252 /// Returns the boundary index for associated with this index. The boundary
253 /// index is the one associated with the LOAD slot for the instruction
254 /// pointed to by this index.
255 SlotIndex getBoundaryIndex() const {
256 return getStoreIndex();
259 /// Returns the index of the LOAD slot for the instruction pointed to by
261 SlotIndex getLoadIndex() const {
262 return SlotIndex(&entry(), SlotIndex::LOAD);
265 /// Returns the index of the USE slot for the instruction pointed to by
267 SlotIndex getUseIndex() const {
268 return SlotIndex(&entry(), SlotIndex::USE);
271 /// Returns the index of the DEF slot for the instruction pointed to by
273 SlotIndex getDefIndex() const {
274 return SlotIndex(&entry(), SlotIndex::DEF);
277 /// Returns the index of the STORE slot for the instruction pointed to by
279 SlotIndex getStoreIndex() const {
280 return SlotIndex(&entry(), SlotIndex::STORE);
283 /// Returns the next slot in the index list. This could be either the
284 /// next slot for the instruction pointed to by this index or, if this
285 /// index is a STORE, the first slot for the next instruction.
286 /// WARNING: This method is considerably more expensive than the methods
287 /// that return specific slots (getUseIndex(), etc). If you can - please
288 /// use one of those methods.
289 SlotIndex getNextSlot() const {
291 if (s == SlotIndex::STORE) {
292 return SlotIndex(entry().getNext(), SlotIndex::LOAD);
294 return SlotIndex(&entry(), s + 1);
297 /// Returns the next index. This is the index corresponding to the this
298 /// index's slot, but for the next instruction.
299 SlotIndex getNextIndex() const {
300 return SlotIndex(entry().getNext(), getSlot());
303 /// Returns the previous slot in the index list. This could be either the
304 /// previous slot for the instruction pointed to by this index or, if this
305 /// index is a LOAD, the last slot for the previous instruction.
306 /// WARNING: This method is considerably more expensive than the methods
307 /// that return specific slots (getUseIndex(), etc). If you can - please
308 /// use one of those methods.
309 SlotIndex getPrevSlot() const {
311 if (s == SlotIndex::LOAD) {
312 return SlotIndex(entry().getPrev(), SlotIndex::STORE);
314 return SlotIndex(&entry(), s - 1);
317 /// Returns the previous index. This is the index corresponding to this
318 /// index's slot, but for the previous instruction.
319 SlotIndex getPrevIndex() const {
320 return SlotIndex(entry().getPrev(), getSlot());
325 /// DenseMapInfo specialization for SlotIndex.
327 struct DenseMapInfo<SlotIndex> {
328 static inline SlotIndex getEmptyKey() {
329 return SlotIndex::getEmptyKey();
331 static inline SlotIndex getTombstoneKey() {
332 return SlotIndex::getTombstoneKey();
334 static inline unsigned getHashValue(const SlotIndex &v) {
335 return SlotIndex::getHashValue(v);
337 static inline bool isEqual(const SlotIndex &LHS, const SlotIndex &RHS) {
342 template <> struct isPodLike<SlotIndex> { static const bool value = true; };
345 inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
350 typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
352 inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
356 inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
360 struct Idx2MBBCompare {
361 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
362 return LHS.first < RHS.first;
366 /// SlotIndexes pass.
368 /// This pass assigns indexes to each instruction.
369 class SlotIndexes : public MachineFunctionPass {
373 IndexListEntry *indexListHead;
374 unsigned functionSize;
376 typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
379 /// MBB2IdxMap - The indexes of the first and last instructions in the
380 /// specified basic block.
381 typedef DenseMap<const MachineBasicBlock*,
382 std::pair<SlotIndex, SlotIndex> > MBB2IdxMap;
383 MBB2IdxMap mbb2IdxMap;
385 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
387 std::vector<IdxMBBPair> idx2MBBMap;
389 // IndexListEntry allocator.
390 BumpPtrAllocator ileAllocator;
392 IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
393 IndexListEntry *entry =
394 static_cast<IndexListEntry*>(
395 ileAllocator.Allocate(sizeof(IndexListEntry),
396 alignOf<IndexListEntry>()));
398 new (entry) IndexListEntry(mi, index);
404 assert(indexListHead == 0 && "Zero entry non-null at initialisation.");
405 indexListHead = createEntry(0, ~0U);
406 indexListHead->setNext(0);
407 indexListHead->setPrev(indexListHead);
412 ileAllocator.Reset();
415 IndexListEntry* getTail() {
416 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
417 return indexListHead->getPrev();
420 const IndexListEntry* getTail() const {
421 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
422 return indexListHead->getPrev();
425 // Returns true if the index list is empty.
426 bool empty() const { return (indexListHead == getTail()); }
428 IndexListEntry* front() {
429 assert(!empty() && "front() called on empty index list.");
430 return indexListHead;
433 const IndexListEntry* front() const {
434 assert(!empty() && "front() called on empty index list.");
435 return indexListHead;
438 IndexListEntry* back() {
439 assert(!empty() && "back() called on empty index list.");
440 return getTail()->getPrev();
443 const IndexListEntry* back() const {
444 assert(!empty() && "back() called on empty index list.");
445 return getTail()->getPrev();
448 /// Insert a new entry before itr.
449 void insert(IndexListEntry *itr, IndexListEntry *val) {
450 assert(itr != 0 && "itr should not be null.");
451 IndexListEntry *prev = itr->getPrev();
455 if (itr != indexListHead) {
464 /// Push a new entry on to the end of the list.
465 void push_back(IndexListEntry *val) {
466 insert(getTail(), val);
472 SlotIndexes() : MachineFunctionPass(ID), indexListHead(0) {
473 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
476 virtual void getAnalysisUsage(AnalysisUsage &au) const;
477 virtual void releaseMemory();
479 virtual bool runOnMachineFunction(MachineFunction &fn);
481 /// Dump the indexes.
484 /// Renumber the index list, providing space for new instructions.
485 void renumberIndexes();
487 /// Returns the zero index for this analysis.
488 SlotIndex getZeroIndex() {
489 assert(front()->getIndex() == 0 && "First index is not 0?");
490 return SlotIndex(front(), 0);
493 /// Returns the base index of the last slot in this analysis.
494 SlotIndex getLastIndex() {
495 return SlotIndex(back(), 0);
498 /// Returns the invalid index marker for this analysis.
499 SlotIndex getInvalidIndex() {
500 return getZeroIndex();
503 /// Returns the distance between the highest and lowest indexes allocated
505 unsigned getIndexesLength() const {
506 assert(front()->getIndex() == 0 &&
507 "Initial index isn't zero?");
509 return back()->getIndex();
512 /// Returns the number of instructions in the function.
513 unsigned getFunctionSize() const {
517 /// Returns true if the given machine instr is mapped to an index,
518 /// otherwise returns false.
519 bool hasIndex(const MachineInstr *instr) const {
520 return (mi2iMap.find(instr) != mi2iMap.end());
523 /// Returns the base index for the given instruction.
524 SlotIndex getInstructionIndex(const MachineInstr *instr) const {
525 Mi2IndexMap::const_iterator itr = mi2iMap.find(instr);
526 assert(itr != mi2iMap.end() && "Instruction not found in maps.");
530 /// Returns the instruction for the given index, or null if the given
531 /// index has no instruction associated with it.
532 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
533 return index.entry().getInstr();
536 /// Returns the next non-null index.
537 SlotIndex getNextNonNullIndex(SlotIndex index) {
538 SlotIndex nextNonNull = index.getNextIndex();
540 while (&nextNonNull.entry() != getTail() &&
541 getInstructionFromIndex(nextNonNull) == 0) {
542 nextNonNull = nextNonNull.getNextIndex();
548 /// Returns the first index in the given basic block.
549 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
550 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
551 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
552 return itr->second.first;
555 /// Returns the last index in the given basic block.
556 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
557 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
558 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
559 return itr->second.second;
562 /// Returns the basic block which the given index falls in.
563 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
564 std::vector<IdxMBBPair>::const_iterator I =
565 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
566 // Take the pair containing the index
567 std::vector<IdxMBBPair>::const_iterator J =
568 ((I != idx2MBBMap.end() && I->first > index) ||
569 (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
571 assert(J != idx2MBBMap.end() && J->first <= index &&
572 index < getMBBEndIdx(J->second) &&
573 "index does not correspond to an MBB");
577 bool findLiveInMBBs(SlotIndex start, SlotIndex end,
578 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
579 std::vector<IdxMBBPair>::const_iterator itr =
580 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
583 while (itr != idx2MBBMap.end()) {
584 if (itr->first >= end)
586 mbbs.push_back(itr->second);
593 /// Returns the MBB covering the given range, or null if the range covers
594 /// more than one basic block.
595 MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
597 assert(start < end && "Backwards ranges not allowed.");
599 std::vector<IdxMBBPair>::const_iterator itr =
600 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
602 if (itr == idx2MBBMap.end()) {
607 // Check that we don't cross the boundary into this block.
608 if (itr->first < end)
613 if (itr->first <= start)
619 /// Insert the given machine instruction into the mapping. Returns the
621 SlotIndex insertMachineInstrInMaps(MachineInstr *mi,
622 bool *deferredRenumber = 0) {
623 assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
625 MachineBasicBlock *mbb = mi->getParent();
627 assert(mbb != 0 && "Instr must be added to function.");
629 MBB2IdxMap::iterator mbbRangeItr = mbb2IdxMap.find(mbb);
631 assert(mbbRangeItr != mbb2IdxMap.end() &&
632 "Instruction's parent MBB has not been added to SlotIndexes.");
634 MachineBasicBlock::iterator miItr(mi);
635 bool needRenumber = false;
636 IndexListEntry *newEntry;
637 // Get previous index, considering that not all instructions are indexed.
638 IndexListEntry *prevEntry;
640 // If mi is at the mbb beginning, get the prev index from the mbb.
641 if (miItr == mbb->begin()) {
642 prevEntry = &mbbRangeItr->second.first.entry();
645 // Otherwise rewind until we find a mapped instruction.
646 Mi2IndexMap::const_iterator itr = mi2iMap.find(--miItr);
647 if (itr != mi2iMap.end()) {
648 prevEntry = &itr->second.entry();
653 // Get next entry from previous entry.
654 IndexListEntry *nextEntry = prevEntry->getNext();
656 // Get a number for the new instr, or 0 if there's no room currently.
657 // In the latter case we'll force a renumber later.
658 unsigned dist = nextEntry->getIndex() - prevEntry->getIndex();
659 unsigned newNumber = dist > SlotIndex::NUM ?
660 prevEntry->getIndex() + ((dist >> 1) & ~3U) : 0;
662 if (newNumber == 0) {
666 // Insert a new list entry for mi.
667 newEntry = createEntry(mi, newNumber);
668 insert(nextEntry, newEntry);
670 SlotIndex newIndex(newEntry, SlotIndex::LOAD);
671 mi2iMap.insert(std::make_pair(mi, newIndex));
673 if (miItr == mbb->end()) {
674 // If this is the last instr in the MBB then we need to fix up the bb
676 mbbRangeItr->second.second = SlotIndex(newEntry, SlotIndex::STORE);
679 // Renumber if we need to.
681 if (deferredRenumber == 0)
684 *deferredRenumber = true;
690 /// Add all instructions in the vector to the index list. This method will
691 /// defer renumbering until all instrs have been added, and should be
692 /// preferred when adding multiple instrs.
693 void insertMachineInstrsInMaps(SmallVectorImpl<MachineInstr*> &mis) {
694 bool renumber = false;
696 for (SmallVectorImpl<MachineInstr*>::iterator
697 miItr = mis.begin(), miEnd = mis.end();
698 miItr != miEnd; ++miItr) {
699 insertMachineInstrInMaps(*miItr, &renumber);
707 /// Remove the given machine instruction from the mapping.
708 void removeMachineInstrFromMaps(MachineInstr *mi) {
709 // remove index -> MachineInstr and
710 // MachineInstr -> index mappings
711 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
712 if (mi2iItr != mi2iMap.end()) {
713 IndexListEntry *miEntry(&mi2iItr->second.entry());
714 assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
715 // FIXME: Eventually we want to actually delete these indexes.
716 miEntry->setInstr(0);
717 mi2iMap.erase(mi2iItr);
721 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
722 /// maps used by register allocator.
723 void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
724 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
725 if (mi2iItr == mi2iMap.end())
727 SlotIndex replaceBaseIndex = mi2iItr->second;
728 IndexListEntry *miEntry(&replaceBaseIndex.entry());
729 assert(miEntry->getInstr() == mi &&
730 "Mismatched instruction in index tables.");
731 miEntry->setInstr(newMI);
732 mi2iMap.erase(mi2iItr);
733 mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
736 /// Add the given MachineBasicBlock into the maps.
737 void insertMBBInMaps(MachineBasicBlock *mbb) {
738 MachineFunction::iterator nextMBB =
739 llvm::next(MachineFunction::iterator(mbb));
740 IndexListEntry *startEntry = createEntry(0, 0);
741 IndexListEntry *stopEntry = createEntry(0, 0);
742 IndexListEntry *nextEntry = 0;
744 if (nextMBB == mbb->getParent()->end()) {
745 nextEntry = getTail();
747 nextEntry = &getMBBStartIdx(nextMBB).entry();
750 insert(nextEntry, startEntry);
751 insert(nextEntry, stopEntry);
753 SlotIndex startIdx(startEntry, SlotIndex::LOAD);
754 SlotIndex endIdx(nextEntry, SlotIndex::LOAD);
757 std::make_pair(mbb, std::make_pair(startIdx, endIdx)));
759 idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
761 if (MachineFunction::iterator(mbb) != mbb->getParent()->begin()) {
762 // Have to update the end index of the previous block.
763 MachineBasicBlock *priorMBB =
764 llvm::prior(MachineFunction::iterator(mbb));
765 mbb2IdxMap[priorMBB].second = startIdx;
769 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
778 #endif // LLVM_CODEGEN_LIVEINDEX_H