InterferenceCache.cpp 8.8 KB

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