39 using namespace llvm::dwarf;
42 if (
auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
63 for (
auto DIG : CU->getGlobalVariables()) {
64 if (!addGlobalVariable(DIG))
66 auto *GV = DIG->getVariable();
67 processScope(GV->getScope());
68 processType(GV->getType().resolve());
70 for (
auto *ET : CU->getEnumTypes())
72 for (
auto *RT : CU->getRetainedTypes())
73 if (
auto *
T = dyn_cast<DIType>(RT))
76 processSubprogram(cast<DISubprogram>(RT));
77 for (
auto *
Import : CU->getImportedEntities()) {
78 auto *Entity =
Import->getEntity().resolve();
79 if (
auto *
T = dyn_cast<DIType>(Entity))
81 else if (
auto *SP = dyn_cast<DISubprogram>(Entity))
82 processSubprogram(SP);
83 else if (
auto *NS = dyn_cast<DINamespace>(Entity))
84 processScope(NS->getScope());
85 else if (
auto *M = dyn_cast<DIModule>(Entity))
86 processScope(M->getScope());
90 if (
auto *SP = cast_or_null<DISubprogram>(
F.getSubprogram()))
91 processSubprogram(SP);
98 processLocation(M,
I.getDebugLoc().get());
107 processScope(Loc->getScope());
108 processLocation(M, Loc->getInlinedAt());
111 void DebugInfoFinder::processType(
DIType *DT) {
115 if (
auto *
ST = dyn_cast<DISubroutineType>(DT)) {
117 processType(Ref.resolve());
120 if (
auto *DCT = dyn_cast<DICompositeType>(DT)) {
121 processType(DCT->getBaseType().resolve());
123 if (
auto *
T = dyn_cast<DIType>(
D))
125 else if (
auto *SP = dyn_cast<DISubprogram>(
D))
126 processSubprogram(SP);
130 if (
auto *DDT = dyn_cast<DIDerivedType>(DT)) {
131 processType(DDT->getBaseType().resolve());
135 void DebugInfoFinder::processScope(
DIScope *Scope) {
138 if (
auto *Ty = dyn_cast<DIType>(Scope)) {
142 if (
auto *CU = dyn_cast<DICompileUnit>(Scope)) {
146 if (
auto *SP = dyn_cast<DISubprogram>(Scope)) {
147 processSubprogram(SP);
150 if (!addScope(Scope))
152 if (
auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
153 processScope(LB->getScope());
154 }
else if (
auto *NS = dyn_cast<DINamespace>(Scope)) {
155 processScope(NS->getScope());
156 }
else if (
auto *M = dyn_cast<DIModule>(Scope)) {
157 processScope(M->getScope());
161 void DebugInfoFinder::processSubprogram(
DISubprogram *SP) {
162 if (!addSubprogram(SP))
165 processType(SP->getType());
166 for (
auto *Element : SP->getTemplateParams()) {
167 if (
auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
168 processType(TType->getType().resolve());
169 }
else if (
auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
170 processType(TVal->getType().resolve());
185 if (!NodesSeen.insert(DV).second)
187 processScope(DV->getScope());
188 processType(DV->getType().resolve());
200 if (!NodesSeen.insert(DV).second)
202 processScope(DV->getScope());
203 processType(DV->getType().resolve());
206 bool DebugInfoFinder::addType(
DIType *DT) {
210 if (!NodesSeen.insert(DT).second)
213 TYs.push_back(const_cast<DIType *>(DT));
220 if (!NodesSeen.insert(CU).second)
228 if (!NodesSeen.insert(DIG).second)
239 if (!NodesSeen.insert(SP).second)
246 bool DebugInfoFinder::addScope(
DIScope *Scope) {
253 if (!NodesSeen.insert(Scope).second)
255 Scopes.push_back(Scope);
264 return isa<DILocation>(
Op.get());
271 return !isa<DILocation>(
Op.get());
281 if (!isa<DILocation>(*
Op))
292 bool Changed =
false;
300 for (
auto II = BB.begin(),
End = BB.end(); II !=
End;) {
302 if (isa<DbgInfoIntrinsic>(&I)) {
313 auto *TermInst = BB.getTerminator();
315 auto *NewLoopID = LoopIDsMap.
lookup(LoopID);
318 if (NewLoopID != LoopID)
326 bool Changed =
false;
336 NMD->
getName() ==
"llvm.gcov") {
345 for (
auto &GV : M.globals()) {
355 Materializer->setStripDebugInfo();
363 class DebugTypeInfoRemoval {
368 MDNode *EmptySubroutineType;
392 auto Replacement = Replacements.find(M);
393 if (Replacement != Replacements.end())
394 return Replacement->second;
398 MDNode *mapNode(
Metadata *
N) {
return dyn_cast_or_null<MDNode>(map(N)); }
402 void traverseAndRemap(
MDNode *
N) { traverse(N); }
407 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->
getFile()));
410 auto *
Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
411 DITypeRef ContainingType(map(MDS->getContainingType()));
412 auto *
Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
413 auto Variables =
nullptr;
414 auto TemplateParams =
nullptr;
417 auto distinctMDSubprogram = [&]() {
420 FileAndScope, MDS->getLine(),
Type, MDS->isLocalToUnit(),
421 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
422 MDS->getVirtuality(), MDS->getVirtualIndex(),
423 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(),
Unit,
424 TemplateParams, Declaration, Variables);
428 return distinctMDSubprogram();
432 FileAndScope, MDS->getLine(),
Type, MDS->isLocalToUnit(),
433 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
434 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
435 MDS->getFlags(), MDS->isOptimized(),
Unit, TemplateParams, Declaration,
438 StringRef OldLinkageName = MDS->getLinkageName();
441 auto OrigLinkage = NewToLinkageName.find(NewMDS);
442 if (OrigLinkage != NewToLinkageName.end()) {
443 if (OrigLinkage->second == OldLinkageName)
449 return distinctMDSubprogram();
452 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
462 auto *
File = cast_or_null<DIFile>(map(CU->
getFile()));
464 MDTuple *RetainedTypes =
nullptr;
465 MDTuple *GlobalVariables =
nullptr;
466 MDTuple *ImportedEntities =
nullptr;
471 RetainedTypes, GlobalVariables, ImportedEntities, CU->
getMacros(),
477 auto *Scope = map(MLD->getScope());
478 auto *InlinedAt = map(MLD->getInlinedAt());
481 MLD->getColumn(), Scope, InlinedAt);
499 if (Replacements.count(N))
505 if (
auto *MDSub = dyn_cast<DISubprogram>(N)) {
506 remap(MDSub->getUnit());
507 return getReplacementSubprogram(MDSub);
509 if (isa<DISubroutineType>(N))
510 return EmptySubroutineType;
511 if (
auto *CU = dyn_cast<DICompileUnit>(N))
512 return getReplacementCU(CU);
515 if (
auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
517 return mapNode(MDLB->getScope());
518 if (
auto *MLD = dyn_cast<DILocation>(N))
519 return getReplacementMDLocation(MLD);
526 return getReplacementMDNode(N);
528 Replacements[
N] = doRemap(N);
537 void DebugTypeInfoRemoval::traverse(
MDNode *N) {
538 if (!N || Replacements.count(N))
544 if (
auto *MDS = dyn_cast<DISubprogram>(Parent))
545 return Child == MDS->getVariables().
get();
554 while (!ToVisit.
empty()) {
555 auto *N = ToVisit.
back();
556 if (!Opened.
insert(N).second) {
563 if (
auto *MDN = dyn_cast_or_null<MDNode>(
I))
564 if (!Opened.
count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
565 !isa<DICompileUnit>(MDN))
571 bool Changed =
false;
576 while (!DbgVal->use_empty())
577 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
578 DbgVal->eraseFromParent();
582 RemoveUses(
"llvm.dbg.declare");
583 RemoveUses(
"llvm.dbg.value");
591 if (NMD->
getName() ==
"llvm.dbg.cu")
603 Mapper.traverseAndRemap(Node);
604 auto *NewNode = Mapper.mapNode(Node);
605 Changed |= Node != NewNode;
613 if (
auto *SP =
F.getSubprogram()) {
614 Mapper.traverseAndRemap(SP);
615 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
616 Changed |= SP != NewSP;
617 F.setSubprogram(NewSP);
622 auto *Scope =
DL.getScope();
623 MDNode *InlinedAt =
DL.getInlinedAt();
624 Scope = remap(Scope);
625 InlinedAt = remap(InlinedAt);
630 I.setDebugLoc(remapDebugLoc(
I.getDebugLoc()));
634 I.getAllMetadata(MDs);
635 for (
auto Attachment : MDs)
636 if (
auto *
T = dyn_cast_or_null<MDTuple>(Attachment.second))
637 for (
unsigned N = 0; N <
T->getNumOperands(); ++
N)
638 if (
auto *Loc = dyn_cast_or_null<DILocation>(
T->getOperand(N)))
640 T->replaceOperandWith(N, remapDebugLoc(Loc));
647 for (
auto &NMD : M.getNamedMDList()) {
664 if (
auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
666 return Val->getZExtValue();
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
StringRef getName() const
void push_back(const T &Elt)
void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
Tracking metadata reference owned by Metadata.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
A Module instance is used to store all the information related to an LLVM module. ...
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
bool getDebugInfoForProfiling() const
StringRef getFlags() const
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Implements a dense probed hash-table based set.
unsigned getNumOperands() const
Return number of MDNode operands.
named_metadata_iterator named_metadata_end()
unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
void reset()
Clear all lists.
op_iterator op_begin() const
bool getSplitDebugInlining() const
bool stripDebugInfo(Function &F)
void reserve(size_type N)
op_iterator op_end() const
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Tagged DWARF-like metadata node.
uint64_t getDWOId() const
DILocation * get() const
Get the underlying DILocation.
void eraseFromParent()
Drop all references and remove the node from parent module.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
bool stripNonLineTableDebugInfo(Module &M)
Downgrade the debug info in a module to contain only line table information.
Holds a subclass of DINode.
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
LLVM_NODISCARD bool empty() const
iterator_range< debug_compile_units_iterator > debug_compile_units() const
Return an iterator for all DICompileUnits listed in this Module's llvm.dbg.cu named metadata node and...
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
DIScopeRef getScope() const
DIScopeRef getScope() const
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
unsigned getRuntimeVersion() const
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
iterator_range< iterator > functions()
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
StringRef getName() const
Import information from summary.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(const ValueT &V)
unsigned getSourceLanguage() const
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
A pair of DIGlobalVariable and DIExpression.
static const unsigned End
DIMacroNodeArray getMacros() const
void processValue(const Module &M, const DbgValueInst *DVI)
Process DbgValueInst.
Base class for scope-like contexts.
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Iterator for intrusive lists based on ilist_node.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
DISubprogram * getSubprogram() const
Get the attached subprogram.
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Type array for a subprogram.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
This represents the llvm.dbg.value instruction.
void processDeclare(const Module &M, const DbgDeclareInst *DDI)
Process DbgDeclareInst.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
op_range operands() const
LLVMContext & getContext() const
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
std::vector< uint8_t > Unit
DILocalVariable * getVariable() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static MDNode * stripDebugLocFromLoopID(MDNode *N)
DILocalVariable * getVariable() const
iterator_range< global_iterator > globals()
StringRef - Represent a constant reference to a string, i.e.
DISubprogram * getSubprogram() const
Get the subprogram for this scope.
This represents the llvm.dbg.declare instruction.
StringRef getProducer() const
named_metadata_iterator named_metadata_begin()
LLVMContext & getContext() const
Get the global data context.
StringRef getSplitDebugFilename() const