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