SlotIndexes.h 24 KB

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