xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/LiveDebugValues/LiveDebugValues.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
1e8d8bef9SDimitry Andric //===- LiveDebugValues.cpp - Tracking Debug Value MIs ---------------------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric 
9e8d8bef9SDimitry Andric #include "LiveDebugValues.h"
10e8d8bef9SDimitry Andric 
11e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
12e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
13e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
14e8d8bef9SDimitry Andric #include "llvm/CodeGen/Passes.h"
15e8d8bef9SDimitry Andric #include "llvm/InitializePasses.h"
16e8d8bef9SDimitry Andric #include "llvm/Pass.h"
17fe6060f1SDimitry Andric #include "llvm/Support/CommandLine.h"
18e8d8bef9SDimitry Andric #include "llvm/Target/TargetMachine.h"
19e8d8bef9SDimitry Andric 
20e8d8bef9SDimitry Andric /// \file LiveDebugValues.cpp
21e8d8bef9SDimitry Andric ///
22e8d8bef9SDimitry Andric /// The LiveDebugValues pass extends the range of variable locations
23e8d8bef9SDimitry Andric /// (specified by DBG_VALUE instructions) from single blocks to successors
24e8d8bef9SDimitry Andric /// and any other code locations where the variable location is valid.
25e8d8bef9SDimitry Andric /// There are currently two implementations: the "VarLoc" implementation
26e8d8bef9SDimitry Andric /// explicitly tracks the location of a variable, while the "InstrRef"
27e8d8bef9SDimitry Andric /// implementation tracks the values defined by instructions through locations.
28e8d8bef9SDimitry Andric ///
29e8d8bef9SDimitry Andric /// This file implements neither; it merely registers the pass, allows the
30e8d8bef9SDimitry Andric /// user to pick which implementation will be used to propagate variable
31e8d8bef9SDimitry Andric /// locations.
32e8d8bef9SDimitry Andric 
33e8d8bef9SDimitry Andric #define DEBUG_TYPE "livedebugvalues"
34e8d8bef9SDimitry Andric 
35e8d8bef9SDimitry Andric using namespace llvm;
36e8d8bef9SDimitry Andric 
37fe6060f1SDimitry Andric static cl::opt<bool>
38fe6060f1SDimitry Andric     ForceInstrRefLDV("force-instr-ref-livedebugvalues", cl::Hidden,
39fe6060f1SDimitry Andric                      cl::desc("Use instruction-ref based LiveDebugValues with "
40fe6060f1SDimitry Andric                               "normal DBG_VALUE inputs"),
41fe6060f1SDimitry Andric                      cl::init(false));
42fe6060f1SDimitry Andric 
43*349cc55cSDimitry Andric // Options to prevent pathological compile-time behavior. If InputBBLimit and
44*349cc55cSDimitry Andric // InputDbgValueLimit are both exceeded, range extension is disabled.
45*349cc55cSDimitry Andric static cl::opt<unsigned> InputBBLimit(
46*349cc55cSDimitry Andric     "livedebugvalues-input-bb-limit",
47*349cc55cSDimitry Andric     cl::desc("Maximum input basic blocks before DBG_VALUE limit applies"),
48*349cc55cSDimitry Andric     cl::init(10000), cl::Hidden);
49*349cc55cSDimitry Andric static cl::opt<unsigned> InputDbgValueLimit(
50*349cc55cSDimitry Andric     "livedebugvalues-input-dbg-value-limit",
51*349cc55cSDimitry Andric     cl::desc(
52*349cc55cSDimitry Andric         "Maximum input DBG_VALUE insts supported by debug range extension"),
53*349cc55cSDimitry Andric     cl::init(50000), cl::Hidden);
54*349cc55cSDimitry Andric 
55*349cc55cSDimitry Andric namespace {
56e8d8bef9SDimitry Andric /// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or
57e8d8bef9SDimitry Andric /// InstrRefBasedLDV to perform location propagation, via the LDVImpl
58e8d8bef9SDimitry Andric /// base class.
59e8d8bef9SDimitry Andric class LiveDebugValues : public MachineFunctionPass {
60e8d8bef9SDimitry Andric public:
61e8d8bef9SDimitry Andric   static char ID;
62e8d8bef9SDimitry Andric 
63e8d8bef9SDimitry Andric   LiveDebugValues();
64*349cc55cSDimitry Andric   ~LiveDebugValues() {}
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric   /// Calculate the liveness information for the given machine function.
67e8d8bef9SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
68e8d8bef9SDimitry Andric 
69e8d8bef9SDimitry Andric   MachineFunctionProperties getRequiredProperties() const override {
70e8d8bef9SDimitry Andric     return MachineFunctionProperties().set(
71e8d8bef9SDimitry Andric         MachineFunctionProperties::Property::NoVRegs);
72e8d8bef9SDimitry Andric   }
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
75e8d8bef9SDimitry Andric     AU.setPreservesCFG();
76e8d8bef9SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
77e8d8bef9SDimitry Andric   }
78e8d8bef9SDimitry Andric 
79e8d8bef9SDimitry Andric private:
80*349cc55cSDimitry Andric   std::unique_ptr<LDVImpl> InstrRefImpl;
81*349cc55cSDimitry Andric   std::unique_ptr<LDVImpl> VarLocImpl;
82e8d8bef9SDimitry Andric   TargetPassConfig *TPC;
83*349cc55cSDimitry Andric   MachineDominatorTree MDT;
84e8d8bef9SDimitry Andric };
85*349cc55cSDimitry Andric } // namespace
86e8d8bef9SDimitry Andric 
87e8d8bef9SDimitry Andric char LiveDebugValues::ID = 0;
88e8d8bef9SDimitry Andric 
89e8d8bef9SDimitry Andric char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
90e8d8bef9SDimitry Andric 
91e8d8bef9SDimitry Andric INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", false,
92e8d8bef9SDimitry Andric                 false)
93e8d8bef9SDimitry Andric 
94e8d8bef9SDimitry Andric /// Default construct and initialize the pass.
95e8d8bef9SDimitry Andric LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
96e8d8bef9SDimitry Andric   initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
97*349cc55cSDimitry Andric   InstrRefImpl =
98*349cc55cSDimitry Andric       std::unique_ptr<LDVImpl>(llvm::makeInstrRefBasedLiveDebugValues());
99*349cc55cSDimitry Andric   VarLocImpl = std::unique_ptr<LDVImpl>(llvm::makeVarLocBasedLiveDebugValues());
100e8d8bef9SDimitry Andric }
101e8d8bef9SDimitry Andric 
102e8d8bef9SDimitry Andric bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
103*349cc55cSDimitry Andric   bool InstrRefBased = MF.useDebugInstrRef();
104fe6060f1SDimitry Andric   // Allow the user to force selection of InstrRef LDV.
105fe6060f1SDimitry Andric   InstrRefBased |= ForceInstrRefLDV;
106fe6060f1SDimitry Andric 
107*349cc55cSDimitry Andric   TPC = getAnalysisIfAvailable<TargetPassConfig>();
108*349cc55cSDimitry Andric   LDVImpl *TheImpl = &*VarLocImpl;
109*349cc55cSDimitry Andric 
110*349cc55cSDimitry Andric   MachineDominatorTree *DomTree = nullptr;
111*349cc55cSDimitry Andric   if (InstrRefBased) {
112*349cc55cSDimitry Andric     DomTree = &MDT;
113*349cc55cSDimitry Andric     MDT.calculate(MF);
114*349cc55cSDimitry Andric     TheImpl = &*InstrRefImpl;
115e8d8bef9SDimitry Andric   }
116e8d8bef9SDimitry Andric 
117*349cc55cSDimitry Andric   return TheImpl->ExtendRanges(MF, DomTree, TPC, InputBBLimit,
118*349cc55cSDimitry Andric                                InputDbgValueLimit);
119e8d8bef9SDimitry Andric }
120