InterferenceCache.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //===-- InterferenceCache.cpp - Caching per-block interference ---------*--===//
  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. // InterferenceCache remembers per-block interference in LiveIntervalUnions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "InterferenceCache.h"
  14. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  15. #include "llvm/Support/ErrorHandling.h"
  16. #include "llvm/Target/TargetRegisterInfo.h"
  17. using namespace llvm;
  18. #define DEBUG_TYPE "regalloc"
  19. // Static member used for null interference cursors.
  20. const InterferenceCache::BlockInterference
  21. InterferenceCache::Cursor::NoInterference;
  22. // Initializes PhysRegEntries (instead of a SmallVector, PhysRegEntries is a
  23. // buffer of size NumPhysRegs to speed up alloc/clear for targets with large
  24. // reg files). Calloced memory is used for good form, and quites tools like
  25. // Valgrind too, but zero initialized memory is not required by the algorithm:
  26. // this is because PhysRegEntries works like a SparseSet and its entries are
  27. // only valid when there is a corresponding CacheEntries assignment. There is
  28. // also support for when pass managers are reused for targets with different
  29. // numbers of PhysRegs: in this case PhysRegEntries is freed and reinitialized.
  30. void InterferenceCache::reinitPhysRegEntries() {
  31. if (PhysRegEntriesCount == TRI->getNumRegs()) return;
  32. free(PhysRegEntries);
  33. PhysRegEntriesCount = TRI->getNumRegs();
  34. PhysRegEntries = (unsigned char*)
  35. calloc(PhysRegEntriesCount, sizeof(unsigned char));
  36. }
  37. void InterferenceCache::init(MachineFunction *mf,
  38. LiveIntervalUnion *liuarray,
  39. SlotIndexes *indexes,
  40. LiveIntervals *lis,
  41. const TargetRegisterInfo *tri) {
  42. MF = mf;
  43. LIUArray = liuarray;
  44. TRI = tri;
  45. reinitPhysRegEntries();
  46. for (unsigned i = 0; i != CacheEntries; ++i)
  47. Entries[i].clear(mf, indexes, lis);
  48. }
  49. InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
  50. unsigned E = PhysRegEntries[PhysReg];
  51. if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
  52. if (!Entries[E].valid(LIUArray, TRI))
  53. Entries[E].revalidate(LIUArray, TRI);
  54. return &Entries[E];
  55. }
  56. // No valid entry exists, pick the next round-robin entry.
  57. E = RoundRobin;
  58. if (++RoundRobin == CacheEntries)
  59. RoundRobin = 0;
  60. for (unsigned i = 0; i != CacheEntries; ++i) {
  61. // Skip entries that are in use.
  62. if (Entries[E].hasRefs()) {
  63. if (++E == CacheEntries)
  64. E = 0;
  65. continue;
  66. }
  67. Entries[E].reset(PhysReg, LIUArray, TRI, MF);
  68. PhysRegEntries[PhysReg] = E;
  69. return &Entries[E];
  70. }
  71. llvm_unreachable("Ran out of interference cache entries.");
  72. }
  73. /// revalidate - LIU contents have changed, update tags.
  74. void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray,
  75. const TargetRegisterInfo *TRI) {
  76. // Invalidate all block entries.
  77. ++Tag;
  78. // Invalidate all iterators.
  79. PrevPos = SlotIndex();
  80. unsigned i = 0;
  81. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i)
  82. RegUnits[i].VirtTag = LIUArray[*Units].getTag();
  83. }
  84. void InterferenceCache::Entry::reset(unsigned physReg,
  85. LiveIntervalUnion *LIUArray,
  86. const TargetRegisterInfo *TRI,
  87. const MachineFunction *MF) {
  88. assert(!hasRefs() && "Cannot reset cache entry with references");
  89. // LIU's changed, invalidate cache.
  90. ++Tag;
  91. PhysReg = physReg;
  92. Blocks.resize(MF->getNumBlockIDs());
  93. // Reset iterators.
  94. PrevPos = SlotIndex();
  95. RegUnits.clear();
  96. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  97. RegUnits.push_back(LIUArray[*Units]);
  98. RegUnits.back().Fixed = &LIS->getRegUnit(*Units);
  99. }
  100. }
  101. bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
  102. const TargetRegisterInfo *TRI) {
  103. unsigned i = 0, e = RegUnits.size();
  104. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) {
  105. if (i == e)
  106. return false;
  107. if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag))
  108. return false;
  109. }
  110. return i == e;
  111. }
  112. void InterferenceCache::Entry::update(unsigned MBBNum) {
  113. SlotIndex Start, Stop;
  114. std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
  115. // Use advanceTo only when possible.
  116. if (PrevPos != Start) {
  117. if (!PrevPos.isValid() || Start < PrevPos) {
  118. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  119. RegUnitInfo &RUI = RegUnits[i];
  120. RUI.VirtI.find(Start);
  121. RUI.FixedI = RUI.Fixed->find(Start);
  122. }
  123. } else {
  124. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  125. RegUnitInfo &RUI = RegUnits[i];
  126. RUI.VirtI.advanceTo(Start);
  127. if (RUI.FixedI != RUI.Fixed->end())
  128. RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start);
  129. }
  130. }
  131. PrevPos = Start;
  132. }
  133. MachineFunction::const_iterator MFI = MF->getBlockNumbered(MBBNum);
  134. BlockInterference *BI = &Blocks[MBBNum];
  135. ArrayRef<SlotIndex> RegMaskSlots;
  136. ArrayRef<const uint32_t*> RegMaskBits;
  137. for (;;) {
  138. BI->Tag = Tag;
  139. BI->First = BI->Last = SlotIndex();
  140. // Check for first interference from virtregs.
  141. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  142. LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
  143. if (!I.valid())
  144. continue;
  145. SlotIndex StartI = I.start();
  146. if (StartI >= Stop)
  147. continue;
  148. if (!BI->First.isValid() || StartI < BI->First)
  149. BI->First = StartI;
  150. }
  151. // Same thing for fixed interference.
  152. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  153. LiveInterval::const_iterator I = RegUnits[i].FixedI;
  154. LiveInterval::const_iterator E = RegUnits[i].Fixed->end();
  155. if (I == E)
  156. continue;
  157. SlotIndex StartI = I->start;
  158. if (StartI >= Stop)
  159. continue;
  160. if (!BI->First.isValid() || StartI < BI->First)
  161. BI->First = StartI;
  162. }
  163. // Also check for register mask interference.
  164. RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum);
  165. RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum);
  166. SlotIndex Limit = BI->First.isValid() ? BI->First : Stop;
  167. for (unsigned i = 0, e = RegMaskSlots.size();
  168. i != e && RegMaskSlots[i] < Limit; ++i)
  169. if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) {
  170. // Register mask i clobbers PhysReg before the LIU interference.
  171. BI->First = RegMaskSlots[i];
  172. break;
  173. }
  174. PrevPos = Stop;
  175. if (BI->First.isValid())
  176. break;
  177. // No interference in this block? Go ahead and precompute the next block.
  178. if (++MFI == MF->end())
  179. return;
  180. MBBNum = MFI->getNumber();
  181. BI = &Blocks[MBBNum];
  182. if (BI->Tag == Tag)
  183. return;
  184. std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
  185. }
  186. // Check for last interference in block.
  187. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  188. LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
  189. if (!I.valid() || I.start() >= Stop)
  190. continue;
  191. I.advanceTo(Stop);
  192. bool Backup = !I.valid() || I.start() >= Stop;
  193. if (Backup)
  194. --I;
  195. SlotIndex StopI = I.stop();
  196. if (!BI->Last.isValid() || StopI > BI->Last)
  197. BI->Last = StopI;
  198. if (Backup)
  199. ++I;
  200. }
  201. // Fixed interference.
  202. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  203. LiveInterval::iterator &I = RegUnits[i].FixedI;
  204. LiveRange *LR = RegUnits[i].Fixed;
  205. if (I == LR->end() || I->start >= Stop)
  206. continue;
  207. I = LR->advanceTo(I, Stop);
  208. bool Backup = I == LR->end() || I->start >= Stop;
  209. if (Backup)
  210. --I;
  211. SlotIndex StopI = I->end;
  212. if (!BI->Last.isValid() || StopI > BI->Last)
  213. BI->Last = StopI;
  214. if (Backup)
  215. ++I;
  216. }
  217. // Also check for register mask interference.
  218. SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start;
  219. for (unsigned i = RegMaskSlots.size();
  220. i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i)
  221. if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) {
  222. // Register mask i-1 clobbers PhysReg after the LIU interference.
  223. // Model the regmask clobber as a dead def.
  224. BI->Last = RegMaskSlots[i-1].getDeadSlot();
  225. break;
  226. }
  227. }