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(
71 DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
78 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
79 DITypeIdentifierMap Map;
80 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
81 auto *CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
82 DIArray Retain = CU->getRetainedTypes();
83 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
84 if (!isa<MDCompositeType>(Retain[Ti]))
86 DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
87 if (MDString *TypeId = Ty.getIdentifier()) {
88 // Definition has priority over declaration.
89 // Try to insert (TypeId, Ty) to Map.
90 std::pair<DITypeIdentifierMap::iterator, bool> P =
91 Map.insert(std::make_pair(TypeId, Ty));
92 // If TypeId already exists in Map and this is a definition, replace
93 // whatever we had (declaration or definition) with the definition.
94 if (!P.second && !Ty.isForwardDecl())
102 //===----------------------------------------------------------------------===//
103 // DebugInfoFinder implementations.
104 //===----------------------------------------------------------------------===//
106 void DebugInfoFinder::reset() {
113 TypeIdentifierMap.clear();
114 TypeMapInitialized = false;
117 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
118 if (!TypeMapInitialized)
119 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
120 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
121 TypeMapInitialized = true;
125 void DebugInfoFinder::processModule(const Module &M) {
126 InitializeTypeMap(M);
127 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
128 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
129 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
131 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
132 if (addGlobalVariable(DIG)) {
133 processScope(DIG->getScope());
134 processType(DIG->getType().resolve(TypeIdentifierMap));
137 for (auto *SP : CU->getSubprograms())
138 processSubprogram(SP);
139 for (auto *ET : CU->getEnumTypes())
141 for (auto *RT : CU->getRetainedTypes())
143 for (DIImportedEntity Import : CU->getImportedEntities()) {
144 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
145 if (auto *T = dyn_cast<MDType>(Entity))
147 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
148 processSubprogram(SP);
149 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
150 processScope(NS->getScope());
156 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
159 InitializeTypeMap(M);
160 processScope(Loc->getScope());
161 processLocation(M, Loc->getInlinedAt());
164 void DebugInfoFinder::processType(DIType DT) {
167 processScope(DT.getContext().resolve(TypeIdentifierMap));
168 if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
169 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
170 if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
171 for (MDTypeRef Ref : ST->getTypeArray())
172 processType(Ref.resolve(TypeIdentifierMap));
175 for (Metadata *D : DCT->getElements()) {
176 if (DIType T = dyn_cast<MDType>(D))
178 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
179 processSubprogram(SP);
181 } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
182 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
186 void DebugInfoFinder::processScope(DIScope Scope) {
189 if (DIType Ty = dyn_cast<MDType>(Scope)) {
193 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
197 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
198 processSubprogram(SP);
201 if (!addScope(Scope))
203 if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
204 processScope(LB->getScope());
205 } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
206 processScope(NS->getScope());
210 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
211 if (!addSubprogram(SP))
213 processScope(SP->getScope().resolve(TypeIdentifierMap));
214 processType(SP->getType());
215 for (auto *Element : SP->getTemplateParams()) {
216 if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
217 processType(TType->getType().resolve(TypeIdentifierMap));
218 } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
219 processType(TVal->getType().resolve(TypeIdentifierMap));
224 void DebugInfoFinder::processDeclare(const Module &M,
225 const DbgDeclareInst *DDI) {
226 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
229 InitializeTypeMap(M);
231 DIVariable DV = dyn_cast<MDLocalVariable>(N);
235 if (!NodesSeen.insert(DV).second)
237 processScope(DV->getScope());
238 processType(DV->getType().resolve(TypeIdentifierMap));
241 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
242 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
245 InitializeTypeMap(M);
247 DIVariable DV = dyn_cast<MDLocalVariable>(N);
251 if (!NodesSeen.insert(DV).second)
253 processScope(DV->getScope());
254 processType(DV->getType().resolve(TypeIdentifierMap));
257 bool DebugInfoFinder::addType(DIType DT) {
261 if (!NodesSeen.insert(DT).second)
268 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
271 if (!NodesSeen.insert(CU).second)
278 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
282 if (!NodesSeen.insert(DIG).second)
289 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
293 if (!NodesSeen.insert(SP).second)
300 bool DebugInfoFinder::addScope(DIScope Scope) {
303 // FIXME: Ocaml binding generates a scope with no content, we treat it
305 if (Scope->getNumOperands() == 0)
307 if (!NodesSeen.insert(Scope).second)
309 Scopes.push_back(Scope);
313 //===----------------------------------------------------------------------===//
314 // DIDescriptor: dump routines for all descriptors.
315 //===----------------------------------------------------------------------===//
317 void DIDescriptor::dump() const {
322 void DIDescriptor::print(raw_ostream &OS) const {
330 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
331 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
334 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
335 return MDScopeRef(Val).resolve(Map);
338 DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
339 return MDTypeRef(Val).resolve(Map);
342 bool llvm::stripDebugInfo(Function &F) {
343 bool Changed = false;
344 for (BasicBlock &BB : F) {
345 for (Instruction &I : BB) {
346 if (I.getDebugLoc()) {
348 I.setDebugLoc(DebugLoc());
355 bool llvm::StripDebugInfo(Module &M) {
356 bool Changed = false;
358 // Remove all of the calls to the debugger intrinsics, and remove them from
360 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
361 while (!Declare->use_empty()) {
362 CallInst *CI = cast<CallInst>(Declare->user_back());
363 CI->eraseFromParent();
365 Declare->eraseFromParent();
369 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
370 while (!DbgVal->use_empty()) {
371 CallInst *CI = cast<CallInst>(DbgVal->user_back());
372 CI->eraseFromParent();
374 DbgVal->eraseFromParent();
378 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
379 NME = M.named_metadata_end(); NMI != NME;) {
380 NamedMDNode *NMD = NMI;
382 if (NMD->getName().startswith("llvm.dbg.")) {
383 NMD->eraseFromParent();
388 for (Function &F : M)
389 Changed |= stripDebugInfo(F);
391 if (GVMaterializer *Materializer = M.getMaterializer())
392 Materializer->setStripDebugInfo();
397 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
398 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
399 M.getModuleFlag("Debug Info Version")))
400 return Val->getZExtValue();
404 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
405 llvm::makeSubprogramMap(const Module &M) {
406 DenseMap<const Function *, DISubprogram> R;
408 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
412 for (MDNode *N : CU_Nodes->operands()) {
413 DICompileUnit CUNode = cast<MDCompileUnit>(N);
414 for (DISubprogram SP : CUNode->getSubprograms()) {
415 if (Function *F = SP->getFunction())
416 R.insert(std::make_pair(F, SP));