1 //===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the helper classes used to build and interpret debug
11 // information in LLVM IR form.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/DebugInfo.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DIBuilder.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/GVMaterializer.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Dwarf.h"
32 #include "llvm/Support/raw_ostream.h"
34 using namespace llvm::dwarf;
36 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
37 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
38 return LocalScope->getSubprogram();
42 DISubprogram *llvm::getDISubprogram(const Function *F) {
43 // We look for the first instr that has a debug annotation leading back to F.
45 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
46 return Inst.getDebugLoc();
50 DebugLoc DLoc = Inst->getDebugLoc();
51 const MDNode *Scope = DLoc.getInlinedAtScope();
52 auto *Subprogram = getDISubprogram(Scope);
53 return Subprogram->describes(F) ? Subprogram : nullptr;
60 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
61 DITypeIdentifierMap Map;
62 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
63 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
64 DINodeArray Retain = CU->getRetainedTypes();
65 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
66 if (!isa<DICompositeType>(Retain[Ti]))
68 auto *Ty = cast<DICompositeType>(Retain[Ti]);
69 if (MDString *TypeId = Ty->getRawIdentifier()) {
70 // Definition has priority over declaration.
71 // Try to insert (TypeId, Ty) to Map.
72 std::pair<DITypeIdentifierMap::iterator, bool> P =
73 Map.insert(std::make_pair(TypeId, Ty));
74 // If TypeId already exists in Map and this is a definition, replace
75 // whatever we had (declaration or definition) with the definition.
76 if (!P.second && !Ty->isForwardDecl())
84 //===----------------------------------------------------------------------===//
85 // DebugInfoFinder implementations.
86 //===----------------------------------------------------------------------===//
88 void DebugInfoFinder::reset() {
95 TypeIdentifierMap.clear();
96 TypeMapInitialized = false;
99 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
100 if (!TypeMapInitialized)
101 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
102 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
103 TypeMapInitialized = true;
107 void DebugInfoFinder::processModule(const Module &M) {
108 InitializeTypeMap(M);
109 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
110 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
111 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
113 for (auto *DIG : CU->getGlobalVariables()) {
114 if (addGlobalVariable(DIG)) {
115 processScope(DIG->getScope());
116 processType(DIG->getType().resolve(TypeIdentifierMap));
119 for (auto *SP : CU->getSubprograms())
120 processSubprogram(SP);
121 for (auto *ET : CU->getEnumTypes())
123 for (auto *RT : CU->getRetainedTypes())
125 for (auto *Import : CU->getImportedEntities()) {
126 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
127 if (auto *T = dyn_cast<DIType>(Entity))
129 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
130 processSubprogram(SP);
131 else if (auto *NS = dyn_cast<DINamespace>(Entity))
132 processScope(NS->getScope());
133 else if (auto *M = dyn_cast<DIModule>(Entity))
134 processScope(M->getScope());
140 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
143 InitializeTypeMap(M);
144 processScope(Loc->getScope());
145 processLocation(M, Loc->getInlinedAt());
148 void DebugInfoFinder::processType(DIType *DT) {
151 processScope(DT->getScope().resolve(TypeIdentifierMap));
152 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
153 for (DITypeRef Ref : ST->getTypeArray())
154 processType(Ref.resolve(TypeIdentifierMap));
157 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
158 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
159 for (Metadata *D : DCT->getElements()) {
160 if (auto *T = dyn_cast<DIType>(D))
162 else if (auto *SP = dyn_cast<DISubprogram>(D))
163 processSubprogram(SP);
167 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
168 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
172 void DebugInfoFinder::processScope(DIScope *Scope) {
175 if (auto *Ty = dyn_cast<DIType>(Scope)) {
179 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
183 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
184 processSubprogram(SP);
187 if (!addScope(Scope))
189 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
190 processScope(LB->getScope());
191 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
192 processScope(NS->getScope());
193 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
194 processScope(M->getScope());
198 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
199 if (!addSubprogram(SP))
201 processScope(SP->getScope().resolve(TypeIdentifierMap));
202 processType(SP->getType());
203 for (auto *Element : SP->getTemplateParams()) {
204 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
205 processType(TType->getType().resolve(TypeIdentifierMap));
206 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
207 processType(TVal->getType().resolve(TypeIdentifierMap));
212 void DebugInfoFinder::processDeclare(const Module &M,
213 const DbgDeclareInst *DDI) {
214 auto *N = dyn_cast<MDNode>(DDI->getVariable());
217 InitializeTypeMap(M);
219 auto *DV = dyn_cast<DILocalVariable>(N);
223 if (!NodesSeen.insert(DV).second)
225 processScope(DV->getScope());
226 processType(DV->getType().resolve(TypeIdentifierMap));
229 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
230 auto *N = dyn_cast<MDNode>(DVI->getVariable());
233 InitializeTypeMap(M);
235 auto *DV = dyn_cast<DILocalVariable>(N);
239 if (!NodesSeen.insert(DV).second)
241 processScope(DV->getScope());
242 processType(DV->getType().resolve(TypeIdentifierMap));
245 bool DebugInfoFinder::addType(DIType *DT) {
249 if (!NodesSeen.insert(DT).second)
252 TYs.push_back(const_cast<DIType *>(DT));
256 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
259 if (!NodesSeen.insert(CU).second)
266 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
270 if (!NodesSeen.insert(DIG).second)
277 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
281 if (!NodesSeen.insert(SP).second)
288 bool DebugInfoFinder::addScope(DIScope *Scope) {
291 // FIXME: Ocaml binding generates a scope with no content, we treat it
293 if (Scope->getNumOperands() == 0)
295 if (!NodesSeen.insert(Scope).second)
297 Scopes.push_back(Scope);
301 bool llvm::stripDebugInfo(Function &F) {
302 bool Changed = false;
303 if (F.getSubprogram()) {
305 F.setSubprogram(nullptr);
307 for (BasicBlock &BB : F) {
308 for (Instruction &I : BB) {
309 if (I.getDebugLoc()) {
311 I.setDebugLoc(DebugLoc());
318 bool llvm::StripDebugInfo(Module &M) {
319 bool Changed = false;
321 // Remove all of the calls to the debugger intrinsics, and remove them from
323 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
324 while (!Declare->use_empty()) {
325 CallInst *CI = cast<CallInst>(Declare->user_back());
326 CI->eraseFromParent();
328 Declare->eraseFromParent();
332 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
333 while (!DbgVal->use_empty()) {
334 CallInst *CI = cast<CallInst>(DbgVal->user_back());
335 CI->eraseFromParent();
337 DbgVal->eraseFromParent();
341 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
342 NME = M.named_metadata_end(); NMI != NME;) {
343 NamedMDNode *NMD = &*NMI;
345 if (NMD->getName().startswith("llvm.dbg.")) {
346 NMD->eraseFromParent();
351 for (Function &F : M)
352 Changed |= stripDebugInfo(F);
354 if (GVMaterializer *Materializer = M.getMaterializer())
355 Materializer->setStripDebugInfo();
360 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
361 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
362 M.getModuleFlag("Debug Info Version")))
363 return Val->getZExtValue();