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"
34 /// This class represents an entry in the slot index list held in the
35 /// SlotIndexes pass. It should not be used directly. See the
36 /// SlotIndex & SlotIndexes classes for the public interface to this
38 class IndexListEntry {
39 friend class SlotIndex;
40 friend class SlotIndexes;
44 IndexListEntry *next, *prev;
50 IndexListEntry(MachineInstr *mi, unsigned index)
51 : mi(mi), index(index) {}
53 MachineInstr* getInstr() const { return mi; }
54 void setInstr(MachineInstr *mi) { this->mi = mi; }
56 unsigned getIndex() const { return index; }
57 void setIndex(unsigned index) { this->index = index; }
59 IndexListEntry* getNext() { return next; }
60 const IndexListEntry* getNext() const { return next; }
61 void setNext(IndexListEntry *next) { this->next = next; }
63 IndexListEntry* getPrev() { return prev; }
64 const IndexListEntry* getPrev() const { return prev; }
65 void setPrev(IndexListEntry *prev) { this->prev = prev; }
68 bool operator==(const IndexListEntry &other) const {
69 assert(getIndex() != other.getIndex() || this == &other &&
70 "Non-equal index list entries compare equal.");
71 return getIndex() == other.getIndex();
74 bool operator!=(const IndexListEntry &other) const {
75 return getIndex() != other.getIndex();
78 bool operator<(const IndexListEntry &other) const {
79 return getIndex() < other.getIndex();
82 bool operator<=(const IndexListEntry &other) const {
83 return getIndex() <= other.getIndex();
86 bool operator>(const IndexListEntry &other) const {
87 return getIndex() > other.getIndex();
90 bool operator>=(const IndexListEntry &other) const {
91 return getIndex() >= other.getIndex();
94 int distance(const IndexListEntry &other) const {
95 return other.getIndex() - getIndex();
100 // Specialize PointerLikeTypeTraits for IndexListEntry.
102 class PointerLikeTypeTraits<IndexListEntry*> {
104 static inline void* getAsVoidPointer(IndexListEntry *p) {
107 static inline IndexListEntry* getFromVoidPointer(void *p) {
108 return static_cast<IndexListEntry*>(p);
110 enum { NumLowBitsAvailable = 3 };
113 /// SlotIndex - An opaque wrapper around machine indexes.
115 friend class SlotIndexes;
116 friend class DenseMapInfo<SlotIndex>;
120 // FIXME: Is there any way to statically allocate these things and have
121 // them 8-byte aligned?
122 static std::auto_ptr<IndexListEntry> emptyKeyPtr, tombstoneKeyPtr;
123 static const unsigned PHI_BIT = 1 << 2;
125 PointerIntPair<IndexListEntry*, 3, unsigned> lie;
127 SlotIndex(IndexListEntry *entry, unsigned phiAndSlot)
128 : lie(entry, phiAndSlot) {
129 assert(entry != 0 && "Attempt to construct index with 0 pointer.");
132 IndexListEntry& entry() const {
133 assert(lie.getPointer() != 0 && "Use of invalid index.");
134 return *lie.getPointer();
137 int getIndex() const {
138 return entry().getIndex() | getSlot();
141 static inline unsigned getHashValue(const SlotIndex &v) {
142 IndexListEntry *ptrVal = &v.entry();
143 return (unsigned((intptr_t)ptrVal) >> 4) ^
144 (unsigned((intptr_t)ptrVal) >> 9);
149 // FIXME: Ugh. This is public because LiveIntervalAnalysis is still using it
150 // for some spill weight stuff. Fix that, then make this private.
151 enum Slot { LOAD, USE, DEF, STORE, NUM };
153 static inline SlotIndex getEmptyKey() {
154 // FIXME: How do we guarantee these numbers don't get allocated to
156 if (emptyKeyPtr.get() == 0)
157 emptyKeyPtr.reset(new IndexListEntry(0, ~0U & ~3U));
159 return SlotIndex(emptyKeyPtr.get(), 0);
162 static inline SlotIndex getTombstoneKey() {
163 // FIXME: How do we guarantee these numbers don't get allocated to
165 if (tombstoneKeyPtr.get() == 0)
166 tombstoneKeyPtr.reset(new IndexListEntry(0, ~0U & ~7U));
168 return SlotIndex(tombstoneKeyPtr.get(), 0);
171 /// Construct an invalid index.
172 SlotIndex() : lie(&getEmptyKey().entry(), 0) {}
174 // Construct a new slot index from the given one, set the phi flag on the
175 // new index to the value of the phi parameter.
176 SlotIndex(const SlotIndex &li, bool phi)
177 : lie(&li.entry(), phi ? PHI_BIT & li.getSlot() : (unsigned)li.getSlot()){
178 assert(lie.getPointer() != 0 &&
179 "Attempt to construct index with 0 pointer.");
182 // Construct a new slot index from the given one, set the phi flag on the
183 // new index to the value of the phi parameter, and the slot to the new slot.
184 SlotIndex(const SlotIndex &li, bool phi, Slot s)
185 : lie(&li.entry(), phi ? PHI_BIT & s : (unsigned)s) {
186 assert(lie.getPointer() != 0 &&
187 "Attempt to construct index with 0 pointer.");
190 /// Returns true if this is a valid index. Invalid indicies do
191 /// not point into an index table, and cannot be compared.
192 bool isValid() const {
193 return (lie.getPointer() != 0) && (lie.getPointer()->getIndex() != 0);
196 /// Print this index to the given raw_ostream.
197 void print(raw_ostream &os) const;
199 /// Dump this index to stderr.
202 /// Compare two SlotIndex objects for equality.
203 bool operator==(SlotIndex other) const {
204 return getIndex() == other.getIndex();
206 /// Compare two SlotIndex objects for inequality.
207 bool operator!=(SlotIndex other) const {
208 return getIndex() != other.getIndex();
211 /// Compare two SlotIndex objects. Return true if the first index
212 /// is strictly lower than the second.
213 bool operator<(SlotIndex other) const {
214 return getIndex() < other.getIndex();
216 /// Compare two SlotIndex objects. Return true if the first index
217 /// is lower than, or equal to, the second.
218 bool operator<=(SlotIndex other) const {
219 return getIndex() <= other.getIndex();
222 /// Compare two SlotIndex objects. Return true if the first index
223 /// is greater than the second.
224 bool operator>(SlotIndex other) const {
225 return getIndex() > other.getIndex();
228 /// Compare two SlotIndex objects. Return true if the first index
229 /// is greater than, or equal to, the second.
230 bool operator>=(SlotIndex other) const {
231 return getIndex() >= other.getIndex();
234 /// Return the distance from this index to the given one.
235 int distance(SlotIndex other) const {
236 return other.getIndex() - getIndex();
239 /// Returns the slot for this SlotIndex.
240 Slot getSlot() const {
241 return static_cast<Slot>(lie.getInt() & ~PHI_BIT);
244 /// Returns the state of the PHI bit.
246 return lie.getInt() & PHI_BIT;
249 /// Returns the base index for associated with this index. The base index
250 /// is the one associated with the LOAD slot for the instruction pointed to
252 SlotIndex getBaseIndex() const {
253 return getLoadIndex();
256 /// Returns the boundary index for associated with this index. The boundary
257 /// index is the one associated with the LOAD slot for the instruction
258 /// pointed to by this index.
259 SlotIndex getBoundaryIndex() const {
260 return getStoreIndex();
263 /// Returns the index of the LOAD slot for the instruction pointed to by
265 SlotIndex getLoadIndex() const {
266 return SlotIndex(&entry(), SlotIndex::LOAD);
269 /// Returns the index of the USE slot for the instruction pointed to by
271 SlotIndex getUseIndex() const {
272 return SlotIndex(&entry(), SlotIndex::USE);
275 /// Returns the index of the DEF slot for the instruction pointed to by
277 SlotIndex getDefIndex() const {
278 return SlotIndex(&entry(), SlotIndex::DEF);
281 /// Returns the index of the STORE slot for the instruction pointed to by
283 SlotIndex getStoreIndex() const {
284 return SlotIndex(&entry(), SlotIndex::STORE);
287 /// Returns the next slot in the index list. This could be either the
288 /// next slot for the instruction pointed to by this index or, if this
289 /// index is a STORE, the first slot for the next instruction.
290 /// WARNING: This method is considerably more expensive than the methods
291 /// that return specific slots (getUseIndex(), etc). If you can - please
292 /// use one of those methods.
293 SlotIndex getNextSlot() const {
295 if (s == SlotIndex::STORE) {
296 return SlotIndex(entry().getNext(), SlotIndex::LOAD);
298 return SlotIndex(&entry(), s + 1);
301 /// Returns the next index. This is the index corresponding to the this
302 /// index's slot, but for the next instruction.
303 SlotIndex getNextIndex() const {
304 return SlotIndex(entry().getNext(), getSlot());
307 /// Returns the previous slot in the index list. This could be either the
308 /// previous slot for the instruction pointed to by this index or, if this
309 /// index is a LOAD, the last slot for the previous instruction.
310 /// WARNING: This method is considerably more expensive than the methods
311 /// that return specific slots (getUseIndex(), etc). If you can - please
312 /// use one of those methods.
313 SlotIndex getPrevSlot() const {
315 if (s == SlotIndex::LOAD) {
316 return SlotIndex(entry().getPrev(), SlotIndex::STORE);
318 return SlotIndex(&entry(), s - 1);
321 /// Returns the previous index. This is the index corresponding to this
322 /// index's slot, but for the previous instruction.
323 SlotIndex getPrevIndex() const {
324 return SlotIndex(entry().getPrev(), getSlot());
329 /// DenseMapInfo specialization for SlotIndex.
331 struct DenseMapInfo<SlotIndex> {
332 static inline SlotIndex getEmptyKey() {
333 return SlotIndex::getEmptyKey();
335 static inline SlotIndex getTombstoneKey() {
336 return SlotIndex::getTombstoneKey();
338 static inline unsigned getHashValue(const SlotIndex &v) {
339 return SlotIndex::getHashValue(v);
341 static inline bool isEqual(const SlotIndex &LHS, const SlotIndex &RHS) {
344 static inline bool isPod() { return false; }
347 inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
352 typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
354 inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
358 inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
362 struct Idx2MBBCompare {
363 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
364 return LHS.first < RHS.first;
368 /// SlotIndexes pass.
370 /// This pass assigns indexes to each instruction.
371 class SlotIndexes : public MachineFunctionPass {
375 IndexListEntry *indexListHead;
376 unsigned functionSize;
378 typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
381 /// MBB2IdxMap - The indexes of the first and last instructions in the
382 /// specified basic block.
383 typedef DenseMap<const MachineBasicBlock*,
384 std::pair<SlotIndex, SlotIndex> > MBB2IdxMap;
385 MBB2IdxMap mbb2IdxMap;
387 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
389 std::vector<IdxMBBPair> idx2MBBMap;
391 typedef DenseMap<const MachineBasicBlock*, SlotIndex> TerminatorGapsMap;
392 TerminatorGapsMap terminatorGaps;
394 // IndexListEntry allocator.
395 BumpPtrAllocator ileAllocator;
397 IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
398 IndexListEntry *entry =
399 static_cast<IndexListEntry*>(
400 ileAllocator.Allocate(sizeof(IndexListEntry),
401 alignof<IndexListEntry>()));
403 new (entry) IndexListEntry(mi, index);
409 assert(indexListHead == 0 && "Zero entry non-null at initialisation.");
410 indexListHead = createEntry(0, ~0U);
411 indexListHead->setNext(0);
412 indexListHead->setPrev(indexListHead);
417 ileAllocator.Reset();
420 IndexListEntry* getTail() {
421 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
422 return indexListHead->getPrev();
425 const IndexListEntry* getTail() const {
426 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
427 return indexListHead->getPrev();
430 // Returns true if the index list is empty.
431 bool empty() const { return (indexListHead == getTail()); }
433 IndexListEntry* front() {
434 assert(!empty() && "front() called on empty index list.");
435 return indexListHead;
438 const IndexListEntry* front() const {
439 assert(!empty() && "front() called on empty index list.");
440 return indexListHead;
443 IndexListEntry* back() {
444 assert(!empty() && "back() called on empty index list.");
445 return getTail()->getPrev();
448 const IndexListEntry* back() const {
449 assert(!empty() && "back() called on empty index list.");
450 return getTail()->getPrev();
453 /// Insert a new entry before itr.
454 void insert(IndexListEntry *itr, IndexListEntry *val) {
455 assert(itr != 0 && "itr should not be null.");
456 IndexListEntry *prev = itr->getPrev();
460 if (itr != indexListHead) {
469 /// Push a new entry on to the end of the list.
470 void push_back(IndexListEntry *val) {
471 insert(getTail(), val);
477 SlotIndexes() : MachineFunctionPass(&ID), indexListHead(0) {}
479 virtual void getAnalysisUsage(AnalysisUsage &au) const;
480 virtual void releaseMemory();
482 virtual bool runOnMachineFunction(MachineFunction &fn);
484 /// Dump the indexes.
487 /// Renumber the index list, providing space for new instructions.
490 /// Returns the zero index for this analysis.
491 SlotIndex getZeroIndex() {
492 assert(front()->getIndex() == 0 && "First index is not 0?");
493 return SlotIndex(front(), 0);
496 /// Returns the invalid index marker for this analysis.
497 SlotIndex getInvalidIndex() {
498 return getZeroIndex();
501 /// Returns the distance between the highest and lowest indexes allocated
503 unsigned getIndexesLength() const {
504 assert(front()->getIndex() == 0 &&
505 "Initial index isn't zero?");
507 return back()->getIndex();
510 /// Returns the number of instructions in the function.
511 unsigned getFunctionSize() const {
515 /// Returns true if the given machine instr is mapped to an index,
516 /// otherwise returns false.
517 bool hasIndex(const MachineInstr *instr) const {
518 return (mi2iMap.find(instr) != mi2iMap.end());
521 /// Returns the base index for the given instruction.
522 SlotIndex getInstructionIndex(const MachineInstr *instr) const {
523 Mi2IndexMap::const_iterator itr = mi2iMap.find(instr);
524 assert(itr != mi2iMap.end() && "Instruction not found in maps.");
528 /// Returns the instruction for the given index, or null if the given
529 /// index has no instruction associated with it.
530 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
531 return index.entry().getInstr();
534 /// Returns the next non-null index.
535 SlotIndex getNextNonNullIndex(SlotIndex index) {
536 SlotIndex nextNonNull = index.getNextIndex();
538 while (&nextNonNull.entry() != getTail() &&
539 getInstructionFromIndex(nextNonNull) == 0) {
540 nextNonNull = nextNonNull.getNextIndex();
546 /// Returns the first index in the given basic block.
547 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
548 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
549 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
550 return itr->second.first;
553 /// Returns the last index in the given basic block.
554 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
555 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
556 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
557 return itr->second.second;
560 /// Returns the terminator gap for the given index.
561 SlotIndex getTerminatorGap(const MachineBasicBlock *mbb) {
562 TerminatorGapsMap::iterator itr = terminatorGaps.find(mbb);
563 assert(itr != terminatorGaps.end() &&
564 "All MBBs should have terminator gaps in their indexes.");
568 /// Returns the basic block which the given index falls in.
569 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
570 std::vector<IdxMBBPair>::const_iterator I =
571 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
572 // Take the pair containing the index
573 std::vector<IdxMBBPair>::const_iterator J =
574 ((I != idx2MBBMap.end() && I->first > index) ||
575 (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
577 assert(J != idx2MBBMap.end() && J->first <= index &&
578 index <= getMBBEndIdx(J->second) &&
579 "index does not correspond to an MBB");
583 bool findLiveInMBBs(SlotIndex start, SlotIndex end,
584 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
585 std::vector<IdxMBBPair>::const_iterator itr =
586 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
589 while (itr != idx2MBBMap.end()) {
590 if (itr->first >= end)
592 mbbs.push_back(itr->second);
599 /// Return a list of MBBs that can be reach via any branches or
601 bool findReachableMBBs(SlotIndex start, SlotIndex end,
602 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
603 std::vector<IdxMBBPair>::const_iterator itr =
604 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
607 while (itr != idx2MBBMap.end()) {
608 if (itr->first > end)
610 MachineBasicBlock *mbb = itr->second;
611 if (getMBBEndIdx(mbb) > end)
613 for (MachineBasicBlock::succ_iterator si = mbb->succ_begin(),
614 se = mbb->succ_end(); si != se; ++si)
622 /// Returns the MBB covering the given range, or null if the range covers
623 /// more than one basic block.
624 MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
626 assert(start < end && "Backwards ranges not allowed.");
628 std::vector<IdxMBBPair>::const_iterator itr =
629 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
631 if (itr == idx2MBBMap.end()) {
636 // Check that we don't cross the boundary into this block.
637 if (itr->first < end)
642 if (itr->first <= start)
648 /// Returns true if there is a gap in the numbering before the given index.
649 bool hasGapBeforeInstr(SlotIndex index) {
650 index = index.getBaseIndex();
651 SlotIndex prevIndex = index.getPrevIndex();
653 if (prevIndex == getZeroIndex())
656 if (getInstructionFromIndex(prevIndex) == 0)
659 if (prevIndex.distance(index) >= 2 * SlotIndex::NUM)
665 /// Returns true if there is a gap in the numbering after the given index.
666 bool hasGapAfterInstr(SlotIndex index) const {
667 // Not implemented yet.
669 "SlotIndexes::hasGapAfterInstr(SlotIndex) not implemented yet.");
673 /// findGapBeforeInstr - Find an empty instruction slot before the
674 /// specified index. If "Furthest" is true, find one that's furthest
675 /// away from the index (but before any index that's occupied).
676 // FIXME: This whole method should go away in future. It should
677 // always be possible to insert code between existing indices.
678 SlotIndex findGapBeforeInstr(SlotIndex index, bool furthest = false) {
679 if (index == getZeroIndex())
680 return getInvalidIndex();
682 index = index.getBaseIndex();
683 SlotIndex prevIndex = index.getPrevIndex();
685 if (prevIndex == getZeroIndex())
686 return getInvalidIndex();
688 // Try to reuse existing index objects with null-instrs.
689 if (getInstructionFromIndex(prevIndex) == 0) {
691 while (getInstructionFromIndex(prevIndex) == 0 &&
692 prevIndex != getZeroIndex()) {
693 prevIndex = prevIndex.getPrevIndex();
696 prevIndex = prevIndex.getNextIndex();
699 assert(getInstructionFromIndex(prevIndex) == 0 && "Index list is broken.");
704 int dist = prevIndex.distance(index);
706 // Double check that the spacing between this instruction and
708 assert(dist >= SlotIndex::NUM &&
709 "Distance between indexes too small.");
711 // If there's no gap return an invalid index.
712 if (dist < 2*SlotIndex::NUM) {
713 return getInvalidIndex();
716 // Otherwise insert new index entries into the list using the
717 // gap in the numbering.
718 IndexListEntry *newEntry =
719 createEntry(0, prevIndex.entry().getIndex() + SlotIndex::NUM);
721 insert(&index.entry(), newEntry);
723 // And return a pointer to the entry at the start of the gap.
724 return index.getPrevIndex();
727 /// Insert the given machine instruction into the mapping at the given
729 void insertMachineInstrInMaps(MachineInstr *mi, SlotIndex index) {
730 index = index.getBaseIndex();
731 IndexListEntry *miEntry = &index.entry();
732 assert(miEntry->getInstr() == 0 && "Index already in use.");
733 miEntry->setInstr(mi);
735 assert(mi2iMap.find(mi) == mi2iMap.end() &&
736 "MachineInstr already has an index.");
738 mi2iMap.insert(std::make_pair(mi, index));
741 /// Remove the given machine instruction from the mapping.
742 void removeMachineInstrFromMaps(MachineInstr *mi) {
743 // remove index -> MachineInstr and
744 // MachineInstr -> index mappings
745 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
746 if (mi2iItr != mi2iMap.end()) {
747 IndexListEntry *miEntry(&mi2iItr->second.entry());
748 assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
749 // FIXME: Eventually we want to actually delete these indexes.
750 miEntry->setInstr(0);
751 mi2iMap.erase(mi2iItr);
755 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
756 /// maps used by register allocator.
757 void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
758 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
759 if (mi2iItr == mi2iMap.end())
761 SlotIndex replaceBaseIndex = mi2iItr->second;
762 IndexListEntry *miEntry(&replaceBaseIndex.entry());
763 assert(miEntry->getInstr() == mi &&
764 "Mismatched instruction in index tables.");
765 miEntry->setInstr(newMI);
766 mi2iMap.erase(mi2iItr);
767 mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
775 #endif // LLVM_CODEGEN_LIVEINDEX_H