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());
148 else if (auto *M = dyn_cast<DIModule>(Entity))
149 processScope(M->getScope());
155 void DebugInfoFinder::processLocation(const Module &M, const 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<DICompositeTypeBase>(DT)) {
168 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
169 if (auto *ST = dyn_cast<DISubroutineType>(DCT)) {
170 for (DITypeRef Ref : ST->getTypeArray())
171 processType(Ref.resolve(TypeIdentifierMap));
174 for (Metadata *D : DCT->getElements()) {
175 if (auto *T = dyn_cast<DIType>(D))
177 else if (auto *SP = dyn_cast<DISubprogram>(D))
178 processSubprogram(SP);
180 } else if (auto *DDT = dyn_cast<DIDerivedTypeBase>(DT)) {
181 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
185 void DebugInfoFinder::processScope(DIScope *Scope) {
188 if (auto *Ty = dyn_cast<DIType>(Scope)) {
192 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
196 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
197 processSubprogram(SP);
200 if (!addScope(Scope))
202 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
203 processScope(LB->getScope());
204 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
205 processScope(NS->getScope());
206 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
207 processScope(M->getScope());
211 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
212 if (!addSubprogram(SP))
214 processScope(SP->getScope().resolve(TypeIdentifierMap));
215 processType(SP->getType());
216 for (auto *Element : SP->getTemplateParams()) {
217 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
218 processType(TType->getType().resolve(TypeIdentifierMap));
219 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
220 processType(TVal->getType().resolve(TypeIdentifierMap));
225 void DebugInfoFinder::processDeclare(const Module &M,
226 const DbgDeclareInst *DDI) {
227 auto *N = dyn_cast<MDNode>(DDI->getVariable());
230 InitializeTypeMap(M);
232 auto *DV = dyn_cast<DILocalVariable>(N);
236 if (!NodesSeen.insert(DV).second)
238 processScope(DV->getScope());
239 processType(DV->getType().resolve(TypeIdentifierMap));
242 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
243 auto *N = dyn_cast<MDNode>(DVI->getVariable());
246 InitializeTypeMap(M);
248 auto *DV = dyn_cast<DILocalVariable>(N);
252 if (!NodesSeen.insert(DV).second)
254 processScope(DV->getScope());
255 processType(DV->getType().resolve(TypeIdentifierMap));
258 bool DebugInfoFinder::addType(DIType *DT) {
262 if (!NodesSeen.insert(DT).second)
265 TYs.push_back(const_cast<DIType *>(DT));
269 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
272 if (!NodesSeen.insert(CU).second)
279 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
283 if (!NodesSeen.insert(DIG).second)
290 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
294 if (!NodesSeen.insert(SP).second)
301 bool DebugInfoFinder::addScope(DIScope *Scope) {
304 // FIXME: Ocaml binding generates a scope with no content, we treat it
306 if (Scope->getNumOperands() == 0)
308 if (!NodesSeen.insert(Scope).second)
310 Scopes.push_back(Scope);
314 bool llvm::stripDebugInfo(Function &F) {
315 bool Changed = false;
316 for (BasicBlock &BB : F) {
317 for (Instruction &I : BB) {
318 if (I.getDebugLoc()) {
320 I.setDebugLoc(DebugLoc());
327 bool llvm::StripDebugInfo(Module &M) {
328 bool Changed = false;
330 // Remove all of the calls to the debugger intrinsics, and remove them from
332 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
333 while (!Declare->use_empty()) {
334 CallInst *CI = cast<CallInst>(Declare->user_back());
335 CI->eraseFromParent();
337 Declare->eraseFromParent();
341 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
342 while (!DbgVal->use_empty()) {
343 CallInst *CI = cast<CallInst>(DbgVal->user_back());
344 CI->eraseFromParent();
346 DbgVal->eraseFromParent();
350 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
351 NME = M.named_metadata_end(); NMI != NME;) {
352 NamedMDNode *NMD = NMI;
354 if (NMD->getName().startswith("llvm.dbg.")) {
355 NMD->eraseFromParent();
360 for (Function &F : M)
361 Changed |= stripDebugInfo(F);
363 if (GVMaterializer *Materializer = M.getMaterializer())
364 Materializer->setStripDebugInfo();
369 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
370 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
371 M.getModuleFlag("Debug Info Version")))
372 return Val->getZExtValue();
376 DenseMap<const llvm::Function *, DISubprogram *>
377 llvm::makeSubprogramMap(const Module &M) {
378 DenseMap<const Function *, DISubprogram *> R;
380 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
384 for (MDNode *N : CU_Nodes->operands()) {
385 auto *CUNode = cast<DICompileUnit>(N);
386 for (auto *SP : CUNode->getSubprograms()) {
387 if (Function *F = SP->getFunction())
388 R.insert(std::make_pair(F, SP));