| 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 | |
| 9 | #include "LiveDebugValues.h" |
| 10 | |
| 11 | #include "llvm/CodeGen/LiveDebugValuesPass.h" |
| 12 | #include "llvm/CodeGen/MachineDominators.h" |
| 13 | #include "llvm/CodeGen/MachineFunction.h" |
| 14 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 15 | #include "llvm/CodeGen/Passes.h" |
| 16 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 17 | #include "llvm/InitializePasses.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/PassRegistry.h" |
| 20 | #include "llvm/Support/CommandLine.h" |
| 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/TargetParser/Triple.h" |
| 23 | |
| 24 | /// \file LiveDebugValues.cpp |
| 25 | /// |
| 26 | /// The LiveDebugValues pass extends the range of variable locations |
| 27 | /// (specified by DBG_VALUE instructions) from single blocks to successors |
| 28 | /// and any other code locations where the variable location is valid. |
| 29 | /// There are currently two implementations: the "VarLoc" implementation |
| 30 | /// explicitly tracks the location of a variable, while the "InstrRef" |
| 31 | /// implementation tracks the values defined by instructions through locations. |
| 32 | /// |
| 33 | /// This file implements neither; it merely registers the pass, allows the |
| 34 | /// user to pick which implementation will be used to propagate variable |
| 35 | /// locations. |
| 36 | |
| 37 | #define DEBUG_TYPE "livedebugvalues" |
| 38 | |
| 39 | using namespace llvm; |
| 40 | |
| 41 | static cl::opt<bool> |
| 42 | ForceInstrRefLDV("force-instr-ref-livedebugvalues" , cl::Hidden, |
| 43 | cl::desc("Use instruction-ref based LiveDebugValues with " |
| 44 | "normal DBG_VALUE inputs" ), |
| 45 | cl::init(Val: false)); |
| 46 | |
| 47 | static cl::opt<cl::boolOrDefault> ValueTrackingVariableLocations( |
| 48 | "experimental-debug-variable-locations" , |
| 49 | cl::desc("Use experimental new value-tracking variable locations" )); |
| 50 | |
| 51 | // Options to prevent pathological compile-time behavior. If InputBBLimit and |
| 52 | // InputDbgValueLimit are both exceeded, range extension is disabled. |
| 53 | static cl::opt<unsigned> InputBBLimit( |
| 54 | "livedebugvalues-input-bb-limit" , |
| 55 | cl::desc("Maximum input basic blocks before DBG_VALUE limit applies" ), |
| 56 | cl::init(Val: 10000), cl::Hidden); |
| 57 | static cl::opt<unsigned> InputDbgValueLimit( |
| 58 | "livedebugvalues-input-dbg-value-limit" , |
| 59 | cl::desc( |
| 60 | "Maximum input DBG_VALUE insts supported by debug range extension" ), |
| 61 | cl::init(Val: 50000), cl::Hidden); |
| 62 | |
| 63 | namespace { |
| 64 | /// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or |
| 65 | /// InstrRefBasedLDV to perform location propagation, via the LDVImpl |
| 66 | /// base class. |
| 67 | class LiveDebugValuesLegacy : public MachineFunctionPass { |
| 68 | public: |
| 69 | static char ID; |
| 70 | |
| 71 | LiveDebugValuesLegacy(); |
| 72 | ~LiveDebugValuesLegacy() = default; |
| 73 | |
| 74 | /// Calculate the liveness information for the given machine function. |
| 75 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 76 | |
| 77 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 78 | AU.setPreservesCFG(); |
| 79 | AU.addRequired<TargetPassConfig>(); |
| 80 | MachineFunctionPass::getAnalysisUsage(AU); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | struct LiveDebugValues { |
| 85 | LiveDebugValues(); |
| 86 | ~LiveDebugValues() = default; |
| 87 | bool run(MachineFunction &MF, bool ShouldEmitDebugEntryValues); |
| 88 | |
| 89 | private: |
| 90 | std::unique_ptr<LDVImpl> InstrRefImpl; |
| 91 | std::unique_ptr<LDVImpl> VarLocImpl; |
| 92 | MachineDominatorTree MDT; |
| 93 | }; |
| 94 | } // namespace |
| 95 | |
| 96 | char LiveDebugValuesLegacy::ID = 0; |
| 97 | |
| 98 | char &llvm::LiveDebugValuesID = LiveDebugValuesLegacy::ID; |
| 99 | |
| 100 | INITIALIZE_PASS(LiveDebugValuesLegacy, DEBUG_TYPE, "Live DEBUG_VALUE analysis" , |
| 101 | false, false) |
| 102 | |
| 103 | /// Default construct and initialize the pass. |
| 104 | LiveDebugValuesLegacy::LiveDebugValuesLegacy() : MachineFunctionPass(ID) { |
| 105 | initializeLiveDebugValuesLegacyPass(Registry&: *PassRegistry::getPassRegistry()); |
| 106 | } |
| 107 | |
| 108 | LiveDebugValues::LiveDebugValues() { |
| 109 | InstrRefImpl = |
| 110 | std::unique_ptr<LDVImpl>(llvm::makeInstrRefBasedLiveDebugValues()); |
| 111 | VarLocImpl = std::unique_ptr<LDVImpl>(llvm::makeVarLocBasedLiveDebugValues()); |
| 112 | } |
| 113 | |
| 114 | PreservedAnalyses |
| 115 | LiveDebugValuesPass::run(MachineFunction &MF, |
| 116 | MachineFunctionAnalysisManager &MFAM) { |
| 117 | if (!LiveDebugValues().run(MF, ShouldEmitDebugEntryValues)) |
| 118 | return PreservedAnalyses::all(); |
| 119 | auto PA = getMachineFunctionPassPreservedAnalyses(); |
| 120 | PA.preserveSet<CFGAnalyses>(); |
| 121 | return PA; |
| 122 | } |
| 123 | |
| 124 | void LiveDebugValuesPass::printPipeline( |
| 125 | raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) { |
| 126 | OS << MapClassName2PassName(name()); |
| 127 | if (ShouldEmitDebugEntryValues) |
| 128 | OS << "<emit-debug-entry-values>" ; |
| 129 | } |
| 130 | |
| 131 | bool LiveDebugValuesLegacy::runOnMachineFunction(MachineFunction &MF) { |
| 132 | auto *TPC = &getAnalysis<TargetPassConfig>(); |
| 133 | return LiveDebugValues().run( |
| 134 | MF, ShouldEmitDebugEntryValues: TPC->getTM<TargetMachine>().Options.ShouldEmitDebugEntryValues()); |
| 135 | } |
| 136 | |
| 137 | bool LiveDebugValues::run(MachineFunction &MF, |
| 138 | bool ShouldEmitDebugEntryValues) { |
| 139 | bool InstrRefBased = MF.useDebugInstrRef(); |
| 140 | // Allow the user to force selection of InstrRef LDV. |
| 141 | InstrRefBased |= ForceInstrRefLDV; |
| 142 | |
| 143 | LDVImpl *TheImpl = &*VarLocImpl; |
| 144 | |
| 145 | MachineDominatorTree *DomTree = nullptr; |
| 146 | if (InstrRefBased) { |
| 147 | DomTree = &MDT; |
| 148 | MDT.recalculate(Func&: MF); |
| 149 | TheImpl = &*InstrRefImpl; |
| 150 | } |
| 151 | |
| 152 | return TheImpl->ExtendRanges(MF, DomTree, ShouldEmitDebugEntryValues, |
| 153 | InputBBLimit, InputDbgValLimit: InputDbgValueLimit); |
| 154 | } |
| 155 | |
| 156 | bool llvm::debuginfoShouldUseDebugInstrRef(const Triple &T) { |
| 157 | // Enable by default on x86_64, disable if explicitly turned off on cmdline. |
| 158 | if (T.getArch() == llvm::Triple::x86_64 && |
| 159 | ValueTrackingVariableLocations != cl::boolOrDefault::BOU_FALSE) |
| 160 | return true; |
| 161 | |
| 162 | // Enable if explicitly requested on command line. |
| 163 | return ValueTrackingVariableLocations == cl::boolOrDefault::BOU_TRUE; |
| 164 | } |
| 165 | |