1 //===- LiveDebugVariables.h - Tracking debug info variables -----*- C++ -*-===// 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 // This file provides the interface to the LiveDebugVariables analysis. 10 // 11 // The analysis removes DBG_VALUE instructions for virtual registers and tracks 12 // live user variables in a data structure that can be updated during register 13 // allocation. 14 // 15 // After register allocation new DBG_VALUE instructions are emitted to reflect 16 // the new locations of user variables. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #ifndef LLVM_CODEGEN_LIVEDEBUGVARIABLES_H 21 #define LLVM_CODEGEN_LIVEDEBUGVARIABLES_H 22 23 #include "llvm/CodeGen/MachineFunctionPass.h" 24 #include "llvm/IR/PassManager.h" 25 #include "llvm/Support/Compiler.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include <memory> 28 29 namespace llvm { 30 31 template <typename T> class ArrayRef; 32 class LiveIntervals; 33 class VirtRegMap; 34 35 class LiveDebugVariables { 36 37 public: 38 class LDVImpl; 39 LiveDebugVariables(); 40 ~LiveDebugVariables(); 41 LiveDebugVariables(LiveDebugVariables &&); 42 43 void analyze(MachineFunction &MF, LiveIntervals *LIS); 44 /// splitRegister - Move any user variables in OldReg to the live ranges in 45 /// NewRegs where they are live. Mark the values as unavailable where no new 46 /// register is live. 47 void splitRegister(Register OldReg, ArrayRef<Register> NewRegs, 48 LiveIntervals &LIS); 49 50 /// emitDebugValues - Emit new DBG_VALUE instructions reflecting the changes 51 /// that happened during register allocation. 52 /// @param VRM Rename virtual registers according to map. 53 void emitDebugValues(VirtRegMap *VRM); 54 55 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 56 /// dump - Print data structures to dbgs(). 57 void dump() const; 58 #endif 59 60 void print(raw_ostream &OS) const; 61 62 void releaseMemory(); 63 64 bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA, 65 MachineFunctionAnalysisManager::Invalidator &Inv); 66 67 private: 68 std::unique_ptr<LDVImpl> PImpl; 69 }; 70 71 class LiveDebugVariablesWrapperLegacy : public MachineFunctionPass { 72 std::unique_ptr<LiveDebugVariables> Impl; 73 74 public: 75 static char ID; // Pass identification, replacement for typeid 76 77 LiveDebugVariablesWrapperLegacy(); 78 79 bool runOnMachineFunction(MachineFunction &) override; 80 getLDV()81 LiveDebugVariables &getLDV() { return *Impl; } getLDV()82 const LiveDebugVariables &getLDV() const { return *Impl; } 83 releaseMemory()84 void releaseMemory() override { 85 if (Impl) 86 Impl->releaseMemory(); 87 } 88 void getAnalysisUsage(AnalysisUsage &) const override; 89 getSetProperties()90 MachineFunctionProperties getSetProperties() const override { 91 return MachineFunctionProperties().setTracksDebugUserValues(); 92 } 93 }; 94 95 class LiveDebugVariablesAnalysis 96 : public AnalysisInfoMixin<LiveDebugVariablesAnalysis> { 97 friend AnalysisInfoMixin<LiveDebugVariablesAnalysis>; 98 static AnalysisKey Key; 99 100 public: 101 using Result = LiveDebugVariables; 102 getSetProperties()103 MachineFunctionProperties getSetProperties() const { 104 return MachineFunctionProperties().setTracksDebugUserValues(); 105 } 106 107 Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM); 108 }; 109 110 class LiveDebugVariablesPrinterPass 111 : public PassInfoMixin<LiveDebugVariablesPrinterPass> { 112 raw_ostream &OS; 113 114 public: LiveDebugVariablesPrinterPass(raw_ostream & OS)115 LiveDebugVariablesPrinterPass(raw_ostream &OS) : OS(OS) {} 116 117 PreservedAnalyses run(MachineFunction &MF, 118 MachineFunctionAnalysisManager &MFAM); 119 }; 120 } // end namespace llvm 121 122 #endif // LLVM_CODEGEN_LIVEDEBUGVARIABLES_H 123