2
0

InterferenceCache.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. delete[] PhysRegEntries; // HLSL Change: Use overridable operator delete
  33. PhysRegEntriesCount = TRI->getNumRegs();
  34. // HLSL Change Begin: Use overridable operator new
  35. PhysRegEntries = new unsigned char[PhysRegEntriesCount];
  36. std::memset(PhysRegEntries, 0, PhysRegEntriesCount);
  37. // HLSL Change End
  38. }
  39. void InterferenceCache::init(MachineFunction *mf,
  40. LiveIntervalUnion *liuarray,
  41. SlotIndexes *indexes,
  42. LiveIntervals *lis,
  43. const TargetRegisterInfo *tri) {
  44. MF = mf;
  45. LIUArray = liuarray;
  46. TRI = tri;
  47. reinitPhysRegEntries();
  48. for (unsigned i = 0; i != CacheEntries; ++i)
  49. Entries[i].clear(mf, indexes, lis);
  50. }
  51. InterferenceCache::Entry *InterferenceCache::get(unsigned PhysReg) {
  52. unsigned E = PhysRegEntries[PhysReg];
  53. if (E < CacheEntries && Entries[E].getPhysReg() == PhysReg) {
  54. if (!Entries[E].valid(LIUArray, TRI))
  55. Entries[E].revalidate(LIUArray, TRI);
  56. return &Entries[E];
  57. }
  58. // No valid entry exists, pick the next round-robin entry.
  59. E = RoundRobin;
  60. if (++RoundRobin == CacheEntries)
  61. RoundRobin = 0;
  62. for (unsigned i = 0; i != CacheEntries; ++i) {
  63. // Skip entries that are in use.
  64. if (Entries[E].hasRefs()) {
  65. if (++E == CacheEntries)
  66. E = 0;
  67. continue;
  68. }
  69. Entries[E].reset(PhysReg, LIUArray, TRI, MF);
  70. PhysRegEntries[PhysReg] = E;
  71. return &Entries[E];
  72. }
  73. llvm_unreachable("Ran out of interference cache entries.");
  74. }
  75. /// revalidate - LIU contents have changed, update tags.
  76. void InterferenceCache::Entry::revalidate(LiveIntervalUnion *LIUArray,
  77. const TargetRegisterInfo *TRI) {
  78. // Invalidate all block entries.
  79. ++Tag;
  80. // Invalidate all iterators.
  81. PrevPos = SlotIndex();
  82. unsigned i = 0;
  83. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i)
  84. RegUnits[i].VirtTag = LIUArray[*Units].getTag();
  85. }
  86. void InterferenceCache::Entry::reset(unsigned physReg,
  87. LiveIntervalUnion *LIUArray,
  88. const TargetRegisterInfo *TRI,
  89. const MachineFunction *MF) {
  90. assert(!hasRefs() && "Cannot reset cache entry with references");
  91. // LIU's changed, invalidate cache.
  92. ++Tag;
  93. PhysReg = physReg;
  94. Blocks.resize(MF->getNumBlockIDs());
  95. // Reset iterators.
  96. PrevPos = SlotIndex();
  97. RegUnits.clear();
  98. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  99. RegUnits.push_back(LIUArray[*Units]);
  100. RegUnits.back().Fixed = &LIS->getRegUnit(*Units);
  101. }
  102. }
  103. bool InterferenceCache::Entry::valid(LiveIntervalUnion *LIUArray,
  104. const TargetRegisterInfo *TRI) {
  105. unsigned i = 0, e = RegUnits.size();
  106. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units, ++i) {
  107. if (i == e)
  108. return false;
  109. if (LIUArray[*Units].changedSince(RegUnits[i].VirtTag))
  110. return false;
  111. }
  112. return i == e;
  113. }
  114. void InterferenceCache::Entry::update(unsigned MBBNum) {
  115. SlotIndex Start, Stop;
  116. std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
  117. // Use advanceTo only when possible.
  118. if (PrevPos != Start) {
  119. if (!PrevPos.isValid() || Start < PrevPos) {
  120. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  121. RegUnitInfo &RUI = RegUnits[i];
  122. RUI.VirtI.find(Start);
  123. RUI.FixedI = RUI.Fixed->find(Start);
  124. }
  125. } else {
  126. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  127. RegUnitInfo &RUI = RegUnits[i];
  128. RUI.VirtI.advanceTo(Start);
  129. if (RUI.FixedI != RUI.Fixed->end())
  130. RUI.FixedI = RUI.Fixed->advanceTo(RUI.FixedI, Start);
  131. }
  132. }
  133. PrevPos = Start;
  134. }
  135. MachineFunction::const_iterator MFI = MF->getBlockNumbered(MBBNum);
  136. BlockInterference *BI = &Blocks[MBBNum];
  137. ArrayRef<SlotIndex> RegMaskSlots;
  138. ArrayRef<const uint32_t*> RegMaskBits;
  139. for (;;) {
  140. BI->Tag = Tag;
  141. BI->First = BI->Last = SlotIndex();
  142. // Check for first interference from virtregs.
  143. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  144. LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
  145. if (!I.valid())
  146. continue;
  147. SlotIndex StartI = I.start();
  148. if (StartI >= Stop)
  149. continue;
  150. if (!BI->First.isValid() || StartI < BI->First)
  151. BI->First = StartI;
  152. }
  153. // Same thing for fixed interference.
  154. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  155. LiveInterval::const_iterator I = RegUnits[i].FixedI;
  156. LiveInterval::const_iterator E = RegUnits[i].Fixed->end();
  157. if (I == E)
  158. continue;
  159. SlotIndex StartI = I->start;
  160. if (StartI >= Stop)
  161. continue;
  162. if (!BI->First.isValid() || StartI < BI->First)
  163. BI->First = StartI;
  164. }
  165. // Also check for register mask interference.
  166. RegMaskSlots = LIS->getRegMaskSlotsInBlock(MBBNum);
  167. RegMaskBits = LIS->getRegMaskBitsInBlock(MBBNum);
  168. SlotIndex Limit = BI->First.isValid() ? BI->First : Stop;
  169. for (unsigned i = 0, e = RegMaskSlots.size();
  170. i != e && RegMaskSlots[i] < Limit; ++i)
  171. if (MachineOperand::clobbersPhysReg(RegMaskBits[i], PhysReg)) {
  172. // Register mask i clobbers PhysReg before the LIU interference.
  173. BI->First = RegMaskSlots[i];
  174. break;
  175. }
  176. PrevPos = Stop;
  177. if (BI->First.isValid())
  178. break;
  179. // No interference in this block? Go ahead and precompute the next block.
  180. if (++MFI == MF->end())
  181. return;
  182. MBBNum = MFI->getNumber();
  183. BI = &Blocks[MBBNum];
  184. if (BI->Tag == Tag)
  185. return;
  186. std::tie(Start, Stop) = Indexes->getMBBRange(MBBNum);
  187. }
  188. // Check for last interference in block.
  189. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  190. LiveIntervalUnion::SegmentIter &I = RegUnits[i].VirtI;
  191. if (!I.valid() || I.start() >= Stop)
  192. continue;
  193. I.advanceTo(Stop);
  194. bool Backup = !I.valid() || I.start() >= Stop;
  195. if (Backup)
  196. --I;
  197. SlotIndex StopI = I.stop();
  198. if (!BI->Last.isValid() || StopI > BI->Last)
  199. BI->Last = StopI;
  200. if (Backup)
  201. ++I;
  202. }
  203. // Fixed interference.
  204. for (unsigned i = 0, e = RegUnits.size(); i != e; ++i) {
  205. LiveInterval::iterator &I = RegUnits[i].FixedI;
  206. LiveRange *LR = RegUnits[i].Fixed;
  207. if (I == LR->end() || I->start >= Stop)
  208. continue;
  209. I = LR->advanceTo(I, Stop);
  210. bool Backup = I == LR->end() || I->start >= Stop;
  211. if (Backup)
  212. --I;
  213. SlotIndex StopI = I->end;
  214. if (!BI->Last.isValid() || StopI > BI->Last)
  215. BI->Last = StopI;
  216. if (Backup)
  217. ++I;
  218. }
  219. // Also check for register mask interference.
  220. SlotIndex Limit = BI->Last.isValid() ? BI->Last : Start;
  221. for (unsigned i = RegMaskSlots.size();
  222. i && RegMaskSlots[i-1].getDeadSlot() > Limit; --i)
  223. if (MachineOperand::clobbersPhysReg(RegMaskBits[i-1], PhysReg)) {
  224. // Register mask i-1 clobbers PhysReg after the LIU interference.
  225. // Model the regmask clobber as a dead def.
  226. BI->Last = RegMaskSlots[i-1].getDeadSlot();
  227. break;
  228. }
  229. }