RegAllocBase.cpp 5.9 KB

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