InterferenceCache.cpp 8.5 KB

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