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 purpose 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/MachineInstrBundle.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/ADT/PointerIntPair.h"
26 #include "llvm/ADT/ilist.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/Support/Allocator.h"
33 /// This class represents an entry in the slot index list held in the
34 /// SlotIndexes pass. It should not be used directly. See the
35 /// SlotIndex & SlotIndexes classes for the public interface to this
37 class IndexListEntry : public ilist_node<IndexListEntry> {
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) {
58 struct ilist_traits<IndexListEntry> : public ilist_default_traits<IndexListEntry> {
60 mutable ilist_half_node<IndexListEntry> Sentinel;
62 IndexListEntry *createSentinel() const {
63 return static_cast<IndexListEntry*>(&Sentinel);
65 void destroySentinel(IndexListEntry *) const {}
67 IndexListEntry *provideInitialHead() const { return createSentinel(); }
68 IndexListEntry *ensureHead(IndexListEntry*) const { return createSentinel(); }
69 static void noteHead(IndexListEntry*, IndexListEntry*) {}
70 void deleteNode(IndexListEntry *N) {}
73 void createNode(const IndexListEntry &);
76 // Specialize PointerLikeTypeTraits for IndexListEntry.
78 class PointerLikeTypeTraits<IndexListEntry*> {
80 static inline void* getAsVoidPointer(IndexListEntry *p) {
83 static inline IndexListEntry* getFromVoidPointer(void *p) {
84 return static_cast<IndexListEntry*>(p);
86 enum { NumLowBitsAvailable = 3 };
89 /// SlotIndex - An opaque wrapper around machine indexes.
91 friend class SlotIndexes;
92 friend struct DenseMapInfo<SlotIndex>;
95 /// Basic block boundary. Used for live ranges entering and leaving a
96 /// block without being live in the layout neighbor. Also used as the
97 /// def slot of PHI-defs.
100 /// Early-clobber register use/def slot. A live range defined at
101 /// Slot_EarlyCLobber interferes with normal live ranges killed at
102 /// Slot_Register. Also used as the kill slot for live ranges tied to an
103 /// early-clobber def.
106 /// Normal register use/def slot. Normal instructions kill and define
107 /// register live ranges at this slot.
110 /// Dead def kill point. Kill slot for a live range that is defined by
111 /// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't
118 PointerIntPair<IndexListEntry*, 2, unsigned> lie;
120 SlotIndex(IndexListEntry *entry, unsigned slot)
121 : lie(entry, slot) {}
123 IndexListEntry* listEntry() const {
124 assert(isValid() && "Attempt to compare reserved index.");
125 return lie.getPointer();
128 int getIndex() const {
129 return listEntry()->getIndex() | getSlot();
132 /// Returns the slot for this SlotIndex.
133 Slot getSlot() const {
134 return static_cast<Slot>(lie.getInt());
137 static inline unsigned getHashValue(const SlotIndex &v) {
138 void *ptrVal = v.lie.getOpaqueValue();
139 return (unsigned((intptr_t)ptrVal)) ^ (unsigned((intptr_t)ptrVal) >> 9);
144 /// The default distance between instructions as returned by distance().
145 /// This may vary as instructions are inserted and removed.
146 InstrDist = 4 * Slot_Count
149 static inline SlotIndex getEmptyKey() {
150 return SlotIndex(0, 1);
153 static inline SlotIndex getTombstoneKey() {
154 return SlotIndex(0, 2);
157 /// Construct an invalid index.
158 SlotIndex() : lie(0, 0) {}
160 // Construct a new slot index from the given one, and set the slot.
161 SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) {
162 assert(lie.getPointer() != 0 &&
163 "Attempt to construct index with 0 pointer.");
166 /// Returns true if this is a valid index. Invalid indicies do
167 /// not point into an index table, and cannot be compared.
168 bool isValid() const {
169 return lie.getPointer();
172 /// Return true for a valid index.
173 operator bool() const { return isValid(); }
175 /// Print this index to the given raw_ostream.
176 void print(raw_ostream &os) const;
178 /// Dump this index to stderr.
181 /// Compare two SlotIndex objects for equality.
182 bool operator==(SlotIndex other) const {
183 return lie == other.lie;
185 /// Compare two SlotIndex objects for inequality.
186 bool operator!=(SlotIndex other) const {
187 return lie != other.lie;
190 /// Compare two SlotIndex objects. Return true if the first index
191 /// is strictly lower than the second.
192 bool operator<(SlotIndex other) const {
193 return getIndex() < other.getIndex();
195 /// Compare two SlotIndex objects. Return true if the first index
196 /// is lower than, or equal to, the second.
197 bool operator<=(SlotIndex other) const {
198 return getIndex() <= other.getIndex();
201 /// Compare two SlotIndex objects. Return true if the first index
202 /// is greater than the second.
203 bool operator>(SlotIndex other) const {
204 return getIndex() > other.getIndex();
207 /// Compare two SlotIndex objects. Return true if the first index
208 /// is greater than, or equal to, the second.
209 bool operator>=(SlotIndex other) const {
210 return getIndex() >= other.getIndex();
213 /// isSameInstr - Return true if A and B refer to the same instruction.
214 static bool isSameInstr(SlotIndex A, SlotIndex B) {
215 return A.lie.getPointer() == B.lie.getPointer();
218 /// isEarlierInstr - Return true if A refers to an instruction earlier than
219 /// B. This is equivalent to A < B && !isSameInstr(A, B).
220 static bool isEarlierInstr(SlotIndex A, SlotIndex B) {
221 return A.listEntry()->getIndex() < B.listEntry()->getIndex();
224 /// Return the distance from this index to the given one.
225 int distance(SlotIndex other) const {
226 return other.getIndex() - getIndex();
229 /// isBlock - Returns true if this is a block boundary slot.
230 bool isBlock() const { return getSlot() == Slot_Block; }
232 /// isEarlyClobber - Returns true if this is an early-clobber slot.
233 bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
235 /// isRegister - Returns true if this is a normal register use/def slot.
236 /// Note that early-clobber slots may also be used for uses and defs.
237 bool isRegister() const { return getSlot() == Slot_Register; }
239 /// isDead - Returns true if this is a dead def kill slot.
240 bool isDead() const { return getSlot() == Slot_Dead; }
242 /// Returns the base index for associated with this index. The base index
243 /// is the one associated with the Slot_Block slot for the instruction
244 /// pointed to by this index.
245 SlotIndex getBaseIndex() const {
246 return SlotIndex(listEntry(), Slot_Block);
249 /// Returns the boundary index for associated with this index. The boundary
250 /// index is the one associated with the Slot_Block slot for the instruction
251 /// pointed to by this index.
252 SlotIndex getBoundaryIndex() const {
253 return SlotIndex(listEntry(), Slot_Dead);
256 /// Returns the register use/def slot in the current instruction for a
257 /// normal or early-clobber def.
258 SlotIndex getRegSlot(bool EC = false) const {
259 return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
262 /// Returns the dead def kill slot for the current instruction.
263 SlotIndex getDeadSlot() const {
264 return SlotIndex(listEntry(), Slot_Dead);
267 /// Returns the next slot in the index list. This could be either the
268 /// next slot for the instruction pointed to by this index or, if this
269 /// index is a STORE, the first slot for the next instruction.
270 /// WARNING: This method is considerably more expensive than the methods
271 /// that return specific slots (getUseIndex(), etc). If you can - please
272 /// use one of those methods.
273 SlotIndex getNextSlot() const {
275 if (s == Slot_Dead) {
276 return SlotIndex(listEntry()->getNextNode(), Slot_Block);
278 return SlotIndex(listEntry(), s + 1);
281 /// Returns the next index. This is the index corresponding to the this
282 /// index's slot, but for the next instruction.
283 SlotIndex getNextIndex() const {
284 return SlotIndex(listEntry()->getNextNode(), getSlot());
287 /// Returns the previous slot in the index list. This could be either the
288 /// previous slot for the instruction pointed to by this index or, if this
289 /// index is a Slot_Block, the last slot for the previous 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 getPrevSlot() const {
295 if (s == Slot_Block) {
296 return SlotIndex(listEntry()->getPrevNode(), Slot_Dead);
298 return SlotIndex(listEntry(), s - 1);
301 /// Returns the previous index. This is the index corresponding to this
302 /// index's slot, but for the previous instruction.
303 SlotIndex getPrevIndex() const {
304 return SlotIndex(listEntry()->getPrevNode(), getSlot());
309 /// DenseMapInfo specialization for SlotIndex.
311 struct DenseMapInfo<SlotIndex> {
312 static inline SlotIndex getEmptyKey() {
313 return SlotIndex::getEmptyKey();
315 static inline SlotIndex getTombstoneKey() {
316 return SlotIndex::getTombstoneKey();
318 static inline unsigned getHashValue(const SlotIndex &v) {
319 return SlotIndex::getHashValue(v);
321 static inline bool isEqual(const SlotIndex &LHS, const SlotIndex &RHS) {
326 template <> struct isPodLike<SlotIndex> { static const bool value = true; };
329 inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
334 typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
336 inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
340 inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
344 struct Idx2MBBCompare {
345 bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
346 return LHS.first < RHS.first;
350 /// SlotIndexes pass.
352 /// This pass assigns indexes to each instruction.
353 class SlotIndexes : public MachineFunctionPass {
356 typedef ilist<IndexListEntry> IndexList;
360 unsigned functionSize;
362 typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
365 /// MBBRanges - Map MBB number to (start, stop) indexes.
366 SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
368 /// Idx2MBBMap - Sorted list of pairs of index of first instruction
370 SmallVector<IdxMBBPair, 8> idx2MBBMap;
372 // IndexListEntry allocator.
373 BumpPtrAllocator ileAllocator;
375 IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
376 IndexListEntry *entry =
377 static_cast<IndexListEntry*>(
378 ileAllocator.Allocate(sizeof(IndexListEntry),
379 alignOf<IndexListEntry>()));
381 new (entry) IndexListEntry(mi, index);
386 /// Renumber locally after inserting curItr.
387 void renumberIndexes(IndexList::iterator curItr);
392 SlotIndexes() : MachineFunctionPass(ID) {
393 initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
396 virtual void getAnalysisUsage(AnalysisUsage &au) const;
397 virtual void releaseMemory();
399 virtual bool runOnMachineFunction(MachineFunction &fn);
401 /// Dump the indexes.
404 /// Renumber the index list, providing space for new instructions.
405 void renumberIndexes();
407 /// Returns the zero index for this analysis.
408 SlotIndex getZeroIndex() {
409 assert(indexList.front().getIndex() == 0 && "First index is not 0?");
410 return SlotIndex(&indexList.front(), 0);
413 /// Returns the base index of the last slot in this analysis.
414 SlotIndex getLastIndex() {
415 return SlotIndex(&indexList.back(), 0);
418 /// Returns the distance between the highest and lowest indexes allocated
420 unsigned getIndexesLength() const {
421 assert(indexList.front().getIndex() == 0 &&
422 "Initial index isn't zero?");
423 return indexList.back().getIndex();
426 /// Returns the number of instructions in the function.
427 unsigned getFunctionSize() const {
431 /// Returns true if the given machine instr is mapped to an index,
432 /// otherwise returns false.
433 bool hasIndex(const MachineInstr *instr) const {
434 return mi2iMap.count(instr);
437 /// Returns the base index for the given instruction.
438 SlotIndex getInstructionIndex(const MachineInstr *MI) const {
439 // Instructions inside a bundle have the same number as the bundle itself.
440 Mi2IndexMap::const_iterator itr = mi2iMap.find(getBundleStart(MI));
441 assert(itr != mi2iMap.end() && "Instruction not found in maps.");
445 /// Returns the instruction for the given index, or null if the given
446 /// index has no instruction associated with it.
447 MachineInstr* getInstructionFromIndex(SlotIndex index) const {
448 return index.isValid() ? index.listEntry()->getInstr() : 0;
451 /// Returns the next non-null index.
452 SlotIndex getNextNonNullIndex(SlotIndex index) {
453 IndexList::iterator itr(index.listEntry());
455 while (itr != indexList.end() && itr->getInstr() == 0) { ++itr; }
456 return SlotIndex(itr, index.getSlot());
459 /// getIndexBefore - Returns the index of the last indexed instruction
460 /// before MI, or the the start index of its basic block.
461 /// MI is not required to have an index.
462 SlotIndex getIndexBefore(const MachineInstr *MI) const {
463 const MachineBasicBlock *MBB = MI->getParent();
464 assert(MBB && "MI must be inserted inna basic block");
465 MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
468 return getMBBStartIdx(MBB);
470 Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
471 if (MapItr != mi2iMap.end())
472 return MapItr->second;
476 /// getIndexAfter - Returns the index of the first indexed instruction
477 /// after MI, or the end index of its basic block.
478 /// MI is not required to have an index.
479 SlotIndex getIndexAfter(const MachineInstr *MI) const {
480 const MachineBasicBlock *MBB = MI->getParent();
481 assert(MBB && "MI must be inserted inna basic block");
482 MachineBasicBlock::const_iterator I = MI, E = MBB->end();
486 return getMBBEndIdx(MBB);
487 Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
488 if (MapItr != mi2iMap.end())
489 return MapItr->second;
493 /// Return the (start,end) range of the given basic block number.
494 const std::pair<SlotIndex, SlotIndex> &
495 getMBBRange(unsigned Num) const {
496 return MBBRanges[Num];
499 /// Return the (start,end) range of the given basic block.
500 const std::pair<SlotIndex, SlotIndex> &
501 getMBBRange(const MachineBasicBlock *MBB) const {
502 return getMBBRange(MBB->getNumber());
505 /// Returns the first index in the given basic block number.
506 SlotIndex getMBBStartIdx(unsigned Num) const {
507 return getMBBRange(Num).first;
510 /// Returns the first index in the given basic block.
511 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
512 return getMBBRange(mbb).first;
515 /// Returns the last index in the given basic block number.
516 SlotIndex getMBBEndIdx(unsigned Num) const {
517 return getMBBRange(Num).second;
520 /// Returns the last index in the given basic block.
521 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
522 return getMBBRange(mbb).second;
525 /// Returns the basic block which the given index falls in.
526 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
527 if (MachineInstr *MI = getInstructionFromIndex(index))
528 return MI->getParent();
529 SmallVectorImpl<IdxMBBPair>::const_iterator I =
530 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
531 // Take the pair containing the index
532 SmallVectorImpl<IdxMBBPair>::const_iterator J =
533 ((I != idx2MBBMap.end() && I->first > index) ||
534 (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
536 assert(J != idx2MBBMap.end() && J->first <= index &&
537 index < getMBBEndIdx(J->second) &&
538 "index does not correspond to an MBB");
542 bool findLiveInMBBs(SlotIndex start, SlotIndex end,
543 SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
544 SmallVectorImpl<IdxMBBPair>::const_iterator itr =
545 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
548 while (itr != idx2MBBMap.end()) {
549 if (itr->first >= end)
551 mbbs.push_back(itr->second);
558 /// Returns the MBB covering the given range, or null if the range covers
559 /// more than one basic block.
560 MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
562 assert(start < end && "Backwards ranges not allowed.");
564 SmallVectorImpl<IdxMBBPair>::const_iterator itr =
565 std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
567 if (itr == idx2MBBMap.end()) {
572 // Check that we don't cross the boundary into this block.
573 if (itr->first < end)
578 if (itr->first <= start)
584 /// Insert the given machine instruction into the mapping. Returns the
586 /// If Late is set and there are null indexes between mi's neighboring
587 /// instructions, create the new index after the null indexes instead of
589 SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late = false) {
590 assert(!mi->isInsideBundle() &&
591 "Instructions inside bundles should use bundle start's slot.");
592 assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
593 // Numbering DBG_VALUE instructions could cause code generation to be
594 // affected by debug information.
595 assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
597 assert(mi->getParent() != 0 && "Instr must be added to function.");
599 // Get the entries where mi should be inserted.
600 IndexList::iterator prevItr, nextItr;
602 // Insert mi's index immediately before the following instruction.
603 nextItr = getIndexAfter(mi).listEntry();
604 prevItr = prior(nextItr);
606 // Insert mi's index immediately after the preceeding instruction.
607 prevItr = getIndexBefore(mi).listEntry();
608 nextItr = llvm::next(prevItr);
611 // Get a number for the new instr, or 0 if there's no room currently.
612 // In the latter case we'll force a renumber later.
613 unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
614 unsigned newNumber = prevItr->getIndex() + dist;
616 // Insert a new list entry for mi.
617 IndexList::iterator newItr =
618 indexList.insert(nextItr, createEntry(mi, newNumber));
620 // Renumber locally if we need to.
622 renumberIndexes(newItr);
624 SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
625 mi2iMap.insert(std::make_pair(mi, newIndex));
629 /// Remove the given machine instruction from the mapping.
630 void removeMachineInstrFromMaps(MachineInstr *mi) {
631 // remove index -> MachineInstr and
632 // MachineInstr -> index mappings
633 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
634 if (mi2iItr != mi2iMap.end()) {
635 IndexListEntry *miEntry(mi2iItr->second.listEntry());
636 assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
637 // FIXME: Eventually we want to actually delete these indexes.
638 miEntry->setInstr(0);
639 mi2iMap.erase(mi2iItr);
643 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
644 /// maps used by register allocator.
645 void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
646 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
647 if (mi2iItr == mi2iMap.end())
649 SlotIndex replaceBaseIndex = mi2iItr->second;
650 IndexListEntry *miEntry(replaceBaseIndex.listEntry());
651 assert(miEntry->getInstr() == mi &&
652 "Mismatched instruction in index tables.");
653 miEntry->setInstr(newMI);
654 mi2iMap.erase(mi2iItr);
655 mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
658 /// Add the given MachineBasicBlock into the maps.
659 void insertMBBInMaps(MachineBasicBlock *mbb) {
660 MachineFunction::iterator nextMBB =
661 llvm::next(MachineFunction::iterator(mbb));
662 IndexListEntry *startEntry = createEntry(0, 0);
663 IndexListEntry *stopEntry = createEntry(0, 0);
664 IndexListEntry *nextEntry = 0;
666 if (nextMBB == mbb->getParent()->end()) {
667 nextEntry = indexList.end();
669 nextEntry = getMBBStartIdx(nextMBB).listEntry();
672 indexList.insert(nextEntry, startEntry);
673 indexList.insert(nextEntry, stopEntry);
675 SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
676 SlotIndex endIdx(nextEntry, SlotIndex::Slot_Block);
678 assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
679 "Blocks must be added in order");
680 MBBRanges.push_back(std::make_pair(startIdx, endIdx));
682 idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
685 std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
691 // Specialize IntervalMapInfo for half-open slot index intervals.
692 template <typename> struct IntervalMapInfo;
693 template <> struct IntervalMapInfo<SlotIndex> {
694 static inline bool startLess(const SlotIndex &x, const SlotIndex &a) {
697 static inline bool stopLess(const SlotIndex &b, const SlotIndex &x) {
700 static inline bool adjacent(const SlotIndex &a, const SlotIndex &b) {
707 #endif // LLVM_CODEGEN_SLOTINDEXES_H