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 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
51 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
52 return ++Ctx.pImpl->DiscriminatorTable[Key];
55 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
56 LLVMContext &VMContext) {
57 return cast<MDLocalVariable>(DV)
58 ->withInline(cast_or_null<MDLocation>(InlinedScope));
61 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
62 return cast<MDLocalVariable>(DV)->withoutInline();
65 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
66 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
67 return LocalScope->getSubprogram();
71 DISubprogram llvm::getDISubprogram(const Function *F) {
72 // We look for the first instr that has a debug annotation leading back to F.
74 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
75 return Inst.getDebugLoc();
79 DebugLoc DLoc = Inst->getDebugLoc();
80 const MDNode *Scope = DLoc.getInlinedAtScope();
81 DISubprogram Subprogram = getDISubprogram(Scope);
82 return Subprogram.describes(F) ? Subprogram : DISubprogram();
85 return DISubprogram();
88 DICompositeType llvm::getDICompositeType(DIType T) {
89 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
92 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
93 // This function is currently used by dragonegg and dragonegg does
94 // not generate identifier for types, so using an empty map to resolve
95 // DerivedFrom should be fine.
96 DITypeIdentifierMap EmptyMap;
97 return getDICompositeType(
98 DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
105 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
106 DITypeIdentifierMap Map;
107 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
108 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
109 DIArray Retain = CU.getRetainedTypes();
110 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
111 if (!isa<MDCompositeType>(Retain[Ti]))
113 DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
114 if (MDString *TypeId = Ty.getIdentifier()) {
115 // Definition has priority over declaration.
116 // Try to insert (TypeId, Ty) to Map.
117 std::pair<DITypeIdentifierMap::iterator, bool> P =
118 Map.insert(std::make_pair(TypeId, Ty));
119 // If TypeId already exists in Map and this is a definition, replace
120 // whatever we had (declaration or definition) with the definition.
121 if (!P.second && !Ty.isForwardDecl())
122 P.first->second = Ty;
129 //===----------------------------------------------------------------------===//
130 // DebugInfoFinder implementations.
131 //===----------------------------------------------------------------------===//
133 void DebugInfoFinder::reset() {
140 TypeIdentifierMap.clear();
141 TypeMapInitialized = false;
144 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
145 if (!TypeMapInitialized)
146 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
147 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
148 TypeMapInitialized = true;
152 void DebugInfoFinder::processModule(const Module &M) {
153 InitializeTypeMap(M);
154 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
155 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
156 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
158 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
159 if (addGlobalVariable(DIG)) {
160 processScope(DIG.getContext());
161 processType(DIG.getType().resolve(TypeIdentifierMap));
164 for (auto *SP : CU->getSubprograms())
165 processSubprogram(SP);
166 for (auto *ET : CU->getEnumTypes())
168 for (auto *RT : CU->getRetainedTypes())
170 for (DIImportedEntity Import : CU->getImportedEntities()) {
171 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
172 if (auto *T = dyn_cast<MDType>(Entity))
174 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
175 processSubprogram(SP);
176 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
177 processScope(NS->getScope());
183 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
186 InitializeTypeMap(M);
187 processScope(Loc.getScope());
188 processLocation(M, Loc.getOrigLocation());
191 void DebugInfoFinder::processType(DIType DT) {
194 processScope(DT.getContext().resolve(TypeIdentifierMap));
195 if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
196 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
197 if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
198 for (MDTypeRef Ref : ST->getTypeArray())
199 processType(Ref.resolve(TypeIdentifierMap));
202 for (Metadata *D : DCT->getElements()->operands()) {
203 if (DIType T = dyn_cast<MDType>(D))
205 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
206 processSubprogram(SP);
208 } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
209 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
213 void DebugInfoFinder::processScope(DIScope Scope) {
216 if (DIType Ty = dyn_cast<MDType>(Scope)) {
220 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
224 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
225 processSubprogram(SP);
228 if (!addScope(Scope))
230 if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
231 processScope(LB.getContext());
232 } else if (DINameSpace NS = dyn_cast<MDNamespace>(Scope)) {
233 processScope(NS.getContext());
237 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
238 if (!addSubprogram(SP))
240 processScope(SP.getContext().resolve(TypeIdentifierMap));
241 processType(SP.getType());
242 for (auto *Element : SP.getTemplateParams()) {
243 if (DITemplateTypeParameter TType =
244 dyn_cast<MDTemplateTypeParameter>(Element)) {
245 processType(TType.getType().resolve(TypeIdentifierMap));
246 } else if (DITemplateValueParameter TVal =
247 dyn_cast<MDTemplateValueParameter>(Element)) {
248 processType(TVal.getType().resolve(TypeIdentifierMap));
253 void DebugInfoFinder::processDeclare(const Module &M,
254 const DbgDeclareInst *DDI) {
255 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
258 InitializeTypeMap(M);
260 DIVariable DV = dyn_cast<MDLocalVariable>(N);
264 if (!NodesSeen.insert(DV).second)
266 processScope(DV.getContext());
267 processType(DV.getType().resolve(TypeIdentifierMap));
270 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
271 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
274 InitializeTypeMap(M);
276 DIVariable DV = dyn_cast<MDLocalVariable>(N);
280 if (!NodesSeen.insert(DV).second)
282 processScope(DV.getContext());
283 processType(DV.getType().resolve(TypeIdentifierMap));
286 bool DebugInfoFinder::addType(DIType DT) {
290 if (!NodesSeen.insert(DT).second)
297 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
300 if (!NodesSeen.insert(CU).second)
307 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
311 if (!NodesSeen.insert(DIG).second)
318 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
322 if (!NodesSeen.insert(SP).second)
329 bool DebugInfoFinder::addScope(DIScope Scope) {
332 // FIXME: Ocaml binding generates a scope with no content, we treat it
334 if (Scope->getNumOperands() == 0)
336 if (!NodesSeen.insert(Scope).second)
338 Scopes.push_back(Scope);
342 //===----------------------------------------------------------------------===//
343 // DIDescriptor: dump routines for all descriptors.
344 //===----------------------------------------------------------------------===//
346 void DIDescriptor::dump() const {
351 void DIDescriptor::print(raw_ostream &OS) const {
357 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
358 const LLVMContext &Ctx) {
362 DIScope Scope = cast<MDScope>(DL.getScope());
363 // Omit the directory, because it's likely to be long and uninteresting.
364 CommentOS << Scope.getFilename();
365 CommentOS << ':' << DL.getLine();
366 if (DL.getCol() != 0)
367 CommentOS << ':' << DL.getCol();
369 DebugLoc InlinedAtDL = DL.getInlinedAt();
374 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
378 void DIVariable::printExtendedName(raw_ostream &OS) const {
379 const LLVMContext &Ctx = DbgNode->getContext();
380 StringRef Res = getName();
382 OS << Res << "," << getLineNumber();
383 if (auto *InlinedAt = get()->getInlinedAt()) {
384 if (DebugLoc InlinedAtDL = InlinedAt) {
386 printDebugLoc(InlinedAtDL, OS, Ctx);
394 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
395 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
398 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
399 return MDScopeRef(Val).resolve(Map);
402 DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
403 return MDTypeRef(Val).resolve(Map);
406 bool llvm::stripDebugInfo(Function &F) {
407 bool Changed = false;
408 for (BasicBlock &BB : F) {
409 for (Instruction &I : BB) {
410 if (I.getDebugLoc()) {
412 I.setDebugLoc(DebugLoc());
419 bool llvm::StripDebugInfo(Module &M) {
420 bool Changed = false;
422 // Remove all of the calls to the debugger intrinsics, and remove them from
424 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
425 while (!Declare->use_empty()) {
426 CallInst *CI = cast<CallInst>(Declare->user_back());
427 CI->eraseFromParent();
429 Declare->eraseFromParent();
433 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
434 while (!DbgVal->use_empty()) {
435 CallInst *CI = cast<CallInst>(DbgVal->user_back());
436 CI->eraseFromParent();
438 DbgVal->eraseFromParent();
442 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
443 NME = M.named_metadata_end(); NMI != NME;) {
444 NamedMDNode *NMD = NMI;
446 if (NMD->getName().startswith("llvm.dbg.")) {
447 NMD->eraseFromParent();
452 for (Function &F : M)
453 Changed |= stripDebugInfo(F);
455 if (GVMaterializer *Materializer = M.getMaterializer())
456 Materializer->setStripDebugInfo();
461 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
462 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
463 M.getModuleFlag("Debug Info Version")))
464 return Val->getZExtValue();
468 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
469 llvm::makeSubprogramMap(const Module &M) {
470 DenseMap<const Function *, DISubprogram> R;
472 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
476 for (MDNode *N : CU_Nodes->operands()) {
477 DICompileUnit CUNode = cast<MDCompileUnit>(N);
478 for (DISubprogram SP : CUNode->getSubprograms()) {
479 if (Function *F = SP.getFunction())
480 R.insert(std::make_pair(F, SP));