Home | History | Annotate | Line # | Download | only in IR
      1 //===- DebugInfo.h - Debug Information Helpers ------------------*- 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 defines a bunch of datatypes that are useful for creating and
     10 // walking debug info in LLVM IR form. They essentially provide wrappers around
     11 // the information in the global variables that's needed when constructing the
     12 // DWARF information.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_IR_DEBUGINFO_H
     17 #define LLVM_IR_DEBUGINFO_H
     18 
     19 #include "llvm/ADT/STLExtras.h"
     20 #include "llvm/ADT/SmallPtrSet.h"
     21 #include "llvm/ADT/SmallVector.h"
     22 #include "llvm/ADT/TinyPtrVector.h"
     23 #include "llvm/ADT/iterator_range.h"
     24 #include "llvm/IR/DebugInfoMetadata.h"
     25 
     26 namespace llvm {
     27 
     28 class DbgDeclareInst;
     29 class DbgValueInst;
     30 class DbgVariableIntrinsic;
     31 class Instruction;
     32 class Module;
     33 
     34 /// Finds all intrinsics declaring local variables as living in the memory that
     35 /// 'V' points to. This may include a mix of dbg.declare and
     36 /// dbg.addr intrinsics.
     37 TinyPtrVector<DbgVariableIntrinsic *> FindDbgAddrUses(Value *V);
     38 
     39 /// Like \c FindDbgAddrUses, but only returns dbg.declare intrinsics, not
     40 /// dbg.addr.
     41 TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V);
     42 
     43 /// Finds the llvm.dbg.value intrinsics describing a value.
     44 void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V);
     45 
     46 /// Finds the debug info intrinsics describing a value.
     47 void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V);
     48 
     49 /// Find subprogram that is enclosing this scope.
     50 DISubprogram *getDISubprogram(const MDNode *Scope);
     51 
     52 /// Strip debug info in the module if it exists.
     53 ///
     54 /// To do this, we remove all calls to the debugger intrinsics and any named
     55 /// metadata for debugging. We also remove debug locations for instructions.
     56 /// Return true if module is modified.
     57 bool StripDebugInfo(Module &M);
     58 bool stripDebugInfo(Function &F);
     59 
     60 /// Downgrade the debug info in a module to contain only line table information.
     61 ///
     62 /// In order to convert debug info to what -gline-tables-only would have
     63 /// created, this does the following:
     64 ///   1) Delete all debug intrinsics.
     65 ///   2) Delete all non-CU named metadata debug info nodes.
     66 ///   3) Create new DebugLocs for each instruction.
     67 ///   4) Create a new CU debug info, and similarly for every metadata node
     68 ///      that's reachable from the CU debug info.
     69 ///   All debug type metadata nodes are unreachable and garbage collected.
     70 bool stripNonLineTableDebugInfo(Module &M);
     71 
     72 /// Update the debug locations contained within the MD_loop metadata attached
     73 /// to the instruction \p I, if one exists. \p Updater is applied to each debug
     74 /// location in the MD_loop metadata: the returned value is included in the
     75 /// updated loop metadata node if it is non-null.
     76 void updateLoopMetadataDebugLocations(
     77     Instruction &I, function_ref<DILocation *(const DILocation &)> Updater);
     78 
     79 /// Return Debug Info Metadata Version by checking module flags.
     80 unsigned getDebugMetadataVersionFromModule(const Module &M);
     81 
     82 /// Utility to find all debug info in a module.
     83 ///
     84 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
     85 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
     86 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
     87 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
     88 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
     89 /// used by the CUs.
     90 class DebugInfoFinder {
     91 public:
     92   /// Process entire module and collect debug info anchors.
     93   void processModule(const Module &M);
     94   /// Process a single instruction and collect debug info anchors.
     95   void processInstruction(const Module &M, const Instruction &I);
     96 
     97   /// Process DbgVariableIntrinsic.
     98   void processVariable(const Module &M, const DbgVariableIntrinsic &DVI);
     99   /// Process debug info location.
    100   void processLocation(const Module &M, const DILocation *Loc);
    101 
    102   /// Process subprogram.
    103   void processSubprogram(DISubprogram *SP);
    104 
    105   /// Clear all lists.
    106   void reset();
    107 
    108 private:
    109   void InitializeTypeMap(const Module &M);
    110 
    111   void processCompileUnit(DICompileUnit *CU);
    112   void processScope(DIScope *Scope);
    113   void processType(DIType *DT);
    114   bool addCompileUnit(DICompileUnit *CU);
    115   bool addGlobalVariable(DIGlobalVariableExpression *DIG);
    116   bool addScope(DIScope *Scope);
    117   bool addSubprogram(DISubprogram *SP);
    118   bool addType(DIType *DT);
    119 
    120 public:
    121   using compile_unit_iterator =
    122       SmallVectorImpl<DICompileUnit *>::const_iterator;
    123   using subprogram_iterator = SmallVectorImpl<DISubprogram *>::const_iterator;
    124   using global_variable_expression_iterator =
    125       SmallVectorImpl<DIGlobalVariableExpression *>::const_iterator;
    126   using type_iterator = SmallVectorImpl<DIType *>::const_iterator;
    127   using scope_iterator = SmallVectorImpl<DIScope *>::const_iterator;
    128 
    129   iterator_range<compile_unit_iterator> compile_units() const {
    130     return make_range(CUs.begin(), CUs.end());
    131   }
    132 
    133   iterator_range<subprogram_iterator> subprograms() const {
    134     return make_range(SPs.begin(), SPs.end());
    135   }
    136 
    137   iterator_range<global_variable_expression_iterator> global_variables() const {
    138     return make_range(GVs.begin(), GVs.end());
    139   }
    140 
    141   iterator_range<type_iterator> types() const {
    142     return make_range(TYs.begin(), TYs.end());
    143   }
    144 
    145   iterator_range<scope_iterator> scopes() const {
    146     return make_range(Scopes.begin(), Scopes.end());
    147   }
    148 
    149   unsigned compile_unit_count() const { return CUs.size(); }
    150   unsigned global_variable_count() const { return GVs.size(); }
    151   unsigned subprogram_count() const { return SPs.size(); }
    152   unsigned type_count() const { return TYs.size(); }
    153   unsigned scope_count() const { return Scopes.size(); }
    154 
    155 private:
    156   SmallVector<DICompileUnit *, 8> CUs;
    157   SmallVector<DISubprogram *, 8> SPs;
    158   SmallVector<DIGlobalVariableExpression *, 8> GVs;
    159   SmallVector<DIType *, 8> TYs;
    160   SmallVector<DIScope *, 8> Scopes;
    161   SmallPtrSet<const MDNode *, 32> NodesSeen;
    162 };
    163 
    164 } // end namespace llvm
    165 
    166 #endif // LLVM_IR_DEBUGINFO_H
    167