34 using namespace llvm::dwarf;
37 if (
auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
45 auto Inst = std::find_if(BB.begin(), BB.end(), [](
const Instruction &Inst) {
46 return Inst.getDebugLoc();
53 return Subprogram->describes(F) ? Subprogram :
nullptr;
60 if (
auto *
C = dyn_cast_or_null<DICompositeTypeBase>(T))
63 if (
auto *D = dyn_cast_or_null<DIDerivedTypeBase>(T)) {
77 for (
unsigned CUi = 0, CUe = CU_Nodes->
getNumOperands(); CUi != CUe; ++CUi) {
78 auto *CU = cast<DICompileUnit>(CU_Nodes->
getOperand(CUi));
79 DINodeArray Retain = CU->getRetainedTypes();
80 for (
unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
81 if (!isa<DICompositeType>(Retain[Ti]))
83 auto *Ty = cast<DICompositeType>(Retain[Ti]);
84 if (
MDString *TypeId = Ty->getRawIdentifier()) {
87 std::pair<DITypeIdentifierMap::iterator, bool>
P =
88 Map.
insert(std::make_pair(TypeId, Ty));
91 if (!P.second && !Ty->isForwardDecl())
110 TypeIdentifierMap.clear();
111 TypeMapInitialized =
false;
114 void DebugInfoFinder::InitializeTypeMap(
const Module &M) {
115 if (!TypeMapInitialized)
118 TypeMapInitialized =
true;
123 InitializeTypeMap(M);
125 for (
unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
126 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
128 for (
auto *DIG : CU->getGlobalVariables()) {
129 if (addGlobalVariable(DIG)) {
130 processScope(DIG->getScope());
131 processType(DIG->getType().resolve(TypeIdentifierMap));
134 for (
auto *
SP : CU->getSubprograms())
135 processSubprogram(
SP);
136 for (
auto *ET : CU->getEnumTypes())
138 for (
auto *RT : CU->getRetainedTypes())
140 for (
auto *Import : CU->getImportedEntities()) {
141 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
142 if (
auto *
T = dyn_cast<DIType>(Entity))
144 else if (
auto *
SP = dyn_cast<DISubprogram>(Entity))
145 processSubprogram(
SP);
146 else if (
auto *NS = dyn_cast<DINamespace>(Entity))
147 processScope(NS->getScope());
148 else if (
auto *M = dyn_cast<DIModule>(Entity))
149 processScope(M->getScope());
158 InitializeTypeMap(M);
159 processScope(Loc->getScope());
160 processLocation(M, Loc->getInlinedAt());
163 void DebugInfoFinder::processType(
DIType *DT) {
167 if (
auto *DCT = dyn_cast<DICompositeTypeBase>(DT)) {
168 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
169 if (
auto *
ST = dyn_cast<DISubroutineType>(DCT)) {
171 processType(Ref.resolve(TypeIdentifierMap));
174 for (
Metadata *D : DCT->getElements()) {
175 if (
auto *
T = dyn_cast<DIType>(D))
177 else if (
auto *SP = dyn_cast<DISubprogram>(D))
178 processSubprogram(SP);
180 }
else if (
auto *DDT = dyn_cast<DIDerivedTypeBase>(DT)) {
181 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
185 void DebugInfoFinder::processScope(
DIScope *Scope) {
188 if (
auto *Ty = dyn_cast<DIType>(Scope)) {
192 if (
auto *CU = dyn_cast<DICompileUnit>(Scope)) {
196 if (
auto *SP = dyn_cast<DISubprogram>(Scope)) {
197 processSubprogram(SP);
200 if (!addScope(Scope))
202 if (
auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
203 processScope(LB->getScope());
204 }
else if (
auto *NS = dyn_cast<DINamespace>(Scope)) {
205 processScope(NS->getScope());
206 }
else if (
auto *M = dyn_cast<DIModule>(Scope)) {
207 processScope(M->getScope());
211 void DebugInfoFinder::processSubprogram(
DISubprogram *SP) {
212 if (!addSubprogram(SP))
215 processType(SP->getType());
216 for (
auto *Element : SP->getTemplateParams()) {
217 if (
auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
218 processType(TType->getType().resolve(TypeIdentifierMap));
219 }
else if (
auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
220 processType(TVal->getType().resolve(TypeIdentifierMap));
230 InitializeTypeMap(M);
236 if (!NodesSeen.insert(DV).second)
238 processScope(DV->getScope());
239 processType(DV->getType().resolve(TypeIdentifierMap));
246 InitializeTypeMap(M);
252 if (!NodesSeen.insert(DV).second)
254 processScope(DV->getScope());
255 processType(DV->getType().resolve(TypeIdentifierMap));
258 bool DebugInfoFinder::addType(
DIType *DT) {
262 if (!NodesSeen.insert(DT).second)
265 TYs.push_back(const_cast<DIType *>(DT));
272 if (!NodesSeen.insert(CU).second)
283 if (!NodesSeen.insert(DIG).second)
294 if (!NodesSeen.insert(SP).second)
301 bool DebugInfoFinder::addScope(
DIScope *Scope) {
308 if (!NodesSeen.insert(Scope).second)
310 Scopes.push_back(Scope);
315 bool Changed =
false;
318 if (
I.getDebugLoc()) {
328 bool Changed =
false;
333 while (!Declare->use_empty()) {
334 CallInst *CI = cast<CallInst>(Declare->user_back());
337 Declare->eraseFromParent();
342 while (!DbgVal->use_empty()) {
343 CallInst *CI = cast<CallInst>(DbgVal->user_back());
346 DbgVal->eraseFromParent();
364 Materializer->setStripDebugInfo();
370 if (
auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
372 return Val->getZExtValue();
385 auto *CUNode = cast<DICompileUnit>(
N);
386 for (
auto *SP : CUNode->getSubprograms()) {
388 R.
insert(std::make_pair(
F, SP));
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
StringRef getName() const
void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
A Module instance is used to store all the information related to an LLVM module. ...
unsigned getNumOperands() const
Return number of MDNode operands.
CallInst - This class represents a function call, abstracting a target machine's calling convention...
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.
bool stripDebugInfo(Function &F)
void processModule(const Module &M)
Process entire module and collect debug info anchors.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void eraseFromParent()
Drop all references and remove the node from parent module.
DICompositeTypeBase * getDICompositeType(DIType *T)
Find underlying composite type.
Pointer union between a subclass of DINode and MDString.
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
T * resolve(const MapTy &Map) const
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
DIScopeRef getScope() const
iterator_range< op_iterator > operands()
DIScopeRef getScope() const
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
LLVM Basic Block Representation.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
MDNode * getOperand(unsigned i) const
MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
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.
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Module.h This file contains the declarations for the Module class.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Base class for DICompositeType and DISubroutineType.
DbgValueInst - This represents the llvm.dbg.value instruction.
void processDeclare(const Module &M, const DbgDeclareInst *DDI)
Process DbgDeclareInst.
NamedMDListType::iterator named_metadata_iterator
The named metadata iterators.
DILocalVariable * getVariable() const
DenseMap< const Function *, DISubprogram * > makeSubprogramMap(const Module &M)
DILocalVariable * getVariable() const
unsigned getNumOperands() const
C - The default llvm calling convention, compatible with C.
DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes)
Generate map by visiting all retained types.
DISubprogram * getSubprogram() const
Get the subprogram for this scope.
DbgDeclareInst - This represents the llvm.dbg.declare instruction.
named_metadata_iterator named_metadata_begin()