RegUsageInfoCollector.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===-- RegUsageInfoCollector.cpp - Register Usage Information Collector --===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// This pass is required to take advantage of the interprocedural register
  10. /// allocation infrastructure.
  11. ///
  12. /// This pass is simple MachineFunction pass which collects register usage
  13. /// details by iterating through each physical registers and checking
  14. /// MRI::isPhysRegUsed() then creates a RegMask based on this details.
  15. /// The pass then stores this RegMask in PhysicalRegisterUsageInfo.cpp
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/CodeGen/MachineBasicBlock.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/MachineInstr.h"
  22. #include "llvm/CodeGen/MachineOperand.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/Passes.h"
  25. #include "llvm/CodeGen/RegisterUsageInfo.h"
  26. #include "llvm/Support/Debug.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include "llvm/CodeGen/TargetFrameLowering.h"
  29. using namespace llvm;
  30. #define DEBUG_TYPE "ip-regalloc"
  31. STATISTIC(NumCSROpt,
  32. "Number of functions optimized for callee saved registers");
  33. namespace {
  34. class RegUsageInfoCollector : public MachineFunctionPass {
  35. public:
  36. RegUsageInfoCollector() : MachineFunctionPass(ID) {
  37. PassRegistry &Registry = *PassRegistry::getPassRegistry();
  38. initializeRegUsageInfoCollectorPass(Registry);
  39. }
  40. StringRef getPassName() const override {
  41. return "Register Usage Information Collector Pass";
  42. }
  43. void getAnalysisUsage(AnalysisUsage &AU) const override {
  44. AU.addRequired<PhysicalRegisterUsageInfo>();
  45. AU.setPreservesAll();
  46. MachineFunctionPass::getAnalysisUsage(AU);
  47. }
  48. bool runOnMachineFunction(MachineFunction &MF) override;
  49. // Call getCalleeSaves and then also set the bits for subregs and
  50. // fully saved superregs.
  51. static void computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF);
  52. static char ID;
  53. };
  54. } // end of anonymous namespace
  55. char RegUsageInfoCollector::ID = 0;
  56. INITIALIZE_PASS_BEGIN(RegUsageInfoCollector, "RegUsageInfoCollector",
  57. "Register Usage Information Collector", false, false)
  58. INITIALIZE_PASS_DEPENDENCY(PhysicalRegisterUsageInfo)
  59. INITIALIZE_PASS_END(RegUsageInfoCollector, "RegUsageInfoCollector",
  60. "Register Usage Information Collector", false, false)
  61. FunctionPass *llvm::createRegUsageInfoCollector() {
  62. return new RegUsageInfoCollector();
  63. }
  64. // TODO: Move to hook somwehere?
  65. // Return true if it is useful to track the used registers for IPRA / no CSR
  66. // optimizations. This is not useful for entry points, and computing the
  67. // register usage information is expensive.
  68. static bool isCallableFunction(const MachineFunction &MF) {
  69. switch (MF.getFunction().getCallingConv()) {
  70. case CallingConv::AMDGPU_VS:
  71. case CallingConv::AMDGPU_GS:
  72. case CallingConv::AMDGPU_PS:
  73. case CallingConv::AMDGPU_CS:
  74. case CallingConv::AMDGPU_HS:
  75. case CallingConv::AMDGPU_ES:
  76. case CallingConv::AMDGPU_LS:
  77. case CallingConv::AMDGPU_KERNEL:
  78. return false;
  79. default:
  80. return true;
  81. }
  82. }
  83. bool RegUsageInfoCollector::runOnMachineFunction(MachineFunction &MF) {
  84. MachineRegisterInfo *MRI = &MF.getRegInfo();
  85. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  86. const LLVMTargetMachine &TM = MF.getTarget();
  87. LLVM_DEBUG(dbgs() << " -------------------- " << getPassName()
  88. << " -------------------- \nFunction Name : "
  89. << MF.getName() << '\n');
  90. // Analyzing the register usage may be expensive on some targets.
  91. if (!isCallableFunction(MF)) {
  92. LLVM_DEBUG(dbgs() << "Not analyzing non-callable function\n");
  93. return false;
  94. }
  95. // If there are no callers, there's no point in computing more precise
  96. // register usage here.
  97. if (MF.getFunction().use_empty()) {
  98. LLVM_DEBUG(dbgs() << "Not analyzing function with no callers\n");
  99. return false;
  100. }
  101. std::vector<uint32_t> RegMask;
  102. // Compute the size of the bit vector to represent all the registers.
  103. // The bit vector is broken into 32-bit chunks, thus takes the ceil of
  104. // the number of registers divided by 32 for the size.
  105. unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs());
  106. RegMask.resize(RegMaskSize, ~((uint32_t)0));
  107. const Function &F = MF.getFunction();
  108. PhysicalRegisterUsageInfo &PRUI = getAnalysis<PhysicalRegisterUsageInfo>();
  109. PRUI.setTargetMachine(TM);
  110. LLVM_DEBUG(dbgs() << "Clobbered Registers: ");
  111. BitVector SavedRegs;
  112. computeCalleeSavedRegs(SavedRegs, MF);
  113. const BitVector &UsedPhysRegsMask = MRI->getUsedPhysRegsMask();
  114. auto SetRegAsDefined = [&RegMask] (unsigned Reg) {
  115. RegMask[Reg / 32] &= ~(1u << Reg % 32);
  116. };
  117. // Some targets can clobber registers "inside" a call, typically in
  118. // linker-generated code.
  119. for (const MCPhysReg Reg : TRI->getIntraCallClobberedRegs(&MF))
  120. for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
  121. SetRegAsDefined(*AI);
  122. // Scan all the physical registers. When a register is defined in the current
  123. // function set it and all the aliasing registers as defined in the regmask.
  124. // FIXME: Rewrite to use regunits.
  125. for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
  126. // Don't count registers that are saved and restored.
  127. if (SavedRegs.test(PReg))
  128. continue;
  129. // If a register is defined by an instruction mark it as defined together
  130. // with all it's unsaved aliases.
  131. if (!MRI->def_empty(PReg)) {
  132. for (MCRegAliasIterator AI(PReg, TRI, true); AI.isValid(); ++AI)
  133. if (!SavedRegs.test(*AI))
  134. SetRegAsDefined(*AI);
  135. continue;
  136. }
  137. // If a register is in the UsedPhysRegsMask set then mark it as defined.
  138. // All clobbered aliases will also be in the set, so we can skip setting
  139. // as defined all the aliases here.
  140. if (UsedPhysRegsMask.test(PReg))
  141. SetRegAsDefined(PReg);
  142. }
  143. if (TargetFrameLowering::isSafeForNoCSROpt(F) &&
  144. MF.getSubtarget().getFrameLowering()->isProfitableForNoCSROpt(F)) {
  145. ++NumCSROpt;
  146. LLVM_DEBUG(dbgs() << MF.getName()
  147. << " function optimized for not having CSR.\n");
  148. }
  149. LLVM_DEBUG(
  150. for (unsigned PReg = 1, PRegE = TRI->getNumRegs(); PReg < PRegE; ++PReg) {
  151. if (MachineOperand::clobbersPhysReg(&(RegMask[0]), PReg))
  152. dbgs() << printReg(PReg, TRI) << " ";
  153. }
  154. dbgs() << " \n----------------------------------------\n";
  155. );
  156. PRUI.storeUpdateRegUsageInfo(F, RegMask);
  157. return false;
  158. }
  159. void RegUsageInfoCollector::
  160. computeCalleeSavedRegs(BitVector &SavedRegs, MachineFunction &MF) {
  161. const TargetFrameLowering &TFI = *MF.getSubtarget().getFrameLowering();
  162. const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
  163. // Target will return the set of registers that it saves/restores as needed.
  164. SavedRegs.clear();
  165. TFI.getCalleeSaves(MF, SavedRegs);
  166. if (SavedRegs.none())
  167. return;
  168. // Insert subregs.
  169. const MCPhysReg *CSRegs = TRI.getCalleeSavedRegs(&MF);
  170. for (unsigned i = 0; CSRegs[i]; ++i) {
  171. MCPhysReg Reg = CSRegs[i];
  172. if (SavedRegs.test(Reg)) {
  173. // Save subregisters
  174. for (MCSubRegIterator SR(Reg, &TRI); SR.isValid(); ++SR)
  175. SavedRegs.set(*SR);
  176. }
  177. }
  178. }