SlotIndexes.h 24 KB

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