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. The SlotIndex
17 // class does hold a PHI bit, which determines whether the index relates to a
18 // PHI use or def point, or an actual instruction. See the SlotIndex class
19 // description for futher information.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CODEGEN_SLOTINDEXES_H
23 #define LLVM_CODEGEN_SLOTINDEXES_H
25 #include "llvm/ADT/PointerIntPair.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/Support/Allocator.h"
31 #include "llvm/Support/ErrorHandling.h"
35 /// This class represents an entry in the slot index list held in the
36 /// SlotIndexes pass. It should not be used directly. See the
37 /// SlotIndex & SlotIndexes classes for the public interface to this
39 class IndexListEntry {
42 static const unsigned EMPTY_KEY_INDEX = ~0U & ~3U,
43 TOMBSTONE_KEY_INDEX = ~0U & ~7U;
45 IndexListEntry *next, *prev;
51 typedef enum { EMPTY_KEY, TOMBSTONE_KEY } ReservedEntryType;
53 // This constructor is only to be used by getEmptyKeyEntry
54 // & getTombstoneKeyEntry. It sets index to the given
55 // value and mi to zero.
56 IndexListEntry(ReservedEntryType r) : mi(0) {
58 case EMPTY_KEY: index = EMPTY_KEY_INDEX; break;
59 case TOMBSTONE_KEY: index = TOMBSTONE_KEY_INDEX; break;
60 default: assert(false && "Invalid value for constructor.");
68 IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {
69 if (index == EMPTY_KEY_INDEX || index == TOMBSTONE_KEY_INDEX) {
70 llvm_report_error("Attempt to create invalid index. "
71 "Available indexes may have been exhausted?.");
75 MachineInstr* getInstr() const { return mi; }
76 void setInstr(MachineInstr *mi) {
77 assert(index != EMPTY_KEY_INDEX && index != TOMBSTONE_KEY_INDEX &&
78 "Attempt to modify reserved index.");
82 unsigned getIndex() const { return index; }
83 void setIndex(unsigned index) {
84 assert(index != EMPTY_KEY_INDEX && index != TOMBSTONE_KEY_INDEX &&
85 "Attempt to set index to invalid value.");
86 assert(this->index != EMPTY_KEY_INDEX &&
87 this->index != TOMBSTONE_KEY_INDEX &&
88 "Attempt to reset reserved index value.");
92 IndexListEntry* getNext() { return next; }
93 const IndexListEntry* getNext() const { return next; }
94 void setNext(IndexListEntry *next) {
95 assert(index != EMPTY_KEY_INDEX && index != TOMBSTONE_KEY_INDEX &&
96 "Attempt to modify reserved index.");
100 IndexListEntry* getPrev() { return prev; }
101 const IndexListEntry* getPrev() const { return prev; }
102 void setPrev(IndexListEntry *prev) {
103 assert(index != EMPTY_KEY_INDEX && index != TOMBSTONE_KEY_INDEX &&
104 "Attempt to modify reserved index.");
108 // This function returns the index list entry that is to be used for empty
110 static IndexListEntry* getEmptyKeyEntry();
112 // This function returns the index list entry that is to be used for
113 // tombstone SlotIndex keys.
114 static IndexListEntry* getTombstoneKeyEntry();
117 // Specialize PointerLikeTypeTraits for IndexListEntry.
119 class PointerLikeTypeTraits<IndexListEntry*> {
121 static inline void* getAsVoidPointer(IndexListEntry *p) {
124 static inline IndexListEntry* getFromVoidPointer(void *p) {
125 return static_cast<IndexListEntry*>(p);
127 enum { NumLowBitsAvailable = 3 };
130 /// SlotIndex - An opaque wrapper around machine indexes.
132 friend class SlotIndexes;
133 friend struct DenseMapInfo<SlotIndex>;
136 static const unsigned PHI_BIT = 1 << 2;
138 PointerIntPair<IndexListEntry*, 3, unsigned> lie;
140 SlotIndex(IndexListEntry *entry, unsigned phiAndSlot)
141 : lie(entry, phiAndSlot) {
142 assert(entry != 0 && "Attempt to construct index with 0 pointer.");
145 IndexListEntry& entry() const {
146 return *lie.getPointer();
149 int getIndex() const {
150 return entry().getIndex() | getSlot();
153 static inline unsigned getHashValue(const SlotIndex &v) {
154 IndexListEntry *ptrVal = &v.entry();
155 return (unsigned((intptr_t)ptrVal) >> 4) ^
156 (unsigned((intptr_t)ptrVal) >> 9);
161 // FIXME: Ugh. This is public because LiveIntervalAnalysis is still using it
162 // for some spill weight stuff. Fix that, then make this private.
163 enum Slot { LOAD, USE, DEF, STORE, NUM };
165 static inline SlotIndex getEmptyKey() {
166 return SlotIndex(IndexListEntry::getEmptyKeyEntry(), 0);
169 static inline SlotIndex getTombstoneKey() {
170 return SlotIndex(IndexListEntry::getTombstoneKeyEntry(), 0);
173 /// Construct an invalid index.
174 SlotIndex() : lie(IndexListEntry::getEmptyKeyEntry(), 0) {}
176 // Construct a new slot index from the given one, set the phi flag on the
177 // new index to the value of the phi parameter.
178 SlotIndex(const SlotIndex &li, bool phi)
179 : lie(&li.entry(), phi ? PHI_BIT | li.getSlot() : (unsigned)li.getSlot()){
180 assert(lie.getPointer() != 0 &&
181 "Attempt to construct index with 0 pointer.");
184 // Construct a new slot index from the given one, set the phi flag on the
185 // new index to the value of the phi parameter, and the slot to the new slot.
186 SlotIndex(const SlotIndex &li, bool phi, Slot s)
187 : lie(&li.entry(), phi ? PHI_BIT | s : (unsigned)s) {
188 assert(lie.getPointer() != 0 &&
189 "Attempt to construct index with 0 pointer.");
192 /// Returns true if this is a valid index. Invalid indicies do
193 /// not point into an index table, and cannot be compared.
194 bool isValid() const {
195 return (lie.getPointer() != 0) && (lie.getPointer()->getIndex() != 0);
198 /// Print this index to the given raw_ostream.
199 void print(raw_ostream &os) const;
201 /// Dump this index to stderr.
204 /// Compare two SlotIndex objects for equality.
205 bool operator==(SlotIndex other) const {
206 return getIndex() == other.getIndex();
208 /// Compare two SlotIndex objects for inequality.
209 bool operator!=(SlotIndex other) const {
210 return getIndex() != other.getIndex();
213 /// Compare two SlotIndex objects. Return true if the first index
214 /// is strictly lower than the second.
215 bool operator<(SlotIndex other) const {
216 return getIndex() < other.getIndex();
218 /// Compare two SlotIndex objects. Return true if the first index
219 /// is lower than, or equal to, the second.
220 bool operator<=(SlotIndex other) const {
221 return getIndex() <= other.getIndex();
224 /// Compare two SlotIndex objects. Return true if the first index
225 /// is greater than the second.
226 bool operator>(SlotIndex other) const {
227 return getIndex() > other.getIndex();
230 /// Compare two SlotIndex objects. Return true if the first index
231 /// is greater than, or equal to, the second.
232 bool operator>=(SlotIndex other) const {
233 return getIndex() >= other.getIndex();
236 /// Return the distance from this index to the given one.
237 int distance(SlotIndex other) const {
238 return other.getIndex() - getIndex();
241 /// Returns the slot for this SlotIndex.
242 Slot getSlot() const {
243 return static_cast<Slot>(lie.getInt() & ~PHI_BIT);
246 /// Returns the state of the PHI bit.
248 return lie.getInt() & PHI_BIT;
251 /// Returns the base index for associated with this index. The base index
252 /// is the one associated with the LOAD slot for the instruction pointed to
254 SlotIndex getBaseIndex() const {
255 return getLoadIndex();
258 /// Returns the boundary index for associated with this index. The boundary
259 /// index is the one associated with the LOAD slot for the instruction
260 /// pointed to by this index.
261 SlotIndex getBoundaryIndex() const {
262 return getStoreIndex();
265 /// Returns the index of the LOAD slot for the instruction pointed to by
267 SlotIndex getLoadIndex() const {
268 return SlotIndex(&entry(), SlotIndex::LOAD);
271 /// Returns the index of the USE slot for the instruction pointed to by
273 SlotIndex getUseIndex() const {
274 return SlotIndex(&entry(), SlotIndex::USE);
277 /// Returns the index of the DEF slot for the instruction pointed to by
279 SlotIndex getDefIndex() const {
280 return SlotIndex(&entry(), SlotIndex::DEF);
283 /// Returns the index of the STORE slot for the instruction pointed to by
285 SlotIndex getStoreIndex() const {
286 return SlotIndex(&entry(), SlotIndex::STORE);
289 /// Returns the next slot in the index list. This could be either the
290 /// next slot for the instruction pointed to by this index or, if this
291 /// index is a STORE, the first slot for the next instruction.
292 /// WARNING: This method is considerably more expensive than the methods
293 /// that return specific slots (getUseIndex(), etc). If you can - please
294 /// use one of those methods.
295 SlotIndex getNextSlot() const {
297 if (s == SlotIndex::STORE) {
298 return SlotIndex(entry().getNext(), SlotIndex::LOAD);
300 return SlotIndex(&entry(), s + 1);
303 /// Returns the next index. This is the index corresponding to the this
304 /// index's slot, but for the next instruction.
305 SlotIndex getNextIndex() const {
306 return SlotIndex(entry().getNext(), getSlot());
309 /// Returns the previous slot in the index list. This could be either the
310 /// previous slot for the instruction pointed to by this index or, if this
311 /// index is a LOAD, the last slot for the previous instruction.
312 /// WARNING: This method is considerably more expensive than the methods
313 /// that return specific slots (getUseIndex(), etc). If you can - please
314 /// use one of those methods.
315 SlotIndex getPrevSlot() const {
317 if (s == SlotIndex::LOAD) {
318 return SlotIndex(entry().getPrev(), SlotIndex::STORE);
320 return SlotIndex(&entry(), s - 1);
323 /// Returns the previous index. This is the index corresponding to this
324 /// index's slot, but for the previous instruction.
325 SlotIndex getPrevIndex() const {
326 return SlotIndex(entry().getPrev(), getSlot());
331 /// DenseMapInfo specialization for SlotIndex.
333 struct DenseMapInfo<SlotIndex> {
334 static inline SlotIndex getEmptyKey() {
335 return SlotIndex::getEmptyKey();
337 static inline SlotIndex getTombstoneKey() {
338 return SlotIndex::getTombstoneKey();
340 static inline unsigned getHashValue(const SlotIndex &v) {
341 return SlotIndex::getHashValue(v);
343 static inline bool isEqual(const SlotIndex &LHS, const SlotIndex &RHS) {
348 template <> struct isPodLike<SlotIndex> { static const bool value = true; };
351 inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
356 typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
358 inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
362 inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
366 struct Idx2MBBCompare {
367 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
368 return LHS.first < RHS.first;
372 /// SlotIndexes pass.
374 /// This pass assigns indexes to each instruction.
375 class SlotIndexes : public MachineFunctionPass {
379 IndexListEntry *indexListHead;
380 unsigned functionSize;
382 typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
385 /// MBB2IdxMap - The indexes of the first and last instructions in the
386 /// specified basic block.
387 typedef DenseMap<const MachineBasicBlock*,
388 std::pair<SlotIndex, SlotIndex> > MBB2IdxMap;
389 MBB2IdxMap mbb2IdxMap;
391 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
393 std::vector<IdxMBBPair> idx2MBBMap;
395 typedef DenseMap<const MachineBasicBlock*, SlotIndex> TerminatorGapsMap;
396 TerminatorGapsMap terminatorGaps;
398 // IndexListEntry allocator.
399 BumpPtrAllocator ileAllocator;
401 IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
402 IndexListEntry *entry =
403 static_cast<IndexListEntry*>(
404 ileAllocator.Allocate(sizeof(IndexListEntry),
405 alignof<IndexListEntry>()));
407 new (entry) IndexListEntry(mi, index);
413 assert(indexListHead == 0 && "Zero entry non-null at initialisation.");
414 indexListHead = createEntry(0, ~0U);
415 indexListHead->setNext(0);
416 indexListHead->setPrev(indexListHead);
421 ileAllocator.Reset();
424 IndexListEntry* getTail() {
425 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
426 return indexListHead->getPrev();
429 const IndexListEntry* getTail() const {
430 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
431 return indexListHead->getPrev();
434 // Returns true if the index list is empty.
435 bool empty() const { return (indexListHead == getTail()); }
437 IndexListEntry* front() {
438 assert(!empty() && "front() called on empty index list.");
439 return indexListHead;
442 const IndexListEntry* front() const {
443 assert(!empty() && "front() called on empty index list.");
444 return indexListHead;
447 IndexListEntry* back() {
448 assert(!empty() && "back() called on empty index list.");
449 return getTail()->getPrev();
452 const IndexListEntry* back() const {
453 assert(!empty() && "back() called on empty index list.");
454 return getTail()->getPrev();
457 /// Insert a new entry before itr.
458 void insert(IndexListEntry *itr, IndexListEntry *val) {
459 assert(itr != 0 && "itr should not be null.");
460 IndexListEntry *prev = itr->getPrev();
464 if (itr != indexListHead) {
473 /// Push a new entry on to the end of the list.
474 void push_back(IndexListEntry *val) {
475 insert(getTail(), val);
481 SlotIndexes() : MachineFunctionPass(&ID), indexListHead(0) {}
483 virtual void getAnalysisUsage(AnalysisUsage &au) const;
484 virtual void releaseMemory();
486 virtual bool runOnMachineFunction(MachineFunction &fn);
488 /// Dump the indexes.
491 /// Renumber the index list, providing space for new instructions.
492 void renumberIndexes();
494 /// Returns the zero index for this analysis.
495 SlotIndex getZeroIndex() {
496 assert(front()->getIndex() == 0 && "First index is not 0?");
497 return SlotIndex(front(), 0);
500 /// Returns the invalid index marker for this analysis.
501 SlotIndex getInvalidIndex() {
502 return getZeroIndex();
505 /// Returns the distance between the highest and lowest indexes allocated
507 unsigned getIndexesLength() const {
508 assert(front()->getIndex() == 0 &&
509 "Initial index isn't zero?");
511 return back()->getIndex();
514 /// Returns the number of instructions in the function.
515 unsigned getFunctionSize() const {
519 /// Returns true if the given machine instr is mapped to an index,
520 /// otherwise returns false.
521 bool hasIndex(const MachineInstr *instr) const {
522 return (mi2iMap.find(instr) != mi2iMap.end());
525 /// Returns the base index for the given instruction.
526 SlotIndex getInstructionIndex(const MachineInstr *instr) const {
527 Mi2IndexMap::const_iterator itr = mi2iMap.find(instr);
528 assert(itr != mi2iMap.end() && "Instruction not found in maps.");
532 /// Returns the instruction for the given index, or null if the given
533 /// index has no instruction associated with it.
534 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
535 return index.entry().getInstr();
538 /// Returns the next non-null index.
539 SlotIndex getNextNonNullIndex(SlotIndex index) {
540 SlotIndex nextNonNull = index.getNextIndex();
542 while (&nextNonNull.entry() != getTail() &&
543 getInstructionFromIndex(nextNonNull) == 0) {
544 nextNonNull = nextNonNull.getNextIndex();
550 /// Returns the first index in the given basic block.
551 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
552 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
553 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
554 return itr->second.first;
557 /// Returns the last index in the given basic block.
558 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
559 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
560 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
561 return itr->second.second;
564 /// Returns the terminator gap for the given index.
565 SlotIndex getTerminatorGap(const MachineBasicBlock *mbb) {
566 TerminatorGapsMap::iterator itr = terminatorGaps.find(mbb);
567 assert(itr != terminatorGaps.end() &&
568 "All MBBs should have terminator gaps in their indexes.");
572 /// Returns the basic block which the given index falls in.
573 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
574 std::vector<IdxMBBPair>::const_iterator I =
575 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
576 // Take the pair containing the index
577 std::vector<IdxMBBPair>::const_iterator J =
578 ((I != idx2MBBMap.end() && I->first > index) ||
579 (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
581 assert(J != idx2MBBMap.end() && J->first <= index &&
582 index <= getMBBEndIdx(J->second) &&
583 "index does not correspond to an MBB");
587 bool findLiveInMBBs(SlotIndex start, SlotIndex end,
588 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
589 std::vector<IdxMBBPair>::const_iterator itr =
590 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
593 while (itr != idx2MBBMap.end()) {
594 if (itr->first >= end)
596 mbbs.push_back(itr->second);
603 /// Return a list of MBBs that can be reach via any branches or
605 bool findReachableMBBs(SlotIndex start, SlotIndex end,
606 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
607 std::vector<IdxMBBPair>::const_iterator itr =
608 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
611 while (itr != idx2MBBMap.end()) {
612 if (itr->first > end)
614 MachineBasicBlock *mbb = itr->second;
615 if (getMBBEndIdx(mbb) > end)
617 for (MachineBasicBlock::succ_iterator si = mbb->succ_begin(),
618 se = mbb->succ_end(); si != se; ++si)
626 /// Returns the MBB covering the given range, or null if the range covers
627 /// more than one basic block.
628 MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
630 assert(start < end && "Backwards ranges not allowed.");
632 std::vector<IdxMBBPair>::const_iterator itr =
633 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
635 if (itr == idx2MBBMap.end()) {
640 // Check that we don't cross the boundary into this block.
641 if (itr->first < end)
646 if (itr->first <= start)
652 /// Insert the given machine instruction into the mapping. Returns the
654 SlotIndex insertMachineInstrInMaps(MachineInstr *mi,
655 bool *deferredRenumber = 0) {
656 assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
658 MachineBasicBlock *mbb = mi->getParent();
660 assert(mbb != 0 && "Instr must be added to function.");
662 MBB2IdxMap::iterator mbbRangeItr = mbb2IdxMap.find(mbb);
664 assert(mbbRangeItr != mbb2IdxMap.end() &&
665 "Instruction's parent MBB has not been added to SlotIndexes.");
667 MachineBasicBlock::iterator miItr(mi);
668 bool needRenumber = false;
669 IndexListEntry *newEntry;
671 IndexListEntry *prevEntry;
672 if (miItr == mbb->begin()) {
673 // If mi is at the mbb beginning, get the prev index from the mbb.
674 prevEntry = &mbbRangeItr->second.first.entry();
676 // Otherwise get it from the previous instr.
677 MachineBasicBlock::iterator pItr(prior(miItr));
678 prevEntry = &getInstructionIndex(pItr).entry();
681 // Get next entry from previous entry.
682 IndexListEntry *nextEntry = prevEntry->getNext();
684 // Get a number for the new instr, or 0 if there's no room currently.
685 // In the latter case we'll force a renumber later.
686 unsigned dist = nextEntry->getIndex() - prevEntry->getIndex();
687 unsigned newNumber = dist > SlotIndex::NUM ?
688 prevEntry->getIndex() + ((dist >> 1) & ~3U) : 0;
690 if (newNumber == 0) {
694 // Insert a new list entry for mi.
695 newEntry = createEntry(mi, newNumber);
696 insert(nextEntry, newEntry);
698 SlotIndex newIndex(newEntry, SlotIndex::LOAD);
699 mi2iMap.insert(std::make_pair(mi, newIndex));
701 if (miItr == mbb->end()) {
702 // If this is the last instr in the MBB then we need to fix up the bb
704 mbbRangeItr->second.second = SlotIndex(newEntry, SlotIndex::STORE);
707 // Renumber if we need to.
709 if (deferredRenumber == 0)
712 *deferredRenumber = true;
718 /// Add all instructions in the vector to the index list. This method will
719 /// defer renumbering until all instrs have been added, and should be
720 /// preferred when adding multiple instrs.
721 void insertMachineInstrsInMaps(SmallVectorImpl<MachineInstr*> &mis) {
722 bool renumber = false;
724 for (SmallVectorImpl<MachineInstr*>::iterator
725 miItr = mis.begin(), miEnd = mis.end();
726 miItr != miEnd; ++miItr) {
727 insertMachineInstrInMaps(*miItr, &renumber);
735 /// Remove the given machine instruction from the mapping.
736 void removeMachineInstrFromMaps(MachineInstr *mi) {
737 // remove index -> MachineInstr and
738 // MachineInstr -> index mappings
739 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
740 if (mi2iItr != mi2iMap.end()) {
741 IndexListEntry *miEntry(&mi2iItr->second.entry());
742 assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
743 // FIXME: Eventually we want to actually delete these indexes.
744 miEntry->setInstr(0);
745 mi2iMap.erase(mi2iItr);
749 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
750 /// maps used by register allocator.
751 void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
752 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
753 if (mi2iItr == mi2iMap.end())
755 SlotIndex replaceBaseIndex = mi2iItr->second;
756 IndexListEntry *miEntry(&replaceBaseIndex.entry());
757 assert(miEntry->getInstr() == mi &&
758 "Mismatched instruction in index tables.");
759 miEntry->setInstr(newMI);
760 mi2iMap.erase(mi2iItr);
761 mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
769 #endif // LLVM_CODEGEN_LIVEINDEX_H