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;
59 DICompositeTypeBase *llvm::getDICompositeType(DIType *T) {
60 if (auto *C = dyn_cast_or_null<DICompositeTypeBase>(T))
63 if (auto *D = dyn_cast_or_null<DIDerivedTypeBase>(T)) {
64 // This function is currently used by dragonegg and dragonegg does
65 // not generate identifier for types, so using an empty map to resolve
66 // DerivedFrom should be fine.
67 DITypeIdentifierMap EmptyMap;
68 return getDICompositeType(D->getBaseType().resolve(EmptyMap));
75 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
76 DITypeIdentifierMap Map;
77 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
78 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
79 DINodeArray Retain = CU->getRetainedTypes();
80 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
81 if (!isa<DICompositeType>(Retain[Ti]))
83 auto *Ty = cast<DICompositeType>(Retain[Ti]);
84 if (MDString *TypeId = Ty->getRawIdentifier()) {
85 // Definition has priority over declaration.
86 // Try to insert (TypeId, Ty) to Map.
87 std::pair<DITypeIdentifierMap::iterator, bool> P =
88 Map.insert(std::make_pair(TypeId, Ty));
89 // If TypeId already exists in Map and this is a definition, replace
90 // whatever we had (declaration or definition) with the definition.
91 if (!P.second && !Ty->isForwardDecl())
99 //===----------------------------------------------------------------------===//
100 // DebugInfoFinder implementations.
101 //===----------------------------------------------------------------------===//
103 void DebugInfoFinder::reset() {
110 TypeIdentifierMap.clear();
111 TypeMapInitialized = false;
114 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
115 if (!TypeMapInitialized)
116 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
117 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
118 TypeMapInitialized = true;
122 void DebugInfoFinder::processModule(const Module &M) {
123 InitializeTypeMap(M);
124 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
125 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
126 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
128 for (auto *DIG : CU->getGlobalVariables()) {
129 if (addGlobalVariable(DIG)) {
130 processScope(DIG->getScope());
131 processType(DIG->getType().resolve(TypeIdentifierMap));
134 for (auto *SP : CU->getSubprograms())
135 processSubprogram(SP);
136 for (auto *ET : CU->getEnumTypes())
138 for (auto *RT : CU->getRetainedTypes())
140 for (auto *Import : CU->getImportedEntities()) {
141 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
142 if (auto *T = dyn_cast<DIType>(Entity))
144 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
145 processSubprogram(SP);
146 else if (auto *NS = dyn_cast<DINamespace>(Entity))
147 processScope(NS->getScope());
153 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
156 InitializeTypeMap(M);
157 processScope(Loc->getScope());
158 processLocation(M, Loc->getInlinedAt());
161 void DebugInfoFinder::processType(DIType *DT) {
164 processScope(DT->getScope().resolve(TypeIdentifierMap));
165 if (auto *DCT = dyn_cast<DICompositeTypeBase>(DT)) {
166 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
167 if (auto *ST = dyn_cast<DISubroutineType>(DCT)) {
168 for (DITypeRef Ref : ST->getTypeArray())
169 processType(Ref.resolve(TypeIdentifierMap));
172 for (Metadata *D : DCT->getElements()) {
173 if (auto *T = dyn_cast<DIType>(D))
175 else if (auto *SP = dyn_cast<DISubprogram>(D))
176 processSubprogram(SP);
178 } else if (auto *DDT = dyn_cast<DIDerivedTypeBase>(DT)) {
179 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
183 void DebugInfoFinder::processScope(DIScope *Scope) {
186 if (auto *Ty = dyn_cast<DIType>(Scope)) {
190 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
194 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
195 processSubprogram(SP);
198 if (!addScope(Scope))
200 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
201 processScope(LB->getScope());
202 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
203 processScope(NS->getScope());
207 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
208 if (!addSubprogram(SP))
210 processScope(SP->getScope().resolve(TypeIdentifierMap));
211 processType(SP->getType());
212 for (auto *Element : SP->getTemplateParams()) {
213 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
214 processType(TType->getType().resolve(TypeIdentifierMap));
215 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
216 processType(TVal->getType().resolve(TypeIdentifierMap));
221 void DebugInfoFinder::processDeclare(const Module &M,
222 const DbgDeclareInst *DDI) {
223 auto *N = dyn_cast<MDNode>(DDI->getVariable());
226 InitializeTypeMap(M);
228 auto *DV = dyn_cast<DILocalVariable>(N);
232 if (!NodesSeen.insert(DV).second)
234 processScope(DV->getScope());
235 processType(DV->getType().resolve(TypeIdentifierMap));
238 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
239 auto *N = dyn_cast<MDNode>(DVI->getVariable());
242 InitializeTypeMap(M);
244 auto *DV = dyn_cast<DILocalVariable>(N);
248 if (!NodesSeen.insert(DV).second)
250 processScope(DV->getScope());
251 processType(DV->getType().resolve(TypeIdentifierMap));
254 bool DebugInfoFinder::addType(DIType *DT) {
258 if (!NodesSeen.insert(DT).second)
261 TYs.push_back(const_cast<DIType *>(DT));
265 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
268 if (!NodesSeen.insert(CU).second)
275 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
279 if (!NodesSeen.insert(DIG).second)
286 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
290 if (!NodesSeen.insert(SP).second)
297 bool DebugInfoFinder::addScope(DIScope *Scope) {
300 // FIXME: Ocaml binding generates a scope with no content, we treat it
302 if (Scope->getNumOperands() == 0)
304 if (!NodesSeen.insert(Scope).second)
306 Scopes.push_back(Scope);
310 bool llvm::stripDebugInfo(Function &F) {
311 bool Changed = false;
312 for (BasicBlock &BB : F) {
313 for (Instruction &I : BB) {
314 if (I.getDebugLoc()) {
316 I.setDebugLoc(DebugLoc());
323 bool llvm::StripDebugInfo(Module &M) {
324 bool Changed = false;
326 // Remove all of the calls to the debugger intrinsics, and remove them from
328 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
329 while (!Declare->use_empty()) {
330 CallInst *CI = cast<CallInst>(Declare->user_back());
331 CI->eraseFromParent();
333 Declare->eraseFromParent();
337 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
338 while (!DbgVal->use_empty()) {
339 CallInst *CI = cast<CallInst>(DbgVal->user_back());
340 CI->eraseFromParent();
342 DbgVal->eraseFromParent();
346 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
347 NME = M.named_metadata_end(); NMI != NME;) {
348 NamedMDNode *NMD = NMI;
350 if (NMD->getName().startswith("llvm.dbg.")) {
351 NMD->eraseFromParent();
356 for (Function &F : M)
357 Changed |= stripDebugInfo(F);
359 if (GVMaterializer *Materializer = M.getMaterializer())
360 Materializer->setStripDebugInfo();
365 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
366 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
367 M.getModuleFlag("Debug Info Version")))
368 return Val->getZExtValue();
372 DenseMap<const llvm::Function *, DISubprogram *>
373 llvm::makeSubprogramMap(const Module &M) {
374 DenseMap<const Function *, DISubprogram *> R;
376 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
380 for (MDNode *N : CU_Nodes->operands()) {
381 auto *CUNode = cast<DICompileUnit>(N);
382 for (auto *SP : CUNode->getSubprograms()) {
383 if (Function *F = SP->getFunction())
384 R.insert(std::make_pair(F, SP));