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/MachineBasicBlock.h"
12 #include "llvm/CodeGen/MachineFrameInfo.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/InitializePasses.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Target/TargetMachine.h"
18 
19 /// \file LiveDebugValues.cpp
20 ///
21 /// The LiveDebugValues pass extends the range of variable locations
22 /// (specified by DBG_VALUE instructions) from single blocks to successors
23 /// and any other code locations where the variable location is valid.
24 /// There are currently two implementations: the "VarLoc" implementation
25 /// explicitly tracks the location of a variable, while the "InstrRef"
26 /// implementation tracks the values defined by instructions through locations.
27 ///
28 /// This file implements neither; it merely registers the pass, allows the
29 /// user to pick which implementation will be used to propagate variable
30 /// locations.
31 
32 #define DEBUG_TYPE "livedebugvalues"
33 
34 using namespace llvm;
35 
36 /// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or
37 /// InstrRefBasedLDV to perform location propagation, via the LDVImpl
38 /// base class.
39 class LiveDebugValues : public MachineFunctionPass {
40 public:
41   static char ID;
42 
43   LiveDebugValues();
44   ~LiveDebugValues() {
45     if (TheImpl)
46       delete TheImpl;
47   }
48 
49   /// Calculate the liveness information for the given machine function.
50   bool runOnMachineFunction(MachineFunction &MF) override;
51 
52   MachineFunctionProperties getRequiredProperties() const override {
53     return MachineFunctionProperties().set(
54         MachineFunctionProperties::Property::NoVRegs);
55   }
56 
57   void getAnalysisUsage(AnalysisUsage &AU) const override {
58     AU.setPreservesCFG();
59     MachineFunctionPass::getAnalysisUsage(AU);
60   }
61 
62 private:
63   LDVImpl *TheImpl;
64   TargetPassConfig *TPC;
65 };
66 
67 char LiveDebugValues::ID = 0;
68 
69 char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
70 
71 INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", false,
72                 false)
73 
74 /// Default construct and initialize the pass.
75 LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
76   initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
77   TheImpl = nullptr;
78 }
79 
80 bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
81   if (!TheImpl) {
82     TPC = getAnalysisIfAvailable<TargetPassConfig>();
83 
84     bool InstrRefBased = false;
85     if (TPC) {
86       auto &TM = TPC->getTM<TargetMachine>();
87       InstrRefBased = TM.Options.ValueTrackingVariableLocations;
88     }
89 
90     if (InstrRefBased)
91       TheImpl = llvm::makeInstrRefBasedLiveDebugValues();
92     else
93       TheImpl = llvm::makeVarLocBasedLiveDebugValues();
94   }
95 
96   return TheImpl->ExtendRanges(MF, TPC);
97 }
98