SlotIndexes.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. //===- llvm/CodeGen/SlotIndexes.h - Slot indexes representation -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements SlotIndex and related classes. The purpose of SlotIndex
  11. // is to describe a position at which a register can become live, or cease to
  12. // be live.
  13. //
  14. // SlotIndex is mostly a proxy for entries of the SlotIndexList, a class which
  15. // is held is LiveIntervals and provides the real numbering. This allows
  16. // LiveIntervals to perform largely transparent renumbering.
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_SLOTINDEXES_H
  19. #define LLVM_CODEGEN_SLOTINDEXES_H
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/ADT/IntervalMap.h"
  22. #include "llvm/ADT/PointerIntPair.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/ilist.h"
  25. #include "llvm/CodeGen/MachineFunction.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include "llvm/CodeGen/MachineInstrBundle.h"
  28. #include "llvm/Support/Allocator.h"
  29. namespace llvm {
  30. /// This class represents an entry in the slot index list held in the
  31. /// SlotIndexes pass. It should not be used directly. See the
  32. /// SlotIndex & SlotIndexes classes for the public interface to this
  33. /// information.
  34. class IndexListEntry : public ilist_node<IndexListEntry> {
  35. MachineInstr *mi;
  36. unsigned index;
  37. public:
  38. IndexListEntry(MachineInstr *mi, unsigned index) : mi(mi), index(index) {}
  39. MachineInstr* getInstr() const { return mi; }
  40. void setInstr(MachineInstr *mi) {
  41. this->mi = mi;
  42. }
  43. unsigned getIndex() const { return index; }
  44. void setIndex(unsigned index) {
  45. this->index = index;
  46. }
  47. #ifdef EXPENSIVE_CHECKS
  48. // When EXPENSIVE_CHECKS is defined, "erased" index list entries will
  49. // actually be moved to a "graveyard" list, and have their pointers
  50. // poisoned, so that dangling SlotIndex access can be reliably detected.
  51. void setPoison() {
  52. intptr_t tmp = reinterpret_cast<intptr_t>(mi);
  53. assert(((tmp & 0x1) == 0x0) && "Pointer already poisoned?");
  54. tmp |= 0x1;
  55. mi = reinterpret_cast<MachineInstr*>(tmp);
  56. }
  57. bool isPoisoned() const { return (reinterpret_cast<intptr_t>(mi) & 0x1) == 0x1; }
  58. #endif // EXPENSIVE_CHECKS
  59. };
  60. template <>
  61. struct ilist_traits<IndexListEntry> : public ilist_default_traits<IndexListEntry> {
  62. private:
  63. mutable ilist_half_node<IndexListEntry> Sentinel;
  64. public:
  65. IndexListEntry *createSentinel() const {
  66. return static_cast<IndexListEntry*>(&Sentinel);
  67. }
  68. void destroySentinel(IndexListEntry *) const {}
  69. IndexListEntry *provideInitialHead() const { return createSentinel(); }
  70. IndexListEntry *ensureHead(IndexListEntry*) const { return createSentinel(); }
  71. static void noteHead(IndexListEntry*, IndexListEntry*) {}
  72. void deleteNode(IndexListEntry *N) {}
  73. private:
  74. void createNode(const IndexListEntry &);
  75. };
  76. /// SlotIndex - An opaque wrapper around machine indexes.
  77. class SlotIndex {
  78. friend class SlotIndexes;
  79. enum Slot {
  80. /// Basic block boundary. Used for live ranges entering and leaving a
  81. /// block without being live in the layout neighbor. Also used as the
  82. /// def slot of PHI-defs.
  83. Slot_Block,
  84. /// Early-clobber register use/def slot. A live range defined at
  85. /// Slot_EarlyCLobber interferes with normal live ranges killed at
  86. /// Slot_Register. Also used as the kill slot for live ranges tied to an
  87. /// early-clobber def.
  88. Slot_EarlyClobber,
  89. /// Normal register use/def slot. Normal instructions kill and define
  90. /// register live ranges at this slot.
  91. Slot_Register,
  92. /// Dead def kill point. Kill slot for a live range that is defined by
  93. /// the same instruction (Slot_Register or Slot_EarlyClobber), but isn't
  94. /// used anywhere.
  95. Slot_Dead,
  96. Slot_Count
  97. };
  98. PointerIntPair<IndexListEntry*, 2, unsigned> lie;
  99. SlotIndex(IndexListEntry *entry, unsigned slot)
  100. : lie(entry, slot) {}
  101. IndexListEntry* listEntry() const {
  102. assert(isValid() && "Attempt to compare reserved index.");
  103. #ifdef EXPENSIVE_CHECKS
  104. assert(!lie.getPointer()->isPoisoned() &&
  105. "Attempt to access deleted list-entry.");
  106. #endif // EXPENSIVE_CHECKS
  107. return lie.getPointer();
  108. }
  109. unsigned getIndex() const {
  110. return listEntry()->getIndex() | getSlot();
  111. }
  112. /// Returns the slot for this SlotIndex.
  113. Slot getSlot() const {
  114. return static_cast<Slot>(lie.getInt());
  115. }
  116. public:
  117. enum {
  118. /// The default distance between instructions as returned by distance().
  119. /// This may vary as instructions are inserted and removed.
  120. InstrDist = 4 * Slot_Count
  121. };
  122. /// Construct an invalid index.
  123. SlotIndex() : lie(nullptr, 0) {}
  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 indicies 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 the distance from this index to the given one.
  178. int distance(SlotIndex other) const {
  179. return other.getIndex() - getIndex();
  180. }
  181. /// Return the scaled distance from this index to the given one, where all
  182. /// slots on the same instruction have zero distance.
  183. int getInstrDistance(SlotIndex other) const {
  184. return (other.listEntry()->getIndex() - listEntry()->getIndex())
  185. / Slot_Count;
  186. }
  187. /// isBlock - Returns true if this is a block boundary slot.
  188. bool isBlock() const { return getSlot() == Slot_Block; }
  189. /// isEarlyClobber - Returns true if this is an early-clobber slot.
  190. bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
  191. /// isRegister - Returns true if this is a normal register use/def slot.
  192. /// Note that early-clobber slots may also be used for uses and defs.
  193. bool isRegister() const { return getSlot() == Slot_Register; }
  194. /// isDead - Returns true if this is a dead def kill slot.
  195. bool isDead() const { return getSlot() == Slot_Dead; }
  196. /// Returns the base index for associated with this index. The base index
  197. /// is the one associated with the Slot_Block slot for the instruction
  198. /// pointed to by this index.
  199. SlotIndex getBaseIndex() const {
  200. return SlotIndex(listEntry(), Slot_Block);
  201. }
  202. /// Returns the boundary index for associated with this index. The boundary
  203. /// index is the one associated with the Slot_Block slot for the instruction
  204. /// pointed to by this index.
  205. SlotIndex getBoundaryIndex() const {
  206. return SlotIndex(listEntry(), Slot_Dead);
  207. }
  208. /// Returns the register use/def slot in the current instruction for a
  209. /// normal or early-clobber def.
  210. SlotIndex getRegSlot(bool EC = false) const {
  211. return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
  212. }
  213. /// Returns the dead def kill slot for the current instruction.
  214. SlotIndex getDeadSlot() const {
  215. return SlotIndex(listEntry(), Slot_Dead);
  216. }
  217. /// Returns the next slot in the index list. This could be either the
  218. /// next slot for the instruction pointed to by this index or, if this
  219. /// index is a STORE, the first slot for the next instruction.
  220. /// WARNING: This method is considerably more expensive than the methods
  221. /// that return specific slots (getUseIndex(), etc). If you can - please
  222. /// use one of those methods.
  223. SlotIndex getNextSlot() const {
  224. Slot s = getSlot();
  225. if (s == Slot_Dead) {
  226. return SlotIndex(listEntry()->getNextNode(), Slot_Block);
  227. }
  228. return SlotIndex(listEntry(), s + 1);
  229. }
  230. /// Returns the next index. This is the index corresponding to the this
  231. /// index's slot, but for the next instruction.
  232. SlotIndex getNextIndex() const {
  233. return SlotIndex(listEntry()->getNextNode(), getSlot());
  234. }
  235. /// Returns the previous slot in the index list. This could be either the
  236. /// previous slot for the instruction pointed to by this index or, if this
  237. /// index is a Slot_Block, the last slot for the previous instruction.
  238. /// WARNING: This method is considerably more expensive than the methods
  239. /// that return specific slots (getUseIndex(), etc). If you can - please
  240. /// use one of those methods.
  241. SlotIndex getPrevSlot() const {
  242. Slot s = getSlot();
  243. if (s == Slot_Block) {
  244. return SlotIndex(listEntry()->getPrevNode(), Slot_Dead);
  245. }
  246. return SlotIndex(listEntry(), s - 1);
  247. }
  248. /// Returns the previous index. This is the index corresponding to this
  249. /// index's slot, but for the previous instruction.
  250. SlotIndex getPrevIndex() const {
  251. return SlotIndex(listEntry()->getPrevNode(), getSlot());
  252. }
  253. };
  254. template <> struct isPodLike<SlotIndex> { static const bool value = true; };
  255. inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
  256. li.print(os);
  257. return os;
  258. }
  259. typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
  260. inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
  261. return V < IM.first;
  262. }
  263. inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
  264. return IM.first < V;
  265. }
  266. struct Idx2MBBCompare {
  267. bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
  268. return LHS.first < RHS.first;
  269. }
  270. };
  271. /// SlotIndexes pass.
  272. ///
  273. /// This pass assigns indexes to each instruction.
  274. class SlotIndexes : public MachineFunctionPass {
  275. private:
  276. typedef ilist<IndexListEntry> IndexList;
  277. IndexList indexList;
  278. #ifdef EXPENSIVE_CHECKS
  279. IndexList graveyardList;
  280. #endif // EXPENSIVE_CHECKS
  281. MachineFunction *mf;
  282. typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
  283. Mi2IndexMap mi2iMap;
  284. /// MBBRanges - Map MBB number to (start, stop) indexes.
  285. SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
  286. /// Idx2MBBMap - Sorted list of pairs of index of first instruction
  287. /// and MBB id.
  288. SmallVector<IdxMBBPair, 8> idx2MBBMap;
  289. // IndexListEntry allocator.
  290. BumpPtrAllocator ileAllocator;
  291. IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
  292. IndexListEntry *entry =
  293. static_cast<IndexListEntry*>(
  294. ileAllocator.Allocate(sizeof(IndexListEntry),
  295. alignOf<IndexListEntry>()));
  296. new (entry) IndexListEntry(mi, index);
  297. return entry;
  298. }
  299. /// Renumber locally after inserting curItr.
  300. void renumberIndexes(IndexList::iterator curItr);
  301. public:
  302. static char ID;
  303. SlotIndexes() : MachineFunctionPass(ID) {
  304. initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
  305. }
  306. void getAnalysisUsage(AnalysisUsage &au) const override;
  307. void releaseMemory() override;
  308. bool runOnMachineFunction(MachineFunction &fn) override;
  309. /// Dump the indexes.
  310. void dump() const;
  311. /// Renumber the index list, providing space for new instructions.
  312. void renumberIndexes();
  313. /// Repair indexes after adding and removing instructions.
  314. void repairIndexesInRange(MachineBasicBlock *MBB,
  315. MachineBasicBlock::iterator Begin,
  316. MachineBasicBlock::iterator End);
  317. /// Returns the zero index for this analysis.
  318. SlotIndex getZeroIndex() {
  319. assert(indexList.front().getIndex() == 0 && "First index is not 0?");
  320. return SlotIndex(&indexList.front(), 0);
  321. }
  322. /// Returns the base index of the last slot in this analysis.
  323. SlotIndex getLastIndex() {
  324. return SlotIndex(&indexList.back(), 0);
  325. }
  326. /// Returns true if the given machine instr is mapped to an index,
  327. /// otherwise returns false.
  328. bool hasIndex(const MachineInstr *instr) const {
  329. return mi2iMap.count(instr);
  330. }
  331. /// Returns the base index for the given instruction.
  332. SlotIndex getInstructionIndex(const MachineInstr *MI) const {
  333. // Instructions inside a bundle have the same number as the bundle itself.
  334. Mi2IndexMap::const_iterator itr = mi2iMap.find(getBundleStart(MI));
  335. assert(itr != mi2iMap.end() && "Instruction not found in maps.");
  336. return itr->second;
  337. }
  338. /// Returns the instruction for the given index, or null if the given
  339. /// index has no instruction associated with it.
  340. MachineInstr* getInstructionFromIndex(SlotIndex index) const {
  341. return index.isValid() ? index.listEntry()->getInstr() : nullptr;
  342. }
  343. /// Returns the next non-null index, if one exists.
  344. /// Otherwise returns getLastIndex().
  345. SlotIndex getNextNonNullIndex(SlotIndex Index) {
  346. IndexList::iterator I = Index.listEntry();
  347. IndexList::iterator E = indexList.end();
  348. while (++I != E)
  349. if (I->getInstr())
  350. return SlotIndex(I, Index.getSlot());
  351. // We reached the end of the function.
  352. return getLastIndex();
  353. }
  354. /// getIndexBefore - Returns the index of the last indexed instruction
  355. /// before MI, or the start index of its basic block.
  356. /// MI is not required to have an index.
  357. SlotIndex getIndexBefore(const MachineInstr *MI) const {
  358. const MachineBasicBlock *MBB = MI->getParent();
  359. assert(MBB && "MI must be inserted inna basic block");
  360. MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
  361. for (;;) {
  362. if (I == B)
  363. return getMBBStartIdx(MBB);
  364. --I;
  365. Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
  366. if (MapItr != mi2iMap.end())
  367. return MapItr->second;
  368. }
  369. }
  370. /// getIndexAfter - Returns the index of the first indexed instruction
  371. /// after MI, or the end index of its basic block.
  372. /// MI is not required to have an index.
  373. SlotIndex getIndexAfter(const MachineInstr *MI) const {
  374. const MachineBasicBlock *MBB = MI->getParent();
  375. assert(MBB && "MI must be inserted inna basic block");
  376. MachineBasicBlock::const_iterator I = MI, E = MBB->end();
  377. for (;;) {
  378. ++I;
  379. if (I == E)
  380. return getMBBEndIdx(MBB);
  381. Mi2IndexMap::const_iterator MapItr = mi2iMap.find(I);
  382. if (MapItr != mi2iMap.end())
  383. return MapItr->second;
  384. }
  385. }
  386. /// Return the (start,end) range of the given basic block number.
  387. const std::pair<SlotIndex, SlotIndex> &
  388. getMBBRange(unsigned Num) const {
  389. return MBBRanges[Num];
  390. }
  391. /// Return the (start,end) range of the given basic block.
  392. const std::pair<SlotIndex, SlotIndex> &
  393. getMBBRange(const MachineBasicBlock *MBB) const {
  394. return getMBBRange(MBB->getNumber());
  395. }
  396. /// Returns the first index in the given basic block number.
  397. SlotIndex getMBBStartIdx(unsigned Num) const {
  398. return getMBBRange(Num).first;
  399. }
  400. /// Returns the first index in the given basic block.
  401. SlotIndex getMBBStartIdx(const MachineBasicBlock *mbb) const {
  402. return getMBBRange(mbb).first;
  403. }
  404. /// Returns the last index in the given basic block number.
  405. SlotIndex getMBBEndIdx(unsigned Num) const {
  406. return getMBBRange(Num).second;
  407. }
  408. /// Returns the last index in the given basic block.
  409. SlotIndex getMBBEndIdx(const MachineBasicBlock *mbb) const {
  410. return getMBBRange(mbb).second;
  411. }
  412. /// Returns the basic block which the given index falls in.
  413. MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
  414. if (MachineInstr *MI = getInstructionFromIndex(index))
  415. return MI->getParent();
  416. SmallVectorImpl<IdxMBBPair>::const_iterator I =
  417. std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
  418. // Take the pair containing the index
  419. SmallVectorImpl<IdxMBBPair>::const_iterator J =
  420. ((I != idx2MBBMap.end() && I->first > index) ||
  421. (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
  422. assert(J != idx2MBBMap.end() && J->first <= index &&
  423. index < getMBBEndIdx(J->second) &&
  424. "index does not correspond to an MBB");
  425. return J->second;
  426. }
  427. bool findLiveInMBBs(SlotIndex start, SlotIndex end,
  428. SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
  429. SmallVectorImpl<IdxMBBPair>::const_iterator itr =
  430. std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
  431. bool resVal = false;
  432. while (itr != idx2MBBMap.end()) {
  433. if (itr->first >= end)
  434. break;
  435. mbbs.push_back(itr->second);
  436. resVal = true;
  437. ++itr;
  438. }
  439. return resVal;
  440. }
  441. /// Returns the MBB covering the given range, or null if the range covers
  442. /// more than one basic block.
  443. MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
  444. assert(start < end && "Backwards ranges not allowed.");
  445. SmallVectorImpl<IdxMBBPair>::const_iterator itr =
  446. std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
  447. if (itr == idx2MBBMap.end()) {
  448. itr = std::prev(itr);
  449. return itr->second;
  450. }
  451. // Check that we don't cross the boundary into this block.
  452. if (itr->first < end)
  453. return nullptr;
  454. itr = std::prev(itr);
  455. if (itr->first <= start)
  456. return itr->second;
  457. return nullptr;
  458. }
  459. /// Insert the given machine instruction into the mapping. Returns the
  460. /// assigned index.
  461. /// If Late is set and there are null indexes between mi's neighboring
  462. /// instructions, create the new index after the null indexes instead of
  463. /// before them.
  464. SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late = false) {
  465. assert(!mi->isInsideBundle() &&
  466. "Instructions inside bundles should use bundle start's slot.");
  467. assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
  468. // Numbering DBG_VALUE instructions could cause code generation to be
  469. // affected by debug information.
  470. assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
  471. assert(mi->getParent() != nullptr && "Instr must be added to function.");
  472. // Get the entries where mi should be inserted.
  473. IndexList::iterator prevItr, nextItr;
  474. if (Late) {
  475. // Insert mi's index immediately before the following instruction.
  476. nextItr = getIndexAfter(mi).listEntry();
  477. prevItr = std::prev(nextItr);
  478. } else {
  479. // Insert mi's index immediately after the preceding instruction.
  480. prevItr = getIndexBefore(mi).listEntry();
  481. nextItr = std::next(prevItr);
  482. }
  483. // Get a number for the new instr, or 0 if there's no room currently.
  484. // In the latter case we'll force a renumber later.
  485. unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
  486. unsigned newNumber = prevItr->getIndex() + dist;
  487. // Insert a new list entry for mi.
  488. IndexList::iterator newItr =
  489. indexList.insert(nextItr, createEntry(mi, newNumber));
  490. // Renumber locally if we need to.
  491. if (dist == 0)
  492. renumberIndexes(newItr);
  493. SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
  494. mi2iMap.insert(std::make_pair(mi, newIndex));
  495. return newIndex;
  496. }
  497. /// Remove the given machine instruction from the mapping.
  498. void removeMachineInstrFromMaps(MachineInstr *mi) {
  499. // remove index -> MachineInstr and
  500. // MachineInstr -> index mappings
  501. Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
  502. if (mi2iItr != mi2iMap.end()) {
  503. IndexListEntry *miEntry(mi2iItr->second.listEntry());
  504. assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
  505. // FIXME: Eventually we want to actually delete these indexes.
  506. miEntry->setInstr(nullptr);
  507. mi2iMap.erase(mi2iItr);
  508. }
  509. }
  510. /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
  511. /// maps used by register allocator.
  512. void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
  513. Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
  514. if (mi2iItr == mi2iMap.end())
  515. return;
  516. SlotIndex replaceBaseIndex = mi2iItr->second;
  517. IndexListEntry *miEntry(replaceBaseIndex.listEntry());
  518. assert(miEntry->getInstr() == mi &&
  519. "Mismatched instruction in index tables.");
  520. miEntry->setInstr(newMI);
  521. mi2iMap.erase(mi2iItr);
  522. mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
  523. }
  524. /// Add the given MachineBasicBlock into the maps.
  525. void insertMBBInMaps(MachineBasicBlock *mbb) {
  526. MachineFunction::iterator nextMBB =
  527. std::next(MachineFunction::iterator(mbb));
  528. IndexListEntry *startEntry = nullptr;
  529. IndexListEntry *endEntry = nullptr;
  530. IndexList::iterator newItr;
  531. if (nextMBB == mbb->getParent()->end()) {
  532. startEntry = &indexList.back();
  533. endEntry = createEntry(nullptr, 0);
  534. newItr = indexList.insertAfter(startEntry, endEntry);
  535. } else {
  536. startEntry = createEntry(nullptr, 0);
  537. endEntry = getMBBStartIdx(nextMBB).listEntry();
  538. newItr = indexList.insert(endEntry, startEntry);
  539. }
  540. SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
  541. SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
  542. MachineFunction::iterator prevMBB(mbb);
  543. assert(prevMBB != mbb->getParent()->end() &&
  544. "Can't insert a new block at the beginning of a function.");
  545. --prevMBB;
  546. MBBRanges[prevMBB->getNumber()].second = startIdx;
  547. assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
  548. "Blocks must be added in order");
  549. MBBRanges.push_back(std::make_pair(startIdx, endIdx));
  550. idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
  551. renumberIndexes(newItr);
  552. std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
  553. }
  554. /// \brief Free the resources that were required to maintain a SlotIndex.
  555. ///
  556. /// Once an index is no longer needed (for instance because the instruction
  557. /// at that index has been moved), the resources required to maintain the
  558. /// index can be relinquished to reduce memory use and improve renumbering
  559. /// performance. Any remaining SlotIndex objects that point to the same
  560. /// index are left 'dangling' (much the same as a dangling pointer to a
  561. /// freed object) and should not be accessed, except to destruct them.
  562. ///
  563. /// Like dangling pointers, access to dangling SlotIndexes can cause
  564. /// painful-to-track-down bugs, especially if the memory for the index
  565. /// previously pointed to has been re-used. To detect dangling SlotIndex
  566. /// bugs, build with EXPENSIVE_CHECKS=1. This will cause "erased" indexes to
  567. /// be retained in a graveyard instead of being freed. Operations on indexes
  568. /// in the graveyard will trigger an assertion.
  569. void eraseIndex(SlotIndex index) {
  570. IndexListEntry *entry = index.listEntry();
  571. #ifdef EXPENSIVE_CHECKS
  572. indexList.remove(entry);
  573. graveyardList.push_back(entry);
  574. entry->setPoison();
  575. #else
  576. indexList.erase(entry);
  577. #endif
  578. }
  579. };
  580. // Specialize IntervalMapInfo for half-open slot index intervals.
  581. template <>
  582. struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
  583. };
  584. }
  585. #endif // LLVM_CODEGEN_SLOTINDEXES_H