InterferenceCache.cpp 8.8 KB

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