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