Debugging infomration is encoded in llvm IR using metadata. This is designed

such a way that debug info for symbols preserved even if symbols are
optimized away by the optimizer. 

Add new special pass to remove debug info for such symbols.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@107416 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel 2010-07-01 19:49:20 +00:00
parent e62b203570
commit 26d14294de
4 changed files with 154 additions and 0 deletions

View File

@ -113,6 +113,7 @@ namespace {
(void) llvm::createSingleLoopExtractorPass();
(void) llvm::createStripSymbolsPass();
(void) llvm::createStripNonDebugSymbolsPass();
(void) llvm::createStripDeadDebugInfoPass();
(void) llvm::createStripDeadPrototypesPass();
(void) llvm::createTailCallEliminationPass();
(void) llvm::createTailDuplicationPass();

View File

@ -44,6 +44,11 @@ ModulePass *createStripNonDebugSymbolsPass();
// These pass removes llvm.dbg.declare intrinsics.
ModulePass *createStripDebugDeclarePass();
//===----------------------------------------------------------------------===//
//
// These pass removes unused symbols' debug info.
ModulePass *createStripDeadDebugInfoPass();
//===----------------------------------------------------------------------===//
/// createLowerSetJmpPass - This function lowers the setjmp/longjmp intrinsics
/// to invoke/unwind instructions. This should really be part of the C/C++

View File

@ -73,6 +73,19 @@ namespace {
AU.setPreservesAll();
}
};
class StripDeadDebugInfo : public ModulePass {
public:
static char ID; // Pass identification, replacement for typeid
explicit StripDeadDebugInfo()
: ModulePass(&ID) {}
virtual bool runOnModule(Module &M);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
}
};
}
char StripSymbols::ID = 0;
@ -99,6 +112,14 @@ ModulePass *llvm::createStripDebugDeclarePass() {
return new StripDebugDeclare();
}
char StripDeadDebugInfo::ID = 0;
static RegisterPass<StripDeadDebugInfo>
A("strip-dead-debug-info", "Strip debug info for unused symbols");
ModulePass *llvm::createStripDeadDebugInfoPass() {
return new StripDeadDebugInfo();
}
/// OnlyUsedBy - Return true if V is only used by Usr.
static bool OnlyUsedBy(Value *V, Value *Usr) {
for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
@ -295,3 +316,83 @@ bool StripDebugDeclare::runOnModule(Module &M) {
return true;
}
/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
/// printer to not emit usual symbol prefix before the symbol name is used then
/// return linkage name after skipping this special LLVM prefix.
static StringRef getRealLinkageName(StringRef LinkageName) {
char One = '\1';
if (LinkageName.startswith(StringRef(&One, 1)))
return LinkageName.substr(1);
return LinkageName;
}
bool StripDeadDebugInfo::runOnModule(Module &M) {
bool Changed = false;
// Debugging infomration is encoded in llvm IR using metadata. This is designed
// such a way that debug info for symbols preserved even if symbols are
// optimized away by the optimizer. This special pass removes debug info for
// such symbols.
// llvm.dbg.gv keeps track of debug info for global variables.
if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
SmallVector<MDNode *, 8> MDs;
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
if (DIGlobalVariable(NMD->getOperand(i)).Verify())
MDs.push_back(NMD->getOperand(i));
else
Changed = true;
NMD->eraseFromParent();
NMD = NULL;
for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
E = MDs.end(); I != E; ++I) {
if (M.getGlobalVariable(DIGlobalVariable(*I).getGlobal()->getName(),
true)) {
if (!NMD)
NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
NMD->addOperand(*I);
}
else
Changed = true;
}
}
// llvm.dbg.sp keeps track of debug info for subprograms.
if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
SmallVector<MDNode *, 8> MDs;
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
if (DISubprogram(NMD->getOperand(i)).Verify())
MDs.push_back(NMD->getOperand(i));
else
Changed = true;
NMD->eraseFromParent();
NMD = NULL;
for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
E = MDs.end(); I != E; ++I) {
bool FnIsLive = false;
if (Function *F = DISubprogram(*I).getFunction())
if (M.getFunction(F->getName()))
FnIsLive = true;
if (FnIsLive) {
if (!NMD)
NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
NMD->addOperand(*I);
} else {
// Remove llvm.dbg.lv.fnname named mdnode which may have been used
// to hold debug info for dead function's local variables.
StringRef FName = DISubprogram(*I).getLinkageName();
if (FName.empty())
FName = DISubprogram(*I).getName();
if (NamedMDNode *LVNMD =
M.getNamedMetadata(Twine("llvm.dbg.lv.",
getRealLinkageName(FName))))
LVNMD->eraseFromParent();
}
}
}
return Changed;
}

