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 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
39 LLVMContext &VMContext) {
40 return cast<MDLocalVariable>(DV)
41 ->withInline(cast_or_null<MDLocation>(InlinedScope));
44 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
45 return cast<MDLocalVariable>(DV)->withoutInline();
48 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
49 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
50 return LocalScope->getSubprogram();
54 DISubprogram llvm::getDISubprogram(const Function *F) {
55 // We look for the first instr that has a debug annotation leading back to F.
57 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
58 return Inst.getDebugLoc();
62 DebugLoc DLoc = Inst->getDebugLoc();
63 const MDNode *Scope = DLoc.getInlinedAtScope();
64 DISubprogram Subprogram = getDISubprogram(Scope);
65 return Subprogram->describes(F) ? Subprogram : DISubprogram();
68 return DISubprogram();
71 DICompositeType llvm::getDICompositeType(DIType T) {
72 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
75 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
76 // This function is currently used by dragonegg and dragonegg does
77 // not generate identifier for types, so using an empty map to resolve
78 // DerivedFrom should be fine.
79 DITypeIdentifierMap EmptyMap;
80 return getDICompositeType(
81 DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
88 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
89 DITypeIdentifierMap Map;
90 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
91 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
92 DIArray Retain = CU.getRetainedTypes();
93 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
94 if (!isa<MDCompositeType>(Retain[Ti]))
96 DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
97 if (MDString *TypeId = Ty.getIdentifier()) {
98 // Definition has priority over declaration.
99 // Try to insert (TypeId, Ty) to Map.
100 std::pair<DITypeIdentifierMap::iterator, bool> P =
101 Map.insert(std::make_pair(TypeId, Ty));
102 // If TypeId already exists in Map and this is a definition, replace
103 // whatever we had (declaration or definition) with the definition.
104 if (!P.second && !Ty.isForwardDecl())
105 P.first->second = Ty;
112 //===----------------------------------------------------------------------===//
113 // DebugInfoFinder implementations.
114 //===----------------------------------------------------------------------===//
116 void DebugInfoFinder::reset() {
123 TypeIdentifierMap.clear();
124 TypeMapInitialized = false;
127 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
128 if (!TypeMapInitialized)
129 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
130 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
131 TypeMapInitialized = true;
135 void DebugInfoFinder::processModule(const Module &M) {
136 InitializeTypeMap(M);
137 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
138 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
139 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
141 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
142 if (addGlobalVariable(DIG)) {
143 processScope(DIG->getScope());
144 processType(DIG->getType().resolve(TypeIdentifierMap));
147 for (auto *SP : CU->getSubprograms())
148 processSubprogram(SP);
149 for (auto *ET : CU->getEnumTypes())
151 for (auto *RT : CU->getRetainedTypes())
153 for (DIImportedEntity Import : CU->getImportedEntities()) {
154 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
155 if (auto *T = dyn_cast<MDType>(Entity))
157 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
158 processSubprogram(SP);
159 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
160 processScope(NS->getScope());
166 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
169 InitializeTypeMap(M);
170 processScope(Loc->getScope());
171 processLocation(M, Loc->getInlinedAt());
174 void DebugInfoFinder::processType(DIType DT) {
177 processScope(DT.getContext().resolve(TypeIdentifierMap));
178 if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
179 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
180 if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
181 for (MDTypeRef Ref : ST->getTypeArray())
182 processType(Ref.resolve(TypeIdentifierMap));
185 for (Metadata *D : DCT->getElements()) {
186 if (DIType T = dyn_cast<MDType>(D))
188 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
189 processSubprogram(SP);
191 } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
192 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
196 void DebugInfoFinder::processScope(DIScope Scope) {
199 if (DIType Ty = dyn_cast<MDType>(Scope)) {
203 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
207 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
208 processSubprogram(SP);
211 if (!addScope(Scope))
213 if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
214 processScope(LB->getScope());
215 } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
216 processScope(NS->getScope());
220 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
221 if (!addSubprogram(SP))
223 processScope(SP->getScope().resolve(TypeIdentifierMap));
224 processType(SP->getType());
225 for (auto *Element : SP->getTemplateParams()) {
226 if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
227 processType(TType->getType().resolve(TypeIdentifierMap));
228 } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
229 processType(TVal->getType().resolve(TypeIdentifierMap));
234 void DebugInfoFinder::processDeclare(const Module &M,
235 const DbgDeclareInst *DDI) {
236 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
239 InitializeTypeMap(M);
241 DIVariable DV = dyn_cast<MDLocalVariable>(N);
245 if (!NodesSeen.insert(DV).second)
247 processScope(DV->getScope());
248 processType(DV->getType().resolve(TypeIdentifierMap));
251 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
252 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
255 InitializeTypeMap(M);
257 DIVariable DV = dyn_cast<MDLocalVariable>(N);
261 if (!NodesSeen.insert(DV).second)
263 processScope(DV->getScope());
264 processType(DV->getType().resolve(TypeIdentifierMap));
267 bool DebugInfoFinder::addType(DIType DT) {
271 if (!NodesSeen.insert(DT).second)
278 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
281 if (!NodesSeen.insert(CU).second)
288 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
292 if (!NodesSeen.insert(DIG).second)
299 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
303 if (!NodesSeen.insert(SP).second)
310 bool DebugInfoFinder::addScope(DIScope Scope) {
313 // FIXME: Ocaml binding generates a scope with no content, we treat it
315 if (Scope->getNumOperands() == 0)
317 if (!NodesSeen.insert(Scope).second)
319 Scopes.push_back(Scope);
323 //===----------------------------------------------------------------------===//
324 // DIDescriptor: dump routines for all descriptors.
325 //===----------------------------------------------------------------------===//
327 void DIDescriptor::dump() const {
332 void DIDescriptor::print(raw_ostream &OS) const {
340 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
341 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
344 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
345 return MDScopeRef(Val).resolve(Map);
348 DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
349 return MDTypeRef(Val).resolve(Map);
352 bool llvm::stripDebugInfo(Function &F) {
353 bool Changed = false;
354 for (BasicBlock &BB : F) {
355 for (Instruction &I : BB) {
356 if (I.getDebugLoc()) {
358 I.setDebugLoc(DebugLoc());
365 bool llvm::StripDebugInfo(Module &M) {
366 bool Changed = false;
368 // Remove all of the calls to the debugger intrinsics, and remove them from
370 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
371 while (!Declare->use_empty()) {
372 CallInst *CI = cast<CallInst>(Declare->user_back());
373 CI->eraseFromParent();
375 Declare->eraseFromParent();
379 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
380 while (!DbgVal->use_empty()) {
381 CallInst *CI = cast<CallInst>(DbgVal->user_back());
382 CI->eraseFromParent();
384 DbgVal->eraseFromParent();
388 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
389 NME = M.named_metadata_end(); NMI != NME;) {
390 NamedMDNode *NMD = NMI;
392 if (NMD->getName().startswith("llvm.dbg.")) {
393 NMD->eraseFromParent();
398 for (Function &F : M)
399 Changed |= stripDebugInfo(F);
401 if (GVMaterializer *Materializer = M.getMaterializer())
402 Materializer->setStripDebugInfo();
407 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
408 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
409 M.getModuleFlag("Debug Info Version")))
410 return Val->getZExtValue();
414 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
415 llvm::makeSubprogramMap(const Module &M) {
416 DenseMap<const Function *, DISubprogram> R;
418 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
422 for (MDNode *N : CU_Nodes->operands()) {
423 DICompileUnit CUNode = cast<MDCompileUnit>(N);
424 for (DISubprogram SP : CUNode->getSubprograms()) {
425 if (Function *F = SP->getFunction())
426 R.insert(std::make_pair(F, SP));