RegisterClassInfo.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 vs. caller-saved and
  12. // reserved registers depend on calling conventions and other dynamic
  13. // information, so some things cannot be determined statically.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/CodeGen/RegisterClassInfo.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/Support/CommandLine.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "regalloc"
  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()
  28. : Tag(0), MF(nullptr), TRI(nullptr), CalleeSaved(nullptr) {}
  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->getSubtarget().getRegisterInfo() != TRI) {
  34. TRI = MF->getSubtarget().getRegisterInfo();
  35. RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
  36. unsigned NumPSets = TRI->getNumRegPressureSets();
  37. PSetLimits.reset(new unsigned[NumPSets]);
  38. std::fill(&PSetLimits[0], &PSetLimits[NumPSets], 0);
  39. Update = true;
  40. }
  41. // Does this MF have different CSRs?
  42. assert(TRI && "no register info set");
  43. const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF);
  44. if (Update || CSR != CalleeSaved) {
  45. // Build a CSRNum map. Every CSR alias gets an entry pointing to the last
  46. // overlapping CSR.
  47. CSRNum.clear();
  48. CSRNum.resize(TRI->getNumRegs(), 0);
  49. for (unsigned N = 0; unsigned Reg = CSR[N]; ++N)
  50. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
  51. CSRNum[*AI] = N + 1; // 0 means no CSR, 1 means CalleeSaved[0], ...
  52. Update = true;
  53. }
  54. CalleeSaved = CSR;
  55. // Different reserved registers?
  56. const BitVector &RR = MF->getRegInfo().getReservedRegs();
  57. if (Reserved.size() != RR.size() || RR != Reserved) {
  58. Update = true;
  59. Reserved = RR;
  60. }
  61. // Invalidate cached information from previous function.
  62. if (Update)
  63. ++Tag;
  64. }
  65. /// compute - Compute the preferred allocation order for RC with reserved
  66. /// registers filtered out. Volatile registers come first followed by CSR
  67. /// aliases ordered according to the CSR order specified by the target.
  68. void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
  69. assert(RC && "no register class given");
  70. RCInfo &RCI = RegClass[RC->getID()];
  71. // Raw register count, including all reserved regs.
  72. unsigned NumRegs = RC->getNumRegs();
  73. if (!RCI.Order)
  74. RCI.Order.reset(new MCPhysReg[NumRegs]);
  75. unsigned N = 0;
  76. SmallVector<MCPhysReg, 16> CSRAlias;
  77. unsigned MinCost = 0xff;
  78. unsigned LastCost = ~0u;
  79. unsigned LastCostChange = 0;
  80. // FIXME: Once targets reserve registers instead of removing them from the
  81. // allocation order, we can simply use begin/end here.
  82. ArrayRef<MCPhysReg> RawOrder = RC->getRawAllocationOrder(*MF);
  83. for (unsigned i = 0; i != RawOrder.size(); ++i) {
  84. unsigned PhysReg = RawOrder[i];
  85. // Remove reserved registers from the allocation order.
  86. if (Reserved.test(PhysReg))
  87. continue;
  88. unsigned Cost = TRI->getCostPerUse(PhysReg);
  89. MinCost = std::min(MinCost, Cost);
  90. if (CSRNum[PhysReg])
  91. // PhysReg aliases a CSR, save it for later.
  92. CSRAlias.push_back(PhysReg);
  93. else {
  94. if (Cost != LastCost)
  95. LastCostChange = N;
  96. RCI.Order[N++] = PhysReg;
  97. LastCost = Cost;
  98. }
  99. }
  100. RCI.NumRegs = N + CSRAlias.size();
  101. assert (RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
  102. // CSR aliases go after the volatile registers, preserve the target's order.
  103. for (unsigned i = 0, e = CSRAlias.size(); i != e; ++i) {
  104. unsigned PhysReg = CSRAlias[i];
  105. unsigned Cost = TRI->getCostPerUse(PhysReg);
  106. if (Cost != LastCost)
  107. LastCostChange = N;
  108. RCI.Order[N++] = PhysReg;
  109. LastCost = Cost;
  110. }
  111. // Register allocator stress test. Clip register class to N registers.
  112. if (StressRA && RCI.NumRegs > StressRA)
  113. RCI.NumRegs = StressRA;
  114. // Check if RC is a proper sub-class.
  115. if (const TargetRegisterClass *Super =
  116. TRI->getLargestLegalSuperClass(RC, *MF))
  117. if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
  118. RCI.ProperSubClass = true;
  119. RCI.MinCost = uint8_t(MinCost);
  120. RCI.LastCostChange = LastCostChange;
  121. DEBUG({
  122. dbgs() << "AllocationOrder(" << TRI->getRegClassName(RC) << ") = [";
  123. for (unsigned I = 0; I != RCI.NumRegs; ++I)
  124. dbgs() << ' ' << PrintReg(RCI.Order[I], TRI);
  125. dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
  126. });
  127. // RCI is now up-to-date.
  128. RCI.Tag = Tag;
  129. }
  130. /// This is not accurate because two overlapping register sets may have some
  131. /// nonoverlapping reserved registers. However, computing the allocation order
  132. /// for all register classes would be too expensive.
  133. unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
  134. const TargetRegisterClass *RC = nullptr;
  135. unsigned NumRCUnits = 0;
  136. for (TargetRegisterInfo::regclass_iterator
  137. RI = TRI->regclass_begin(), RE = TRI->regclass_end(); RI != RE; ++RI) {
  138. const int *PSetID = TRI->getRegClassPressureSets(*RI);
  139. for (; *PSetID != -1; ++PSetID) {
  140. if ((unsigned)*PSetID == Idx)
  141. break;
  142. }
  143. if (*PSetID == -1)
  144. continue;
  145. // Found a register class that counts against this pressure set.
  146. // For efficiency, only compute the set order for the largest set.
  147. unsigned NUnits = TRI->getRegClassWeight(*RI).WeightLimit;
  148. if (!RC || NUnits > NumRCUnits) {
  149. RC = *RI;
  150. NumRCUnits = NUnits;
  151. }
  152. }
  153. compute(RC);
  154. unsigned NReserved = RC->getNumRegs() - getNumAllocatableRegs(RC);
  155. return TRI->getRegPressureSetLimit(*MF, Idx) -
  156. TRI->getRegClassWeight(RC).RegWeight * NReserved;
  157. }