#include "llvm/IR/DebugInfo.h"
#include "LLVMContextImpl.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/GVMaterializer.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::dwarf;
DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
return LocalScope->getSubprogram();
return nullptr;
}
DISubprogram *llvm::getDISubprogram(const Function *F) {
for (auto &BB : *F) {
auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
return Inst.getDebugLoc();
});
if (Inst == BB.end())
continue;
DebugLoc DLoc = Inst->getDebugLoc();
const MDNode *Scope = DLoc.getInlinedAtScope();
auto *Subprogram = getDISubprogram(Scope);
return Subprogram->describes(F) ? Subprogram : nullptr;
}
return nullptr;
}
DITypeIdentifierMap
llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
DITypeIdentifierMap Map;
for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
DINodeArray Retain = CU->getRetainedTypes();
for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
if (!isa<DICompositeType>(Retain[Ti]))
continue;
auto *Ty = cast<DICompositeType>(Retain[Ti]);
if (MDString *TypeId = Ty->getRawIdentifier()) {
std::pair<DITypeIdentifierMap::iterator, bool> P =
Map.insert(std::make_pair(TypeId, Ty));
if (!P.second && !Ty->isForwardDecl())
P.first->second = Ty;
}
}
}
return Map;
}
void DebugInfoFinder::reset() {
CUs.clear();
SPs.clear();
GVs.clear();
TYs.clear();
Scopes.clear();
NodesSeen.clear();
TypeIdentifierMap.clear();
TypeMapInitialized = false;
}
void DebugInfoFinder::InitializeTypeMap(const Module &M) {
if (!TypeMapInitialized)
if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
TypeMapInitialized = true;
}
}
void DebugInfoFinder::processModule(const Module &M) {
InitializeTypeMap(M);
if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
addCompileUnit(CU);
for (auto *DIG : CU->getGlobalVariables()) {
if (addGlobalVariable(DIG)) {
processScope(DIG->getScope());
processType(DIG->getType().resolve(TypeIdentifierMap));
}
}
for (auto *SP : CU->getSubprograms())
processSubprogram(SP);
for (auto *ET : CU->getEnumTypes())
processType(ET);
for (auto *RT : CU->getRetainedTypes())
processType(RT);
for (auto *Import : CU->getImportedEntities()) {
auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
if (auto *T = dyn_cast<DIType>(Entity))
processType(T);
else if (auto *SP = dyn_cast<DISubprogram>(Entity))
processSubprogram(SP);
else if (auto *NS = dyn_cast<DINamespace>(Entity))
processScope(NS->getScope());
else if (auto *M = dyn_cast<DIModule>(Entity))
processScope(M->getScope());
}
}
}
}
void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
if (!Loc)
return;
InitializeTypeMap(M);
processScope(Loc->getScope());
processLocation(M, Loc->getInlinedAt());
}
void DebugInfoFinder::processType(DIType *DT) {
if (!addType(DT))
return;
processScope(DT->getScope().resolve(TypeIdentifierMap));
if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
for (DITypeRef Ref : ST->getTypeArray())
processType(Ref.resolve(TypeIdentifierMap));
return;
}
if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
processType(DCT->getBaseType().resolve(TypeIdentifierMap));
for (Metadata *D : DCT->getElements()) {
if (auto *T = dyn_cast<DIType>(D))
processType(T);
else if (auto *SP = dyn_cast<DISubprogram>(D))
processSubprogram(SP);
}
return;
}
if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
processType(DDT->getBaseType().resolve(TypeIdentifierMap));
}
}
void DebugInfoFinder::processScope(DIScope *Scope) {
if (!Scope)
return;
if (auto *Ty = dyn_cast<DIType>(Scope)) {
processType(Ty);
return;
}
if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
addCompileUnit(CU);
return;
}
if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
processSubprogram(SP);
return;
}
if (!addScope(Scope))
return;
if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
processScope(LB->getScope());
} else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
processScope(NS->getScope());
} else if (auto *M = dyn_cast<DIModule>(Scope)) {
processScope(M->getScope());
}
}
void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
if (!addSubprogram(SP))
return;
processScope(SP->getScope().resolve(TypeIdentifierMap));
processType(SP->getType());
for (auto *Element : SP->getTemplateParams()) {
if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
processType(TType->getType().resolve(TypeIdentifierMap));
} else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
processType(TVal->getType().resolve(TypeIdentifierMap));
}
}
}
void DebugInfoFinder::processDeclare(const Module &M,
const DbgDeclareInst *DDI) {
auto *N = dyn_cast<MDNode>(DDI->getVariable());
if (!N)
return;
InitializeTypeMap(M);
auto *DV = dyn_cast<DILocalVariable>(N);
if (!DV)
return;
if (!NodesSeen.insert(DV).second)
return;
processScope(DV->getScope());
processType(DV->getType().resolve(TypeIdentifierMap));
}
void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
auto *N = dyn_cast<MDNode>(DVI->getVariable());
if (!N)
return;
InitializeTypeMap(M);
auto *DV = dyn_cast<DILocalVariable>(N);
if (!DV)
return;
if (!NodesSeen.insert(DV).second)
return;
processScope(DV->getScope());
processType(DV->getType().resolve(TypeIdentifierMap));
}
bool DebugInfoFinder::addType(DIType *DT) {
if (!DT)
return false;
if (!NodesSeen.insert(DT).second)
return false;
TYs.push_back(const_cast<DIType *>(DT));
return true;
}
bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
if (!CU)
return false;
if (!NodesSeen.insert(CU).second)
return false;
CUs.push_back(CU);
return true;
}
bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
if (!DIG)
return false;
if (!NodesSeen.insert(DIG).second)
return false;
GVs.push_back(DIG);
return true;
}
bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
if (!SP)
return false;
if (!NodesSeen.insert(SP).second)
return false;
SPs.push_back(SP);
return true;
}
bool DebugInfoFinder::addScope(DIScope *Scope) {
if (!Scope)
return false;
if (Scope->getNumOperands() == 0)
return false;
if (!NodesSeen.insert(Scope).second)
return false;
Scopes.push_back(Scope);
return true;
}
bool llvm::stripDebugInfo(Function &F) {
bool Changed = false;
if (F.getSubprogram()) {
Changed = true;
F.setSubprogram(nullptr);
}
for (BasicBlock &BB : F) {
for (Instruction &I : BB) {
if (I.getDebugLoc()) {
Changed = true;
I.setDebugLoc(DebugLoc());
}
}
}
return Changed;
}
bool llvm::StripDebugInfo(Module &M) {
bool Changed = false;
if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
while (!Declare->use_empty()) {
CallInst *CI = cast<CallInst>(Declare->user_back());
CI->eraseFromParent();
}
Declare->eraseFromParent();
Changed = true;
}
if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
while (!DbgVal->use_empty()) {
CallInst *CI = cast<CallInst>(DbgVal->user_back());
CI->eraseFromParent();
}
DbgVal->eraseFromParent();
Changed = true;
}
for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
NME = M.named_metadata_end(); NMI != NME;) {
NamedMDNode *NMD = &*NMI;
++NMI;
if (NMD->getName().startswith("llvm.dbg.")) {
NMD->eraseFromParent();
Changed = true;
}
}
for (Function &F : M)
Changed |= stripDebugInfo(F);
if (GVMaterializer *Materializer = M.getMaterializer())
Materializer->setStripDebugInfo();
return Changed;
}
class GLTOMapper {
DenseMap<Metadata *, Metadata *> Replacements;
public:
MDNode *EmptyNode;
MDNode *EmptySubroutineType;
private:
DenseMap<DISubprogram *, StringRef> NewToLinkageName;
public:
GLTOMapper(LLVMContext &C)
: EmptyNode(MDNode::get(C, {})),
EmptySubroutineType(DISubroutineType::get(C, 0, EmptyNode)) {}
Metadata *map(Metadata *M) {
if (!M)
return nullptr;
if (Replacements.count(M))
return Replacements.lookup(M);
return M;
}
MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
void traverseAndRemap(MDNode *N) { traverse(N); }
private:
DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
auto FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
auto LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
DISubprogram *Declaration = nullptr;
auto Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
auto ContainingType = map(MDS->getContainingType());
auto Variables = cast<MDTuple>(EmptyNode);
auto TemplateParams = nullptr;
auto distinctMDSubprogram = [&]() {
return DISubprogram::getDistinct(
MDS->getContext(), FileAndScope->getRef(), MDS->getName(),
LinkageName, FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
MDS->isDefinition(), MDS->getScopeLine(), DITypeRef(ContainingType),
MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getFlags(),
MDS->isOptimized(), TemplateParams, Declaration, Variables);
};
if (MDS->isDistinct())
return distinctMDSubprogram();
auto NewMDS = DISubprogram::get(
MDS->getContext(), FileAndScope->getRef(), MDS->getName(), LinkageName,
FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
MDS->isDefinition(), MDS->getScopeLine(), DITypeRef(ContainingType),
MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getFlags(),
MDS->isOptimized(), TemplateParams, Declaration, Variables);
auto OldLinkageName = MDS->getLinkageName();
if (NewToLinkageName.count(NewMDS)) {
if (NewToLinkageName.lookup(NewMDS) == OldLinkageName)
return NewMDS;
return distinctMDSubprogram();
}
NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
return NewMDS;
}
DICompileUnit *getReplacementCU(DICompileUnit *CU) {
if (CU->getDWOId())
return nullptr;
auto EmissionKind = 2; auto *File = cast_or_null<DIFile>(map(CU->getFile()));
MDTuple *EnumTypes = nullptr;
MDTuple *RetainedTypes = nullptr;
auto *Subprograms = cast_or_null<MDTuple>(map(CU->getSubprograms().get()));
MDTuple *GlobalVariables = nullptr;
MDTuple *ImportedEntities = nullptr;
return DICompileUnit::getDistinct(
CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
CU->getSplitDebugFilename(), EmissionKind, EnumTypes, RetainedTypes,
Subprograms, GlobalVariables, ImportedEntities, CU->getDWOId());
}
DILocation *getReplacementMDLocation(DILocation *MLD) {
auto Scope = map(MLD->getScope());
auto InlinedAt = map(MLD->getInlinedAt());
if (MLD->isDistinct())
return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
MLD->getColumn(), Scope, InlinedAt);
return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
Scope, InlinedAt);
}
MDNode *getReplacementMDNode(MDNode *N) {
SmallVector<Metadata *, 8> Ops;
Ops.reserve(N->getNumOperands());
for (auto &I : N->operands())
if (I)
Ops.push_back(map(I));
auto Ret = MDNode::get(N->getContext(), Ops);
return Ret;
}
void remap(MDNode *N) {
if (Replacements.count(N))
return;
auto doRemap = [&](MDNode * N) -> MDNode * {
if (!N)
return nullptr;
if (auto MDSub = dyn_cast<DISubprogram>(N))
return getReplacementSubprogram(MDSub);
if (isa<DISubroutineType>(N))
return EmptySubroutineType;
if (auto CU = dyn_cast<DICompileUnit>(N))
return getReplacementCU(CU);
if (isa<DIFile>(N))
return N;
if (auto MDLB = dyn_cast<DILexicalBlockBase>(N))
return mapNode(MDLB->getScope());
if (auto MLD = dyn_cast<DILocation>(N))
return getReplacementMDLocation(MLD);
if (isa<DINode>(N))
return EmptyNode;
return getReplacementMDNode(N);
};
Replacements[N] = doRemap(N);
}
void traverse(MDNode *);
};
void GLTOMapper::traverse(MDNode *N) {
if (!N || Replacements.count(N))
return;
auto prune = [](MDNode *Parent, MDNode *Child) {
if (auto MDS = dyn_cast<DISubprogram>(Parent))
return Child == MDS->getVariables().get();
return false;
};
SmallVector<MDNode *, 16> ToVisit;
DenseSet<MDNode *> Opened;
ToVisit.push_back(N);
while (!ToVisit.empty()) {
auto N = ToVisit.back();
if (Opened.count(N)) {
remap(N);
ToVisit.pop_back();
continue;
}
Opened.insert(N);
for (auto &I : N->operands())
if (auto MDN = dyn_cast_or_null<MDNode>(I))
if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN))
ToVisit.push_back(MDN);
}
}
bool llvm::convertDebugInfoToLineTables(Module &M) {
bool Changed = false;
if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
while (!Declare->use_empty()) {
auto *DDI = cast<DbgDeclareInst>(Declare->user_back());
DDI->eraseFromParent();
}
Declare->eraseFromParent();
Changed = true;
}
if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
while (!DbgVal->use_empty()) {
auto *DVI = cast<DbgValueInst>(DbgVal->user_back());
DVI->eraseFromParent();
}
DbgVal->eraseFromParent();
Changed = true;
}
for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
NME = M.named_metadata_end();
NMI != NME;) {
NamedMDNode *NMD = &*NMI;
++NMI;
if (NMD->getName() == "llvm.dbg.cu")
continue;
if (NMD->getName().startswith("llvm.dbg.")) {
NMD->eraseFromParent();
Changed = true;
}
}
GLTOMapper Mapper(M.getContext());
for (auto &F : M) {
auto *SP = F.getSubprogram();
if (SP) {
Mapper.traverseAndRemap(SP);
auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
Changed |= SP != NewSP;
F.setSubprogram(NewSP);
}
for (auto &BB : F) {
for (auto &I : BB) {
if (I.getDebugLoc() == DebugLoc())
continue;
auto &DL = I.getDebugLoc();
auto Scope = DL.getScope();
auto InlinedAt = DL.getInlinedAt();
if (Scope) {
Mapper.traverseAndRemap(Scope);
auto NewScope = Mapper.mapNode(Scope);
Changed |= Scope != NewScope;
Scope = NewScope;
}
if (InlinedAt) {
Mapper.traverseAndRemap(InlinedAt);
auto NewIA = Mapper.mapNode(InlinedAt);
Changed |= InlinedAt != NewIA;
InlinedAt = cast_or_null<DILocation>(NewIA);
}
I.setDebugLoc(
DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt));
}
}
}
for (auto &NMD : M.getNamedMDList()) {
SmallVector<MDNode *, 8> Ops;
for (MDNode *Op : NMD.operands()) {
Mapper.traverseAndRemap(Op);
auto *NewOp = Mapper.mapNode(Op);
Changed |= NewOp != Op;
Ops.push_back(NewOp);
}
if (Changed) {
NMD.dropAllReferences();
for (auto *Op : Ops)
if (Op)
NMD.addOperand(Op);
}
}
return Changed;
}
unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
M.getModuleFlag("Debug Info Version")))
return Val->getZExtValue();
return 0;
}