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 Find underlying composite type.
00048 DICompositeTypeBase *getDICompositeType(DIType *T);
00049 
00050 /// \brief Generate map by visiting all retained types.
00051 DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes);
00052 
00053 /// \brief Strip debug info in the module if it exists.
00054 ///
00055 /// To do this, we remove all calls to the debugger intrinsics and any named
00056 /// metadata for debugging. We also remove debug locations for instructions.
00057 /// Return true if module is modified.
00058 bool StripDebugInfo(Module &M);
00059 bool stripDebugInfo(Function &F);
00060 
00061 /// \brief Return Debug Info Metadata Version by checking module flags.
00062 unsigned getDebugMetadataVersionFromModule(const Module &M);
00063 
00064 /// \brief Utility to find all debug info in a module.
00065 ///
00066 /// DebugInfoFinder tries to list all debug info MDNodes used in a module. To
00067 /// list debug info MDNodes used by an instruction, DebugInfoFinder uses
00068 /// processDeclare, processValue and processLocation to handle DbgDeclareInst,
00069 /// DbgValueInst and DbgLoc attached to instructions. processModule will go
00070 /// through all DICompileUnits in llvm.dbg.cu and list debug info MDNodes
00071 /// used by the CUs.
00072 class DebugInfoFinder {
00073 public:
00074   DebugInfoFinder() : TypeMapInitialized(false) {}
00075 
00076   /// \brief Process entire module and collect debug info anchors.
00077   void processModule(const Module &M);
00078 
00079   /// \brief Process DbgDeclareInst.
00080   void processDeclare(const Module &M, const DbgDeclareInst *DDI);
00081   /// \brief Process DbgValueInst.
00082   void processValue(const Module &M, const DbgValueInst *DVI);
00083   /// \brief Process debug info location.
00084   void processLocation(const Module &M, const DILocation *Loc);
00085 
00086   /// \brief Clear all lists.
00087   void reset();
00088 
00089 private:
00090   void InitializeTypeMap(const Module &M);
00091 
00092   void processType(DIType *DT);
00093   void processSubprogram(DISubprogram *SP);
00094   void processScope(DIScope *Scope);
00095   bool addCompileUnit(DICompileUnit *CU);
00096   bool addGlobalVariable(DIGlobalVariable *DIG);
00097   bool addSubprogram(DISubprogram *SP);
00098   bool addType(DIType *DT);
00099   bool addScope(DIScope *Scope);
00100 
00101 public:
00102   typedef SmallVectorImpl<DICompileUnit *>::const_iterator
00103       compile_unit_iterator;
00104   typedef SmallVectorImpl<DISubprogram *>::const_iterator subprogram_iterator;
00105   typedef SmallVectorImpl<DIGlobalVariable *>::const_iterator
00106       global_variable_iterator;
00107   typedef SmallVectorImpl<DIType *>::const_iterator type_iterator;
00108   typedef SmallVectorImpl<DIScope *>::const_iterator scope_iterator;
00109 
00110   iterator_range<compile_unit_iterator> compile_units() const {
00111     return iterator_range<compile_unit_iterator>(CUs.begin(), CUs.end());
00112   }
00113 
00114   iterator_range<subprogram_iterator> subprograms() const {
00115     return iterator_range<subprogram_iterator>(SPs.begin(), SPs.end());
00116   }
00117 
00118   iterator_range<global_variable_iterator> global_variables() const {
00119     return iterator_range<global_variable_iterator>(GVs.begin(), GVs.end());
00120   }
00121 
00122   iterator_range<type_iterator> types() const {
00123     return iterator_range<type_iterator>(TYs.begin(), TYs.end());
00124   }
00125 
00126   iterator_range<scope_iterator> scopes() const {
00127     return iterator_range<scope_iterator>(Scopes.begin(), Scopes.end());
00128   }
00129 
00130   unsigned compile_unit_count() const { return CUs.size(); }
00131   unsigned global_variable_count() const { return GVs.size(); }
00132   unsigned subprogram_count() const { return SPs.size(); }
00133   unsigned type_count() const { return TYs.size(); }
00134   unsigned scope_count() const { return Scopes.size(); }
00135 
00136 private:
00137   SmallVector<DICompileUnit *, 8> CUs;
00138   SmallVector<DISubprogram *, 8> SPs;
00139   SmallVector<DIGlobalVariable *, 8> GVs;
00140   SmallVector<DIType *, 8> TYs;
00141   SmallVector<DIScope *, 8> Scopes;
00142   SmallPtrSet<const MDNode *, 64> NodesSeen;
00143   DITypeIdentifierMap TypeIdentifierMap;
00144 
00145   /// \brief Specify if TypeIdentifierMap is initialized.
00146   bool TypeMapInitialized;
00147 };
00148 
00149 DenseMap<const Function *, DISubprogram *> makeSubprogramMap(const Module &M);
00150 
00151 } // end namespace llvm
00152 
00153 #endif