1 //===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements SlotIndex and related classes. The purpose of SlotIndex 10 // is to describe a position at which a register can become live, or cease to 11 // be live. 12 // 13 // SlotIndex is mostly a proxy for entries of the SlotIndexList, a class which 14 // is held is LiveIntervals and provides the real numbering. This allows 15 // LiveIntervals to perform largely transparent renumbering. 16 //===----------------------------------------------------------------------===// 17 18 #ifndef LLVM_CODEGEN_SLOTINDEXES_H 19 #define LLVM_CODEGEN_SLOTINDEXES_H 20 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/IntervalMap.h" 23 #include "llvm/ADT/PointerIntPair.h" 24 #include "llvm/ADT/SmallVector.h" 25 #include "llvm/ADT/simple_ilist.h" 26 #include "llvm/CodeGen/MachineBasicBlock.h" 27 #include "llvm/CodeGen/MachineFunction.h" 28 #include "llvm/CodeGen/MachineFunctionPass.h" 29 #include "llvm/CodeGen/MachineInstr.h" 30 #include "llvm/CodeGen/MachineInstrBundle.h" 31 #include "llvm/CodeGen/MachinePassManager.h" 32 #include "llvm/Support/Allocator.h" 33 #include "llvm/Support/Compiler.h" 34 #include <algorithm> 35 #include <cassert> 36 #include <iterator> 37 #include <utility> 38 39 namespace llvm { 40 41 class raw_ostream; 42 43 /// This class represents an entry in the slot index list held in the 44 /// SlotIndexes pass. It should not be used directly. See the 45 /// SlotIndex & SlotIndexes classes for the public interface to this 46 /// information. 47 class IndexListEntry : public ilist_node<IndexListEntry> { 48 MachineInstr *mi; 49 unsigned index; 50 51 public: IndexListEntry(MachineInstr * mi,unsigned index)52 IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {} 53 getInstr()54 MachineInstr* getInstr() const { return mi; } setInstr(MachineInstr * mi)55 void setInstr(MachineInstr *mi) { 56 this->mi = mi; 57 } 58 getIndex()59 unsigned getIndex() const { return index; } setIndex(unsigned index)60 void setIndex(unsigned index) { 61 this->index = index; 62 } 63 }; 64 65 /// SlotIndex - An opaque wrapper around machine indexes. 66 class SlotIndex { 67 friend class SlotIndexes; 68 69 enum Slot { 70 /// Basic block boundary. Used for live ranges entering and leaving a 71 /// block without being live in the layout neighbor. Also used as the 72 /// def slot of PHI-defs. 73 Slot_Block, 74 75 /// Early-clobber register use/def slot. A live range defined at 76 /// Slot_EarlyClobber interferes with normal live ranges killed at 77 /// Slot_Register. Also used as the kill slot for live ranges tied to an 78 /// early-clobber def. 79 Slot_EarlyClobber, 80 81 /// Normal register use/def slot. Normal instructions kill and define 82 /// register live ranges at this slot. 83 Slot_Register, 84 85 /// Dead def kill point. Kill slot for a live range that is defined by 86 /// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't 87 /// used anywhere. 88 Slot_Dead, 89 90 Slot_Count 91 }; 92 93 PointerIntPair<IndexListEntry*, 2, unsigned> lie; 94 listEntry()95 IndexListEntry* listEntry() const { 96 assert(isValid() && "Attempt to compare reserved index."); 97 return lie.getPointer(); 98 } 99 getIndex()100 unsigned getIndex() const { 101 return listEntry()->getIndex() | getSlot(); 102 } 103 104 /// Returns the slot for this SlotIndex. getSlot()105 Slot getSlot() const { 106 return static_cast<Slot>(lie.getInt()); 107 } 108 109 public: 110 enum { 111 /// The default distance between instructions as returned by distance(). 112 /// This may vary as instructions are inserted and removed. 113 InstrDist = 4 * Slot_Count 114 }; 115 116 /// Construct an invalid index. 117 SlotIndex() = default; 118 119 // Creates a SlotIndex from an IndexListEntry and a slot. Generally should 120 // not be used. This method is only public to facilitate writing certain 121 // unit tests. SlotIndex(IndexListEntry * entry,unsigned slot)122 SlotIndex(IndexListEntry *entry, unsigned slot) : lie(entry, slot) {} 123 124 // Construct a new slot index from the given one, and set the slot. SlotIndex(const SlotIndex & li,Slot s)125 SlotIndex(const SlotIndex &li, Slot s) : lie(li.listEntry(), unsigned(s)) { 126 assert(isValid() && "Attempt to construct index with 0 pointer."); 127 } 128 129 /// Returns true if this is a valid index. Invalid indices do 130 /// not point into an index table, and cannot be compared. isValid()131 bool isValid() const { 132 return lie.getPointer(); 133 } 134 135 /// Return true for a valid index. 136 explicit operator bool() const { return isValid(); } 137 138 /// Print this index to the given raw_ostream. 139 LLVM_ABI void print(raw_ostream &os) const; 140 141 /// Dump this index to stderr. 142 LLVM_ABI void dump() const; 143 144 /// Compare two SlotIndex objects for equality. 145 bool operator==(SlotIndex other) const { 146 return lie == other.lie; 147 } 148 /// Compare two SlotIndex objects for inequality. 149 bool operator!=(SlotIndex other) const { 150 return lie != other.lie; 151 } 152 153 /// Compare two SlotIndex objects. Return true if the first index 154 /// is strictly lower than the second. 155 bool operator<(SlotIndex other) const { 156 return getIndex() < other.getIndex(); 157 } 158 /// Compare two SlotIndex objects. Return true if the first index 159 /// is lower than, or equal to, the second. 160 bool operator<=(SlotIndex other) const { 161 return getIndex() <= other.getIndex(); 162 } 163 164 /// Compare two SlotIndex objects. Return true if the first index 165 /// is greater than the second. 166 bool operator>(SlotIndex other) const { 167 return getIndex() > other.getIndex(); 168 } 169 170 /// Compare two SlotIndex objects. Return true if the first index 171 /// is greater than, or equal to, the second. 172 bool operator>=(SlotIndex other) const { 173 return getIndex() >= other.getIndex(); 174 } 175 176 /// isSameInstr - Return true if A and B refer to the same instruction. isSameInstr(SlotIndex A,SlotIndex B)177 static bool isSameInstr(SlotIndex A, SlotIndex B) { 178 return A.listEntry() == B.listEntry(); 179 } 180 181 /// isEarlierInstr - Return true if A refers to an instruction earlier than 182 /// B. This is equivalent to A < B && !isSameInstr(A, B). isEarlierInstr(SlotIndex A,SlotIndex B)183 static bool isEarlierInstr(SlotIndex A, SlotIndex B) { 184 return A.listEntry()->getIndex() < B.listEntry()->getIndex(); 185 } 186 187 /// Return true if A refers to the same instruction as B or an earlier one. 188 /// This is equivalent to !isEarlierInstr(B, A). isEarlierEqualInstr(SlotIndex A,SlotIndex B)189 static bool isEarlierEqualInstr(SlotIndex A, SlotIndex B) { 190 return !isEarlierInstr(B, A); 191 } 192 193 /// Return the distance from this index to the given one. distance(SlotIndex other)194 int distance(SlotIndex other) const { 195 return other.getIndex() - getIndex(); 196 } 197 198 /// Return the scaled distance from this index to the given one, where all 199 /// slots on the same instruction have zero distance, assuming that the slot 200 /// indices are packed as densely as possible. There are normally gaps 201 /// between instructions, so this assumption often doesn't hold. This 202 /// results in this function often returning a value greater than the actual 203 /// instruction distance. getApproxInstrDistance(SlotIndex other)204 int getApproxInstrDistance(SlotIndex other) const { 205 return (other.listEntry()->getIndex() - listEntry()->getIndex()) 206 / Slot_Count; 207 } 208 209 /// isBlock - Returns true if this is a block boundary slot. isBlock()210 bool isBlock() const { return getSlot() == Slot_Block; } 211 212 /// isEarlyClobber - Returns true if this is an early-clobber slot. isEarlyClobber()213 bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; } 214 215 /// isRegister - Returns true if this is a normal register use/def slot. 216 /// Note that early-clobber slots may also be used for uses and defs. isRegister()217 bool isRegister() const { return getSlot() == Slot_Register; } 218 219 /// isDead - Returns true if this is a dead def kill slot. isDead()220 bool isDead() const { return getSlot() == Slot_Dead; } 221 222 /// Returns the base index for associated with this index. The base index 223 /// is the one associated with the Slot_Block slot for the instruction 224 /// pointed to by this index. getBaseIndex()225 SlotIndex getBaseIndex() const { 226 return SlotIndex(listEntry(), Slot_Block); 227 } 228 229 /// Returns the boundary index for associated with this index. The boundary 230 /// index is the one associated with the Slot_Block slot for the instruction 231 /// pointed to by this index. getBoundaryIndex()232 SlotIndex getBoundaryIndex() const { 233 return SlotIndex(listEntry(), Slot_Dead); 234 } 235 236 /// Returns the register use/def slot in the current instruction for a 237 /// normal or early-clobber def. 238 SlotIndex getRegSlot(bool EC = false) const { 239 return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register); 240 } 241 242 /// Returns the dead def kill slot for the current instruction. getDeadSlot()243 SlotIndex getDeadSlot() const { 244 return SlotIndex(listEntry(), Slot_Dead); 245 } 246 247 /// Returns the next slot in the index list. This could be either the 248 /// next slot for the instruction pointed to by this index or, if this 249 /// index is a STORE, the first slot for the next instruction. 250 /// WARNING: This method is considerably more expensive than the methods 251 /// that return specific slots (getUseIndex(), etc). If you can - please 252 /// use one of those methods. getNextSlot()253 SlotIndex getNextSlot() const { 254 Slot s = getSlot(); 255 if (s == Slot_Dead) { 256 return SlotIndex(&*++listEntry()->getIterator(), Slot_Block); 257 } 258 return SlotIndex(listEntry(), s + 1); 259 } 260 261 /// Returns the next index. This is the index corresponding to the this 262 /// index's slot, but for the next instruction. getNextIndex()263 SlotIndex getNextIndex() const { 264 return SlotIndex(&*++listEntry()->getIterator(), getSlot()); 265 } 266 267 /// Returns the previous slot in the index list. This could be either the 268 /// previous slot for the instruction pointed to by this index or, if this 269 /// index is a Slot_Block, the last slot for the previous 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. getPrevSlot()273 SlotIndex getPrevSlot() const { 274 Slot s = getSlot(); 275 if (s == Slot_Block) { 276 return SlotIndex(&*--listEntry()->getIterator(), Slot_Dead); 277 } 278 return SlotIndex(listEntry(), s - 1); 279 } 280 281 /// Returns the previous index. This is the index corresponding to this 282 /// index's slot, but for the previous instruction. getPrevIndex()283 SlotIndex getPrevIndex() const { 284 return SlotIndex(&*--listEntry()->getIterator(), getSlot()); 285 } 286 }; 287 288 inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) { 289 li.print(os); 290 return os; 291 } 292 293 using IdxMBBPair = std::pair<SlotIndex, MachineBasicBlock *>; 294 295 /// SlotIndexes pass. 296 /// 297 /// This pass assigns indexes to each instruction. 298 class SlotIndexes { 299 friend class SlotIndexesWrapperPass; 300 301 private: 302 // IndexListEntry allocator. 303 BumpPtrAllocator ileAllocator; 304 305 using IndexList = simple_ilist<IndexListEntry>; 306 IndexList indexList; 307 308 MachineFunction *mf = nullptr; 309 310 using Mi2IndexMap = DenseMap<const MachineInstr *, SlotIndex>; 311 Mi2IndexMap mi2iMap; 312 313 /// MBBRanges - Map MBB number to (start, stop) indexes. 314 SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges; 315 316 /// Idx2MBBMap - Sorted list of pairs of index of first instruction 317 /// and MBB id. 318 SmallVector<IdxMBBPair, 8> idx2MBBMap; 319 320 // For legacy pass manager. 321 SlotIndexes() = default; 322 323 LLVM_ABI void clear(); 324 325 LLVM_ABI void analyze(MachineFunction &MF); 326 createEntry(MachineInstr * mi,unsigned index)327 IndexListEntry* createEntry(MachineInstr *mi, unsigned index) { 328 IndexListEntry *entry = 329 static_cast<IndexListEntry *>(ileAllocator.Allocate( 330 sizeof(IndexListEntry), alignof(IndexListEntry))); 331 332 new (entry) IndexListEntry(mi, index); 333 334 return entry; 335 } 336 337 /// Renumber locally after inserting curItr. 338 LLVM_ABI void renumberIndexes(IndexList::iterator curItr); 339 340 public: 341 SlotIndexes(SlotIndexes &&) = default; 342 SlotIndexes(MachineFunction & MF)343 SlotIndexes(MachineFunction &MF) { analyze(MF); } 344 345 LLVM_ABI ~SlotIndexes(); 346 reanalyze(MachineFunction & MF)347 void reanalyze(MachineFunction &MF) { 348 clear(); 349 analyze(MF); 350 } 351 352 LLVM_ABI void print(raw_ostream &OS) const; 353 354 /// Dump the indexes. 355 LLVM_ABI void dump() const; 356 357 /// Repair indexes after adding and removing instructions. 358 LLVM_ABI void repairIndexesInRange(MachineBasicBlock *MBB, 359 MachineBasicBlock::iterator Begin, 360 MachineBasicBlock::iterator End); 361 362 /// Returns the zero index for this analysis. getZeroIndex()363 SlotIndex getZeroIndex() { 364 assert(indexList.front().getIndex() == 0 && "First index is not 0?"); 365 return SlotIndex(&indexList.front(), 0); 366 } 367 368 /// Returns the base index of the last slot in this analysis. getLastIndex()369 SlotIndex getLastIndex() { 370 return SlotIndex(&indexList.back(), 0); 371 } 372 373 /// Returns true if the given machine instr is mapped to an index, 374 /// otherwise returns false. hasIndex(const MachineInstr & instr)375 bool hasIndex(const MachineInstr &instr) const { 376 return mi2iMap.count(&instr); 377 } 378 379 /// Returns the base index for the given instruction. 380 SlotIndex getInstructionIndex(const MachineInstr &MI, 381 bool IgnoreBundle = false) const { 382 // Instructions inside a bundle have the same number as the bundle itself. 383 auto BundleStart = getBundleStart(MI.getIterator()); 384 auto BundleEnd = getBundleEnd(MI.getIterator()); 385 // Use the first non-debug instruction in the bundle to get SlotIndex. 386 const MachineInstr &BundleNonDebug = 387 IgnoreBundle ? MI 388 : *skipDebugInstructionsForward(BundleStart, BundleEnd); 389 assert(!BundleNonDebug.isDebugInstr() && 390 "Could not use a debug instruction to query mi2iMap."); 391 Mi2IndexMap::const_iterator itr = mi2iMap.find(&BundleNonDebug); 392 assert(itr != mi2iMap.end() && "Instruction not found in maps."); 393 return itr->second; 394 } 395 396 /// Returns the instruction for the given index, or null if the given 397 /// index has no instruction associated with it. getInstructionFromIndex(SlotIndex index)398 MachineInstr* getInstructionFromIndex(SlotIndex index) const { 399 return index.listEntry()->getInstr(); 400 } 401 402 /// Returns the next non-null index, if one exists. 403 /// Otherwise returns getLastIndex(). getNextNonNullIndex(SlotIndex Index)404 SlotIndex getNextNonNullIndex(SlotIndex Index) { 405 IndexList::iterator I = Index.listEntry()->getIterator(); 406 IndexList::iterator E = indexList.end(); 407 while (++I != E) 408 if (I->getInstr()) 409 return SlotIndex(&*I, Index.getSlot()); 410 // We reached the end of the function. 411 return getLastIndex(); 412 } 413 414 /// getIndexBefore - Returns the index of the last indexed instruction 415 /// before MI, or the start index of its basic block. 416 /// MI is not required to have an index. getIndexBefore(const MachineInstr & MI)417 SlotIndex getIndexBefore(const MachineInstr &MI) const { 418 const MachineBasicBlock *MBB = MI.getParent(); 419 assert(MBB && "MI must be inserted in a basic block"); 420 MachineBasicBlock::const_iterator I = MI, B = MBB->begin(); 421 while (true) { 422 if (I == B) 423 return getMBBStartIdx(MBB); 424 --I; 425 Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I); 426 if (MapItr != mi2iMap.end()) 427 return MapItr->second; 428 } 429 } 430 431 /// getIndexAfter - Returns the index of the first indexed instruction 432 /// after MI, or the end index of its basic block. 433 /// MI is not required to have an index. getIndexAfter(const MachineInstr & MI)434 SlotIndex getIndexAfter(const MachineInstr &MI) const { 435 const MachineBasicBlock *MBB = MI.getParent(); 436 assert(MBB && "MI must be inserted in a basic block"); 437 MachineBasicBlock::const_iterator I = MI, E = MBB->end(); 438 while (true) { 439 ++I; 440 if (I == E) 441 return getMBBEndIdx(MBB); 442 Mi2IndexMap::const_iterator MapItr = mi2iMap.find(&*I); 443 if (MapItr != mi2iMap.end()) 444 return MapItr->second; 445 } 446 } 447 448 /// Return the (start,end) range of the given basic block number. 449 const std::pair<SlotIndex, SlotIndex> & getMBBRange(unsigned Num)450 getMBBRange(unsigned Num) const { 451 return MBBRanges[Num]; 452 } 453 454 /// Return the (start,end) range of the given basic block. 455 const std::pair<SlotIndex, SlotIndex> & getMBBRange(const MachineBasicBlock * MBB)456 getMBBRange(const MachineBasicBlock *MBB) const { 457 return getMBBRange(MBB->getNumber()); 458 } 459 460 /// Returns the first index in the given basic block number. getMBBStartIdx(unsigned Num)461 SlotIndex getMBBStartIdx(unsigned Num) const { 462 return getMBBRange(Num).first; 463 } 464 465 /// Returns the first index in the given basic block. getMBBStartIdx(const MachineBasicBlock * mbb)466 SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const { 467 return getMBBRange(mbb).first; 468 } 469 470 /// Returns the last index in the given basic block number. getMBBEndIdx(unsigned Num)471 SlotIndex getMBBEndIdx(unsigned Num) const { 472 return getMBBRange(Num).second; 473 } 474 475 /// Returns the last index in the given basic block. getMBBEndIdx(const MachineBasicBlock * mbb)476 SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const { 477 return getMBBRange(mbb).second; 478 } 479 480 /// Iterator over the idx2MBBMap (sorted pairs of slot index of basic block 481 /// begin and basic block) 482 using MBBIndexIterator = SmallVectorImpl<IdxMBBPair>::const_iterator; 483 484 /// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater 485 /// than or equal to \p Idx. If \p Start is provided, only search the range 486 /// from \p Start to the end of the function. getMBBLowerBound(MBBIndexIterator Start,SlotIndex Idx)487 MBBIndexIterator getMBBLowerBound(MBBIndexIterator Start, 488 SlotIndex Idx) const { 489 return std::lower_bound( 490 Start, MBBIndexEnd(), Idx, 491 [](const IdxMBBPair &IM, SlotIndex Idx) { return IM.first < Idx; }); 492 } getMBBLowerBound(SlotIndex Idx)493 MBBIndexIterator getMBBLowerBound(SlotIndex Idx) const { 494 return getMBBLowerBound(MBBIndexBegin(), Idx); 495 } 496 497 /// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater 498 /// than \p Idx. getMBBUpperBound(SlotIndex Idx)499 MBBIndexIterator getMBBUpperBound(SlotIndex Idx) const { 500 return std::upper_bound( 501 MBBIndexBegin(), MBBIndexEnd(), Idx, 502 [](SlotIndex Idx, const IdxMBBPair &IM) { return Idx < IM.first; }); 503 } 504 505 /// Returns an iterator for the begin of the idx2MBBMap. MBBIndexBegin()506 MBBIndexIterator MBBIndexBegin() const { 507 return idx2MBBMap.begin(); 508 } 509 510 /// Return an iterator for the end of the idx2MBBMap. MBBIndexEnd()511 MBBIndexIterator MBBIndexEnd() const { 512 return idx2MBBMap.end(); 513 } 514 515 /// Returns the basic block which the given index falls in. getMBBFromIndex(SlotIndex index)516 MachineBasicBlock* getMBBFromIndex(SlotIndex index) const { 517 if (MachineInstr *MI = getInstructionFromIndex(index)) 518 return MI->getParent(); 519 520 MBBIndexIterator I = std::prev(getMBBUpperBound(index)); 521 assert(I != MBBIndexEnd() && I->first <= index && 522 index < getMBBEndIdx(I->second) && 523 "index does not correspond to an MBB"); 524 return I->second; 525 } 526 527 /// Insert the given machine instruction into the mapping. Returns the 528 /// assigned index. 529 /// If Late is set and there are null indexes between mi's neighboring 530 /// instructions, create the new index after the null indexes instead of 531 /// before them. 532 SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late = false) { 533 assert(!MI.isInsideBundle() && 534 "Instructions inside bundles should use bundle start's slot."); 535 assert(!mi2iMap.contains(&MI) && "Instr already indexed."); 536 // Numbering debug instructions could cause code generation to be 537 // affected by debug information. 538 assert(!MI.isDebugInstr() && "Cannot number debug instructions."); 539 540 assert(MI.getParent() != nullptr && "Instr must be added to function."); 541 542 // Get the entries where MI should be inserted. 543 IndexList::iterator prevItr, nextItr; 544 if (Late) { 545 // Insert MI's index immediately before the following instruction. 546 nextItr = getIndexAfter(MI).listEntry()->getIterator(); 547 prevItr = std::prev(nextItr); 548 } else { 549 // Insert MI's index immediately after the preceding instruction. 550 prevItr = getIndexBefore(MI).listEntry()->getIterator(); 551 nextItr = std::next(prevItr); 552 } 553 554 // Get a number for the new instr, or 0 if there's no room currently. 555 // In the latter case we'll force a renumber later. 556 unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u; 557 unsigned newNumber = prevItr->getIndex() + dist; 558 559 // Insert a new list entry for MI. 560 IndexList::iterator newItr = 561 indexList.insert(nextItr, *createEntry(&MI, newNumber)); 562 563 // Renumber locally if we need to. 564 if (dist == 0) 565 renumberIndexes(newItr); 566 567 SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block); 568 mi2iMap.insert(std::make_pair(&MI, newIndex)); 569 return newIndex; 570 } 571 572 /// Removes machine instruction (bundle) \p MI from the mapping. 573 /// This should be called before MachineInstr::eraseFromParent() is used to 574 /// remove a whole bundle or an unbundled instruction. 575 /// If \p AllowBundled is set then this can be used on a bundled 576 /// instruction; however, this exists to support handleMoveIntoBundle, 577 /// and in general removeSingleMachineInstrFromMaps should be used instead. 578 LLVM_ABI void removeMachineInstrFromMaps(MachineInstr &MI, 579 bool AllowBundled = false); 580 581 /// Removes a single machine instruction \p MI from the mapping. 582 /// This should be called before MachineInstr::eraseFromBundle() is used to 583 /// remove a single instruction (out of a bundle). 584 LLVM_ABI void removeSingleMachineInstrFromMaps(MachineInstr &MI); 585 586 /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in 587 /// maps used by register allocator. \returns the index where the new 588 /// instruction was inserted. replaceMachineInstrInMaps(MachineInstr & MI,MachineInstr & NewMI)589 SlotIndex replaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI) { 590 Mi2IndexMap::iterator mi2iItr = mi2iMap.find(&MI); 591 if (mi2iItr == mi2iMap.end()) 592 return SlotIndex(); 593 SlotIndex replaceBaseIndex = mi2iItr->second; 594 IndexListEntry *miEntry(replaceBaseIndex.listEntry()); 595 assert(miEntry->getInstr() == &MI && 596 "Mismatched instruction in index tables."); 597 miEntry->setInstr(&NewMI); 598 mi2iMap.erase(mi2iItr); 599 mi2iMap.insert(std::make_pair(&NewMI, replaceBaseIndex)); 600 return replaceBaseIndex; 601 } 602 603 /// Add the given MachineBasicBlock into the maps. 604 /// If it contains any instructions then they must already be in the maps. 605 /// This is used after a block has been split by moving some suffix of its 606 /// instructions into a newly created block. insertMBBInMaps(MachineBasicBlock * mbb)607 void insertMBBInMaps(MachineBasicBlock *mbb) { 608 assert(mbb != &mbb->getParent()->front() && 609 "Can't insert a new block at the beginning of a function."); 610 auto prevMBB = std::prev(MachineFunction::iterator(mbb)); 611 612 // Create a new entry to be used for the start of mbb and the end of 613 // prevMBB. 614 IndexListEntry *startEntry = createEntry(nullptr, 0); 615 IndexListEntry *endEntry = getMBBEndIdx(&*prevMBB).listEntry(); 616 IndexListEntry *insEntry = 617 mbb->empty() ? endEntry 618 : getInstructionIndex(mbb->front()).listEntry(); 619 IndexList::iterator newItr = 620 indexList.insert(insEntry->getIterator(), *startEntry); 621 622 SlotIndex startIdx(startEntry, SlotIndex::Slot_Block); 623 SlotIndex endIdx(endEntry, SlotIndex::Slot_Block); 624 625 MBBRanges[prevMBB->getNumber()].second = startIdx; 626 627 assert(unsigned(mbb->getNumber()) == MBBRanges.size() && 628 "Blocks must be added in order"); 629 MBBRanges.push_back(std::make_pair(startIdx, endIdx)); 630 idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb)); 631 632 renumberIndexes(newItr); 633 llvm::sort(idx2MBBMap, less_first()); 634 } 635 636 /// Renumber all indexes using the default instruction distance. 637 LLVM_ABI void packIndexes(); 638 }; 639 640 // Specialize IntervalMapInfo for half-open slot index intervals. 641 template <> 642 struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> { 643 }; 644 645 class SlotIndexesAnalysis : public AnalysisInfoMixin<SlotIndexesAnalysis> { 646 friend AnalysisInfoMixin<SlotIndexesAnalysis>; 647 LLVM_ABI static AnalysisKey Key; 648 649 public: 650 using Result = SlotIndexes; 651 LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &); 652 }; 653 654 class SlotIndexesPrinterPass : public PassInfoMixin<SlotIndexesPrinterPass> { 655 raw_ostream &OS; 656 657 public: 658 explicit SlotIndexesPrinterPass(raw_ostream &OS) : OS(OS) {} 659 LLVM_ABI PreservedAnalyses run(MachineFunction &MF, 660 MachineFunctionAnalysisManager &MFAM); 661 static bool isRequired() { return true; } 662 }; 663 664 class LLVM_ABI SlotIndexesWrapperPass : public MachineFunctionPass { 665 SlotIndexes SI; 666 667 public: 668 static char ID; 669 670 SlotIndexesWrapperPass(); 671 672 void getAnalysisUsage(AnalysisUsage &au) const override; 673 void releaseMemory() override { SI.clear(); } 674 675 bool runOnMachineFunction(MachineFunction &fn) override { 676 SI.analyze(fn); 677 return false; 678 } 679 680 SlotIndexes &getSI() { return SI; } 681 }; 682 683 } // end namespace llvm 684 685 #endif // LLVM_CODEGEN_SLOTINDEXES_H 686