RegisterClassInfo.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===-- RegisterClassInfo.cpp - Dynamic Register Class Info ---------------===//
  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 implements the RegisterClassInfo class which provides dynamic
  11. // information about target register classes. Callee saved and reserved
  12. // registers depends on calling conventions and other dynamic information, so
  13. // some things cannot be determined statically.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #define DEBUG_TYPE "regalloc"
  17. #include "RegisterClassInfo.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace llvm;
  24. static cl::opt<unsigned>
  25. StressRA("stress-regalloc", cl::Hidden, cl::init(0), cl::value_desc("N"),
  26. cl::desc("Limit all regclasses to N registers"));
  27. RegisterClassInfo::RegisterClassInfo() : Tag(0), MF(0), TRI(0), CalleeSaved(0)
  28. {}
  29. void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf) {
  30. bool Update = false;
  31. MF = &mf;
  32. // Allocate new array the first time we see a new target.
  33. if (MF->getTarget().getRegisterInfo() != TRI) {
  34. TRI = MF->getTarget().getRegisterInfo();
  35. RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
  36. Update = true;
  37. }
  38. // Does this MF have different CSRs?
  39. const uint16_t *CSR = TRI->getCalleeSavedRegs(MF);
  40. if (Update || CSR != CalleeSaved) {
  41. // Build a CSRNum map. Every CSR alias gets an entry pointing to the last
  42. // overlapping CSR.
  43. CSRNum.clear();
  44. CSRNum.resize(TRI->getNumRegs(), 0);
  45. for (unsigned N = 0; unsigned Reg = CSR[N]; ++N)
  46. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
  47. CSRNum[*AI] = N + 1; // 0 means no CSR, 1 means CalleeSaved[0], ...
  48. Update = true;
  49. }
  50. CalleeSaved = CSR;
  51. // Different reserved registers?
  52. BitVector RR = TRI->getReservedRegs(*MF);
  53. if (RR != Reserved)
  54. Update = true;
  55. Reserved = RR;
  56. // Invalidate cached information from previous function.
  57. if (Update)
  58. ++Tag;
  59. }
  60. /// compute - Compute the preferred allocation order for RC with reserved
  61. /// registers filtered out. Volatile registers come first followed by CSR
  62. /// aliases ordered according to the CSR order specified by the target.
  63. void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
  64. RCInfo &RCI = RegClass[RC->getID()];
  65. // Raw register count, including all reserved regs.
  66. unsigned NumRegs = RC->getNumRegs();
  67. if (!RCI.Order)
  68. RCI.Order.reset(new unsigned[NumRegs]);
  69. unsigned N = 0;
  70. SmallVector<unsigned, 16> CSRAlias;
  71. // FIXME: Once targets reserve registers instead of removing them from the
  72. // allocation order, we can simply use begin/end here.
  73. ArrayRef<uint16_t> RawOrder = RC->getRawAllocationOrder(*MF);
  74. for (unsigned i = 0; i != RawOrder.size(); ++i) {
  75. unsigned PhysReg = RawOrder[i];
  76. // Remove reserved registers from the allocation order.
  77. if (Reserved.test(PhysReg))
  78. continue;
  79. if (CSRNum[PhysReg])
  80. // PhysReg aliases a CSR, save it for later.
  81. CSRAlias.push_back(PhysReg);
  82. else
  83. RCI.Order[N++] = PhysReg;
  84. }
  85. RCI.NumRegs = N + CSRAlias.size();
  86. assert (RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
  87. // CSR aliases go after the volatile registers, preserve the target's order.
  88. std::copy(CSRAlias.begin(), CSRAlias.end(), &RCI.Order[N]);
  89. // Register allocator stress test. Clip register class to N registers.
  90. if (StressRA && RCI.NumRegs > StressRA)
  91. RCI.NumRegs = StressRA;
  92. // Check if RC is a proper sub-class.
  93. if (const TargetRegisterClass *Super = TRI->getLargestLegalSuperClass(RC))
  94. if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
  95. RCI.ProperSubClass = true;
  96. DEBUG({
  97. dbgs() << "AllocationOrder(" << RC->getName() << ") = [";
  98. for (unsigned I = 0; I != RCI.NumRegs; ++I)
  99. dbgs() << ' ' << PrintReg(RCI.Order[I], TRI);
  100. dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
  101. });
  102. // RCI is now up-to-date.
  103. RCI.Tag = Tag;
  104. }