StackMapLivenessAnalysis.cpp 6.1 KB

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