Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

703 lines
25 KiB

  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(0, 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() != 0 &&
  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. 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. /// isBlock - Returns true if this is a block boundary slot.
  182. bool isBlock() const { return getSlot() == Slot_Block; }
  183. /// isEarlyClobber - Returns true if this is an early-clobber slot.
  184. bool isEarlyClobber() const { return getSlot() == Slot_EarlyClobber; }
  185. /// isRegister - Returns true if this is a normal register use/def slot.
  186. /// Note that early-clobber slots may also be used for uses and defs.
  187. bool isRegister() const { return getSlot() == Slot_Register; }
  188. /// isDead - Returns true if this is a dead def kill slot.
  189. bool isDead() const { return getSlot() == Slot_Dead; }
  190. /// Returns the base index for associated with this index. The base index
  191. /// is the one associated with the Slot_Block slot for the instruction
  192. /// pointed to by this index.
  193. SlotIndex getBaseIndex() const {
  194. return SlotIndex(listEntry(), Slot_Block);
  195. }
  196. /// Returns the boundary index for associated with this index. The boundary
  197. /// index is the one associated with the Slot_Block slot for the instruction
  198. /// pointed to by this index.
  199. SlotIndex getBoundaryIndex() const {
  200. return SlotIndex(listEntry(), Slot_Dead);
  201. }
  202. /// Returns the register use/def slot in the current instruction for a
  203. /// normal or early-clobber def.
  204. SlotIndex getRegSlot(bool EC = false) const {
  205. return SlotIndex(listEntry(), EC ? Slot_EarlyClobber : Slot_Register);
  206. }
  207. /// Returns the dead def kill slot for the current instruction.
  208. SlotIndex getDeadSlot() const {
  209. return SlotIndex(listEntry(), Slot_Dead);
  210. }
  211. /// Returns the next slot in the index list. This could be either the
  212. /// next slot for the instruction pointed to by this index or, if this
  213. /// index is a STORE, the first slot for the next instruction.
  214. /// WARNING: This method is considerably more expensive than the methods
  215. /// that return specific slots (getUseIndex(), etc). If you can - please
  216. /// use one of those methods.
  217. SlotIndex getNextSlot() const {
  218. Slot s = getSlot();
  219. if (s == Slot_Dead) {
  220. return SlotIndex(listEntry()->getNextNode(), Slot_Block);
  221. }
  222. return SlotIndex(listEntry(), s + 1);
  223. }
  224. /// Returns the next index. This is the index corresponding to the this
  225. /// index's slot, but for the next instruction.
  226. SlotIndex getNextIndex() const {
  227. return SlotIndex(listEntry()->getNextNode(), getSlot());
  228. }
  229. /// Returns the previous slot in the index list. This could be either the
  230. /// previous slot for the instruction pointed to by this index or, if this
  231. /// index is a Slot_Block, the last slot for the previous instruction.
  232. /// WARNING: This method is considerably more expensive than the methods
  233. /// that return specific slots (getUseIndex(), etc). If you can - please
  234. /// use one of those methods.
  235. SlotIndex getPrevSlot() const {
  236. Slot s = getSlot();
  237. if (s == Slot_Block) {
  238. return SlotIndex(listEntry()->getPrevNode(), Slot_Dead);
  239. }
  240. return SlotIndex(listEntry(), s - 1);
  241. }
  242. /// Returns the previous index. This is the index corresponding to this
  243. /// index's slot, but for the previous instruction.
  244. SlotIndex getPrevIndex() const {
  245. return SlotIndex(listEntry()->getPrevNode(), getSlot());
  246. }
  247. };
  248. template <> struct isPodLike<SlotIndex> { static const bool value = true; };
  249. inline raw_ostream& operator<<(raw_ostream &os, SlotIndex li) {
  250. li.print(os);
  251. return os;
  252. }
  253. typedef std::pair<SlotIndex, MachineBasicBlock*> IdxMBBPair;
  254. inline bool operator<(SlotIndex V, const IdxMBBPair &IM) {
  255. return V < IM.first;
  256. }
  257. inline bool operator<(const IdxMBBPair &IM, SlotIndex V) {
  258. return IM.first < V;
  259. }
  260. struct Idx2MBBCompare {
  261. bool operator()(const IdxMBBPair &LHS, const IdxMBBPair &RHS) const {
  262. return LHS.first < RHS.first;
  263. }
  264. };
  265. /// SlotIndexes pass.
  266. ///
  267. /// This pass assigns indexes to each instruction.
  268. class SlotIndexes : public MachineFunctionPass {
  269. private:
  270. typedef ilist<IndexListEntry> IndexList;
  271. IndexList indexList;
  272. #ifdef EXPENSIVE_CHECKS
  273. IndexList graveyardList;
  274. #endif // EXPENSIVE_CHECKS
  275. MachineFunction *mf;
  276. typedef DenseMap<const MachineInstr*, SlotIndex> Mi2IndexMap;
  277. Mi2IndexMap mi2iMap;
  278. /// MBBRanges - Map MBB number to (start, stop) indexes.
  279. SmallVector<std::pair<SlotIndex, SlotIndex>, 8> MBBRanges;
  280. /// Idx2MBBMap - Sorted list of pairs of index of first instruction
  281. /// and MBB id.
  282. SmallVector<IdxMBBPair, 8> idx2MBBMap;
  283. // IndexListEntry allocator.
  284. BumpPtrAllocator ileAllocator;
  285. IndexListEntry* createEntry(MachineInstr *mi, unsigned index) {
  286. IndexListEntry *entry =
  287. static_cast<IndexListEntry*>(
  288. ileAllocator.Allocate(sizeof(IndexListEntry),
  289. alignOf<IndexListEntry>()));
  290. new (entry) IndexListEntry(mi, index);
  291. return entry;
  292. }
  293. /// Renumber locally after inserting curItr.
  294. void renumberIndexes(IndexList::iterator curItr);
  295. public:
  296. static char ID;
  297. SlotIndexes() : MachineFunctionPass(ID) {
  298. initializeSlotIndexesPass(*PassRegistry::getPassRegistry());
  299. }
  300. virtual void getAnalysisUsage(AnalysisUsage &au) const;
  301. virtual void releaseMemory();
  302. virtual bool runOnMachineFunction(MachineFunction &fn);
  303. /// Dump the indexes.
  304. void dump() const;
  305. /// Renumber the index list, providing space for new instructions.
  306. void renumberIndexes();
  307. /// Repair indexes after adding and removing instructions.
  308. void repairIndexesInRange(MachineBasicBlock *MBB,
  309. MachineBasicBlock::iterator Begin,
  310. MachineBasicBlock::iterator End);
  311. /// Returns the zero index for this analysis.
  312. SlotIndex getZeroIndex() {
  313. assert(indexList.front().getIndex() == 0 && "First index is not 0?");
  314. return SlotIndex(&indexList.front(), 0);
  315. }
  316. /// Returns the base index of the last slot in this analysis.
  317. SlotIndex getLastIndex() {
  318. return SlotIndex(&indexList.back(), 0);
  319. }
  320. /// Returns true if the given machine instr is mapped to an index,
  321. /// otherwise returns false.
  322. bool hasIndex(const MachineInstr *instr) const {
  323. return mi2iMap.count(instr);
  324. }
  325. /// Returns the base index for the given instruction.
  326. SlotIndex getInstructionIndex(const MachineInstr *MI) const {
  327. // Instructions inside a bundle have the same number as the bundle itself.
  328. Mi2IndexMap::const_iterator itr = mi2iMap.find(getBundleStart(MI));
  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() : 0;
  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();
  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 inna basic block");
  354. MachineBasicBlock::const_iterator I = MI, B = MBB->begin();
  355. for (;;) {
  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 inna basic block");
  370. MachineBasicBlock::const_iterator I = MI, E = MBB->end();
  371. for (;;) {
  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. /// Returns the basic block which the given index falls in.
  407. MachineBasicBlock* getMBBFromIndex(SlotIndex index) const {
  408. if (MachineInstr *MI = getInstructionFromIndex(index))
  409. return MI->getParent();
  410. SmallVectorImpl<IdxMBBPair>::const_iterator I =
  411. std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), index);
  412. // Take the pair containing the index
  413. SmallVectorImpl<IdxMBBPair>::const_iterator J =
  414. ((I != idx2MBBMap.end() && I->first > index) ||
  415. (I == idx2MBBMap.end() && idx2MBBMap.size()>0)) ? (I-1): I;
  416. assert(J != idx2MBBMap.end() && J->first <= index &&
  417. index < getMBBEndIdx(J->second) &&
  418. "index does not correspond to an MBB");
  419. return J->second;
  420. }
  421. bool findLiveInMBBs(SlotIndex start, SlotIndex end,
  422. SmallVectorImpl<MachineBasicBlock*> &mbbs) const {
  423. SmallVectorImpl<IdxMBBPair>::const_iterator itr =
  424. std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
  425. bool resVal = false;
  426. while (itr != idx2MBBMap.end()) {
  427. if (itr->first >= end)
  428. break;
  429. mbbs.push_back(itr->second);
  430. resVal = true;
  431. ++itr;
  432. }
  433. return resVal;
  434. }
  435. /// Returns the MBB covering the given range, or null if the range covers
  436. /// more than one basic block.
  437. MachineBasicBlock* getMBBCoveringRange(SlotIndex start, SlotIndex end) const {
  438. assert(start < end && "Backwards ranges not allowed.");
  439. SmallVectorImpl<IdxMBBPair>::const_iterator itr =
  440. std::lower_bound(idx2MBBMap.begin(), idx2MBBMap.end(), start);
  441. if (itr == idx2MBBMap.end()) {
  442. itr = prior(itr);
  443. return itr->second;
  444. }
  445. // Check that we don't cross the boundary into this block.
  446. if (itr->first < end)
  447. return 0;
  448. itr = prior(itr);
  449. if (itr->first <= start)
  450. return itr->second;
  451. return 0;
  452. }
  453. /// Insert the given machine instruction into the mapping. Returns the
  454. /// assigned index.
  455. /// If Late is set and there are null indexes between mi's neighboring
  456. /// instructions, create the new index after the null indexes instead of
  457. /// before them.
  458. SlotIndex insertMachineInstrInMaps(MachineInstr *mi, bool Late = false) {
  459. assert(!mi->isInsideBundle() &&
  460. "Instructions inside bundles should use bundle start's slot.");
  461. assert(mi2iMap.find(mi) == mi2iMap.end() && "Instr already indexed.");
  462. // Numbering DBG_VALUE instructions could cause code generation to be
  463. // affected by debug information.
  464. assert(!mi->isDebugValue() && "Cannot number DBG_VALUE instructions.");
  465. assert(mi->getParent() != 0 && "Instr must be added to function.");
  466. // Get the entries where mi should be inserted.
  467. IndexList::iterator prevItr, nextItr;
  468. if (Late) {
  469. // Insert mi's index immediately before the following instruction.
  470. nextItr = getIndexAfter(mi).listEntry();
  471. prevItr = prior(nextItr);
  472. } else {
  473. // Insert mi's index immediately after the preceding instruction.
  474. prevItr = getIndexBefore(mi).listEntry();
  475. nextItr = llvm::next(prevItr);
  476. }
  477. // Get a number for the new instr, or 0 if there's no room currently.
  478. // In the latter case we'll force a renumber later.
  479. unsigned dist = ((nextItr->getIndex() - prevItr->getIndex())/2) & ~3u;
  480. unsigned newNumber = prevItr->getIndex() + dist;
  481. // Insert a new list entry for mi.
  482. IndexList::iterator newItr =
  483. indexList.insert(nextItr, createEntry(mi, newNumber));
  484. // Renumber locally if we need to.
  485. if (dist == 0)
  486. renumberIndexes(newItr);
  487. SlotIndex newIndex(&*newItr, SlotIndex::Slot_Block);
  488. mi2iMap.insert(std::make_pair(mi, newIndex));
  489. return newIndex;
  490. }
  491. /// Remove the given machine instruction from the mapping.
  492. void removeMachineInstrFromMaps(MachineInstr *mi) {
  493. // remove index -> MachineInstr and
  494. // MachineInstr -> index mappings
  495. Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
  496. if (mi2iItr != mi2iMap.end()) {
  497. IndexListEntry *miEntry(mi2iItr->second.listEntry());
  498. assert(miEntry->getInstr() == mi && "Instruction indexes broken.");
  499. // FIXME: Eventually we want to actually delete these indexes.
  500. miEntry->setInstr(0);
  501. mi2iMap.erase(mi2iItr);
  502. }
  503. }
  504. /// ReplaceMachineInstrInMaps - Replacing a machine instr with a new one in
  505. /// maps used by register allocator.
  506. void replaceMachineInstrInMaps(MachineInstr *mi, MachineInstr *newMI) {
  507. Mi2IndexMap::iterator mi2iItr = mi2iMap.find(mi);
  508. if (mi2iItr == mi2iMap.end())
  509. return;
  510. SlotIndex replaceBaseIndex = mi2iItr->second;
  511. IndexListEntry *miEntry(replaceBaseIndex.listEntry());
  512. assert(miEntry->getInstr() == mi &&
  513. "Mismatched instruction in index tables.");
  514. miEntry->setInstr(newMI);
  515. mi2iMap.erase(mi2iItr);
  516. mi2iMap.insert(std::make_pair(newMI, replaceBaseIndex));
  517. }
  518. /// Add the given MachineBasicBlock into the maps.
  519. void insertMBBInMaps(MachineBasicBlock *mbb) {
  520. MachineFunction::iterator nextMBB =
  521. llvm::next(MachineFunction::iterator(mbb));
  522. IndexListEntry *startEntry = 0;
  523. IndexListEntry *endEntry = 0;
  524. IndexList::iterator newItr;
  525. if (nextMBB == mbb->getParent()->end()) {
  526. startEntry = &indexList.back();
  527. endEntry = createEntry(0, 0);
  528. newItr = indexList.insertAfter(startEntry, endEntry);
  529. } else {
  530. startEntry = createEntry(0, 0);
  531. endEntry = getMBBStartIdx(nextMBB).listEntry();
  532. newItr = indexList.insert(endEntry, startEntry);
  533. }
  534. SlotIndex startIdx(startEntry, SlotIndex::Slot_Block);
  535. SlotIndex endIdx(endEntry, SlotIndex::Slot_Block);
  536. MachineFunction::iterator prevMBB(mbb);
  537. assert(prevMBB != mbb->getParent()->end() &&
  538. "Can't insert a new block at the beginning of a function.");
  539. --prevMBB;
  540. MBBRanges[prevMBB->getNumber()].second = startIdx;
  541. assert(unsigned(mbb->getNumber()) == MBBRanges.size() &&
  542. "Blocks must be added in order");
  543. MBBRanges.push_back(std::make_pair(startIdx, endIdx));
  544. idx2MBBMap.push_back(IdxMBBPair(startIdx, mbb));
  545. renumberIndexes(newItr);
  546. std::sort(idx2MBBMap.begin(), idx2MBBMap.end(), Idx2MBBCompare());
  547. }
  548. /// \brief Free the resources that were required to maintain a SlotIndex.
  549. ///
  550. /// Once an index is no longer needed (for instance because the instruction
  551. /// at that index has been moved), the resources required to maintain the
  552. /// index can be relinquished to reduce memory use and improve renumbering
  553. /// performance. Any remaining SlotIndex objects that point to the same
  554. /// index are left 'dangling' (much the same as a dangling pointer to a
  555. /// freed object) and should not be accessed, except to destruct them.
  556. ///
  557. /// Like dangling pointers, access to dangling SlotIndexes can cause
  558. /// painful-to-track-down bugs, especially if the memory for the index
  559. /// previously pointed to has been re-used. To detect dangling SlotIndex
  560. /// bugs, build with EXPENSIVE_CHECKS=1. This will cause "erased" indexes to
  561. /// be retained in a graveyard instead of being freed. Operations on indexes
  562. /// in the graveyard will trigger an assertion.
  563. void eraseIndex(SlotIndex index) {
  564. IndexListEntry *entry = index.listEntry();
  565. #ifdef EXPENSIVE_CHECKS
  566. indexList.remove(entry);
  567. graveyardList.push_back(entry);
  568. entry->setPoison();
  569. #else
  570. indexList.erase(entry);
  571. #endif
  572. }
  573. };
  574. // Specialize IntervalMapInfo for half-open slot index intervals.
  575. template <>
  576. struct IntervalMapInfo<SlotIndex> : IntervalMapHalfOpenInfo<SlotIndex> {
  577. };
  578. }
  579. #endif // LLVM_CODEGEN_SLOTINDEXES_H