1 //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the helper classes used to build and interpret debug
10 // information in LLVM IR form.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/DebugInfo.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/None.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DebugInfoMetadata.h"
25 #include "llvm/IR/DebugLoc.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/DIBuilder.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/GVMaterializer.h"
30 #include "llvm/IR/Instruction.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/Support/Casting.h"
41 using namespace llvm::dwarf
;
43 /// Finds all intrinsics declaring local variables as living in the memory that
44 /// 'V' points to. This may include a mix of dbg.declare and
45 /// dbg.addr intrinsics.
46 TinyPtrVector
<DbgVariableIntrinsic
*> llvm::FindDbgAddrUses(Value
*V
) {
47 // This function is hot. Check whether the value has any metadata to avoid a
49 if (!V
->isUsedByMetadata())
51 auto *L
= LocalAsMetadata::getIfExists(V
);
54 auto *MDV
= MetadataAsValue::getIfExists(V
->getContext(), L
);
58 TinyPtrVector
<DbgVariableIntrinsic
*> Declares
;
59 for (User
*U
: MDV
->users()) {
60 if (auto *DII
= dyn_cast
<DbgVariableIntrinsic
>(U
))
61 if (DII
->isAddressOfVariable())
62 Declares
.push_back(DII
);
68 TinyPtrVector
<DbgDeclareInst
*> llvm::FindDbgDeclareUses(Value
*V
) {
69 TinyPtrVector
<DbgDeclareInst
*> DDIs
;
70 for (DbgVariableIntrinsic
*DVI
: FindDbgAddrUses(V
))
71 if (auto *DDI
= dyn_cast
<DbgDeclareInst
>(DVI
))
76 void llvm::findDbgValues(SmallVectorImpl
<DbgValueInst
*> &DbgValues
, Value
*V
) {
77 // This function is hot. Check whether the value has any metadata to avoid a
79 if (!V
->isUsedByMetadata())
81 // TODO: If this value appears multiple times in a DIArgList, we should still
82 // only add the owning DbgValueInst once; use this set to track ArgListUsers.
83 // This behaviour can be removed when we can automatically remove duplicates.
84 SmallPtrSet
<DbgValueInst
*, 4> EncounteredDbgValues
;
85 if (auto *L
= LocalAsMetadata::getIfExists(V
)) {
86 if (auto *MDV
= MetadataAsValue::getIfExists(V
->getContext(), L
)) {
87 for (User
*U
: MDV
->users())
88 if (DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(U
))
89 DbgValues
.push_back(DVI
);
91 for (Metadata
*AL
: L
->getAllArgListUsers()) {
92 if (auto *MDV
= MetadataAsValue::getIfExists(V
->getContext(), AL
)) {
93 for (User
*U
: MDV
->users())
94 if (DbgValueInst
*DVI
= dyn_cast
<DbgValueInst
>(U
))
95 if (EncounteredDbgValues
.insert(DVI
).second
)
96 DbgValues
.push_back(DVI
);
102 void llvm::findDbgUsers(SmallVectorImpl
<DbgVariableIntrinsic
*> &DbgUsers
,
104 // This function is hot. Check whether the value has any metadata to avoid a
106 if (!V
->isUsedByMetadata())
108 // TODO: If this value appears multiple times in a DIArgList, we should still
109 // only add the owning DbgValueInst once; use this set to track ArgListUsers.
110 // This behaviour can be removed when we can automatically remove duplicates.
111 SmallPtrSet
<DbgVariableIntrinsic
*, 4> EncounteredDbgValues
;
112 if (auto *L
= LocalAsMetadata::getIfExists(V
)) {
113 if (auto *MDV
= MetadataAsValue::getIfExists(V
->getContext(), L
)) {
114 for (User
*U
: MDV
->users())
115 if (DbgVariableIntrinsic
*DII
= dyn_cast
<DbgVariableIntrinsic
>(U
))
116 DbgUsers
.push_back(DII
);
118 for (Metadata
*AL
: L
->getAllArgListUsers()) {
119 if (auto *MDV
= MetadataAsValue::getIfExists(V
->getContext(), AL
)) {
120 for (User
*U
: MDV
->users())
121 if (DbgVariableIntrinsic
*DII
= dyn_cast
<DbgVariableIntrinsic
>(U
))
122 if (EncounteredDbgValues
.insert(DII
).second
)
123 DbgUsers
.push_back(DII
);
129 DISubprogram
*llvm::getDISubprogram(const MDNode
*Scope
) {
130 if (auto *LocalScope
= dyn_cast_or_null
<DILocalScope
>(Scope
))
131 return LocalScope
->getSubprogram();
135 //===----------------------------------------------------------------------===//
136 // DebugInfoFinder implementations.
137 //===----------------------------------------------------------------------===//
139 void DebugInfoFinder::reset() {
148 void DebugInfoFinder::processModule(const Module
&M
) {
149 for (auto *CU
: M
.debug_compile_units())
150 processCompileUnit(CU
);
151 for (auto &F
: M
.functions()) {
152 if (auto *SP
= cast_or_null
<DISubprogram
>(F
.getSubprogram()))
153 processSubprogram(SP
);
154 // There could be subprograms from inlined functions referenced from
155 // instructions only. Walk the function to find them.
156 for (const BasicBlock
&BB
: F
)
157 for (const Instruction
&I
: BB
)
158 processInstruction(M
, I
);
162 void DebugInfoFinder::processCompileUnit(DICompileUnit
*CU
) {
163 if (!addCompileUnit(CU
))
165 for (auto DIG
: CU
->getGlobalVariables()) {
166 if (!addGlobalVariable(DIG
))
168 auto *GV
= DIG
->getVariable();
169 processScope(GV
->getScope());
170 processType(GV
->getType());
172 for (auto *ET
: CU
->getEnumTypes())
174 for (auto *RT
: CU
->getRetainedTypes())
175 if (auto *T
= dyn_cast
<DIType
>(RT
))
178 processSubprogram(cast
<DISubprogram
>(RT
));
179 for (auto *Import
: CU
->getImportedEntities()) {
180 auto *Entity
= Import
->getEntity();
181 if (auto *T
= dyn_cast
<DIType
>(Entity
))
183 else if (auto *SP
= dyn_cast
<DISubprogram
>(Entity
))
184 processSubprogram(SP
);
185 else if (auto *NS
= dyn_cast
<DINamespace
>(Entity
))
186 processScope(NS
->getScope());
187 else if (auto *M
= dyn_cast
<DIModule
>(Entity
))
188 processScope(M
->getScope());
192 void DebugInfoFinder::processInstruction(const Module
&M
,
193 const Instruction
&I
) {
194 if (auto *DVI
= dyn_cast
<DbgVariableIntrinsic
>(&I
))
195 processVariable(M
, *DVI
);
197 if (auto DbgLoc
= I
.getDebugLoc())
198 processLocation(M
, DbgLoc
.get());
201 void DebugInfoFinder::processLocation(const Module
&M
, const DILocation
*Loc
) {
204 processScope(Loc
->getScope());
205 processLocation(M
, Loc
->getInlinedAt());
208 void DebugInfoFinder::processType(DIType
*DT
) {
211 processScope(DT
->getScope());
212 if (auto *ST
= dyn_cast
<DISubroutineType
>(DT
)) {
213 for (DIType
*Ref
: ST
->getTypeArray())
217 if (auto *DCT
= dyn_cast
<DICompositeType
>(DT
)) {
218 processType(DCT
->getBaseType());
219 for (Metadata
*D
: DCT
->getElements()) {
220 if (auto *T
= dyn_cast
<DIType
>(D
))
222 else if (auto *SP
= dyn_cast
<DISubprogram
>(D
))
223 processSubprogram(SP
);
227 if (auto *DDT
= dyn_cast
<DIDerivedType
>(DT
)) {
228 processType(DDT
->getBaseType());
232 void DebugInfoFinder::processScope(DIScope
*Scope
) {
235 if (auto *Ty
= dyn_cast
<DIType
>(Scope
)) {
239 if (auto *CU
= dyn_cast
<DICompileUnit
>(Scope
)) {
243 if (auto *SP
= dyn_cast
<DISubprogram
>(Scope
)) {
244 processSubprogram(SP
);
247 if (!addScope(Scope
))
249 if (auto *LB
= dyn_cast
<DILexicalBlockBase
>(Scope
)) {
250 processScope(LB
->getScope());
251 } else if (auto *NS
= dyn_cast
<DINamespace
>(Scope
)) {
252 processScope(NS
->getScope());
253 } else if (auto *M
= dyn_cast
<DIModule
>(Scope
)) {
254 processScope(M
->getScope());
258 void DebugInfoFinder::processSubprogram(DISubprogram
*SP
) {
259 if (!addSubprogram(SP
))
261 processScope(SP
->getScope());
262 // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
263 // ValueMap containing identity mappings for all of the DICompileUnit's, not
264 // just DISubprogram's, referenced from anywhere within the Function being
265 // cloned prior to calling MapMetadata / RemapInstruction to avoid their
266 // duplication later as DICompileUnit's are also directly referenced by
267 // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
268 // Also, DICompileUnit's may reference DISubprogram's too and therefore need
269 // to be at least looked through.
270 processCompileUnit(SP
->getUnit());
271 processType(SP
->getType());
272 for (auto *Element
: SP
->getTemplateParams()) {
273 if (auto *TType
= dyn_cast
<DITemplateTypeParameter
>(Element
)) {
274 processType(TType
->getType());
275 } else if (auto *TVal
= dyn_cast
<DITemplateValueParameter
>(Element
)) {
276 processType(TVal
->getType());
281 void DebugInfoFinder::processVariable(const Module
&M
,
282 const DbgVariableIntrinsic
&DVI
) {
283 auto *N
= dyn_cast
<MDNode
>(DVI
.getVariable());
287 auto *DV
= dyn_cast
<DILocalVariable
>(N
);
291 if (!NodesSeen
.insert(DV
).second
)
293 processScope(DV
->getScope());
294 processType(DV
->getType());
297 bool DebugInfoFinder::addType(DIType
*DT
) {
301 if (!NodesSeen
.insert(DT
).second
)
304 TYs
.push_back(const_cast<DIType
*>(DT
));
308 bool DebugInfoFinder::addCompileUnit(DICompileUnit
*CU
) {
311 if (!NodesSeen
.insert(CU
).second
)
318 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression
*DIG
) {
319 if (!NodesSeen
.insert(DIG
).second
)
326 bool DebugInfoFinder::addSubprogram(DISubprogram
*SP
) {
330 if (!NodesSeen
.insert(SP
).second
)
337 bool DebugInfoFinder::addScope(DIScope
*Scope
) {
340 // FIXME: Ocaml binding generates a scope with no content, we treat it
342 if (Scope
->getNumOperands() == 0)
344 if (!NodesSeen
.insert(Scope
).second
)
346 Scopes
.push_back(Scope
);
350 static MDNode
*updateLoopMetadataDebugLocationsImpl(
351 MDNode
*OrigLoopID
, function_ref
<Metadata
*(Metadata
*)> Updater
) {
352 assert(OrigLoopID
&& OrigLoopID
->getNumOperands() > 0 &&
353 "Loop ID needs at least one operand");
354 assert(OrigLoopID
&& OrigLoopID
->getOperand(0).get() == OrigLoopID
&&
355 "Loop ID should refer to itself");
357 // Save space for the self-referential LoopID.
358 SmallVector
<Metadata
*, 4> MDs
= {nullptr};
360 for (unsigned i
= 1; i
< OrigLoopID
->getNumOperands(); ++i
) {
361 Metadata
*MD
= OrigLoopID
->getOperand(i
);
363 MDs
.push_back(nullptr);
364 else if (Metadata
*NewMD
= Updater(MD
))
365 MDs
.push_back(NewMD
);
368 MDNode
*NewLoopID
= MDNode::getDistinct(OrigLoopID
->getContext(), MDs
);
369 // Insert the self-referential LoopID.
370 NewLoopID
->replaceOperandWith(0, NewLoopID
);
374 void llvm::updateLoopMetadataDebugLocations(
375 Instruction
&I
, function_ref
<Metadata
*(Metadata
*)> Updater
) {
376 MDNode
*OrigLoopID
= I
.getMetadata(LLVMContext::MD_loop
);
379 MDNode
*NewLoopID
= updateLoopMetadataDebugLocationsImpl(OrigLoopID
, Updater
);
380 I
.setMetadata(LLVMContext::MD_loop
, NewLoopID
);
383 /// Return true if a node is a DILocation or if a DILocation is
384 /// indirectly referenced by one of the node's children.
385 static bool isDILocationReachable(SmallPtrSetImpl
<Metadata
*> &Visited
,
386 SmallPtrSetImpl
<Metadata
*> &Reachable
,
388 MDNode
*N
= dyn_cast_or_null
<MDNode
>(MD
);
391 if (isa
<DILocation
>(N
) || Reachable
.count(N
))
393 if (!Visited
.insert(N
).second
)
395 for (auto &OpIt
: N
->operands()) {
396 Metadata
*Op
= OpIt
.get();
397 if (isDILocationReachable(Visited
, Reachable
, Op
)) {
405 static MDNode
*stripDebugLocFromLoopID(MDNode
*N
) {
406 assert(!N
->operands().empty() && "Missing self reference?");
407 SmallPtrSet
<Metadata
*, 8> Visited
, DILocationReachable
;
408 // If we already visited N, there is nothing to do.
409 if (!Visited
.insert(N
).second
)
412 // If there is no debug location, we do not have to rewrite this
413 // MDNode. This loop also initializes DILocationReachable, later
414 // needed by updateLoopMetadataDebugLocationsImpl; the use of
415 // count_if avoids an early exit.
416 if (!std::count_if(N
->op_begin() + 1, N
->op_end(),
417 [&Visited
, &DILocationReachable
](const MDOperand
&Op
) {
418 return isDILocationReachable(
419 Visited
, DILocationReachable
, Op
.get());
423 // If there is only the debug location without any actual loop metadata, we
424 // can remove the metadata.
426 N
->op_begin() + 1, N
->op_end(),
427 [&Visited
, &DILocationReachable
](const MDOperand
&Op
) {
428 return isDILocationReachable(Visited
, DILocationReachable
,
433 return updateLoopMetadataDebugLocationsImpl(
434 N
, [&DILocationReachable
](Metadata
*MD
) -> Metadata
* {
435 if (isa
<DILocation
>(MD
) || DILocationReachable
.count(MD
))
441 bool llvm::stripDebugInfo(Function
&F
) {
442 bool Changed
= false;
443 if (F
.hasMetadata(LLVMContext::MD_dbg
)) {
445 F
.setSubprogram(nullptr);
448 DenseMap
<MDNode
*, MDNode
*> LoopIDsMap
;
449 for (BasicBlock
&BB
: F
) {
450 for (auto II
= BB
.begin(), End
= BB
.end(); II
!= End
;) {
451 Instruction
&I
= *II
++; // We may delete the instruction, increment now.
452 if (isa
<DbgInfoIntrinsic
>(&I
)) {
457 if (I
.getDebugLoc()) {
459 I
.setDebugLoc(DebugLoc());
461 if (auto *LoopID
= I
.getMetadata(LLVMContext::MD_loop
)) {
462 auto *NewLoopID
= LoopIDsMap
.lookup(LoopID
);
464 NewLoopID
= LoopIDsMap
[LoopID
] = stripDebugLocFromLoopID(LoopID
);
465 if (NewLoopID
!= LoopID
)
466 I
.setMetadata(LLVMContext::MD_loop
, NewLoopID
);
468 // Strip heapallocsite attachments, they point into the DIType system.
469 if (I
.hasMetadataOtherThanDebugLoc())
470 I
.setMetadata("heapallocsite", nullptr);
476 bool llvm::StripDebugInfo(Module
&M
) {
477 bool Changed
= false;
479 for (NamedMDNode
&NMD
: llvm::make_early_inc_range(M
.named_metadata())) {
480 // We're stripping debug info, and without them, coverage information
481 // doesn't quite make sense.
482 if (NMD
.getName().startswith("llvm.dbg.") ||
483 NMD
.getName() == "llvm.gcov") {
484 NMD
.eraseFromParent();
489 for (Function
&F
: M
)
490 Changed
|= stripDebugInfo(F
);
492 for (auto &GV
: M
.globals()) {
493 Changed
|= GV
.eraseMetadata(LLVMContext::MD_dbg
);
496 if (GVMaterializer
*Materializer
= M
.getMaterializer())
497 Materializer
->setStripDebugInfo();
504 /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
505 class DebugTypeInfoRemoval
{
506 DenseMap
<Metadata
*, Metadata
*> Replacements
;
509 /// The (void)() type.
510 MDNode
*EmptySubroutineType
;
513 /// Remember what linkage name we originally had before stripping. If we end
514 /// up making two subprograms identical who originally had different linkage
515 /// names, then we need to make one of them distinct, to avoid them getting
516 /// uniqued. Maps the new node to the old linkage name.
517 DenseMap
<DISubprogram
*, StringRef
> NewToLinkageName
;
519 // TODO: Remember the distinct subprogram we created for a given linkage name,
520 // so that we can continue to unique whenever possible. Map <newly created
521 // node, old linkage name> to the first (possibly distinct) mdsubprogram
522 // created for that combination. This is not strictly needed for correctness,
523 // but can cut down on the number of MDNodes and let us diff cleanly with the
524 // output of -gline-tables-only.
527 DebugTypeInfoRemoval(LLVMContext
&C
)
528 : EmptySubroutineType(DISubroutineType::get(C
, DINode::FlagZero
, 0,
529 MDNode::get(C
, {}))) {}
531 Metadata
*map(Metadata
*M
) {
534 auto Replacement
= Replacements
.find(M
);
535 if (Replacement
!= Replacements
.end())
536 return Replacement
->second
;
540 MDNode
*mapNode(Metadata
*N
) { return dyn_cast_or_null
<MDNode
>(map(N
)); }
542 /// Recursively remap N and all its referenced children. Does a DF post-order
543 /// traversal, so as to remap bottoms up.
544 void traverseAndRemap(MDNode
*N
) { traverse(N
); }
547 // Create a new DISubprogram, to replace the one given.
548 DISubprogram
*getReplacementSubprogram(DISubprogram
*MDS
) {
549 auto *FileAndScope
= cast_or_null
<DIFile
>(map(MDS
->getFile()));
550 StringRef LinkageName
= MDS
->getName().empty() ? MDS
->getLinkageName() : "";
551 DISubprogram
*Declaration
= nullptr;
552 auto *Type
= cast_or_null
<DISubroutineType
>(map(MDS
->getType()));
553 DIType
*ContainingType
=
554 cast_or_null
<DIType
>(map(MDS
->getContainingType()));
555 auto *Unit
= cast_or_null
<DICompileUnit
>(map(MDS
->getUnit()));
556 auto Variables
= nullptr;
557 auto TemplateParams
= nullptr;
559 // Make a distinct DISubprogram, for situations that warrent it.
560 auto distinctMDSubprogram
= [&]() {
561 return DISubprogram::getDistinct(
562 MDS
->getContext(), FileAndScope
, MDS
->getName(), LinkageName
,
563 FileAndScope
, MDS
->getLine(), Type
, MDS
->getScopeLine(),
564 ContainingType
, MDS
->getVirtualIndex(), MDS
->getThisAdjustment(),
565 MDS
->getFlags(), MDS
->getSPFlags(), Unit
, TemplateParams
, Declaration
,
569 if (MDS
->isDistinct())
570 return distinctMDSubprogram();
572 auto *NewMDS
= DISubprogram::get(
573 MDS
->getContext(), FileAndScope
, MDS
->getName(), LinkageName
,
574 FileAndScope
, MDS
->getLine(), Type
, MDS
->getScopeLine(), ContainingType
,
575 MDS
->getVirtualIndex(), MDS
->getThisAdjustment(), MDS
->getFlags(),
576 MDS
->getSPFlags(), Unit
, TemplateParams
, Declaration
, Variables
);
578 StringRef OldLinkageName
= MDS
->getLinkageName();
580 // See if we need to make a distinct one.
581 auto OrigLinkage
= NewToLinkageName
.find(NewMDS
);
582 if (OrigLinkage
!= NewToLinkageName
.end()) {
583 if (OrigLinkage
->second
== OldLinkageName
)
587 // Otherwise, need to make a distinct one.
588 // TODO: Query the map to see if we already have one.
589 return distinctMDSubprogram();
592 NewToLinkageName
.insert({NewMDS
, MDS
->getLinkageName()});
596 /// Create a new compile unit, to replace the one given
597 DICompileUnit
*getReplacementCU(DICompileUnit
*CU
) {
598 // Drop skeleton CUs.
602 auto *File
= cast_or_null
<DIFile
>(map(CU
->getFile()));
603 MDTuple
*EnumTypes
= nullptr;
604 MDTuple
*RetainedTypes
= nullptr;
605 MDTuple
*GlobalVariables
= nullptr;
606 MDTuple
*ImportedEntities
= nullptr;
607 return DICompileUnit::getDistinct(
608 CU
->getContext(), CU
->getSourceLanguage(), File
, CU
->getProducer(),
609 CU
->isOptimized(), CU
->getFlags(), CU
->getRuntimeVersion(),
610 CU
->getSplitDebugFilename(), DICompileUnit::LineTablesOnly
, EnumTypes
,
611 RetainedTypes
, GlobalVariables
, ImportedEntities
, CU
->getMacros(),
612 CU
->getDWOId(), CU
->getSplitDebugInlining(),
613 CU
->getDebugInfoForProfiling(), CU
->getNameTableKind(),
614 CU
->getRangesBaseAddress(), CU
->getSysRoot(), CU
->getSDK());
617 DILocation
*getReplacementMDLocation(DILocation
*MLD
) {
618 auto *Scope
= map(MLD
->getScope());
619 auto *InlinedAt
= map(MLD
->getInlinedAt());
620 if (MLD
->isDistinct())
621 return DILocation::getDistinct(MLD
->getContext(), MLD
->getLine(),
622 MLD
->getColumn(), Scope
, InlinedAt
);
623 return DILocation::get(MLD
->getContext(), MLD
->getLine(), MLD
->getColumn(),
627 /// Create a new generic MDNode, to replace the one given
628 MDNode
*getReplacementMDNode(MDNode
*N
) {
629 SmallVector
<Metadata
*, 8> Ops
;
630 Ops
.reserve(N
->getNumOperands());
631 for (auto &I
: N
->operands())
633 Ops
.push_back(map(I
));
634 auto *Ret
= MDNode::get(N
->getContext(), Ops
);
638 /// Attempt to re-map N to a newly created node.
639 void remap(MDNode
*N
) {
640 if (Replacements
.count(N
))
643 auto doRemap
= [&](MDNode
*N
) -> MDNode
* {
646 if (auto *MDSub
= dyn_cast
<DISubprogram
>(N
)) {
647 remap(MDSub
->getUnit());
648 return getReplacementSubprogram(MDSub
);
650 if (isa
<DISubroutineType
>(N
))
651 return EmptySubroutineType
;
652 if (auto *CU
= dyn_cast
<DICompileUnit
>(N
))
653 return getReplacementCU(CU
);
656 if (auto *MDLB
= dyn_cast
<DILexicalBlockBase
>(N
))
657 // Remap to our referenced scope (recursively).
658 return mapNode(MDLB
->getScope());
659 if (auto *MLD
= dyn_cast
<DILocation
>(N
))
660 return getReplacementMDLocation(MLD
);
662 // Otherwise, if we see these, just drop them now. Not strictly necessary,
663 // but this speeds things up a little.
667 return getReplacementMDNode(N
);
669 Replacements
[N
] = doRemap(N
);
672 /// Do the remapping traversal.
673 void traverse(MDNode
*);
676 } // end anonymous namespace
678 void DebugTypeInfoRemoval::traverse(MDNode
*N
) {
679 if (!N
|| Replacements
.count(N
))
682 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
683 // parts of the graph.
684 auto prune
= [](MDNode
*Parent
, MDNode
*Child
) {
685 if (auto *MDS
= dyn_cast
<DISubprogram
>(Parent
))
686 return Child
== MDS
->getRetainedNodes().get();
690 SmallVector
<MDNode
*, 16> ToVisit
;
691 DenseSet
<MDNode
*> Opened
;
693 // Visit each node starting at N in post order, and map them.
694 ToVisit
.push_back(N
);
695 while (!ToVisit
.empty()) {
696 auto *N
= ToVisit
.back();
697 if (!Opened
.insert(N
).second
) {
703 for (auto &I
: N
->operands())
704 if (auto *MDN
= dyn_cast_or_null
<MDNode
>(I
))
705 if (!Opened
.count(MDN
) && !Replacements
.count(MDN
) && !prune(N
, MDN
) &&
706 !isa
<DICompileUnit
>(MDN
))
707 ToVisit
.push_back(MDN
);
711 bool llvm::stripNonLineTableDebugInfo(Module
&M
) {
712 bool Changed
= false;
714 // First off, delete the debug intrinsics.
715 auto RemoveUses
= [&](StringRef Name
) {
716 if (auto *DbgVal
= M
.getFunction(Name
)) {
717 while (!DbgVal
->use_empty())
718 cast
<Instruction
>(DbgVal
->user_back())->eraseFromParent();
719 DbgVal
->eraseFromParent();
723 RemoveUses("llvm.dbg.addr");
724 RemoveUses("llvm.dbg.declare");
725 RemoveUses("llvm.dbg.label");
726 RemoveUses("llvm.dbg.value");
728 // Delete non-CU debug info named metadata nodes.
729 for (auto NMI
= M
.named_metadata_begin(), NME
= M
.named_metadata_end();
731 NamedMDNode
*NMD
= &*NMI
;
733 // Specifically keep dbg.cu around.
734 if (NMD
->getName() == "llvm.dbg.cu")
738 // Drop all dbg attachments from global variables.
739 for (auto &GV
: M
.globals())
740 GV
.eraseMetadata(LLVMContext::MD_dbg
);
742 DebugTypeInfoRemoval
Mapper(M
.getContext());
743 auto remap
= [&](MDNode
*Node
) -> MDNode
* {
746 Mapper
.traverseAndRemap(Node
);
747 auto *NewNode
= Mapper
.mapNode(Node
);
748 Changed
|= Node
!= NewNode
;
753 // Rewrite the DebugLocs to be equivalent to what
754 // -gline-tables-only would have created.
756 if (auto *SP
= F
.getSubprogram()) {
757 Mapper
.traverseAndRemap(SP
);
758 auto *NewSP
= cast
<DISubprogram
>(Mapper
.mapNode(SP
));
759 Changed
|= SP
!= NewSP
;
760 F
.setSubprogram(NewSP
);
764 auto remapDebugLoc
= [&](const DebugLoc
&DL
) -> DebugLoc
{
765 auto *Scope
= DL
.getScope();
766 MDNode
*InlinedAt
= DL
.getInlinedAt();
767 Scope
= remap(Scope
);
768 InlinedAt
= remap(InlinedAt
);
769 return DILocation::get(M
.getContext(), DL
.getLine(), DL
.getCol(),
773 if (I
.getDebugLoc() != DebugLoc())
774 I
.setDebugLoc(remapDebugLoc(I
.getDebugLoc()));
776 // Remap DILocations in llvm.loop attachments.
777 updateLoopMetadataDebugLocations(I
, [&](Metadata
*MD
) -> Metadata
* {
778 if (auto *Loc
= dyn_cast_or_null
<DILocation
>(MD
))
779 return remapDebugLoc(Loc
).get();
783 // Strip heapallocsite attachments, they point into the DIType system.
784 if (I
.hasMetadataOtherThanDebugLoc())
785 I
.setMetadata("heapallocsite", nullptr);
790 // Create a new llvm.dbg.cu, which is equivalent to the one
791 // -gline-tables-only would have created.
792 for (auto &NMD
: M
.getNamedMDList()) {
793 SmallVector
<MDNode
*, 8> Ops
;
794 for (MDNode
*Op
: NMD
.operands())
795 Ops
.push_back(remap(Op
));
808 unsigned llvm::getDebugMetadataVersionFromModule(const Module
&M
) {
809 if (auto *Val
= mdconst::dyn_extract_or_null
<ConstantInt
>(
810 M
.getModuleFlag("Debug Info Version")))
811 return Val
->getZExtValue();
815 void Instruction::applyMergedLocation(const DILocation
*LocA
,
816 const DILocation
*LocB
) {
817 setDebugLoc(DILocation::getMergedLocation(LocA
, LocB
));
820 void Instruction::updateLocationAfterHoist() { dropLocation(); }
822 void Instruction::dropLocation() {
823 const DebugLoc
&DL
= getDebugLoc();
827 // If this isn't a call, drop the location to allow a location from a
828 // preceding instruction to propagate.
829 if (!isa
<CallBase
>(this)) {
830 setDebugLoc(DebugLoc());
834 // Set a line 0 location for calls to preserve scope information in case
836 DISubprogram
*SP
= getFunction()->getSubprogram();
838 // If a function scope is available, set it on the line 0 location. When
839 // hoisting a call to a predecessor block, using the function scope avoids
840 // making it look like the callee was reached earlier than it should be.
841 setDebugLoc(DILocation::get(getContext(), 0, 0, SP
));
843 // The parent function has no scope. Go ahead and drop the location. If
844 // the parent function is inlined, and the callee has a subprogram, the
845 // inliner will attach a location to the call.
847 // One alternative is to set a line 0 location with the existing scope and
848 // inlinedAt info. The location might be sensitive to when inlining occurs.
849 setDebugLoc(DebugLoc());
852 //===----------------------------------------------------------------------===//
853 // LLVM C API implementations.
854 //===----------------------------------------------------------------------===//
856 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang
) {
858 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
859 case LLVMDWARFSourceLanguage##NAME: \
861 #include "llvm/BinaryFormat/Dwarf.def"
862 #undef HANDLE_DW_LANG
864 llvm_unreachable("Unhandled Tag");
867 template <typename DIT
> DIT
*unwrapDI(LLVMMetadataRef Ref
) {
868 return (DIT
*)(Ref
? unwrap
<MDNode
>(Ref
) : nullptr);
871 static DINode::DIFlags
map_from_llvmDIFlags(LLVMDIFlags Flags
) {
872 return static_cast<DINode::DIFlags
>(Flags
);
875 static LLVMDIFlags
map_to_llvmDIFlags(DINode::DIFlags Flags
) {
876 return static_cast<LLVMDIFlags
>(Flags
);
879 static DISubprogram::DISPFlags
880 pack_into_DISPFlags(bool IsLocalToUnit
, bool IsDefinition
, bool IsOptimized
) {
881 return DISubprogram::toSPFlags(IsLocalToUnit
, IsDefinition
, IsOptimized
);
884 unsigned LLVMDebugMetadataVersion() {
885 return DEBUG_METADATA_VERSION
;
888 LLVMDIBuilderRef
LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M
) {
889 return wrap(new DIBuilder(*unwrap(M
), false));
892 LLVMDIBuilderRef
LLVMCreateDIBuilder(LLVMModuleRef M
) {
893 return wrap(new DIBuilder(*unwrap(M
)));
896 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M
) {
897 return getDebugMetadataVersionFromModule(*unwrap(M
));
900 LLVMBool
LLVMStripModuleDebugInfo(LLVMModuleRef M
) {
901 return StripDebugInfo(*unwrap(M
));
904 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder
) {
905 delete unwrap(Builder
);
908 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder
) {
909 unwrap(Builder
)->finalize();
912 LLVMMetadataRef
LLVMDIBuilderCreateCompileUnit(
913 LLVMDIBuilderRef Builder
, LLVMDWARFSourceLanguage Lang
,
914 LLVMMetadataRef FileRef
, const char *Producer
, size_t ProducerLen
,
915 LLVMBool isOptimized
, const char *Flags
, size_t FlagsLen
,
916 unsigned RuntimeVer
, const char *SplitName
, size_t SplitNameLen
,
917 LLVMDWARFEmissionKind Kind
, unsigned DWOId
, LLVMBool SplitDebugInlining
,
918 LLVMBool DebugInfoForProfiling
, const char *SysRoot
, size_t SysRootLen
,
919 const char *SDK
, size_t SDKLen
) {
920 auto File
= unwrapDI
<DIFile
>(FileRef
);
922 return wrap(unwrap(Builder
)->createCompileUnit(
923 map_from_llvmDWARFsourcelanguage(Lang
), File
,
924 StringRef(Producer
, ProducerLen
), isOptimized
, StringRef(Flags
, FlagsLen
),
925 RuntimeVer
, StringRef(SplitName
, SplitNameLen
),
926 static_cast<DICompileUnit::DebugEmissionKind
>(Kind
), DWOId
,
927 SplitDebugInlining
, DebugInfoForProfiling
,
928 DICompileUnit::DebugNameTableKind::Default
, false,
929 StringRef(SysRoot
, SysRootLen
), StringRef(SDK
, SDKLen
)));
933 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder
, const char *Filename
,
934 size_t FilenameLen
, const char *Directory
,
935 size_t DirectoryLen
) {
936 return wrap(unwrap(Builder
)->createFile(StringRef(Filename
, FilenameLen
),
937 StringRef(Directory
, DirectoryLen
)));
941 LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder
, LLVMMetadataRef ParentScope
,
942 const char *Name
, size_t NameLen
,
943 const char *ConfigMacros
, size_t ConfigMacrosLen
,
944 const char *IncludePath
, size_t IncludePathLen
,
945 const char *APINotesFile
, size_t APINotesFileLen
) {
946 return wrap(unwrap(Builder
)->createModule(
947 unwrapDI
<DIScope
>(ParentScope
), StringRef(Name
, NameLen
),
948 StringRef(ConfigMacros
, ConfigMacrosLen
),
949 StringRef(IncludePath
, IncludePathLen
),
950 StringRef(APINotesFile
, APINotesFileLen
)));
953 LLVMMetadataRef
LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder
,
954 LLVMMetadataRef ParentScope
,
955 const char *Name
, size_t NameLen
,
956 LLVMBool ExportSymbols
) {
957 return wrap(unwrap(Builder
)->createNameSpace(
958 unwrapDI
<DIScope
>(ParentScope
), StringRef(Name
, NameLen
), ExportSymbols
));
961 LLVMMetadataRef
LLVMDIBuilderCreateFunction(
962 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
963 size_t NameLen
, const char *LinkageName
, size_t LinkageNameLen
,
964 LLVMMetadataRef File
, unsigned LineNo
, LLVMMetadataRef Ty
,
965 LLVMBool IsLocalToUnit
, LLVMBool IsDefinition
,
966 unsigned ScopeLine
, LLVMDIFlags Flags
, LLVMBool IsOptimized
) {
967 return wrap(unwrap(Builder
)->createFunction(
968 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
}, {LinkageName
, LinkageNameLen
},
969 unwrapDI
<DIFile
>(File
), LineNo
, unwrapDI
<DISubroutineType
>(Ty
), ScopeLine
,
970 map_from_llvmDIFlags(Flags
),
971 pack_into_DISPFlags(IsLocalToUnit
, IsDefinition
, IsOptimized
), nullptr,
976 LLVMMetadataRef
LLVMDIBuilderCreateLexicalBlock(
977 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
,
978 LLVMMetadataRef File
, unsigned Line
, unsigned Col
) {
979 return wrap(unwrap(Builder
)->createLexicalBlock(unwrapDI
<DIScope
>(Scope
),
980 unwrapDI
<DIFile
>(File
),
985 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder
,
986 LLVMMetadataRef Scope
,
987 LLVMMetadataRef File
,
988 unsigned Discriminator
) {
989 return wrap(unwrap(Builder
)->createLexicalBlockFile(unwrapDI
<DIScope
>(Scope
),
990 unwrapDI
<DIFile
>(File
),
995 LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder
,
996 LLVMMetadataRef Scope
,
998 LLVMMetadataRef File
,
1000 return wrap(unwrap(Builder
)->createImportedModule(unwrapDI
<DIScope
>(Scope
),
1001 unwrapDI
<DINamespace
>(NS
),
1002 unwrapDI
<DIFile
>(File
),
1007 LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder
,
1008 LLVMMetadataRef Scope
,
1009 LLVMMetadataRef ImportedEntity
,
1010 LLVMMetadataRef File
,
1012 return wrap(unwrap(Builder
)->createImportedModule(
1013 unwrapDI
<DIScope
>(Scope
),
1014 unwrapDI
<DIImportedEntity
>(ImportedEntity
),
1015 unwrapDI
<DIFile
>(File
), Line
));
1019 LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder
,
1020 LLVMMetadataRef Scope
,
1022 LLVMMetadataRef File
,
1024 return wrap(unwrap(Builder
)->createImportedModule(unwrapDI
<DIScope
>(Scope
),
1025 unwrapDI
<DIModule
>(M
),
1026 unwrapDI
<DIFile
>(File
),
1031 LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder
,
1032 LLVMMetadataRef Scope
,
1033 LLVMMetadataRef Decl
,
1034 LLVMMetadataRef File
,
1036 const char *Name
, size_t NameLen
) {
1037 return wrap(unwrap(Builder
)->createImportedDeclaration(
1038 unwrapDI
<DIScope
>(Scope
),
1039 unwrapDI
<DINode
>(Decl
),
1040 unwrapDI
<DIFile
>(File
), Line
, {Name
, NameLen
}));
1044 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx
, unsigned Line
,
1045 unsigned Column
, LLVMMetadataRef Scope
,
1046 LLVMMetadataRef InlinedAt
) {
1047 return wrap(DILocation::get(*unwrap(Ctx
), Line
, Column
, unwrap(Scope
),
1048 unwrap(InlinedAt
)));
1051 unsigned LLVMDILocationGetLine(LLVMMetadataRef Location
) {
1052 return unwrapDI
<DILocation
>(Location
)->getLine();
1055 unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location
) {
1056 return unwrapDI
<DILocation
>(Location
)->getColumn();
1059 LLVMMetadataRef
LLVMDILocationGetScope(LLVMMetadataRef Location
) {
1060 return wrap(unwrapDI
<DILocation
>(Location
)->getScope());
1063 LLVMMetadataRef
LLVMDILocationGetInlinedAt(LLVMMetadataRef Location
) {
1064 return wrap(unwrapDI
<DILocation
>(Location
)->getInlinedAt());
1067 LLVMMetadataRef
LLVMDIScopeGetFile(LLVMMetadataRef Scope
) {
1068 return wrap(unwrapDI
<DIScope
>(Scope
)->getFile());
1071 const char *LLVMDIFileGetDirectory(LLVMMetadataRef File
, unsigned *Len
) {
1072 auto Dir
= unwrapDI
<DIFile
>(File
)->getDirectory();
1077 const char *LLVMDIFileGetFilename(LLVMMetadataRef File
, unsigned *Len
) {
1078 auto Name
= unwrapDI
<DIFile
>(File
)->getFilename();
1083 const char *LLVMDIFileGetSource(LLVMMetadataRef File
, unsigned *Len
) {
1084 if (auto Src
= unwrapDI
<DIFile
>(File
)->getSource()) {
1092 LLVMMetadataRef
LLVMDIBuilderCreateMacro(LLVMDIBuilderRef Builder
,
1093 LLVMMetadataRef ParentMacroFile
,
1095 LLVMDWARFMacinfoRecordType RecordType
,
1096 const char *Name
, size_t NameLen
,
1097 const char *Value
, size_t ValueLen
) {
1099 unwrap(Builder
)->createMacro(unwrapDI
<DIMacroFile
>(ParentMacroFile
), Line
,
1100 static_cast<MacinfoRecordType
>(RecordType
),
1101 {Name
, NameLen
}, {Value
, ValueLen
}));
1105 LLVMDIBuilderCreateTempMacroFile(LLVMDIBuilderRef Builder
,
1106 LLVMMetadataRef ParentMacroFile
, unsigned Line
,
1107 LLVMMetadataRef File
) {
1108 return wrap(unwrap(Builder
)->createTempMacroFile(
1109 unwrapDI
<DIMacroFile
>(ParentMacroFile
), Line
, unwrapDI
<DIFile
>(File
)));
1112 LLVMMetadataRef
LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder
,
1113 const char *Name
, size_t NameLen
,
1115 LLVMBool IsUnsigned
) {
1116 return wrap(unwrap(Builder
)->createEnumerator({Name
, NameLen
}, Value
,
1120 LLVMMetadataRef
LLVMDIBuilderCreateEnumerationType(
1121 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1122 size_t NameLen
, LLVMMetadataRef File
, unsigned LineNumber
,
1123 uint64_t SizeInBits
, uint32_t AlignInBits
, LLVMMetadataRef
*Elements
,
1124 unsigned NumElements
, LLVMMetadataRef ClassTy
) {
1125 auto Elts
= unwrap(Builder
)->getOrCreateArray({unwrap(Elements
),
1127 return wrap(unwrap(Builder
)->createEnumerationType(
1128 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
}, unwrapDI
<DIFile
>(File
),
1129 LineNumber
, SizeInBits
, AlignInBits
, Elts
, unwrapDI
<DIType
>(ClassTy
)));
1132 LLVMMetadataRef
LLVMDIBuilderCreateUnionType(
1133 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1134 size_t NameLen
, LLVMMetadataRef File
, unsigned LineNumber
,
1135 uint64_t SizeInBits
, uint32_t AlignInBits
, LLVMDIFlags Flags
,
1136 LLVMMetadataRef
*Elements
, unsigned NumElements
, unsigned RunTimeLang
,
1137 const char *UniqueId
, size_t UniqueIdLen
) {
1138 auto Elts
= unwrap(Builder
)->getOrCreateArray({unwrap(Elements
),
1140 return wrap(unwrap(Builder
)->createUnionType(
1141 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
}, unwrapDI
<DIFile
>(File
),
1142 LineNumber
, SizeInBits
, AlignInBits
, map_from_llvmDIFlags(Flags
),
1143 Elts
, RunTimeLang
, {UniqueId
, UniqueIdLen
}));
1148 LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder
, uint64_t Size
,
1149 uint32_t AlignInBits
, LLVMMetadataRef Ty
,
1150 LLVMMetadataRef
*Subscripts
,
1151 unsigned NumSubscripts
) {
1152 auto Subs
= unwrap(Builder
)->getOrCreateArray({unwrap(Subscripts
),
1154 return wrap(unwrap(Builder
)->createArrayType(Size
, AlignInBits
,
1155 unwrapDI
<DIType
>(Ty
), Subs
));
1159 LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder
, uint64_t Size
,
1160 uint32_t AlignInBits
, LLVMMetadataRef Ty
,
1161 LLVMMetadataRef
*Subscripts
,
1162 unsigned NumSubscripts
) {
1163 auto Subs
= unwrap(Builder
)->getOrCreateArray({unwrap(Subscripts
),
1165 return wrap(unwrap(Builder
)->createVectorType(Size
, AlignInBits
,
1166 unwrapDI
<DIType
>(Ty
), Subs
));
1170 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder
, const char *Name
,
1171 size_t NameLen
, uint64_t SizeInBits
,
1172 LLVMDWARFTypeEncoding Encoding
,
1173 LLVMDIFlags Flags
) {
1174 return wrap(unwrap(Builder
)->createBasicType({Name
, NameLen
},
1175 SizeInBits
, Encoding
,
1176 map_from_llvmDIFlags(Flags
)));
1179 LLVMMetadataRef
LLVMDIBuilderCreatePointerType(
1180 LLVMDIBuilderRef Builder
, LLVMMetadataRef PointeeTy
,
1181 uint64_t SizeInBits
, uint32_t AlignInBits
, unsigned AddressSpace
,
1182 const char *Name
, size_t NameLen
) {
1183 return wrap(unwrap(Builder
)->createPointerType(unwrapDI
<DIType
>(PointeeTy
),
1184 SizeInBits
, AlignInBits
,
1185 AddressSpace
, {Name
, NameLen
}));
1188 LLVMMetadataRef
LLVMDIBuilderCreateStructType(
1189 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1190 size_t NameLen
, LLVMMetadataRef File
, unsigned LineNumber
,
1191 uint64_t SizeInBits
, uint32_t AlignInBits
, LLVMDIFlags Flags
,
1192 LLVMMetadataRef DerivedFrom
, LLVMMetadataRef
*Elements
,
1193 unsigned NumElements
, unsigned RunTimeLang
, LLVMMetadataRef VTableHolder
,
1194 const char *UniqueId
, size_t UniqueIdLen
) {
1195 auto Elts
= unwrap(Builder
)->getOrCreateArray({unwrap(Elements
),
1197 return wrap(unwrap(Builder
)->createStructType(
1198 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
}, unwrapDI
<DIFile
>(File
),
1199 LineNumber
, SizeInBits
, AlignInBits
, map_from_llvmDIFlags(Flags
),
1200 unwrapDI
<DIType
>(DerivedFrom
), Elts
, RunTimeLang
,
1201 unwrapDI
<DIType
>(VTableHolder
), {UniqueId
, UniqueIdLen
}));
1204 LLVMMetadataRef
LLVMDIBuilderCreateMemberType(
1205 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1206 size_t NameLen
, LLVMMetadataRef File
, unsigned LineNo
, uint64_t SizeInBits
,
1207 uint32_t AlignInBits
, uint64_t OffsetInBits
, LLVMDIFlags Flags
,
1208 LLVMMetadataRef Ty
) {
1209 return wrap(unwrap(Builder
)->createMemberType(unwrapDI
<DIScope
>(Scope
),
1210 {Name
, NameLen
}, unwrapDI
<DIFile
>(File
), LineNo
, SizeInBits
, AlignInBits
,
1211 OffsetInBits
, map_from_llvmDIFlags(Flags
), unwrapDI
<DIType
>(Ty
)));
1215 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder
, const char *Name
,
1217 return wrap(unwrap(Builder
)->createUnspecifiedType({Name
, NameLen
}));
1221 LLVMDIBuilderCreateStaticMemberType(
1222 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1223 size_t NameLen
, LLVMMetadataRef File
, unsigned LineNumber
,
1224 LLVMMetadataRef Type
, LLVMDIFlags Flags
, LLVMValueRef ConstantVal
,
1225 uint32_t AlignInBits
) {
1226 return wrap(unwrap(Builder
)->createStaticMemberType(
1227 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
},
1228 unwrapDI
<DIFile
>(File
), LineNumber
, unwrapDI
<DIType
>(Type
),
1229 map_from_llvmDIFlags(Flags
), unwrap
<Constant
>(ConstantVal
),
1234 LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder
,
1235 const char *Name
, size_t NameLen
,
1236 LLVMMetadataRef File
, unsigned LineNo
,
1237 uint64_t SizeInBits
, uint32_t AlignInBits
,
1238 uint64_t OffsetInBits
, LLVMDIFlags Flags
,
1239 LLVMMetadataRef Ty
, LLVMMetadataRef PropertyNode
) {
1240 return wrap(unwrap(Builder
)->createObjCIVar(
1241 {Name
, NameLen
}, unwrapDI
<DIFile
>(File
), LineNo
,
1242 SizeInBits
, AlignInBits
, OffsetInBits
,
1243 map_from_llvmDIFlags(Flags
), unwrapDI
<DIType
>(Ty
),
1244 unwrapDI
<MDNode
>(PropertyNode
)));
1248 LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder
,
1249 const char *Name
, size_t NameLen
,
1250 LLVMMetadataRef File
, unsigned LineNo
,
1251 const char *GetterName
, size_t GetterNameLen
,
1252 const char *SetterName
, size_t SetterNameLen
,
1253 unsigned PropertyAttributes
,
1254 LLVMMetadataRef Ty
) {
1255 return wrap(unwrap(Builder
)->createObjCProperty(
1256 {Name
, NameLen
}, unwrapDI
<DIFile
>(File
), LineNo
,
1257 {GetterName
, GetterNameLen
}, {SetterName
, SetterNameLen
},
1258 PropertyAttributes
, unwrapDI
<DIType
>(Ty
)));
1262 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder
,
1263 LLVMMetadataRef Type
) {
1264 return wrap(unwrap(Builder
)->createObjectPointerType(unwrapDI
<DIType
>(Type
)));
1268 LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder
, LLVMMetadataRef Type
,
1269 const char *Name
, size_t NameLen
,
1270 LLVMMetadataRef File
, unsigned LineNo
,
1271 LLVMMetadataRef Scope
, uint32_t AlignInBits
) {
1272 return wrap(unwrap(Builder
)->createTypedef(
1273 unwrapDI
<DIType
>(Type
), {Name
, NameLen
}, unwrapDI
<DIFile
>(File
), LineNo
,
1274 unwrapDI
<DIScope
>(Scope
), AlignInBits
));
1278 LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder
,
1279 LLVMMetadataRef Ty
, LLVMMetadataRef BaseTy
,
1280 uint64_t BaseOffset
, uint32_t VBPtrOffset
,
1281 LLVMDIFlags Flags
) {
1282 return wrap(unwrap(Builder
)->createInheritance(
1283 unwrapDI
<DIType
>(Ty
), unwrapDI
<DIType
>(BaseTy
),
1284 BaseOffset
, VBPtrOffset
, map_from_llvmDIFlags(Flags
)));
1288 LLVMDIBuilderCreateForwardDecl(
1289 LLVMDIBuilderRef Builder
, unsigned Tag
, const char *Name
,
1290 size_t NameLen
, LLVMMetadataRef Scope
, LLVMMetadataRef File
, unsigned Line
,
1291 unsigned RuntimeLang
, uint64_t SizeInBits
, uint32_t AlignInBits
,
1292 const char *UniqueIdentifier
, size_t UniqueIdentifierLen
) {
1293 return wrap(unwrap(Builder
)->createForwardDecl(
1294 Tag
, {Name
, NameLen
}, unwrapDI
<DIScope
>(Scope
),
1295 unwrapDI
<DIFile
>(File
), Line
, RuntimeLang
, SizeInBits
,
1296 AlignInBits
, {UniqueIdentifier
, UniqueIdentifierLen
}));
1300 LLVMDIBuilderCreateReplaceableCompositeType(
1301 LLVMDIBuilderRef Builder
, unsigned Tag
, const char *Name
,
1302 size_t NameLen
, LLVMMetadataRef Scope
, LLVMMetadataRef File
, unsigned Line
,
1303 unsigned RuntimeLang
, uint64_t SizeInBits
, uint32_t AlignInBits
,
1304 LLVMDIFlags Flags
, const char *UniqueIdentifier
,
1305 size_t UniqueIdentifierLen
) {
1306 return wrap(unwrap(Builder
)->createReplaceableCompositeType(
1307 Tag
, {Name
, NameLen
}, unwrapDI
<DIScope
>(Scope
),
1308 unwrapDI
<DIFile
>(File
), Line
, RuntimeLang
, SizeInBits
,
1309 AlignInBits
, map_from_llvmDIFlags(Flags
),
1310 {UniqueIdentifier
, UniqueIdentifierLen
}));
1314 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder
, unsigned Tag
,
1315 LLVMMetadataRef Type
) {
1316 return wrap(unwrap(Builder
)->createQualifiedType(Tag
,
1317 unwrapDI
<DIType
>(Type
)));
1321 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder
, unsigned Tag
,
1322 LLVMMetadataRef Type
) {
1323 return wrap(unwrap(Builder
)->createReferenceType(Tag
,
1324 unwrapDI
<DIType
>(Type
)));
1328 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder
) {
1329 return wrap(unwrap(Builder
)->createNullPtrType());
1333 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder
,
1334 LLVMMetadataRef PointeeType
,
1335 LLVMMetadataRef ClassType
,
1336 uint64_t SizeInBits
,
1337 uint32_t AlignInBits
,
1338 LLVMDIFlags Flags
) {
1339 return wrap(unwrap(Builder
)->createMemberPointerType(
1340 unwrapDI
<DIType
>(PointeeType
),
1341 unwrapDI
<DIType
>(ClassType
), AlignInBits
, SizeInBits
,
1342 map_from_llvmDIFlags(Flags
)));
1346 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder
,
1347 LLVMMetadataRef Scope
,
1348 const char *Name
, size_t NameLen
,
1349 LLVMMetadataRef File
, unsigned LineNumber
,
1350 uint64_t SizeInBits
,
1351 uint64_t OffsetInBits
,
1352 uint64_t StorageOffsetInBits
,
1353 LLVMDIFlags Flags
, LLVMMetadataRef Type
) {
1354 return wrap(unwrap(Builder
)->createBitFieldMemberType(
1355 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
},
1356 unwrapDI
<DIFile
>(File
), LineNumber
,
1357 SizeInBits
, OffsetInBits
, StorageOffsetInBits
,
1358 map_from_llvmDIFlags(Flags
), unwrapDI
<DIType
>(Type
)));
1361 LLVMMetadataRef
LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder
,
1362 LLVMMetadataRef Scope
, const char *Name
, size_t NameLen
,
1363 LLVMMetadataRef File
, unsigned LineNumber
, uint64_t SizeInBits
,
1364 uint32_t AlignInBits
, uint64_t OffsetInBits
, LLVMDIFlags Flags
,
1365 LLVMMetadataRef DerivedFrom
,
1366 LLVMMetadataRef
*Elements
, unsigned NumElements
,
1367 LLVMMetadataRef VTableHolder
, LLVMMetadataRef TemplateParamsNode
,
1368 const char *UniqueIdentifier
, size_t UniqueIdentifierLen
) {
1369 auto Elts
= unwrap(Builder
)->getOrCreateArray({unwrap(Elements
),
1371 return wrap(unwrap(Builder
)->createClassType(
1372 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
},
1373 unwrapDI
<DIFile
>(File
), LineNumber
,
1374 SizeInBits
, AlignInBits
, OffsetInBits
,
1375 map_from_llvmDIFlags(Flags
), unwrapDI
<DIType
>(DerivedFrom
),
1376 Elts
, unwrapDI
<DIType
>(VTableHolder
),
1377 unwrapDI
<MDNode
>(TemplateParamsNode
),
1378 {UniqueIdentifier
, UniqueIdentifierLen
}));
1382 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder
,
1383 LLVMMetadataRef Type
) {
1384 return wrap(unwrap(Builder
)->createArtificialType(unwrapDI
<DIType
>(Type
)));
1387 const char *LLVMDITypeGetName(LLVMMetadataRef DType
, size_t *Length
) {
1388 StringRef Str
= unwrap
<DIType
>(DType
)->getName();
1389 *Length
= Str
.size();
1393 uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType
) {
1394 return unwrapDI
<DIType
>(DType
)->getSizeInBits();
1397 uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType
) {
1398 return unwrapDI
<DIType
>(DType
)->getOffsetInBits();
1401 uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType
) {
1402 return unwrapDI
<DIType
>(DType
)->getAlignInBits();
1405 unsigned LLVMDITypeGetLine(LLVMMetadataRef DType
) {
1406 return unwrapDI
<DIType
>(DType
)->getLine();
1409 LLVMDIFlags
LLVMDITypeGetFlags(LLVMMetadataRef DType
) {
1410 return map_to_llvmDIFlags(unwrapDI
<DIType
>(DType
)->getFlags());
1413 LLVMMetadataRef
LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder
,
1414 LLVMMetadataRef
*Types
,
1417 unwrap(Builder
)->getOrCreateTypeArray({unwrap(Types
), Length
}).get());
1421 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder
,
1422 LLVMMetadataRef File
,
1423 LLVMMetadataRef
*ParameterTypes
,
1424 unsigned NumParameterTypes
,
1425 LLVMDIFlags Flags
) {
1426 auto Elts
= unwrap(Builder
)->getOrCreateTypeArray({unwrap(ParameterTypes
),
1427 NumParameterTypes
});
1428 return wrap(unwrap(Builder
)->createSubroutineType(
1429 Elts
, map_from_llvmDIFlags(Flags
)));
1432 LLVMMetadataRef
LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder
,
1433 int64_t *Addr
, size_t Length
) {
1434 return wrap(unwrap(Builder
)->createExpression(ArrayRef
<int64_t>(Addr
,
1439 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder
,
1441 return wrap(unwrap(Builder
)->createConstantValueExpression(Value
));
1444 LLVMMetadataRef
LLVMDIBuilderCreateGlobalVariableExpression(
1445 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1446 size_t NameLen
, const char *Linkage
, size_t LinkLen
, LLVMMetadataRef File
,
1447 unsigned LineNo
, LLVMMetadataRef Ty
, LLVMBool LocalToUnit
,
1448 LLVMMetadataRef Expr
, LLVMMetadataRef Decl
, uint32_t AlignInBits
) {
1449 return wrap(unwrap(Builder
)->createGlobalVariableExpression(
1450 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
}, {Linkage
, LinkLen
},
1451 unwrapDI
<DIFile
>(File
), LineNo
, unwrapDI
<DIType
>(Ty
), LocalToUnit
,
1452 true, unwrap
<DIExpression
>(Expr
), unwrapDI
<MDNode
>(Decl
),
1453 nullptr, AlignInBits
));
1456 LLVMMetadataRef
LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE
) {
1457 return wrap(unwrapDI
<DIGlobalVariableExpression
>(GVE
)->getVariable());
1460 LLVMMetadataRef
LLVMDIGlobalVariableExpressionGetExpression(
1461 LLVMMetadataRef GVE
) {
1462 return wrap(unwrapDI
<DIGlobalVariableExpression
>(GVE
)->getExpression());
1465 LLVMMetadataRef
LLVMDIVariableGetFile(LLVMMetadataRef Var
) {
1466 return wrap(unwrapDI
<DIVariable
>(Var
)->getFile());
1469 LLVMMetadataRef
LLVMDIVariableGetScope(LLVMMetadataRef Var
) {
1470 return wrap(unwrapDI
<DIVariable
>(Var
)->getScope());
1473 unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var
) {
1474 return unwrapDI
<DIVariable
>(Var
)->getLine();
1477 LLVMMetadataRef
LLVMTemporaryMDNode(LLVMContextRef Ctx
, LLVMMetadataRef
*Data
,
1480 MDTuple::getTemporary(*unwrap(Ctx
), {unwrap(Data
), Count
}).release());
1483 void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode
) {
1484 MDNode::deleteTemporary(unwrapDI
<MDNode
>(TempNode
));
1487 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata
,
1488 LLVMMetadataRef Replacement
) {
1489 auto *Node
= unwrapDI
<MDNode
>(TargetMetadata
);
1490 Node
->replaceAllUsesWith(unwrap
<Metadata
>(Replacement
));
1491 MDNode::deleteTemporary(Node
);
1494 LLVMMetadataRef
LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
1495 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1496 size_t NameLen
, const char *Linkage
, size_t LnkLen
, LLVMMetadataRef File
,
1497 unsigned LineNo
, LLVMMetadataRef Ty
, LLVMBool LocalToUnit
,
1498 LLVMMetadataRef Decl
, uint32_t AlignInBits
) {
1499 return wrap(unwrap(Builder
)->createTempGlobalVariableFwdDecl(
1500 unwrapDI
<DIScope
>(Scope
), {Name
, NameLen
}, {Linkage
, LnkLen
},
1501 unwrapDI
<DIFile
>(File
), LineNo
, unwrapDI
<DIType
>(Ty
), LocalToUnit
,
1502 unwrapDI
<MDNode
>(Decl
), nullptr, AlignInBits
));
1506 LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder
, LLVMValueRef Storage
,
1507 LLVMMetadataRef VarInfo
, LLVMMetadataRef Expr
,
1508 LLVMMetadataRef DL
, LLVMValueRef Instr
) {
1509 return wrap(unwrap(Builder
)->insertDeclare(
1510 unwrap(Storage
), unwrap
<DILocalVariable
>(VarInfo
),
1511 unwrap
<DIExpression
>(Expr
), unwrap
<DILocation
>(DL
),
1512 unwrap
<Instruction
>(Instr
)));
1515 LLVMValueRef
LLVMDIBuilderInsertDeclareAtEnd(
1516 LLVMDIBuilderRef Builder
, LLVMValueRef Storage
, LLVMMetadataRef VarInfo
,
1517 LLVMMetadataRef Expr
, LLVMMetadataRef DL
, LLVMBasicBlockRef Block
) {
1518 return wrap(unwrap(Builder
)->insertDeclare(
1519 unwrap(Storage
), unwrap
<DILocalVariable
>(VarInfo
),
1520 unwrap
<DIExpression
>(Expr
), unwrap
<DILocation
>(DL
),
1524 LLVMValueRef
LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder
,
1526 LLVMMetadataRef VarInfo
,
1527 LLVMMetadataRef Expr
,
1528 LLVMMetadataRef DebugLoc
,
1529 LLVMValueRef Instr
) {
1530 return wrap(unwrap(Builder
)->insertDbgValueIntrinsic(
1531 unwrap(Val
), unwrap
<DILocalVariable
>(VarInfo
),
1532 unwrap
<DIExpression
>(Expr
), unwrap
<DILocation
>(DebugLoc
),
1533 unwrap
<Instruction
>(Instr
)));
1536 LLVMValueRef
LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder
,
1538 LLVMMetadataRef VarInfo
,
1539 LLVMMetadataRef Expr
,
1540 LLVMMetadataRef DebugLoc
,
1541 LLVMBasicBlockRef Block
) {
1542 return wrap(unwrap(Builder
)->insertDbgValueIntrinsic(
1543 unwrap(Val
), unwrap
<DILocalVariable
>(VarInfo
),
1544 unwrap
<DIExpression
>(Expr
), unwrap
<DILocation
>(DebugLoc
),
1548 LLVMMetadataRef
LLVMDIBuilderCreateAutoVariable(
1549 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1550 size_t NameLen
, LLVMMetadataRef File
, unsigned LineNo
, LLVMMetadataRef Ty
,
1551 LLVMBool AlwaysPreserve
, LLVMDIFlags Flags
, uint32_t AlignInBits
) {
1552 return wrap(unwrap(Builder
)->createAutoVariable(
1553 unwrap
<DIScope
>(Scope
), {Name
, NameLen
}, unwrap
<DIFile
>(File
),
1554 LineNo
, unwrap
<DIType
>(Ty
), AlwaysPreserve
,
1555 map_from_llvmDIFlags(Flags
), AlignInBits
));
1558 LLVMMetadataRef
LLVMDIBuilderCreateParameterVariable(
1559 LLVMDIBuilderRef Builder
, LLVMMetadataRef Scope
, const char *Name
,
1560 size_t NameLen
, unsigned ArgNo
, LLVMMetadataRef File
, unsigned LineNo
,
1561 LLVMMetadataRef Ty
, LLVMBool AlwaysPreserve
, LLVMDIFlags Flags
) {
1562 return wrap(unwrap(Builder
)->createParameterVariable(
1563 unwrap
<DIScope
>(Scope
), {Name
, NameLen
}, ArgNo
, unwrap
<DIFile
>(File
),
1564 LineNo
, unwrap
<DIType
>(Ty
), AlwaysPreserve
,
1565 map_from_llvmDIFlags(Flags
)));
1568 LLVMMetadataRef
LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder
,
1569 int64_t Lo
, int64_t Count
) {
1570 return wrap(unwrap(Builder
)->getOrCreateSubrange(Lo
, Count
));
1573 LLVMMetadataRef
LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder
,
1574 LLVMMetadataRef
*Data
,
1576 Metadata
**DataValue
= unwrap(Data
);
1577 return wrap(unwrap(Builder
)->getOrCreateArray({DataValue
, Length
}).get());
1580 LLVMMetadataRef
LLVMGetSubprogram(LLVMValueRef Func
) {
1581 return wrap(unwrap
<Function
>(Func
)->getSubprogram());
1584 void LLVMSetSubprogram(LLVMValueRef Func
, LLVMMetadataRef SP
) {
1585 unwrap
<Function
>(Func
)->setSubprogram(unwrap
<DISubprogram
>(SP
));
1588 unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram
) {
1589 return unwrapDI
<DISubprogram
>(Subprogram
)->getLine();
1592 LLVMMetadataRef
LLVMInstructionGetDebugLoc(LLVMValueRef Inst
) {
1593 return wrap(unwrap
<Instruction
>(Inst
)->getDebugLoc().getAsMDNode());
1596 void LLVMInstructionSetDebugLoc(LLVMValueRef Inst
, LLVMMetadataRef Loc
) {
1598 unwrap
<Instruction
>(Inst
)->setDebugLoc(DebugLoc(unwrap
<MDNode
>(Loc
)));
1600 unwrap
<Instruction
>(Inst
)->setDebugLoc(DebugLoc());
1603 LLVMMetadataKind
LLVMGetMetadataKind(LLVMMetadataRef Metadata
) {
1604 switch(unwrap(Metadata
)->getMetadataID()) {
1605 #define HANDLE_METADATA_LEAF(CLASS) \
1606 case Metadata::CLASS##Kind: \
1607 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
1608 #include "llvm/IR/Metadata.def"
1610 return (LLVMMetadataKind
)LLVMGenericDINodeMetadataKind
;