RegisterClassInfo.cpp 6.1 KB

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