RegAllocBase.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //===- RegAllocBase.cpp - Register Allocator Base Class -------------------===//
  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. // This file defines the RegAllocBase class which provides common functionality
  10. // for LiveIntervalUnion-based register allocators.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "RegAllocBase.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/CodeGen/LiveInterval.h"
  17. #include "llvm/CodeGen/LiveIntervals.h"
  18. #include "llvm/CodeGen/LiveRegMatrix.h"
  19. #include "llvm/CodeGen/MachineInstr.h"
  20. #include "llvm/CodeGen/MachineModuleInfo.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/CodeGen/Spiller.h"
  23. #include "llvm/CodeGen/TargetRegisterInfo.h"
  24. #include "llvm/CodeGen/VirtRegMap.h"
  25. #include "llvm/Pass.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/Debug.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Support/Timer.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include <cassert>
  32. using namespace llvm;
  33. #define DEBUG_TYPE "regalloc"
  34. STATISTIC(NumNewQueued, "Number of new live ranges queued");
  35. // Temporary verification option until we can put verification inside
  36. // MachineVerifier.
  37. static cl::opt<bool, true>
  38. VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
  39. cl::Hidden, cl::desc("Verify during register allocation"));
  40. const char RegAllocBase::TimerGroupName[] = "regalloc";
  41. const char RegAllocBase::TimerGroupDescription[] = "Register Allocation";
  42. bool RegAllocBase::VerifyEnabled = false;
  43. //===----------------------------------------------------------------------===//
  44. // RegAllocBase Implementation
  45. //===----------------------------------------------------------------------===//
  46. // Pin the vtable to this file.
  47. void RegAllocBase::anchor() {}
  48. void RegAllocBase::init(VirtRegMap &vrm, LiveIntervals &lis,
  49. LiveRegMatrix &mat) {
  50. TRI = &vrm.getTargetRegInfo();
  51. MRI = &vrm.getRegInfo();
  52. VRM = &vrm;
  53. LIS = &lis;
  54. Matrix = &mat;
  55. MRI->freezeReservedRegs(vrm.getMachineFunction());
  56. RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
  57. }
  58. // Visit all the live registers. If they are already assigned to a physical
  59. // register, unify them with the corresponding LiveIntervalUnion, otherwise push
  60. // them on the priority queue for later assignment.
  61. void RegAllocBase::seedLiveRegs() {
  62. NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
  63. TimerGroupDescription, TimePassesIsEnabled);
  64. for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
  65. Register Reg = Register::index2VirtReg(i);
  66. if (MRI->reg_nodbg_empty(Reg))
  67. continue;
  68. enqueue(&LIS->getInterval(Reg));
  69. }
  70. }
  71. // Top-level driver to manage the queue of unassigned VirtRegs and call the
  72. // selectOrSplit implementation.
  73. void RegAllocBase::allocatePhysRegs() {
  74. seedLiveRegs();
  75. // Continue assigning vregs one at a time to available physical registers.
  76. while (LiveInterval *VirtReg = dequeue()) {
  77. assert(!VRM->hasPhys(VirtReg->reg()) && "Register already assigned");
  78. // Unused registers can appear when the spiller coalesces snippets.
  79. if (MRI->reg_nodbg_empty(VirtReg->reg())) {
  80. LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
  81. aboutToRemoveInterval(*VirtReg);
  82. LIS->removeInterval(VirtReg->reg());
  83. continue;
  84. }
  85. // Invalidate all interference queries, live ranges could have changed.
  86. Matrix->invalidateVirtRegs();
  87. // selectOrSplit requests the allocator to return an available physical
  88. // register if possible and populate a list of new live intervals that
  89. // result from splitting.
  90. LLVM_DEBUG(dbgs() << "\nselectOrSplit "
  91. << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg()))
  92. << ':' << *VirtReg << " w=" << VirtReg->weight() << '\n');
  93. using VirtRegVec = SmallVector<Register, 4>;
  94. VirtRegVec SplitVRegs;
  95. MCRegister AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
  96. if (AvailablePhysReg == ~0u) {
  97. // selectOrSplit failed to find a register!
  98. // Probably caused by an inline asm.
  99. MachineInstr *MI = nullptr;
  100. for (MachineRegisterInfo::reg_instr_iterator
  101. I = MRI->reg_instr_begin(VirtReg->reg()),
  102. E = MRI->reg_instr_end();
  103. I != E;) {
  104. MI = &*(I++);
  105. if (MI->isInlineAsm())
  106. break;
  107. }
  108. const TargetRegisterClass *RC = MRI->getRegClass(VirtReg->reg());
  109. ArrayRef<MCPhysReg> AllocOrder = RegClassInfo.getOrder(RC);
  110. if (AllocOrder.empty())
  111. report_fatal_error("no registers from class available to allocate");
  112. else if (MI && MI->isInlineAsm()) {
  113. MI->emitError("inline assembly requires more registers than available");
  114. } else if (MI) {
  115. LLVMContext &Context =
  116. MI->getParent()->getParent()->getMMI().getModule()->getContext();
  117. Context.emitError("ran out of registers during register allocation");
  118. } else {
  119. report_fatal_error("ran out of registers during register allocation");
  120. }
  121. // Keep going after reporting the error.
  122. VRM->assignVirt2Phys(VirtReg->reg(), AllocOrder.front());
  123. continue;
  124. }
  125. if (AvailablePhysReg)
  126. Matrix->assign(*VirtReg, AvailablePhysReg);
  127. for (Register Reg : SplitVRegs) {
  128. assert(LIS->hasInterval(Reg));
  129. LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
  130. assert(!VRM->hasPhys(SplitVirtReg->reg()) && "Register already assigned");
  131. if (MRI->reg_nodbg_empty(SplitVirtReg->reg())) {
  132. assert(SplitVirtReg->empty() && "Non-empty but used interval");
  133. LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
  134. aboutToRemoveInterval(*SplitVirtReg);
  135. LIS->removeInterval(SplitVirtReg->reg());
  136. continue;
  137. }
  138. LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
  139. assert(Register::isVirtualRegister(SplitVirtReg->reg()) &&
  140. "expect split value in virtual register");
  141. enqueue(SplitVirtReg);
  142. ++NumNewQueued;
  143. }
  144. }
  145. }
  146. void RegAllocBase::postOptimization() {
  147. spiller().postOptimization();
  148. for (auto DeadInst : DeadRemats) {
  149. LIS->RemoveMachineInstrFromMaps(*DeadInst);
  150. DeadInst->eraseFromParent();
  151. }
  152. DeadRemats.clear();
  153. }
  154. void RegAllocBase::enqueue(LiveInterval *LI) {
  155. const Register Reg = LI->reg();
  156. assert(Reg.isVirtual() && "Can only enqueue virtual registers");
  157. if (VRM->hasPhys(Reg))
  158. return;
  159. const TargetRegisterClass &RC = *MRI->getRegClass(Reg);
  160. if (ShouldAllocateClass(*TRI, RC)) {
  161. LLVM_DEBUG(dbgs() << "Enqueuing " << printReg(Reg, TRI) << '\n');
  162. enqueueImpl(LI);
  163. } else {
  164. LLVM_DEBUG(dbgs() << "Not enqueueing " << printReg(Reg, TRI)
  165. << " in skipped register class\n");
  166. }
  167. }