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(); }
259 bool DISubprogram::Verify() const { return isSubprogram(); }
260 bool DIGlobalVariable::Verify() const { return isGlobalVariable(); }
261 bool DIVariable::Verify() const { return isVariable(); }
263 bool DILocation::Verify() const {
264 return dyn_cast_or_null<MDLocation>(DbgNode);
266 bool DINameSpace::Verify() const {
267 return dyn_cast_or_null<MDNamespace>(DbgNode);
269 bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); }
270 bool DIEnumerator::Verify() const {
271 return dyn_cast_or_null<MDEnumerator>(DbgNode);
273 bool DISubrange::Verify() const {
274 return dyn_cast_or_null<MDSubrange>(DbgNode);
276 bool DILexicalBlock::Verify() const {
277 return dyn_cast_or_null<MDLexicalBlock>(DbgNode);
279 bool DILexicalBlockFile::Verify() const {
280 return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode);
282 bool DITemplateTypeParameter::Verify() const {
283 return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode);
285 bool DITemplateValueParameter::Verify() const {
286 return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode);
288 bool DIImportedEntity::Verify() const {
289 return dyn_cast_or_null<MDImportedEntity>(DbgNode);
292 void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
293 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
295 N->replaceElements(cast<MDTuple>(Elements));
297 N->replaceTemplateParams(cast<MDTuple>(TParams));
301 DIScopeRef DIScope::getRef() const {
302 if (!isCompositeType())
303 return DIScopeRef(*this);
304 DICompositeType DTy(DbgNode);
305 if (!DTy.getIdentifier())
306 return DIScopeRef(*this);
307 return DIScopeRef(DTy.getIdentifier());
310 void DICompositeType::setContainingType(DICompositeType ContainingType) {
311 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
312 N->replaceVTableHolder(ContainingType.getRef());
316 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
317 assert(CurFn && "Invalid function");
318 if (!getContext().isSubprogram())
320 // This variable is not inlined function argument if its scope
321 // does not describe current function.
322 return !DISubprogram(getContext()).describes(CurFn);
325 Function *DISubprogram::getFunction() const {
327 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
328 return dyn_cast<Function>(C->getValue());
332 bool DISubprogram::describes(const Function *F) {
333 assert(F && "Invalid function");
334 if (F == getFunction())
336 StringRef Name = getLinkageName();
339 if (F->getName() == Name)
344 GlobalVariable *DIGlobalVariable::getGlobal() const {
345 return dyn_cast_or_null<GlobalVariable>(getConstant());
348 DIScopeRef DIScope::getContext() const {
351 return DIType(DbgNode).getContext();
354 return DIScopeRef(DISubprogram(DbgNode).getContext());
356 if (isLexicalBlock())
357 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
359 if (isLexicalBlockFile())
360 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
363 return DIScopeRef(DINameSpace(DbgNode).getContext());
365 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
366 return DIScopeRef(nullptr);
369 StringRef DIScope::getName() const {
371 return DIType(DbgNode).getName();
373 return DISubprogram(DbgNode).getName();
375 return DINameSpace(DbgNode).getName();
376 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
378 "Unhandled type of scope.");
382 StringRef DIScope::getFilename() const {
384 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0);
388 StringRef DIScope::getDirectory() const {
390 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1);
394 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
395 assert(Verify() && "Expected compile unit");
396 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
399 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
400 assert(Verify() && "Expected compile unit");
401 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
404 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
405 DILexicalBlockFile NewScope) {
407 assert(NewScope && "Expected valid scope");
409 const auto *Old = cast<MDLocation>(DbgNode);
410 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
411 NewScope, Old->getInlinedAt()));
414 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
415 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
416 return ++Ctx.pImpl->DiscriminatorTable[Key];
419 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
420 LLVMContext &VMContext) {
421 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
422 return cast<MDLocalVariable>(DV)
423 ->withInline(cast_or_null<MDLocation>(InlinedScope));
426 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
427 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
428 return cast<MDLocalVariable>(DV)->withoutInline();
431 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
432 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
433 return LocalScope->getSubprogram();
437 DISubprogram llvm::getDISubprogram(const Function *F) {
438 // We look for the first instr that has a debug annotation leading back to F.
439 for (auto &BB : *F) {
440 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
441 return Inst.getDebugLoc();
443 if (Inst == BB.end())
445 DebugLoc DLoc = Inst->getDebugLoc();
446 const MDNode *Scope = DLoc.getInlinedAtScope();
447 DISubprogram Subprogram = getDISubprogram(Scope);
448 return Subprogram.describes(F) ? Subprogram : DISubprogram();
451 return DISubprogram();
454 DICompositeType llvm::getDICompositeType(DIType T) {
455 if (T.isCompositeType())
456 return DICompositeType(T);
458 if (T.isDerivedType()) {
459 // This function is currently used by dragonegg and dragonegg does
460 // not generate identifier for types, so using an empty map to resolve
461 // DerivedFrom should be fine.
462 DITypeIdentifierMap EmptyMap;
463 return getDICompositeType(
464 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
467 return DICompositeType();
471 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
472 DITypeIdentifierMap Map;
473 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
474 DICompileUnit CU(CU_Nodes->getOperand(CUi));
475 DIArray Retain = CU.getRetainedTypes();
476 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
477 if (!Retain.getElement(Ti).isCompositeType())
479 DICompositeType Ty(Retain.getElement(Ti));
480 if (MDString *TypeId = Ty.getIdentifier()) {
481 // Definition has priority over declaration.
482 // Try to insert (TypeId, Ty) to Map.
483 std::pair<DITypeIdentifierMap::iterator, bool> P =
484 Map.insert(std::make_pair(TypeId, Ty));
485 // If TypeId already exists in Map and this is a definition, replace
486 // whatever we had (declaration or definition) with the definition.
487 if (!P.second && !Ty.isForwardDecl())
488 P.first->second = Ty;
495 //===----------------------------------------------------------------------===//
496 // DebugInfoFinder implementations.
497 //===----------------------------------------------------------------------===//
499 void DebugInfoFinder::reset() {
506 TypeIdentifierMap.clear();
507 TypeMapInitialized = false;
510 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
511 if (!TypeMapInitialized)
512 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
513 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
514 TypeMapInitialized = true;
518 void DebugInfoFinder::processModule(const Module &M) {
519 InitializeTypeMap(M);
520 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
521 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
522 DICompileUnit CU(CU_Nodes->getOperand(i));
524 DIArray GVs = CU.getGlobalVariables();
525 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
526 DIGlobalVariable DIG(GVs.getElement(i));
527 if (addGlobalVariable(DIG)) {
528 processScope(DIG.getContext());
529 processType(DIG.getType().resolve(TypeIdentifierMap));
532 DIArray SPs = CU.getSubprograms();
533 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
534 processSubprogram(DISubprogram(SPs.getElement(i)));
535 DIArray EnumTypes = CU.getEnumTypes();
536 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
537 processType(DIType(EnumTypes.getElement(i)));
538 DIArray RetainedTypes = CU.getRetainedTypes();
539 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
540 processType(DIType(RetainedTypes.getElement(i)));
541 DIArray Imports = CU.getImportedEntities();
542 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
543 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
546 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
548 processType(DIType(Entity));
549 else if (Entity.isSubprogram())
550 processSubprogram(DISubprogram(Entity));
551 else if (Entity.isNameSpace())
552 processScope(DINameSpace(Entity).getContext());
558 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
561 InitializeTypeMap(M);
562 processScope(Loc.getScope());
563 processLocation(M, Loc.getOrigLocation());
566 void DebugInfoFinder::processType(DIType DT) {
569 processScope(DT.getContext().resolve(TypeIdentifierMap));
570 if (DT.isCompositeType()) {
571 DICompositeType DCT(DT);
572 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
573 if (DT.isSubroutineType()) {
574 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
575 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
576 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
579 DIArray DA = DCT.getElements();
580 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
581 DIDescriptor D = DA.getElement(i);
583 processType(DIType(D));
584 else if (D.isSubprogram())
585 processSubprogram(DISubprogram(D));
587 } else if (DT.isDerivedType()) {
588 DIDerivedType DDT(DT);
589 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
593 void DebugInfoFinder::processScope(DIScope Scope) {
594 if (Scope.isType()) {
599 if (Scope.isCompileUnit()) {
600 addCompileUnit(DICompileUnit(Scope));
603 if (Scope.isSubprogram()) {
604 processSubprogram(DISubprogram(Scope));
607 if (!addScope(Scope))
609 if (Scope.isLexicalBlock()) {
610 DILexicalBlock LB(Scope);
611 processScope(LB.getContext());
612 } else if (Scope.isLexicalBlockFile()) {
613 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
614 processScope(LBF.getScope());
615 } else if (Scope.isNameSpace()) {
616 DINameSpace NS(Scope);
617 processScope(NS.getContext());
621 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
622 if (!addSubprogram(SP))
624 processScope(SP.getContext().resolve(TypeIdentifierMap));
625 processType(SP.getType());
626 DIArray TParams = SP.getTemplateParams();
627 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
628 DIDescriptor Element = TParams.getElement(I);
629 if (Element.isTemplateTypeParameter()) {
630 DITemplateTypeParameter TType(Element);
631 processType(TType.getType().resolve(TypeIdentifierMap));
632 } else if (Element.isTemplateValueParameter()) {
633 DITemplateValueParameter TVal(Element);
634 processType(TVal.getType().resolve(TypeIdentifierMap));
639 void DebugInfoFinder::processDeclare(const Module &M,
640 const DbgDeclareInst *DDI) {
641 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
644 InitializeTypeMap(M);
647 if (!DV.isVariable())
650 if (!NodesSeen.insert(DV).second)
652 processScope(DIVariable(N).getContext());
653 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
656 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
657 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
660 InitializeTypeMap(M);
663 if (!DV.isVariable())
666 if (!NodesSeen.insert(DV).second)
668 processScope(DIVariable(N).getContext());
669 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
672 bool DebugInfoFinder::addType(DIType DT) {
676 if (!NodesSeen.insert(DT).second)
683 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
686 if (!NodesSeen.insert(CU).second)
693 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
697 if (!NodesSeen.insert(DIG).second)
704 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
708 if (!NodesSeen.insert(SP).second)
715 bool DebugInfoFinder::addScope(DIScope Scope) {
718 // FIXME: Ocaml binding generates a scope with no content, we treat it
720 if (Scope->getNumOperands() == 0)
722 if (!NodesSeen.insert(Scope).second)
724 Scopes.push_back(Scope);
728 //===----------------------------------------------------------------------===//
729 // DIDescriptor: dump routines for all descriptors.
730 //===----------------------------------------------------------------------===//
732 void DIDescriptor::dump() const {
737 void DIDescriptor::print(raw_ostream &OS) const {
743 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
744 const LLVMContext &Ctx) {
748 DIScope Scope(DL.getScope());
749 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
750 // Omit the directory, because it's likely to be long and uninteresting.
751 CommentOS << Scope.getFilename();
752 CommentOS << ':' << DL.getLine();
753 if (DL.getCol() != 0)
754 CommentOS << ':' << DL.getCol();
756 DebugLoc InlinedAtDL = DL.getInlinedAt();
761 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
765 void DIVariable::printExtendedName(raw_ostream &OS) const {
766 const LLVMContext &Ctx = DbgNode->getContext();
767 StringRef Res = getName();
769 OS << Res << "," << getLineNumber();
770 if (auto *InlinedAt = get()->getInlinedAt()) {
771 if (DebugLoc InlinedAtDL = InlinedAt) {
773 printDebugLoc(InlinedAtDL, OS, Ctx);
779 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
780 assert(isDescriptorRef(V) &&
781 "DIDescriptorRef should be a MDString or MDNode");
783 template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
784 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
786 template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
787 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
791 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
792 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
795 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
796 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
798 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
799 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
802 bool llvm::stripDebugInfo(Function &F) {
803 bool Changed = false;
804 for (BasicBlock &BB : F) {
805 for (Instruction &I : BB) {
806 if (I.getDebugLoc()) {
808 I.setDebugLoc(DebugLoc());
815 bool llvm::StripDebugInfo(Module &M) {
816 bool Changed = false;
818 // Remove all of the calls to the debugger intrinsics, and remove them from
820 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
821 while (!Declare->use_empty()) {
822 CallInst *CI = cast<CallInst>(Declare->user_back());
823 CI->eraseFromParent();
825 Declare->eraseFromParent();
829 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
830 while (!DbgVal->use_empty()) {
831 CallInst *CI = cast<CallInst>(DbgVal->user_back());
832 CI->eraseFromParent();
834 DbgVal->eraseFromParent();
838 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
839 NME = M.named_metadata_end(); NMI != NME;) {
840 NamedMDNode *NMD = NMI;
842 if (NMD->getName().startswith("llvm.dbg.")) {
843 NMD->eraseFromParent();
848 for (Function &F : M)
849 Changed |= stripDebugInfo(F);
851 if (GVMaterializer *Materializer = M.getMaterializer())
852 Materializer->setStripDebugInfo();
857 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
858 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
859 M.getModuleFlag("Debug Info Version")))
860 return Val->getZExtValue();
864 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
865 llvm::makeSubprogramMap(const Module &M) {
866 DenseMap<const Function *, DISubprogram> R;
868 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
872 for (MDNode *N : CU_Nodes->operands()) {
873 DICompileUnit CUNode(N);
874 DIArray SPs = CUNode.getSubprograms();
875 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
876 DISubprogram SP(SPs.getElement(i));
877 if (Function *F = SP.getFunction())
878 R.insert(std::make_pair(F, SP));