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/IR/Constants.h"
20 #include "llvm/IR/DIBuilder.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/GVMaterializer.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/raw_ostream.h"
32 using namespace llvm::dwarf;
34 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
35 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
36 return LocalScope->getSubprogram();
40 //===----------------------------------------------------------------------===//
41 // DebugInfoFinder implementations.
42 //===----------------------------------------------------------------------===//
44 void DebugInfoFinder::reset() {
53 void DebugInfoFinder::processModule(const Module &M) {
54 for (auto *CU : M.debug_compile_units()) {
56 for (auto *DIG : CU->getGlobalVariables()) {
57 if (addGlobalVariable(DIG)) {
58 processScope(DIG->getScope());
59 processType(DIG->getType().resolve());
62 for (auto *ET : CU->getEnumTypes())
64 for (auto *RT : CU->getRetainedTypes())
65 if (auto *T = dyn_cast<DIType>(RT))
68 processSubprogram(cast<DISubprogram>(RT));
69 for (auto *Import : CU->getImportedEntities()) {
70 auto *Entity = Import->getEntity().resolve();
71 if (auto *T = dyn_cast<DIType>(Entity))
73 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
74 processSubprogram(SP);
75 else if (auto *NS = dyn_cast<DINamespace>(Entity))
76 processScope(NS->getScope());
77 else if (auto *M = dyn_cast<DIModule>(Entity))
78 processScope(M->getScope());
81 for (auto &F : M.functions())
82 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
83 processSubprogram(SP);
86 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
89 processScope(Loc->getScope());
90 processLocation(M, Loc->getInlinedAt());
93 void DebugInfoFinder::processType(DIType *DT) {
96 processScope(DT->getScope().resolve());
97 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
98 for (DITypeRef Ref : ST->getTypeArray())
99 processType(Ref.resolve());
102 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
103 processType(DCT->getBaseType().resolve());
104 for (Metadata *D : DCT->getElements()) {
105 if (auto *T = dyn_cast<DIType>(D))
107 else if (auto *SP = dyn_cast<DISubprogram>(D))
108 processSubprogram(SP);
112 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
113 processType(DDT->getBaseType().resolve());
117 void DebugInfoFinder::processScope(DIScope *Scope) {
120 if (auto *Ty = dyn_cast<DIType>(Scope)) {
124 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
128 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
129 processSubprogram(SP);
132 if (!addScope(Scope))
134 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
135 processScope(LB->getScope());
136 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
137 processScope(NS->getScope());
138 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
139 processScope(M->getScope());
143 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
144 if (!addSubprogram(SP))
146 processScope(SP->getScope().resolve());
147 processType(SP->getType());
148 for (auto *Element : SP->getTemplateParams()) {
149 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
150 processType(TType->getType().resolve());
151 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
152 processType(TVal->getType().resolve());
157 void DebugInfoFinder::processDeclare(const Module &M,
158 const DbgDeclareInst *DDI) {
159 auto *N = dyn_cast<MDNode>(DDI->getVariable());
163 auto *DV = dyn_cast<DILocalVariable>(N);
167 if (!NodesSeen.insert(DV).second)
169 processScope(DV->getScope());
170 processType(DV->getType().resolve());
173 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
174 auto *N = dyn_cast<MDNode>(DVI->getVariable());
178 auto *DV = dyn_cast<DILocalVariable>(N);
182 if (!NodesSeen.insert(DV).second)
184 processScope(DV->getScope());
185 processType(DV->getType().resolve());
188 bool DebugInfoFinder::addType(DIType *DT) {
192 if (!NodesSeen.insert(DT).second)
195 TYs.push_back(const_cast<DIType *>(DT));
199 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
202 if (!NodesSeen.insert(CU).second)
209 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
213 if (!NodesSeen.insert(DIG).second)
220 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
224 if (!NodesSeen.insert(SP).second)
231 bool DebugInfoFinder::addScope(DIScope *Scope) {
234 // FIXME: Ocaml binding generates a scope with no content, we treat it
236 if (Scope->getNumOperands() == 0)
238 if (!NodesSeen.insert(Scope).second)
240 Scopes.push_back(Scope);
244 bool llvm::stripDebugInfo(Function &F) {
245 bool Changed = false;
246 if (F.getSubprogram()) {
248 F.setSubprogram(nullptr);
251 for (BasicBlock &BB : F) {
252 for (auto II = BB.begin(), End = BB.end(); II != End;) {
253 Instruction &I = *II++; // We may delete the instruction, increment now.
254 if (isa<DbgInfoIntrinsic>(&I)) {
259 if (I.getDebugLoc()) {
261 I.setDebugLoc(DebugLoc());
268 bool llvm::StripDebugInfo(Module &M) {
269 bool Changed = false;
271 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
272 NME = M.named_metadata_end(); NMI != NME;) {
273 NamedMDNode *NMD = &*NMI;
275 if (NMD->getName().startswith("llvm.dbg.")) {
276 NMD->eraseFromParent();
281 for (Function &F : M)
282 Changed |= stripDebugInfo(F);
284 if (GVMaterializer *Materializer = M.getMaterializer())
285 Materializer->setStripDebugInfo();
290 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
291 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
292 M.getModuleFlag("Debug Info Version")))
293 return Val->getZExtValue();