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 DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
38 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
39 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
40 return LocalScope->getSubprogram();
44 DISubprogram llvm::getDISubprogram(const Function *F) {
45 // We look for the first instr that has a debug annotation leading back to F.
47 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
48 return Inst.getDebugLoc();
52 DebugLoc DLoc = Inst->getDebugLoc();
53 const MDNode *Scope = DLoc.getInlinedAtScope();
54 DISubprogram Subprogram = getDISubprogram(Scope);
55 return Subprogram->describes(F) ? Subprogram : DISubprogram();
58 return DISubprogram();
61 DICompositeType llvm::getDICompositeType(DIType T) {
62 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
65 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
66 // This function is currently used by dragonegg and dragonegg does
67 // not generate identifier for types, so using an empty map to resolve
68 // DerivedFrom should be fine.
69 DITypeIdentifierMap EmptyMap;
70 return getDICompositeType(D->getBaseType().resolve(EmptyMap));
77 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
78 DITypeIdentifierMap Map;
79 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
80 auto *CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
81 DIArray Retain = CU->getRetainedTypes();
82 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
83 if (!isa<MDCompositeType>(Retain[Ti]))
85 auto *Ty = cast<MDCompositeType>(Retain[Ti]);
86 if (MDString *TypeId = Ty->getRawIdentifier()) {
87 // Definition has priority over declaration.
88 // Try to insert (TypeId, Ty) to Map.
89 std::pair<DITypeIdentifierMap::iterator, bool> P =
90 Map.insert(std::make_pair(TypeId, Ty));
91 // If TypeId already exists in Map and this is a definition, replace
92 // whatever we had (declaration or definition) with the definition.
93 if (!P.second && !Ty->isForwardDecl())
101 //===----------------------------------------------------------------------===//
102 // DebugInfoFinder implementations.
103 //===----------------------------------------------------------------------===//
105 void DebugInfoFinder::reset() {
112 TypeIdentifierMap.clear();
113 TypeMapInitialized = false;
116 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
117 if (!TypeMapInitialized)
118 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
119 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
120 TypeMapInitialized = true;
124 void DebugInfoFinder::processModule(const Module &M) {
125 InitializeTypeMap(M);
126 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
127 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
128 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
130 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
131 if (addGlobalVariable(DIG)) {
132 processScope(DIG->getScope());
133 processType(DIG->getType().resolve(TypeIdentifierMap));
136 for (auto *SP : CU->getSubprograms())
137 processSubprogram(SP);
138 for (auto *ET : CU->getEnumTypes())
140 for (auto *RT : CU->getRetainedTypes())
142 for (DIImportedEntity Import : CU->getImportedEntities()) {
143 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
144 if (auto *T = dyn_cast<MDType>(Entity))
146 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
147 processSubprogram(SP);
148 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
149 processScope(NS->getScope());
155 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
158 InitializeTypeMap(M);
159 processScope(Loc->getScope());
160 processLocation(M, Loc->getInlinedAt());
163 void DebugInfoFinder::processType(DIType DT) {
166 processScope(DT->getScope().resolve(TypeIdentifierMap));
167 if (auto *DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
168 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
169 if (auto *ST = dyn_cast<MDSubroutineType>(DCT)) {
170 for (MDTypeRef Ref : ST->getTypeArray())
171 processType(Ref.resolve(TypeIdentifierMap));
174 for (Metadata *D : DCT->getElements()) {
175 if (auto *T = dyn_cast<MDType>(D))
177 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
178 processSubprogram(SP);
180 } else if (auto *DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
181 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
185 void DebugInfoFinder::processScope(DIScope Scope) {
188 if (DIType Ty = dyn_cast<MDType>(Scope)) {
192 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
196 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
197 processSubprogram(SP);
200 if (!addScope(Scope))
202 if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
203 processScope(LB->getScope());
204 } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
205 processScope(NS->getScope());
209 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
210 if (!addSubprogram(SP))
212 processScope(SP->getScope().resolve(TypeIdentifierMap));
213 processType(SP->getType());
214 for (auto *Element : SP->getTemplateParams()) {
215 if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
216 processType(TType->getType().resolve(TypeIdentifierMap));
217 } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
218 processType(TVal->getType().resolve(TypeIdentifierMap));
223 void DebugInfoFinder::processDeclare(const Module &M,
224 const DbgDeclareInst *DDI) {
225 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
228 InitializeTypeMap(M);
230 DIVariable DV = dyn_cast<MDLocalVariable>(N);
234 if (!NodesSeen.insert(DV).second)
236 processScope(DV->getScope());
237 processType(DV->getType().resolve(TypeIdentifierMap));
240 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
241 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
244 InitializeTypeMap(M);
246 DIVariable DV = dyn_cast<MDLocalVariable>(N);
250 if (!NodesSeen.insert(DV).second)
252 processScope(DV->getScope());
253 processType(DV->getType().resolve(TypeIdentifierMap));
256 bool DebugInfoFinder::addType(DIType DT) {
260 if (!NodesSeen.insert(DT).second)
267 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
270 if (!NodesSeen.insert(CU).second)
277 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
281 if (!NodesSeen.insert(DIG).second)
288 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
292 if (!NodesSeen.insert(SP).second)
299 bool DebugInfoFinder::addScope(DIScope Scope) {
302 // FIXME: Ocaml binding generates a scope with no content, we treat it
304 if (Scope->getNumOperands() == 0)
306 if (!NodesSeen.insert(Scope).second)
308 Scopes.push_back(Scope);
312 //===----------------------------------------------------------------------===//
313 // DIDescriptor: dump routines for all descriptors.
314 //===----------------------------------------------------------------------===//
316 void DIDescriptor::dump() const {
321 void DIDescriptor::print(raw_ostream &OS) const {
329 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
330 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
333 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
334 return MDScopeRef(Val).resolve(Map);
337 DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
338 return MDTypeRef(Val).resolve(Map);
341 bool llvm::stripDebugInfo(Function &F) {
342 bool Changed = false;
343 for (BasicBlock &BB : F) {
344 for (Instruction &I : BB) {
345 if (I.getDebugLoc()) {
347 I.setDebugLoc(DebugLoc());
354 bool llvm::StripDebugInfo(Module &M) {
355 bool Changed = false;
357 // Remove all of the calls to the debugger intrinsics, and remove them from
359 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
360 while (!Declare->use_empty()) {
361 CallInst *CI = cast<CallInst>(Declare->user_back());
362 CI->eraseFromParent();
364 Declare->eraseFromParent();
368 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
369 while (!DbgVal->use_empty()) {
370 CallInst *CI = cast<CallInst>(DbgVal->user_back());
371 CI->eraseFromParent();
373 DbgVal->eraseFromParent();
377 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
378 NME = M.named_metadata_end(); NMI != NME;) {
379 NamedMDNode *NMD = NMI;
381 if (NMD->getName().startswith("llvm.dbg.")) {
382 NMD->eraseFromParent();
387 for (Function &F : M)
388 Changed |= stripDebugInfo(F);
390 if (GVMaterializer *Materializer = M.getMaterializer())
391 Materializer->setStripDebugInfo();
396 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
397 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
398 M.getModuleFlag("Debug Info Version")))
399 return Val->getZExtValue();
403 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
404 llvm::makeSubprogramMap(const Module &M) {
405 DenseMap<const Function *, DISubprogram> R;
407 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
411 for (MDNode *N : CU_Nodes->operands()) {
412 DICompileUnit CUNode = cast<MDCompileUnit>(N);
413 for (DISubprogram SP : CUNode->getSubprograms()) {
414 if (Function *F = SP->getFunction())
415 R.insert(std::make_pair(F, SP));