InterferenceCache.cpp 8.7 KB

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