SlotIndexes.h 24 KB

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