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 //===----------------------------------------------------------------------===//
37 // Simple Descriptor Constructors and other Methods
38 //===----------------------------------------------------------------------===//
40 DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
42 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
43 get()->replaceSubprograms(MDSubprogramArray(Subprograms));
46 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
47 get()->replaceGlobalVariables(MDGlobalVariableArray(GlobalVariables));
50 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
51 LLVMContext &VMContext) {
52 return cast<MDLocalVariable>(DV)
53 ->withInline(cast_or_null<MDLocation>(InlinedScope));
56 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
57 return cast<MDLocalVariable>(DV)->withoutInline();
60 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
61 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
62 return LocalScope->getSubprogram();
66 DISubprogram llvm::getDISubprogram(const Function *F) {
67 // We look for the first instr that has a debug annotation leading back to F.
69 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
70 return Inst.getDebugLoc();
74 DebugLoc DLoc = Inst->getDebugLoc();
75 const MDNode *Scope = DLoc.getInlinedAtScope();
76 DISubprogram Subprogram = getDISubprogram(Scope);
77 return Subprogram.describes(F) ? Subprogram : DISubprogram();
80 return DISubprogram();
83 DICompositeType llvm::getDICompositeType(DIType T) {
84 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
87 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
88 // This function is currently used by dragonegg and dragonegg does
89 // not generate identifier for types, so using an empty map to resolve
90 // DerivedFrom should be fine.
91 DITypeIdentifierMap EmptyMap;
92 return getDICompositeType(
93 DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
100 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
101 DITypeIdentifierMap Map;
102 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
103 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
104 DIArray Retain = CU.getRetainedTypes();
105 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
106 if (!isa<MDCompositeType>(Retain[Ti]))
108 DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
109 if (MDString *TypeId = Ty.getIdentifier()) {
110 // Definition has priority over declaration.
111 // Try to insert (TypeId, Ty) to Map.
112 std::pair<DITypeIdentifierMap::iterator, bool> P =
113 Map.insert(std::make_pair(TypeId, Ty));
114 // If TypeId already exists in Map and this is a definition, replace
115 // whatever we had (declaration or definition) with the definition.
116 if (!P.second && !Ty.isForwardDecl())
117 P.first->second = Ty;
124 //===----------------------------------------------------------------------===//
125 // DebugInfoFinder implementations.
126 //===----------------------------------------------------------------------===//
128 void DebugInfoFinder::reset() {
135 TypeIdentifierMap.clear();
136 TypeMapInitialized = false;
139 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
140 if (!TypeMapInitialized)
141 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
142 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
143 TypeMapInitialized = true;
147 void DebugInfoFinder::processModule(const Module &M) {
148 InitializeTypeMap(M);
149 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
150 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
151 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
153 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
154 if (addGlobalVariable(DIG)) {
155 processScope(DIG.getContext());
156 processType(DIG.getType().resolve(TypeIdentifierMap));
159 for (auto *SP : CU->getSubprograms())
160 processSubprogram(SP);
161 for (auto *ET : CU->getEnumTypes())
163 for (auto *RT : CU->getRetainedTypes())
165 for (DIImportedEntity Import : CU->getImportedEntities()) {
166 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
167 if (auto *T = dyn_cast<MDType>(Entity))
169 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
170 processSubprogram(SP);
171 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
172 processScope(NS->getScope());
178 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
181 InitializeTypeMap(M);
182 processScope(Loc->getScope());
183 processLocation(M, Loc->getInlinedAt());
186 void DebugInfoFinder::processType(DIType DT) {
189 processScope(DT.getContext().resolve(TypeIdentifierMap));
190 if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
191 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
192 if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
193 for (MDTypeRef Ref : ST->getTypeArray())
194 processType(Ref.resolve(TypeIdentifierMap));
197 for (Metadata *D : DCT->getElements()->operands()) {
198 if (DIType T = dyn_cast<MDType>(D))
200 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
201 processSubprogram(SP);
203 } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
204 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
208 void DebugInfoFinder::processScope(DIScope Scope) {
211 if (DIType Ty = dyn_cast<MDType>(Scope)) {
215 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
219 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
220 processSubprogram(SP);
223 if (!addScope(Scope))
225 if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
226 processScope(LB.getContext());
227 } else if (DINameSpace NS = dyn_cast<MDNamespace>(Scope)) {
228 processScope(NS.getContext());
232 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
233 if (!addSubprogram(SP))
235 processScope(SP.getContext().resolve(TypeIdentifierMap));
236 processType(SP.getType());
237 for (auto *Element : SP.getTemplateParams()) {
238 if (DITemplateTypeParameter TType =
239 dyn_cast<MDTemplateTypeParameter>(Element)) {
240 processType(TType.getType().resolve(TypeIdentifierMap));
241 } else if (DITemplateValueParameter TVal =
242 dyn_cast<MDTemplateValueParameter>(Element)) {
243 processType(TVal.getType().resolve(TypeIdentifierMap));
248 void DebugInfoFinder::processDeclare(const Module &M,
249 const DbgDeclareInst *DDI) {
250 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
253 InitializeTypeMap(M);
255 DIVariable DV = dyn_cast<MDLocalVariable>(N);
259 if (!NodesSeen.insert(DV).second)
261 processScope(DV.getContext());
262 processType(DV.getType().resolve(TypeIdentifierMap));
265 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
266 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
269 InitializeTypeMap(M);
271 DIVariable DV = dyn_cast<MDLocalVariable>(N);
275 if (!NodesSeen.insert(DV).second)
277 processScope(DV.getContext());
278 processType(DV.getType().resolve(TypeIdentifierMap));
281 bool DebugInfoFinder::addType(DIType DT) {
285 if (!NodesSeen.insert(DT).second)
292 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
295 if (!NodesSeen.insert(CU).second)
302 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
306 if (!NodesSeen.insert(DIG).second)
313 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
317 if (!NodesSeen.insert(SP).second)
324 bool DebugInfoFinder::addScope(DIScope Scope) {
327 // FIXME: Ocaml binding generates a scope with no content, we treat it
329 if (Scope->getNumOperands() == 0)
331 if (!NodesSeen.insert(Scope).second)
333 Scopes.push_back(Scope);
337 //===----------------------------------------------------------------------===//
338 // DIDescriptor: dump routines for all descriptors.
339 //===----------------------------------------------------------------------===//
341 void DIDescriptor::dump() const {
346 void DIDescriptor::print(raw_ostream &OS) const {
352 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
353 const LLVMContext &Ctx) {
357 DIScope Scope = cast<MDScope>(DL.getScope());
358 // Omit the directory, because it's likely to be long and uninteresting.
359 CommentOS << Scope.getFilename();
360 CommentOS << ':' << DL.getLine();
361 if (DL.getCol() != 0)
362 CommentOS << ':' << DL.getCol();
364 DebugLoc InlinedAtDL = DL.getInlinedAt();
369 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
373 void DIVariable::printExtendedName(raw_ostream &OS) const {
374 const LLVMContext &Ctx = DbgNode->getContext();
375 StringRef Res = getName();
377 OS << Res << "," << getLineNumber();
378 if (auto *InlinedAt = get()->getInlinedAt()) {
379 if (DebugLoc InlinedAtDL = InlinedAt) {
381 printDebugLoc(InlinedAtDL, OS, Ctx);
389 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
390 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
393 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
394 return MDScopeRef(Val).resolve(Map);
397 DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
398 return MDTypeRef(Val).resolve(Map);
401 bool llvm::stripDebugInfo(Function &F) {
402 bool Changed = false;
403 for (BasicBlock &BB : F) {
404 for (Instruction &I : BB) {
405 if (I.getDebugLoc()) {
407 I.setDebugLoc(DebugLoc());
414 bool llvm::StripDebugInfo(Module &M) {
415 bool Changed = false;
417 // Remove all of the calls to the debugger intrinsics, and remove them from
419 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
420 while (!Declare->use_empty()) {
421 CallInst *CI = cast<CallInst>(Declare->user_back());
422 CI->eraseFromParent();
424 Declare->eraseFromParent();
428 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
429 while (!DbgVal->use_empty()) {
430 CallInst *CI = cast<CallInst>(DbgVal->user_back());
431 CI->eraseFromParent();
433 DbgVal->eraseFromParent();
437 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
438 NME = M.named_metadata_end(); NMI != NME;) {
439 NamedMDNode *NMD = NMI;
441 if (NMD->getName().startswith("llvm.dbg.")) {
442 NMD->eraseFromParent();
447 for (Function &F : M)
448 Changed |= stripDebugInfo(F);
450 if (GVMaterializer *Materializer = M.getMaterializer())
451 Materializer->setStripDebugInfo();
456 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
457 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
458 M.getModuleFlag("Debug Info Version")))
459 return Val->getZExtValue();
463 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
464 llvm::makeSubprogramMap(const Module &M) {
465 DenseMap<const Function *, DISubprogram> R;
467 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
471 for (MDNode *N : CU_Nodes->operands()) {
472 DICompileUnit CUNode = cast<MDCompileUnit>(N);
473 for (DISubprogram SP : CUNode->getSubprograms()) {
474 if (Function *F = SP.getFunction())
475 R.insert(std::make_pair(F, SP));