View File

@ -0,0 +1,47 @@
; RUN: opt -strip-dead-debug-info | llvm-dis -o %t.ll
; RUN: grep -v bar %t.ll
; RUN: grep -v abcd %t.ll
@xyz = global i32 2 ; <i32*> [#uses=1]
declare void @llvm.dbg.value(metadata, i64, metadata) nounwind readnone
define i32 @fn() nounwind readnone ssp {
entry:
ret i32 0, !dbg !17
}
define i32 @foo(i32 %i) nounwind readonly ssp {
entry:
tail call void @llvm.dbg.value(metadata !{i32 %i}, i64 0, metadata !14), !dbg !19
%.0 = load i32* @xyz, align 4 ; <i32> [#uses=1]
ret i32 %.0, !dbg !20
}
!llvm.dbg.sp = !{!0, !5, !9}
!llvm.dbg.lv.bar = !{!12}
!llvm.dbg.lv.foo = !{!14}
!llvm.dbg.gv = !{!15, !16}
!0 = metadata !{i32 524334, i32 0, metadata !1, metadata !"bar", metadata !"bar", metadata !"", metadata !1, i32 5, metadata !3, i1 true, i1 true, i32 0, i32 0, null, i1 false, i1 true, null} ; [ DW_TAG_subprogram ]
!1 = metadata !{i32 524329, metadata !"g.c", metadata !"/tmp/", metadata !2} ; [ DW_TAG_file_type ]
!2 = metadata !{i32 524305, i32 0, i32 1, metadata !"g.c", metadata !"/tmp/", metadata !"4.2.1 (Based on Apple Inc. build 5658) (LLVM build)", i1 true, i1 true, metadata !"", i32 0} ; [ DW_TAG_compile_unit ]
!3 = metadata !{i32 524309, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !4, i32 0, null} ; [ DW_TAG_subroutine_type ]
!4 = metadata !{null}
!5 = metadata !{i32 524334, i32 0, metadata !1, metadata !"fn", metadata !"fn", metadata !"fn", metadata !1, i32 6, metadata !6, i1 false, i1 true, i32 0, i32 0, null, i1 false, i1 true, i32 ()* @fn} ; [ DW_TAG_subprogram ]
!6 = metadata !{i32 524309, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !7, i32 0, null} ; [ DW_TAG_subroutine_type ]
!7 = metadata !{metadata !8}
!8 = metadata !{i32 524324, metadata !1, metadata !"int", metadata !1, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ]
!9 = metadata !{i32 524334, i32 0, metadata !1, metadata !"foo", metadata !"foo", metadata !"foo", metadata !1, i32 7, metadata !10, i1 false, i1 true, i32 0, i32 0, null, i1 false, i1 true, i32 (i32)* @foo} ; [ DW_TAG_subprogram ]
!10 = metadata !{i32 524309, metadata !1, metadata !"", metadata !1, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !11, i32 0, null} ; [ DW_TAG_subroutine_type ]
!11 = metadata !{metadata !8, metadata !8}
!12 = metadata !{i32 524544, metadata !13, metadata !"bb", metadata !1, i32 5, metadata !8} ; [ DW_TAG_auto_variable ]
!13 = metadata !{i32 524299, metadata !0, i32 5, i32 0} ; [ DW_TAG_lexical_block ]
!14 = metadata !{i32 524545, metadata !9, metadata !"i", metadata !1, i32 7, metadata !8} ; [ DW_TAG_arg_variable ]
!15 = metadata !{i32 524340, i32 0, metadata !1, metadata !"abcd", metadata !"abcd", metadata !"", metadata !1, i32 2, metadata !8, i1 true, i1 true, null} ; [ DW_TAG_variable ]
!16 = metadata !{i32 524340, i32 0, metadata !1, metadata !"xyz", metadata !"xyz", metadata !"", metadata !1, i32 3, metadata !8, i1 false, i1 true, i32* @xyz} ; [ DW_TAG_variable ]
!17 = metadata !{i32 6, i32 0, metadata !18, null}
!18 = metadata !{i32 524299, metadata !5, i32 6, i32 0} ; [ DW_TAG_lexical_block ]
!19 = metadata !{i32 7, i32 0, metadata !9, null}
!20 = metadata !{i32 10, i32 0, metadata !21, null}
!21 = metadata !{i32 524299, metadata !9, i32 7, i32 0} ; [ DW_TAG_lexical_block ]