RegAllocBase.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/Target/TargetRegisterInfo.h"
  24. #ifndef NDEBUG
  25. #include "llvm/ADT/SparseBitVector.h"
  26. #endif
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include "llvm/Support/Timer.h"
  33. using namespace llvm;
  34. #define DEBUG_TYPE "regalloc"
  35. STATISTIC(NumNewQueued , "Number of new live ranges queued");
  36. // Temporary verification option until we can put verification inside
  37. // MachineVerifier.
  38. static cl::opt<bool, true>
  39. VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
  40. cl::desc("Verify during register allocation"));
  41. const char RegAllocBase::TimerGroupName[] = "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 Live Regs", TimerGroupName, TimePassesIsEnabled);
  64. for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
  65. unsigned Reg = TargetRegisterInfo::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. 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. DEBUG(dbgs() << "\nselectOrSplit "
  91. << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg))
  92. << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
  93. typedef SmallVector<unsigned, 4> VirtRegVec;
  94. VirtRegVec SplitVRegs;
  95. unsigned 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), E = MRI->reg_instr_end();
  102. I != E; ) {
  103. MachineInstr *TmpMI = &*(I++);
  104. if (TmpMI->isInlineAsm()) {
  105. MI = TmpMI;
  106. break;
  107. }
  108. }
  109. if (MI)
  110. MI->emitError("inline assembly requires more registers than available");
  111. else
  112. report_fatal_error("ran out of registers during register allocation");
  113. // Keep going after reporting the error.
  114. VRM->assignVirt2Phys(VirtReg->reg,
  115. RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
  116. continue;
  117. }
  118. if (AvailablePhysReg)
  119. Matrix->assign(*VirtReg, AvailablePhysReg);
  120. for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
  121. I != E; ++I) {
  122. LiveInterval *SplitVirtReg = &LIS->getInterval(*I);
  123. assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
  124. if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
  125. DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
  126. aboutToRemoveInterval(*SplitVirtReg);
  127. LIS->removeInterval(SplitVirtReg->reg);
  128. continue;
  129. }
  130. DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
  131. assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
  132. "expect split value in virtual register");
  133. enqueue(SplitVirtReg);
  134. ++NumNewQueued;
  135. }
  136. }
  137. }