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;
43 IndexListEntry *next, *prev;
49 IndexListEntry(MachineInstr *mi, unsigned index)
50 : mi(mi), index(index) {}
52 MachineInstr* getInstr() const { return mi; }
53 void setInstr(MachineInstr *mi) { this->mi = mi; }
55 unsigned getIndex() const { return index; }
56 void setIndex(unsigned index) { this->index = index; }
58 IndexListEntry* getNext() { return next; }
59 const IndexListEntry* getNext() const { return next; }
60 void setNext(IndexListEntry *next) { this->next = next; }
62 IndexListEntry* getPrev() { return prev; }
63 const IndexListEntry* getPrev() const { return prev; }
64 void setPrev(IndexListEntry *prev) { this->prev = prev; }
67 // Specialize PointerLikeTypeTraits for IndexListEntry.
69 class PointerLikeTypeTraits<IndexListEntry*> {
71 static inline void* getAsVoidPointer(IndexListEntry *p) {
74 static inline IndexListEntry* getFromVoidPointer(void *p) {
75 return static_cast<IndexListEntry*>(p);
77 enum { NumLowBitsAvailable = 3 };
80 /// SlotIndex - An opaque wrapper around machine indexes.
82 friend class SlotIndexes;
83 friend class DenseMapInfo<SlotIndex>;
87 // FIXME: Is there any way to statically allocate these things and have
88 // them 8-byte aligned?
89 static std::auto_ptr<IndexListEntry> emptyKeyPtr, tombstoneKeyPtr;
90 static const unsigned PHI_BIT = 1 << 2;
92 PointerIntPair<IndexListEntry*, 3, unsigned> lie;
94 SlotIndex(IndexListEntry *entry, unsigned phiAndSlot)
95 : lie(entry, phiAndSlot) {
96 assert(entry != 0 && "Attempt to construct index with 0 pointer.");
99 IndexListEntry& entry() const {
100 assert(lie.getPointer() != 0 && "Use of invalid index.");
101 return *lie.getPointer();
104 int getIndex() const {
105 return entry().getIndex() | getSlot();
108 static inline unsigned getHashValue(const SlotIndex &v) {
109 IndexListEntry *ptrVal = &v.entry();
110 return (unsigned((intptr_t)ptrVal) >> 4) ^
111 (unsigned((intptr_t)ptrVal) >> 9);
116 // FIXME: Ugh. This is public because LiveIntervalAnalysis is still using it
117 // for some spill weight stuff. Fix that, then make this private.
118 enum Slot { LOAD, USE, DEF, STORE, NUM };
120 static inline SlotIndex getEmptyKey() {
121 // FIXME: How do we guarantee these numbers don't get allocated to
123 if (emptyKeyPtr.get() == 0)
124 emptyKeyPtr.reset(new IndexListEntry(0, ~0U & ~3U));
126 return SlotIndex(emptyKeyPtr.get(), 0);
129 static inline SlotIndex getTombstoneKey() {
130 // FIXME: How do we guarantee these numbers don't get allocated to
132 if (tombstoneKeyPtr.get() == 0)
133 tombstoneKeyPtr.reset(new IndexListEntry(0, ~0U & ~7U));
135 return SlotIndex(tombstoneKeyPtr.get(), 0);
138 /// Construct an invalid index.
139 SlotIndex() : lie(&getEmptyKey().entry(), 0) {}
141 // Construct a new slot index from the given one, set the phi flag on the
142 // new index to the value of the phi parameter.
143 SlotIndex(const SlotIndex &li, bool phi)
144 : lie(&li.entry(), phi ? PHI_BIT & li.getSlot() : (unsigned)li.getSlot()){
145 assert(lie.getPointer() != 0 &&
146 "Attempt to construct index with 0 pointer.");
149 // Construct a new slot index from the given one, set the phi flag on the
150 // new index to the value of the phi parameter, and the slot to the new slot.
151 SlotIndex(const SlotIndex &li, bool phi, Slot s)
152 : lie(&li.entry(), phi ? PHI_BIT & s : (unsigned)s) {
153 assert(lie.getPointer() != 0 &&
154 "Attempt to construct index with 0 pointer.");
157 /// Returns true if this is a valid index. Invalid indicies do
158 /// not point into an index table, and cannot be compared.
159 bool isValid() const {
160 return (lie.getPointer() != 0) && (lie.getPointer()->getIndex() != 0);
163 /// Print this index to the given raw_ostream.
164 void print(raw_ostream &os) const;
166 /// Dump this index to stderr.
169 /// Compare two SlotIndex objects for equality.
170 bool operator==(SlotIndex other) const {
171 return getIndex() == other.getIndex();
173 /// Compare two SlotIndex objects for inequality.
174 bool operator!=(SlotIndex other) const {
175 return getIndex() != other.getIndex();
178 /// Compare two SlotIndex objects. Return true if the first index
179 /// is strictly lower than the second.
180 bool operator<(SlotIndex other) const {
181 return getIndex() < other.getIndex();
183 /// Compare two SlotIndex objects. Return true if the first index
184 /// is lower than, or equal to, the second.
185 bool operator<=(SlotIndex other) const {
186 return getIndex() <= other.getIndex();
189 /// Compare two SlotIndex objects. Return true if the first index
190 /// is greater than the second.
191 bool operator>(SlotIndex other) const {
192 return getIndex() > other.getIndex();
195 /// Compare two SlotIndex objects. Return true if the first index
196 /// is greater than, or equal to, the second.
197 bool operator>=(SlotIndex other) const {
198 return getIndex() >= other.getIndex();
201 /// Return the distance from this index to the given one.
202 int distance(SlotIndex other) const {
203 return other.getIndex() - getIndex();
206 /// Returns the slot for this SlotIndex.
207 Slot getSlot() const {
208 return static_cast<Slot>(lie.getInt() & ~PHI_BIT);
211 /// Returns the state of the PHI bit.
213 return lie.getInt() & PHI_BIT;
216 /// Returns the base index for associated with this index. The base index
217 /// is the one associated with the LOAD slot for the instruction pointed to
219 SlotIndex getBaseIndex() const {
220 return getLoadIndex();
223 /// Returns the boundary index for associated with this index. The boundary
224 /// index is the one associated with the LOAD slot for the instruction
225 /// pointed to by this index.
226 SlotIndex getBoundaryIndex() const {
227 return getStoreIndex();
230 /// Returns the index of the LOAD slot for the instruction pointed to by
232 SlotIndex getLoadIndex() const {
233 return SlotIndex(&entry(), SlotIndex::LOAD);
236 /// Returns the index of the USE slot for the instruction pointed to by
238 SlotIndex getUseIndex() const {
239 return SlotIndex(&entry(), SlotIndex::USE);
242 /// Returns the index of the DEF slot for the instruction pointed to by
244 SlotIndex getDefIndex() const {
245 return SlotIndex(&entry(), SlotIndex::DEF);
248 /// Returns the index of the STORE slot for the instruction pointed to by
250 SlotIndex getStoreIndex() const {
251 return SlotIndex(&entry(), SlotIndex::STORE);
254 /// Returns the next slot in the index list. This could be either the
255 /// next slot for the instruction pointed to by this index or, if this
256 /// index is a STORE, the first slot for the next instruction.
257 /// WARNING: This method is considerably more expensive than the methods
258 /// that return specific slots (getUseIndex(), etc). If you can - please
259 /// use one of those methods.
260 SlotIndex getNextSlot() const {
262 if (s == SlotIndex::STORE) {
263 return SlotIndex(entry().getNext(), SlotIndex::LOAD);
265 return SlotIndex(&entry(), s + 1);
268 /// Returns the next index. This is the index corresponding to the this
269 /// index's slot, but for the next instruction.
270 SlotIndex getNextIndex() const {
271 return SlotIndex(entry().getNext(), getSlot());
274 /// Returns the previous slot in the index list. This could be either the
275 /// previous slot for the instruction pointed to by this index or, if this
276 /// index is a LOAD, the last slot for the previous instruction.
277 /// WARNING: This method is considerably more expensive than the methods
278 /// that return specific slots (getUseIndex(), etc). If you can - please
279 /// use one of those methods.
280 SlotIndex getPrevSlot() const {
282 if (s == SlotIndex::LOAD) {
283 return SlotIndex(entry().getPrev(), SlotIndex::STORE);
285 return SlotIndex(&entry(), s - 1);
288 /// Returns the previous index. This is the index corresponding to this
289 /// index's slot, but for the previous instruction.
290 SlotIndex getPrevIndex() const {
291 return SlotIndex(entry().getPrev(), getSlot());
296 /// DenseMapInfo specialization for SlotIndex.
298 struct DenseMapInfo<SlotIndex> {
299 static inline SlotIndex getEmptyKey() {
300 return SlotIndex::getEmptyKey();
302 static inline SlotIndex getTombstoneKey() {
303 return SlotIndex::getTombstoneKey();
305 static inline unsigned getHashValue(const SlotIndex &v) {
306 return SlotIndex::getHashValue(v);
308 static inline bool isEqual(const SlotIndex &LHS, const SlotIndex &RHS) {
311 static inline bool isPod() { return false; }
314 inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
319 typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
321 inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
325 inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
329 struct Idx2MBBCompare {
330 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
331 return LHS.first < RHS.first;
335 /// SlotIndexes pass.
337 /// This pass assigns indexes to each instruction.
338 class SlotIndexes : public MachineFunctionPass {
342 IndexListEntry *indexListHead;
343 unsigned functionSize;
345 typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
348 /// MBB2IdxMap - The indexes of the first and last instructions in the
349 /// specified basic block.
350 typedef DenseMap<const MachineBasicBlock*,
351 std::pair<SlotIndex, SlotIndex> > MBB2IdxMap;
352 MBB2IdxMap mbb2IdxMap;
354 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
356 std::vector<IdxMBBPair> idx2MBBMap;
358 typedef DenseMap<const MachineBasicBlock*, SlotIndex> TerminatorGapsMap;
359 TerminatorGapsMap terminatorGaps;
361 // IndexListEntry allocator.
362 BumpPtrAllocator ileAllocator;
364 IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
365 IndexListEntry *entry =
366 static_cast<IndexListEntry*>(
367 ileAllocator.Allocate(sizeof(IndexListEntry),
368 alignof<IndexListEntry>()));
370 new (entry) IndexListEntry(mi, index);
376 assert(indexListHead == 0 && "Zero entry non-null at initialisation.");
377 indexListHead = createEntry(0, ~0U);
378 indexListHead->setNext(0);
379 indexListHead->setPrev(indexListHead);
384 ileAllocator.Reset();
387 IndexListEntry* getTail() {
388 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
389 return indexListHead->getPrev();
392 const IndexListEntry* getTail() const {
393 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
394 return indexListHead->getPrev();
397 // Returns true if the index list is empty.
398 bool empty() const { return (indexListHead == getTail()); }
400 IndexListEntry* front() {
401 assert(!empty() && "front() called on empty index list.");
402 return indexListHead;
405 const IndexListEntry* front() const {
406 assert(!empty() && "front() called on empty index list.");
407 return indexListHead;
410 IndexListEntry* back() {
411 assert(!empty() && "back() called on empty index list.");
412 return getTail()->getPrev();
415 const IndexListEntry* back() const {
416 assert(!empty() && "back() called on empty index list.");
417 return getTail()->getPrev();
420 /// Insert a new entry before itr.
421 void insert(IndexListEntry *itr, IndexListEntry *val) {
422 assert(itr != 0 && "itr should not be null.");
423 IndexListEntry *prev = itr->getPrev();
427 if (itr != indexListHead) {
436 /// Push a new entry on to the end of the list.
437 void push_back(IndexListEntry *val) {
438 insert(getTail(), val);
444 SlotIndexes() : MachineFunctionPass(&ID), indexListHead(0) {}
446 virtual void getAnalysisUsage(AnalysisUsage &au) const;
447 virtual void releaseMemory();
449 virtual bool runOnMachineFunction(MachineFunction &fn);
451 /// Dump the indexes.
454 /// Renumber the index list, providing space for new instructions.
457 /// Returns the zero index for this analysis.
458 SlotIndex getZeroIndex() {
459 assert(front()->getIndex() == 0 && "First index is not 0?");
460 return SlotIndex(front(), 0);
463 /// Returns the invalid index marker for this analysis.
464 SlotIndex getInvalidIndex() {
465 return getZeroIndex();
468 /// Returns the distance between the highest and lowest indexes allocated
470 unsigned getIndexesLength() const {
471 assert(front()->getIndex() == 0 &&
472 "Initial index isn't zero?");
474 return back()->getIndex();
477 /// Returns the number of instructions in the function.
478 unsigned getFunctionSize() const {
482 /// Returns true if the given machine instr is mapped to an index,
483 /// otherwise returns false.
484 bool hasIndex(const MachineInstr *instr) const {
485 return (mi2iMap.find(instr) != mi2iMap.end());
488 /// Returns the base index for the given instruction.
489 SlotIndex getInstructionIndex(const MachineInstr *instr) const {
490 Mi2IndexMap::const_iterator itr = mi2iMap.find(instr);
491 assert(itr != mi2iMap.end() && "Instruction not found in maps.");
495 /// Returns the instruction for the given index, or null if the given
496 /// index has no instruction associated with it.
497 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
498 return index.entry().getInstr();
501 /// Returns the next non-null index.
502 SlotIndex getNextNonNullIndex(SlotIndex index) {
503 SlotIndex nextNonNull = index.getNextIndex();
505 while (&nextNonNull.entry() != getTail() &&
506 getInstructionFromIndex(nextNonNull) == 0) {
507 nextNonNull = nextNonNull.getNextIndex();
513 /// Returns the first index in the given basic block.
514 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
515 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
516 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
517 return itr->second.first;
520 /// Returns the last index in the given basic block.
521 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
522 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
523 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
524 return itr->second.second;
527 /// Returns the terminator gap for the given index.
528 SlotIndex getTerminatorGap(const MachineBasicBlock *mbb) {
529 TerminatorGapsMap::iterator itr = terminatorGaps.find(mbb);
530 assert(itr != terminatorGaps.end() &&
531 "All MBBs should have terminator gaps in their indexes.");
535 /// Returns the basic block which the given index falls in.
536 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
537 std::vector<IdxMBBPair>::const_iterator I =
538 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
539 // Take the pair containing the index
540 std::vector<IdxMBBPair>::const_iterator J =
541 ((I != idx2MBBMap.end() && I->first > index) ||
542 (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
544 assert(J != idx2MBBMap.end() && J->first <= index &&
545 index <= getMBBEndIdx(J->second) &&
546 "index does not correspond to an MBB");
550 bool findLiveInMBBs(SlotIndex start, SlotIndex end,
551 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
552 std::vector<IdxMBBPair>::const_iterator itr =
553 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
556 while (itr != idx2MBBMap.end()) {
557 if (itr->first >= end)
559 mbbs.push_back(itr->second);
566 /// Return a list of MBBs that can be reach via any branches or
568 bool findReachableMBBs(SlotIndex start, SlotIndex end,
569 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
570 std::vector<IdxMBBPair>::const_iterator itr =
571 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
574 while (itr != idx2MBBMap.end()) {
575 if (itr->first > end)
577 MachineBasicBlock *mbb = itr->second;
578 if (getMBBEndIdx(mbb) > end)
580 for (MachineBasicBlock::succ_iterator si = mbb->succ_begin(),
581 se = mbb->succ_end(); si != se; ++si)
589 /// Returns the MBB covering the given range, or null if the range covers
590 /// more than one basic block.
591 MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
593 assert(start < end && "Backwards ranges not allowed.");
595 std::vector<IdxMBBPair>::const_iterator itr =
596 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
598 if (itr == idx2MBBMap.end()) {
603 // Check that we don't cross the boundary into this block.
604 if (itr->first < end)
609 if (itr->first <= start)
615 /// Returns true if there is a gap in the numbering before the given index.
616 bool hasGapBeforeInstr(SlotIndex index) {
617 index = index.getBaseIndex();
618 SlotIndex prevIndex = index.getPrevIndex();
620 if (prevIndex == getZeroIndex())
623 if (getInstructionFromIndex(prevIndex) == 0)
626 if (prevIndex.distance(index) >= 2 * SlotIndex::NUM)
632 /// Returns true if there is a gap in the numbering after the given index.
633 bool hasGapAfterInstr(SlotIndex index) const {
634 // Not implemented yet.
636 "SlotIndexes::hasGapAfterInstr(SlotIndex) not implemented yet.");
640 /// findGapBeforeInstr - Find an empty instruction slot before the
641 /// specified index. If "Furthest" is true, find one that's furthest
642 /// away from the index (but before any index that's occupied).
643 // FIXME: This whole method should go away in future. It should
644 // always be possible to insert code between existing indices.
645 SlotIndex findGapBeforeInstr(SlotIndex index, bool furthest = false) {
646 if (index == getZeroIndex())
647 return getInvalidIndex();
649 index = index.getBaseIndex();
650 SlotIndex prevIndex = index.getPrevIndex();
652 if (prevIndex == getZeroIndex())
653 return getInvalidIndex();
655 // Try to reuse existing index objects with null-instrs.
656 if (getInstructionFromIndex(prevIndex) == 0) {
658 while (getInstructionFromIndex(prevIndex) == 0 &&
659 prevIndex != getZeroIndex()) {
660 prevIndex = prevIndex.getPrevIndex();
663 prevIndex = prevIndex.getNextIndex();
666 assert(getInstructionFromIndex(prevIndex) == 0 && "Index list is broken.");
671 int dist = prevIndex.distance(index);
673 // Double check that the spacing between this instruction and
675 assert(dist >= SlotIndex::NUM &&
676 "Distance between indexes too small.");
678 // If there's no gap return an invalid index.
679 if (dist < 2*SlotIndex::NUM) {
680 return getInvalidIndex();
683 // Otherwise insert new index entries into the list using the
684 // gap in the numbering.
685 IndexListEntry *newEntry =
686 createEntry(0, prevIndex.entry().getIndex() + SlotIndex::NUM);
688 insert(&index.entry(), newEntry);
690 // And return a pointer to the entry at the start of the gap.
691 return index.getPrevIndex();
694 /// Insert the given machine instruction into the mapping at the given
696 void insertMachineInstrInMaps(MachineInstr *mi, SlotIndex index) {
697 index = index.getBaseIndex();
698 IndexListEntry *miEntry = &index.entry();
699 assert(miEntry->getInstr() == 0 && "Index already in use.");
700 miEntry->setInstr(mi);
702 assert(mi2iMap.find(mi) == mi2iMap.end() &&
703 "MachineInstr already has an index.");
705 mi2iMap.insert(std::make_pair(mi, index));
708 /// Remove the given machine instruction from the mapping.
709 void removeMachineInstrFromMaps(MachineInstr *mi) {
710 // remove index -> MachineInstr and
711 // MachineInstr -> index mappings
712 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
713 if (mi2iItr != mi2iMap.end()) {
714 IndexListEntry *miEntry(&mi2iItr->second.entry());
715 assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
716 // FIXME: Eventually we want to actually delete these indexes.
717 miEntry->setInstr(0);
718 mi2iMap.erase(mi2iItr);
722 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
723 /// maps used by register allocator.
724 void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
725 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
726 if (mi2iItr == mi2iMap.end())
728 SlotIndex replaceBaseIndex = mi2iItr->second;
729 IndexListEntry *miEntry(&replaceBaseIndex.entry());
730 assert(miEntry->getInstr() == mi &&
731 "Mismatched instruction in index tables.");
732 miEntry->setInstr(newMI);
733 mi2iMap.erase(mi2iItr);
734 mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
742 #endif // LLVM_CODEGEN_LIVEINDEX_H