RegAllocBase.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "Spiller.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/CodeGen/LiveInterval.h"
  18. #include "llvm/CodeGen/LiveIntervals.h"
  19. #include "llvm/CodeGen/LiveRegMatrix.h"
  20. #include "llvm/CodeGen/MachineInstr.h"
  21. #include "llvm/CodeGen/MachineModuleInfo.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.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,
  49. LiveIntervals &lis,
  50. LiveRegMatrix &mat) {
  51. TRI = &vrm.getTargetRegInfo();
  52. MRI = &vrm.getRegInfo();
  53. VRM = &vrm;
  54. LIS = &lis;
  55. Matrix = &mat;
  56. MRI->freezeReservedRegs(vrm.getMachineFunction());
  57. RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
  58. }
  59. // Visit all the live registers. If they are already assigned to a physical
  60. // register, unify them with the corresponding LiveIntervalUnion, otherwise push
  61. // them on the priority queue for later assignment.
  62. void RegAllocBase::seedLiveRegs() {
  63. NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName,
  64. TimerGroupDescription, TimePassesIsEnabled);
  65. for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
  66. unsigned Reg = Register::index2VirtReg(i);
  67. if (MRI->reg_nodbg_empty(Reg))
  68. continue;
  69. enqueue(&LIS->getInterval(Reg));
  70. }
  71. }
  72. // Top-level driver to manage the queue of unassigned VirtRegs and call the
  73. // selectOrSplit implementation.
  74. void RegAllocBase::allocatePhysRegs() {
  75. seedLiveRegs();
  76. // Continue assigning vregs one at a time to available physical registers.
  77. while (LiveInterval *VirtReg = dequeue()) {
  78. assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
  79. // Unused registers can appear when the spiller coalesces snippets.
  80. if (MRI->reg_nodbg_empty(VirtReg->reg)) {
  81. LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
  82. aboutToRemoveInterval(*VirtReg);
  83. LIS->removeInterval(VirtReg->reg);
  84. continue;
  85. }
  86. // Invalidate all interference queries, live ranges could have changed.
  87. Matrix->invalidateVirtRegs();
  88. // selectOrSplit requests the allocator to return an available physical
  89. // register if possible and populate a list of new live intervals that
  90. // result from splitting.
  91. LLVM_DEBUG(dbgs() << "\nselectOrSplit "
  92. << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg))
  93. << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
  94. using VirtRegVec = SmallVector<unsigned, 4>;
  95. VirtRegVec SplitVRegs;
  96. unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
  97. if (AvailablePhysReg == ~0u) {
  98. // selectOrSplit failed to find a register!
  99. // Probably caused by an inline asm.
  100. MachineInstr *MI = nullptr;
  101. for (MachineRegisterInfo::reg_instr_iterator
  102. I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
  103. I != E; ) {
  104. MI = &*(I++);
  105. if (MI->isInlineAsm())
  106. break;
  107. }
  108. if (MI && MI->isInlineAsm()) {
  109. MI->emitError("inline assembly requires more registers than available");
  110. } else if (MI) {
  111. LLVMContext &Context =
  112. MI->getParent()->getParent()->getMMI().getModule()->getContext();
  113. Context.emitError("ran out of registers during register allocation");
  114. } else {
  115. report_fatal_error("ran out of registers during register allocation");
  116. }
  117. // Keep going after reporting the error.
  118. VRM->assignVirt2Phys(VirtReg->reg,
  119. RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
  120. continue;
  121. }
  122. if (AvailablePhysReg)
  123. Matrix->assign(*VirtReg, AvailablePhysReg);
  124. for (unsigned Reg : SplitVRegs) {
  125. assert(LIS->hasInterval(Reg));
  126. LiveInterval *SplitVirtReg = &LIS->getInterval(Reg);
  127. assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
  128. if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
  129. assert(SplitVirtReg->empty() && "Non-empty but used interval");
  130. LLVM_DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
  131. aboutToRemoveInterval(*SplitVirtReg);
  132. LIS->removeInterval(SplitVirtReg->reg);
  133. continue;
  134. }
  135. LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
  136. assert(Register::isVirtualRegister(SplitVirtReg->reg) &&
  137. "expect split value in virtual register");
  138. enqueue(SplitVirtReg);
  139. ++NumNewQueued;
  140. }
  141. }
  142. }
  143. void RegAllocBase::postOptimization() {
  144. spiller().postOptimization();
  145. for (auto DeadInst : DeadRemats) {
  146. LIS->RemoveMachineInstrFromMaps(*DeadInst);
  147. DeadInst->eraseFromParent();
  148. }
  149. DeadRemats.clear();
  150. }