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 IndexListEntry *next, *prev;
43 IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
45 MachineInstr* getInstr() const { return mi; }
46 void setInstr(MachineInstr *mi) {
50 unsigned getIndex() const { return index; }
51 void setIndex(unsigned index) {
55 IndexListEntry* getNext() { return next; }
56 const IndexListEntry* getNext() const { return next; }
57 void setNext(IndexListEntry *next) {
61 IndexListEntry* getPrev() { return prev; }
62 const IndexListEntry* getPrev() const { return prev; }
63 void setPrev(IndexListEntry *prev) {
68 // Specialize PointerLikeTypeTraits for IndexListEntry.
70 class PointerLikeTypeTraits<IndexListEntry*> {
72 static inline void* getAsVoidPointer(IndexListEntry *p) {
75 static inline IndexListEntry* getFromVoidPointer(void *p) {
76 return static_cast<IndexListEntry*>(p);
78 enum { NumLowBitsAvailable = 3 };
81 /// SlotIndex - An opaque wrapper around machine indexes.
83 friend class SlotIndexes;
84 friend struct DenseMapInfo<SlotIndex>;
86 enum Slot { LOAD, USE, DEF, STORE, NUM };
88 PointerIntPair<IndexListEntry*, 2, unsigned> lie;
90 SlotIndex(IndexListEntry *entry, unsigned slot)
93 IndexListEntry& entry() const {
94 assert(isValid() && "Attempt to compare reserved index.");
95 return *lie.getPointer();
98 int getIndex() const {
99 return entry().getIndex() | getSlot();
102 /// Returns the slot for this SlotIndex.
103 Slot getSlot() const {
104 return static_cast<Slot>(lie.getInt());
107 static inline unsigned getHashValue(const SlotIndex &v) {
108 void *ptrVal = v.lie.getOpaqueValue();
109 return (unsigned((intptr_t)ptrVal)) ^ (unsigned((intptr_t)ptrVal) >> 9);
113 static inline SlotIndex getEmptyKey() {
114 return SlotIndex(0, 1);
117 static inline SlotIndex getTombstoneKey() {
118 return SlotIndex(0, 2);
121 /// Construct an invalid index.
122 SlotIndex() : lie(0, 0) {}
124 // Construct a new slot index from the given one, and set the slot.
125 SlotIndex(const SlotIndex &li, Slot s)
126 : lie(&li.entry(), unsigned(s)) {
127 assert(lie.getPointer() != 0 &&
128 "Attempt to construct index with 0 pointer.");
131 /// Returns true if this is a valid index. Invalid indicies do
132 /// not point into an index table, and cannot be compared.
133 bool isValid() const {
134 return lie.getPointer();
137 /// Print this index to the given raw_ostream.
138 void print(raw_ostream &os) const;
140 /// Dump this index to stderr.
143 /// Compare two SlotIndex objects for equality.
144 bool operator==(SlotIndex other) const {
145 return lie == other.lie;
147 /// Compare two SlotIndex objects for inequality.
148 bool operator!=(SlotIndex other) const {
149 return lie != other.lie;
152 /// Compare two SlotIndex objects. Return true if the first index
153 /// is strictly lower than the second.
154 bool operator<(SlotIndex other) const {
155 return getIndex() < other.getIndex();
157 /// Compare two SlotIndex objects. Return true if the first index
158 /// is lower than, or equal to, the second.
159 bool operator<=(SlotIndex other) const {
160 return getIndex() <= other.getIndex();
163 /// Compare two SlotIndex objects. Return true if the first index
164 /// is greater than the second.
165 bool operator>(SlotIndex other) const {
166 return getIndex() > other.getIndex();
169 /// Compare two SlotIndex objects. Return true if the first index
170 /// is greater than, or equal to, the second.
171 bool operator>=(SlotIndex other) const {
172 return getIndex() >= other.getIndex();
175 /// Return the distance from this index to the given one.
176 int distance(SlotIndex other) const {
177 return other.getIndex() - getIndex();
180 /// isLoad - Return true if this is a LOAD slot.
181 bool isLoad() const {
182 return getSlot() == LOAD;
185 /// isDef - Return true if this is a DEF slot.
187 return getSlot() == DEF;
190 /// isUse - Return true if this is a USE slot.
192 return getSlot() == USE;
195 /// isStore - Return true if this is a STORE slot.
196 bool isStore() const {
197 return getSlot() == STORE;
200 /// Returns the base index for associated with this index. The base index
201 /// is the one associated with the LOAD slot for the instruction pointed to
203 SlotIndex getBaseIndex() const {
204 return getLoadIndex();
207 /// Returns the boundary index for associated with this index. The boundary
208 /// index is the one associated with the LOAD slot for the instruction
209 /// pointed to by this index.
210 SlotIndex getBoundaryIndex() const {
211 return getStoreIndex();
214 /// Returns the index of the LOAD slot for the instruction pointed to by
216 SlotIndex getLoadIndex() const {
217 return SlotIndex(&entry(), SlotIndex::LOAD);
220 /// Returns the index of the USE slot for the instruction pointed to by
222 SlotIndex getUseIndex() const {
223 return SlotIndex(&entry(), SlotIndex::USE);
226 /// Returns the index of the DEF slot for the instruction pointed to by
228 SlotIndex getDefIndex() const {
229 return SlotIndex(&entry(), SlotIndex::DEF);
232 /// Returns the index of the STORE slot for the instruction pointed to by
234 SlotIndex getStoreIndex() const {
235 return SlotIndex(&entry(), SlotIndex::STORE);
238 /// Returns the next slot in the index list. This could be either the
239 /// next slot for the instruction pointed to by this index or, if this
240 /// index is a STORE, the first slot for the next instruction.
241 /// WARNING: This method is considerably more expensive than the methods
242 /// that return specific slots (getUseIndex(), etc). If you can - please
243 /// use one of those methods.
244 SlotIndex getNextSlot() const {
246 if (s == SlotIndex::STORE) {
247 return SlotIndex(entry().getNext(), SlotIndex::LOAD);
249 return SlotIndex(&entry(), s + 1);
252 /// Returns the next index. This is the index corresponding to the this
253 /// index's slot, but for the next instruction.
254 SlotIndex getNextIndex() const {
255 return SlotIndex(entry().getNext(), getSlot());
258 /// Returns the previous slot in the index list. This could be either the
259 /// previous slot for the instruction pointed to by this index or, if this
260 /// index is a LOAD, the last slot for the previous instruction.
261 /// WARNING: This method is considerably more expensive than the methods
262 /// that return specific slots (getUseIndex(), etc). If you can - please
263 /// use one of those methods.
264 SlotIndex getPrevSlot() const {
266 if (s == SlotIndex::LOAD) {
267 return SlotIndex(entry().getPrev(), SlotIndex::STORE);
269 return SlotIndex(&entry(), s - 1);
272 /// Returns the previous index. This is the index corresponding to this
273 /// index's slot, but for the previous instruction.
274 SlotIndex getPrevIndex() const {
275 return SlotIndex(entry().getPrev(), getSlot());
280 /// DenseMapInfo specialization for SlotIndex.
282 struct DenseMapInfo<SlotIndex> {
283 static inline SlotIndex getEmptyKey() {
284 return SlotIndex::getEmptyKey();
286 static inline SlotIndex getTombstoneKey() {
287 return SlotIndex::getTombstoneKey();
289 static inline unsigned getHashValue(const SlotIndex &v) {
290 return SlotIndex::getHashValue(v);
292 static inline bool isEqual(const SlotIndex &LHS, const SlotIndex &RHS) {
297 template <> struct isPodLike<SlotIndex> { static const bool value = true; };
300 inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
305 typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
307 inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
311 inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
315 struct Idx2MBBCompare {
316 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
317 return LHS.first < RHS.first;
321 /// SlotIndexes pass.
323 /// This pass assigns indexes to each instruction.
324 class SlotIndexes : public MachineFunctionPass {
328 IndexListEntry *indexListHead;
329 unsigned functionSize;
331 typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
334 /// MBB2IdxMap - The indexes of the first and last instructions in the
335 /// specified basic block.
336 typedef DenseMap<const MachineBasicBlock*,
337 std::pair<SlotIndex, SlotIndex> > MBB2IdxMap;
338 MBB2IdxMap mbb2IdxMap;
340 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
342 std::vector<IdxMBBPair> idx2MBBMap;
344 // IndexListEntry allocator.
345 BumpPtrAllocator ileAllocator;
347 IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
348 IndexListEntry *entry =
349 static_cast<IndexListEntry*>(
350 ileAllocator.Allocate(sizeof(IndexListEntry),
351 alignOf<IndexListEntry>()));
353 new (entry) IndexListEntry(mi, index);
359 assert(indexListHead == 0 && "Zero entry non-null at initialisation.");
360 indexListHead = createEntry(0, ~0U);
361 indexListHead->setNext(0);
362 indexListHead->setPrev(indexListHead);
367 ileAllocator.Reset();
370 IndexListEntry* getTail() {
371 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
372 return indexListHead->getPrev();
375 const IndexListEntry* getTail() const {
376 assert(indexListHead != 0 && "Call to getTail on uninitialized list.");
377 return indexListHead->getPrev();
380 // Returns true if the index list is empty.
381 bool empty() const { return (indexListHead == getTail()); }
383 IndexListEntry* front() {
384 assert(!empty() && "front() called on empty index list.");
385 return indexListHead;
388 const IndexListEntry* front() const {
389 assert(!empty() && "front() called on empty index list.");
390 return indexListHead;
393 IndexListEntry* back() {
394 assert(!empty() && "back() called on empty index list.");
395 return getTail()->getPrev();
398 const IndexListEntry* back() const {
399 assert(!empty() && "back() called on empty index list.");
400 return getTail()->getPrev();
403 /// Insert a new entry before itr.
404 void insert(IndexListEntry *itr, IndexListEntry *val) {
405 assert(itr != 0 && "itr should not be null.");
406 IndexListEntry *prev = itr->getPrev();
410 if (itr != indexListHead) {
419 /// Push a new entry on to the end of the list.
420 void push_back(IndexListEntry *val) {
421 insert(getTail(), val);
427 SlotIndexes() : MachineFunctionPass(ID), indexListHead(0) {
428 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
431 virtual void getAnalysisUsage(AnalysisUsage &au) const;
432 virtual void releaseMemory();
434 virtual bool runOnMachineFunction(MachineFunction &fn);
436 /// Dump the indexes.
439 /// Renumber the index list, providing space for new instructions.
440 void renumberIndexes();
442 /// Returns the zero index for this analysis.
443 SlotIndex getZeroIndex() {
444 assert(front()->getIndex() == 0 && "First index is not 0?");
445 return SlotIndex(front(), 0);
448 /// Returns the base index of the last slot in this analysis.
449 SlotIndex getLastIndex() {
450 return SlotIndex(back(), 0);
453 /// Returns the invalid index marker for this analysis.
454 SlotIndex getInvalidIndex() {
455 return getZeroIndex();
458 /// Returns the distance between the highest and lowest indexes allocated
460 unsigned getIndexesLength() const {
461 assert(front()->getIndex() == 0 &&
462 "Initial index isn't zero?");
464 return back()->getIndex();
467 /// Returns the number of instructions in the function.
468 unsigned getFunctionSize() const {
472 /// Returns true if the given machine instr is mapped to an index,
473 /// otherwise returns false.
474 bool hasIndex(const MachineInstr *instr) const {
475 return (mi2iMap.find(instr) != mi2iMap.end());
478 /// Returns the base index for the given instruction.
479 SlotIndex getInstructionIndex(const MachineInstr *instr) const {
480 Mi2IndexMap::const_iterator itr = mi2iMap.find(instr);
481 assert(itr != mi2iMap.end() && "Instruction not found in maps.");
485 /// Returns the instruction for the given index, or null if the given
486 /// index has no instruction associated with it.
487 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
488 return index.isValid() ? index.entry().getInstr() : 0;
491 /// Returns the next non-null index.
492 SlotIndex getNextNonNullIndex(SlotIndex index) {
493 SlotIndex nextNonNull = index.getNextIndex();
495 while (&nextNonNull.entry() != getTail() &&
496 getInstructionFromIndex(nextNonNull) == 0) {
497 nextNonNull = nextNonNull.getNextIndex();
503 /// Return the (start,end) range of the given basic block.
504 const std::pair<SlotIndex, SlotIndex> &
505 getMBBRange(const MachineBasicBlock *mbb) const {
506 MBB2IdxMap::const_iterator itr = mbb2IdxMap.find(mbb);
507 assert(itr != mbb2IdxMap.end() && "MBB not found in maps.");
511 /// Returns the first index in the given basic block.
512 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
513 return getMBBRange(mbb).first;
516 /// Returns the last index in the given basic block.
517 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
518 return getMBBRange(mbb).second;
521 /// Returns the basic block which the given index falls in.
522 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
523 std::vector<IdxMBBPair>::const_iterator I =
524 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
525 // Take the pair containing the index
526 std::vector<IdxMBBPair>::const_iterator J =
527 ((I != idx2MBBMap.end() && I->first > index) ||
528 (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
530 assert(J != idx2MBBMap.end() && J->first <= index &&
531 index < getMBBEndIdx(J->second) &&
532 "index does not correspond to an MBB");
536 bool findLiveInMBBs(SlotIndex start, SlotIndex end,
537 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
538 std::vector<IdxMBBPair>::const_iterator itr =
539 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
542 while (itr != idx2MBBMap.end()) {
543 if (itr->first >= end)
545 mbbs.push_back(itr->second);
552 /// Returns the MBB covering the given range, or null if the range covers
553 /// more than one basic block.
554 MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
556 assert(start < end && "Backwards ranges not allowed.");
558 std::vector<IdxMBBPair>::const_iterator itr =
559 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
561 if (itr == idx2MBBMap.end()) {
566 // Check that we don't cross the boundary into this block.
567 if (itr->first < end)
572 if (itr->first <= start)
578 /// Insert the given machine instruction into the mapping. Returns the
580 SlotIndex insertMachineInstrInMaps(MachineInstr *mi,
581 bool *deferredRenumber = 0) {
582 assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
583 // Numbering DBG_VALUE instructions could cause code generation to be
584 // affected by debug information.
585 assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
587 MachineBasicBlock *mbb = mi->getParent();
589 assert(mbb != 0 && "Instr must be added to function.");
591 MBB2IdxMap::iterator mbbRangeItr = mbb2IdxMap.find(mbb);
593 assert(mbbRangeItr != mbb2IdxMap.end() &&
594 "Instruction's parent MBB has not been added to SlotIndexes.");
596 MachineBasicBlock::iterator miItr(mi);
597 bool needRenumber = false;
598 IndexListEntry *newEntry;
599 // Get previous index, considering that not all instructions are indexed.
600 IndexListEntry *prevEntry;
602 // If mi is at the mbb beginning, get the prev index from the mbb.
603 if (miItr == mbb->begin()) {
604 prevEntry = &mbbRangeItr->second.first.entry();
607 // Otherwise rewind until we find a mapped instruction.
608 Mi2IndexMap::const_iterator itr = mi2iMap.find(--miItr);
609 if (itr != mi2iMap.end()) {
610 prevEntry = &itr->second.entry();
615 // Get next entry from previous entry.
616 IndexListEntry *nextEntry = prevEntry->getNext();
618 // Get a number for the new instr, or 0 if there's no room currently.
619 // In the latter case we'll force a renumber later.
620 unsigned dist = nextEntry->getIndex() - prevEntry->getIndex();
621 unsigned newNumber = dist > SlotIndex::NUM ?
622 prevEntry->getIndex() + ((dist >> 1) & ~3U) : 0;
624 if (newNumber == 0) {
628 // Insert a new list entry for mi.
629 newEntry = createEntry(mi, newNumber);
630 insert(nextEntry, newEntry);
632 SlotIndex newIndex(newEntry, SlotIndex::LOAD);
633 mi2iMap.insert(std::make_pair(mi, newIndex));
635 if (miItr == mbb->end()) {
636 // If this is the last instr in the MBB then we need to fix up the bb
638 mbbRangeItr->second.second = SlotIndex(newEntry, SlotIndex::STORE);
641 // Renumber if we need to.
643 if (deferredRenumber == 0)
646 *deferredRenumber = true;
652 /// Add all instructions in the vector to the index list. This method will
653 /// defer renumbering until all instrs have been added, and should be
654 /// preferred when adding multiple instrs.
655 void insertMachineInstrsInMaps(SmallVectorImpl<MachineInstr*> &mis) {
656 bool renumber = false;
658 for (SmallVectorImpl<MachineInstr*>::iterator
659 miItr = mis.begin(), miEnd = mis.end();
660 miItr != miEnd; ++miItr) {
661 insertMachineInstrInMaps(*miItr, &renumber);
669 /// Remove the given machine instruction from the mapping.
670 void removeMachineInstrFromMaps(MachineInstr *mi) {
671 // remove index -> MachineInstr and
672 // MachineInstr -> index mappings
673 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
674 if (mi2iItr != mi2iMap.end()) {
675 IndexListEntry *miEntry(&mi2iItr->second.entry());
676 assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
677 // FIXME: Eventually we want to actually delete these indexes.
678 miEntry->setInstr(0);
679 mi2iMap.erase(mi2iItr);
683 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
684 /// maps used by register allocator.
685 void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
686 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
687 if (mi2iItr == mi2iMap.end())
689 SlotIndex replaceBaseIndex = mi2iItr->second;
690 IndexListEntry *miEntry(&replaceBaseIndex.entry());
691 assert(miEntry->getInstr() == mi &&
692 "Mismatched instruction in index tables.");
693 miEntry->setInstr(newMI);
694 mi2iMap.erase(mi2iItr);
695 mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
698 /// Add the given MachineBasicBlock into the maps.
699 void insertMBBInMaps(MachineBasicBlock *mbb) {
700 MachineFunction::iterator nextMBB =
701 llvm::next(MachineFunction::iterator(mbb));
702 IndexListEntry *startEntry = createEntry(0, 0);
703 IndexListEntry *stopEntry = createEntry(0, 0);
704 IndexListEntry *nextEntry = 0;
706 if (nextMBB == mbb->getParent()->end()) {
707 nextEntry = getTail();
709 nextEntry = &getMBBStartIdx(nextMBB).entry();
712 insert(nextEntry, startEntry);
713 insert(nextEntry, stopEntry);
715 SlotIndex startIdx(startEntry, SlotIndex::LOAD);
716 SlotIndex endIdx(nextEntry, SlotIndex::LOAD);
719 std::make_pair(mbb, std::make_pair(startIdx, endIdx)));
721 idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
723 if (MachineFunction::iterator(mbb) != mbb->getParent()->begin()) {
724 // Have to update the end index of the previous block.
725 MachineBasicBlock *priorMBB =
726 llvm::prior(MachineFunction::iterator(mbb));
727 mbb2IdxMap[priorMBB].second = startIdx;
731 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
738 // Specialize IntervalMapInfo for half-open slot index intervals.
739 template <typename> struct IntervalMapInfo;
740 template <> struct IntervalMapInfo<SlotIndex> {
741 static inline bool startLess(const SlotIndex &x, const SlotIndex &a) {
744 static inline bool stopLess(const SlotIndex &b, const SlotIndex &x) {
747 static inline bool adjacent(const SlotIndex &a, const SlotIndex &b) {
754 #endif // LLVM_CODEGEN_LIVEINDEX_H