LLVM  mainline
DebugInfo.h
Go to the documentation of this file.
00001 //===- DebugInfo.h - Debug Information Helpers ------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a bunch of datatypes that are useful for creating and
00011 // walking debug info in LLVM IR form. They essentially provide wrappers around
00012 // the information in the global variables that's needed when constructing the
00013 // DWARF information.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_IR_DEBUGINFO_H
00018 #define LLVM_IR_DEBUGINFO_H
00019 
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/SmallPtrSet.h"
00022 #include "llvm/ADT/SmallVector.h"
00023 #include "llvm/ADT/StringRef.h"
00024 #include "llvm/ADT/iterator_range.h"
00025 #include "llvm/IR/DebugInfoMetadata.h"
00026 #include "llvm/Support/Casting.h"
00027 #include "llvm/Support/Dwarf.h"
00028 #include "llvm/Support/ErrorHandling.h"
00029 #include <iterator>
00030 
00031 namespace llvm {
00032 class Module;
00033 class DbgDeclareInst;
00034 class DbgValueInst;
00035 
00036 /// \brief Maps from type identifier to the actual MDNode.
00037 typedef DenseMap<const MDString *, DIType *> DITypeIdentifierMap;
00038 
00039 /// \brief Find subprogram that is enclosing this scope.
00040 DISubprogram *getDISubprogram(const MDNode *Scope);
00041 
00042 /// \brief Find debug info for a given function.
00043 ///
00044 /// \returns a valid subprogram, if found. Otherwise, return \c nullptr.
00045 DISubprogram *getDISubprogram(const Function *F);
00046 
00047 /// \brief Generate map by visiting all retained types.
00048 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
00049 
00050 /// \brief Strip debug info in the module if it exists.
00051 ///
00052 /// To do this, we remove all calls to the debugger intrinsics and any named
00053 /// metadata for debugging. We also remove debug locations for instructions.
00054 /// Return true if module is modified.
00055 bool StripDebugInfo(Module &M);
00056 bool stripDebugInfo(Function &F);
00057 
00058 /// \brief Return Debug Info Metadata Version by checking module flags.
00059 unsigned getDebugMetadataVersionFromModule(const Module &M);
00060 
00061 /// \brief Utility to find all debug info in a module.
00062 ///
00063 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
00064 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
00065 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
00066 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
00067 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
00068 /// used by the CUs.
00069 class DebugInfoFinder {
00070 public:
00071   DebugInfoFinder() : TypeMapInitialized(false) {}
00072 
00073   /// \brief Process entire module and collect debug info anchors.
00074   void processModule(const Module &M);
00075 
00076   /// \brief Process DbgDeclareInst.
00077   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
00078   /// \brief Process DbgValueInst.
00079   void processValue(const Module &M, const DbgValueInst *DVI);
00080   /// \brief Process debug info location.
00081   void processLocation(const Module &M, const DILocation *Loc);
00082 
00083   /// \brief Clear all lists.
00084   void reset();
00085 
00086 private:
00087   void InitializeTypeMap(const Module &M);
00088 
00089   void processType(DIType *DT);
00090   void processSubprogram(DISubprogram *SP);
00091   void processScope(DIScope *Scope);
00092   bool addCompileUnit(DICompileUnit *CU);
00093   bool addGlobalVariable(DIGlobalVariable *DIG);
00094   bool addSubprogram(DISubprogram *SP);
00095   bool addType(DIType *DT);
00096   bool addScope(DIScope *Scope);
00097 
00098 public:
00099   typedef SmallVectorImpl<DICompileUnit *>::const_iterator
00100       compile_unit_iterator;
00101   typedef SmallVectorImpl<DISubprogram *>::const_iterator subprogram_iterator;
00102   typedef SmallVectorImpl<DIGlobalVariable *>::const_iterator
00103       global_variable_iterator;
00104   typedef SmallVectorImpl<DIType *>::const_iterator type_iterator;
00105   typedef SmallVectorImpl<DIScope *>::const_iterator scope_iterator;
00106 
00107   iterator_range<compile_unit_iterator> compile_units() const {
00108     return make_range(CUs.begin(), CUs.end());
00109   }
00110 
00111   iterator_range<subprogram_iterator> subprograms() const {
00112     return make_range(SPs.begin(), SPs.end());
00113   }
00114 
00115   iterator_range<global_variable_iterator> global_variables() const {
00116     return make_range(GVs.begin(), GVs.end());
00117   }
00118 
00119   iterator_range<type_iterator> types() const {
00120     return make_range(TYs.begin(), TYs.end());
00121   }
00122 
00123   iterator_range<scope_iterator> scopes() const {
00124     return make_range(Scopes.begin(), Scopes.end());
00125   }
00126 
00127   unsigned compile_unit_count() const { return CUs.size(); }
00128   unsigned global_variable_count() const { return GVs.size(); }
00129   unsigned subprogram_count() const { return SPs.size(); }
00130   unsigned type_count() const { return TYs.size(); }
00131   unsigned scope_count() const { return Scopes.size(); }
00132 
00133 private:
00134   SmallVector<DICompileUnit *, 8> CUs;
00135   SmallVector<DISubprogram *, 8> SPs;
00136   SmallVector<DIGlobalVariable *, 8> GVs;
00137   SmallVector<DIType *, 8> TYs;
00138   SmallVector<DIScope *, 8> Scopes;
00139   SmallPtrSet<const MDNode *, 64> NodesSeen;
00140   DITypeIdentifierMap TypeIdentifierMap;
00141 
00142   /// \brief Specify if TypeIdentifierMap is initialized.
00143   bool TypeMapInitialized;
00144 };
00145 
00146 } // end namespace llvm
00147 
00148 #endif