1 //===- LiveDebugValues.cpp - Tracking Debug Value MIs ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "LiveDebugValues.h"
11 #include "llvm/CodeGen/MachineDominators.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/CodeGen/TargetPassConfig.h"
16 #include "llvm/InitializePasses.h"
17 #include "llvm/Pass.h"
18 #include "llvm/PassRegistry.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/TargetParser/Triple.h"
23 /// \file LiveDebugValues.cpp
25 /// The LiveDebugValues pass extends the range of variable locations
26 /// (specified by DBG_VALUE instructions) from single blocks to successors
27 /// and any other code locations where the variable location is valid.
28 /// There are currently two implementations: the "VarLoc" implementation
29 /// explicitly tracks the location of a variable, while the "InstrRef"
30 /// implementation tracks the values defined by instructions through locations.
32 /// This file implements neither; it merely registers the pass, allows the
33 /// user to pick which implementation will be used to propagate variable
36 #define DEBUG_TYPE "livedebugvalues"
41 ForceInstrRefLDV("force-instr-ref-livedebugvalues", cl::Hidden
,
42 cl::desc("Use instruction-ref based LiveDebugValues with "
43 "normal DBG_VALUE inputs"),
46 static cl::opt
<cl::boolOrDefault
> ValueTrackingVariableLocations(
47 "experimental-debug-variable-locations",
48 cl::desc("Use experimental new value-tracking variable locations"));
50 // Options to prevent pathological compile-time behavior. If InputBBLimit and
51 // InputDbgValueLimit are both exceeded, range extension is disabled.
52 static cl::opt
<unsigned> InputBBLimit(
53 "livedebugvalues-input-bb-limit",
54 cl::desc("Maximum input basic blocks before DBG_VALUE limit applies"),
55 cl::init(10000), cl::Hidden
);
56 static cl::opt
<unsigned> InputDbgValueLimit(
57 "livedebugvalues-input-dbg-value-limit",
59 "Maximum input DBG_VALUE insts supported by debug range extension"),
60 cl::init(50000), cl::Hidden
);
63 /// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or
64 /// InstrRefBasedLDV to perform location propagation, via the LDVImpl
66 class LiveDebugValues
: public MachineFunctionPass
{
71 ~LiveDebugValues() = default;
73 /// Calculate the liveness information for the given machine function.
74 bool runOnMachineFunction(MachineFunction
&MF
) override
;
76 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
78 MachineFunctionPass::getAnalysisUsage(AU
);
82 std::unique_ptr
<LDVImpl
> InstrRefImpl
;
83 std::unique_ptr
<LDVImpl
> VarLocImpl
;
84 TargetPassConfig
*TPC
= nullptr;
85 MachineDominatorTree MDT
;
89 char LiveDebugValues::ID
= 0;
91 char &llvm::LiveDebugValuesID
= LiveDebugValues::ID
;
93 INITIALIZE_PASS(LiveDebugValues
, DEBUG_TYPE
, "Live DEBUG_VALUE analysis", false,
96 /// Default construct and initialize the pass.
97 LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID
) {
98 initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
100 std::unique_ptr
<LDVImpl
>(llvm::makeInstrRefBasedLiveDebugValues());
101 VarLocImpl
= std::unique_ptr
<LDVImpl
>(llvm::makeVarLocBasedLiveDebugValues());
104 bool LiveDebugValues::runOnMachineFunction(MachineFunction
&MF
) {
105 bool InstrRefBased
= MF
.useDebugInstrRef();
106 // Allow the user to force selection of InstrRef LDV.
107 InstrRefBased
|= ForceInstrRefLDV
;
109 TPC
= getAnalysisIfAvailable
<TargetPassConfig
>();
110 LDVImpl
*TheImpl
= &*VarLocImpl
;
112 MachineDominatorTree
*DomTree
= nullptr;
116 TheImpl
= &*InstrRefImpl
;
119 return TheImpl
->ExtendRanges(MF
, DomTree
, TPC
, InputBBLimit
,
123 bool llvm::debuginfoShouldUseDebugInstrRef(const Triple
&T
) {
124 // Enable by default on x86_64, disable if explicitly turned off on cmdline.
125 if (T
.getArch() == llvm::Triple::x86_64
&&
126 ValueTrackingVariableLocations
!= cl::boolOrDefault::BOU_FALSE
)
129 // Enable if explicitly requested on command line.
130 return ValueTrackingVariableLocations
== cl::boolOrDefault::BOU_TRUE
;