| 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: |
| 52 | IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {} |
| 53 | |
| 54 | MachineInstr* getInstr() const { return mi; } |
| 55 | void setInstr(MachineInstr *mi) { |
| 56 | this->mi = mi; |
| 57 | } |
| 58 | |
| 59 | unsigned getIndex() const { return 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 | |
| 95 | IndexListEntry* listEntry() const { |
| 96 | assert(isValid() && "Attempt to compare reserved index." ); |
| 97 | return lie.getPointer(); |
| 98 | } |
| 99 | |
| 100 | unsigned getIndex() const { |
| 101 | return listEntry()->getIndex() | getSlot(); |
| 102 | } |
| 103 | |
| 104 | /// Returns the slot for this SlotIndex. |
| 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. |
| 122 | SlotIndex(IndexListEntry *entry, unsigned slot) : lie(entry, slot) {} |
| 123 | |
| 124 | // Construct a new slot index from the given one, and set the slot. |
| 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. |
| 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. |
| 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). |
| 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). |
| 189 | static bool isEarlierEqualInstr(SlotIndex A, SlotIndex B) { |
| 190 | return !isEarlierInstr(A: B, B: A); |
| 191 | } |
| 192 | |
| 193 | /// Return the distance from this index to the given one. |
| 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. |
| 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. |
| 210 | bool isBlock() const { return getSlot() == Slot_Block; } |
| 211 | |
| 212 | /// isEarlyClobber - Returns true if this is an early-clobber slot. |
| 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. |
| 217 | bool isRegister() const { return getSlot() == Slot_Register; } |
| 218 | |
| 219 | /// isDead - Returns true if this is a dead def kill slot. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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. |
| 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 | |
| 327 | IndexListEntry* createEntry(MachineInstr *mi, unsigned index) { |
| 328 | IndexListEntry *entry = |
| 329 | static_cast<IndexListEntry *>(ileAllocator.Allocate( |
| 330 | Size: sizeof(IndexListEntry), Alignment: 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 | |
| 343 | SlotIndexes(MachineFunction &MF) { analyze(MF); } |
| 344 | |
| 345 | LLVM_ABI ~SlotIndexes(); |
| 346 | |
| 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. |
| 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. |
| 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. |
| 375 | bool hasIndex(const MachineInstr &instr) const { |
| 376 | return mi2iMap.count(Val: &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(I: MI.getIterator()); |
| 384 | auto BundleEnd = getBundleEnd(I: MI.getIterator()); |
| 385 | // Use the first non-debug instruction in the bundle to get SlotIndex. |
| 386 | const MachineInstr &BundleNonDebug = |
| 387 | IgnoreBundle ? MI |
| 388 | : *skipDebugInstructionsForward(It: BundleStart, End: BundleEnd); |
| 389 | assert(!BundleNonDebug.isDebugInstr() && |
| 390 | "Could not use a debug instruction to query mi2iMap." ); |
| 391 | Mi2IndexMap::const_iterator itr = mi2iMap.find(Val: &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. |
| 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(). |
| 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. |
| 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: MBB); |
| 424 | --I; |
| 425 | Mi2IndexMap::const_iterator MapItr = mi2iMap.find(Val: &*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. |
| 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: MBB); |
| 442 | Mi2IndexMap::const_iterator MapItr = mi2iMap.find(Val: &*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> & |
| 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> & |
| 456 | getMBBRange(const MachineBasicBlock *MBB) const { |
| 457 | return getMBBRange(Num: MBB->getNumber()); |
| 458 | } |
| 459 | |
| 460 | /// Returns the first index in the given basic block number. |
| 461 | SlotIndex getMBBStartIdx(unsigned Num) const { |
| 462 | return getMBBRange(Num).first; |
| 463 | } |
| 464 | |
| 465 | /// Returns the first index in the given basic block. |
| 466 | SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const { |
| 467 | return getMBBRange(MBB: mbb).first; |
| 468 | } |
| 469 | |
| 470 | /// Returns the index past the last valid index in the given basic block. |
| 471 | SlotIndex getMBBEndIdx(unsigned Num) const { |
| 472 | return getMBBRange(Num).second; |
| 473 | } |
| 474 | |
| 475 | /// Returns the index past the last valid index in the given basic block. |
| 476 | SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const { |
| 477 | return getMBBRange(MBB: mbb).second; |
| 478 | } |
| 479 | |
| 480 | /// Returns the last valid index in the given basic block. |
| 481 | /// This index corresponds to the dead slot of the last non-debug |
| 482 | /// instruction and can be used to find live-out ranges of the block. Note |
| 483 | /// that getMBBEndIdx returns the start index of the next block, which is |
| 484 | /// also used as the start index for segments with phi-def values. If the |
| 485 | /// basic block doesn't contain any non-debug instructions, this returns |
| 486 | /// the same as getMBBStartIdx.getDeadSlot(). |
| 487 | SlotIndex getMBBLastIdx(const MachineBasicBlock *MBB) const { |
| 488 | return getMBBEndIdx(mbb: MBB).getPrevSlot(); |
| 489 | } |
| 490 | |
| 491 | /// Iterator over the idx2MBBMap (sorted pairs of slot index of basic block |
| 492 | /// begin and basic block) |
| 493 | using MBBIndexIterator = SmallVectorImpl<IdxMBBPair>::const_iterator; |
| 494 | |
| 495 | /// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater |
| 496 | /// than or equal to \p Idx. If \p Start is provided, only search the range |
| 497 | /// from \p Start to the end of the function. |
| 498 | MBBIndexIterator getMBBLowerBound(MBBIndexIterator Start, |
| 499 | SlotIndex Idx) const { |
| 500 | return std::lower_bound( |
| 501 | first: Start, last: MBBIndexEnd(), val: Idx, |
| 502 | comp: [](const IdxMBBPair &IM, SlotIndex Idx) { return IM.first < Idx; }); |
| 503 | } |
| 504 | MBBIndexIterator getMBBLowerBound(SlotIndex Idx) const { |
| 505 | return getMBBLowerBound(Start: MBBIndexBegin(), Idx); |
| 506 | } |
| 507 | |
| 508 | /// Get an iterator pointing to the first IdxMBBPair with SlotIndex greater |
| 509 | /// than \p Idx. |
| 510 | MBBIndexIterator getMBBUpperBound(SlotIndex Idx) const { |
| 511 | return std::upper_bound( |
| 512 | first: MBBIndexBegin(), last: MBBIndexEnd(), val: Idx, |
| 513 | comp: [](SlotIndex Idx, const IdxMBBPair &IM) { return Idx < IM.first; }); |
| 514 | } |
| 515 | |
| 516 | /// Returns an iterator for the begin of the idx2MBBMap. |
| 517 | MBBIndexIterator MBBIndexBegin() const { |
| 518 | return idx2MBBMap.begin(); |
| 519 | } |
| 520 | |
| 521 | /// Return an iterator for the end of the idx2MBBMap. |
| 522 | MBBIndexIterator MBBIndexEnd() const { |
| 523 | return idx2MBBMap.end(); |
| 524 | } |
| 525 | |
| 526 | /// Returns the basic block which the given index falls in. |
| 527 | MachineBasicBlock* getMBBFromIndex(SlotIndex index) const { |
| 528 | if (MachineInstr *MI = getInstructionFromIndex(index)) |
| 529 | return MI->getParent(); |
| 530 | |
| 531 | MBBIndexIterator I = std::prev(x: getMBBUpperBound(Idx: index)); |
| 532 | assert(I != MBBIndexEnd() && I->first <= index && |
| 533 | index < getMBBEndIdx(I->second) && |
| 534 | "index does not correspond to an MBB" ); |
| 535 | return I->second; |
| 536 | } |
| 537 | |
| 538 | /// Insert the given machine instruction into the mapping. Returns the |
| 539 | /// assigned index. |
| 540 | /// If Late is set and there are null indexes between mi's neighboring |
| 541 | /// instructions, create the new index after the null indexes instead of |
| 542 | /// before them. |
| 543 | SlotIndex insertMachineInstrInMaps(MachineInstr &MI, bool Late = false) { |
| 544 | assert(!MI.isInsideBundle() && |
| 545 | "Instructions inside bundles should use bundle start's slot." ); |
| 546 | assert(!mi2iMap.contains(&MI) && "Instr already indexed." ); |
| 547 | // Numbering debug instructions could cause code generation to be |
| 548 | // affected by debug information. |
| 549 | assert(!MI.isDebugInstr() && "Cannot number debug instructions." ); |
| 550 | |
| 551 | assert(MI.getParent() != nullptr && "Instr must be added to function." ); |
| 552 | |
| 553 | // Get the entries where MI should be inserted. |
| 554 | IndexList::iterator prevItr, nextItr; |
| 555 | if (Late) { |
| 556 | // Insert MI's index immediately before the following instruction. |
| 557 | nextItr = getIndexAfter(MI).listEntry()->getIterator(); |
| 558 | prevItr = std::prev(x: nextItr); |
| 559 | } else { |
| 560 | // Insert MI's index immediately after the preceding instruction. |
| 561 | prevItr = getIndexBefore(MI).listEntry()->getIterator(); |
| 562 | nextItr = std::next(x: prevItr); |
| 563 | } |
| 564 | |
| 565 | // Get a number for the new instr, or 0 if there's no room currently. |
| 566 | // In the latter case we'll force a renumber later. |
| 567 | unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u; |
| 568 | unsigned newNumber = prevItr->getIndex() + dist; |
| 569 | |
| 570 | // Insert a new list entry for MI. |
| 571 | IndexList::iterator newItr = |
| 572 | indexList.insert(I: nextItr, Node&: *createEntry(mi: &MI, index: newNumber)); |
| 573 | |
| 574 | // Renumber locally if we need to. |
| 575 | if (dist == 0) |
| 576 | renumberIndexes(curItr: newItr); |
| 577 | |
| 578 | SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block); |
| 579 | mi2iMap.insert(KV: std::make_pair(x: &MI, y&: newIndex)); |
| 580 | return newIndex; |
| 581 | } |
| 582 | |
| 583 | /// Removes machine instruction (bundle) \p MI from the mapping. |
| 584 | /// This should be called before MachineInstr::eraseFromParent() is used to |
| 585 | /// remove a whole bundle or an unbundled instruction. |
| 586 | /// If \p AllowBundled is set then this can be used on a bundled |
| 587 | /// instruction; however, this exists to support handleMoveIntoBundle, |
| 588 | /// and in general removeSingleMachineInstrFromMaps should be used instead. |
| 589 | LLVM_ABI void removeMachineInstrFromMaps(MachineInstr &MI, |
| 590 | bool AllowBundled = false); |
| 591 | |
| 592 | /// Removes a single machine instruction \p MI from the mapping. |
| 593 | /// This should be called before MachineInstr::eraseFromBundle() is used to |
| 594 | /// remove a single instruction (out of a bundle). |
| 595 | LLVM_ABI void removeSingleMachineInstrFromMaps(MachineInstr &MI); |
| 596 | |
| 597 | /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in |
| 598 | /// maps used by register allocator. \returns the index where the new |
| 599 | /// instruction was inserted. |
| 600 | SlotIndex replaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI) { |
| 601 | Mi2IndexMap::iterator mi2iItr = mi2iMap.find(Val: &MI); |
| 602 | if (mi2iItr == mi2iMap.end()) |
| 603 | return SlotIndex(); |
| 604 | SlotIndex replaceBaseIndex = mi2iItr->second; |
| 605 | IndexListEntry *miEntry(replaceBaseIndex.listEntry()); |
| 606 | assert(miEntry->getInstr() == &MI && |
| 607 | "Mismatched instruction in index tables." ); |
| 608 | miEntry->setInstr(&NewMI); |
| 609 | mi2iMap.erase(I: mi2iItr); |
| 610 | mi2iMap.insert(KV: std::make_pair(x: &NewMI, y&: replaceBaseIndex)); |
| 611 | return replaceBaseIndex; |
| 612 | } |
| 613 | |
| 614 | /// Add the given MachineBasicBlock into the maps. |
| 615 | /// If it contains any instructions then they must already be in the maps. |
| 616 | /// This is used after a block has been split by moving some suffix of its |
| 617 | /// instructions into a newly created block. |
| 618 | void insertMBBInMaps(MachineBasicBlock *mbb) { |
| 619 | assert(mbb != &mbb->getParent()->front() && |
| 620 | "Can't insert a new block at the beginning of a function." ); |
| 621 | auto prevMBB = std::prev(x: MachineFunction::iterator(mbb)); |
| 622 | |
| 623 | // Create a new entry to be used for the start of mbb and the end of |
| 624 | // prevMBB. |
| 625 | IndexListEntry *startEntry = createEntry(mi: nullptr, index: 0); |
| 626 | IndexListEntry *endEntry = getMBBEndIdx(mbb: &*prevMBB).listEntry(); |
| 627 | IndexListEntry *insEntry = |
| 628 | mbb->empty() ? endEntry |
| 629 | : getInstructionIndex(MI: mbb->front()).listEntry(); |
| 630 | IndexList::iterator newItr = |
| 631 | indexList.insert(I: insEntry->getIterator(), Node&: *startEntry); |
| 632 | |
| 633 | SlotIndex startIdx(startEntry, SlotIndex::Slot_Block); |
| 634 | SlotIndex endIdx(endEntry, SlotIndex::Slot_Block); |
| 635 | |
| 636 | MBBRanges[prevMBB->getNumber()].second = startIdx; |
| 637 | |
| 638 | assert(unsigned(mbb->getNumber()) == MBBRanges.size() && |
| 639 | "Blocks must be added in order" ); |
| 640 | MBBRanges.push_back(Elt: std::make_pair(x&: startIdx, y&: endIdx)); |
| 641 | idx2MBBMap.push_back(Elt: IdxMBBPair(startIdx, mbb)); |
| 642 | |
| 643 | renumberIndexes(curItr: newItr); |
| 644 | llvm::sort(C&: idx2MBBMap, Comp: less_first()); |
| 645 | } |
| 646 | |
| 647 | /// Renumber all indexes using the default instruction distance. |
| 648 | LLVM_ABI void packIndexes(); |
| 649 | }; |
| 650 | |
| 651 | // Specialize IntervalMapInfo for half-open slot index intervals. |
| 652 | template <> |
| 653 | struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> { |
| 654 | }; |
| 655 | |
| 656 | class SlotIndexesAnalysis : public AnalysisInfoMixin<SlotIndexesAnalysis> { |
| 657 | friend AnalysisInfoMixin<SlotIndexesAnalysis>; |
| 658 | LLVM_ABI static AnalysisKey Key; |
| 659 | |
| 660 | public: |
| 661 | using Result = SlotIndexes; |
| 662 | LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &); |
| 663 | }; |
| 664 | |
| 665 | class SlotIndexesPrinterPass : public PassInfoMixin<SlotIndexesPrinterPass> { |
| 666 | raw_ostream &OS; |
| 667 | |
| 668 | public: |
| 669 | explicit SlotIndexesPrinterPass(raw_ostream &OS) : OS(OS) {} |
| 670 | LLVM_ABI PreservedAnalyses run(MachineFunction &MF, |
| 671 | MachineFunctionAnalysisManager &MFAM); |
| 672 | static bool isRequired() { return true; } |
| 673 | }; |
| 674 | |
| 675 | class LLVM_ABI SlotIndexesWrapperPass : public MachineFunctionPass { |
| 676 | SlotIndexes SI; |
| 677 | |
| 678 | public: |
| 679 | static char ID; |
| 680 | |
| 681 | SlotIndexesWrapperPass(); |
| 682 | |
| 683 | void getAnalysisUsage(AnalysisUsage &au) const override; |
| 684 | void releaseMemory() override { SI.clear(); } |
| 685 | |
| 686 | bool runOnMachineFunction(MachineFunction &fn) override { |
| 687 | SI.analyze(MF&: fn); |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | SlotIndexes &getSI() { return SI; } |
| 692 | }; |
| 693 | |
| 694 | } // end namespace llvm |
| 695 | |
| 696 | #endif // LLVM_CODEGEN_SLOTINDEXES_H |
| 697 | |