Add new helper pass that strips all symbol names except debugging information.

This pass makes it easier to test wheter debugging info. influences optimization passes or not.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@59552 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel 2008-11-18 21:34:39 +00:00
parent c99da1348d
commit f17fc461d1
3 changed files with 59 additions and 28 deletions

View File

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

View File

@ -33,6 +33,13 @@ class GlobalValue;
// //
ModulePass *createStripSymbolsPass(bool OnlyDebugInfo = false); ModulePass *createStripSymbolsPass(bool OnlyDebugInfo = false);
//===----------------------------------------------------------------------===//
//
// These functions removes symbols from functions and modules.
// Only debugging information is not removed.
//
ModulePass *createStripNonDebugSymbolsPass();
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// createLowerSetJmpPass - This function lowers the setjmp/longjmp intrinsics /// createLowerSetJmpPass - This function lowers the setjmp/longjmp intrinsics
/// to invoke/unwind instructions. This should really be part of the C/C++ /// to invoke/unwind instructions. This should really be part of the C/C++

View File

@ -40,13 +40,18 @@ namespace {
explicit StripSymbols(bool ODI = false) explicit StripSymbols(bool ODI = false)
: ModulePass(&ID), OnlyDebugInfo(ODI) {} : ModulePass(&ID), OnlyDebugInfo(ODI) {}
/// StripSymbolNames - Strip symbol names. virtual bool runOnModule(Module &M);
bool StripSymbolNames(Module &M);
// StripDebugInfo - Strip debug info in the module if it exists. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and AU.setPreservesAll();
// llvm.dbg.region.end calls, and any globals they point to if now dead. }
bool StripDebugInfo(Module &M); };
class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
public:
static char ID; // Pass identification, replacement for typeid
explicit StripNonDebugSymbols()
: ModulePass(&ID) {}
virtual bool runOnModule(Module &M); virtual bool runOnModule(Module &M);
@ -64,6 +69,14 @@ ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
return new StripSymbols(OnlyDebugInfo); return new StripSymbols(OnlyDebugInfo);
} }
char StripNonDebugSymbols::ID = 0;
static RegisterPass<StripNonDebugSymbols>
Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
ModulePass *llvm::createStripNonDebugSymbolsPass() {
return new StripNonDebugSymbols();
}
/// OnlyUsedBy - Return true if V is only used by Usr. /// OnlyUsedBy - Return true if V is only used by Usr.
static bool OnlyUsedBy(Value *V, Value *Usr) { static bool OnlyUsedBy(Value *V, Value *Usr) {
for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
@ -96,29 +109,27 @@ static void RemoveDeadConstant(Constant *C) {
// Strip the symbol table of its names. // Strip the symbol table of its names.
// //
static void StripSymtab(ValueSymbolTable &ST) { static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) { for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Value *V = VI->getValue(); Value *V = VI->getValue();
++VI; ++VI;
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) { if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
// Set name to "", removing from symbol table! // Set name to "", removing from symbol table!
V->setName(""); V->setName("");
} }
} }
} }
bool StripSymbols::runOnModule(Module &M) {
bool Changed = false;
Changed |= StripDebugInfo(M);
Changed |= StripSymbolNames(M);
return Changed;
}
// Strip the symbol table of its names. // Strip the symbol table of its names.
static void StripTypeSymtab(TypeSymbolTable &ST) { static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
++TI;
else
ST.remove(TI++); ST.remove(TI++);
} }
}
/// Find values that are marked as llvm.used. /// Find values that are marked as llvm.used.
void findUsedValues(Module &M, void findUsedValues(Module &M,
@ -143,10 +154,7 @@ void findUsedValues(Module &M,
} }
/// StripSymbolNames - Strip symbol names. /// StripSymbolNames - Strip symbol names.
bool StripSymbols::StripSymbolNames(Module &M) { bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
if (OnlyDebugInfo)
return false;
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues; SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
findUsedValues(M, llvmUsedValues); findUsedValues(M, llvmUsedValues);
@ -154,17 +162,19 @@ bool StripSymbols::StripSymbolNames(Module &M) {
for (Module::global_iterator I = M.global_begin(), E = M.global_end(); for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) { I != E; ++I) {
if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0) if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
I->setName(""); // Internal symbols can't participate in linkage I->setName(""); // Internal symbols can't participate in linkage
} }
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0) if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
I->setName(""); // Internal symbols can't participate in linkage I->setName(""); // Internal symbols can't participate in linkage
StripSymtab(I->getValueSymbolTable()); StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
} }
// Remove all names from types. // Remove all names from types.
StripTypeSymtab(M.getTypeSymbolTable()); StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
return true; return true;
} }
@ -172,7 +182,7 @@ bool StripSymbols::StripSymbolNames(Module &M) {
// StripDebugInfo - Strip debug info in the module if it exists. // StripDebugInfo - Strip debug info in the module if it exists.
// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
// llvm.dbg.region.end calls, and any globals they point to if now dead. // llvm.dbg.region.end calls, and any globals they point to if now dead.
bool StripSymbols::StripDebugInfo(Module &M) { bool StripDebugInfo(Module &M) {
Function *FuncStart = M.getFunction("llvm.dbg.func.start"); Function *FuncStart = M.getFunction("llvm.dbg.func.start");
Function *StopPoint = M.getFunction("llvm.dbg.stoppoint"); Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
@ -302,3 +312,16 @@ bool StripSymbols::StripDebugInfo(Module &M) {
return true; return true;
} }
bool StripSymbols::runOnModule(Module &M) {
bool Changed = false;
Changed |= StripDebugInfo(M);
if (!OnlyDebugInfo)
Changed |= StripSymbolNames(M, false);
return Changed;
}
bool StripNonDebugSymbols::runOnModule(Module &M) {
return StripSymbolNames(M, true);
}