LiveDebugValues.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===- LiveDebugValues.cpp - Tracking Debug Value MIs ---------------------===//
  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. #include "LiveDebugValues.h"
  9. #include "llvm/ADT/Triple.h"
  10. #include "llvm/CodeGen/MachineDominators.h"
  11. #include "llvm/CodeGen/MachineFunction.h"
  12. #include "llvm/CodeGen/MachineFunctionPass.h"
  13. #include "llvm/CodeGen/Passes.h"
  14. #include "llvm/CodeGen/TargetPassConfig.h"
  15. #include "llvm/InitializePasses.h"
  16. #include "llvm/Pass.h"
  17. #include "llvm/PassRegistry.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. /// \file LiveDebugValues.cpp
  21. ///
  22. /// The LiveDebugValues pass extends the range of variable locations
  23. /// (specified by DBG_VALUE instructions) from single blocks to successors
  24. /// and any other code locations where the variable location is valid.
  25. /// There are currently two implementations: the "VarLoc" implementation
  26. /// explicitly tracks the location of a variable, while the "InstrRef"
  27. /// implementation tracks the values defined by instructions through locations.
  28. ///
  29. /// This file implements neither; it merely registers the pass, allows the
  30. /// user to pick which implementation will be used to propagate variable
  31. /// locations.
  32. #define DEBUG_TYPE "livedebugvalues"
  33. using namespace llvm;
  34. static cl::opt<bool>
  35. ForceInstrRefLDV("force-instr-ref-livedebugvalues", cl::Hidden,
  36. cl::desc("Use instruction-ref based LiveDebugValues with "
  37. "normal DBG_VALUE inputs"),
  38. cl::init(false));
  39. static cl::opt<cl::boolOrDefault> ValueTrackingVariableLocations(
  40. "experimental-debug-variable-locations",
  41. cl::desc("Use experimental new value-tracking variable locations"));
  42. // Options to prevent pathological compile-time behavior. If InputBBLimit and
  43. // InputDbgValueLimit are both exceeded, range extension is disabled.
  44. static cl::opt<unsigned> InputBBLimit(
  45. "livedebugvalues-input-bb-limit",
  46. cl::desc("Maximum input basic blocks before DBG_VALUE limit applies"),
  47. cl::init(10000), cl::Hidden);
  48. static cl::opt<unsigned> InputDbgValueLimit(
  49. "livedebugvalues-input-dbg-value-limit",
  50. cl::desc(
  51. "Maximum input DBG_VALUE insts supported by debug range extension"),
  52. cl::init(50000), cl::Hidden);
  53. namespace {
  54. /// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or
  55. /// InstrRefBasedLDV to perform location propagation, via the LDVImpl
  56. /// base class.
  57. class LiveDebugValues : public MachineFunctionPass {
  58. public:
  59. static char ID;
  60. LiveDebugValues();
  61. ~LiveDebugValues() = default;
  62. /// Calculate the liveness information for the given machine function.
  63. bool runOnMachineFunction(MachineFunction &MF) override;
  64. void getAnalysisUsage(AnalysisUsage &AU) const override {
  65. AU.setPreservesCFG();
  66. MachineFunctionPass::getAnalysisUsage(AU);
  67. }
  68. private:
  69. std::unique_ptr<LDVImpl> InstrRefImpl;
  70. std::unique_ptr<LDVImpl> VarLocImpl;
  71. TargetPassConfig *TPC;
  72. MachineDominatorTree MDT;
  73. };
  74. } // namespace
  75. char LiveDebugValues::ID = 0;
  76. char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
  77. INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", false,
  78. false)
  79. /// Default construct and initialize the pass.
  80. LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
  81. initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
  82. InstrRefImpl =
  83. std::unique_ptr<LDVImpl>(llvm::makeInstrRefBasedLiveDebugValues());
  84. VarLocImpl = std::unique_ptr<LDVImpl>(llvm::makeVarLocBasedLiveDebugValues());
  85. }
  86. bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
  87. // Except for Wasm, all targets should be only using physical register at this
  88. // point. Wasm only use virtual registers throught its pipeline, but its
  89. // virtual registers don't participate in this LiveDebugValues analysis; only
  90. // its target indices do.
  91. assert(MF.getTarget().getTargetTriple().isWasm() ||
  92. MF.getProperties().hasProperty(
  93. MachineFunctionProperties::Property::NoVRegs));
  94. bool InstrRefBased = MF.useDebugInstrRef();
  95. // Allow the user to force selection of InstrRef LDV.
  96. InstrRefBased |= ForceInstrRefLDV;
  97. TPC = getAnalysisIfAvailable<TargetPassConfig>();
  98. LDVImpl *TheImpl = &*VarLocImpl;
  99. MachineDominatorTree *DomTree = nullptr;
  100. if (InstrRefBased) {
  101. DomTree = &MDT;
  102. MDT.calculate(MF);
  103. TheImpl = &*InstrRefImpl;
  104. }
  105. return TheImpl->ExtendRanges(MF, DomTree, TPC, InputBBLimit,
  106. InputDbgValueLimit);
  107. }
  108. bool llvm::debuginfoShouldUseDebugInstrRef(const Triple &T) {
  109. // Enable by default on x86_64, disable if explicitly turned off on cmdline.
  110. if (T.getArch() == llvm::Triple::x86_64 &&
  111. ValueTrackingVariableLocations != cl::boolOrDefault::BOU_FALSE)
  112. return true;
  113. // Enable if explicitly requested on command line.
  114. return ValueTrackingVariableLocations == cl::boolOrDefault::BOU_TRUE;
  115. }