StackMapLivenessAnalysis.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. MachineFunctionProperties getRequiredProperties() const override {
  56. return MachineFunctionProperties().set(
  57. MachineFunctionProperties::Property::AllVRegsAllocated);
  58. }
  59. /// \brief Calculate the liveness information for the given machine function.
  60. bool runOnMachineFunction(MachineFunction &MF) override;
  61. private:
  62. /// \brief Performs the actual liveness calculation for the function.
  63. bool calculateLiveness(MachineFunction &MF);
  64. /// \brief Add the current register live set to the instruction.
  65. void addLiveOutSetToMI(MachineFunction &MF, MachineInstr &MI);
  66. /// \brief Create a register mask and initialize it with the registers from
  67. /// the register live set.
  68. uint32_t *createRegisterMask(MachineFunction &MF) const;
  69. };
  70. } // namespace
  71. char StackMapLiveness::ID = 0;
  72. char &llvm::StackMapLivenessID = StackMapLiveness::ID;
  73. INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
  74. "StackMap Liveness Analysis", false, false)
  75. /// Default construct and initialize the pass.
  76. StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
  77. initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
  78. }
  79. /// Tell the pass manager which passes we depend on and what information we
  80. /// preserve.
  81. void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
  82. // We preserve all information.
  83. AU.setPreservesAll();
  84. AU.setPreservesCFG();
  85. MachineFunctionPass::getAnalysisUsage(AU);
  86. }
  87. /// Calculate the liveness information for the given machine function.
  88. bool StackMapLiveness::runOnMachineFunction(MachineFunction &MF) {
  89. if (!EnablePatchPointLiveness)
  90. return false;
  91. DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: " << MF.getName()
  92. << " **********\n");
  93. TRI = MF.getSubtarget().getRegisterInfo();
  94. ++NumStackMapFuncVisited;
  95. // Skip this function if there are no patchpoints to process.
  96. if (!MF.getFrameInfo()->hasPatchPoint()) {
  97. ++NumStackMapFuncSkipped;
  98. return false;
  99. }
  100. return calculateLiveness(MF);
  101. }
  102. /// Performs the actual liveness calculation for the function.
  103. bool StackMapLiveness::calculateLiveness(MachineFunction &MF) {
  104. bool HasChanged = false;
  105. // For all basic blocks in the function.
  106. for (auto &MBB : MF) {
  107. DEBUG(dbgs() << "****** BB " << MBB.getName() << " ******\n");
  108. LiveRegs.init(TRI);
  109. // FIXME: This should probably be addLiveOuts().
  110. LiveRegs.addLiveOutsNoPristines(MBB);
  111. bool HasStackMap = false;
  112. // Reverse iterate over all instructions and add the current live register
  113. // set to an instruction if we encounter a patchpoint instruction.
  114. for (auto I = MBB.rbegin(), E = MBB.rend(); I != E; ++I) {
  115. if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
  116. addLiveOutSetToMI(MF, *I);
  117. HasChanged = true;
  118. HasStackMap = true;
  119. ++NumStackMaps;
  120. }
  121. DEBUG(dbgs() << " " << LiveRegs << " " << *I);
  122. LiveRegs.stepBackward(*I);
  123. }
  124. ++NumBBsVisited;
  125. if (!HasStackMap)
  126. ++NumBBsHaveNoStackmap;
  127. }
  128. return HasChanged;
  129. }
  130. /// Add the current register live set to the instruction.
  131. void StackMapLiveness::addLiveOutSetToMI(MachineFunction &MF,
  132. MachineInstr &MI) {
  133. uint32_t *Mask = createRegisterMask(MF);
  134. MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
  135. MI.addOperand(MF, MO);
  136. }
  137. /// Create a register mask and initialize it with the registers from the
  138. /// register live set.
  139. uint32_t *StackMapLiveness::createRegisterMask(MachineFunction &MF) const {
  140. // The mask is owned and cleaned up by the Machine Function.
  141. uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
  142. for (auto Reg : LiveRegs)
  143. Mask[Reg / 32] |= 1U << (Reg % 32);
  144. // Give the target a chance to adjust the mask.
  145. TRI->adjustStackMapLiveOutMask(Mask);
  146. return Mask;
  147. }