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