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/ADT/StringSwitch.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DIBuilder.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Intrinsics.h"
28 #include "llvm/IR/GVMaterializer.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/ValueHandle.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/Dwarf.h"
33 #include "llvm/Support/raw_ostream.h"
35 using namespace llvm::dwarf;
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
41 unsigned DIDescriptor::getFlag(StringRef Flag) {
42 return StringSwitch<unsigned>(Flag)
43 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
44 #include "llvm/IR/DebugInfoFlags.def"
48 const char *DIDescriptor::getFlagString(unsigned Flag) {
52 #define HANDLE_DI_FLAG(ID, NAME) \
54 return "DIFlag" #NAME;
55 #include "llvm/IR/DebugInfoFlags.def"
59 unsigned DIDescriptor::splitFlags(unsigned Flags,
60 SmallVectorImpl<unsigned> &SplitFlags) {
61 // Accessibility flags need to be specially handled, since they're packed
63 if (unsigned A = Flags & FlagAccessibility) {
65 SplitFlags.push_back(FlagPrivate);
66 else if (A == FlagProtected)
67 SplitFlags.push_back(FlagProtected);
69 SplitFlags.push_back(FlagPublic);
73 #define HANDLE_DI_FLAG(ID, NAME) \
74 if (unsigned Bit = Flags & ID) { \
75 SplitFlags.push_back(Bit); \
78 #include "llvm/IR/DebugInfoFlags.def"
83 bool DIDescriptor::Verify() const {
85 (DIDerivedType(DbgNode).Verify() ||
86 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
87 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
88 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
89 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
90 DILexicalBlock(DbgNode).Verify() ||
91 DILexicalBlockFile(DbgNode).Verify() ||
92 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
93 DIObjCProperty(DbgNode).Verify() ||
94 DITemplateTypeParameter(DbgNode).Verify() ||
95 DITemplateValueParameter(DbgNode).Verify() ||
96 DIImportedEntity(DbgNode).Verify());
99 static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
100 if (!DbgNode || Elt >= DbgNode->getNumOperands())
102 return DbgNode->getOperand(Elt);
105 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
106 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
109 static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
110 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
111 return MDS->getString();
115 StringRef DIDescriptor::getStringField(unsigned Elt) const {
116 return ::getStringField(DbgNode, Elt);
119 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
120 if (auto *C = getConstantField(Elt))
121 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
122 return CI->getZExtValue();
127 int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
128 if (auto *C = getConstantField(Elt))
129 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
130 return CI->getZExtValue();
135 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
136 MDNode *Field = getNodeField(DbgNode, Elt);
137 return DIDescriptor(Field);
140 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
141 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
144 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
148 if (Elt < DbgNode->getNumOperands())
150 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
151 return C->getValue();
155 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
156 return dyn_cast_or_null<Function>(getConstantField(Elt));
159 /// \brief Return the size reported by the variable's type.
160 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
161 DIType Ty = getType().resolve(Map);
162 // Follow derived types until we reach a type that
163 // reports back a size.
164 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
165 DIDerivedType DT(&*Ty);
166 Ty = DT.getTypeDerivedFrom().resolve(Map);
168 assert(Ty.getSizeInBits() && "type with size 0");
169 return Ty.getSizeInBits();
172 bool DIExpression::isBitPiece() const {
173 unsigned N = getNumElements();
174 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
177 uint64_t DIExpression::getBitPieceOffset() const {
178 assert(isBitPiece() && "not a piece");
179 return getElement(getNumElements()-2);
182 uint64_t DIExpression::getBitPieceSize() const {
183 assert(isBitPiece() && "not a piece");
184 return getElement(getNumElements()-1);
187 DIExpression::iterator DIExpression::Operand::getNext() const {
192 //===----------------------------------------------------------------------===//
193 // Simple Descriptor Constructors and other Methods
194 //===----------------------------------------------------------------------===//
196 void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
197 assert(DbgNode && "Trying to replace an unverified type!");
198 assert(DbgNode->isTemporary() && "Expected temporary node");
199 TempMDNode Temp(get());
201 // Since we use a TrackingVH for the node, its easy for clients to manufacture
202 // legitimate situations where they want to replaceAllUsesWith() on something
203 // which, due to uniquing, has merged with the source. We shield clients from
204 // this detail by allowing a value to be replaced with replaceAllUsesWith()
206 if (Temp.get() == D.get()) {
207 DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
211 Temp->replaceAllUsesWith(D.get());
215 void DIDescriptor::replaceAllUsesWith(MDNode *D) {
216 assert(DbgNode && "Trying to replace an unverified type!");
217 assert(DbgNode != D && "This replacement should always happen");
218 assert(DbgNode->isTemporary() && "Expected temporary node");
219 TempMDNode Node(get());
220 Node->replaceAllUsesWith(D);
223 bool DICompileUnit::Verify() const { return isCompileUnit(); }
224 bool DIObjCProperty::Verify() const { return isObjCProperty(); }
227 /// \brief Check if a value can be a reference to a type.
228 static bool isTypeRef(const Metadata *MD) {
231 if (auto *S = dyn_cast<MDString>(MD))
232 return !S->getString().empty();
233 return isa<MDType>(MD);
236 /// \brief Check if a value can be a ScopeRef.
237 static bool isScopeRef(const Metadata *MD) {
240 if (auto *S = dyn_cast<MDString>(MD))
241 return !S->getString().empty();
242 return isa<MDScope>(MD);
245 /// \brief Check if a value can be a DescriptorRef.
246 static bool isDescriptorRef(const Metadata *MD) {
249 if (auto *S = dyn_cast<MDString>(MD))
250 return !S->getString().empty();
251 return isa<MDNode>(MD);
255 bool DIType::Verify() const { return isType(); }
256 bool DIBasicType::Verify() const { return isBasicType(); }
257 bool DIDerivedType::Verify() const { return isDerivedType(); }
258 bool DICompositeType::Verify() const { return isCompositeType(); }
260 bool DISubprogram::Verify() const {
261 auto *N = dyn_cast_or_null<MDSubprogram>(DbgNode);
265 // If a DISubprogram has an llvm::Function*, then scope chains from all
266 // instructions within the function should lead to this DISubprogram.
267 if (auto *F = getFunction()) {
268 for (auto &BB : *F) {
270 MDLocation *DL = I.getDebugLoc();
274 // walk the inlined-at scopes
275 MDScope *Scope = DL->getInlinedAtScope();
278 while (!isa<MDSubprogram>(Scope)) {
279 Scope = cast<MDLexicalBlockBase>(Scope)->getScope();
283 if (!DISubprogram(Scope).describes(F))
292 bool DIGlobalVariable::Verify() const { return isGlobalVariable(); }
293 bool DIVariable::Verify() const { return isVariable(); }
295 bool DILocation::Verify() const {
296 return dyn_cast_or_null<MDLocation>(DbgNode);
298 bool DINameSpace::Verify() const {
299 return dyn_cast_or_null<MDNamespace>(DbgNode);
301 bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); }
302 bool DIEnumerator::Verify() const {
303 return dyn_cast_or_null<MDEnumerator>(DbgNode);
305 bool DISubrange::Verify() const {
306 return dyn_cast_or_null<MDSubrange>(DbgNode);
308 bool DILexicalBlock::Verify() const {
309 return dyn_cast_or_null<MDLexicalBlock>(DbgNode);
311 bool DILexicalBlockFile::Verify() const {
312 return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode);
314 bool DITemplateTypeParameter::Verify() const {
315 return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode);
317 bool DITemplateValueParameter::Verify() const {
318 return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode);
320 bool DIImportedEntity::Verify() const {
321 return dyn_cast_or_null<MDImportedEntity>(DbgNode);
324 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
325 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
327 N->replaceElements(cast<MDTuple>(Elements));
329 N->replaceTemplateParams(cast<MDTuple>(TParams));
333 DIScopeRef DIScope::getRef() const {
334 if (!isCompositeType())
335 return DIScopeRef(*this);
336 DICompositeType DTy(DbgNode);
337 if (!DTy.getIdentifier())
338 return DIScopeRef(*this);
339 return DIScopeRef(DTy.getIdentifier());
342 void DICompositeType::setContainingType(DICompositeType ContainingType) {
343 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
344 N->replaceVTableHolder(ContainingType.getRef());
348 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
349 assert(CurFn && "Invalid function");
350 if (!getContext().isSubprogram())
352 // This variable is not inlined function argument if its scope
353 // does not describe current function.
354 return !DISubprogram(getContext()).describes(CurFn);
357 Function *DISubprogram::getFunction() const {
359 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
360 return dyn_cast<Function>(C->getValue());
364 bool DISubprogram::describes(const Function *F) {
365 assert(F && "Invalid function");
366 if (F == getFunction())
368 StringRef Name = getLinkageName();
371 if (F->getName() == Name)
376 GlobalVariable *DIGlobalVariable::getGlobal() const {
377 return dyn_cast_or_null<GlobalVariable>(getConstant());
380 DIScopeRef DIScope::getContext() const {
383 return DIType(DbgNode).getContext();
386 return DIScopeRef(DISubprogram(DbgNode).getContext());
388 if (isLexicalBlock())
389 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
391 if (isLexicalBlockFile())
392 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
395 return DIScopeRef(DINameSpace(DbgNode).getContext());
397 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
398 return DIScopeRef(nullptr);
401 StringRef DIScope::getName() const {
403 return DIType(DbgNode).getName();
405 return DISubprogram(DbgNode).getName();
407 return DINameSpace(DbgNode).getName();
408 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
410 "Unhandled type of scope.");
414 StringRef DIScope::getFilename() const {
416 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0);
420 StringRef DIScope::getDirectory() const {
422 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1);
426 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
427 assert(Verify() && "Expected compile unit");
428 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
431 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
432 assert(Verify() && "Expected compile unit");
433 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
436 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
437 DILexicalBlockFile NewScope) {
439 assert(NewScope && "Expected valid scope");
441 const auto *Old = cast<MDLocation>(DbgNode);
442 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
443 NewScope, Old->getInlinedAt()));
446 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
447 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
448 return ++Ctx.pImpl->DiscriminatorTable[Key];
451 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
452 LLVMContext &VMContext) {
453 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
454 return cast<MDLocalVariable>(DV)
455 ->withInline(cast_or_null<MDLocation>(InlinedScope));
458 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
459 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
460 return cast<MDLocalVariable>(DV)->withoutInline();
463 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
464 DIDescriptor D(Scope);
465 if (D.isSubprogram())
466 return DISubprogram(Scope);
468 if (D.isLexicalBlockFile())
469 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
471 if (D.isLexicalBlock())
472 return getDISubprogram(DILexicalBlock(Scope).getContext());
474 return DISubprogram();
477 DISubprogram llvm::getDISubprogram(const Function *F) {
478 // We look for the first instr that has a debug annotation leading back to F.
479 for (auto &BB : *F) {
480 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
481 return Inst.getDebugLoc();
483 if (Inst == BB.end())
485 DebugLoc DLoc = Inst->getDebugLoc();
486 const MDNode *Scope = DLoc.getInlinedAtScope();
487 DISubprogram Subprogram = getDISubprogram(Scope);
488 return Subprogram.describes(F) ? Subprogram : DISubprogram();
491 return DISubprogram();
494 DICompositeType llvm::getDICompositeType(DIType T) {
495 if (T.isCompositeType())
496 return DICompositeType(T);
498 if (T.isDerivedType()) {
499 // This function is currently used by dragonegg and dragonegg does
500 // not generate identifier for types, so using an empty map to resolve
501 // DerivedFrom should be fine.
502 DITypeIdentifierMap EmptyMap;
503 return getDICompositeType(
504 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
507 return DICompositeType();
511 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
512 DITypeIdentifierMap Map;
513 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
514 DICompileUnit CU(CU_Nodes->getOperand(CUi));
515 DIArray Retain = CU.getRetainedTypes();
516 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
517 if (!Retain.getElement(Ti).isCompositeType())
519 DICompositeType Ty(Retain.getElement(Ti));
520 if (MDString *TypeId = Ty.getIdentifier()) {
521 // Definition has priority over declaration.
522 // Try to insert (TypeId, Ty) to Map.
523 std::pair<DITypeIdentifierMap::iterator, bool> P =
524 Map.insert(std::make_pair(TypeId, Ty));
525 // If TypeId already exists in Map and this is a definition, replace
526 // whatever we had (declaration or definition) with the definition.
527 if (!P.second && !Ty.isForwardDecl())
528 P.first->second = Ty;
535 //===----------------------------------------------------------------------===//
536 // DebugInfoFinder implementations.
537 //===----------------------------------------------------------------------===//
539 void DebugInfoFinder::reset() {
546 TypeIdentifierMap.clear();
547 TypeMapInitialized = false;
550 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
551 if (!TypeMapInitialized)
552 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
553 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
554 TypeMapInitialized = true;
558 void DebugInfoFinder::processModule(const Module &M) {
559 InitializeTypeMap(M);
560 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
561 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
562 DICompileUnit CU(CU_Nodes->getOperand(i));
564 DIArray GVs = CU.getGlobalVariables();
565 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
566 DIGlobalVariable DIG(GVs.getElement(i));
567 if (addGlobalVariable(DIG)) {
568 processScope(DIG.getContext());
569 processType(DIG.getType().resolve(TypeIdentifierMap));
572 DIArray SPs = CU.getSubprograms();
573 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
574 processSubprogram(DISubprogram(SPs.getElement(i)));
575 DIArray EnumTypes = CU.getEnumTypes();
576 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
577 processType(DIType(EnumTypes.getElement(i)));
578 DIArray RetainedTypes = CU.getRetainedTypes();
579 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
580 processType(DIType(RetainedTypes.getElement(i)));
581 DIArray Imports = CU.getImportedEntities();
582 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
583 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
586 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
588 processType(DIType(Entity));
589 else if (Entity.isSubprogram())
590 processSubprogram(DISubprogram(Entity));
591 else if (Entity.isNameSpace())
592 processScope(DINameSpace(Entity).getContext());
598 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
601 InitializeTypeMap(M);
602 processScope(Loc.getScope());
603 processLocation(M, Loc.getOrigLocation());
606 void DebugInfoFinder::processType(DIType DT) {
609 processScope(DT.getContext().resolve(TypeIdentifierMap));
610 if (DT.isCompositeType()) {
611 DICompositeType DCT(DT);
612 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
613 if (DT.isSubroutineType()) {
614 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
615 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
616 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
619 DIArray DA = DCT.getElements();
620 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
621 DIDescriptor D = DA.getElement(i);
623 processType(DIType(D));
624 else if (D.isSubprogram())
625 processSubprogram(DISubprogram(D));
627 } else if (DT.isDerivedType()) {
628 DIDerivedType DDT(DT);
629 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
633 void DebugInfoFinder::processScope(DIScope Scope) {
634 if (Scope.isType()) {
639 if (Scope.isCompileUnit()) {
640 addCompileUnit(DICompileUnit(Scope));
643 if (Scope.isSubprogram()) {
644 processSubprogram(DISubprogram(Scope));
647 if (!addScope(Scope))
649 if (Scope.isLexicalBlock()) {
650 DILexicalBlock LB(Scope);
651 processScope(LB.getContext());
652 } else if (Scope.isLexicalBlockFile()) {
653 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
654 processScope(LBF.getScope());
655 } else if (Scope.isNameSpace()) {
656 DINameSpace NS(Scope);
657 processScope(NS.getContext());
661 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
662 if (!addSubprogram(SP))
664 processScope(SP.getContext().resolve(TypeIdentifierMap));
665 processType(SP.getType());
666 DIArray TParams = SP.getTemplateParams();
667 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
668 DIDescriptor Element = TParams.getElement(I);
669 if (Element.isTemplateTypeParameter()) {
670 DITemplateTypeParameter TType(Element);
671 processType(TType.getType().resolve(TypeIdentifierMap));
672 } else if (Element.isTemplateValueParameter()) {
673 DITemplateValueParameter TVal(Element);
674 processType(TVal.getType().resolve(TypeIdentifierMap));
679 void DebugInfoFinder::processDeclare(const Module &M,
680 const DbgDeclareInst *DDI) {
681 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
684 InitializeTypeMap(M);
687 if (!DV.isVariable())
690 if (!NodesSeen.insert(DV).second)
692 processScope(DIVariable(N).getContext());
693 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
696 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
697 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
700 InitializeTypeMap(M);
703 if (!DV.isVariable())
706 if (!NodesSeen.insert(DV).second)
708 processScope(DIVariable(N).getContext());
709 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
712 bool DebugInfoFinder::addType(DIType DT) {
716 if (!NodesSeen.insert(DT).second)
723 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
726 if (!NodesSeen.insert(CU).second)
733 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
737 if (!NodesSeen.insert(DIG).second)
744 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
748 if (!NodesSeen.insert(SP).second)
755 bool DebugInfoFinder::addScope(DIScope Scope) {
758 // FIXME: Ocaml binding generates a scope with no content, we treat it
760 if (Scope->getNumOperands() == 0)
762 if (!NodesSeen.insert(Scope).second)
764 Scopes.push_back(Scope);
768 //===----------------------------------------------------------------------===//
769 // DIDescriptor: dump routines for all descriptors.
770 //===----------------------------------------------------------------------===//
772 void DIDescriptor::dump() const {
777 void DIDescriptor::print(raw_ostream &OS) const {
783 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
784 const LLVMContext &Ctx) {
788 DIScope Scope(DL.getScope());
789 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
790 // Omit the directory, because it's likely to be long and uninteresting.
791 CommentOS << Scope.getFilename();
792 CommentOS << ':' << DL.getLine();
793 if (DL.getCol() != 0)
794 CommentOS << ':' << DL.getCol();
796 DebugLoc InlinedAtDL = DL.getInlinedAt();
801 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
805 void DIVariable::printExtendedName(raw_ostream &OS) const {
806 const LLVMContext &Ctx = DbgNode->getContext();
807 StringRef Res = getName();
809 OS << Res << "," << getLineNumber();
810 if (auto *InlinedAt = get()->getInlinedAt()) {
811 if (DebugLoc InlinedAtDL = InlinedAt) {
813 printDebugLoc(InlinedAtDL, OS, Ctx);
819 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
820 assert(isDescriptorRef(V) &&
821 "DIDescriptorRef should be a MDString or MDNode");
823 template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
824 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
826 template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
827 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
831 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
832 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
835 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
836 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
838 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
839 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
842 bool llvm::stripDebugInfo(Function &F) {
843 bool Changed = false;
844 for (BasicBlock &BB : F) {
845 for (Instruction &I : BB) {
846 if (I.getDebugLoc()) {
848 I.setDebugLoc(DebugLoc());
855 bool llvm::StripDebugInfo(Module &M) {
856 bool Changed = false;
858 // Remove all of the calls to the debugger intrinsics, and remove them from
860 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
861 while (!Declare->use_empty()) {
862 CallInst *CI = cast<CallInst>(Declare->user_back());
863 CI->eraseFromParent();
865 Declare->eraseFromParent();
869 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
870 while (!DbgVal->use_empty()) {
871 CallInst *CI = cast<CallInst>(DbgVal->user_back());
872 CI->eraseFromParent();
874 DbgVal->eraseFromParent();
878 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
879 NME = M.named_metadata_end(); NMI != NME;) {
880 NamedMDNode *NMD = NMI;
882 if (NMD->getName().startswith("llvm.dbg.")) {
883 NMD->eraseFromParent();
888 for (Function &F : M)
889 Changed |= stripDebugInfo(F);
891 if ( GVMaterializer *Materializer = M.getMaterializer())
892 Materializer->setStripDebugInfo();
897 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
898 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
899 M.getModuleFlag("Debug Info Version")))
900 return Val->getZExtValue();
904 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
905 llvm::makeSubprogramMap(const Module &M) {
906 DenseMap<const Function *, DISubprogram> R;
908 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
912 for (MDNode *N : CU_Nodes->operands()) {
913 DICompileUnit CUNode(N);
914 DIArray SPs = CUNode.getSubprograms();
915 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
916 DISubprogram SP(SPs.getElement(i));
917 if (Function *F = SP.getFunction())
918 R.insert(std::make_pair(F, SP));