120bb9fe5SJeremy Morse //===- LiveDebugValues.cpp - Tracking Debug Value MIs ---------------------===//
220bb9fe5SJeremy Morse //
320bb9fe5SJeremy Morse // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
420bb9fe5SJeremy Morse // See https://llvm.org/LICENSE.txt for license information.
520bb9fe5SJeremy Morse // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
620bb9fe5SJeremy Morse //
720bb9fe5SJeremy Morse //===----------------------------------------------------------------------===//
820bb9fe5SJeremy Morse 
920bb9fe5SJeremy Morse #include "LiveDebugValues.h"
1020bb9fe5SJeremy Morse 
1120bb9fe5SJeremy Morse #include "llvm/CodeGen/MachineBasicBlock.h"
1220bb9fe5SJeremy Morse #include "llvm/CodeGen/MachineFrameInfo.h"
1320bb9fe5SJeremy Morse #include "llvm/CodeGen/MachineFunctionPass.h"
1420bb9fe5SJeremy Morse #include "llvm/CodeGen/Passes.h"
1520bb9fe5SJeremy Morse #include "llvm/InitializePasses.h"
1620bb9fe5SJeremy Morse #include "llvm/Pass.h"
1749555441SJeremy Morse #include "llvm/Support/CommandLine.h"
18121a49d8SJeremy Morse #include "llvm/Target/TargetMachine.h"
1920bb9fe5SJeremy Morse 
2020bb9fe5SJeremy Morse /// \file LiveDebugValues.cpp
2120bb9fe5SJeremy Morse ///
2220bb9fe5SJeremy Morse /// The LiveDebugValues pass extends the range of variable locations
2320bb9fe5SJeremy Morse /// (specified by DBG_VALUE instructions) from single blocks to successors
2420bb9fe5SJeremy Morse /// and any other code locations where the variable location is valid.
2520bb9fe5SJeremy Morse /// There are currently two implementations: the "VarLoc" implementation
2620bb9fe5SJeremy Morse /// explicitly tracks the location of a variable, while the "InstrRef"
2720bb9fe5SJeremy Morse /// implementation tracks the values defined by instructions through locations.
2820bb9fe5SJeremy Morse ///
2920bb9fe5SJeremy Morse /// This file implements neither; it merely registers the pass, allows the
3020bb9fe5SJeremy Morse /// user to pick which implementation will be used to propagate variable
3120bb9fe5SJeremy Morse /// locations.
3220bb9fe5SJeremy Morse 
3320bb9fe5SJeremy Morse #define DEBUG_TYPE "livedebugvalues"
3420bb9fe5SJeremy Morse 
3520bb9fe5SJeremy Morse using namespace llvm;
3620bb9fe5SJeremy Morse 
3749555441SJeremy Morse static cl::opt<bool>
3849555441SJeremy Morse     ForceInstrRefLDV("force-instr-ref-livedebugvalues", cl::Hidden,
3949555441SJeremy Morse                      cl::desc("Use instruction-ref based LiveDebugValues with "
4049555441SJeremy Morse                               "normal DBG_VALUE inputs"),
4149555441SJeremy Morse                      cl::init(false));
4249555441SJeremy Morse 
43*708cbda5SJeremy Morse // Options to prevent pathological compile-time behavior. If InputBBLimit and
44*708cbda5SJeremy Morse // InputDbgValueLimit are both exceeded, range extension is disabled.
45*708cbda5SJeremy Morse static cl::opt<unsigned> InputBBLimit(
46*708cbda5SJeremy Morse     "livedebugvalues-input-bb-limit",
47*708cbda5SJeremy Morse     cl::desc("Maximum input basic blocks before DBG_VALUE limit applies"),
48*708cbda5SJeremy Morse     cl::init(10000), cl::Hidden);
49*708cbda5SJeremy Morse static cl::opt<unsigned> InputDbgValueLimit(
50*708cbda5SJeremy Morse     "livedebugvalues-input-dbg-value-limit",
51*708cbda5SJeremy Morse     cl::desc(
52*708cbda5SJeremy Morse         "Maximum input DBG_VALUE insts supported by debug range extension"),
53*708cbda5SJeremy Morse     cl::init(50000), cl::Hidden);
54*708cbda5SJeremy Morse 
5520bb9fe5SJeremy Morse /// Generic LiveDebugValues pass. Calls through to VarLocBasedLDV or
5620bb9fe5SJeremy Morse /// InstrRefBasedLDV to perform location propagation, via the LDVImpl
5720bb9fe5SJeremy Morse /// base class.
5820bb9fe5SJeremy Morse class LiveDebugValues : public MachineFunctionPass {
5920bb9fe5SJeremy Morse public:
6020bb9fe5SJeremy Morse   static char ID;
6120bb9fe5SJeremy Morse 
6220bb9fe5SJeremy Morse   LiveDebugValues();
63121a49d8SJeremy Morse   ~LiveDebugValues() {
64121a49d8SJeremy Morse     if (TheImpl)
65121a49d8SJeremy Morse       delete TheImpl;
66121a49d8SJeremy Morse   }
6720bb9fe5SJeremy Morse 
6820bb9fe5SJeremy Morse   /// Calculate the liveness information for the given machine function.
6920bb9fe5SJeremy Morse   bool runOnMachineFunction(MachineFunction &MF) override;
7020bb9fe5SJeremy Morse 
7120bb9fe5SJeremy Morse   MachineFunctionProperties getRequiredProperties() const override {
7220bb9fe5SJeremy Morse     return MachineFunctionProperties().set(
7320bb9fe5SJeremy Morse         MachineFunctionProperties::Property::NoVRegs);
7420bb9fe5SJeremy Morse   }
7520bb9fe5SJeremy Morse 
7620bb9fe5SJeremy Morse   void getAnalysisUsage(AnalysisUsage &AU) const override {
7720bb9fe5SJeremy Morse     AU.setPreservesCFG();
7820bb9fe5SJeremy Morse     MachineFunctionPass::getAnalysisUsage(AU);
7920bb9fe5SJeremy Morse   }
8020bb9fe5SJeremy Morse 
8120bb9fe5SJeremy Morse private:
8220bb9fe5SJeremy Morse   LDVImpl *TheImpl;
83121a49d8SJeremy Morse   TargetPassConfig *TPC;
8420bb9fe5SJeremy Morse };
8520bb9fe5SJeremy Morse 
8620bb9fe5SJeremy Morse char LiveDebugValues::ID = 0;
8720bb9fe5SJeremy Morse 
8820bb9fe5SJeremy Morse char &llvm::LiveDebugValuesID = LiveDebugValues::ID;
8920bb9fe5SJeremy Morse 
9020bb9fe5SJeremy Morse INITIALIZE_PASS(LiveDebugValues, DEBUG_TYPE, "Live DEBUG_VALUE analysis", false,
9120bb9fe5SJeremy Morse                 false)
9220bb9fe5SJeremy Morse 
9320bb9fe5SJeremy Morse /// Default construct and initialize the pass.
9420bb9fe5SJeremy Morse LiveDebugValues::LiveDebugValues() : MachineFunctionPass(ID) {
9520bb9fe5SJeremy Morse   initializeLiveDebugValuesPass(*PassRegistry::getPassRegistry());
96121a49d8SJeremy Morse   TheImpl = nullptr;
9720bb9fe5SJeremy Morse }
9820bb9fe5SJeremy Morse 
9920bb9fe5SJeremy Morse bool LiveDebugValues::runOnMachineFunction(MachineFunction &MF) {
100121a49d8SJeremy Morse   if (!TheImpl) {
101121a49d8SJeremy Morse     TPC = getAnalysisIfAvailable<TargetPassConfig>();
102121a49d8SJeremy Morse 
103121a49d8SJeremy Morse     bool InstrRefBased = false;
104121a49d8SJeremy Morse     if (TPC) {
105121a49d8SJeremy Morse       auto &TM = TPC->getTM<TargetMachine>();
106121a49d8SJeremy Morse       InstrRefBased = TM.Options.ValueTrackingVariableLocations;
107121a49d8SJeremy Morse     }
108121a49d8SJeremy Morse 
10949555441SJeremy Morse     // Allow the user to force selection of InstrRef LDV.
11049555441SJeremy Morse     InstrRefBased |= ForceInstrRefLDV;
11149555441SJeremy Morse 
112121a49d8SJeremy Morse     if (InstrRefBased)
113121a49d8SJeremy Morse       TheImpl = llvm::makeInstrRefBasedLiveDebugValues();
114121a49d8SJeremy Morse     else
115121a49d8SJeremy Morse       TheImpl = llvm::makeVarLocBasedLiveDebugValues();
116121a49d8SJeremy Morse   }
117121a49d8SJeremy Morse 
118*708cbda5SJeremy Morse   return TheImpl->ExtendRanges(MF, TPC, InputBBLimit, InputDbgValueLimit);
11920bb9fe5SJeremy Morse }
120