2
0

StackMapLivenessAnalysis.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //===-- StackMapLivenessAnalysis.cpp - StackMap live Out Analysis ----------===//
  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 StackMap Liveness analysis pass. The pass calculates
  11. // the liveness for each basic block in a function and attaches the register
  12. // live-out information to a stackmap or patchpoint intrinsic if present.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/CodeGen/LivePhysRegs.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineFunctionAnalysis.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/Passes.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/Target/TargetSubtargetInfo.h"
  26. using namespace llvm;
  27. #define DEBUG_TYPE "stackmaps"
  28. static cl::opt<bool> EnablePatchPointLiveness(
  29. "enable-patchpoint-liveness", cl::Hidden, cl::init(true),
  30. cl::desc("Enable PatchPoint Liveness Analysis Pass"));
  31. STATISTIC(NumStackMapFuncVisited, "Number of functions visited");
  32. STATISTIC(NumStackMapFuncSkipped, "Number of functions skipped");
  33. STATISTIC(NumBBsVisited, "Number of basic blocks visited");
  34. STATISTIC(NumBBsHaveNoStackmap, "Number of basic blocks with no stackmap");
  35. STATISTIC(NumStackMaps, "Number of StackMaps visited");
  36. namespace {
  37. /// \brief This pass calculates the liveness information for each basic block in
  38. /// a function and attaches the register live-out information to a patchpoint
  39. /// intrinsic if present.
  40. ///
  41. /// This pass can be disabled via the -enable-patchpoint-liveness=false flag.
  42. /// The pass skips functions that don't have any patchpoint intrinsics. The
  43. /// information provided by this pass is optional and not required by the
  44. /// aformentioned intrinsic to function.
  45. class StackMapLiveness : public MachineFunctionPass {
  46. const TargetRegisterInfo *TRI;
  47. LivePhysRegs LiveRegs;
  48. public:
  49. static char ID;
  50. /// \brief Default construct and initialize the pass.
  51. StackMapLiveness();
  52. /// \brief Tell the pass manager which passes we depend on and what
  53. /// information we preserve.
  54. void getAnalysisUsage(AnalysisUsage &AU) const override;
  55. /// \brief Calculate the liveness information for the given machine function.
  56. bool runOnMachineFunction(MachineFunction &MF) override;
  57. private:
  58. /// \brief Performs the actual liveness calculation for the function.
  59. bool calculateLiveness(MachineFunction &MF);
  60. /// \brief Add the current register live set to the instruction.
  61. void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
  62. /// \brief Create a register mask and initialize it with the registers from
  63. /// the register live set.
  64. uint32_t *createRegisterMask(MachineFunction &MF) const;
  65. };
  66. } // namespace
  67. char StackMapLiveness::ID = 0;
  68. char &llvm::StackMapLivenessID = StackMapLiveness::ID;
  69. INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
  70. "StackMap Liveness Analysis", false, false)
  71. /// Default construct and initialize the pass.
  72. StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
  73. initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
  74. }
  75. /// Tell the pass manager which passes we depend on and what information we
  76. /// preserve.
  77. void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
  78. // We preserve all information.
  79. AU.setPreservesAll();
  80. AU.setPreservesCFG();
  81. MachineFunctionPass::getAnalysisUsage(AU);
  82. }
  83. /// Calculate the liveness information for the given machine function.
  84. bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
  85. if (!EnablePatchPointLiveness)
  86. return false;
  87. DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName()
  88. << " **********\n");
  89. TRI = MF.getSubtarget().getRegisterInfo();
  90. ++NumStackMapFuncVisited;
  91. // Skip this function if there are no patchpoints to process.
  92. if (!MF.getFrameInfo()->hasPatchPoint()) {
  93. ++NumStackMapFuncSkipped;
  94. return false;
  95. }
  96. return calculateLiveness(MF);
  97. }
  98. /// Performs the actual liveness calculation for the function.
  99. bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
  100. bool HasChanged = false;
  101. // For all basic blocks in the function.
  102. for (auto &MBB : MF) {
  103. DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
  104. LiveRegs.init(TRI);
  105. LiveRegs.addLiveOuts(&MBB);
  106. bool HasStackMap = false;
  107. // Reverse iterate over all instructions and add the current live register
  108. // set to an instruction if we encounter a patchpoint instruction.
  109. for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
  110. if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
  111. addLiveOutSetToMI(MF, *I);
  112. HasChanged = true;
  113. HasStackMap = true;
  114. ++NumStackMaps;
  115. }
  116. DEBUG(dbgs() << " " << LiveRegs << " " << *I);
  117. LiveRegs.stepBackward(*I);
  118. }
  119. ++NumBBsVisited;
  120. if (!HasStackMap)
  121. ++NumBBsHaveNoStackmap;
  122. }
  123. return HasChanged;
  124. }
  125. /// Add the current register live set to the instruction.
  126. void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
  127. MachineInstr &MI) {
  128. uint32_t *Mask = createRegisterMask(MF);
  129. MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
  130. MI.addOperand(MF, MO);
  131. }
  132. /// Create a register mask and initialize it with the registers from the
  133. /// register live set.
  134. uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
  135. // The mask is owned and cleaned up by the Machine Function.
  136. uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
  137. for (auto Reg : LiveRegs)
  138. Mask[Reg / 32] |= 1U << (Reg % 32);
  139. // Give the target a chance to adjust the mask.
  140. TRI->adjustStackMapLiveOutMask(Mask);
  141. return Mask;
  142. }