StackMapLivenessAnalysis.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/MachineFrameInfo.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineFunctionAnalysis.h"
  19. #include "llvm/CodeGen/Passes.h"
  20. #include "llvm/CodeGen/StackMapLivenessAnalysis.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Target/TargetSubtargetInfo.h"
  24. using namespace llvm;
  25. #define DEBUG_TYPE "stackmaps"
  26. namespace llvm {
  27. cl::opt<bool> EnablePatchPointLiveness("enable-patchpoint-liveness",
  28. cl::Hidden, cl::init(true),
  29. cl::desc("Enable PatchPoint Liveness Analysis Pass"));
  30. }
  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. char StackMapLiveness::ID = 0;
  37. char &llvm::StackMapLivenessID = StackMapLiveness::ID;
  38. INITIALIZE_PASS(StackMapLiveness, "stackmap-liveness",
  39. "StackMap Liveness Analysis", false, false)
  40. /// Default construct and initialize the pass.
  41. StackMapLiveness::StackMapLiveness() : MachineFunctionPass(ID) {
  42. initializeStackMapLivenessPass(*PassRegistry::getPassRegistry());
  43. }
  44. /// Tell the pass manager which passes we depend on and what information we
  45. /// preserve.
  46. void StackMapLiveness::getAnalysisUsage(AnalysisUsage &AU) const {
  47. // We preserve all information.
  48. AU.setPreservesAll();
  49. AU.setPreservesCFG();
  50. // Default dependencie for all MachineFunction passes.
  51. AU.addRequired<MachineFunctionAnalysis>();
  52. }
  53. /// Calculate the liveness information for the given machine function.
  54. bool StackMapLiveness::runOnMachineFunction(MachineFunction &_MF) {
  55. if (!EnablePatchPointLiveness)
  56. return false;
  57. DEBUG(dbgs() << "********** COMPUTING STACKMAP LIVENESS: "
  58. << _MF.getName() << " **********\n");
  59. MF = &_MF;
  60. TRI = MF->getTarget().getSubtargetImpl()->getRegisterInfo();
  61. ++NumStackMapFuncVisited;
  62. // Skip this function if there are no patchpoints to process.
  63. if (!MF->getFrameInfo()->hasPatchPoint()) {
  64. ++NumStackMapFuncSkipped;
  65. return false;
  66. }
  67. return calculateLiveness();
  68. }
  69. /// Performs the actual liveness calculation for the function.
  70. bool StackMapLiveness::calculateLiveness() {
  71. bool HasChanged = false;
  72. // For all basic blocks in the function.
  73. for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end();
  74. MBBI != MBBE; ++MBBI) {
  75. DEBUG(dbgs() << "****** BB " << MBBI->getName() << " ******\n");
  76. LiveRegs.init(TRI);
  77. LiveRegs.addLiveOuts(MBBI);
  78. bool HasStackMap = false;
  79. // Reverse iterate over all instructions and add the current live register
  80. // set to an instruction if we encounter a patchpoint instruction.
  81. for (MachineBasicBlock::reverse_iterator I = MBBI->rbegin(),
  82. E = MBBI->rend(); I != E; ++I) {
  83. if (I->getOpcode() == TargetOpcode::PATCHPOINT) {
  84. addLiveOutSetToMI(*I);
  85. HasChanged = true;
  86. HasStackMap = true;
  87. ++NumStackMaps;
  88. }
  89. DEBUG(dbgs() << " " << LiveRegs << " " << *I);
  90. LiveRegs.stepBackward(*I);
  91. }
  92. ++NumBBsVisited;
  93. if (!HasStackMap)
  94. ++NumBBsHaveNoStackmap;
  95. }
  96. return HasChanged;
  97. }
  98. /// Add the current register live set to the instruction.
  99. void StackMapLiveness::addLiveOutSetToMI(MachineInstr &MI) {
  100. uint32_t *Mask = createRegisterMask();
  101. MachineOperand MO = MachineOperand::CreateRegLiveOut(Mask);
  102. MI.addOperand(*MF, MO);
  103. }
  104. /// Create a register mask and initialize it with the registers from the
  105. /// register live set.
  106. uint32_t *StackMapLiveness::createRegisterMask() const {
  107. // The mask is owned and cleaned up by the Machine Function.
  108. uint32_t *Mask = MF->allocateRegisterMask(TRI->getNumRegs());
  109. for (LivePhysRegs::const_iterator RI = LiveRegs.begin(), RE = LiveRegs.end();
  110. RI != RE; ++RI)
  111. Mask[*RI / 32] |= 1U << (*RI % 32);
  112. return Mask;
  113. }