StackMapLivenessAnalysis.cpp 6.1 KB

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