StackMapLivenessAnalysis.cpp 4.6 KB

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