RegAllocBase.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #define DEBUG_TYPE "regalloc"
  15. #include "RegAllocBase.h"
  16. #include "Spiller.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/CodeGen/LiveIntervalAnalysis.h"
  19. #include "llvm/CodeGen/LiveRangeEdit.h"
  20. #include "llvm/CodeGen/LiveRegMatrix.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/CodeGen/VirtRegMap.h"
  24. #include "llvm/Target/TargetMachine.h"
  25. #include "llvm/Target/TargetRegisterInfo.h"
  26. #ifndef NDEBUG
  27. #include "llvm/ADT/SparseBitVector.h"
  28. #endif
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/raw_ostream.h"
  33. #include "llvm/Support/Timer.h"
  34. using namespace llvm;
  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. LIS->removeInterval(VirtReg->reg);
  82. continue;
  83. }
  84. // Invalidate all interference queries, live ranges could have changed.
  85. Matrix->invalidateVirtRegs();
  86. // selectOrSplit requests the allocator to return an available physical
  87. // register if possible and populate a list of new live intervals that
  88. // result from splitting.
  89. DEBUG(dbgs() << "\nselectOrSplit "
  90. << MRI->getRegClass(VirtReg->reg)->getName()
  91. << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
  92. typedef SmallVector<unsigned, 4> VirtRegVec;
  93. VirtRegVec SplitVRegs;
  94. unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
  95. if (AvailablePhysReg == ~0u) {
  96. // selectOrSplit failed to find a register!
  97. // Probably caused by an inline asm.
  98. MachineInstr *MI;
  99. for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(VirtReg->reg);
  100. (MI = I.skipInstruction());)
  101. if (MI->isInlineAsm())
  102. break;
  103. if (MI)
  104. MI->emitError("inline assembly requires more registers than available");
  105. else
  106. report_fatal_error("ran out of registers during register allocation");
  107. // Keep going after reporting the error.
  108. VRM->assignVirt2Phys(VirtReg->reg,
  109. RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
  110. continue;
  111. }
  112. if (AvailablePhysReg)
  113. Matrix->assign(*VirtReg, AvailablePhysReg);
  114. for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
  115. I != E; ++I) {
  116. LiveInterval *SplitVirtReg = &LIS->getInterval(*I);
  117. assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
  118. if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
  119. DEBUG(dbgs() << "not queueing unused " << *SplitVirtReg << '\n');
  120. LIS->removeInterval(SplitVirtReg->reg);
  121. continue;
  122. }
  123. DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
  124. assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
  125. "expect split value in virtual register");
  126. enqueue(SplitVirtReg);
  127. ++NumNewQueued;
  128. }
  129. }
  130. }