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